
import inspect
import unittest
import warnings
from typing import List

from transformers import EfficientFormerConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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_FOR_IMAGE_CLASSIFICATION_MAPPING,
     MODEL_MAPPING,
     EfficientFormerForImageClassification,
     EfficientFormerForImageClassificationWithTeacher,
     EfficientFormerModel,
 )
 from transformers.models.efficientformer.modeling_efficientformer import (
     EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
 )


if is_vision_available():
 from PIL import Image

 from transformers import EfficientFormerImageProcessor







class       UpperCAmelCase_						:



      """simple docstring"""
      def __init__(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_ = 13   ,    SCREAMING_SNAKE_CASE_ = 64   ,    SCREAMING_SNAKE_CASE_ = 2   ,    SCREAMING_SNAKE_CASE_ = 3   ,    SCREAMING_SNAKE_CASE_ = 3   ,    SCREAMING_SNAKE_CASE_ = True   ,    SCREAMING_SNAKE_CASE_ = True   ,    SCREAMING_SNAKE_CASE_ = 128   ,    SCREAMING_SNAKE_CASE_=[16, 32, 64, 128]   ,    SCREAMING_SNAKE_CASE_ = 7   ,    SCREAMING_SNAKE_CASE_ = 4   ,    SCREAMING_SNAKE_CASE_ = 37   ,    SCREAMING_SNAKE_CASE_ = "gelu"   ,    SCREAMING_SNAKE_CASE_ = 0.1   ,    SCREAMING_SNAKE_CASE_ = 0.1   ,    SCREAMING_SNAKE_CASE_ = 10   ,    SCREAMING_SNAKE_CASE_ = 0.02   ,    SCREAMING_SNAKE_CASE_ = 2   ,    SCREAMING_SNAKE_CASE_ = 1   ,    SCREAMING_SNAKE_CASE_ = 128   ,    SCREAMING_SNAKE_CASE_ = [2, 2, 2, 2]   ,    SCREAMING_SNAKE_CASE_ = 2   ,    SCREAMING_SNAKE_CASE_ = 2   ,    )    ->				Optional[int]:
          UpperCamelCase	:int     							=       parent
          UpperCamelCase	:Optional[Any]     							=       batch_size
          UpperCamelCase	:Optional[int]     							=       image_size
          UpperCamelCase	:Optional[Any]     							=       patch_size
          UpperCamelCase	:Dict     							=       num_channels
          UpperCamelCase	:int     							=       is_training
          UpperCamelCase	:Dict     							=       use_labels
          UpperCamelCase	:Optional[Any]     							=       hidden_size
          UpperCamelCase	:Any     							=       num_hidden_layers
          UpperCamelCase	:List[str]     							=       num_attention_heads
          UpperCamelCase	:int     							=       intermediate_size
          UpperCamelCase	:List[str]     							=       hidden_act
          UpperCamelCase	:Optional[Any]     							=       hidden_dropout_prob
          UpperCamelCase	:List[Any]     							=       attention_probs_dropout_prob
          UpperCamelCase	:Optional[Any]     							=       type_sequence_label_size
          UpperCamelCase	:Optional[int]     							=       initializer_range
          UpperCamelCase	:Tuple     							=       encoder_stride
          UpperCamelCase	:str     							=       num_attention_outputs
          UpperCamelCase	:Any     							=       embed_dim
          UpperCamelCase	:str     							=       embed_dim + 1
          UpperCamelCase	:List[Any]     							=       resolution
          UpperCamelCase	:List[Any]     							=       depths
          UpperCamelCase	:List[Any]     							=       hidden_sizes
          UpperCamelCase	:Union[str, Any]     							=       dim
          UpperCamelCase	:Union[str, Any]     							=       mlp_expansion_ratio
      def    UpperCAmelCase						(    self			)    ->				Optional[int]:
          UpperCamelCase	:Optional[Any]     							=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

          UpperCamelCase	:int     							=       None
          if self.use_labels:
              UpperCamelCase	:List[str]     							=       ids_tensor([self.batch_size]   ,    self.type_sequence_label_size			)

          UpperCamelCase	:int     							=       self.get_config()
          return config, pixel_values, labels
      def    UpperCAmelCase						(    self			)    ->				Tuple:
          return EfficientFormerConfig(
              image_size=self.image_size   ,    patch_size=self.patch_size   ,    num_channels=self.num_channels   ,    hidden_size=self.hidden_size   ,    num_hidden_layers=self.num_hidden_layers   ,    num_attention_heads=self.num_attention_heads   ,    intermediate_size=self.intermediate_size   ,    hidden_act=self.hidden_act   ,    hidden_dropout_prob=self.hidden_dropout_prob   ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,    is_decoder=__lowercase   ,    initializer_range=self.initializer_range   ,    encoder_stride=self.encoder_stride   ,    resolution=self.resolution   ,    depths=self.depths   ,    hidden_sizes=self.hidden_sizes   ,    dim=self.dim   ,    mlp_expansion_ratio=self.mlp_expansion_ratio   ,    )
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)    ->				Dict:
          UpperCamelCase	:Dict     							=       EfficientFormerModel(config=__lowercase			)
          model.to(__lowercase			)
          model.eval()
          UpperCamelCase	:int     							=       model(__lowercase			)
          self.parent.assertEqual(result.last_hidden_state.shape   ,    (self.batch_size, self.seq_length, self.hidden_size)			)
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)    ->				Tuple:
          UpperCamelCase	:Optional[int]     							=       self.type_sequence_label_size
          UpperCamelCase	:int     							=       EfficientFormerForImageClassification(__lowercase			)
          model.to(__lowercase			)
          model.eval()
          UpperCamelCase	:Any     							=       model(__lowercase   ,    labels=__lowercase			)
          self.parent.assertEqual(result.logits.shape   ,    (self.batch_size, self.type_sequence_label_size)			)

          # test greyscale images
          UpperCamelCase	:Optional[Any]     							=       1
          UpperCamelCase	:Optional[Any]     							=       EfficientFormerForImageClassification(__lowercase			)
          model.to(__lowercase			)
          model.eval()

          UpperCamelCase	:str     							=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
          UpperCamelCase	:Any     							=       model(__lowercase			)
          self.parent.assertEqual(result.logits.shape   ,    (self.batch_size, self.type_sequence_label_size)			)




      def    UpperCAmelCase						(    self			)    ->				int:
          UpperCamelCase	:List[str]     							=       self.prepare_config_and_inputs()
          (
              (
              UpperCamelCase				
          )							,       (
              UpperCamelCase				
          )							,       (
              UpperCamelCase				
          )							,       				
          )	:Any     							=       config_and_inputs
          UpperCamelCase	:List[str]     							=       {'''pixel_values''': pixel_values}
          return config, inputs_dict







