





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


__lowerCamelCase       :     List[str]   =     logging.get_logger(__name__)

# General docstring
__lowerCamelCase       :     Optional[int]   =     """PoolFormerConfig"""

# Base docstring
__lowerCamelCase       :     Union[str, Any]   =     """sail/poolformer_s12"""
__lowerCamelCase       :     Dict   =     [1, 512, 7, 7]

# Image classification docstring
__lowerCamelCase       :     Union[str, Any]   =     """sail/poolformer_s12"""
__lowerCamelCase       :     Optional[int]   =     """tabby, tabby cat"""

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



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



class 					A__   (       nn.Module			):





      def __init__( self       ,							A_ = None   ):
       '''simple docstring'''




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





      def        __UpperCamelCase( self       ,							A_   ):
       '''simple docstring'''




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





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




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



class 					A__   (       nn.Module			):





      def __init__( self       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_=None   ):
       '''simple docstring'''




       super().__init__()
       UpperCamelCase	:					Tuple       	=   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	:					Tuple       	=   padding if isinstance(A_       ,							collections.abc.Iterable   ) else (padding, padding)

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





      def        __UpperCamelCase( self       ,							A_   ):
       '''simple docstring'''




       UpperCamelCase	:					str       	=   self.projection(A_   )
       UpperCamelCase	:					Any       	=   self.norm(A_   )
       return embeddings



class 					A__   (       nn.GroupNorm			):





      def __init__( self       ,							A_       ,							**A_   ):
       '''simple docstring'''




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



class 					A__   (       nn.Module			):





      def __init__( self       ,							A_   ):
       '''simple docstring'''




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





      def        __UpperCamelCase( self       ,							A_   ):
       '''simple docstring'''




       return self.pool(A_   ) - hidden_states



class 					A__   (       nn.Module			):





      def __init__( self       ,							A_       ,							A_       ,							A_       ,							A_   ):
       '''simple docstring'''




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





      def        __UpperCamelCase( self       ,							A_   ):
       '''simple docstring'''




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

       return hidden_states



class 					A__   (       nn.Module			):





      def __init__( self       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_       ,							A_   ):
       '''simple docstring'''




       super().__init__()
       UpperCamelCase	:					Tuple       	=   PoolFormerPooling(A_   )
       UpperCamelCase	:					Tuple       	=   PoolFormerOutput(A_       ,							A_       ,							A_       ,							A_   )
       UpperCamelCase	:					str       	=   PoolFormerGroupNorm(A_   )
       UpperCamelCase	:					str       	=   PoolFormerGroupNorm(A_   )

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





      def        __UpperCamelCase( self       ,							A_   ):
       '''simple docstring'''




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

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

        UpperCamelCase	:					Union[str, Any]       	=   (output,) + outputs
        return outputs

       else:
        UpperCamelCase	:					List[str]       	=   self.drop_path(self.pooling(self.before_norm(A_   )   )   )
        # First residual connection
        UpperCamelCase	:					Tuple       	=   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	:					Union[str, Any]       	=   hidden_states + layer_output

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



class 					A__   (       nn.Module			):





      def __init__( self       ,							A_   ):
       '''simple docstring'''




       super().__init__()
       UpperCamelCase	:					List[Any]       	=   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	:					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]       ,							)   )
       UpperCamelCase	:					Any       	=   nn.ModuleList(A_   )

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





      def        __UpperCamelCase( self       ,							A_       ,							A_=False       ,							A_=True   ):
       '''simple docstring'''




       UpperCamelCase	:					int       	=   () if output_hidden_states else None

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

        if output_hidden_states:
         UpperCamelCase	:					List[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 					A__   (       __a			):
      _UpperCAmelCase	:Optional[int]						    =   PoolFormerConfig
      _UpperCAmelCase	:Dict						    =   """poolformer"""
      _UpperCAmelCase	:Union[str, Any]						    =   """pixel_values"""
      _UpperCAmelCase	:Dict						    =   True





      def        __UpperCamelCase( self       ,							A_   ):
       '''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        __UpperCamelCase( self       ,							A_       ,							A_=False   ):
       '''simple docstring'''




       if isinstance(A_       ,							A_   ):
        UpperCamelCase	:					Optional[Any]       	=   value


__lowerCamelCase       :     Optional[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"""

__lowerCamelCase       :     Tuple   =     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.'       ,   __a       ,   )
class 					A__   (       __a			):





      def __init__( self       ,							A_   ):
       '''simple docstring'''




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

       UpperCamelCase	:					List[Any]       	=   PoolFormerEncoder(A_   )

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





      def        __UpperCamelCase( self   ):
       '''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        __UpperCamelCase( self       ,							A_ = None       ,							A_ = None       ,							A_ = None       ,							):
       '''simple docstring'''




       UpperCamelCase	:					Tuple       	=   (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       UpperCamelCase	:					Dict       	=   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	:					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 					A__   (       nn.Module			):





      def __init__( self       ,							A_   ):
       '''simple docstring'''




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





      def        __UpperCamelCase( self       ,							A_   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[int]       	=   self.dense(A_   )
       return output



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





      def __init__( self       ,							A_   ):
       '''simple docstring'''




       super().__init__(A_   )
       UpperCamelCase	:					Optional[Any]       	=   config.num_labels
       UpperCamelCase	:					List[str]       	=   PoolFormerModel(A_   )

       # Final norm
       UpperCamelCase	:					Dict       	=   PoolFormerGroupNorm(config.hidden_sizes[-1]   )
       # Classifier head
       UpperCamelCase	:					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        __UpperCamelCase( self       ,							A_ = None       ,							A_ = None       ,							A_ = None       ,							A_ = None       ,							):
       '''simple docstring'''




       UpperCamelCase	:					List[Any]       	=   return_dict if return_dict is not None else self.config.use_return_dict

       UpperCamelCase	:					int       	=   self.poolformer(
           A_       ,							output_hidden_states=A_       ,							return_dict=A_       ,							)

       UpperCamelCase	:					str       	=   outputs[0]

       UpperCamelCase	:					List[str]       	=   self.classifier(self.norm(A_   ).mean([-2, -1]   )   )

       UpperCamelCase	:					Union[str, Any]       	=   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	:					str       	=   "single_label_classification"
         else:
          UpperCamelCase	:					Optional[int]       	=   "multi_label_classification"

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

       if not return_dict:
        UpperCamelCase	:					str       	=   (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'''







import os





def 						snake_case_							():
    UpperCAmelCase			       =		os.path.join(os.path.dirname(_a     )    ,   '''num.txt'''     )
    with open(_a     ) as file_hand:
        return str(sum(int(_a     ) for line in file_hand     )     )[:1_0]


if __name__ == "__main__":
   print(solution())




