


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






 """simple docstring"""




 def __init__(						self					,       __A					,       __A = 13					,       __A = 64					,       __A = 2					,       __A = 3					,       __A = 3					,       __A = True					,       __A = True					,       __A = 128					,       __A=[16, 32, 64, 128]					,       __A = 7					,       __A = 4					,       __A = 37					,       __A = "gelu"					,       __A = 0.1					,       __A = 0.1					,       __A = 10					,       __A = 0.02					,       __A = 2					,       __A = 1					,       __A = 128					,       __A = [2, 2, 2, 2]					,       __A = 2					,       __A = 2					,       )   ->	List[str]:
     a											=parent
     a											=batch_size
     a											=image_size
     a											=patch_size
     a											=num_channels
     a											=is_training
     a											=use_labels
     a											=hidden_size
     a											=num_hidden_layers
     a											=num_attention_heads
     a											=intermediate_size
     a											=hidden_act
     a											=hidden_dropout_prob
     a											=attention_probs_dropout_prob
     a											=type_sequence_label_size
     a											=initializer_range
     a											=encoder_stride
     a											=num_attention_outputs
     a											=embed_dim
     a											=embed_dim + 1
     a											=resolution
     a											=depths
     a											=hidden_sizes
     a											=dim
     a											=mlp_expansion_ratio


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	Union[str, Any]:
     a											=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

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


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


 def  SCREAMING_SNAKE_CASE				(						self					,       __A					,       __A					,       __A		)   ->	Dict:
     a											=self.type_sequence_label_size
     a											=EfficientFormerForImageClassification(__A		)
     model.to(__A		)
     model.eval()
     a											=model(__A					,       labels=__A		)
     self.parent.assertEqual(result.logits.shape					,       (self.batch_size, self.type_sequence_label_size)		)

     # test greyscale images
     a											=1
     a											=EfficientFormerForImageClassification(__A		)
     model.to(__A		)
     model.eval()

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





 def  SCREAMING_SNAKE_CASE				(						self		)   ->	int:
     a											=self.prepare_config_and_inputs()
     (
         (
         a
     )						, (
         a
     )						, (
         a
     )						, 
     )											=config_and_inputs
     a											={'''pixel_values''': pixel_values}
     return config, inputs_dict






