






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


UpperCamelCase    							=      logging.get_logger(__name__)

# General docstring
UpperCamelCase    							=      'PoolFormerConfig'

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

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

UpperCamelCase    							=      [
    '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							)     ->  int:
   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	:			str					=					keep_prob + torch.rand(__lowerCAmelCase					,							dtype=input.dtype					,							device=input.device							)
   random_tensor.floor_()  # binarize
   __UpperCamelCase	:			Tuple					=					input.div(__lowerCAmelCase							) * random_tensor
   return output







class 		_A						( nn.Module ):

  def __init__(    self						:	int   ,				lowerCamelCase__						:	Optional[float] = None			):
     """simple docstring"""

     super().__init__()
     __UpperCamelCase	:			Any					=					drop_prob

  def    a					(    self						:	Dict   ,				lowerCamelCase__						:	torch.Tensor			):
     """simple docstring"""

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

  def    a					(    self						:	Optional[Any]			):
     """simple docstring"""

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







class 		_A						( nn.Module ):

  def __init__(    self						:	Tuple   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	Any   ,				lowerCamelCase__						:	Optional[Any]   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	Optional[int]   ,				lowerCamelCase__						:	int=None			):
     """simple docstring"""

     super().__init__()
     __UpperCamelCase	:			Any					=					patch_size if isinstance(lowerCamelCase__   ,				collections.abc.Iterable			) else (patch_size, patch_size)
     __UpperCamelCase	:			Tuple					=					stride if isinstance(lowerCamelCase__   ,				collections.abc.Iterable			) else (stride, stride)
     __UpperCamelCase	:			List[Any]					=					padding if isinstance(lowerCamelCase__   ,				collections.abc.Iterable			) else (padding, padding)

     __UpperCamelCase	:			int					=					nn.Convad(lowerCamelCase__   ,				lowerCamelCase__   ,				kernel_size=lowerCamelCase__   ,				stride=lowerCamelCase__   ,				padding=lowerCamelCase__			)
     __UpperCamelCase	:			Any					=					norm_layer(lowerCamelCase__			) if norm_layer else nn.Identity()

  def    a					(    self						:	List[str]   ,				lowerCamelCase__						:	Union[str, Any]			):
     """simple docstring"""

     __UpperCamelCase	:			Union[str, Any]					=					self.projection(lowerCamelCase__			)
     __UpperCamelCase	:			Optional[Any]					=					self.norm(lowerCamelCase__			)
     return embeddings







class 		_A						( nn.GroupNorm ):

  def __init__(    self						:	Optional[int]   ,				lowerCamelCase__						:	List[Any]   ,				**lowerCamelCase__						:	Tuple			):
     """simple docstring"""

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







class 		_A						( nn.Module ):

  def __init__(    self						:	Any   ,				lowerCamelCase__						:	Tuple			):
     """simple docstring"""

     super().__init__()
     __UpperCamelCase	:			Any					=					nn.AvgPoolad(lowerCamelCase__   ,				stride=1   ,				padding=pool_size // 2   ,				count_include_pad=lowerCamelCase__			)

  def    a					(    self						:	Dict   ,				lowerCamelCase__						:	Any			):
     """simple docstring"""

     return self.pool(lowerCamelCase__			) - hidden_states







class 		_A						( nn.Module ):

  def __init__(    self						:	Tuple   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	int   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	List[str]			):
     """simple docstring"""

     super().__init__()
     __UpperCamelCase	:			List[Any]					=					nn.Convad(lowerCamelCase__   ,				lowerCamelCase__   ,				1			)
     __UpperCamelCase	:			str					=					nn.Convad(lowerCamelCase__   ,				lowerCamelCase__   ,				1			)
     __UpperCamelCase	:			str					=					PoolFormerDropPath(lowerCamelCase__			)
     if isinstance(config.hidden_act   ,				lowerCamelCase__			):
        __UpperCamelCase	:			Any					=					ACTaFN[config.hidden_act]
     else:
        __UpperCamelCase	:			Dict					=					config.hidden_act

  def    a					(    self						:	Optional[Any]   ,				lowerCamelCase__						:	Tuple			):
     """simple docstring"""

     __UpperCamelCase	:			Union[str, Any]					=					self.conva(lowerCamelCase__			)
     __UpperCamelCase	:			Dict					=					self.act_fn(lowerCamelCase__			)
     __UpperCamelCase	:			List[Any]					=					self.drop(lowerCamelCase__			)
     __UpperCamelCase	:			str					=					self.conva(lowerCamelCase__			)
     __UpperCamelCase	:			Union[str, Any]					=					self.drop(lowerCamelCase__			)

     return hidden_states







class 		_A						( nn.Module ):

  def __init__(    self						:	Optional[Any]   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	Dict   ,				lowerCamelCase__						:	Optional[int]   ,				lowerCamelCase__						:	int   ,				lowerCamelCase__						:	Union[str, Any]			):
     """simple docstring"""

     super().__init__()
     __UpperCamelCase	:			str					=					PoolFormerPooling(lowerCamelCase__			)
     __UpperCamelCase	:			List[str]					=					PoolFormerOutput(lowerCamelCase__   ,				lowerCamelCase__   ,				lowerCamelCase__   ,				lowerCamelCase__			)
     __UpperCamelCase	:			List[str]					=					PoolFormerGroupNorm(lowerCamelCase__			)
     __UpperCamelCase	:			Union[str, Any]					=					PoolFormerGroupNorm(lowerCamelCase__			)

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

  def    a					(    self						:	Any   ,				lowerCamelCase__						:	Dict			):
     """simple docstring"""

     if self.use_layer_scale:
        __UpperCamelCase	:			Optional[Any]					=					self.pooling(self.before_norm(lowerCamelCase__			)			)
        __UpperCamelCase	:			Union[str, Any]					=					self.layer_scale_a.unsqueeze(-1			).unsqueeze(-1			) * pooling_output
        # First residual connection
        __UpperCamelCase	:			Dict					=					hidden_states + self.drop_path(lowerCamelCase__			)
        __UpperCamelCase	:			Union[str, Any]					=					()

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

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

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

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

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







class 		_A						( nn.Module ):

  def __init__(    self						:	int   ,				lowerCamelCase__						:	str			):
     """simple docstring"""

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

     # patch embeddings
     __UpperCamelCase	:			Dict					=					[]
     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(lowerCamelCase__			)

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

     __UpperCamelCase	:			Optional[int]					=					nn.ModuleList(lowerCamelCase__			)

  def    a					(    self						:	str   ,				lowerCamelCase__						:	List[Any]   ,				lowerCamelCase__						:	List[str]=False   ,				lowerCamelCase__						:	Dict=True			):
     """simple docstring"""

     __UpperCamelCase	:			Union[str, Any]					=					() if output_hidden_states else None

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

        if output_hidden_states:
           __UpperCamelCase	:			str					=					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=lowerCamelCase__   ,				hidden_states=lowerCamelCase__			)







class 		_A						( UpperCAmelCase_ ):
  lowercase_						:			Optional[Any]						=					PoolFormerConfig
  lowercase_						:			Any						=					'''poolformer'''
  lowercase_						:			Any						=					'''pixel_values'''
  lowercase_						:			str						=					True

  def    a					(    self						:	Union[str, Any]   ,				lowerCamelCase__						:	Any			):
     """simple docstring"""

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

  def    a					(    self						:	str   ,				lowerCamelCase__						:	Tuple   ,				lowerCamelCase__						:	List[str]=False			):
     """simple docstring"""

     if isinstance(lowerCamelCase__   ,				lowerCamelCase__			):
        __UpperCamelCase	:			Optional[int]					=					value







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

UpperCamelCase    							=      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.'''	,				UpperCAmelCase_	,				)
class 		_A						( UpperCAmelCase_ ):

  def __init__(    self						:	List[str]   ,				lowerCamelCase__						:	Union[str, Any]			):
     """simple docstring"""

     super().__init__(lowerCamelCase__			)
     __UpperCamelCase	:			List[str]					=					config

     __UpperCamelCase	:			Optional[int]					=					PoolFormerEncoder(lowerCamelCase__			)

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

  def    a					(    self						:	List[str]			):
     """simple docstring"""

     return self.embeddings.patch_embeddings

  @add_start_docstrings_to_model_forward(lowerCamelCase__			)
  @add_code_sample_docstrings(
      checkpoint=_CHECKPOINT_FOR_DOC   ,				output_type=lowerCamelCase__   ,				config_class=_CONFIG_FOR_DOC   ,				modality="""vision"""   ,				expected_output=_EXPECTED_OUTPUT_SHAPE   ,				)
  def    a					(    self						:	Optional[int]   ,				lowerCamelCase__						:	Optional[torch.FloatTensor] = None   ,				lowerCamelCase__						:	Optional[bool] = None   ,				lowerCamelCase__						:	Optional[bool] = None   ,				):
     """simple docstring"""

     __UpperCamelCase	:			Any					=					(
         output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
     )
     __UpperCamelCase	:			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	:			Any					=					self.encoder(
         lowerCamelCase__   ,				output_hidden_states=lowerCamelCase__   ,				return_dict=lowerCamelCase__   ,				)
     __UpperCamelCase	:			Union[str, Any]					=					encoder_outputs[0]

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

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







class 		_A						( nn.Module ):

  def __init__(    self						:	str   ,				lowerCamelCase__						:	List[Any]			):
     """simple docstring"""

     super().__init__()
     __UpperCamelCase	:			str					=					nn.Linear(config.hidden_size   ,				config.hidden_size			)

  def    a					(    self						:	Union[str, Any]   ,				lowerCamelCase__						:	Optional[int]			):
     """simple docstring"""

     __UpperCamelCase	:			List[Any]					=					self.dense(lowerCamelCase__			)
     return output







@add_start_docstrings(
    '''
    PoolFormer Model transformer with an image classification head on top
    '''	,				UpperCAmelCase_	,				)
class 		_A						( UpperCAmelCase_ ):

  def __init__(    self						:	Tuple   ,				lowerCamelCase__						:	List[Any]			):
     """simple docstring"""

     super().__init__(lowerCamelCase__			)
     __UpperCamelCase	:			str					=					config.num_labels
     __UpperCamelCase	:			Dict					=					PoolFormerModel(lowerCamelCase__			)

     # Final norm
     __UpperCamelCase	:			Tuple					=					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(lowerCamelCase__			)
  @add_code_sample_docstrings(
      checkpoint=_IMAGE_CLASS_CHECKPOINT   ,				output_type=lowerCamelCase__   ,				config_class=_CONFIG_FOR_DOC   ,				expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT   ,				)
  def    a					(    self						:	str   ,				lowerCamelCase__						:	Optional[torch.FloatTensor] = None   ,				lowerCamelCase__						:	Optional[torch.LongTensor] = None   ,				lowerCamelCase__						:	Optional[bool] = None   ,				lowerCamelCase__						:	Optional[bool] = None   ,				):
     """simple docstring"""

     __UpperCamelCase	:			Tuple					=					return_dict if return_dict is not None else self.config.use_return_dict

     __UpperCamelCase	:			Tuple					=					self.poolformer(
         lowerCamelCase__   ,				output_hidden_states=lowerCamelCase__   ,				return_dict=lowerCamelCase__   ,				)

     __UpperCamelCase	:			Optional[Any]					=					outputs[0]

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

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

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

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

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











UpperCamelCase    							=      8.3_144_598


def 				__lowerCamelCase     (							__lowerCAmelCase							:					float					,							__lowerCAmelCase							:					float							)     ->  float:
   if temperature < 0:
      raise Exception("""Temperature cannot be less than 0 K"""							)
   if molar_mass <= 0:
      raise Exception("""Molar mass cannot be less than or equal to 0 kg/mol"""							)
   else:
      return (3 * UNIVERSAL_GAS_CONSTANT * temperature / molar_mass) ** 0.5


if __name__ == "__main__":
       import doctest

       # run doctest
       doctest.testmod()

       # example
       UpperCamelCase    							=      300
       UpperCamelCase    							=      28
       UpperCamelCase    							=      rms_speed_of_molecule(temperature, molar_mass)
       print(F"""Vrms of Nitrogen gas at 300 K is {vrms} m/s""")



