





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

# General docstring
A__    :       Dict              ='''PoolFormerConfig'''

# Base docstring
A__    :       List[str]              ='''sail/poolformer_s12'''
A__    :       str              =[1, 5_12, 7, 7]

# Image classification docstring
A__    :       Any              ='''sail/poolformer_s12'''
A__    :       str              ='''tabby, tabby cat'''

A__    :       Optional[int]              =[
    '''sail/poolformer_s12''',
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]
def        UpperCamelCase__				(		lowerCAmelCase							,     lowerCAmelCase = 0.0							,     lowerCAmelCase = False  ):





      """simple docstring"""


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




class 			UpperCAmelCase					(      nn.Module       ):




      def __init__(      self      :			Optional[int]      ,      __snake_case      :			Optional[float] = None  )    ->				None:
            super().__init__()
            _lowerCAmelCase												=							drop_prob




      def     lowercase__   (      self      :			Tuple      ,      __snake_case      :			torch.Tensor  )    ->				torch.Tensor:
            return drop_path(__snake_case      ,      self.drop_prob      ,      self.training  )




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				str:
            return "p={}".format(self.drop_prob  )




class 			UpperCAmelCase					(      nn.Module       ):




      def __init__(      self      :			Optional[Any]      ,      __snake_case      :			Tuple      ,      __snake_case      :			Any      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Dict      ,      __snake_case      :			int      ,      __snake_case      :			Optional[Any]=None  )    ->				Dict:
            super().__init__()
            _lowerCAmelCase												=							patch_size if isinstance(__snake_case      ,      collections.abc.Iterable  ) else (patch_size, patch_size)
            _lowerCAmelCase												=							stride if isinstance(__snake_case      ,      collections.abc.Iterable  ) else (stride, stride)
            _lowerCAmelCase												=							padding if isinstance(__snake_case      ,      collections.abc.Iterable  ) else (padding, padding)

            _lowerCAmelCase												=							nn.Convad(__snake_case      ,      __snake_case      ,      kernel_size=__snake_case      ,      stride=__snake_case      ,      padding=__snake_case  )
            _lowerCAmelCase												=							norm_layer(__snake_case  ) if norm_layer else nn.Identity()




      def     lowercase__   (      self      :			str      ,      __snake_case      :			Dict  )    ->				int:
            _lowerCAmelCase												=							self.projection(__snake_case  )
            _lowerCAmelCase												=							self.norm(__snake_case  )
            return embeddings




class 			UpperCAmelCase					(      nn.GroupNorm       ):




      def __init__(      self      :			int      ,      __snake_case      :			Tuple      ,      **__snake_case      :			str  )    ->				Dict:
            super().__init__(1      ,      __snake_case      ,      **__snake_case  )




