code: 
  
  
  
  
  
  import inspect
  import unittest
  
  from transformers import is_torch_available, is_vision_available
  from transformers.models.auto import get_values
  from transformers.testing_utils import require_torch, slow, torch_device
  
  from ...test_configuration_common import ConfigTester
  from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
  from ...test_pipeline_mixin import PipelineTesterMixin
  
  
  if is_torch_available():
     import torch
  
     from transformers import MODEL_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
     from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST
  
  
  if is_vision_available():
     from PIL import Image
  
     from transformers import PoolFormerImageProcessor
  
  class 				A    (__UpperCAmelCase				):
  
  
  
        def    __a (					self					)      ->   int:
  
  
  
               '''simple docstring'''
  
               _snake_case      :							Optional[int]     				=						self.config_class(**self.inputs_dict					)
               self.parent.assertTrue(hasattr(lowercase_     ,  '''hidden_sizes'''					)					)
               self.parent.assertTrue(hasattr(lowercase_     ,  '''num_encoder_blocks'''					)					)
  
  
  class 				A    :
  
  
  
        def __init__(					self     ,  lowercase_     ,  lowercase_=13     ,  lowercase_=64     ,  lowercase_=3     ,  lowercase_=4     ,  lowercase_=[2, 2, 2, 2]     ,  lowercase_=[8, 4, 2, 1]     ,  lowercase_=[16, 32, 64, 128]     ,  lowercase_=[1, 4, 8, 16]     ,  lowercase_=False     ,  lowercase_=True     ,  lowercase_="gelu"     ,  lowercase_=0.1     ,  lowercase_=0.02     ,  lowercase_=3     ,  lowercase_=None     ,  )      ->   Optional[int]:
  
  
  
               '''simple docstring'''
  
               _snake_case      :							str     				=						parent
               _snake_case      :							Optional[Any]     				=						batch_size
               _snake_case      :							int     				=						image_size
               _snake_case      :							str     				=						num_channels
               _snake_case      :							Optional[Any]     				=						num_encoder_blocks
               _snake_case      :							Dict     				=						sr_ratios
               _snake_case      :							Tuple     				=						depths
               _snake_case      :							Dict     				=						hidden_sizes
               _snake_case      :							int     				=						downsampling_rates
               _snake_case      :							str     				=						is_training
               _snake_case      :							List[Any]     				=						use_labels
               _snake_case      :							List[Any]     				=						hidden_act
               _snake_case      :							List[str]     				=						hidden_dropout_prob
               _snake_case      :							int     				=						initializer_range
               _snake_case      :							List[str]     				=						num_labels
               _snake_case      :							Tuple     				=						scope
  
  
  
        def    __a (					self					)      ->   List[Any]:
  
  
  
               '''simple docstring'''
  
               _snake_case      :							int     				=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)
  
               _snake_case      :							Optional[Any]     				=						None
               if self.use_labels:
                      _snake_case      :							Any     				=						ids_tensor([self.batch_size, self.image_size, self.image_size]     ,  self.num_labels					)
  
               _snake_case      :							int     				=						PoolFormerConfig(
                   image_size=self.image_size     ,  num_channels=self.num_channels     ,  num_encoder_blocks=self.num_encoder_blocks     ,  depths=self.depths     ,  hidden_sizes=self.hidden_sizes     ,  hidden_act=self.hidden_act     ,  hidden_dropout_prob=self.hidden_dropout_prob     ,  initializer_range=self.initializer_range     ,  )
  
               return config, pixel_values, labels
  
  
  
        def    __a (					self     ,  lowercase_     ,  lowercase_     ,  lowercase_					)      ->   int:
  
  
  
               '''simple docstring'''
  
               _snake_case      :							Optional[Any]     				=						PoolFormerModel(config=lowercase_					)
               model.to(lowercase_					)
               model.eval()
               _snake_case      :							Any     				=						model(lowercase_					)
               _snake_case      :							Union[str, Any]     				=						self.image_size // 32.0
               self.parent.assertEqual(
                   result.last_hidden_state.shape     ,  (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)					)
  
  
  
        def    __a (					self					)      ->   Dict:
  
  
  
               '''simple docstring'''
  
               _snake_case      :							Tuple     				=						self.prepare_config_and_inputs()
               _snake_case				,    _snake_case				,    _snake_case      :							int     				=						config_and_inputs
               _snake_case      :							Optional[int]     				=						{'''pixel_values''': pixel_values}
               return config, inputs_dict
  
  
  @require_torch
  class 				A    (__UpperCAmelCase       ,__UpperCAmelCase       ,unittest.TestCase				):
        _SCREAMING_SNAKE_CASE					=							(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
        _SCREAMING_SNAKE_CASE					=							(
            {"""feature-extraction""": PoolFormerModel, """image-classification""": PoolFormerForImageClassification}
            if is_torch_available()
            else {}
        )
  
        _SCREAMING_SNAKE_CASE					=							False
        _SCREAMING_SNAKE_CASE					=							False
        _SCREAMING_SNAKE_CASE					=							False
        _SCREAMING_SNAKE_CASE					=							False
        _SCREAMING_SNAKE_CASE					=							False
  
  
  
        def    __a (					self					)      ->   str:
  
  
  
               '''simple docstring'''
  
               _snake_case      :							int     				=						PoolFormerModelTester(self					)
               _snake_case      :							int     				=						PoolFormerConfigTester(self     ,  config_class=lowercase_					)
  
  
  
        def    __a (					self					)      ->   Tuple:
  
  
  
               '''simple docstring'''
  
               self.config_tester.run_common_tests()
  
  
  
        def    __a (					self					)      ->   Dict:
  
  
  
               '''simple docstring'''
  
               _snake_case      :							List[str]     				=						self.model_tester.prepare_config_and_inputs()
               self.model_tester.create_and_check_model(*lowercase_					)
  
  
  
        @unittest.skip('''PoolFormer does not use inputs_embeds'''					)
        def    __a (					self					)      ->   Tuple:
  
  
  
               '''simple docstring'''
  
               pass
  
  
  
        @unittest.skip('''PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'''					)
        def    __a (					self					)      ->   List[Any]:
  
  
  
               '''simple docstring'''
  
               pass
  
  
  
        def    __a (					self					)      ->   Optional[int]:
  
  
  
               '''simple docstring'''
  
               def check_hidden_states_output(lowercase_     ,  lowercase_     ,  lowercase_					):
                      _snake_case      :							str     				=						model_class(lowercase_					)
                      model.to(lowercase_					)
                      model.eval()
  
                      with torch.no_grad():
                             _snake_case      :							str     				=						model(**self._prepare_for_class(lowercase_     ,  lowercase_					)					)
  
                      _snake_case      :							int     				=						outputs.hidden_states
  
                      _snake_case      :							Optional[int]     				=						self.model_tester.num_encoder_blocks
                      self.assertEqual(len(lowercase_					)     ,  lowercase_					)
  
                      # verify the first hidden states (first block)
                      self.assertListEqual(
                          list(hidden_states[0].shape[-3:]					)     ,  [
                              self.model_tester.hidden_sizes[0],
                              self.model_tester.image_size // 4,
                              self.model_tester.image_size // 4,
                          ]     ,  )
  
               _snake_case				,    _snake_case      :							Any     				=						self.model_tester.prepare_config_and_inputs_for_common()
  
               for model_class in self.all_model_classes:
                      _snake_case      :							Union[str, Any]     				=						True
                      check_hidden_states_output(lowercase_     ,  lowercase_     ,  lowercase_					)
  
                      # check that output_hidden_states also work using config
                      del inputs_dict["output_hidden_states"]
                      _snake_case      :							Union[str, Any]     				=						True
  
                      check_hidden_states_output(lowercase_     ,  lowercase_     ,  lowercase_					)
  
  
  
        def    __a (					self					)      ->   int:
  
  
  
               '''simple docstring'''
  
               if not self.model_tester.is_training:
                      return
  
               _snake_case				,    _snake_case      :							Any     				=						self.model_tester.prepare_config_and_inputs_for_common()
               _snake_case      :							Any     				=						True
  
               for model_class in self.all_model_classes:
                      if model_class in get_values(lowercase_					):
                             continue
                      _snake_case      :							int     				=						model_class(lowercase_					)
                      model.to(lowercase_					)
                      model.train()
                      _snake_case      :							Dict     				=						self._prepare_for_class(lowercase_     ,  lowercase_     ,  return_labels=lowercase_					)
                      _snake_case      :							str     				=						model(**lowercase_					).loss
                      loss.backward()
  
  
  
        def    __a (					self					)      ->   List[Any]:
  
  
  
               '''simple docstring'''
  
               _snake_case				,    _snake_case      :							int     				=						self.model_tester.prepare_config_and_inputs_for_common()
  
               for model_class in self.all_model_classes:
                      _snake_case      :							List[str]     				=						model_class(lowercase_					)
                      _snake_case      :							Optional[int]     				=						inspect.signature(model.forward					)
                      # signature.parameters is an OrderedDict => so arg_names order is deterministic
                      _snake_case      :							Any     				=						[*signature.parameters.keys()]
  
                      _snake_case      :							Optional[int]     				=						['''pixel_values''']
                      self.assertListEqual(arg_names[:1]     ,  lowercase_					)
  
  
  
        @slow
        def    __a (					self					)      ->   Optional[int]:
  
  
  
               '''simple docstring'''
  
               for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                      _snake_case      :							Dict     				=						PoolFormerModel.from_pretrained(lowercase_					)
                      self.assertIsNotNone(lowercase_					)
  
  
  
  
  def 						A_			(  )			->						Any:
         _snake_case      :							Any     				=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
         return image
  
  @require_torch
  class 				A    (unittest.TestCase				):
  
  
  
        @slow
        def    __a (					self					)      ->   Tuple:
  
  
  
               '''simple docstring'''
  
               _snake_case      :							Optional[int]     				=						PoolFormerImageProcessor()
               _snake_case      :							List[str]     				=						PoolFormerForImageClassification.from_pretrained('''sail/poolformer_s12'''					).to(lowercase_					)
  
               _snake_case      :							Tuple     				=						image_processor(images=prepare_img()     ,  return_tensors='''pt'''					).to(lowercase_					)
  
               # forward pass
               with torch.no_grad():
                      _snake_case      :							int     				=						model(**lowercase_					)
  
               # verify the logits
               _snake_case      :							Optional[Any]     				=						torch.Size((1, 1000)					)
               self.assertEqual(outputs.logits.shape     ,  lowercase_					)
  
               _snake_case      :							Any     				=						torch.tensor([-0.6113, 0.1685, -0.0492]					).to(lowercase_					)
               self.assertTrue(torch.allclose(outputs.logits[0, :3]     ,  lowercase_     ,  atol=1E-4					)					)
  
  
  
  
  
  
  
style_context: 
  
  
  
  
  
  from functools import partial
  from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload
  
  import torch
  import torch.nn as nn
  import torch.types
  
  
  
  
  def 						A_			(  lowercase_      ,		lowercase_      ,		lowercase_  )			->						torch.Tensor:
         # The first operation in a checkpoint can't be in-place, but it's
         # nice to have in-place addition during inference. Thus...
         if not inplace:
                _snake_case      :							Any     				=						ma + ma
         else:
                ma += ma
  
         return ma
  
  
  
  
  def 						A_			(  lowercase_      ,		lowercase_  )			->						torch.Tensor:
         _snake_case      :							str     				=						-1 * len(lowercase_  )
         _snake_case      :							Any     				=						list(range(len(tensor.shape[:zero_index]  )  )  )
         return tensor.permute(first_inds + [zero_index + i for i in inds]  )
  
  
  
  
  def 						A_			(  lowercase_      ,		lowercase_  )			->						torch.Tensor:
         return t.reshape(t.shape[:-no_dims] + (-1,)  )
  
  
  
  
  def 						A_			(  lowercase_      ,		lowercase_      ,		lowercase_      ,		lowercase_ = 1E-4  )			->						torch.Tensor:
         _snake_case      :							Any     				=						mask.expand(*value.shape  )
         return torch.sum(mask * value      ,		dim=lowercase_  ) / (eps + torch.sum(lowercase_      ,		dim=lowercase_  ))
  
  
  
  
  def 						A_			(  lowercase_      ,		lowercase_ = 2.31_25      ,		lowercase_ = 21.68_75      ,		lowercase_ = 64  )			->						torch.Tensor:
         _snake_case      :							int     				=						torch.linspace(lowercase_      ,		lowercase_      ,		no_bins - 1      ,		device=pts.device  )
         _snake_case      :							Optional[Any]     				=						torch.sqrt(torch.sum((pts.unsqueeze(-2  ) - pts.unsqueeze(-3  )) ** 2      ,		dim=-1  )  )
         return torch.bucketize(lowercase_      ,		lowercase_  )
  
  
  
  
  def 						A_			(  lowercase_      ,		lowercase_  )			->						dict:
         _snake_case      :							Dict     				=						dicts[0]
         _snake_case      :							str     				=						{}
         for k, v in first.items():
                _snake_case      :							List[Any]     				=						[d[k] for d in dicts]
                if isinstance(lowercase_      ,		lowercase_  ):
                       _snake_case      :							Any     				=						dict_multimap(lowercase_      ,		lowercase_  )
                else:
                       _snake_case      :							Dict     				=						fn(lowercase_  )
  
         return new_dict
  
  
  
  
  def 						A_			(  lowercase_      ,		lowercase_  )			->						torch.Tensor:
         _snake_case      :							str     				=						v_bins.view(((1,) * len(x.shape  )) + (len(lowercase_  ),)  )
         _snake_case      :							Union[str, Any]     				=						x[..., None] - reshaped_bins
         _snake_case      :							Optional[Any]     				=						torch.argmin(torch.abs(lowercase_  )      ,		dim=-1  )
         return nn.functional.one_hot(lowercase_      ,		num_classes=len(lowercase_  )  ).float()
  
  
  
  
  def 						A_			(  lowercase_      ,		lowercase_      ,		lowercase_ = 0      ,		lowercase_ = 0  )			->						torch.Tensor:
         _snake_case      :							List[Union[slice, torch.Tensor]]     				=						[]
         for i, s in enumerate(data.shape[:no_batch_dims]  ):
                _snake_case      :							Optional[Any]     				=						torch.arange(lowercase_  )
                _snake_case      :							Union[str, Any]     				=						r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape  ) - i - 1)))  )
                ranges.append(lowercase_  )
  
         _snake_case      :							List[Union[slice, torch.Tensor]]     				=						[slice(lowercase_  ) for _ in range(len(data.shape  ) - no_batch_dims  )]
         _snake_case      :							Union[str, Any]     				=						inds
         ranges.extend(lowercase_  )
         # Matt note: Editing this to get around the behaviour of using a list as an array index changing
         # in recent Numpy versions
         return data[tuple(lowercase_  )]
  
  
  lowerCAmelCase_   =   TypeVar("T")
  
  
  
  
  def 						A_			(  lowercase_      ,		lowercase_      ,		lowercase_  )			->						Dict[Any, Union[dict, list, tuple, Any]]:
         _snake_case      :							Dict[Any, Union[dict, list, tuple, Any]]     				=						{}
         for k, v in dic.items():
                if isinstance(lowercase_      ,		lowercase_  ):
                       _snake_case      :							Dict     				=						dict_map(lowercase_      ,		lowercase_      ,		lowercase_  )
                else:
                       _snake_case      :							List[Any]     				=						tree_map(lowercase_      ,		lowercase_      ,		lowercase_  )
  
         return new_dict
  
  
  
  
  @overload
  def 						A_			(  lowercase_      ,		lowercase_      ,		lowercase_  )			->						Any:
         ...
  
  
  
  
  @overload
  def 						A_			(  lowercase_      ,		lowercase_      ,		lowercase_  )			->						dict:
         ...
  
  
  
  
  @overload
  def 						A_			(  lowercase_      ,		lowercase_      ,		lowercase_  )			->						list:
         ...
  
  
  
  
  @overload
  def 						A_			(  lowercase_      ,		lowercase_      ,		lowercase_  )			->						tuple:
         ...
  
  
  
  
  def 						A_			(  lowercase_      ,		lowercase_      ,		lowercase_  )			->						Any:
         if isinstance(lowercase_      ,		lowercase_  ):
                return dict_map(lowercase_      ,		lowercase_      ,		lowercase_  )
         elif isinstance(lowercase_      ,		lowercase_  ):
                return [tree_map(lowercase_      ,		lowercase_      ,		lowercase_  ) for x in tree]
         elif isinstance(lowercase_      ,		lowercase_  ):
                return tuple(tree_map(lowercase_      ,		lowercase_      ,		lowercase_  ) for x in tree  )
         elif isinstance(lowercase_      ,		lowercase_  ):
                return fn(lowercase_  )
         else:
                print(type(lowercase_  )  )
                raise ValueError('''Not supported'''  )
  lowerCAmelCase_   =   partial(tree_map, leaf_type=torch.Tensor)
  
  
  
  
  
  
  