
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


lowercase						:      Optional[Any]									= logging.get_logger(__name__)

# General docstring
lowercase						:      Optional[int]									= 'PoolFormerConfig'

# Base docstring
lowercase						:      Tuple									= 'sail/poolformer_s12'
lowercase						:      Any									= [1, 512, 7, 7]

# Image classification docstring
lowercase						:      str									= 'sail/poolformer_s12'
lowercase						:      List[str]									= 'tabby, tabby cat'

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




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    Any		,						_lowerCamelCase				:    float = 0.0		,						_lowerCamelCase				:    bool = False)      ->					str:






							'''simple docstring'''





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




class 				lowerCamelCase__							(       nn.Module):




	'''simple docstring'''




	def __init__(      self	:Optional[Any]  ,     a	:Optional[float] = None		)	->				None:
								super().__init__()
								__UpperCamelCase			:     Union[str, Any]         =      drop_prob







	def      _lowerCamelCase       (      self	:Optional[Any]  ,     a	:torch.Tensor		)	->				torch.Tensor:
								return drop_path(a  ,     self.drop_prob  ,     self.training		)







	def      _lowerCamelCase       (      self	:int		)	->				str:
								return "p={}".format(self.drop_prob		)







class 				lowerCamelCase__							(       nn.Module):




	'''simple docstring'''




	def __init__(      self	:Dict  ,     a	:List[Any]  ,     a	:Dict  ,     a	:int  ,     a	:Union[str, Any]  ,     a	:int  ,     a	:List[str]=None		)	->				str:
								super().__init__()
								__UpperCamelCase			:     int         =      patch_size if isinstance(a  ,     collections.abc.Iterable		) else (patch_size, patch_size)
								__UpperCamelCase			:     str         =      stride if isinstance(a  ,     collections.abc.Iterable		) else (stride, stride)
								__UpperCamelCase			:     Dict         =      padding if isinstance(a  ,     collections.abc.Iterable		) else (padding, padding)

								__UpperCamelCase			:     Optional[int]         =      nn.Convad(a  ,     a  ,     kernel_size=a  ,     stride=a  ,     padding=a		)
								__UpperCamelCase			:     Optional[int]         =      norm_layer(a		) if norm_layer else nn.Identity()







	def      _lowerCamelCase       (      self	:List[Any]  ,     a	:Tuple		)	->				Optional[int]:
								__UpperCamelCase			:     Dict         =      self.projection(a		)
								__UpperCamelCase			:     str         =      self.norm(a		)
								return embeddings







class 				lowerCamelCase__							(       nn.GroupNorm):




	'''simple docstring'''




	def __init__(      self	:Dict  ,     a	:int  ,     **a	:Dict		)	->				List[str]:
								super().__init__(1  ,     a  ,     **a		)







