






"""simple docstring"""



import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union

class 			a	(  enum.Enum      ):
    UpperCAmelCase_					:	Tuple      	="timm"
    UpperCAmelCase_					:	Any      	="transformers"


def 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    Optional[Iterable[str]]						,   __snake_case     :    Optional[Iterable[int]]						,   __snake_case     :    Optional[Iterable[str]]       ):





  '''simple docstring'''





  if stage_names is None:
    raise ValueError('Stage_names must be set for transformers backbones'       )

  if out_features is not None:
    if not isinstance(__snake_case						,   (list,)       ):
      raise ValueError(f'out_features must be a list {type(__snake_case       )}'       )
    if any(feat not in stage_names for feat in out_features       ):
      raise ValueError(f'out_features must be a subset of stage_names: {stage_names} got {out_features}'       )

  if out_indices is not None:
    if not isinstance(__snake_case						,   (list, tuple)       ):
      raise ValueError(f'out_indices must be a list or tuple, got {type(__snake_case       )}'       )
    if any(idx >= len(__snake_case       ) for idx in out_indices       ):
      raise ValueError('out_indices must be valid indices for stage_names {stage_names}, got {out_indices}'       )

  if out_features is not None and out_indices is not None:
    if len(__snake_case       ) != len(__snake_case       ):
      raise ValueError('out_features and out_indices should have the same length if both are set'       )
    if out_features != [stage_names[idx] for idx in out_indices]:
      raise ValueError('out_features and out_indices should correspond to the same stages if both are set'       )
def 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    Optional[List[str]]						,   __snake_case     :    Optional[Union[List[int], Tuple[int]]]						,   __snake_case     :    List[str]						,   ):





  '''simple docstring'''





  if out_indices is None and out_features is None:
    lowercase				       =			[len(__snake_case       ) - 1]
    lowercase				       =			[stage_names[-1]]
  elif out_indices is None and out_features is not None:
    lowercase				       =			[stage_names.index(__snake_case       ) for layer in out_features]
  elif out_features is None and out_indices is not None:
    lowercase				       =			[stage_names[idx] for idx in out_indices]
  return out_features, out_indices
def 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    Optional[List[str]]						,   __snake_case     :    Optional[Union[List[int], Tuple[int]]]						,   __snake_case     :    List[str]						,   ):





  '''simple docstring'''





  verify_out_features_out_indices(out_features=__snake_case						,   out_indices=__snake_case						,   stage_names=__snake_case       )
  lowercase       ,	lowercase				       =			_align_output_features_output_indices(
      out_features=__snake_case						,   out_indices=__snake_case						,   stage_names=__snake_case       )
  # Verify that the aligned out_features and out_indices are valid
  verify_out_features_out_indices(out_features=__snake_case						,   out_indices=__snake_case						,   stage_names=__snake_case       )
  return output_features, output_indices