class 			UpperCAmelCase					(      nn.Module       ):




      def __init__(      self      :			Optional[Any]      ,      __snake_case      :			Optional[Any]  )    ->				int:
            super().__init__()
            _lowerCAmelCase												=							nn.AvgPoolad(__snake_case      ,      stride=1      ,      padding=pool_size // 2      ,      count_include_pad=__snake_case  )




      def     lowercase__   (      self      :			List[str]      ,      __snake_case      :			str  )    ->				List[str]:
            return self.pool(__snake_case  ) - hidden_states




class 			UpperCAmelCase					(      nn.Module       ):




      def __init__(      self      :			Dict      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			List[Any]      ,      __snake_case      :			Dict      ,      __snake_case      :			Optional[Any]  )    ->				List[str]:
            super().__init__()
            _lowerCAmelCase												=							nn.Convad(__snake_case      ,      __snake_case      ,      1  )
            _lowerCAmelCase												=							nn.Convad(__snake_case      ,      __snake_case      ,      1  )
            _lowerCAmelCase												=							PoolFormerDropPath(__snake_case  )
            if isinstance(config.hidden_act      ,      __snake_case  ):
                  _lowerCAmelCase												=							ACTaFN[config.hidden_act]
            else:
                  _lowerCAmelCase												=							config.hidden_act




      def     lowercase__   (      self      :			Union[str, Any]      ,      __snake_case      :			Optional[int]  )    ->				Any:
            _lowerCAmelCase												=							self.conva(__snake_case  )
            _lowerCAmelCase												=							self.act_fn(__snake_case  )
            _lowerCAmelCase												=							self.drop(__snake_case  )
            _lowerCAmelCase												=							self.conva(__snake_case  )
            _lowerCAmelCase												=							self.drop(__snake_case  )

            return hidden_states




class 			UpperCAmelCase					(      nn.Module       ):




      def __init__(      self      :			str      ,      __snake_case      :			str      ,      __snake_case      :			int      ,      __snake_case      :			Dict      ,      __snake_case      :			str      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Optional[int]  )    ->				str:
            super().__init__()
            _lowerCAmelCase												=							PoolFormerPooling(__snake_case  )
            _lowerCAmelCase												=							PoolFormerOutput(__snake_case      ,      __snake_case      ,      __snake_case      ,      __snake_case  )
            _lowerCAmelCase												=							PoolFormerGroupNorm(__snake_case  )
            _lowerCAmelCase												=							PoolFormerGroupNorm(__snake_case  )

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




      def     lowercase__   (      self      :			Any      ,      __snake_case      :			Union[str, Any]  )    ->				Optional[int]:
            if self.use_layer_scale:
                  _lowerCAmelCase												=							self.pooling(self.before_norm(__snake_case  )  )
                  _lowerCAmelCase												=							self.layer_scale_a.unsqueeze(-1  ).unsqueeze(-1  ) * pooling_output
                  # First residual connection
                  _lowerCAmelCase												=							hidden_states + self.drop_path(__snake_case  )
                  _lowerCAmelCase												=							()

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

                  _lowerCAmelCase												=							(output,) + outputs
                  return outputs

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

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

                  _lowerCAmelCase												=							(output,) + outputs
                  return outputs




class 			UpperCAmelCase					(      nn.Module       ):




      def __init__(      self      :			List[str]      ,      __snake_case      :			str  )    ->				Dict:
            super().__init__()
            _lowerCAmelCase												=							config
            # stochastic depth decay rule
            _lowerCAmelCase												=							[x.item() for x in torch.linspace(0      ,      config.drop_path_rate      ,      sum(config.depths  )  )]

            # patch embeddings
            _lowerCAmelCase												=							[]
            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												=							nn.ModuleList(__snake_case  )

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

            _lowerCAmelCase												=							nn.ModuleList(__snake_case  )




      def     lowercase__   (      self      :			List[str]      ,      __snake_case      :			str      ,      __snake_case      :			Any=False      ,      __snake_case      :			Dict=True  )    ->				Any:
            _lowerCAmelCase												=							() if output_hidden_states else None

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

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




class 			UpperCAmelCase					(      snake_case_       ):
      _lowercase:      Union[str, Any]											=       PoolFormerConfig
      _lowercase:      Optional[int]											=       '''poolformer'''
      _lowercase:      str											=       '''pixel_values'''
      _lowercase:      Optional[Any]											=       True




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




      def     lowercase__   (      self      :			int      ,      __snake_case      :			int      ,      __snake_case      :			List[Any]=False  )    ->				Tuple:
            if isinstance(__snake_case      ,      __snake_case  ):
                  _lowerCAmelCase												=							value




A__    :       Optional[Any]              =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__    :       List[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.'''							,		snake_case_							,		)
class 			UpperCAmelCase					(      snake_case_       ):




      def __init__(      self      :			Optional[int]      ,      __snake_case      :			str  )    ->				List[str]:
            super().__init__(__snake_case  )
            _lowerCAmelCase												=							config

            _lowerCAmelCase												=							PoolFormerEncoder(__snake_case  )

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




      def     lowercase__   (      self      :			str  )    ->				int:
            return self.embeddings.patch_embeddings




      @add_start_docstrings_to_model_forward(__snake_case  )
      @add_code_sample_docstrings(
          checkpoint=_CHECKPOINT_FOR_DOC      ,      output_type=__snake_case      ,      config_class=_CONFIG_FOR_DOC      ,      modality="""vision"""      ,      expected_output=_EXPECTED_OUTPUT_SHAPE      ,      )
      def     lowercase__   (      self      :			Tuple      ,      __snake_case      :			Optional[torch.FloatTensor] = None      ,      __snake_case      :			Optional[bool] = None      ,      __snake_case      :			Optional[bool] = None      ,      )    ->				Union[Tuple, BaseModelOutputWithNoAttention]:
            _lowerCAmelCase												=							(
                output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
            )
            _lowerCAmelCase												=							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												=							self.encoder(
                __snake_case      ,      output_hidden_states=__snake_case      ,      return_dict=__snake_case      ,      )
            _lowerCAmelCase												=							encoder_outputs[0]

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

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




class 			UpperCAmelCase					(      nn.Module       ):




      def __init__(      self      :			Optional[int]      ,      __snake_case      :			Union[str, Any]  )    ->				Dict:
            super().__init__()
            _lowerCAmelCase												=							nn.Linear(config.hidden_size      ,      config.hidden_size  )




      def     lowercase__   (      self      :			Any      ,      __snake_case      :			Dict  )    ->				Dict:
            _lowerCAmelCase												=							self.dense(__snake_case  )
            return output




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




      def __init__(      self      :			str      ,      __snake_case      :			List[Any]  )    ->				Optional[Any]:
            super().__init__(__snake_case  )
            _lowerCAmelCase												=							config.num_labels
            _lowerCAmelCase												=							PoolFormerModel(__snake_case  )

            # Final norm
            _lowerCAmelCase												=							PoolFormerGroupNorm(config.hidden_sizes[-1]  )
            # Classifier head
            _lowerCAmelCase												=							(
                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(__snake_case  )
      @add_code_sample_docstrings(
          checkpoint=_IMAGE_CLASS_CHECKPOINT      ,      output_type=__snake_case      ,      config_class=_CONFIG_FOR_DOC      ,      expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT      ,      )
      def     lowercase__   (      self      :			List[str]      ,      __snake_case      :			Optional[torch.FloatTensor] = None      ,      __snake_case      :			Optional[torch.LongTensor] = None      ,      __snake_case      :			Optional[bool] = None      ,      __snake_case      :			Optional[bool] = None      ,      )    ->				Union[Tuple, ImageClassifierOutputWithNoAttention]:
            _lowerCAmelCase												=							return_dict if return_dict is not None else self.config.use_return_dict

            _lowerCAmelCase												=							self.poolformer(
                __snake_case      ,      output_hidden_states=__snake_case      ,      return_dict=__snake_case      ,      )

            _lowerCAmelCase												=							outputs[0]

            _lowerCAmelCase												=							self.classifier(self.norm(__snake_case  ).mean([-2, -1]  )  )

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

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

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

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













'''simple docstring'''






import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
      from PIL import Image
else:




      class 			UpperCAmelCase					:




            @staticmethod
            def     lowercase__   (      *__snake_case      :			Optional[Any]      ,      **__snake_case      :			Any  )    ->				Tuple:
                  pass




@is_pipeline_test
@require_torch
@require_vision
class 			UpperCAmelCase					(      unittest.TestCase       ):
      _lowercase:      Union[str, Any]											=       MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




      def     lowercase__   (      self      :			List[str]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			str      ,      __snake_case      :			List[str]  )    ->				int:
            _lowerCAmelCase												=							pipeline("""visual-question-answering"""      ,      model="""hf-internal-testing/tiny-vilt-random-vqa"""  )
            _lowerCAmelCase												=							[
                {
                    """image""": Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  ),
                    """question""": """How many cats are there?""",
                },
                {
                    """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
                    """question""": """How many cats are there?""",
                },
            ]
            return vqa_pipeline, examples




      def     lowercase__   (      self      :			Any      ,      __snake_case      :			List[Any]      ,      __snake_case      :			List[Any]  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							vqa_pipeline(__snake_case      ,      top_k=1  )
            self.assertEqual(
                __snake_case      ,      [
                    [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}],
                    [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}],
                ]      ,      )




      @require_torch
      def     lowercase__   (      self      :			str  )    ->				int:
            _lowerCAmelCase												=							pipeline("""visual-question-answering"""      ,      model="""hf-internal-testing/tiny-vilt-random-vqa"""  )
            _lowerCAmelCase												=							"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
            _lowerCAmelCase												=							"""How many cats are there?"""

            _lowerCAmelCase												=							vqa_pipeline(image=__snake_case      ,      question="""How many cats are there?"""      ,      top_k=2  )
            self.assertEqual(
                __snake_case      ,      [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}, {"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}]  )

            _lowerCAmelCase												=							vqa_pipeline({"""image""": image, """question""": question}      ,      top_k=2  )
            self.assertEqual(
                __snake_case      ,      [{"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}, {"""score""": ANY(__snake_case  ), """answer""": ANY(__snake_case  )}]  )




      @slow
      @require_torch
      def     lowercase__   (      self      :			List[Any]  )    ->				List[str]:
            _lowerCAmelCase												=							pipeline("""visual-question-answering"""      ,      model="""dandelin/vilt-b32-finetuned-vqa"""  )
            _lowerCAmelCase												=							"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
            _lowerCAmelCase												=							"""How many cats are there?"""

            _lowerCAmelCase												=							vqa_pipeline(image=__snake_case      ,      question=__snake_case      ,      top_k=2  )
            self.assertEqual(
                nested_simplify(__snake_case      ,      decimals=4  )      ,      [{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}]  )

            _lowerCAmelCase												=							vqa_pipeline({"""image""": image, """question""": question}      ,      top_k=2  )
            self.assertEqual(
                nested_simplify(__snake_case      ,      decimals=4  )      ,      [{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}]  )

            _lowerCAmelCase												=							vqa_pipeline(
                [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]      ,      top_k=2  )
            self.assertEqual(
                nested_simplify(__snake_case      ,      decimals=4  )      ,      [[{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}]] * 2      ,      )




      @require_tf
      @unittest.skip("""Visual question answering not implemented in TF"""  )
      def     lowercase__   (      self      :			List[str]  )    ->				Union[str, Any]:
            pass