class 				lowerCamelCase__							(       nn.Module):




	'''simple docstring'''




	def __init__(      self	:List[Any]  ,     a	:Optional[int]		)	->				Any:
								super().__init__()
								__UpperCamelCase			:     Any         =      nn.AvgPoolad(a  ,     stride=1  ,     padding=pool_size // 2  ,     count_include_pad=a		)







	def      _lowerCamelCase       (      self	:str  ,     a	:List[str]		)	->				List[str]:
								return self.pool(a		) - hidden_states







class 				lowerCamelCase__							(       nn.Module):




	'''simple docstring'''




	def __init__(      self	:Tuple  ,     a	:str  ,     a	:Optional[int]  ,     a	:List[str]  ,     a	:List[str]		)	->				Any:
								super().__init__()
								__UpperCamelCase			:     List[str]         =      nn.Convad(a  ,     a  ,     1		)
								__UpperCamelCase			:     Optional[int]         =      nn.Convad(a  ,     a  ,     1		)
								__UpperCamelCase			:     Dict         =      PoolFormerDropPath(a		)
								if isinstance(config.hidden_act  ,     a		):
															__UpperCamelCase			:     Tuple         =      ACTaFN[config.hidden_act]
								else:
															__UpperCamelCase			:     int         =      config.hidden_act







	def      _lowerCamelCase       (      self	:str  ,     a	:Dict		)	->				List[Any]:
								__UpperCamelCase			:     Dict         =      self.conva(a		)
								__UpperCamelCase			:     Union[str, Any]         =      self.act_fn(a		)
								__UpperCamelCase			:     str         =      self.drop(a		)
								__UpperCamelCase			:     List[Any]         =      self.conva(a		)
								__UpperCamelCase			:     Any         =      self.drop(a		)

								return hidden_states







class 				lowerCamelCase__							(       nn.Module):




	'''simple docstring'''




	def __init__(      self	:int  ,     a	:Union[str, Any]  ,     a	:Optional[Any]  ,     a	:int  ,     a	:int  ,     a	:List[str]  ,     a	:Any		)	->				List[str]:
								super().__init__()
								__UpperCamelCase			:     Dict         =      PoolFormerPooling(a		)
								__UpperCamelCase			:     Any         =      PoolFormerOutput(a  ,     a  ,     a  ,     a		)
								__UpperCamelCase			:     List[str]         =      PoolFormerGroupNorm(a		)
								__UpperCamelCase			:     Tuple         =      PoolFormerGroupNorm(a		)

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







	def      _lowerCamelCase       (      self	:Dict  ,     a	:Dict		)	->				List[str]:
								if self.use_layer_scale:
															__UpperCamelCase			:     int         =      self.pooling(self.before_norm(a		)		)
															__UpperCamelCase			:     int         =      self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * pooling_output
															# First residual connection
															__UpperCamelCase			:     str         =      hidden_states + self.drop_path(a		)
															__UpperCamelCase			:     Union[str, Any]         =      ()

															__UpperCamelCase			:     str         =      self.output(self.after_norm(a		)		)
															__UpperCamelCase			:     str         =      self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * layer_output
															# Second residual connection
															__UpperCamelCase			:     Any         =      hidden_states + self.drop_path(a		)

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

								else:
															__UpperCamelCase			:     Tuple         =      self.drop_path(self.pooling(self.before_norm(a		)		)		)
															# First residual connection
															__UpperCamelCase			:     List[Any]         =      pooling_output + hidden_states
															__UpperCamelCase			:     List[Any]         =      ()

															# Second residual connection inside the PoolFormerOutput block
															__UpperCamelCase			:     Optional[int]         =      self.drop_path(self.output(self.after_norm(a		)		)		)
															__UpperCamelCase			:     List[str]         =      hidden_states + layer_output

															__UpperCamelCase			:     str         =      (output,) + outputs
															return outputs







class 				lowerCamelCase__							(       nn.Module):




	'''simple docstring'''




	def __init__(      self	:int  ,     a	:str		)	->				Optional[int]:
								super().__init__()
								__UpperCamelCase			:     Tuple         =      config
								# stochastic depth decay rule
								__UpperCamelCase			:     Union[str, Any]         =      [x.item() for x in torch.linspace(0  ,     config.drop_path_rate  ,     sum(config.depths		)		)]

								# patch embeddings
								__UpperCamelCase			:     Union[str, 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]  ,     )		)
								__UpperCamelCase			:     Optional[Any]         =      nn.ModuleList(a		)

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

								__UpperCamelCase			:     List[str]         =      nn.ModuleList(a		)







	def      _lowerCamelCase       (      self	:Optional[int]  ,     a	:List[str]  ,     a	:Dict=False  ,     a	:int=True		)	->				int:
								__UpperCamelCase			:     Union[str, Any]         =      () if output_hidden_states else None

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

															if output_hidden_states:
																						__UpperCamelCase			:     Optional[Any]         =      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 				lowerCamelCase__							(       __lowercase):




	'''simple docstring'''




	_A			=  PoolFormerConfig
	_A			=  'poolformer'
	_A			=  'pixel_values'
	_A			=  True







	def      _lowerCamelCase       (      self	:Optional[Any]  ,     a	:Tuple		)	->				Optional[int]:
								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      _lowerCamelCase       (      self	:Tuple  ,     a	:Tuple  ,     a	:Any=False		)	->				List[Any]:
								if isinstance(a  ,     a		):
															__UpperCamelCase			:     List[str]         =      value







lowercase						:      Optional[int]									= 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'

lowercase						:      Optional[Any]									= 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.'  ,		__lowercase  ,		)
class 				lowerCamelCase__							(       __lowercase):




	'''simple docstring'''




	def __init__(      self	:Optional[int]  ,     a	:List[str]		)	->				Union[str, Any]:
								super().__init__(a		)
								__UpperCamelCase			:     Tuple         =      config

								__UpperCamelCase			:     int         =      PoolFormerEncoder(a		)

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







	def      _lowerCamelCase       (      self	:List[str]		)	->				Union[str, Any]:
								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      _lowerCamelCase       (      self	:List[str]  ,     a	:Optional[torch.FloatTensor] = None  ,     a	:Optional[bool] = None  ,     a	:Optional[bool] = None  ,     )	->				Union[Tuple, BaseModelOutputWithNoAttention]:
								__UpperCamelCase			:     Tuple         =      (
								    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
								)
								__UpperCamelCase			:     Optional[int]         =      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"		)

								__UpperCamelCase			:     Optional[int]         =      self.encoder(
								    a  ,     output_hidden_states=a  ,     return_dict=a  ,     )
								__UpperCamelCase			:     Optional[Any]         =      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 				lowerCamelCase__							(       nn.Module):




	'''simple docstring'''




	def __init__(      self	:Tuple  ,     a	:Optional[Any]		)	->				Optional[int]:
								super().__init__()
								__UpperCamelCase			:     Any         =      nn.Linear(config.hidden_size  ,     config.hidden_size		)







	def      _lowerCamelCase       (      self	:Optional[Any]  ,     a	:List[Any]		)	->				Optional[int]:
								__UpperCamelCase			:     Union[str, Any]         =      self.dense(a		)
								return output