@require_torch
class       UpperCAmelCase_						( UpperCAmelCase__,     UpperCAmelCase__,     unittest.TestCase			):



      """simple docstring"""
      UpperCamelCase_    : Dict									=(
          (
              EfficientFormerModel,
              EfficientFormerForImageClassificationWithTeacher,
              EfficientFormerForImageClassification,
          )
          if is_torch_available()
          else ()
      )
      UpperCamelCase_    : str									=(
          {
              'feature-extraction': EfficientFormerModel,
              'image-classification': (
                  EfficientFormerForImageClassification,
                  EfficientFormerForImageClassificationWithTeacher,
              ),
          }
          if is_torch_available()
          else {}
      )
      UpperCamelCase_    : Tuple									=False

      UpperCamelCase_    : Any									=False
      UpperCamelCase_    : Tuple									=False
      UpperCamelCase_    : List[str]									=False
      def    UpperCAmelCase						(    self			)    ->				Any:
          UpperCamelCase	:Tuple     							=       EfficientFormerModelTester(self			)
          UpperCamelCase	:int     							=       ConfigTester(
              self   ,    config_class=__lowercase   ,    has_text_modality=__lowercase   ,    hidden_size=37			)
      def    UpperCAmelCase						(    self			)    ->				List[Any]:
          self.config_tester.run_common_tests()
      @unittest.skip(reason='''EfficientFormer does not use inputs_embeds'''			)
      def    UpperCAmelCase						(    self			)    ->				Union[str, Any]:
          pass
      @unittest.skip(reason='''EfficientFormer does not support input and output embeddings'''			)
      def    UpperCAmelCase						(    self			)    ->				Union[str, Any]:
          pass
      def    UpperCAmelCase						(    self			)    ->				Optional[Any]:
          UpperCamelCase							,       UpperCamelCase	:Dict     							=       self.model_tester.prepare_config_and_inputs_for_common()

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

              UpperCamelCase	:str     							=       ['''pixel_values''']
              self.assertListEqual(arg_names[:1]   ,    __lowercase			)
      def    UpperCAmelCase						(    self			)    ->				Optional[Any]:






          def check_hidden_states_output(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			):
              UpperCamelCase	:str     							=       model_class(__lowercase			)
              model.to(__lowercase			)
              model.eval()

              with torch.no_grad():
                  UpperCamelCase	:Optional[int]     							=       model(**self._prepare_for_class(__lowercase   ,    __lowercase			)			)

              UpperCamelCase	:List[str]     							=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

              UpperCamelCase	:Tuple     							=       getattr(
                  self.model_tester   ,    '''expected_num_hidden_layers'''   ,    self.model_tester.num_hidden_layers + 1			)
              self.assertEqual(len(__lowercase			)   ,    __lowercase			)

              if hasattr(self.model_tester   ,    '''encoder_seq_length'''			):
                  UpperCamelCase	:int     							=       self.model_tester.encoder_seq_length
                  if hasattr(self.model_tester   ,    '''chunk_length'''			) and self.model_tester.chunk_length > 1:
                      UpperCamelCase	:Optional[Any]     							=       seq_length * self.model_tester.chunk_length
              else:
                  UpperCamelCase	:Dict     							=       self.model_tester.seq_length

              self.assertListEqual(
                  list(hidden_states[-1].shape[-2:]			)   ,    [seq_length, self.model_tester.hidden_size]   ,    )

              if config.is_encoder_decoder:
                  UpperCamelCase	:str     							=       outputs.decoder_hidden_states

                  self.assertIsInstance(__lowercase   ,    (list, tuple)			)
                  self.assertEqual(len(__lowercase			)   ,    __lowercase			)
                  UpperCamelCase	:Optional[int]     							=       getattr(self.model_tester   ,    '''seq_length'''   ,    __lowercase			)
                  UpperCamelCase	:int     							=       getattr(self.model_tester   ,    '''decoder_seq_length'''   ,    __lowercase			)

                  self.assertListEqual(
                      list(hidden_states[-1].shape[-2:]			)   ,    [decoder_seq_length, self.model_tester.hidden_size]   ,    )

          UpperCamelCase							,       UpperCamelCase	:Union[str, Any]     							=       self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              UpperCamelCase	:Tuple     							=       True
              check_hidden_states_output(__lowercase   ,    __lowercase   ,    __lowercase			)

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              UpperCamelCase	:Any     							=       True

              check_hidden_states_output(__lowercase   ,    __lowercase   ,    __lowercase			)
      def    UpperCAmelCase						(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_=False			)    ->				Dict:
          UpperCamelCase	:str     							=       super()._prepare_for_class(__lowercase   ,    __lowercase   ,    return_labels=__lowercase			)

          if return_labels:
              if model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher":
                  del inputs_dict["labels"]

          return inputs_dict
      def    UpperCAmelCase						(    self			)    ->				Union[str, Any]:
          UpperCamelCase	:Optional[int]     							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__lowercase			)
      @unittest.skip(reason='''EfficientFormer does not implement masked image modeling yet'''			)
      def    UpperCAmelCase						(    self			)    ->				int:
          UpperCamelCase	:int     							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*__lowercase			)
      def    UpperCAmelCase						(    self			)    ->				Tuple:
          UpperCamelCase	:List[Any]     							=       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*__lowercase			)
      def    UpperCAmelCase						(    self			)    ->				int:






          if not self.model_tester.is_training:
              return

          UpperCamelCase							,       UpperCamelCase	:Tuple     							=       self.model_tester.prepare_config_and_inputs_for_common()
          UpperCamelCase	:Tuple     							=       True

          for model_class in self.all_model_classes:
              # EfficientFormerForImageClassificationWithTeacher supports inference-only
              if (
                  model_class in get_values(__lowercase			)
                  or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
              ):
                  continue
              UpperCamelCase	:Any     							=       model_class(__lowercase			)
              model.to(__lowercase			)
              model.train()
              UpperCamelCase	:str     							=       self._prepare_for_class(__lowercase   ,    __lowercase   ,    return_labels=__lowercase			)
              UpperCamelCase	:List[str]     							=       model(**__lowercase			).loss
              loss.backward()
      def    UpperCAmelCase						(    self			)    ->				List[str]:
          UpperCamelCase							,       UpperCamelCase	:Dict     							=       self.model_tester.prepare_config_and_inputs_for_common()

          UpperCamelCase	:str     							=       [
              {'''title''': '''multi_label_classification''', '''num_labels''': 2, '''dtype''': torch.float},
              {'''title''': '''single_label_classification''', '''num_labels''': 1, '''dtype''': torch.long},
              {'''title''': '''regression''', '''num_labels''': 1, '''dtype''': torch.float},
          ]

          for model_class in self.all_model_classes:
              if (
                  model_class
                  not in [
                      *get_values(__lowercase			),
                  ]
                  or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
              ):
                  continue

              for problem_type in problem_types:
                  with self.subTest(msg=F'''Testing {model_class} with {problem_type["title"]}'''			):
                      UpperCamelCase	:Union[str, Any]     							=       problem_type['''title''']
                      UpperCamelCase	:Dict     							=       problem_type['''num_labels''']

                      UpperCamelCase	:Any     							=       model_class(__lowercase			)
                      model.to(__lowercase			)
                      model.train()

                      UpperCamelCase	:Optional[int]     							=       self._prepare_for_class(__lowercase   ,    __lowercase   ,    return_labels=__lowercase			)

                      if problem_type["num_labels"] > 1:
                          UpperCamelCase	:int     							=       inputs['''labels'''].unsqueeze(1			).repeat(1   ,    problem_type['''num_labels''']			)

                      UpperCamelCase	:str     							=       inputs['''labels'''].to(problem_type['''dtype''']			)

                      # This tests that we do not trigger the warning form PyTorch "Using a target size that is different
                      # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
                      # they have the same size." which is a symptom something in wrong for the regression problem.
                      # See https://github.com/huggingface/transformers/issues/11780
                      with warnings.catch_warnings(record=__lowercase			) as warning_list:
                          UpperCamelCase	:Optional[int]     							=       model(**__lowercase			).loss
                      for w in warning_list:
                          if "Using a target size that is different to the input size" in str(w.message			):
                              raise ValueError(
                                  F'''Something is going wrong in the regression problem: intercepted {w.message}'''			)

                      loss.backward()
      @slow
      def    UpperCAmelCase						(    self			)    ->				List[Any]:






          for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCamelCase	:List[str]     							=       EfficientFormerModel.from_pretrained(__lowercase			)
              self.assertIsNotNone(__lowercase			)




      def    UpperCAmelCase						(    self			)    ->				str:
          UpperCamelCase							,       UpperCamelCase	:Union[str, Any]     							=       self.model_tester.prepare_config_and_inputs_for_common()
          UpperCamelCase	:List[Any]     							=       True

          UpperCamelCase	:Any     							=       getattr(self.model_tester   ,    '''seq_length'''   ,    __lowercase			)
          UpperCamelCase	:Dict     							=       getattr(self.model_tester   ,    '''encoder_seq_length'''   ,    __lowercase			)
          UpperCamelCase	:List[str]     							=       getattr(self.model_tester   ,    '''key_length'''   ,    __lowercase			)
          UpperCamelCase	:Tuple     							=       getattr(self.model_tester   ,    '''chunk_length'''   ,    __lowercase			)

          if chunk_length is not None and hasattr(self.model_tester   ,    '''num_hashes'''			):
              UpperCamelCase	:Optional[int]     							=       encoder_seq_length * self.model_tester.num_hashes

          for model_class in self.all_model_classes:
              UpperCamelCase	:List[Any]     							=       True
              UpperCamelCase	:Tuple     							=       False
              UpperCamelCase	:int     							=       True
              UpperCamelCase	:Union[str, Any]     							=       model_class(__lowercase			)
              model.to(__lowercase			)
              model.eval()
              with torch.no_grad():
                  UpperCamelCase	:Tuple     							=       model(**self._prepare_for_class(__lowercase   ,    __lowercase			)			)
              UpperCamelCase	:Union[str, Any]     							=       outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
              self.assertEqual(len(__lowercase			)   ,    self.model_tester.num_attention_outputs			)

              # check that output_attentions also work using config
              del inputs_dict["output_attentions"]
              UpperCamelCase	:str     							=       True
              UpperCamelCase	:List[str]     							=       model_class(__lowercase			)
              model.to(__lowercase			)
              model.eval()
              with torch.no_grad():
                  UpperCamelCase	:Any     							=       model(**self._prepare_for_class(__lowercase   ,    __lowercase			)			)
              UpperCamelCase	:Dict     							=       outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
              self.assertEqual(len(__lowercase			)   ,    self.model_tester.num_attention_outputs			)

              if chunk_length is not None:
                  self.assertListEqual(
                      list(attentions[0].shape[-4:]			)   ,    [self.model_tester.num_attention_heads, encoder_seq_length, chunk_length, encoder_key_length]   ,    )
              else:
                  self.assertListEqual(
                      list(attentions[0].shape[-3:]			)   ,    [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]   ,    )







