

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


A__     :   Optional[int]						=							logging.get_logger(__name__)

# General docstring
A__     :   List[str]						=							"PoolFormerConfig"

# Base docstring
A__     :   Optional[int]						=							"sail/poolformer_s12"
A__     :   Optional[Any]						=							[1, 512, 7, 7]

# Image classification docstring
A__     :   Tuple						=							"sail/poolformer_s12"
A__     :   int						=							"tabby, tabby cat"

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




def 					_lowercase     (				a_				:					Any					,a_				:					float = 0.0					,a_				:					bool = False				)				->	str:




					'''simple docstring'''







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

class 				__UpperCamelCase				(					nn.Module			):







	def __init__(   self: Tuple  , __UpperCamelCase: Optional[float] = None      ):

						'''simple docstring'''





						super().__init__()
						__magic_name__							=    drop_prob







	def 	_SCREAMING_SNAKE_CASE							(   self: Union[str, Any]  , __UpperCamelCase: torch.Tensor      ):

						'''simple docstring'''





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







	def 	_SCREAMING_SNAKE_CASE							(   self: Dict      ):

						'''simple docstring'''





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




class 				__UpperCamelCase				(					nn.Module			):







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

						'''simple docstring'''





						super().__init__()
						__magic_name__							=    patch_size if isinstance(_a  , collections.abc.Iterable      ) else (patch_size, patch_size)
						__magic_name__							=    stride if isinstance(_a  , collections.abc.Iterable      ) else (stride, stride)
						__magic_name__							=    padding if isinstance(_a  , collections.abc.Iterable      ) else (padding, padding)

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







	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]  , __UpperCamelCase: int      ):

						'''simple docstring'''





						__magic_name__							=    self.projection(_a      )
						__magic_name__							=    self.norm(_a      )
						return embeddings




class 				__UpperCamelCase				(					nn.GroupNorm			):







	def __init__(   self: Union[str, Any]  , __UpperCamelCase: Optional[int]  , **__UpperCamelCase: Dict      ):

						'''simple docstring'''





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




class 				__UpperCamelCase				(					nn.Module			):







	def __init__(   self: Optional[Any]  , __UpperCamelCase: int      ):

						'''simple docstring'''





						super().__init__()
						__magic_name__							=    nn.AvgPoolad(_a  , stride=1  , padding=pool_size // 2  , count_include_pad=_a      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Dict  , __UpperCamelCase: Optional[int]      ):

						'''simple docstring'''





						return self.pool(_a      ) - hidden_states




class 				__UpperCamelCase				(					nn.Module			):







	def __init__(   self: Any  , __UpperCamelCase: List[str]  , __UpperCamelCase: Any  , __UpperCamelCase: int  , __UpperCamelCase: int      ):

						'''simple docstring'''





						super().__init__()
						__magic_name__							=    nn.Convad(_a  , _a  , 1      )
						__magic_name__							=    nn.Convad(_a  , _a  , 1      )
						__magic_name__							=    PoolFormerDropPath(_a      )
						if isinstance(config.hidden_act  , _a      ):
											__magic_name__							=    ACTaFN[config.hidden_act]
						else:
											__magic_name__							=    config.hidden_act







	def 	_SCREAMING_SNAKE_CASE							(   self: str  , __UpperCamelCase: Tuple      ):

						'''simple docstring'''





						__magic_name__							=    self.conva(_a      )
						__magic_name__							=    self.act_fn(_a      )
						__magic_name__							=    self.drop(_a      )
						__magic_name__							=    self.conva(_a      )
						__magic_name__							=    self.drop(_a      )

						return hidden_states




class 				__UpperCamelCase				(					nn.Module			):







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

						'''simple docstring'''





						super().__init__()
						__magic_name__							=    PoolFormerPooling(_a      )
						__magic_name__							=    PoolFormerOutput(_a  , _a  , _a  , _a      )
						__magic_name__							=    PoolFormerGroupNorm(_a      )
						__magic_name__							=    PoolFormerGroupNorm(_a      )

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







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[int]  , __UpperCamelCase: Any      ):

						'''simple docstring'''





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

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

											__magic_name__							=    (output,) + outputs
											return outputs

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

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

											__magic_name__							=    (output,) + outputs
											return outputs




