


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


__magic_name__			=      logging.get_logger(__name__)

# General docstring
__magic_name__			=      'PoolFormerConfig'

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

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

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




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



class 	__lowerCAmelCase		(							nn.Module    ):


  '''simple docstring'''







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

        '''simple docstring'''




        super().__init__()
        A_						:  str          =				drop_prob






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

        '''simple docstring'''




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






  def        _a							(							self :       Dict ):

        '''simple docstring'''




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







class 	__lowerCAmelCase		(							nn.Module    ):


  '''simple docstring'''







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

        '''simple docstring'''




        super().__init__()
        A_						:  str          =				patch_size if isinstance(_a	,collections.abc.Iterable ) else (patch_size, patch_size)
        A_						:  Dict          =				stride if isinstance(_a	,collections.abc.Iterable ) else (stride, stride)
        A_						:  Any          =				padding if isinstance(_a	,collections.abc.Iterable ) else (padding, padding)

        A_						:  List[Any]          =				nn.Convad(_a	,_a	,kernel_size=_a	,stride=_a	,padding=_a )
        A_						:  Dict          =				norm_layer(_a ) if norm_layer else nn.Identity()






  def        _a							(							self :       List[str]	,_a :       int ):

        '''simple docstring'''




        A_						:  str          =				self.projection(_a )
        A_						:  Tuple          =				self.norm(_a )
        return embeddings







class 	__lowerCAmelCase		(							nn.GroupNorm    ):


  '''simple docstring'''







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

        '''simple docstring'''




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







class 	__lowerCAmelCase		(							nn.Module    ):


  '''simple docstring'''







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

        '''simple docstring'''




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






  def        _a							(							self :       Dict	,_a :       Optional[int] ):

        '''simple docstring'''




        return self.pool(_a ) - hidden_states







class 	__lowerCAmelCase		(							nn.Module    ):


  '''simple docstring'''







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

        '''simple docstring'''




        super().__init__()
        A_						:  Dict          =				nn.Convad(_a	,_a	,1 )
        A_						:  Union[str, Any]          =				nn.Convad(_a	,_a	,1 )
        A_						:  Optional[Any]          =				PoolFormerDropPath(_a )
        if isinstance(config.hidden_act	,_a ):
              A_						:  Union[str, Any]          =				ACTaFN[config.hidden_act]
        else:
              A_						:  Dict          =				config.hidden_act






  def        _a							(							self :       str	,_a :       Tuple ):

        '''simple docstring'''




        A_						:  Any          =				self.conva(_a )
        A_						:  str          =				self.act_fn(_a )
        A_						:  Optional[int]          =				self.drop(_a )
        A_						:  List[str]          =				self.conva(_a )
        A_						:  List[str]          =				self.drop(_a )

        return hidden_states







class 	__lowerCAmelCase		(							nn.Module    ):


  '''simple docstring'''







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

        '''simple docstring'''




        super().__init__()
        A_						:  List[Any]          =				PoolFormerPooling(_a )
        A_						:  str          =				PoolFormerOutput(_a	,_a	,_a	,_a )
        A_						:  List[Any]          =				PoolFormerGroupNorm(_a )
        A_						:  Optional[Any]          =				PoolFormerGroupNorm(_a )

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






  def        _a							(							self :       Optional[int]	,_a :       Any ):

        '''simple docstring'''




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

              A_						:  List[Any]          =				self.output(self.after_norm(_a ) )
              A_						:  str          =				self.layer_scale_a.unsqueeze(-1 ).unsqueeze(-1 ) * layer_output
              # Second residual connection
              A_						:  Union[str, Any]          =				hidden_states + self.drop_path(_a )

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

        else:
              A_						:  Union[str, Any]          =				self.drop_path(self.pooling(self.before_norm(_a ) ) )
              # First residual connection
              A_						:  Any          =				pooling_output + hidden_states
              A_						:  List[str]          =				()

              # Second residual connection inside the PoolFormerOutput block
              A_						:  List[Any]          =				self.drop_path(self.output(self.after_norm(_a ) ) )
              A_						:  str          =				hidden_states + layer_output

              A_						:  Optional[Any]          =				(output,) + outputs
              return outputs







