




import collections.abc
from typing import Optional, Tuple, Union

import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_poolformer import PoolFormerConfig


__SCREAMING_SNAKE_CASE 						=logging.get_logger(__name__)

# General docstring
__SCREAMING_SNAKE_CASE 						="""PoolFormerConfig"""

# Base docstring
__SCREAMING_SNAKE_CASE 						="""sail/poolformer_s12"""
__SCREAMING_SNAKE_CASE 						=[1, 512, 7, 7]

# Image classification docstring
__SCREAMING_SNAKE_CASE 						="""sail/poolformer_s12"""
__SCREAMING_SNAKE_CASE 						="""tabby, tabby cat"""

__SCREAMING_SNAKE_CASE 						=[
    """sail/poolformer_s12""",
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]

def 			a   (_lowerCAmelCase  ,    _lowerCAmelCase = 0.0  ,    _lowerCAmelCase = False     ):
							if drop_prob == 0.0 or not training:
														return input
							SCREAMING_SNAKE_CASE_   	=    1 - drop_prob
							SCREAMING_SNAKE_CASE_   	=    (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
							SCREAMING_SNAKE_CASE_   	=    keep_prob + torch.rand(_lowerCAmelCase  ,    dtype=input.dtype  ,    device=input.device     )
							random_tensor.floor_()  # binarize
							SCREAMING_SNAKE_CASE_   	=    input.div(_lowerCAmelCase     ) * random_tensor
							return output






class  __magic_name__						(  nn.Module):
				'''simple docstring'''







				def __init__(							self:   Tuple		,					_lowerCamelCase:   Optional[float] = None		):
											super().__init__()
											SCREAMING_SNAKE_CASE_   	=    drop_prob


				def     _A							(							self:   Union[str, Any]		,					_lowerCamelCase:   torch.Tensor		):
											return drop_path(_a		,					self.drop_prob		,					self.training		)





				def     _A							(							self:   Dict		):
											return "p={}".format(self.drop_prob		)






class  __magic_name__						(  nn.Module):
				'''simple docstring'''







				def __init__(							self:   Any		,					_lowerCamelCase:   Dict		,					_lowerCamelCase:   Optional[int]		,					_lowerCamelCase:   Any		,					_lowerCamelCase:   Optional[int]		,					_lowerCamelCase:   Tuple		,					_lowerCamelCase:   str=None		):
											super().__init__()
											SCREAMING_SNAKE_CASE_   	=    patch_size if isinstance(_a		,					collections.abc.Iterable		) else (patch_size, patch_size)
											SCREAMING_SNAKE_CASE_   	=    stride if isinstance(_a		,					collections.abc.Iterable		) else (stride, stride)
											SCREAMING_SNAKE_CASE_   	=    padding if isinstance(_a		,					collections.abc.Iterable		) else (padding, padding)

											SCREAMING_SNAKE_CASE_   	=    nn.Convad(_a		,					_a		,					kernel_size=_a		,					stride=_a		,					padding=_a		)
											SCREAMING_SNAKE_CASE_   	=    norm_layer(_a		) if norm_layer else nn.Identity()





				def     _A							(							self:   List[str]		,					_lowerCamelCase:   int		):
											SCREAMING_SNAKE_CASE_   	=    self.projection(_a		)
											SCREAMING_SNAKE_CASE_   	=    self.norm(_a		)
											return embeddings






class  __magic_name__						(  nn.GroupNorm):
				'''simple docstring'''







				def __init__(							self:   Union[str, Any]		,					_lowerCamelCase:   Optional[int]		,					**_lowerCamelCase:   Dict		):
											super().__init__(1		,					_a		,					**_a		)






class  __magic_name__						(  nn.Module):
				'''simple docstring'''







				def __init__(							self:   Optional[Any]		,					_lowerCamelCase:   int		):
											super().__init__()
											SCREAMING_SNAKE_CASE_   	=    nn.AvgPoolad(_a		,					stride=1		,					padding=pool_size // 2		,					count_include_pad=_a		)





				def     _A							(							self:   Dict		,					_lowerCamelCase:   Optional[int]		):
											return self.pool(_a		) - hidden_states






class  __magic_name__						(  nn.Module):
				'''simple docstring'''







				def __init__(							self:   Any		,					_lowerCamelCase:   List[str]		,					_lowerCamelCase:   Any		,					_lowerCamelCase:   int		,					_lowerCamelCase:   int		):
											super().__init__()
											SCREAMING_SNAKE_CASE_   	=    nn.Convad(_a		,					_a		,					1		)
											SCREAMING_SNAKE_CASE_   	=    nn.Convad(_a		,					_a		,					1		)
											SCREAMING_SNAKE_CASE_   	=    PoolFormerDropPath(_a		)
											if isinstance(config.hidden_act		,					_a		):
																		SCREAMING_SNAKE_CASE_   	=    ACTaFN[config.hidden_act]
											else:
																		SCREAMING_SNAKE_CASE_   	=    config.hidden_act





				def     _A							(							self:   str		,					_lowerCamelCase:   Tuple		):
											SCREAMING_SNAKE_CASE_   	=    self.conva(_a		)
											SCREAMING_SNAKE_CASE_   	=    self.act_fn(_a		)
											SCREAMING_SNAKE_CASE_   	=    self.drop(_a		)
											SCREAMING_SNAKE_CASE_   	=    self.conva(_a		)
											SCREAMING_SNAKE_CASE_   	=    self.drop(_a		)

											return hidden_states






class  __magic_name__						(  nn.Module):
				'''simple docstring'''







				def __init__(							self:   List[Any]		,					_lowerCamelCase:   Any		,					_lowerCamelCase:   Tuple		,					_lowerCamelCase:   List[Any]		,					_lowerCamelCase:   Tuple		,					_lowerCamelCase:   Optional[Any]		,					_lowerCamelCase:   List[Any]		):
											super().__init__()
											SCREAMING_SNAKE_CASE_   	=    PoolFormerPooling(_a		)
											SCREAMING_SNAKE_CASE_   	=    PoolFormerOutput(_a		,					_a		,					_a		,					_a		)
											SCREAMING_SNAKE_CASE_   	=    PoolFormerGroupNorm(_a		)
											SCREAMING_SNAKE_CASE_   	=    PoolFormerGroupNorm(_a		)

											# Useful for training neural nets
											SCREAMING_SNAKE_CASE_   	=    PoolFormerDropPath(_a		) if drop_path > 0.0 else nn.Identity()
											SCREAMING_SNAKE_CASE_   	=    config.use_layer_scale
											if config.use_layer_scale:
																		SCREAMING_SNAKE_CASE_   	=    nn.Parameter(
																		    config.layer_scale_init_value * torch.ones((_a)		)		,					requires_grad=_a		)
																		SCREAMING_SNAKE_CASE_   	=    nn.Parameter(
																		    config.layer_scale_init_value * torch.ones((_a)		)		,					requires_grad=_a		)





				def     _A							(							self:   Optional[int]		,					_lowerCamelCase:   Any		):




											if self.use_layer_scale:
																		SCREAMING_SNAKE_CASE_   	=    self.pooling(self.before_norm(_a		)		)
																		SCREAMING_SNAKE_CASE_   	=    self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * pooling_output
																		# First residual connection
																		SCREAMING_SNAKE_CASE_   	=    hidden_states + self.drop_path(_a		)
																		SCREAMING_SNAKE_CASE_   	=    ()

																		SCREAMING_SNAKE_CASE_   	=    self.output(self.after_norm(_a		)		)
																		SCREAMING_SNAKE_CASE_   	=    self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * layer_output
																		# Second residual connection
																		SCREAMING_SNAKE_CASE_   	=    hidden_states + self.drop_path(_a		)

																		SCREAMING_SNAKE_CASE_   	=    (output,) + outputs
																		return outputs

											else:
																		SCREAMING_SNAKE_CASE_   	=    self.drop_path(self.pooling(self.before_norm(_a		)		)		)
																		# First residual connection
																		SCREAMING_SNAKE_CASE_   	=    pooling_output + hidden_states
																		SCREAMING_SNAKE_CASE_   	=    ()

																		# Second residual connection inside the PoolFormerOutput block
																		SCREAMING_SNAKE_CASE_   	=    self.drop_path(self.output(self.after_norm(_a		)		)		)
																		SCREAMING_SNAKE_CASE_   	=    hidden_states + layer_output

																		SCREAMING_SNAKE_CASE_   	=    (output,) + outputs
																		return outputs






class  __magic_name__						(  nn.Module):
				'''simple docstring'''







				def __init__(							self:   Tuple		,					_lowerCamelCase:   Optional[Any]		):
											super().__init__()
											SCREAMING_SNAKE_CASE_   	=    config
											# stochastic depth decay rule
											SCREAMING_SNAKE_CASE_   	=    [x.item() for x in torch.linspace(0		,					config.drop_path_rate		,					sum(config.depths		)		)]

											# patch embeddings
											SCREAMING_SNAKE_CASE_   	=    []
											for i in range(config.num_encoder_blocks		):
																		embeddings.append(
																		    PoolFormerEmbeddings(
																		        patch_size=config.patch_sizes[i]		,					stride=config.strides[i]		,					padding=config.padding[i]		,					num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1]		,					hidden_size=config.hidden_sizes[i]		,					)		)
											SCREAMING_SNAKE_CASE_   	=    nn.ModuleList(_a		)

											# Transformer blocks
											SCREAMING_SNAKE_CASE_   	=    []
											SCREAMING_SNAKE_CASE_   	=    0
											for i in range(config.num_encoder_blocks		):
																		# each block consists of layers
																		SCREAMING_SNAKE_CASE_   	=    []
																		if i != 0:
																									cur += config.depths[i - 1]
																		for j in range(config.depths[i]		):
																									layers.append(
																									    PoolFormerLayer(
																									        _a		,					num_channels=config.hidden_sizes[i]		,					pool_size=config.pool_size		,					hidden_size=config.hidden_sizes[i]		,					intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio		)		,					drop_path=dpr[cur + j]		,					)		)
																		blocks.append(nn.ModuleList(_a		)		)

											SCREAMING_SNAKE_CASE_   	=    nn.ModuleList(_a		)





				def     _A							(							self:   Tuple		,					_lowerCamelCase:   List[str]		,					_lowerCamelCase:   List[Any]=False		,					_lowerCamelCase:   Dict=True		):
											SCREAMING_SNAKE_CASE_   	=    () if output_hidden_states else None

											SCREAMING_SNAKE_CASE_   	=    pixel_values
											for idx, layers in enumerate(zip(self.patch_embeddings		,					self.block		)		):
																		SCREAMING_SNAKE_CASE_   	=    layers
																		# Get patch embeddings from hidden_states
																		SCREAMING_SNAKE_CASE_   	=    embedding_layer(_a		)
																		# Send the embeddings through the blocks
																		for _, blk in enumerate(_a		):
																									SCREAMING_SNAKE_CASE_   	=    blk(_a		)
																									SCREAMING_SNAKE_CASE_   	=    layer_outputs[0]

																		if output_hidden_states:
																									SCREAMING_SNAKE_CASE_   	=    all_hidden_states + (hidden_states,)

											if not return_dict:
																		return tuple(v for v in [hidden_states, all_hidden_states] if v is not None		)

											return BaseModelOutputWithNoAttention(last_hidden_state=_a		,					hidden_states=_a		)






