





'''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_ : Union[str, Any]	  =       logging.get_logger(__name__)

# General docstring
lowerCAmelCase_ : List[Any]	  =       "PoolFormerConfig"

# Base docstring
lowerCAmelCase_ : Dict	  =       "sail/poolformer_s12"
lowerCAmelCase_ : Tuple	  =       [1, 512, 7, 7]

# Image classification docstring
lowerCAmelCase_ : Optional[Any]	  =       "sail/poolformer_s12"
lowerCAmelCase_ : List[Any]	  =       "tabby, tabby cat"

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







def  _lowerCamelCase	(__lowerCamelCase	:  List[Any]       ,	__lowerCamelCase	:  float = 0.0       ,	__lowerCamelCase	:  bool = False  ) ->     str:
			if drop_prob == 0.0 or not training:
						return input
			a__	 =			1 - drop_prob
			a__	 =			(input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
			a__	 =			keep_prob + torch.rand(_UpperCamelCase       ,	dtype=input.dtype       ,	device=input.device  )
			random_tensor.floor_()  # binarize
			a__	 =			input.div(_UpperCamelCase  ) * random_tensor
			return output






class    UpperCamelCase__	(		nn.Module  ):







	def __init__(					self      : Union[str, Any]			,			lowerCamelCase      : List[str] = None		):




				'''simple docstring'''
				super().__init__()
				a__	 =			drop_prob







	def     __a (					self      : str			,			lowerCamelCase      : str		):




				'''simple docstring'''
				return drop_path(__UpperCAmelCase			,			self.drop_prob			,			self.training		)







	def     __a (					self      : List[str]		):




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






class    UpperCamelCase__	(		nn.Module  ):







	def __init__(					self      : Tuple			,			lowerCamelCase      : str			,			lowerCamelCase      : Optional[Any]			,			lowerCamelCase      : Tuple			,			lowerCamelCase      : Dict			,			lowerCamelCase      : List[Any]			,			lowerCamelCase      : Optional[int]=None		):




				'''simple docstring'''
				super().__init__()
				a__	 =			patch_size if isinstance(__UpperCAmelCase			,			collections.abc.Iterable		) else (patch_size, patch_size)
				a__	 =			stride if isinstance(__UpperCAmelCase			,			collections.abc.Iterable		) else (stride, stride)
				a__	 =			padding if isinstance(__UpperCAmelCase			,			collections.abc.Iterable		) else (padding, padding)

				a__	 =			nn.Convad(__UpperCAmelCase			,			__UpperCAmelCase			,			kernel_size=__UpperCAmelCase			,			stride=__UpperCAmelCase			,			padding=__UpperCAmelCase		)
				a__	 =			norm_layer(__UpperCAmelCase		) if norm_layer else nn.Identity()







	def     __a (					self      : List[Any]			,			lowerCamelCase      : Tuple		):




				'''simple docstring'''
				a__	 =			self.projection(__UpperCAmelCase		)
				a__	 =			self.norm(__UpperCAmelCase		)
				return embeddings






class    UpperCamelCase__	(		nn.GroupNorm  ):







	def __init__(					self      : List[Any]			,			lowerCamelCase      : int			,			**lowerCamelCase      : int		):




				'''simple docstring'''
				super().__init__(1			,			__UpperCAmelCase			,			**__UpperCAmelCase		)






class    UpperCamelCase__	(		nn.Module  ):







	def __init__(					self      : Optional[Any]			,			lowerCamelCase      : Dict		):




				'''simple docstring'''
				super().__init__()
				a__	 =			nn.AvgPoolad(__UpperCAmelCase			,			stride=1			,			padding=pool_size // 2			,			count_include_pad=__UpperCAmelCase		)







	def     __a (					self      : int			,			lowerCamelCase      : Dict		):




				'''simple docstring'''
				return self.pool(__UpperCAmelCase		) - hidden_states






class    UpperCamelCase__	(		nn.Module  ):







	def __init__(					self      : List[Any]			,			lowerCamelCase      : Optional[Any]			,			lowerCamelCase      : Any			,			lowerCamelCase      : Tuple			,			lowerCamelCase      : str		):




				'''simple docstring'''
				super().__init__()
				a__	 =			nn.Convad(__UpperCAmelCase			,			__UpperCAmelCase			,			1		)
				a__	 =			nn.Convad(__UpperCAmelCase			,			__UpperCAmelCase			,			1		)
				a__	 =			PoolFormerDropPath(__UpperCAmelCase		)
				if isinstance(config.hidden_act			,			__UpperCAmelCase		):
							a__	 =			ACTaFN[config.hidden_act]
				else:
							a__	 =			config.hidden_act







	def     __a (					self      : Dict			,			lowerCamelCase      : Tuple		):




				'''simple docstring'''
				a__	 =			self.conva(__UpperCAmelCase		)
				a__	 =			self.act_fn(__UpperCAmelCase		)
				a__	 =			self.drop(__UpperCAmelCase		)
				a__	 =			self.conva(__UpperCAmelCase		)
				a__	 =			self.drop(__UpperCAmelCase		)

				return hidden_states






class    UpperCamelCase__	(		nn.Module  ):







	def __init__(					self      : Any			,			lowerCamelCase      : Optional[Any]			,			lowerCamelCase      : List[Any]			,			lowerCamelCase      : str			,			lowerCamelCase      : List[Any]			,			lowerCamelCase      : Dict			,			lowerCamelCase      : Optional[Any]		):




				'''simple docstring'''
				super().__init__()
				a__	 =			PoolFormerPooling(__UpperCAmelCase		)
				a__	 =			PoolFormerOutput(__UpperCAmelCase			,			__UpperCAmelCase			,			__UpperCAmelCase			,			__UpperCAmelCase		)
				a__	 =			PoolFormerGroupNorm(__UpperCAmelCase		)
				a__	 =			PoolFormerGroupNorm(__UpperCAmelCase		)

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







	def     __a (					self      : Optional[int]			,			lowerCamelCase      : Tuple		):




				'''simple docstring'''
				if self.use_layer_scale:
							a__	 =			self.pooling(self.before_norm(__UpperCAmelCase		)		)
							a__	 =			self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * pooling_output
							# First residual connection
							a__	 =			hidden_states + self.drop_path(__UpperCAmelCase		)
							a__	 =			()

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

							a__	 =			(output,) + outputs
							return outputs

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

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

							a__	 =			(output,) + outputs
							return outputs






class    UpperCamelCase__	(		nn.Module  ):







	def __init__(					self      : Union[str, Any]			,			lowerCamelCase      : Union[str, Any]		):




				'''simple docstring'''
				super().__init__()
				a__	 =			config
				# stochastic depth decay rule
				a__	 =			[x.item() for x in torch.linspace(0			,			config.drop_path_rate			,			sum(config.depths		)		)]

				# patch embeddings
				a__	 =			[]
				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]			,			)		)
				a__	 =			nn.ModuleList(__UpperCAmelCase		)

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

				a__	 =			nn.ModuleList(__UpperCAmelCase		)







	def     __a (					self      : Optional[Any]			,			lowerCamelCase      : Optional[int]			,			lowerCamelCase      : Optional[Any]=False			,			lowerCamelCase      : Optional[Any]=True		):




				'''simple docstring'''
				a__	 =			() if output_hidden_states else None

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

							if output_hidden_states:
										a__	 =			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=__UpperCAmelCase			,			hidden_states=__UpperCAmelCase		)






class    UpperCamelCase__	(		lowerCAmelCase__  ):
	lowerCAmelCase__       :      Optional[int]										=    PoolFormerConfig
	lowerCAmelCase__       :      Any										=    "poolformer"
	lowerCAmelCase__       :      str										=    "pixel_values"
	lowerCAmelCase__       :      Dict										=    True







	def     __a (					self      : Any			,			lowerCamelCase      : Optional[int]		):




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







	def     __a (					self      : str			,			lowerCamelCase      : Optional[int]			,			lowerCamelCase      : Optional[Any]=False		):




				'''simple docstring'''
				if isinstance(__UpperCAmelCase			,			__UpperCAmelCase		):
							a__	 =			value





lowerCAmelCase_ : Tuple	  =       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_ : str	  =       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."						,lowerCAmelCase__						,)
class    UpperCamelCase__	(		lowerCAmelCase__  ):







	def __init__(					self      : int			,			lowerCamelCase      : Tuple		):




				'''simple docstring'''
				super().__init__(__UpperCAmelCase		)
				a__	 =			config

				a__	 =			PoolFormerEncoder(__UpperCAmelCase		)

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







	def     __a (					self      : Optional[int]		):




				'''simple docstring'''
				return self.embeddings.patch_embeddings







	@add_start_docstrings_to_model_forward(__UpperCAmelCase		)
	@add_code_sample_docstrings(
	    checkpoint=_CHECKPOINT_FOR_DOC			,			output_type=__UpperCAmelCase			,			config_class=_CONFIG_FOR_DOC			,			modality="vision"			,			expected_output=_EXPECTED_OUTPUT_SHAPE			,			)
	def     __a (					self      : Optional[Any]			,			lowerCamelCase      : str = None			,			lowerCamelCase      : str = None			,			lowerCamelCase      : Any = None			,			):




				'''simple docstring'''
				a__	 =			(
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				a__	 =			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"		)

				a__	 =			self.encoder(
				    __UpperCAmelCase			,			output_hidden_states=__UpperCAmelCase			,			return_dict=__UpperCAmelCase			,			)
				a__	 =			encoder_outputs[0]

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

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






class    UpperCamelCase__	(		nn.Module  ):







	def __init__(					self      : Union[str, Any]			,			lowerCamelCase      : List[Any]		):




				'''simple docstring'''
				super().__init__()
				a__	 =			nn.Linear(config.hidden_size			,			config.hidden_size		)







	def     __a (					self      : Tuple			,			lowerCamelCase      : Optional[Any]		):




				'''simple docstring'''
				a__	 =			self.dense(__UpperCAmelCase		)
				return output






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







	def __init__(					self      : str			,			lowerCamelCase      : Optional[Any]		):




				'''simple docstring'''
				super().__init__(__UpperCAmelCase		)
				a__	 =			config.num_labels
				a__	 =			PoolFormerModel(__UpperCAmelCase		)

				# Final norm
				a__	 =			PoolFormerGroupNorm(config.hidden_sizes[-1]		)
				# Classifier head
				a__	 =			(
				    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(__UpperCAmelCase		)
	@add_code_sample_docstrings(
	    checkpoint=_IMAGE_CLASS_CHECKPOINT			,			output_type=__UpperCAmelCase			,			config_class=_CONFIG_FOR_DOC			,			expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,			)
	def     __a (					self      : Any			,			lowerCamelCase      : str = None			,			lowerCamelCase      : List[Any] = None			,			lowerCamelCase      : List[str] = None			,			lowerCamelCase      : List[Any] = None			,			):




				'''simple docstring'''
				a__	 =			return_dict if return_dict is not None else self.config.use_return_dict

				a__	 =			self.poolformer(
				    __UpperCAmelCase			,			output_hidden_states=__UpperCAmelCase			,			return_dict=__UpperCAmelCase			,			)

				a__	 =			outputs[0]

				a__	 =			self.classifier(self.norm(__UpperCAmelCase		).mean([-2, -1]		)		)

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

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

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

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








from __future__ import annotations





def 		a__      (		_UpperCamelCase      :			list[float]				):
   if len(_UpperCamelCase				) < 2:
      raise ValueError('''Monogons and Digons are not polygons in the Euclidean space'''				)
   if any(i <= 0 for i in nums				):
      raise ValueError('''All values must be greater than 0'''				)
   __lowerCamelCase    				=     nums.copy()
   copy_nums.sort()
   return copy_nums[-1] < sum(copy_nums[:-1]				)


if __name__ == "__main__":
  import doctest

  doctest.testmod()