class 				__UpperCamelCase				(					nn.Module			):







	def __init__(   self: Tuple  , __UpperCamelCase: Optional[Any]      ):

						'''simple docstring'''





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

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

						# Transformer blocks
						__magic_name__							=    []
						__magic_name__							=    0
						for i in range(config.num_encoder_blocks      ):
											# each block consists of layers
											__magic_name__							=    []
											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      )      )

						__magic_name__							=    nn.ModuleList(_a      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple  , __UpperCamelCase: List[str]  , __UpperCamelCase: List[Any]=False  , __UpperCamelCase: Dict=True      ):

						'''simple docstring'''





						__magic_name__							=    () if output_hidden_states else None

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

											if output_hidden_states:
																__magic_name__							=    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 				__UpperCamelCase				(					__SCREAMING_SNAKE_CASE			):
	_lowercase  :   Optional[int]       				=  PoolFormerConfig
	_lowercase  :   List[str]       				=  "poolformer"
	_lowercase  :   Union[str, Any]       				=  "pixel_values"
	_lowercase  :   int       				=  True







	def 	_SCREAMING_SNAKE_CASE							(   self: int  , __UpperCamelCase: List[str]      ):

						'''simple docstring'''





						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 	_SCREAMING_SNAKE_CASE							(   self: Any  , __UpperCamelCase: Optional[int]  , __UpperCamelCase: List[str]=False      ):

						'''simple docstring'''





						if isinstance(_a  , _a      ):
											__magic_name__							=    value




A__     :   List[Any]						=							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"

A__     :   Dict						=							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 				__UpperCamelCase				(					__SCREAMING_SNAKE_CASE			):







	def __init__(   self: str  , __UpperCamelCase: str      ):

						'''simple docstring'''





						super().__init__(_a      )
						__magic_name__							=    config

						__magic_name__							=    PoolFormerEncoder(_a      )

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







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[int]      ):

						'''simple docstring'''





						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 	_SCREAMING_SNAKE_CASE							(   self: List[str]  , __UpperCamelCase: Optional[torch.FloatTensor] = None  , __UpperCamelCase: Optional[bool] = None  , __UpperCamelCase: Optional[bool] = None  , ):

						'''simple docstring'''





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

						__magic_name__							=    self.encoder(
						    _a  , output_hidden_states=_a  , return_dict=_a  , )
						__magic_name__							=    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 				__UpperCamelCase				(					nn.Module			):







	def __init__(   self: Optional[int]  , __UpperCamelCase: str      ):

						'''simple docstring'''





						super().__init__()
						__magic_name__							=    nn.Linear(config.hidden_size  , config.hidden_size      )







	def 	_SCREAMING_SNAKE_CASE							(   self: str  , __UpperCamelCase: Any      ):

						'''simple docstring'''





						__magic_name__							=    self.dense(_a      )
						return output




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







	def __init__(   self: int  , __UpperCamelCase: List[str]      ):

						'''simple docstring'''





						super().__init__(_a      )
						__magic_name__							=    config.num_labels
						__magic_name__							=    PoolFormerModel(_a      )

						# Final norm
						__magic_name__							=    PoolFormerGroupNorm(config.hidden_sizes[-1]      )
						# Classifier head
						__magic_name__							=    (
						    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 	_SCREAMING_SNAKE_CASE							(   self: str  , __UpperCamelCase: Optional[torch.FloatTensor] = None  , __UpperCamelCase: Optional[torch.LongTensor] = None  , __UpperCamelCase: Optional[bool] = None  , __UpperCamelCase: Optional[bool] = None  , ):

						'''simple docstring'''





						__magic_name__							=    return_dict if return_dict is not None else self.config.use_return_dict

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

						__magic_name__							=    outputs[0]

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

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

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

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

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







from math import factorial, radians




def 					_lowercase     (				a_				:					float					,a_				:					int = 1_8					,a_				:					int = 1_0				)				->	float:




					'''simple docstring'''







					__magic_name__							=    angle_in_degrees - ((angle_in_degrees // 360.0) * 360.0)

					# Converting from degrees to radians
					__magic_name__							=    radians(a_				)

					__magic_name__							=    angle_in_radians
					__magic_name__							=    3
					__magic_name__							=    -1

					for _ in range(a_				):
										result += (b * (angle_in_radians**a)) / factorial(a_				)

										__magic_name__							=    -b  # One positive term and the next will be negative and so on...
										a += 2  # Increased by 2 for every term.

					return round(a_					,a_				)


if __name__ == "__main__":
						__import__("doctest").testmod()