class  __magic_name__						(  __SCREAMING_SNAKE_CASE):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							str	      =					PoolFormerConfig
				SCREAMING_SNAKE_CASE__					:							List[str]	      =					"poolformer"
				SCREAMING_SNAKE_CASE__					:							Optional[int]	      =					"pixel_values"
				SCREAMING_SNAKE_CASE__					:							Tuple	      =					True


				def     _A							(							self:   int		,					_lowerCamelCase:   List[str]		):




											if isinstance(_a		,					(nn.Linear, nn.Convad)		):
																		module.weight.data.normal_(mean=0.0		,					std=self.config.initializer_range		)
																		if module.bias is not None:
																									module.bias.data.zero_()
											elif isinstance(_a		,					nn.LayerNorm		):
																		module.bias.data.zero_()
																		module.weight.data.fill_(1.0		)





				def     _A							(							self:   Any		,					_lowerCamelCase:   Optional[int]		,					_lowerCamelCase:   List[str]=False		):




											if isinstance(_a		,					_a		):
																		SCREAMING_SNAKE_CASE_   	=    value






__SCREAMING_SNAKE_CASE 						=r"""\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n"""

__SCREAMING_SNAKE_CASE 						=r"""\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`PoolFormerImageProcessor.__call__`] for details.\n"""






