






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


_a    :   Tuple        =			logging.get_logger(__name__)

# General docstring
_a    :   Union[str, Any]        =			"""PoolFormerConfig"""

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

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

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


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







class 	_UpperCAmelCase  (  nn.Module		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase = None		):



          '''simple docstring'''
          super().__init__()
          lowerCAmelCase__   :str      							=				drop_prob
      def 			snake_case_      (   self    ,	_lowerCAmelCase		):



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



      def 			snake_case_      (   self		):



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







class 	_UpperCAmelCase  (  nn.Module		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase=None		):



          '''simple docstring'''
          super().__init__()
          lowerCAmelCase__   :str      							=				patch_size if isinstance(_lowerCAmelCase    ,	collections.abc.Iterable		) else (patch_size, patch_size)
          lowerCAmelCase__   :Dict      							=				stride if isinstance(_lowerCAmelCase    ,	collections.abc.Iterable		) else (stride, stride)
          lowerCAmelCase__   :List[Any]      							=				padding if isinstance(_lowerCAmelCase    ,	collections.abc.Iterable		) else (padding, padding)

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



      def 			snake_case_      (   self    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				self.projection(_lowerCAmelCase		)
          lowerCAmelCase__   :Tuple      							=				self.norm(_lowerCAmelCase		)
          return embeddings







class 	_UpperCAmelCase  (  nn.GroupNorm		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase    ,	**_lowerCAmelCase		):



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







class 	_UpperCAmelCase  (  nn.Module		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase		):



          '''simple docstring'''
          super().__init__()
          lowerCAmelCase__   :Union[str, Any]      							=				nn.AvgPoolad(_lowerCAmelCase    ,	stride=1    ,	padding=pool_size // 2    ,	count_include_pad=_lowerCAmelCase		)



      def 			snake_case_      (   self    ,	_lowerCAmelCase		):



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







class 	_UpperCAmelCase  (  nn.Module		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          super().__init__()
          lowerCAmelCase__   :Optional[Any]      							=				nn.Convad(_lowerCAmelCase    ,	_lowerCAmelCase    ,	1		)
          lowerCAmelCase__   :Union[str, Any]      							=				nn.Convad(_lowerCAmelCase    ,	_lowerCAmelCase    ,	1		)
          lowerCAmelCase__   :Tuple      							=				PoolFormerDropPath(_lowerCAmelCase		)
          if isinstance(config.hidden_act    ,	_lowerCAmelCase		):
              lowerCAmelCase__   :Optional[int]      							=				ACTaFN[config.hidden_act]
          else:
              lowerCAmelCase__   :List[str]      							=				config.hidden_act



      def 			snake_case_      (   self    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				self.conva(_lowerCAmelCase		)
          lowerCAmelCase__   :int      							=				self.act_fn(_lowerCAmelCase		)
          lowerCAmelCase__   :Dict      							=				self.drop(_lowerCAmelCase		)
          lowerCAmelCase__   :Any      							=				self.conva(_lowerCAmelCase		)
          lowerCAmelCase__   :List[str]      							=				self.drop(_lowerCAmelCase		)

          return hidden_states







class 	_UpperCAmelCase  (  nn.Module		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          super().__init__()
          lowerCAmelCase__   :Optional[Any]      							=				PoolFormerPooling(_lowerCAmelCase		)
          lowerCAmelCase__   :int      							=				PoolFormerOutput(_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		)
          lowerCAmelCase__   :Tuple      							=				PoolFormerGroupNorm(_lowerCAmelCase		)
          lowerCAmelCase__   :List[Any]      							=				PoolFormerGroupNorm(_lowerCAmelCase		)

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



      def 			snake_case_      (   self    ,	_lowerCAmelCase		):



          '''simple docstring'''
          if self.use_layer_scale:
              lowerCAmelCase__   :Tuple      							=				self.pooling(self.before_norm(_lowerCAmelCase		)		)
              lowerCAmelCase__   :Tuple      							=				self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * pooling_output
              # First residual connection
              lowerCAmelCase__   :List[Any]      							=				hidden_states + self.drop_path(_lowerCAmelCase		)
              lowerCAmelCase__   :Any      							=				()

              lowerCAmelCase__   :int      							=				self.output(self.after_norm(_lowerCAmelCase		)		)
              lowerCAmelCase__   :Dict      							=				self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * layer_output
              # Second residual connection
              lowerCAmelCase__   :Dict      							=				hidden_states + self.drop_path(_lowerCAmelCase		)

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

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

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

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







class 	_UpperCAmelCase  (  nn.Module		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase		):



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

          # patch embeddings
          lowerCAmelCase__   :Optional[int]      							=				[]
          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]    ,	)		)
          lowerCAmelCase__   :str      							=				nn.ModuleList(_lowerCAmelCase		)

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

          lowerCAmelCase__   :Optional[Any]      							=				nn.ModuleList(_lowerCAmelCase		)



      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase=False    ,	_lowerCAmelCase=True		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				() if output_hidden_states else None

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

              if output_hidden_states:
                  lowerCAmelCase__   :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=_lowerCAmelCase    ,	hidden_states=_lowerCAmelCase		)







class 	_UpperCAmelCase  (  _A		):




      """simple docstring"""



      A          =    PoolFormerConfig
      A          =    '''poolformer'''
      A          =    '''pixel_values'''
      A          =    True
      def 			snake_case_      (   self    ,	_lowerCAmelCase		):



          '''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 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase=False		):



          '''simple docstring'''
          if isinstance(_lowerCAmelCase    ,	_lowerCAmelCase		):
              lowerCAmelCase__   :Union[str, Any]      							=				value





_a    :   Dict        =			R"""
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
"""

_a    :   Any        =			R"""
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`PoolFormerImageProcessor.__call__`] for details.
"""







@add_start_docstrings(
    '''The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.'''				,							_A				,							)
class 	_UpperCAmelCase  (  _A		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase		):



          '''simple docstring'''
          super().__init__(_lowerCAmelCase		)
          lowerCAmelCase__   :Dict      							=				config

          lowerCAmelCase__   :Optional[int]      							=				PoolFormerEncoder(_lowerCAmelCase		)

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



          '''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 			snake_case_      (   self    ,	_lowerCAmelCase = None    ,	_lowerCAmelCase = None    ,	_lowerCAmelCase = None    ,	):



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

          lowerCAmelCase__   :List[str]      							=				self.encoder(
              _lowerCAmelCase    ,	output_hidden_states=_lowerCAmelCase    ,	return_dict=_lowerCAmelCase    ,	)
          lowerCAmelCase__   :Optional[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 	_UpperCAmelCase  (  nn.Module		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase		):



          '''simple docstring'''
          super().__init__()
          lowerCAmelCase__   :Optional[Any]      							=				nn.Linear(config.hidden_size    ,	config.hidden_size		)



      def 			snake_case_      (   self    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				self.dense(_lowerCAmelCase		)
          return output







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




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase		):



          '''simple docstring'''
          super().__init__(_lowerCAmelCase		)
          lowerCAmelCase__   :List[Any]      							=				config.num_labels
          lowerCAmelCase__   :str      							=				PoolFormerModel(_lowerCAmelCase		)

          # Final norm
          lowerCAmelCase__   :str      							=				PoolFormerGroupNorm(config.hidden_sizes[-1]		)
          # Classifier head
          lowerCAmelCase__   :int      							=				(
              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 			snake_case_      (   self    ,	_lowerCAmelCase = None    ,	_lowerCAmelCase = None    ,	_lowerCAmelCase = None    ,	_lowerCAmelCase = None    ,	):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				return_dict if return_dict is not None else self.config.use_return_dict

          lowerCAmelCase__   :str      							=				self.poolformer(
              _lowerCAmelCase    ,	output_hidden_states=_lowerCAmelCase    ,	return_dict=_lowerCAmelCase    ,	)

          lowerCAmelCase__   :Tuple      							=				outputs[0]

          lowerCAmelCase__   :str      							=				self.classifier(self.norm(_lowerCAmelCase		).mean([-2, -1]		)		)

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

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

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

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











def 				snake_case__							(							UpperCAmelCase							:  float    ,			UpperCAmelCase							:  list[float]       ):
    if discount_rate < 0:
        raise ValueError("Discount rate cannot be negative"       )
    if not cash_flows:
        raise ValueError("Cash flows list cannot be empty"       )
    lowerCAmelCase__   :List[Any]      							=				sum(
        cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(UpperCAmelCase       )       )
    return round(UpperCAmelCase    ,			ndigits=2       )


if __name__ == "__main__":
 import doctest

 doctest.testmod()