class 			a	:
    UpperCAmelCase_					:	Optional[BackboneType]      	=None







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      if getattr(self				,						'_backbone'				,						_lowerCamelCase			) is None:
        raise ValueError('self._backbone must be set before calling _init_timm_backbone'			)

      # These will diagree with the defaults for the transformers models e.g. for resnet50
      # the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
      # the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
      lowercase				       =			[stage['module'] for stage in self._backbone.feature_info.info]
      lowercase				       =			[stage['num_chs'] for stage in self._backbone.feature_info.info]
      lowercase				       =			self._backbone.feature_info.out_indices
      lowercase				       =			self._backbone.feature_info.module_name()

      # We verify the out indices and out features are valid
      verify_out_features_out_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=self.stage_names			)
      lowercase       ,	lowercase				       =			out_features, out_indices







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase				       =			getattr(_lowerCamelCase				,						'stage_names'			)
      lowercase				       =			getattr(_lowerCamelCase				,						'out_features'				,						_lowerCamelCase			)
      lowercase				       =			getattr(_lowerCamelCase				,						'out_indices'				,						_lowerCamelCase			)

      lowercase				       =			stage_names
      lowercase       ,	lowercase				       =			get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=_lowerCamelCase			)
      # Number of channels for each stage. This is set in the transformer backbone model init
      lowercase				       =			None







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase				       =			config

      lowercase				       =			getattr(_lowerCamelCase				,						'use_timm_backbone'				,						_lowerCamelCase			)
      lowercase				       =			BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

      if self.backbone_type == BackboneType.TIMM:
        self._init_timm_backbone(_lowerCamelCase			)
      elif self.backbone_type == BackboneType.TRANSFORMERS:
        self._init_transformers_backbone(_lowerCamelCase			)
      else:
        raise ValueError(F'backbone_type {self.backbone_type} not supported.'			)







    @property
    def      UpperCamelCase_ (     self			):
      return self._out_features







    @out_features.setter
    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase       ,	lowercase				       =			get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=self.stage_names			)







    @property
    def      UpperCamelCase_ (     self			):
      return self._out_indices







    @out_indices.setter
    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase       ,	lowercase				       =			get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=self.stage_names			)







    @property
    def      UpperCamelCase_ (     self			):
      # the current backbones will output the number of channels for each stage
      # even if that stage is not in the out_features list.
      return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names			)}







    @property
    def      UpperCamelCase_ (     self			):
      return [self.out_feature_channels[name] for name in self.out_features]







    def      UpperCamelCase_ (     self				,						*_lowerCamelCase				,						**_lowerCamelCase			):
      lowercase				       =			dict(inspect.signature(self.forward			).parameters			)
      lowercase				       =			{k: v for k, v in kwargs.items() if k in signature}
      return self(*_lowerCamelCase				,						**_lowerCamelCase			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase = None				,						_lowerCamelCase = None				,						_lowerCamelCase = None				,						):
      raise NotImplementedError('This method should be implemented by the derived class.'			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			super().to_dict()
      lowercase				       =			output.pop('_out_features'			)
      lowercase				       =			output.pop('_out_indices'			)
      return output


class 			a	:







    @property
    def      UpperCamelCase_ (     self			):
      return self._out_features







    @out_features.setter
    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase       ,	lowercase				       =			get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=self.stage_names			)







    @property
    def      UpperCamelCase_ (     self			):
      return self._out_indices







    @out_indices.setter
    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase       ,	lowercase				       =			get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=self.stage_names			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			super().to_dict()
      lowercase				       =			output.pop('_out_features'			)
      lowercase				       =			output.pop('_out_indices'			)
      return output








'''simple docstring'''







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    (      __a					):
       def        A (      self		:      Tuple							):



           '''simple docstring'''

           UpperCAmelCase     			=						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		:      Any  ,							lowercase		:      Any  ,							lowercase		:      Optional[Any]=13  ,							lowercase		:      List[str]=64  ,							lowercase		:      Optional[Any]=3  ,							lowercase		:      List[str]=4  ,							lowercase		:      Optional[int]=[2, 2, 2, 2]  ,							lowercase		:      Tuple=[8, 4, 2, 1]  ,							lowercase		:      Optional[int]=[16, 32, 64, 128]  ,							lowercase		:      Tuple=[1, 4, 8, 16]  ,							lowercase		:      List[Any]=False  ,							lowercase		:      List[Any]=True  ,							lowercase		:      List[str]="gelu"  ,							lowercase		:      Any=0.1  ,							lowercase		:      Dict=0.02  ,							lowercase		:      str=3  ,							lowercase		:      Optional[Any]=None  ,							):



           '''simple docstring'''

           UpperCAmelCase     			=						parent
           UpperCAmelCase     			=						batch_size
           UpperCAmelCase     			=						image_size
           UpperCAmelCase     			=						num_channels
           UpperCAmelCase     			=						num_encoder_blocks
           UpperCAmelCase     			=						sr_ratios
           UpperCAmelCase     			=						depths
           UpperCAmelCase     			=						hidden_sizes
           UpperCAmelCase     			=						downsampling_rates
           UpperCAmelCase     			=						is_training
           UpperCAmelCase     			=						use_labels
           UpperCAmelCase     			=						hidden_act
           UpperCAmelCase     			=						hidden_dropout_prob
           UpperCAmelCase     			=						initializer_range
           UpperCAmelCase     			=						num_labels
           UpperCAmelCase     			=						scope
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

           UpperCAmelCase     			=						None
           if self.use_labels:
               UpperCAmelCase     			=						ids_tensor([self.batch_size, self.image_size, self.image_size]  ,							self.num_labels							)

           UpperCAmelCase     			=						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		:      Optional[Any]  ,							lowercase		:      List[Any]  ,							lowercase		:      Dict  ,							lowercase		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						PoolFormerModel(config=lowercase							)
           model.to(lowercase							)
           model.eval()
           UpperCAmelCase     			=						model(lowercase							)
           UpperCAmelCase     			=						UpperCAmelCase     			=						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		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.prepare_config_and_inputs()
           UpperCAmelCase			,	UpperCAmelCase			,	UpperCAmelCase     			=						config_and_inputs
           UpperCAmelCase     			=						{'''pixel_values''': pixel_values}
           return config, inputs_dict






