

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 A__     :



 """simple docstring"""







 def __init__(		self			:						List[Any]  ,    A_			:						Tuple  ,    A_			:						int = 1_3  ,    A_			:						int = 6_4  ,    A_			:						int = 2  ,    A_			:						int = 3  ,    A_			:						int = 3  ,    A_			:						bool = True  ,    A_			:						bool = True  ,    A_			:						int = 1_2_8  ,    A_			:						int=[1_6, 3_2, 6_4, 1_2_8]  ,    A_			:						int = 7  ,    A_			:						int = 4  ,    A_			:						int = 3_7  ,    A_			:						str = "gelu"  ,    A_			:						float = 0.1  ,    A_			:						float = 0.1  ,    A_			:						int = 1_0  ,    A_			:						float = 0.02  ,    A_			:						int = 2  ,    A_			:						int = 1  ,    A_			:						int = 1_2_8  ,    A_			:						List[int] = [2, 2, 2, 2]  ,    A_			:						int = 2  ,    A_			:						int = 2  ,    ):






      '''simple docstring'''




      _lowerCAmelCase :       Union[str, Any]	   =     parent
      _lowerCAmelCase :       int	   =     batch_size
      _lowerCAmelCase :       int	   =     image_size
      _lowerCAmelCase :       List[Any]	   =     patch_size
      _lowerCAmelCase :       int	   =     num_channels
      _lowerCAmelCase :       Any	   =     is_training
      _lowerCAmelCase :       int	   =     use_labels
      _lowerCAmelCase :       List[Any]	   =     hidden_size
      _lowerCAmelCase :       Dict	   =     num_hidden_layers
      _lowerCAmelCase :       str	   =     num_attention_heads
      _lowerCAmelCase :       str	   =     intermediate_size
      _lowerCAmelCase :       Optional[Any]	   =     hidden_act
      _lowerCAmelCase :       Optional[int]	   =     hidden_dropout_prob
      _lowerCAmelCase :       List[str]	   =     attention_probs_dropout_prob
      _lowerCAmelCase :       Optional[int]	   =     type_sequence_label_size
      _lowerCAmelCase :       Optional[int]	   =     initializer_range
      _lowerCAmelCase :       Dict	   =     encoder_stride
      _lowerCAmelCase :       Tuple	   =     num_attention_outputs
      _lowerCAmelCase :       Dict	   =     embed_dim
      _lowerCAmelCase :       Union[str, Any]	   =     embed_dim + 1
      _lowerCAmelCase :       Union[str, Any]	   =     resolution
      _lowerCAmelCase :       List[str]	   =     depths
      _lowerCAmelCase :       Tuple	   =     hidden_sizes
      _lowerCAmelCase :       Dict	   =     dim
      _lowerCAmelCase :       int	   =     mlp_expansion_ratio

 def  __magic_name__							(		self			:						Union[str, Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[int]	   =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

      _lowerCAmelCase :       List[Any]	   =     None
      if self.use_labels:
           _lowerCAmelCase :       Optional[int]	   =     ids_tensor([self.batch_size]  ,    self.type_sequence_label_size							)

      _lowerCAmelCase :       Any	   =     self.get_config()
      return config, pixel_values, labels

 def  __magic_name__							(		self			:						Optional[Any]							):






      '''simple docstring'''




      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=A_  ,    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  __magic_name__							(		self			:						int  ,    A_			:						str  ,    A_			:						int  ,    A_			:						Dict							):






      '''simple docstring'''




      _lowerCAmelCase :       str	   =     EfficientFormerModel(config=A_							)
      model.to(A_							)
      model.eval()
      _lowerCAmelCase :       Optional[int]	   =     model(A_							)
      self.parent.assertEqual(result.last_hidden_state.shape  ,    (self.batch_size, self.seq_length, self.hidden_size)							)

 def  __magic_name__							(		self			:						int  ,    A_			:						Dict  ,    A_			:						Dict  ,    A_			:						Tuple							):






      '''simple docstring'''




      _lowerCAmelCase :       Union[str, Any]	   =     self.type_sequence_label_size
      _lowerCAmelCase :       Tuple	   =     EfficientFormerForImageClassification(A_							)
      model.to(A_							)
      model.eval()
      _lowerCAmelCase :       str	   =     model(A_  ,    labels=A_							)
      self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.type_sequence_label_size)							)

      # test greyscale images
      _lowerCAmelCase :       List[str]	   =     1
      _lowerCAmelCase :       Tuple	   =     EfficientFormerForImageClassification(A_							)
      model.to(A_							)
      model.eval()

      _lowerCAmelCase :       Tuple	   =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
      _lowerCAmelCase :       Dict	   =     model(A_							)
      self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.type_sequence_label_size)							)


 def  __magic_name__							(		self			:						Union[str, Any]							):






      '''simple docstring'''




      _lowerCAmelCase :       Optional[Any]	   =     self.prepare_config_and_inputs()
      (
          (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      (
          _lowerCAmelCase
      )   ,      
      ) :       Union[str, Any]	   =     config_and_inputs
      _lowerCAmelCase :       Optional[int]	   =     {"pixel_values": pixel_values}
      return config, inputs_dict





@require_torch
class A__     (			A	,		A	,		unittest.TestCase			):



 """simple docstring"""







 _lowercase  :       Union[str, Any]  			=    (
     (
         EfficientFormerModel,
         EfficientFormerForImageClassificationWithTeacher,
         EfficientFormerForImageClassification,
     )
     if is_torch_available()
     else ()
 )
 _lowercase  :       Union[str, Any]  			=    (
     {
         '''feature-extraction''': EfficientFormerModel,
         '''image-classification''': (
             EfficientFormerForImageClassification,
             EfficientFormerForImageClassificationWithTeacher,
         ),
     }
     if is_torch_available()
     else {}
 )
 _lowercase  :       List[Any]  			=    False

 _lowercase  :       List[str]  			=    False
 _lowercase  :       Optional[int]  			=    False
 _lowercase  :       Optional[Any]  			=    False

 def  __magic_name__							(		self			:						Optional[int]							):






      '''simple docstring'''




      _lowerCAmelCase :       Tuple	   =     EfficientFormerModelTester(self							)
      _lowerCAmelCase :       Optional[Any]	   =     ConfigTester(
          self  ,    config_class=A_  ,    has_text_modality=A_  ,    hidden_size=3_7							)

 def  __magic_name__							(		self			:						List[Any]							):






      '''simple docstring'''




      self.config_tester.run_common_tests()

 @unittest.skip(reason="EfficientFormer does not use inputs_embeds"							)
 def  __magic_name__							(		self			:						Tuple							):






      '''simple docstring'''




      pass

 @unittest.skip(reason="EfficientFormer does not support input and output embeddings"							)
 def  __magic_name__							(		self			:						int							):






      '''simple docstring'''




      pass

 def  __magic_name__							(		self			:						Any							):






      '''simple docstring'''




      _lowerCAmelCase   ,      _lowerCAmelCase :       str	   =     self.model_tester.prepare_config_and_inputs_for_common()

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

           _lowerCAmelCase :       Optional[Any]	   =     ["pixel_values"]
           self.assertListEqual(arg_names[:1]  ,    A_							)

 def  __magic_name__							(		self			:						Any							):






      '''simple docstring'''




      def check_hidden_states_output(A_			:						int  ,    A_			:						Union[str, Any]  ,    A_			:						Tuple							):
           _lowerCAmelCase :       List[Any]	   =     model_class(A_							)
           model.to(A_							)
           model.eval()

           with torch.no_grad():
                _lowerCAmelCase :       Dict	   =     model(**self._prepare_for_class(A_  ,    A_							)							)

           _lowerCAmelCase :       Any	   =     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

           _lowerCAmelCase :       int	   =     getattr(
               self.model_tester  ,    "expected_num_hidden_layers"  ,    self.model_tester.num_hidden_layers + 1							)
           self.assertEqual(len(A_							)  ,    A_							)

           if hasattr(self.model_tester  ,    "encoder_seq_length"							):
                _lowerCAmelCase :       Tuple	   =     self.model_tester.encoder_seq_length
                if hasattr(self.model_tester  ,    "chunk_length"							) and self.model_tester.chunk_length > 1:
                     _lowerCAmelCase :       List[Any]	   =     seq_length * self.model_tester.chunk_length
           else:
                _lowerCAmelCase :       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:
                _lowerCAmelCase :       Any	   =     outputs.decoder_hidden_states

                self.assertIsInstance(A_  ,    (list, tuple)							)
                self.assertEqual(len(A_							)  ,    A_							)
                _lowerCAmelCase :       Optional[int]	   =     getattr(self.model_tester  ,    "seq_length"  ,    A_							)
                _lowerCAmelCase :       str	   =     getattr(self.model_tester  ,    "decoder_seq_length"  ,    A_							)

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

      _lowerCAmelCase   ,      _lowerCAmelCase :       Optional[Any]	   =     self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           _lowerCAmelCase :       Tuple	   =     True
           check_hidden_states_output(A_  ,    A_  ,    A_							)

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           _lowerCAmelCase :       List[str]	   =     True

           check_hidden_states_output(A_  ,    A_  ,    A_							)

 def  __magic_name__							(		self			:						Union[str, Any]  ,    A_			:						Any  ,    A_			:						Dict  ,    A_			:						Optional[Any]=False							):






      '''simple docstring'''




      _lowerCAmelCase :       Union[str, Any]	   =     super()._prepare_for_class(A_  ,    A_  ,    return_labels=A_							)

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

      return inputs_dict

 def  __magic_name__							(		self			:						int							):






      '''simple docstring'''




      _lowerCAmelCase :       str	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*A_							)

 @unittest.skip(reason="EfficientFormer does not implement masked image modeling yet"							)
 def  __magic_name__							(		self			:						int							):






      '''simple docstring'''




      _lowerCAmelCase :       Any	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*A_							)

 def  __magic_name__							(		self			:						Dict							):






      '''simple docstring'''




      _lowerCAmelCase :       Dict	   =     self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*A_							)

 def  __magic_name__							(		self			:						Optional[Any]							):






      '''simple docstring'''




      if not self.model_tester.is_training:
           return

      _lowerCAmelCase   ,      _lowerCAmelCase :       List[Any]	   =     self.model_tester.prepare_config_and_inputs_for_common()
      _lowerCAmelCase :       Any	   =     True

      for model_class in self.all_model_classes:
           # EfficientFormerForImageClassificationWithTeacher supports inference-only
           if (
               model_class in get_values(A_							)
               or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
           ):
                continue
           _lowerCAmelCase :       Dict	   =     model_class(A_							)
           model.to(A_							)
           model.train()
           _lowerCAmelCase :       Union[str, Any]	   =     self._prepare_for_class(A_  ,    A_  ,    return_labels=A_							)
           _lowerCAmelCase :       str	   =     model(**A_							).loss
           loss.backward()

 def  __magic_name__							(		self			:						str							):






      '''simple docstring'''




      _lowerCAmelCase   ,      _lowerCAmelCase :       Optional[Any]	   =     self.model_tester.prepare_config_and_inputs_for_common()

      _lowerCAmelCase :       Tuple	   =     [
          {"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(A_							),
               ]
               or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
           ):
                continue

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

                     _lowerCAmelCase :       str	   =     model_class(A_							)
                     model.to(A_							)
                     model.train()

                     _lowerCAmelCase :       Any	   =     self._prepare_for_class(A_  ,    A_  ,    return_labels=A_							)

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

                     _lowerCAmelCase :       Tuple	   =     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=A_							) as warning_list:
                          _lowerCAmelCase :       Optional[Any]	   =     model(**A_							).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  __magic_name__							(		self			:						Union[str, Any]							):






      '''simple docstring'''




      for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowerCAmelCase :       Dict	   =     EfficientFormerModel.from_pretrained(A_							)
           self.assertIsNotNone(A_							)


 def  __magic_name__							(		self			:						str							):






      '''simple docstring'''




      _lowerCAmelCase   ,      _lowerCAmelCase :       List[Any]	   =     self.model_tester.prepare_config_and_inputs_for_common()
      _lowerCAmelCase :       Any	   =     True

      _lowerCAmelCase :       List[Any]	   =     getattr(self.model_tester  ,    "seq_length"  ,    A_							)
      _lowerCAmelCase :       Any	   =     getattr(self.model_tester  ,    "encoder_seq_length"  ,    A_							)
      _lowerCAmelCase :       List[Any]	   =     getattr(self.model_tester  ,    "key_length"  ,    A_							)
      _lowerCAmelCase :       List[str]	   =     getattr(self.model_tester  ,    "chunk_length"  ,    A_							)

      if chunk_length is not None and hasattr(self.model_tester  ,    "num_hashes"							):
           _lowerCAmelCase :       Dict	   =     encoder_seq_length * self.model_tester.num_hashes

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

           # check that output_attentions also work using config
           del inputs_dict["output_attentions"]
           _lowerCAmelCase :       int	   =     True
           _lowerCAmelCase :       Tuple	   =     model_class(A_							)
           model.to(A_							)
           model.eval()
           with torch.no_grad():
                _lowerCAmelCase :       List[str]	   =     model(**self._prepare_for_class(A_  ,    A_							)							)
           _lowerCAmelCase :       List[Any]	   =     outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
           self.assertEqual(len(A_							)  ,    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 						_snake_case		(     )							->   Dict:

     """simple docstring"""







     _lowerCAmelCase :       Union[str, Any]	   =     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
     return image





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



 """simple docstring"""







 @cached_property
 def  __magic_name__							(		self			:						str							):






      '''simple docstring'''




      return (
          EfficientFormerImageProcessor.from_pretrained("snap-research/efficientformer-l1-300"							)
          if is_vision_available()
          else None
      )

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






      '''simple docstring'''




      _lowerCAmelCase :       str	   =     EfficientFormerForImageClassification.from_pretrained("snap-research/efficientformer-l1-300"							).to(
          A_							)

      _lowerCAmelCase :       Optional[Any]	   =     self.default_image_processor
      _lowerCAmelCase :       str	   =     prepare_img()
      _lowerCAmelCase :       int	   =     image_processor(images=A_  ,    return_tensors="pt"							).to(A_							)

      # forward pass
      with torch.no_grad():
           _lowerCAmelCase :       Any	   =     model(**A_							)

      # verify the logits
      _lowerCAmelCase :       Optional[Any]	   =     (1, 1_0_0_0)
      self.assertEqual(outputs.logits.shape  ,    A_							)

      _lowerCAmelCase :       int	   =     torch.tensor([-0.0555, 0.4825, -0.0852]							).to(A_							)
      self.assertTrue(torch.allclose(outputs.logits[0][:3]  ,    A_  ,    atol=1E-4							)							)


 @slow
 def  __magic_name__							(		self			:						List[str]							):






      '''simple docstring'''




      _lowerCAmelCase :       List[str]	   =     EfficientFormerForImageClassificationWithTeacher.from_pretrained(
          "snap-research/efficientformer-l1-300"							).to(A_							)

      _lowerCAmelCase :       Dict	   =     self.default_image_processor
      _lowerCAmelCase :       Optional[int]	   =     prepare_img()
      _lowerCAmelCase :       Optional[Any]	   =     image_processor(images=A_  ,    return_tensors="pt"							).to(A_							)

      # forward pass
      with torch.no_grad():
           _lowerCAmelCase :       Optional[int]	   =     model(**A_							)

      # verify the logits
      _lowerCAmelCase :       Any	   =     (1, 1_0_0_0)
      self.assertEqual(outputs.logits.shape  ,    A_							)

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









def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Optional[int]:

     """simple docstring"""







     if power < 0:
          return -1
     base %= mod
     _lowerCAmelCase :       Tuple	   =     1

     while power > 0:
          if power & 1:
               _lowerCAmelCase :       Optional[Any]	   =     (result * base) % mod
          _lowerCAmelCase :       int	   =     power >> 1
          _lowerCAmelCase :       Optional[int]	   =     (base * base) % mod

     return result







def 						_snake_case		(     )							->   Optional[int]:

     """simple docstring"""







     print(modular_exponential(3		,				200		,				13					)					)


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  main()






