


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

       def __init__( self   :  Union[str, Any]   ,				lowerCamelCase_   :  List[Any]   ,				lowerCamelCase_   :  int = 1_3   ,				lowerCamelCase_   :  int = 6_4   ,				lowerCamelCase_   :  int = 2   ,				lowerCamelCase_   :  int = 3   ,				lowerCamelCase_   :  int = 3   ,				lowerCamelCase_   :  bool = True   ,				lowerCamelCase_   :  bool = True   ,				lowerCamelCase_   :  int = 1_2_8   ,				lowerCamelCase_   :  int=[1_6, 3_2, 6_4, 1_2_8]   ,				lowerCamelCase_   :  int = 7   ,				lowerCamelCase_   :  int = 4   ,				lowerCamelCase_   :  int = 3_7   ,				lowerCamelCase_   :  str = "gelu"   ,				lowerCamelCase_   :  float = 0.1   ,				lowerCamelCase_   :  float = 0.1   ,				lowerCamelCase_   :  int = 1_0   ,				lowerCamelCase_   :  float = 0.0_2   ,				lowerCamelCase_   :  int = 2   ,				lowerCamelCase_   :  int = 1   ,				lowerCamelCase_   :  int = 1_2_8   ,				lowerCamelCase_   :  List[int] = [2, 2, 2, 2]   ,				lowerCamelCase_   :  int = 2   ,				lowerCamelCase_   :  int = 2   ,				):
            a_     :  Optional[Any]				      =     parent
            a_     :  Union[str, Any]				      =     batch_size
            a_     :  List[str]				      =     image_size
            a_     :  Tuple				      =     patch_size
            a_     :  Any				      =     num_channels
            a_     :  Optional[int]				      =     is_training
            a_     :  int				      =     use_labels
            a_     :  Optional[int]				      =     hidden_size
            a_     :  Tuple				      =     num_hidden_layers
            a_     :  Union[str, Any]				      =     num_attention_heads
            a_     :  Union[str, Any]				      =     intermediate_size
            a_     :  str				      =     hidden_act
            a_     :  Optional[int]				      =     hidden_dropout_prob
            a_     :  int				      =     attention_probs_dropout_prob
            a_     :  List[Any]				      =     type_sequence_label_size
            a_     :  Any				      =     initializer_range
            a_     :  Dict				      =     encoder_stride
            a_     :  Optional[int]				      =     num_attention_outputs
            a_     :  Optional[Any]				      =     embed_dim
            a_     :  Tuple				      =     embed_dim + 1
            a_     :  List[Any]				      =     resolution
            a_     :  Optional[int]				      =     depths
            a_     :  Optional[int]				      =     hidden_sizes
            a_     :  Dict				      =     dim
            a_     :  Optional[Any]				      =     mlp_expansion_ratio

       def     UpperCAmelCase( self   :  Optional[int]     ):
            a_     :  Optional[Any]				      =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

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

            a_     :  str				      =     self.get_config()
            return config, pixel_values, labels

       def     UpperCAmelCase( self   :  List[Any]     ):
            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=__UpperCamelCase   ,				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   :  Union[str, Any]   ,				lowerCamelCase_   :  Any   ,				lowerCamelCase_   :  str   ,				lowerCamelCase_   :  Dict     ):
            a_     :  str				      =     EfficientFormerModel(config=__UpperCamelCase     )
            model.to(__UpperCamelCase     )
            model.eval()
            a_     :  Optional[int]				      =     model(__UpperCamelCase     )
            self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)     )

       def     UpperCAmelCase( self   :  str   ,				lowerCamelCase_   :  Union[str, Any]   ,				lowerCamelCase_   :  Any   ,				lowerCamelCase_   :  int     ):
            a_     :  List[str]				      =     self.type_sequence_label_size
            a_     :  str				      =     EfficientFormerForImageClassification(__UpperCamelCase     )
            model.to(__UpperCamelCase     )
            model.eval()
            a_     :  int				      =     model(__UpperCamelCase   ,				labels=__UpperCamelCase     )
            self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)     )

            # test greyscale images
            a_     :  str				      =     1
            a_     :  Dict				      =     EfficientFormerForImageClassification(__UpperCamelCase     )
            model.to(__UpperCamelCase     )
            model.eval()

            a_     :  Optional[int]				      =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
            a_     :  List[Any]				      =     model(__UpperCamelCase     )
            self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)     )

       def     UpperCAmelCase( self   :  int     ):
            a_     :  Dict				      =     self.prepare_config_and_inputs()
            (
                (
                a_			
            )      ,      (
                a_			
            )      ,      (
                a_			
            )      ,      			
            )     :  List[Any]				      =     config_and_inputs
            a_     :  Optional[int]				      =     {"""pixel_values""": pixel_values}
            return config, inputs_dict