@require_torch
class      _a    (      __a				,				__a				,				unittest.TestCase					):
       __a							:	Optional[int]  							=   (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
       __a							:	Dict  							=   (
           {"""feature-extraction""": PoolFormerModel, """image-classification""": PoolFormerForImageClassification}
           if is_torch_available()
           else {}
       )

       __a							:	str  							=   False
       __a							:	Optional[int]  							=   False
       __a							:	Tuple  							=   False
       __a							:	Union[str, Any]  							=   False
       __a							:	Dict  							=   False
       def        A (      self		:      Any							):



           '''simple docstring'''

           UpperCAmelCase     			=						PoolFormerModelTester(self							)
           UpperCAmelCase     			=						PoolFormerConfigTester(self  ,							config_class=lowercase							)
       def        A (      self		:      Any							):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       def        A (      self		:      Optional[int]							):



           '''simple docstring'''

           UpperCAmelCase     			=						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		:      Dict							):



           '''simple docstring'''

           pass
       @unittest.skip('''PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'''							)
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           pass
       def        A (      self		:      Dict							):



           '''simple docstring'''

           def check_hidden_states_output(lowercase		:      int  ,							lowercase		:      Any  ,							lowercase		:      Union[str, Any]							):
               UpperCAmelCase     			=						model_class(lowercase							)
               model.to(lowercase							)
               model.eval()

               with torch.no_grad():
                   UpperCAmelCase     			=						model(**self._prepare_for_class(lowercase  ,							lowercase							)							)

               UpperCAmelCase     			=						outputs.hidden_states

               UpperCAmelCase     			=						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,
                   ]  ,							)

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase     			=						True
               check_hidden_states_output(lowercase  ,							lowercase  ,							lowercase							)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               UpperCAmelCase     			=						True

               check_hidden_states_output(lowercase  ,							lowercase  ,							lowercase							)
       def        A (      self		:      Optional[Any]							):



           '''simple docstring'''

           if not self.model_tester.is_training:
               return

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()
           UpperCAmelCase     			=						True

           for model_class in self.all_model_classes:
               if model_class in get_values(lowercase							):
                   continue
               UpperCAmelCase     			=						model_class(lowercase							)
               model.to(lowercase							)
               model.train()
               UpperCAmelCase     			=						self._prepare_for_class(lowercase  ,							lowercase  ,							return_labels=lowercase							)
               UpperCAmelCase     			=						model(**lowercase							).loss
               loss.backward()
       def        A (      self		:      str							):



           '''simple docstring'''

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase     			=						model_class(lowercase							)
               UpperCAmelCase     			=						inspect.signature(model.forward							)
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               UpperCAmelCase     			=						[*signature.parameters.keys()]

               UpperCAmelCase     			=						['''pixel_values''']
               self.assertListEqual(arg_names[:1]  ,							lowercase							)







       @slow
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase     			=						PoolFormerModel.from_pretrained(lowercase							)
               self.assertIsNotNone(lowercase							)






def 		snake_case_				():
    UpperCAmelCase     			=						Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
    return image







@require_torch
class      _a    (      unittest.TestCase					):
       @slow
       def        A (      self		:      str							):



           '''simple docstring'''

           UpperCAmelCase     			=						PoolFormerImageProcessor()
           UpperCAmelCase     			=						PoolFormerForImageClassification.from_pretrained('''sail/poolformer_s12'''							).to(lowercase							)

           UpperCAmelCase     			=						image_processor(images=prepare_img()  ,							return_tensors='''pt'''							).to(lowercase							)

           # forward pass
           with torch.no_grad():
               UpperCAmelCase     			=						model(**lowercase							)

           # verify the logits
           UpperCAmelCase     			=						torch.Size((1, 1_000)							)
           self.assertEqual(outputs.logits.shape  ,							lowercase							)

           UpperCAmelCase     			=						torch.tensor([-0.6113, 0.1685, -0.0492]							).to(lowercase							)
           self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,							lowercase  ,							atol=1E-4							)							)




