

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			:					Optional[Any]						=			logging.get_logger(__name__)

# General docstring
__A			:					int						=			'PoolFormerConfig'

# Base docstring
__A			:					str						=			'sail/poolformer_s12'
__A			:					Any						=			[1, 5_1_2, 7, 7]

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

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





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





class 			_SCREAMING_SNAKE_CASE (   nn.Module		):







   '''simple docstring'''




   def __init__(       self					:			int     , __lowerCamelCase					:			Optional[float] = None			):
       super().__init__()
       SCREAMING_SNAKE_CASE										=   drop_prob





   def      _snake_case		(       self					:			int     , __lowerCamelCase					:			torch.Tensor			):
       return drop_path(__lowerCamelCase     , self.drop_prob     , self.training			)





   def      _snake_case		(       self					:			List[Any]			):
       return "p={}".format(self.drop_prob			)





class 			_SCREAMING_SNAKE_CASE (   nn.Module		):







   '''simple docstring'''




   def __init__(       self					:			Any     , __lowerCamelCase					:			Union[str, Any]     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			Union[str, Any]     , __lowerCamelCase					:			Tuple     , __lowerCamelCase					:			Optional[int]     , __lowerCamelCase					:			Dict=None			):
       super().__init__()
       SCREAMING_SNAKE_CASE										=   patch_size if isinstance(__lowerCamelCase     , collections.abc.Iterable			) else (patch_size, patch_size)
       SCREAMING_SNAKE_CASE										=   stride if isinstance(__lowerCamelCase     , collections.abc.Iterable			) else (stride, stride)
       SCREAMING_SNAKE_CASE										=   padding if isinstance(__lowerCamelCase     , collections.abc.Iterable			) else (padding, padding)

       SCREAMING_SNAKE_CASE										=   nn.Convad(__lowerCamelCase     , __lowerCamelCase     , kernel_size=__lowerCamelCase     , stride=__lowerCamelCase     , padding=__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   norm_layer(__lowerCamelCase			) if norm_layer else nn.Identity()





   def      _snake_case		(       self					:			Union[str, Any]     , __lowerCamelCase					:			Any			):
       SCREAMING_SNAKE_CASE										=   self.projection(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   self.norm(__lowerCamelCase			)
       return embeddings





class 			_SCREAMING_SNAKE_CASE (   nn.GroupNorm		):







   '''simple docstring'''




   def __init__(       self					:			Dict     , __lowerCamelCase					:			Any     , **__lowerCamelCase					:			List[str]			):
       super().__init__(1     , __lowerCamelCase     , **__lowerCamelCase			)





class 			_SCREAMING_SNAKE_CASE (   nn.Module		):







   '''simple docstring'''




   def __init__(       self					:			Optional[Any]     , __lowerCamelCase					:			Dict			):
       super().__init__()
       SCREAMING_SNAKE_CASE										=   nn.AvgPoolad(__lowerCamelCase     , stride=1     , padding=pool_size // 2     , count_include_pad=__lowerCamelCase			)





   def      _snake_case		(       self					:			int     , __lowerCamelCase					:			int			):
       return self.pool(__lowerCamelCase			) - hidden_states





class 			_SCREAMING_SNAKE_CASE (   nn.Module		):







   '''simple docstring'''




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





   def      _snake_case		(       self					:			Any     , __lowerCamelCase					:			int			):
       SCREAMING_SNAKE_CASE										=   self.conva(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   self.act_fn(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   self.drop(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   self.conva(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   self.drop(__lowerCamelCase			)

       return hidden_states





class 			_SCREAMING_SNAKE_CASE (   nn.Module		):







   '''simple docstring'''




   def __init__(       self					:			str     , __lowerCamelCase					:			int     , __lowerCamelCase					:			List[str]     , __lowerCamelCase					:			int     , __lowerCamelCase					:			int     , __lowerCamelCase					:			Dict     , __lowerCamelCase					:			List[str]			):
       super().__init__()
       SCREAMING_SNAKE_CASE										=   PoolFormerPooling(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   PoolFormerOutput(__lowerCamelCase     , __lowerCamelCase     , __lowerCamelCase     , __lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   PoolFormerGroupNorm(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   PoolFormerGroupNorm(__lowerCamelCase			)

       # Useful for training neural nets
       SCREAMING_SNAKE_CASE										=   PoolFormerDropPath(__lowerCamelCase			) if drop_path > 0.0 else nn.Identity()
       SCREAMING_SNAKE_CASE										=   config.use_layer_scale
       if config.use_layer_scale:
           SCREAMING_SNAKE_CASE										=   nn.Parameter(
               config.layer_scale_init_value * torch.ones((__lowerCamelCase)			)     , requires_grad=__lowerCamelCase			)
           SCREAMING_SNAKE_CASE										=   nn.Parameter(
               config.layer_scale_init_value * torch.ones((__lowerCamelCase)			)     , requires_grad=__lowerCamelCase			)





   def      _snake_case		(       self					:			Optional[int]     , __lowerCamelCase					:			Optional[int]			):
       if self.use_layer_scale:
           SCREAMING_SNAKE_CASE										=   self.pooling(self.before_norm(__lowerCamelCase			)			)
           SCREAMING_SNAKE_CASE										=   self.layer_scale_a.unsqueeze(-1			).unsqueeze(-1			) * pooling_output
           # First residual connection
           SCREAMING_SNAKE_CASE										=   hidden_states + self.drop_path(__lowerCamelCase			)
           SCREAMING_SNAKE_CASE										=   ()

           SCREAMING_SNAKE_CASE										=   self.output(self.after_norm(__lowerCamelCase			)			)
           SCREAMING_SNAKE_CASE										=   self.layer_scale_a.unsqueeze(-1			).unsqueeze(-1			) * layer_output
           # Second residual connection
           SCREAMING_SNAKE_CASE										=   hidden_states + self.drop_path(__lowerCamelCase			)

           SCREAMING_SNAKE_CASE										=   (output,) + outputs
           return outputs

       else:
           SCREAMING_SNAKE_CASE										=   self.drop_path(self.pooling(self.before_norm(__lowerCamelCase			)			)			)
           # First residual connection
           SCREAMING_SNAKE_CASE										=   pooling_output + hidden_states
           SCREAMING_SNAKE_CASE										=   ()

           # Second residual connection inside the PoolFormerOutput block
           SCREAMING_SNAKE_CASE										=   self.drop_path(self.output(self.after_norm(__lowerCamelCase			)			)			)
           SCREAMING_SNAKE_CASE										=   hidden_states + layer_output

           SCREAMING_SNAKE_CASE										=   (output,) + outputs
           return outputs





class 			_SCREAMING_SNAKE_CASE (   nn.Module		):







   '''simple docstring'''




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

       # patch embeddings
       SCREAMING_SNAKE_CASE										=   []
       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]     , )			)
       SCREAMING_SNAKE_CASE										=   nn.ModuleList(__lowerCamelCase			)

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

       SCREAMING_SNAKE_CASE										=   nn.ModuleList(__lowerCamelCase			)





   def      _snake_case		(       self					:			Optional[int]     , __lowerCamelCase					:			Optional[Any]     , __lowerCamelCase					:			Union[str, Any]=False     , __lowerCamelCase					:			List[Any]=True			):
       SCREAMING_SNAKE_CASE										=   () if output_hidden_states else None

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

           if output_hidden_states:
               SCREAMING_SNAKE_CASE										=   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 			_SCREAMING_SNAKE_CASE (   __snake_case		):







   '''simple docstring'''




   lowerCamelCase__				=      PoolFormerConfig
   lowerCamelCase__				=      "poolformer"
   lowerCamelCase__				=      "pixel_values"
   lowerCamelCase__				=      True





   def      _snake_case		(       self					:			Optional[Any]     , __lowerCamelCase					:			Any			):
       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					:			str     , __lowerCamelCase					:			Tuple     , __lowerCamelCase					:			Any=False			):
       if isinstance(__lowerCamelCase     , __lowerCamelCase			):
           SCREAMING_SNAKE_CASE										=   value




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

__A			:					Dict						=			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."					,       __snake_case					,       )
class 			_SCREAMING_SNAKE_CASE (   __snake_case		):







   '''simple docstring'''




   def __init__(       self					:			str     , __lowerCamelCase					:			str			):
       super().__init__(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   config

       SCREAMING_SNAKE_CASE										=   PoolFormerEncoder(__lowerCamelCase			)

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





   def      _snake_case		(       self					:			Any			):
       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					:			Union[str, Any]     , __lowerCamelCase					:			Optional[torch.FloatTensor] = None     , __lowerCamelCase					:			Optional[bool] = None     , __lowerCamelCase					:			Optional[bool] = None     , ):
       SCREAMING_SNAKE_CASE										=   (
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       SCREAMING_SNAKE_CASE										=   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"			)

       SCREAMING_SNAKE_CASE										=   self.encoder(
           __lowerCamelCase     , output_hidden_states=__lowerCamelCase     , return_dict=__lowerCamelCase     , )
       SCREAMING_SNAKE_CASE										=   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 			_SCREAMING_SNAKE_CASE (   nn.Module		):







   '''simple docstring'''




   def __init__(       self					:			Optional[Any]     , __lowerCamelCase					:			List[str]			):
       super().__init__()
       SCREAMING_SNAKE_CASE										=   nn.Linear(config.hidden_size     , config.hidden_size			)





   def      _snake_case		(       self					:			Tuple     , __lowerCamelCase					:			Tuple			):
       SCREAMING_SNAKE_CASE										=   self.dense(__lowerCamelCase			)
       return output





@add_start_docstrings(
    "\n    PoolFormer Model transformer with an image classification head on top\n    "					,       __snake_case					,       )
class 			_SCREAMING_SNAKE_CASE (   __snake_case		):







   '''simple docstring'''




   def __init__(       self					:			Optional[Any]     , __lowerCamelCase					:			List[str]			):
       super().__init__(__lowerCamelCase			)
       SCREAMING_SNAKE_CASE										=   config.num_labels
       SCREAMING_SNAKE_CASE										=   PoolFormerModel(__lowerCamelCase			)

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

       SCREAMING_SNAKE_CASE										=   self.poolformer(
           __lowerCamelCase     , output_hidden_states=__lowerCamelCase     , return_dict=__lowerCamelCase     , )

       SCREAMING_SNAKE_CASE										=   outputs[0]

       SCREAMING_SNAKE_CASE										=   self.classifier(self.norm(__lowerCamelCase			).mean([-2, -1]			)			)

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

           if self.config.problem_type == "regression":
               SCREAMING_SNAKE_CASE										=   MSELoss()
               if self.num_labels == 1:
                   SCREAMING_SNAKE_CASE										=   loss_fct(logits.squeeze()     , labels.squeeze()			)
               else:
                   SCREAMING_SNAKE_CASE										=   loss_fct(__lowerCamelCase     , __lowerCamelCase			)
           elif self.config.problem_type == "single_label_classification":
               SCREAMING_SNAKE_CASE										=   CrossEntropyLoss()
               SCREAMING_SNAKE_CASE										=   loss_fct(logits.view(-1     , self.num_labels			)     , labels.view(-1			)			)
           elif self.config.problem_type == "multi_label_classification":
               SCREAMING_SNAKE_CASE										=   BCEWithLogitsLoss()
               SCREAMING_SNAKE_CASE										=   loss_fct(__lowerCamelCase     , __lowerCamelCase			)

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

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


from __future__ import annotations





def 	__a     (      A__			:  list[int | str]       ):
    create_state_space_tree(A__			,  []			,  0			,  [0 for i in range(len(A__       )       )]       )





def 	__a     (      A__			:  list[int | str]			,  A__			:  list[int | str]			,  A__			:  int			,  A__			:  list[int]			,  ):

    if index == len(A__       ):
        print(A__       )
        return

    for i in range(len(A__       )       ):
        if not index_used[i]:
            current_sequence.append(sequence[i]       )
            SCREAMING_SNAKE_CASE										=   True
            create_state_space_tree(A__			,  A__			,  index + 1			,  A__       )
            current_sequence.pop()
            SCREAMING_SNAKE_CASE										=   False
__A			:					list[int | str]						=			[3, 1, 2, 4]
generate_all_permutations(sequence)

__A			:					list[int | str]						=			["A", "B", "C"]





generate_all_permutations(sequence_a)