@require_torch
class 	a__						(    __lowerCAmelCase    ,							__lowerCAmelCase    ,							unittest.TestCase     ):
       lowerCamelCase__:  int				     =  (
           (
               EfficientFormerModel,
               EfficientFormerForImageClassificationWithTeacher,
               EfficientFormerForImageClassification,
           )
           if is_torch_available()
           else ()
       )
       lowerCamelCase__:  Union[str, Any]				     =  (
           {
               """feature-extraction""": EfficientFormerModel,
               """image-classification""": (
                   EfficientFormerForImageClassification,
                   EfficientFormerForImageClassificationWithTeacher,
               ),
           }
           if is_torch_available()
           else {}
       )
       lowerCamelCase__:  List[str]				     =  False

       lowerCamelCase__:  Tuple				     =  False
       lowerCamelCase__:  List[str]				     =  False
       lowerCamelCase__:  Tuple				     =  False

       def     UpperCAmelCase( self   :  Tuple     ):
            a_     :  List[str]				      =     EfficientFormerModelTester(self     )
            a_     :  int				      =     ConfigTester(
                self   ,				config_class=__UpperCamelCase   ,				has_text_modality=__UpperCamelCase   ,				hidden_size=3_7     )

       def     UpperCAmelCase( self   :  List[str]     ):
            self.config_tester.run_common_tests()

       @unittest.skip(reason="""EfficientFormer does not use inputs_embeds"""     )
       def     UpperCAmelCase( self   :  Dict     ):
            pass

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

       def     UpperCAmelCase( self   :  Optional[Any]     ):
            a_      ,      a_     :  Dict				      =     self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 a_     :  Union[str, Any]				      =     model_class(__UpperCamelCase     )
                 a_     :  List[Any]				      =     inspect.signature(model.forward     )
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 a_     :  List[str]				      =     [*signature.parameters.keys()]

                 a_     :  Tuple				      =     ["""pixel_values"""]
                 self.assertListEqual(arg_names[:1]   ,				__UpperCamelCase     )

       def     UpperCAmelCase( self   :  Optional[Any]     ):






            def check_hidden_states_output(lowerCamelCase_   :  int   ,				lowerCamelCase_   :  List[Any]   ,				lowerCamelCase_   :  int     ):
                 a_     :  Optional[int]				      =     model_class(__UpperCamelCase     )
                 model.to(__UpperCamelCase     )
                 model.eval()

                 with torch.no_grad():
                      a_     :  int				      =     model(**self._prepare_for_class(__UpperCamelCase   ,				__UpperCamelCase     )     )

                 a_     :  Optional[int]				      =     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                 a_     :  List[Any]				      =     getattr(
                     self.model_tester   ,				"""expected_num_hidden_layers"""   ,				self.model_tester.num_hidden_layers + 1     )
                 self.assertEqual(len(__UpperCamelCase     )   ,				__UpperCamelCase     )

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

                      self.assertIsInstance(__UpperCamelCase   ,				(list, tuple)     )
                      self.assertEqual(len(__UpperCamelCase     )   ,				__UpperCamelCase     )
                      a_     :  Union[str, Any]				      =     getattr(self.model_tester   ,				"""seq_length"""   ,				__UpperCamelCase     )
                      a_     :  Tuple				      =     getattr(self.model_tester   ,				"""decoder_seq_length"""   ,				__UpperCamelCase     )

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

            a_      ,      a_     :  List[str]				      =     self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 a_     :  int				      =     True
                 check_hidden_states_output(__UpperCamelCase   ,				__UpperCamelCase   ,				__UpperCamelCase     )

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 a_     :  Union[str, Any]				      =     True

                 check_hidden_states_output(__UpperCamelCase   ,				__UpperCamelCase   ,				__UpperCamelCase     )

       def     UpperCAmelCase( self   :  List[str]   ,				lowerCamelCase_   :  Any   ,				lowerCamelCase_   :  Any   ,				lowerCamelCase_   :  str=False     ):
            a_     :  Optional[Any]				      =     super()._prepare_for_class(__UpperCamelCase   ,				__UpperCamelCase   ,				return_labels=__UpperCamelCase     )

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

            return inputs_dict

       def     UpperCAmelCase( self   :  str     ):
            a_     :  List[Any]				      =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__UpperCamelCase     )

       @unittest.skip(reason="""EfficientFormer does not implement masked image modeling yet"""     )
       def     UpperCAmelCase( self   :  List[str]     ):
            a_     :  Union[str, Any]				      =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase     )

       def     UpperCAmelCase( self   :  Optional[int]     ):
            a_     :  List[str]				      =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase     )

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






            if not self.model_tester.is_training:
                 return

            a_      ,      a_     :  List[str]				      =     self.model_tester.prepare_config_and_inputs_for_common()
            a_     :  Dict				      =     True

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

       def     UpperCAmelCase( self   :  Optional[Any]     ):
            a_      ,      a_     :  Any				      =     self.model_tester.prepare_config_and_inputs_for_common()

            a_     :  int				      =     [
                {"""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(__UpperCamelCase     ),
                     ]
                     or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
                 ):
                      continue

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

                           a_     :  Tuple				      =     model_class(__UpperCamelCase     )
                           model.to(__UpperCamelCase     )
                           model.train()

                           a_     :  Tuple				      =     self._prepare_for_class(__UpperCamelCase   ,				__UpperCamelCase   ,				return_labels=__UpperCamelCase     )

                           if problem_type["num_labels"] > 1:
                                a_     :  Tuple				      =     inputs["""labels"""].unsqueeze(1     ).repeat(1   ,				problem_type["""num_labels"""]     )

                           a_     :  List[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=__UpperCamelCase     ) as warning_list:
                                a_     :  List[str]				      =     model(**__UpperCamelCase     ).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   :  Union[str, Any]     ):






            for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 a_     :  Tuple				      =     EfficientFormerModel.from_pretrained(__UpperCamelCase     )
                 self.assertIsNotNone(__UpperCamelCase     )

       def     UpperCAmelCase( self   :  Any     ):
            a_      ,      a_     :  Tuple				      =     self.model_tester.prepare_config_and_inputs_for_common()
            a_     :  Any				      =     True

            a_     :  Tuple				      =     getattr(self.model_tester   ,				"""seq_length"""   ,				__UpperCamelCase     )
            a_     :  List[str]				      =     getattr(self.model_tester   ,				"""encoder_seq_length"""   ,				__UpperCamelCase     )
            a_     :  Optional[Any]				      =     getattr(self.model_tester   ,				"""key_length"""   ,				__UpperCamelCase     )
            a_     :  str				      =     getattr(self.model_tester   ,				"""chunk_length"""   ,				__UpperCamelCase     )

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

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

                 # check that output_attentions also work using config
                 del inputs_dict["output_attentions"]
                 a_     :  Tuple				      =     True
                 a_     :  List[Any]				      =     model_class(__UpperCamelCase     )
                 model.to(__UpperCamelCase     )
                 model.eval()
                 with torch.no_grad():
                      a_     :  Dict				      =     model(**self._prepare_for_class(__UpperCamelCase   ,				__UpperCamelCase     )     )
                 a_     :  List[Any]				      =     outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
                 self.assertEqual(len(__UpperCamelCase     )   ,				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      (    ):
     a_     :  int				      =     Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
     return image


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

       @cached_property
       def     UpperCAmelCase( self   :  Optional[Any]     ):
            return (
                EfficientFormerImageProcessor.from_pretrained("""snap-research/efficientformer-l1-300"""     )
                if is_vision_available()
                else None
            )

       @slow
       def     UpperCAmelCase( self   :  List[str]     ):
            a_     :  str				      =     EfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300"""     ).to(
                __UpperCamelCase     )

            a_     :  List[str]				      =     self.default_image_processor
            a_     :  Optional[Any]				      =     prepare_img()
            a_     :  List[str]				      =     image_processor(images=__UpperCamelCase   ,				return_tensors="""pt"""     ).to(__UpperCamelCase     )

            # forward pass
            with torch.no_grad():
                 a_     :  List[Any]				      =     model(**__UpperCamelCase     )

            # verify the logits
            a_     :  List[str]				      =     (1, 1_0_0_0)
            self.assertEqual(outputs.logits.shape   ,				__UpperCamelCase     )

            a_     :  Any				      =     torch.tensor([-0.0_5_5_5, 0.4_8_2_5, -0.0_8_5_2]     ).to(__UpperCamelCase     )
            self.assertTrue(torch.allclose(outputs.logits[0][:3]   ,				__UpperCamelCase   ,				atol=1E-4     )     )

       @slow
       def     UpperCAmelCase( self   :  Optional[Any]     ):
            a_     :  str				      =     EfficientFormerForImageClassificationWithTeacher.from_pretrained(
                """snap-research/efficientformer-l1-300"""     ).to(__UpperCamelCase     )

            a_     :  List[Any]				      =     self.default_image_processor
            a_     :  str				      =     prepare_img()
            a_     :  Tuple				      =     image_processor(images=__UpperCamelCase   ,				return_tensors="""pt"""     ).to(__UpperCamelCase     )

            # forward pass
            with torch.no_grad():
                 a_     :  Dict				      =     model(**__UpperCamelCase     )

            # verify the logits
            a_     :  List[Any]				      =     (1, 1_0_0_0)
            self.assertEqual(outputs.logits.shape   ,				__UpperCamelCase     )

            a_     :  List[str]				      =     torch.tensor([-0.1_3_1_2, 0.4_3_5_3, -1.0_4_9_9]     ).to(__UpperCamelCase     )
            self.assertTrue(torch.allclose(outputs.logits[0][:3]   ,				__UpperCamelCase   ,				atol=1E-4     )     )









'''simple docstring'''

import warnings

from ...utils import logging
from .image_processing_levit import LevitImageProcessor


a__         =     logging.get_logger(__name__)





class __magic_name__(						__lowerCAmelCase      ):






						def __init__(   self						:						Optional[Any]					,   *__UpperCamelCase						:						Union[str, Any]					,   **__UpperCamelCase						:						Dict       ):



									'''simple docstring'''






									warnings.warn(
									    """The class LevitFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"""
									    """ use LevitImageProcessor instead."""					,   __UpperCamelCase					,   )
									super().__init__(*__UpperCamelCase					,   **__UpperCamelCase       )