@require_torch
class  __A							(			_SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE,       unittest.TestCase     ):






 """simple docstring"""




 __lowerCAmelCase					   =    (
     (
         EfficientFormerModel,
         EfficientFormerForImageClassificationWithTeacher,
         EfficientFormerForImageClassification,
     )
     if is_torch_available()
     else ()
 )
 __lowerCAmelCase					   =    (
     {
         "feature-extraction": EfficientFormerModel,
         "image-classification": (
             EfficientFormerForImageClassification,
             EfficientFormerForImageClassificationWithTeacher,
         ),
     }
     if is_torch_available()
     else {}
 )
 __lowerCAmelCase					   =    False

 __lowerCAmelCase					   =    False
 __lowerCAmelCase					   =    False
 __lowerCAmelCase					   =    False


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	Tuple:
     a											=EfficientFormerModelTester(self		)
     a											=ConfigTester(
         self					,       config_class=__A					,       has_text_modality=__A					,       hidden_size=37		)


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	Union[str, Any]:
     self.config_tester.run_common_tests()


 @unittest.skip(reason='''EfficientFormer does not use inputs_embeds'''		)
 def  SCREAMING_SNAKE_CASE				(						self		)   ->	List[str]:
     pass


 @unittest.skip(reason='''EfficientFormer does not support input and output embeddings'''		)
 def  SCREAMING_SNAKE_CASE				(						self		)   ->	List[str]:
     pass


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	List[Any]:
     a						, a											=self.model_tester.prepare_config_and_inputs_for_common()

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

         a											=['''pixel_values''']
         self.assertListEqual(arg_names[:1]					,       __A		)


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	List[str]:
     def check_hidden_states_output(__A					,       __A					,       __A		):
         a											=model_class(__A		)
         model.to(__A		)
         model.eval()

         with torch.no_grad():
             a											=model(**self._prepare_for_class(__A					,       __A		)		)

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

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

             self.assertIsInstance(__A					,       (list, tuple)		)
             self.assertEqual(len(__A		)					,       __A		)
             a											=getattr(self.model_tester					,       '''seq_length'''					,       __A		)
             a											=getattr(self.model_tester					,       '''decoder_seq_length'''					,       __A		)

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

     a						, a											=self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         a											=True
         check_hidden_states_output(__A					,       __A					,       __A		)

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

         check_hidden_states_output(__A					,       __A					,       __A		)


 def  SCREAMING_SNAKE_CASE				(						self					,       __A					,       __A					,       __A=False		)   ->	Any:
     a											=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  SCREAMING_SNAKE_CASE				(						self		)   ->	Any:
     a											=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  SCREAMING_SNAKE_CASE				(						self		)   ->	List[str]:
     a											=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_image_modeling(*__A		)


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	str:
     a											=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__A		)


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	List[Any]:
     if not self.model_tester.is_training:
         return

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


 def  SCREAMING_SNAKE_CASE				(						self		)   ->	str:
     a						, a											=self.model_tester.prepare_config_and_inputs_for_common()

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

                 a											=model_class(__A		)
                 model.to(__A		)
                 model.train()

                 a											=self._prepare_for_class(__A					,       __A					,       return_labels=__A		)

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

                 a											=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:
                     a											=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  SCREAMING_SNAKE_CASE				(						self		)   ->	Tuple:
     for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a											=EfficientFormerModel.from_pretrained(__A		)
         self.assertIsNotNone(__A		)





 def  SCREAMING_SNAKE_CASE				(						self		)   ->	Optional[int]:
     a						, a											=self.model_tester.prepare_config_and_inputs_for_common()
     a											=True

     a											=getattr(self.model_tester					,       '''seq_length'''					,       __A		)
     a											=getattr(self.model_tester					,       '''encoder_seq_length'''					,       __A		)
     a											=getattr(self.model_tester					,       '''key_length'''					,       __A		)
     a											=getattr(self.model_tester					,       '''chunk_length'''					,       __A		)

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

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







    """simple docstring"""
    a											=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
    return image






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






 """simple docstring"""




 @cached_property
 def  SCREAMING_SNAKE_CASE				(						self		)   ->	List[Any]:
     return (
         EfficientFormerImageProcessor.from_pretrained('''snap-research/efficientformer-l1-300'''		)
         if is_vision_available()
         else None
     )


 @slow
 def  SCREAMING_SNAKE_CASE				(						self		)   ->	Dict:
     a											=EfficientFormerForImageClassification.from_pretrained('''snap-research/efficientformer-l1-300'''		).to(
         __A		)

     a											=self.default_image_processor
     a											=prepare_img()
     a											=image_processor(images=__A					,       return_tensors='''pt'''		).to(__A		)

     # forward pass
     with torch.no_grad():
         a											=model(**__A		)

     # verify the logits
     a											=(1, 1000)
     self.assertEqual(outputs.logits.shape					,       __A		)

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





 @slow
 def  SCREAMING_SNAKE_CASE				(						self		)   ->	Dict:
     a											=EfficientFormerForImageClassificationWithTeacher.from_pretrained(
         '''snap-research/efficientformer-l1-300'''		).to(__A		)

     a											=self.default_image_processor
     a											=prepare_img()
     a											=image_processor(images=__A					,       return_tensors='''pt'''		).to(__A		)

     # forward pass
     with torch.no_grad():
         a											=model(**__A		)

     # verify the logits
     a											=(1, 1000)
     self.assertEqual(outputs.logits.shape					,       __A		)

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



"""simple docstring"""







import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import (
    TaBlock,
    TaConfig,
    TaLayerNorm,
)

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin






class  __A							(			_SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE     ):






 """simple docstring"""




 @register_to_config
 def __init__(						self					,       __A					,       __A					,       __A					,       __A					,       __A					,       __A					,       __A					,       __A					,       __A					,       __A = False					,       )   ->	Optional[Any]:
     super().__init__()

     a											=nn.Linear(__A					,       __A					,       bias=__A		)

     a											=nn.Embedding(__A					,       __A		)
     a											=False

     a											=nn.Dropout(p=__A		)

     a											=TaConfig(
         d_model=__A					,       num_heads=__A					,       d_kv=__A					,       d_ff=__A					,       feed_forward_proj=__A					,       dropout_rate=__A					,       is_decoder=__A					,       is_encoder_decoder=__A					,       )
     a											=nn.ModuleList()
     for lyr_num in range(__A		):
         a											=TaBlock(__A		)
         self.encoders.append(__A		)

     a											=TaLayerNorm(__A		)
     a											=nn.Dropout(p=__A		)





 def  SCREAMING_SNAKE_CASE				(						self					,       __A					,       __A		)   ->	str:
     a											=self.input_proj(__A		)

     # terminal relative positional encodings
     a											=encoder_inputs.shape[1]
     a											=torch.arange(__A					,       device=encoder_inputs.device		)

     a											=encoder_inputs_mask.sum(-1		)
     a											=torch.roll(input_positions.unsqueeze(0		)					,       tuple(seq_lens.tolist()		)					,       dims=0		)
     x += self.position_encoding(__A		)

     a											=self.dropout_pre(__A		)

     # inverted the attention mask
     a											=encoder_inputs.size()
     a											=self.get_extended_attention_mask(__A					,       __A		)

     for lyr in self.encoders:
         a											=lyr(__A					,       __A		)[0]
     a											=self.layer_norm(__A		)

     return self.dropout_post(__A		), encoder_inputs_mask