



"""simple docstring"""







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


lowerCAmelCase_   						=   logging.get_logger(__name__)

# General docstring
lowerCAmelCase_   						=   'PoolFormerConfig'

# Base docstring
lowerCAmelCase_   						=   'sail/poolformer_s12'
lowerCAmelCase_   						=   [1, 512, 7, 7]

# Image classification docstring
lowerCAmelCase_   						=   'sail/poolformer_s12'
lowerCAmelCase_   						=   'tabby, tabby cat'

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



def      __UpperCAmelCase      (    __lowerCamelCase						,     __lowerCamelCase = 0.0						,     __lowerCamelCase = False	)							->      Optional[Any]:
		if drop_prob == 0.0 or not training:
				return input
		lowercase__   :    int				=			1 - drop_prob
		lowercase__   :    List[str]				=			(input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
		lowercase__   :    Union[str, Any]				=			keep_prob + torch.rand(__lowerCamelCase						,     dtype=input.dtype						,     device=input.device	)
		random_tensor.floor_()  # binarize
		lowercase__   :    int				=			input.div(__lowerCamelCase	) * random_tensor
		return output
class        __A       (	nn.Module    ):







		'''simple docstring'''





		def __init__(	self						:  str			,_snake_case						:  Optional[float] = None						)					->		None:






				"""simple docstring"""



				super().__init__()
				lowercase__   :    Dict				=			drop_prob







		def 					UpperCAmelCase    (	self						:  int			,_snake_case						:  torch.Tensor						)					->		torch.Tensor:






				"""simple docstring"""



				return drop_path(_snake_case			,self.drop_prob			,self.training						)







		def 					UpperCAmelCase    (	self						:  str						)					->		str:






				"""simple docstring"""



				return "p={}".format(self.drop_prob						)





class        __A       (	nn.Module    ):







		'''simple docstring'''





		def __init__(	self						:  Optional[Any]			,_snake_case						:  Tuple			,_snake_case						:  int			,_snake_case						:  List[str]			,_snake_case						:  int			,_snake_case						:  Optional[Any]			,_snake_case						:  Optional[int]=None						)					->		Any:






				"""simple docstring"""



				super().__init__()
				lowercase__   :    List[str]				=			patch_size if isinstance(_snake_case			,collections.abc.Iterable						) else (patch_size, patch_size)
				lowercase__   :    str				=			stride if isinstance(_snake_case			,collections.abc.Iterable						) else (stride, stride)
				lowercase__   :    Union[str, Any]				=			padding if isinstance(_snake_case			,collections.abc.Iterable						) else (padding, padding)

				lowercase__   :    int				=			nn.Convad(_snake_case			,_snake_case			,kernel_size=_snake_case			,stride=_snake_case			,padding=_snake_case						)
				lowercase__   :    int				=			norm_layer(_snake_case						) if norm_layer else nn.Identity()







		def 					UpperCAmelCase    (	self						:  Any			,_snake_case						:  int						)					->		List[Any]:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			self.projection(_snake_case						)
				lowercase__   :    Tuple				=			self.norm(_snake_case						)
				return embeddings





class        __A       (	nn.GroupNorm    ):







		'''simple docstring'''





		def __init__(	self						:  Tuple			,_snake_case						:  str			,**_snake_case						:  Tuple						)					->		List[str]:






				"""simple docstring"""



				super().__init__(1			,_snake_case			,**_snake_case						)





class        __A       (	nn.Module    ):







		'''simple docstring'''





		def __init__(	self						:  Optional[Any]			,_snake_case						:  Optional[Any]						)					->		str:






				"""simple docstring"""



				super().__init__()
				lowercase__   :    Union[str, Any]				=			nn.AvgPoolad(_snake_case			,stride=1			,padding=pool_size // 2			,count_include_pad=_snake_case						)







		def 					UpperCAmelCase    (	self						:  Optional[int]			,_snake_case						:  Optional[int]						)					->		Any:






				"""simple docstring"""



				return self.pool(_snake_case						) - hidden_states





class        __A       (	nn.Module    ):







		'''simple docstring'''





		def __init__(	self						:  Any			,_snake_case						:  Tuple			,_snake_case						:  Optional[Any]			,_snake_case						:  Tuple			,_snake_case						:  List[Any]						)					->		List[str]:






				"""simple docstring"""



				super().__init__()
				lowercase__   :    Dict				=			nn.Convad(_snake_case			,_snake_case			,1						)
				lowercase__   :    Any				=			nn.Convad(_snake_case			,_snake_case			,1						)
				lowercase__   :    int				=			PoolFormerDropPath(_snake_case						)
				if isinstance(config.hidden_act			,_snake_case						):
						lowercase__   :    List[Any]				=			ACTaFN[config.hidden_act]
				else:
						lowercase__   :    Dict				=			config.hidden_act







		def 					UpperCAmelCase    (	self						:  Dict			,_snake_case						:  Optional[int]						)					->		str:






				"""simple docstring"""



				lowercase__   :    Optional[int]				=			self.conva(_snake_case						)
				lowercase__   :    Any				=			self.act_fn(_snake_case						)
				lowercase__   :    Union[str, Any]				=			self.drop(_snake_case						)
				lowercase__   :    int				=			self.conva(_snake_case						)
				lowercase__   :    str				=			self.drop(_snake_case						)

				return hidden_states





class        __A       (	nn.Module    ):







		'''simple docstring'''





		def __init__(	self						:  Any			,_snake_case						:  List[str]			,_snake_case						:  str			,_snake_case						:  Any			,_snake_case						:  Dict			,_snake_case						:  List[str]			,_snake_case						:  Union[str, Any]						)					->		Tuple:






				"""simple docstring"""



				super().__init__()
				lowercase__   :    List[Any]				=			PoolFormerPooling(_snake_case						)
				lowercase__   :    int				=			PoolFormerOutput(_snake_case			,_snake_case			,_snake_case			,_snake_case						)
				lowercase__   :    str				=			PoolFormerGroupNorm(_snake_case						)
				lowercase__   :    Optional[Any]				=			PoolFormerGroupNorm(_snake_case						)

				# Useful for training neural nets
				lowercase__   :    Optional[Any]				=			PoolFormerDropPath(_snake_case						) if drop_path > 0.0 else nn.Identity()
				lowercase__   :    str				=			config.use_layer_scale
				if config.use_layer_scale:
						lowercase__   :    Optional[int]				=			nn.Parameter(
						    config.layer_scale_init_value * torch.ones((_snake_case)						)			,requires_grad=_snake_case						)
						lowercase__   :    List[Any]				=			nn.Parameter(
						    config.layer_scale_init_value * torch.ones((_snake_case)						)			,requires_grad=_snake_case						)







		def 					UpperCAmelCase    (	self						:  Union[str, Any]			,_snake_case						:  List[str]						)					->		Any:






				"""simple docstring"""



				if self.use_layer_scale:
						lowercase__   :    List[str]				=			self.pooling(self.before_norm(_snake_case						)						)
						lowercase__   :    Tuple				=			self.layer_scale_a.unsqueeze(-1						).unsqueeze(-1						) * pooling_output
						# First residual connection
						lowercase__   :    Any				=			hidden_states + self.drop_path(_snake_case						)
						lowercase__   :    int				=			()

						lowercase__   :    List[str]				=			self.output(self.after_norm(_snake_case						)						)
						lowercase__   :    Optional[int]				=			self.layer_scale_a.unsqueeze(-1						).unsqueeze(-1						) * layer_output
						# Second residual connection
						lowercase__   :    Optional[int]				=			hidden_states + self.drop_path(_snake_case						)

						lowercase__   :    Optional[int]				=			(output,) + outputs
						return outputs

				else:
						lowercase__   :    Any				=			self.drop_path(self.pooling(self.before_norm(_snake_case						)						)						)
						# First residual connection
						lowercase__   :    Dict				=			pooling_output + hidden_states
						lowercase__   :    Tuple				=			()

						# Second residual connection inside the PoolFormerOutput block
						lowercase__   :    Union[str, Any]				=			self.drop_path(self.output(self.after_norm(_snake_case						)						)						)
						lowercase__   :    Optional[Any]				=			hidden_states + layer_output

						lowercase__   :    Dict				=			(output,) + outputs
						return outputs





class        __A       (	nn.Module    ):







		'''simple docstring'''





		def __init__(	self						:  List[Any]			,_snake_case						:  List[Any]						)					->		Dict:






				"""simple docstring"""



				super().__init__()
				lowercase__   :    Union[str, Any]				=			config
				# stochastic depth decay rule
				lowercase__   :    List[Any]				=			[x.item() for x in torch.linspace(0			,config.drop_path_rate			,sum(config.depths						)						)]

				# patch embeddings
				lowercase__   :    Any				=			[]
				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]			,)						)
				lowercase__   :    Optional[Any]				=			nn.ModuleList(_snake_case						)

				# Transformer blocks
				lowercase__   :    str				=			[]
				lowercase__   :    Any				=			0
				for i in range(config.num_encoder_blocks						):
						# each block consists of layers
						lowercase__   :    int				=			[]
						if i != 0:
								cur += config.depths[i - 1]
						for j in range(config.depths[i]						):
								layers.append(
								    PoolFormerLayer(
								        _snake_case			,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(_snake_case						)						)

				lowercase__   :    Tuple				=			nn.ModuleList(_snake_case						)







		def 					UpperCAmelCase    (	self						:  str			,_snake_case						:  Tuple			,_snake_case						:  List[Any]=False			,_snake_case						:  Union[str, Any]=True						)					->		List[str]:






				"""simple docstring"""



				lowercase__   :    List[str]				=			() if output_hidden_states else None

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

						if output_hidden_states:
								lowercase__   :    Tuple				=			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=_snake_case			,hidden_states=_snake_case						)