@add_start_docstrings(
    '\n    PoolFormer Model transformer with an image classification head on top\n    '  ,		__lowercase  ,		)
class 				lowerCamelCase__							(       __lowercase):




	'''simple docstring'''




	def __init__(      self	:str  ,     a	:str		)	->				Any:
								super().__init__(a		)
								__UpperCamelCase			:     str         =      config.num_labels
								__UpperCamelCase			:     Optional[int]         =      PoolFormerModel(a		)

								# Final norm
								__UpperCamelCase			:     Optional[Any]         =      PoolFormerGroupNorm(config.hidden_sizes[-1]		)
								# Classifier head
								__UpperCamelCase			:     str         =      (
								    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      _lowerCamelCase       (      self	:List[Any]  ,     a	:Optional[torch.FloatTensor] = None  ,     a	:Optional[torch.LongTensor] = None  ,     a	:Optional[bool] = None  ,     a	:Optional[bool] = None  ,     )	->				Union[Tuple, ImageClassifierOutputWithNoAttention]:
								__UpperCamelCase			:     Dict         =      return_dict if return_dict is not None else self.config.use_return_dict

								__UpperCamelCase			:     Optional[Any]         =      self.poolformer(
								    a  ,     output_hidden_states=a  ,     return_dict=a  ,     )

								__UpperCamelCase			:     Optional[Any]         =      outputs[0]

								__UpperCamelCase			:     Any         =      self.classifier(self.norm(a		).mean([-2, -1]		)		)

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

															if self.config.problem_type == "regression":
																						__UpperCamelCase			:     int         =      MSELoss()
																						if self.num_labels == 1:
																													__UpperCamelCase			:     Any         =      loss_fct(logits.squeeze()  ,     labels.squeeze()		)
																						else:
																													__UpperCamelCase			:     Optional[Any]         =      loss_fct(a  ,     a		)
															elif self.config.problem_type == "single_label_classification":
																						__UpperCamelCase			:     str         =      CrossEntropyLoss()
																						__UpperCamelCase			:     Optional[int]         =      loss_fct(logits.view(-1  ,     self.num_labels		)  ,     labels.view(-1		)		)
															elif self.config.problem_type == "multi_label_classification":
																						__UpperCamelCase			:     List[Any]         =      BCEWithLogitsLoss()
																						__UpperCamelCase			:     Optional[int]         =      loss_fct(a  ,     a		)

								if not return_dict:
															__UpperCamelCase			:     int         =      (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 ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase						:      Dict									= logging.get_logger(__name__)

lowercase						:      Optional[Any]									= {
    'facebook/s2t-wav2vec2-large-en-de': (
        'https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json'
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}




class 				lowerCamelCase__							(       __lowercase):




	'''simple docstring'''




	_A			=  'speech_to_text_2'
	_A			=  ['past_key_values']
	_A			=  {'num_attention_heads': 'decoder_attention_heads', 'hidden_size': 'd_model'}







	def __init__(      self	:Dict  ,     a	:Tuple=1_0_0_0_0  ,     a	:Optional[int]=6  ,     a	:List[str]=2_0_4_8  ,     a	:Tuple=4  ,     a	:List[Any]=0.0  ,     a	:str=True  ,     a	:Any="relu"  ,     a	:Any=2_5_6  ,     a	:Optional[int]=0.1  ,     a	:Any=0.0  ,     a	:int=0.0  ,     a	:int=0.02  ,     a	:List[Any]=2  ,     a	:Tuple=True  ,     a	:str=1  ,     a	:Optional[int]=0  ,     a	:List[Any]=2  ,     a	:Any=1_0_2_4  ,     **a	:str  ,     )	->				int:
								__UpperCamelCase			:     List[str]         =      vocab_size
								__UpperCamelCase			:     int         =      d_model
								__UpperCamelCase			:     Optional[int]         =      decoder_ffn_dim
								__UpperCamelCase			:     Any         =      decoder_layers
								__UpperCamelCase			:     Any         =      decoder_attention_heads
								__UpperCamelCase			:     Tuple         =      dropout
								__UpperCamelCase			:     Any         =      attention_dropout
								__UpperCamelCase			:     Any         =      activation_dropout
								__UpperCamelCase			:     Dict         =      activation_function
								__UpperCamelCase			:     int         =      init_std
								__UpperCamelCase			:     List[str]         =      decoder_layerdrop
								__UpperCamelCase			:     Optional[Any]         =      use_cache
								__UpperCamelCase			:     int         =      decoder_layers
								__UpperCamelCase			:     Union[str, Any]         =      scale_embedding  # scale factor will be sqrt(d_model) if True
								__UpperCamelCase			:     Dict         =      max_target_positions

								super().__init__(
								    pad_token_id=a  ,     bos_token_id=a  ,     eos_token_id=a  ,     decoder_start_token_id=a  ,     **a  ,     )