"""simple docstring"""


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,					_UpperCAmelCase,					_UpperCAmelCase = 13,					_UpperCAmelCase = 64,					_UpperCAmelCase = 2,					_UpperCAmelCase = 3,					_UpperCAmelCase = 3,					_UpperCAmelCase = True,					_UpperCAmelCase = True,					_UpperCAmelCase = 128,					_UpperCAmelCase=[16, 32, 64, 128],					_UpperCAmelCase = 7,					_UpperCAmelCase = 4,					_UpperCAmelCase = 37,					_UpperCAmelCase = "gelu",					_UpperCAmelCase = 0.1,					_UpperCAmelCase = 0.1,					_UpperCAmelCase = 10,					_UpperCAmelCase = 0.02,					_UpperCAmelCase = 2,					_UpperCAmelCase = 1,					_UpperCAmelCase = 128,					_UpperCAmelCase = [2, 2, 2, 2],					_UpperCAmelCase = 2,					_UpperCAmelCase = 2,					):
        '''simple docstring'''



        lowercase__								=				parent
        lowercase__								=				batch_size
        lowercase__								=				image_size
        lowercase__								=				patch_size
        lowercase__								=				num_channels
        lowercase__								=				is_training
        lowercase__								=				use_labels
        lowercase__								=				hidden_size
        lowercase__								=				num_hidden_layers
        lowercase__								=				num_attention_heads
        lowercase__								=				intermediate_size
        lowercase__								=				hidden_act
        lowercase__								=				hidden_dropout_prob
        lowercase__								=				attention_probs_dropout_prob
        lowercase__								=				type_sequence_label_size
        lowercase__								=				initializer_range
        lowercase__								=				encoder_stride
        lowercase__								=				num_attention_outputs
        lowercase__								=				embed_dim
        lowercase__								=				embed_dim + 1
        lowercase__								=				resolution
        lowercase__								=				depths
        lowercase__								=				hidden_sizes
        lowercase__								=				dim
        lowercase__								=				mlp_expansion_ratio


   def 						snake_case__						(  self	):
        '''simple docstring'''



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

        lowercase__								=				None
        if self.use_labels:
             lowercase__								=				ids_tensor([self.batch_size],					self.type_sequence_label_size	)

        lowercase__								=				self.get_config()
        return config, pixel_values, labels


   def 						snake_case__						(  self	):
        '''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=_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 						snake_case__						(  self,					_UpperCAmelCase,					_UpperCAmelCase,					_UpperCAmelCase	):
        '''simple docstring'''



        lowercase__								=				EfficientFormerModel(config=_UpperCAmelCase	)
        model.to(_UpperCAmelCase	)
        model.eval()
        lowercase__								=				model(_UpperCAmelCase	)
        self.parent.assertEqual(result.last_hidden_state.shape,					(self.batch_size, self.seq_length, self.hidden_size)	)


   def 						snake_case__						(  self,					_UpperCAmelCase,					_UpperCAmelCase,					_UpperCAmelCase	):
        '''simple docstring'''



        lowercase__								=				self.type_sequence_label_size
        lowercase__								=				EfficientFormerForImageClassification(_UpperCAmelCase	)
        model.to(_UpperCAmelCase	)
        model.eval()
        lowercase__								=				model(_UpperCAmelCase,					labels=_UpperCAmelCase	)
        self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.type_sequence_label_size)	)

        # test greyscale images
        lowercase__								=				1
        lowercase__								=				EfficientFormerForImageClassification(_UpperCAmelCase	)
        model.to(_UpperCAmelCase	)
        model.eval()

        lowercase__								=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
        lowercase__								=				model(_UpperCAmelCase	)
        self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.type_sequence_label_size)	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				self.prepare_config_and_inputs()
        (
            (
            lowercase__
        )			,    (
            lowercase__
        )			,    (
            lowercase__
        )			,    
        )								=				config_and_inputs
        lowercase__								=				{"pixel_values": pixel_values}
        return config, inputs_dict






@require_torch
class  a__     ( _a       ,       _a       ,       unittest.TestCase						):
   snake_case_				=   (
       (
           EfficientFormerModel,
           EfficientFormerForImageClassificationWithTeacher,
           EfficientFormerForImageClassification,
       )
       if is_torch_available()
       else ()
   )
   snake_case_				=   (
       {
           "feature-extraction": EfficientFormerModel,
           "image-classification": (
               EfficientFormerForImageClassification,
               EfficientFormerForImageClassificationWithTeacher,
           ),
       }
       if is_torch_available()
       else {}
   )
   snake_case_				=   False

   snake_case_				=   False
   snake_case_				=   False
   snake_case_				=   False


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				EfficientFormerModelTester(self	)
        lowercase__								=				ConfigTester(
            self,					config_class=_UpperCAmelCase,					has_text_modality=_UpperCAmelCase,					hidden_size=37	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        self.config_tester.run_common_tests()


   @unittest.skip(reason="EfficientFormer does not use inputs_embeds"	)
   def 						snake_case__						(  self	):
        '''simple docstring'''



        pass


   @unittest.skip(reason="EfficientFormer does not support input and output embeddings"	)
   def 						snake_case__						(  self	):
        '''simple docstring'''



        pass


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__			,    lowercase__								=				self.model_tester.prepare_config_and_inputs_for_common()

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

             lowercase__								=				["pixel_values"]
             self.assertListEqual(arg_names[:1],					_UpperCAmelCase	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        def check_hidden_states_output(_UpperCAmelCase,					_UpperCAmelCase,					_UpperCAmelCase	):
             lowercase__								=				model_class(_UpperCAmelCase	)
             model.to(_UpperCAmelCase	)
             model.eval()

             with torch.no_grad():
                  lowercase__								=				model(**self._prepare_for_class(_UpperCAmelCase,					_UpperCAmelCase	)	)

             lowercase__								=				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

                  self.assertIsInstance(_UpperCAmelCase,					(list, tuple)	)
                  self.assertEqual(len(_UpperCAmelCase	),					_UpperCAmelCase	)
                  lowercase__								=				getattr(self.model_tester,					"seq_length",					_UpperCAmelCase	)
                  lowercase__								=				getattr(self.model_tester,					"decoder_seq_length",					_UpperCAmelCase	)

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

        lowercase__			,    lowercase__								=				self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase__								=				True
             check_hidden_states_output(_UpperCAmelCase,					_UpperCAmelCase,					_UpperCAmelCase	)

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

             check_hidden_states_output(_UpperCAmelCase,					_UpperCAmelCase,					_UpperCAmelCase	)


   def 						snake_case__						(  self,					_UpperCAmelCase,					_UpperCAmelCase,					_UpperCAmelCase=False	):
        '''simple docstring'''



        lowercase__								=				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 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				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 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCAmelCase	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        if not self.model_tester.is_training:
             return

        lowercase__			,    lowercase__								=				self.model_tester.prepare_config_and_inputs_for_common()
        lowercase__								=				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
             lowercase__								=				model_class(_UpperCAmelCase	)
             model.to(_UpperCAmelCase	)
             model.train()
             lowercase__								=				self._prepare_for_class(_UpperCAmelCase,					_UpperCAmelCase,					return_labels=_UpperCAmelCase	)
             lowercase__								=				model(**_UpperCAmelCase	).loss
             loss.backward()


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__			,    lowercase__								=				self.model_tester.prepare_config_and_inputs_for_common()

        lowercase__								=				[
            {"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']}'''	):
                       lowercase__								=				problem_type["title"]
                       lowercase__								=				problem_type["num_labels"]

                       lowercase__								=				model_class(_UpperCAmelCase	)
                       model.to(_UpperCAmelCase	)
                       model.train()

                       lowercase__								=				self._prepare_for_class(_UpperCAmelCase,					_UpperCAmelCase,					return_labels=_UpperCAmelCase	)

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

                       lowercase__								=				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:
                            lowercase__								=				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 						snake_case__						(  self	):
        '''simple docstring'''



        for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase__								=				EfficientFormerModel.from_pretrained(_UpperCAmelCase	)
             self.assertIsNotNone(_UpperCAmelCase	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



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

        lowercase__								=				getattr(self.model_tester,					"seq_length",					_UpperCAmelCase	)
        lowercase__								=				getattr(self.model_tester,					"encoder_seq_length",					_UpperCAmelCase	)
        lowercase__								=				getattr(self.model_tester,					"key_length",					_UpperCAmelCase	)
        lowercase__								=				getattr(self.model_tester,					"chunk_length",					_UpperCAmelCase	)

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

        for model_class in self.all_model_classes:
             lowercase__								=				True
             lowercase__								=				False
             lowercase__								=				True
             lowercase__								=				model_class(_UpperCAmelCase	)
             model.to(_UpperCAmelCase	)
             model.eval()
             with torch.no_grad():
                  lowercase__								=				model(**self._prepare_for_class(_UpperCAmelCase,					_UpperCAmelCase	)	)
             lowercase__								=				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"]
             lowercase__								=				True
             lowercase__								=				model_class(_UpperCAmelCase	)
             model.to(_UpperCAmelCase	)
             model.eval()
             with torch.no_grad():
                  lowercase__								=				model(**self._prepare_for_class(_UpperCAmelCase,					_UpperCAmelCase	)	)
             lowercase__								=				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 (		):






     '''simple docstring'''



     lowercase__								=				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
     return image






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


   @cached_property
   def 						snake_case__						(  self	):
        '''simple docstring'''



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


   @slow
   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				EfficientFormerForImageClassification.from_pretrained("snap-research/efficientformer-l1-300"	).to(
            _UpperCAmelCase	)

        lowercase__								=				self.default_image_processor
        lowercase__								=				prepare_img()
        lowercase__								=				image_processor(images=_UpperCAmelCase,					return_tensors="pt"	).to(_UpperCAmelCase	)

        # forward pass
        with torch.no_grad():
             lowercase__								=				model(**_UpperCAmelCase	)

        # verify the logits
        lowercase__								=				(1, 1000)
        self.assertEqual(outputs.logits.shape,					_UpperCAmelCase	)

        lowercase__								=				torch.tensor([-0.0_555, 0.4_825, -0.0_852]	).to(_UpperCAmelCase	)
        self.assertTrue(torch.allclose(outputs.logits[0][:3],					_UpperCAmelCase,					atol=1E-4	)	)


   @slow
   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				EfficientFormerForImageClassificationWithTeacher.from_pretrained(
            "snap-research/efficientformer-l1-300"	).to(_UpperCAmelCase	)

        lowercase__								=				self.default_image_processor
        lowercase__								=				prepare_img()
        lowercase__								=				image_processor(images=_UpperCAmelCase,					return_tensors="pt"	).to(_UpperCAmelCase	)

        # forward pass
        with torch.no_grad():
             lowercase__								=				model(**_UpperCAmelCase	)

        # verify the logits
        lowercase__								=				(1, 1000)
        self.assertEqual(outputs.logits.shape,					_UpperCAmelCase	)

        lowercase__								=				torch.tensor([-0.1_312, 0.4_353, -1.0_499]	).to(_UpperCAmelCase	)
        self.assertTrue(torch.allclose(outputs.logits[0][:3],					_UpperCAmelCase,					atol=1E-4	)	)





"""simple docstring"""


import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


lowerCAmelCase_:       List[str]			     =       logging.get_logger(__name__)






class  a__     ( _a						):
   snake_case_				=   "speech-encoder-decoder"
   snake_case_				=   True


   def __init__(  self,					**_UpperCAmelCase	):
        '''simple docstring'''



        super().__init__(**_UpperCAmelCase	)
        if "encoder" not in kwargs or "decoder" not in kwargs:
             raise ValueError(
                 F'''A configuraton of type {self.model_type} cannot be instantiated because not both `encoder` and'''
                 F''' `decoder` sub-configurations are passed, but only {kwargs}'''	)

        lowercase__								=				kwargs.pop("encoder"	)
        lowercase__								=				encoder_config.pop("model_type"	)
        lowercase__								=				kwargs.pop("decoder"	)
        lowercase__								=				decoder_config.pop("model_type"	)

        lowercase__								=				AutoConfig.for_model(_UpperCAmelCase,					**_UpperCAmelCase	)
        lowercase__								=				AutoConfig.for_model(_UpperCAmelCase,					**_UpperCAmelCase	)
        lowercase__								=				True


   @classmethod
   def 						snake_case__						(  cls,					_UpperCAmelCase,					_UpperCAmelCase,					**_UpperCAmelCase	):
        '''simple docstring'''



        logger.info("Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config"	)
        lowercase__								=				True
        lowercase__								=				True

        return cls(encoder=encoder_config.to_dict(),					decoder=decoder_config.to_dict(),					**_UpperCAmelCase	)


   def 						snake_case__						(  self	):
        '''simple docstring'''



        lowercase__								=				copy.deepcopy(self.__dict__	)
        lowercase__								=				self.encoder.to_dict()
        lowercase__								=				self.decoder.to_dict()
        lowercase__								=				self.__class__.model_type
        return output




