






"""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  __lowerCamelCase     (	_a	):

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Dict:
       UpperCamelCase__								=     self.config_class(**self.inputs_dict   )
       self.parent.assertTrue(hasattr(snake_case_			,					'hidden_sizes'   )   )
       self.parent.assertTrue(hasattr(snake_case_			,					'num_encoder_blocks'   )   )







class  __lowerCamelCase     :

   def __init__(       self			,					snake_case_			,					snake_case_=13			,					snake_case_=64			,					snake_case_=3			,					snake_case_=4			,					snake_case_=[2, 2, 2, 2]			,					snake_case_=[8, 4, 2, 1]			,					snake_case_=[16, 32, 64, 128]			,					snake_case_=[1, 4, 8, 16]			,					snake_case_=False			,					snake_case_=True			,					snake_case_="gelu"			,					snake_case_=0.1			,					snake_case_=0.02			,					snake_case_=3			,					snake_case_=None			,					)		->							str:
       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     SCREAMING_SNAKE_CASE__			(       self   )		->							Union[str, Any]:
       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     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_			,					snake_case_			,					snake_case_   )		->							int:
       UpperCamelCase__								=     PoolFormerModel(config=snake_case_   )
       model.to(snake_case_   )
       model.eval()
       UpperCamelCase__								=     model(snake_case_   )
       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     SCREAMING_SNAKE_CASE__			(       self   )		->							Union[str, Any]:
       UpperCamelCase__								=     self.prepare_config_and_inputs()
       UpperCamelCase__							,    UpperCamelCase__							,    UpperCamelCase__								=     config_and_inputs
       UpperCamelCase__								=     {'pixel_values': pixel_values}
       return config, inputs_dict







@require_torch
class  __lowerCamelCase     (	_a				,						_a				,						unittest.TestCase	):
   a   :					int			    =(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
   a   :					Any			    =(
       {"""feature-extraction""": PoolFormerModel, """image-classification""": PoolFormerForImageClassification}
       if is_torch_available()
       else {}
   )

   a   :					Dict			    =False
   a   :					Dict			    =False
   a   :					str			    =False
   a   :					int			    =False
   a   :					Optional[int]			    =False

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Optional[int]:
       UpperCamelCase__								=     PoolFormerModelTester(self   )
       UpperCamelCase__								=     PoolFormerConfigTester(self			,					config_class=snake_case_   )

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							str:
       self.config_tester.run_common_tests()

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Tuple:
       UpperCamelCase__								=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*snake_case_   )

   @unittest.skip('PoolFormer does not use inputs_embeds'   )
   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Optional[Any]:
       pass

   @unittest.skip('PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'   )
   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Optional[Any]:
       pass

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Dict:
       def check_hidden_states_output(snake_case_			,					snake_case_			,					snake_case_   ):
           UpperCamelCase__								=     model_class(snake_case_   )
           model.to(snake_case_   )
           model.eval()

           with torch.no_grad():
               UpperCamelCase__								=     model(**self._prepare_for_class(snake_case_			,					snake_case_   )   )

           UpperCamelCase__								=     outputs.hidden_states

           UpperCamelCase__								=     self.model_tester.num_encoder_blocks
           self.assertEqual(len(snake_case_   )			,					snake_case_   )

           # 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(snake_case_			,					snake_case_			,					snake_case_   )

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

           check_hidden_states_output(snake_case_			,					snake_case_			,					snake_case_   )

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Optional[Any]:
       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(snake_case_   ):
               continue
           UpperCamelCase__								=     model_class(snake_case_   )
           model.to(snake_case_   )
           model.train()
           UpperCamelCase__								=     self._prepare_for_class(snake_case_			,					snake_case_			,					return_labels=snake_case_   )
           UpperCamelCase__								=     model(**snake_case_   ).loss
           loss.backward()

   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Union[str, Any]:
       UpperCamelCase__							,    UpperCamelCase__								=     self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
           UpperCamelCase__								=     model_class(snake_case_   )
           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]			,					snake_case_   )







   @slow
   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Optional[int]:
       for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__								=     PoolFormerModel.from_pretrained(snake_case_   )
           self.assertIsNotNone(snake_case_   )







def 	lowerCAmelCase_(			)       ->						Any:
    """simple docstring"""

    UpperCamelCase__								=     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
    return image





@require_torch
class  __lowerCamelCase     (	unittest.TestCase	):

   @slow
   def     SCREAMING_SNAKE_CASE__			(       self   )		->							Any:
       UpperCamelCase__								=     PoolFormerImageProcessor()
       UpperCamelCase__								=     PoolFormerForImageClassification.from_pretrained('sail/poolformer_s12'   ).to(snake_case_   )

       UpperCamelCase__								=     image_processor(images=prepare_img()			,					return_tensors='pt'   ).to(snake_case_   )

       # forward pass
       with torch.no_grad():
           UpperCamelCase__								=     model(**snake_case_   )

       # verify the logits
       UpperCamelCase__								=     torch.Size((1, 1000)   )
       self.assertEqual(outputs.logits.shape			,					snake_case_   )

       UpperCamelCase__								=     torch.tensor([-0.6_113, 0.1_685, -0.0_492]   ).to(snake_case_   )
       self.assertTrue(torch.allclose(outputs.logits[0, :3]			,					snake_case_			,					atol=1E-4   )   )












"""simple docstring"""





import torch

from transformers import PreTrainedModel

from .custom_configuration import CustomConfig, NoSuperInitConfig
class  __lowerCamelCase     (	_a	):
   a   :					List[Any]			    =CustomConfig

   def __init__(       self			,					snake_case_   )		->							str:
       super().__init__(snake_case_   )
       UpperCamelCase__								=     torch.nn.Linear(config.hidden_size			,					config.hidden_size   )

   def     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_   )		->							Optional[int]:
       return self.linear(snake_case_   )







   def     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_   )		->							Union[str, Any]:
       pass







class  __lowerCamelCase     (	_a	):
   a   :					Optional[int]			    =NoSuperInitConfig

   def __init__(       self			,					snake_case_   )		->							Optional[int]:
       super().__init__(snake_case_   )
       UpperCamelCase__								=     torch.nn.Linear(config.attribute			,					config.attribute   )

   def     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_   )		->							int:
       return self.linear(snake_case_   )







   def     SCREAMING_SNAKE_CASE__			(       self			,					snake_case_   )		->							Union[str, Any]:
       pass