def   _A       (    ):
    UpperCamelCase	:List[str]     							=       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
    return image







@require_torch
@require_vision
class       UpperCAmelCase_						( unittest.TestCase			):



      """simple docstring"""
      @cached_property
      def    UpperCAmelCase						(    self			)    ->				str:
          return (
              EfficientFormerImageProcessor.from_pretrained('''snap-research/efficientformer-l1-300'''			)
              if is_vision_available()
              else None
          )
      @slow
      def    UpperCAmelCase						(    self			)    ->				Optional[int]:
          UpperCamelCase	:Optional[int]     							=       EfficientFormerForImageClassification.from_pretrained('''snap-research/efficientformer-l1-300'''			).to(
              __lowercase			)

          UpperCamelCase	:int     							=       self.default_image_processor
          UpperCamelCase	:int     							=       prepare_img()
          UpperCamelCase	:Dict     							=       image_processor(images=__lowercase   ,    return_tensors='''pt'''			).to(__lowercase			)

          # forward pass
          with torch.no_grad():
              UpperCamelCase	:Any     							=       model(**__lowercase			)

          # verify the logits
          UpperCamelCase	:List[Any]     							=       (1, 1000)
          self.assertEqual(outputs.logits.shape   ,    __lowercase			)

          UpperCamelCase	:Union[str, Any]     							=       torch.tensor([-0.0555, 0.4825, -0.0852]			).to(__lowercase			)
          self.assertTrue(torch.allclose(outputs.logits[0][:3]   ,    __lowercase   ,    atol=1e-4			)			)




      @slow
      def    UpperCAmelCase						(    self			)    ->				str:
          UpperCamelCase	:List[str]     							=       EfficientFormerForImageClassificationWithTeacher.from_pretrained(
              '''snap-research/efficientformer-l1-300'''			).to(__lowercase			)

          UpperCamelCase	:Any     							=       self.default_image_processor
          UpperCamelCase	:List[Any]     							=       prepare_img()
          UpperCamelCase	:List[str]     							=       image_processor(images=__lowercase   ,    return_tensors='''pt'''			).to(__lowercase			)

          # forward pass
          with torch.no_grad():
              UpperCamelCase	:int     							=       model(**__lowercase			)

          # verify the logits
          UpperCamelCase	:Dict     							=       (1, 1000)
          self.assertEqual(outputs.logits.shape   ,    __lowercase			)

          UpperCamelCase	:Union[str, Any]     							=       torch.tensor([-0.1312, 0.4353, -1.0499]			).to(__lowercase			)
          self.assertTrue(torch.allclose(outputs.logits[0][:3]   ,    __lowercase   ,    atol=1e-4			)			)













def   _A       (    SCREAMING_SNAKE_CASE__  :     int	):
    UpperCamelCase	:Optional[Any]     							=       9

    while num > 0:
        if num % 10 != digit:
            return False
        num //= 100
        digit -= 1

    return True


def   _A       (    ):
    UpperCamelCase	:List[str]     							=       138902663

    while not is_square_form(num * num	):
        if num % 10 == 3:
            num -= 6  # (3 - 6) % 10 = 7
        else:
            num -= 4  # (7 - 4) % 10 = 3

    return num * 10


if __name__ == "__main__":
 print(f'''{solution() = }''')