@add_start_docstrings(
    "The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top."    ,  __SCREAMING_SNAKE_CASE    ,  )
class  __magic_name__						(  __SCREAMING_SNAKE_CASE):
				'''simple docstring'''







				def __init__(							self:   str		,					_lowerCamelCase:   str		):
											super().__init__(_a		)
											SCREAMING_SNAKE_CASE_   	=    config

											SCREAMING_SNAKE_CASE_   	=    PoolFormerEncoder(_a		)

											# Initialize weights and apply final processing
											self.post_init()


				def     _A							(							self:   Optional[int]		):
											return self.embeddings.patch_embeddings





				@add_start_docstrings_to_model_forward(_a		)
				@add_code_sample_docstrings(
				    checkpoint=_CHECKPOINT_FOR_DOC		,					output_type=_a		,					config_class=_CONFIG_FOR_DOC		,					modality='''vision'''		,					expected_output=_EXPECTED_OUTPUT_SHAPE		,					)
				def     _A							(							self:   List[str]		,					_lowerCamelCase:   Optional[torch.FloatTensor] = None		,					_lowerCamelCase:   Optional[bool] = None		,					_lowerCamelCase:   Optional[bool] = None		,					):
											SCREAMING_SNAKE_CASE_   	=    (
											    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
											)
											SCREAMING_SNAKE_CASE_   	=    return_dict if return_dict is not None else self.config.use_return_dict

											if pixel_values is None:
																		raise ValueError('''You have to specify pixel_values'''		)

											SCREAMING_SNAKE_CASE_   	=    self.encoder(
											    _a		,					output_hidden_states=_a		,					return_dict=_a		,					)
											SCREAMING_SNAKE_CASE_   	=    encoder_outputs[0]

											if not return_dict:
																		return (sequence_output, None) + encoder_outputs[1:]

											return BaseModelOutputWithNoAttention(
											    last_hidden_state=_a		,					hidden_states=encoder_outputs.hidden_states		,					)