class 	__lowerCAmelCase		(							nn.Module    ):


  '''simple docstring'''







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

        '''simple docstring'''




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

        # patch embeddings
        A_						:  Tuple          =				[]
        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_						:  Optional[Any]          =				nn.ModuleList(_a )

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

        A_						:  List[Any]          =				nn.ModuleList(_a )






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

        '''simple docstring'''




        A_						:  List[Any]          =				() if output_hidden_states else None

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

              if output_hidden_states:
                    A_						:  Union[str, 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		(							__SCREAMING_SNAKE_CASE    ):


  '''simple docstring'''







  a_  					=							PoolFormerConfig
  a_  					=							"""poolformer"""
  a_  					=							"""pixel_values"""
  a_  					=							True






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

        '''simple docstring'''




        if isinstance(_a	,_a ):
              A_						:  int          =				value







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

__magic_name__			=      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 	__lowerCAmelCase		(							__SCREAMING_SNAKE_CASE    ):


  '''simple docstring'''







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

        '''simple docstring'''




        super().__init__(_a )
        A_						:  List[str]          =				config

        A_						:  Optional[Any]          =				PoolFormerEncoder(_a )

        # 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(_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]	,_a :       Optional[torch.FloatTensor] = None	,_a :       Optional[bool] = None	,_a :       Optional[bool] = None	,):

        '''simple docstring'''




        A_						:  Any          =				(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        A_						:  Union[str, 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""" )

        A_						:  List[Any]          =				self.encoder(
            _a	,output_hidden_states=_a	,return_dict=_a	,)
        A_						:  int          =				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 :       Optional[int]	,_a :       str ):

        '''simple docstring'''




        super().__init__()
        A_						:  Optional[int]          =				nn.Linear(config.hidden_size	,config.hidden_size )






  def        _a							(							self :       str	,_a :       Any ):

        '''simple docstring'''




        A_						:  Optional[Any]          =				self.dense(_a )
        return output







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


  '''simple docstring'''







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

        '''simple docstring'''




        super().__init__(_a )
        A_						:  List[str]          =				config.num_labels
        A_						:  str          =				PoolFormerModel(_a )

        # Final norm
        A_						:  Union[str, Any]          =				PoolFormerGroupNorm(config.hidden_sizes[-1] )
        # Classifier head
        A_						:  Optional[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(_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	,_a :       Optional[torch.FloatTensor] = None	,_a :       Optional[torch.LongTensor] = None	,_a :       Optional[bool] = None	,_a :       Optional[bool] = None	,):

        '''simple docstring'''




        A_						:  int          =				return_dict if return_dict is not None else self.config.use_return_dict

        A_						:  Tuple          =				self.poolformer(
            _a	,output_hidden_states=_a	,return_dict=_a	,)

        A_						:  Optional[Any]          =				outputs[0]

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

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

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

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

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




'''simple docstring'''


from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


if TYPE_CHECKING:
     from ... import FeatureExtractionMixin, PreTrainedTokenizerBase, TensorType


__magic_name__			=      logging.get_logger(__name__)

__magic_name__			=      {
    'microsoft/deberta-v2-xlarge': 'https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/config.json',
    'microsoft/deberta-v2-xxlarge': 'https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/config.json',
    'microsoft/deberta-v2-xlarge-mnli': (
        'https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/config.json'
    ),
    'microsoft/deberta-v2-xxlarge-mnli': (
        'https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/config.json'
    ),
}



class 	__lowerCAmelCase		(							__SCREAMING_SNAKE_CASE    ):


  '''simple docstring'''







  a_  					=							"""deberta-v2"""






  def __init__(							self :       Optional[Any]	,_a :       Union[str, Any]=128100	,_a :       Optional[int]=1536	,_a :       Dict=24	,_a :       int=24	,_a :       Tuple=6144	,_a :       Union[str, Any]="gelu"	,_a :       List[Any]=0.1	,_a :       Dict=0.1	,_a :       int=512	,_a :       int=0	,_a :       int=0.02	,_a :       int=1e-7	,_a :       List[str]=False	,_a :       Union[str, Any]=-1	,_a :       List[Any]=0	,_a :       Optional[Any]=True	,_a :       Tuple=None	,_a :       Any=0	,_a :       int="gelu"	,**_a :       Any	,):

        '''simple docstring'''




        super().__init__(**_a )

        A_						:  Union[str, Any]          =				hidden_size
        A_						:  Dict          =				num_hidden_layers
        A_						:  Union[str, Any]          =				num_attention_heads
        A_						:  List[Any]          =				intermediate_size
        A_						:  List[Any]          =				hidden_act
        A_						:  Optional[int]          =				hidden_dropout_prob
        A_						:  Dict          =				attention_probs_dropout_prob
        A_						:  int          =				max_position_embeddings
        A_						:  Any          =				type_vocab_size
        A_						:  List[Any]          =				initializer_range
        A_						:  int          =				relative_attention
        A_						:  Tuple          =				max_relative_positions
        A_						:  int          =				pad_token_id
        A_						:  Tuple          =				position_biased_input

        # Backwards compatibility
        if type(_a ) == str:
              A_						:  str          =				[x.strip() for x in pos_att_type.lower().split("""|""" )]

        A_						:  Any          =				pos_att_type
        A_						:  Optional[int]          =				vocab_size
        A_						:  Tuple          =				layer_norm_eps

        A_						:  Any          =				kwargs.get("""pooler_hidden_size"""	,_a )
        A_						:  Union[str, Any]          =				pooler_dropout
        A_						:  List[Any]          =				pooler_hidden_act







class 	__lowerCAmelCase		(							__SCREAMING_SNAKE_CASE    ):


  '''simple docstring'''







  @property
  def        _a							(							self :       Any ):

        '''simple docstring'''




        if self.task == "multiple-choice":
              A_						:  Any          =				{0: """batch""", 1: """choice""", 2: """sequence"""}
        else:
              A_						:  Any          =				{0: """batch""", 1: """sequence"""}
        if self._config.type_vocab_size > 0:
              return OrderedDict(
                  [("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis), ("""token_type_ids""", dynamic_axis)] )
        else:
              return OrderedDict([("""input_ids""", dynamic_axis), ("""attention_mask""", dynamic_axis)] )






  @property
  def        _a							(							self :       Optional[int] ):

        '''simple docstring'''




        return 12






  def        _a							(							self :       int	,_a :       Union["PreTrainedTokenizerBase", "FeatureExtractionMixin"]	,_a :       int = -1	,_a :       int = -1	,_a :       int = -1	,_a :       bool = False	,_a :       Optional["TensorType"] = None	,_a :       int = 3	,_a :       int = 40	,_a :       int = 40	,_a :       "PreTrainedTokenizerBase" = None	,):

        '''simple docstring'''




        A_						:  Any          =				super().generate_dummy_inputs(preprocessor=_a	,framework=_a )
        if self._config.type_vocab_size == 0 and "token_type_ids" in dummy_inputs:
              del dummy_inputs["token_type_ids"]
        return dummy_inputs