class        __A       (	A_    ):







		'''simple docstring'''





		lowerCAmelCase			:					Any   	=				PoolFormerConfig
		lowerCAmelCase			:					List[str]   	=				"poolformer"
		lowerCAmelCase			:					int   	=				"pixel_values"
		lowerCAmelCase			:					int   	=				True







		def 					UpperCAmelCase    (	self						:  Union[str, Any]			,_snake_case						:  str						)					->		List[str]:






				"""simple docstring"""



				if isinstance(_snake_case			,(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(_snake_case			,nn.LayerNorm						):
						module.bias.data.zero_()
						module.weight.data.fill_(1.0						)







		def 					UpperCAmelCase    (	self						:  Optional[int]			,_snake_case						:  Optional[int]			,_snake_case						:  Optional[Any]=False						)					->		int:






				"""simple docstring"""



				if isinstance(_snake_case			,_snake_case						):
						lowercase__   :    Optional[int]				=			value





lowerCAmelCase_   						=   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'

lowerCAmelCase_   						=   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."     ,A_     ,)
class        __A       (	A_    ):







		'''simple docstring'''





		def __init__(	self						:  Union[str, Any]			,_snake_case						:  Any						)					->		List[str]:






				"""simple docstring"""



				super().__init__(_snake_case						)
				lowercase__   :    List[Any]				=			config

				lowercase__   :    Optional[int]				=			PoolFormerEncoder(_snake_case						)

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







		def 					UpperCAmelCase    (	self						:  str						)					->		str:






				"""simple docstring"""



				return self.embeddings.patch_embeddings







		@add_start_docstrings_to_model_forward(_snake_case						)
		@add_code_sample_docstrings(
		    checkpoint=_CHECKPOINT_FOR_DOC			,output_type=_snake_case			,config_class=_CONFIG_FOR_DOC			,modality='''vision'''			,expected_output=_EXPECTED_OUTPUT_SHAPE			,)
		def 					UpperCAmelCase    (	self						:  List[str]			,_snake_case						:  Optional[torch.FloatTensor] = None			,_snake_case						:  Optional[bool] = None			,_snake_case						:  Optional[bool] = None			,)					->		Union[Tuple, BaseModelOutputWithNoAttention]:






				"""simple docstring"""



				lowercase__   :    Optional[int]				=			(
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				lowercase__   :    Optional[Any]				=			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'''						)

				lowercase__   :    Tuple				=			self.encoder(
				    _snake_case			,output_hidden_states=_snake_case			,return_dict=_snake_case			,)
				lowercase__   :    Optional[Any]				=			encoder_outputs[0]

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

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





class        __A       (	nn.Module    ):







		'''simple docstring'''





		def __init__(	self						:  int			,_snake_case						:  Any						)					->		Union[str, Any]:






				"""simple docstring"""



				super().__init__()
				lowercase__   :    Dict				=			nn.Linear(config.hidden_size			,config.hidden_size						)







		def 					UpperCAmelCase    (	self						:  str			,_snake_case						:  Dict						)					->		Tuple:






				"""simple docstring"""



				lowercase__   :    List[Any]				=			self.dense(_snake_case						)
				return output





@add_start_docstrings(
    "\n    PoolFormer Model transformer with an image classification head on top\n    "     ,A_     ,)
class        __A       (	A_    ):







		'''simple docstring'''





		def __init__(	self						:  Tuple			,_snake_case						:  int						)					->		int:






				"""simple docstring"""



				super().__init__(_snake_case						)
				lowercase__   :    Optional[Any]				=			config.num_labels
				lowercase__   :    Optional[Any]				=			PoolFormerModel(_snake_case						)

				# Final norm
				lowercase__   :    Tuple				=			PoolFormerGroupNorm(config.hidden_sizes[-1]						)
				# Classifier head
				lowercase__   :    Any				=			(
				    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(_snake_case						)
		@add_code_sample_docstrings(
		    checkpoint=_IMAGE_CLASS_CHECKPOINT			,output_type=_snake_case			,config_class=_CONFIG_FOR_DOC			,expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,)
		def 					UpperCAmelCase    (	self						:  List[Any]			,_snake_case						:  Optional[torch.FloatTensor] = None			,_snake_case						:  Optional[torch.LongTensor] = None			,_snake_case						:  Optional[bool] = None			,_snake_case						:  Optional[bool] = None			,)					->		Union[Tuple, ImageClassifierOutputWithNoAttention]:






				"""simple docstring"""



				lowercase__   :    Optional[Any]				=			return_dict if return_dict is not None else self.config.use_return_dict

				lowercase__   :    Dict				=			self.poolformer(
				    _snake_case			,output_hidden_states=_snake_case			,return_dict=_snake_case			,)

				lowercase__   :    Dict				=			outputs[0]

				lowercase__   :    Optional[int]				=			self.classifier(self.norm(_snake_case						).mean([-2, -1]						)						)

				lowercase__   :    Tuple				=			None
				if labels is not None:
						if self.config.problem_type is None:
								if self.num_labels == 1:
										lowercase__   :    int				=			'''regression'''
								elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
										lowercase__   :    List[str]				=			'''single_label_classification'''
								else:
										lowercase__   :    Optional[int]				=			'''multi_label_classification'''

						if self.config.problem_type == "regression":
								lowercase__   :    List[Any]				=			MSELoss()
								if self.num_labels == 1:
										lowercase__   :    List[Any]				=			loss_fct(logits.squeeze()			,labels.squeeze()						)
								else:
										lowercase__   :    Tuple				=			loss_fct(_snake_case			,_snake_case						)
						elif self.config.problem_type == "single_label_classification":
								lowercase__   :    Any				=			CrossEntropyLoss()
								lowercase__   :    Union[str, Any]				=			loss_fct(logits.view(-1			,self.num_labels						)			,labels.view(-1						)						)
						elif self.config.problem_type == "multi_label_classification":
								lowercase__   :    str				=			BCEWithLogitsLoss()
								lowercase__   :    Dict				=			loss_fct(_snake_case			,_snake_case						)

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

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







from __future__ import annotations







def  _SCREAMING_SNAKE_CASE				(   a  )      ->      int:
					if not nums:
										return 0
					__A	:						Optional[int]       =				nums[0]
					__A	:						str       =				0
					for num in nums[1:]:
										__A    ,				__A	:						Tuple       =				(
										    max_excluding + num,
										    max(a					,						a  ),
										)
					return max(a					,						a  )


if __name__ == "__main__":
				import doctest

				doctest.testmod()