class  __magic_name__						(  nn.Module):
				'''simple docstring'''







				def __init__(							self:   Optional[int]		,					_lowerCamelCase:   str		):
											super().__init__()
											SCREAMING_SNAKE_CASE_   	=    nn.Linear(config.hidden_size		,					config.hidden_size		)





				def     _A							(							self:   str		,					_lowerCamelCase:   Any		):
											SCREAMING_SNAKE_CASE_   	=    self.dense(_a		)
											return output






@add_start_docstrings(
    "\n    PoolFormer Model transformer with an image classification head on top\n    "    ,  __SCREAMING_SNAKE_CASE    ,  )
class  __magic_name__						(  __SCREAMING_SNAKE_CASE):
				'''simple docstring'''







				def __init__(							self:   int		,					_lowerCamelCase:   List[str]		):
											super().__init__(_a		)
											SCREAMING_SNAKE_CASE_   	=    config.num_labels
											SCREAMING_SNAKE_CASE_   	=    PoolFormerModel(_a		)

											# Final norm
											SCREAMING_SNAKE_CASE_   	=    PoolFormerGroupNorm(config.hidden_sizes[-1]		)
											# Classifier head
											SCREAMING_SNAKE_CASE_   	=    (
											    nn.Linear(config.hidden_sizes[-1]		,					config.num_labels		) if config.num_labels > 0 else nn.Identity()
											)

											# Initialize weights and apply final processing
											self.post_init()





				@add_start_docstrings_to_model_forward(_a		)
				@add_code_sample_docstrings(
				    checkpoint=_IMAGE_CLASS_CHECKPOINT		,					output_type=_a		,					config_class=_CONFIG_FOR_DOC		,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,					)
				def     _A							(							self:   str		,					_lowerCamelCase:   Optional[torch.FloatTensor] = None		,					_lowerCamelCase:   Optional[torch.LongTensor] = None		,					_lowerCamelCase:   Optional[bool] = None		,					_lowerCamelCase:   Optional[bool] = None		,					):
											SCREAMING_SNAKE_CASE_   	=    return_dict if return_dict is not None else self.config.use_return_dict

											SCREAMING_SNAKE_CASE_   	=    self.poolformer(
											    _a		,					output_hidden_states=_a		,					return_dict=_a		,					)

											SCREAMING_SNAKE_CASE_   	=    outputs[0]

											SCREAMING_SNAKE_CASE_   	=    self.classifier(self.norm(_a		).mean([-2, -1]		)		)

											SCREAMING_SNAKE_CASE_   	=    None
											if labels is not None:
																		if self.config.problem_type is None:
																									if self.num_labels == 1:
																																SCREAMING_SNAKE_CASE_   	=    """regression"""
																									elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																																SCREAMING_SNAKE_CASE_   	=    """single_label_classification"""
																									else:
																																SCREAMING_SNAKE_CASE_   	=    """multi_label_classification"""

																		if self.config.problem_type == "regression":
																									SCREAMING_SNAKE_CASE_   	=    MSELoss()
																									if self.num_labels == 1:
																																SCREAMING_SNAKE_CASE_   	=    loss_fct(logits.squeeze()		,					labels.squeeze()		)
																									else:
																																SCREAMING_SNAKE_CASE_   	=    loss_fct(_a		,					_a		)
																		elif self.config.problem_type == "single_label_classification":
																									SCREAMING_SNAKE_CASE_   	=    CrossEntropyLoss()
																									SCREAMING_SNAKE_CASE_   	=    loss_fct(logits.view(-1		,					self.num_labels		)		,					labels.view(-1		)		)
																		elif self.config.problem_type == "multi_label_classification":
																									SCREAMING_SNAKE_CASE_   	=    BCEWithLogitsLoss()
																									SCREAMING_SNAKE_CASE_   	=    loss_fct(_a		,					_a		)

											if not return_dict:
																		SCREAMING_SNAKE_CASE_   	=    (logits,) + outputs[2:]
																		return ((loss,) + output) if loss is not None else output

											return ImageClassifierOutputWithNoAttention(loss=_a		,					logits=_a		,					hidden_states=outputs.hidden_states		)












import webbrowser
from sys import argv
from urllib.parse import parse_qs, quote

import requests
from bsa import BeautifulSoup
from fake_useragent import UserAgent

if __name__ == "__main__":
			__SCREAMING_SNAKE_CASE 						="""%20""".join(argv[1:]) if len(argv) > 1 else quote(str(input("""Search: """)))

			print("""Googling.....""")

			__SCREAMING_SNAKE_CASE 						=f"""https://www.google.com/search?q={query}&num=100"""

			__SCREAMING_SNAKE_CASE 						=requests.get(
			    url,
			    headers={"""User-Agent""": str(UserAgent().random)},
			)

			try:
						__SCREAMING_SNAKE_CASE 						=(
						    BeautifulSoup(res.text, """html.parser""")
						    .find("""div""", attrs={"""class""": """yuRUbf"""})
						    .find("""a""")
						    .get("""href""")
						)

			except AttributeError:
						__SCREAMING_SNAKE_CASE 						=parse_qs(
						    BeautifulSoup(res.text, """html.parser""")
						    .find("""div""", attrs={"""class""": """kCrYT"""})
						    .find("""a""")
						    .get("""href""")
						)["""url"""][0]

			webbrowser.open(link)


