

"""simple docstring"""


from __future__ import annotations

import unittest
import warnings

from transformers import AutoTokenizer, MarianConfig, MarianTokenizer, TranslationPipeline, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import TFAutoModelForSeqaSeqLM, TFMarianModel, TFMarianMTModel



@require_tf
class        lowercase_			:
       snake_case__			:							List[Any]										=       MarianConfig
       snake_case__			:							Optional[Any]										=       {}
       snake_case__			:							str										=       """gelu"""

       def __init__(     self				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE=13				,   __SCREAMING_SNAKE_CASE=7				,   __SCREAMING_SNAKE_CASE=True				,   __SCREAMING_SNAKE_CASE=False				,   __SCREAMING_SNAKE_CASE=99				,   __SCREAMING_SNAKE_CASE=32				,   __SCREAMING_SNAKE_CASE=2				,   __SCREAMING_SNAKE_CASE=4				,   __SCREAMING_SNAKE_CASE=37				,   __SCREAMING_SNAKE_CASE=0.1				,   __SCREAMING_SNAKE_CASE=0.1				,   __SCREAMING_SNAKE_CASE=20				,   __SCREAMING_SNAKE_CASE=2				,   __SCREAMING_SNAKE_CASE=1				,   __SCREAMING_SNAKE_CASE=0				,   ):






        '''simple docstring'''

        snake_case__      							=			parent
        snake_case__      							=			batch_size
        snake_case__      							=			seq_length
        snake_case__      							=			is_training
        snake_case__      							=			use_labels
        snake_case__      							=			vocab_size
        snake_case__      							=			hidden_size
        snake_case__      							=			num_hidden_layers
        snake_case__      							=			num_attention_heads
        snake_case__      							=			intermediate_size

        snake_case__      							=			hidden_dropout_prob
        snake_case__      							=			attention_probs_dropout_prob
        snake_case__      							=			max_position_embeddings
        snake_case__      							=			eos_token_id
        snake_case__      							=			pad_token_id
        snake_case__      							=			bos_token_id

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			ids_tensor([self.batch_size, self.seq_length - 1]				,   self.vocab_size       )
        snake_case__      							=			tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size       )				,   1       )
        snake_case__      							=			tf.concat([input_ids, eos_tensor]				,   axis=1       )

        snake_case__      							=			ids_tensor([self.batch_size, self.seq_length]				,   self.vocab_size       )

        snake_case__      							=			self.config_cls(
            vocab_size=self.vocab_size				,   d_model=self.hidden_size				,   encoder_layers=self.num_hidden_layers				,   decoder_layers=self.num_hidden_layers				,   encoder_attention_heads=self.num_attention_heads				,   decoder_attention_heads=self.num_attention_heads				,   encoder_ffn_dim=self.intermediate_size				,   decoder_ffn_dim=self.intermediate_size				,   dropout=self.hidden_dropout_prob				,   attention_dropout=self.attention_probs_dropout_prob				,   max_position_embeddings=self.max_position_embeddings				,   eos_token_ids=[2]				,   bos_token_id=self.bos_token_id				,   pad_token_id=self.pad_token_id				,   decoder_start_token_id=self.pad_token_id				,   **self.config_updates				,   )
        snake_case__      							=			prepare_marian_inputs_dict(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
        return config, inputs_dict

       def 						_SCREAMING_SNAKE_CASE(     self				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       ):






        '''simple docstring'''

        snake_case__      							=			TFMarianModel(config=__SCREAMING_SNAKE_CASE       ).get_decoder()
        snake_case__      							=			inputs_dict["input_ids"]

        snake_case__      							=			input_ids[:1, :]
        snake_case__      							=			inputs_dict["attention_mask"][:1, :]
        snake_case__      							=			inputs_dict["head_mask"]
        snake_case__      							=			1

        # first forward pass
        snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   attention_mask=__SCREAMING_SNAKE_CASE				,   head_mask=__SCREAMING_SNAKE_CASE				,   use_cache=__SCREAMING_SNAKE_CASE       )

        snake_case__							,				snake_case__      							=			outputs.to_tuple()

        # create hypothetical next token and extent to next_input_ids
        snake_case__      							=			ids_tensor((self.batch_size, 3)				,   config.vocab_size       )
        snake_case__      							=			tf.cast(ids_tensor((self.batch_size, 3)				,   2       )				,   tf.inta       )

        # append to next input_ids and
        snake_case__      							=			tf.concat([input_ids, next_tokens]				,   axis=-1       )
        snake_case__      							=			tf.concat([attention_mask, next_attn_mask]				,   axis=-1       )

        snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   attention_mask=__SCREAMING_SNAKE_CASE       )[0]
        snake_case__      							=			model(__SCREAMING_SNAKE_CASE				,   attention_mask=__SCREAMING_SNAKE_CASE				,   past_key_values=__SCREAMING_SNAKE_CASE       )[0]

        self.parent.assertEqual(next_tokens.shape[1]				,   output_from_past.shape[1]       )

        # select random slice
        snake_case__      							=			int(ids_tensor((1,)				,   output_from_past.shape[-1]       )       )
        snake_case__      							=			output_from_no_past[:, -3:, random_slice_idx]
        snake_case__      							=			output_from_past[:, :, random_slice_idx]

        # test that outputs are equal for slice
        tf.debugging.assert_near(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   rtol=1e-3       )





def 							a__     (					_UpperCAmelCase		,       _UpperCAmelCase		,       _UpperCAmelCase		,       _UpperCAmelCase=None		,       _UpperCAmelCase=None		,       _UpperCAmelCase=None		,       _UpperCAmelCase=None		,       _UpperCAmelCase=None		,       )      ->					Optional[Any]:

 '''simple docstring'''






 if attention_mask is None:
  snake_case__      							=			tf.cast(tf.math.not_equal(_UpperCAmelCase		,       config.pad_token_id		)		,       tf.inta		)
 if decoder_attention_mask is None:
  snake_case__      							=			tf.concat(
      [
          tf.ones(decoder_input_ids[:, :1].shape		,       dtype=tf.inta		),
          tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]		,       config.pad_token_id		)		,       tf.inta		),
      ]		,       axis=-1		,       )
 if head_mask is None:
  snake_case__      							=			tf.ones((config.encoder_layers, config.encoder_attention_heads)		)
 if decoder_head_mask is None:
  snake_case__      							=			tf.ones((config.decoder_layers, config.decoder_attention_heads)		)
 if cross_attn_head_mask is None:
  snake_case__      							=			tf.ones((config.decoder_layers, config.decoder_attention_heads)		)
 return {
     "input_ids": input_ids,
     "decoder_input_ids": decoder_input_ids,
     "attention_mask": attention_mask,
     "decoder_attention_mask": decoder_attention_mask,
     "head_mask": head_mask,
     "decoder_head_mask": decoder_head_mask,
     "cross_attn_head_mask": cross_attn_head_mask,
 }



@require_tf
class        lowercase_			(       __lowerCAmelCase			,__lowerCAmelCase			,unittest.TestCase							):
       snake_case__			:							Union[str, Any]										=       (TFMarianMTModel, TFMarianModel) if is_tf_available() else ()
       snake_case__			:							Tuple										=       (TFMarianMTModel,) if is_tf_available() else ()
       snake_case__			:							List[Any]										=       (
           {
               """conversational""": TFMarianMTModel,
               """feature-extraction""": TFMarianModel,
               """summarization""": TFMarianMTModel,
               """text2text-generation""": TFMarianMTModel,
               """translation""": TFMarianMTModel,
           }
           if is_tf_available()
           else {}
       )
       snake_case__			:							List[str]										=       True
       snake_case__			:							Optional[Any]										=       False
       snake_case__			:							Any										=       False

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			TFMarianModelTester(self       )
        snake_case__      							=			ConfigTester(self				,   config_class=__SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        self.config_tester.run_common_tests()

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			self.model_tester.prepare_config_and_inputs_for_common()
        self.model_tester.check_decoder_model_past_large_inputs(*__SCREAMING_SNAKE_CASE       )



@require_tf
class        lowercase_			(       unittest.TestCase							):
       snake_case__			:							Optional[int]										=       10_00  # show more chars for failing integration tests

       @classmethod
       def 						_SCREAMING_SNAKE_CASE(     cls       ):






        '''simple docstring'''

        snake_case__      							=			F"Helsinki-NLP/opus-mt-{cls.src}-{cls.tgt}"
        return cls

       @cached_property
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        return AutoTokenizer.from_pretrained(self.model_name       )

       @property
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        return self.tokenizer.eos_token_id

       @cached_property
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        warnings.simplefilter("error"       )
        snake_case__      							=			TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name       )
        assert isinstance(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )
        snake_case__      							=			model.config
        self.assertListEqual(c.bad_words_ids				,   [[c.pad_token_id]]       )
        self.assertEqual(c.max_length				,   512       )
        self.assertEqual(c.decoder_start_token_id				,   c.pad_token_id       )
        return model

       def 						_SCREAMING_SNAKE_CASE(     self				,   **__SCREAMING_SNAKE_CASE       ):






        '''simple docstring'''

        snake_case__      							=			self.translate_src_text(**__SCREAMING_SNAKE_CASE       )
        self.assertListEqual(self.expected_text				,   __SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self				,   **__SCREAMING_SNAKE_CASE       ):






        '''simple docstring'''

        snake_case__      							=			self.tokenizer(self.src_text				,   **__SCREAMING_SNAKE_CASE				,   padding=__SCREAMING_SNAKE_CASE				,   return_tensors="tf"       )
        snake_case__      							=			self.model.generate(
            model_inputs.input_ids				,   attention_mask=model_inputs.attention_mask				,   num_beams=2				,   max_length=128       )
        snake_case__      							=			self.tokenizer.batch_decode(generated_ids.numpy()				,   skip_special_tokens=__SCREAMING_SNAKE_CASE       )
        return generated_words



@require_sentencepiece
@require_tokenizers
@require_tf
class        lowercase_			(       __lowerCAmelCase							):
       snake_case__			:							int										=       """mt"""
       snake_case__			:							Dict										=       """en"""
       snake_case__			:							Dict										=       ["""Billi messu b'mod ġentili, Ġesù fejjaq raġel li kien milqut bil - marda kerha tal - ġdiem."""]
       snake_case__			:							List[str]										=       ["""Touching gently, Jesus healed a man who was affected by the sad disease of leprosy."""]

       @unittest.skip("Skipping until #12647 is resolved."       )
       @slow
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        self._assert_generated_batch_equal_expected()



@require_sentencepiece
@require_tokenizers
@require_tf
class        lowercase_			(       __lowerCAmelCase							):
       snake_case__			:							List[Any]										=       """en"""
       snake_case__			:							Any										=       """zh"""
       snake_case__			:							Optional[Any]										=       ["""My name is Wolfgang and I live in Berlin"""]
       snake_case__			:							Optional[Any]										=       ["""我叫沃尔夫冈 我住在柏林"""]

       @unittest.skip("Skipping until #12647 is resolved."       )
       @slow
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        self._assert_generated_batch_equal_expected()



@require_sentencepiece
@require_tokenizers
@require_tf
class        lowercase_			(       __lowerCAmelCase							):
       snake_case__			:							Union[str, Any]										=       """en"""
       snake_case__			:							str										=       """ROMANCE"""
       snake_case__			:							int										=       [
           """>>fr<< Don't spend so much time watching TV.""",
           """>>pt<< Your message has been sent.""",
           """>>es<< He's two years older than me.""",
       ]
       snake_case__			:							Optional[Any]										=       [
           """Ne passez pas autant de temps à regarder la télé.""",
           """A sua mensagem foi enviada.""",
           """Es dos años más viejo que yo.""",
       ]

       @unittest.skip("Skipping until #12647 is resolved."       )
       @slow
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        self._assert_generated_batch_equal_expected()

       @unittest.skip("Skipping until #12647 is resolved."       )
       @slow
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			TranslationPipeline(self.model				,   self.tokenizer				,   framework="tf"       )
        snake_case__      							=			pipeline(self.src_text       )
        self.assertEqual(self.expected_text				,   [x["translation_text"] for x in output]       )








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

       def __init__(     self				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE = 13				,   __SCREAMING_SNAKE_CASE = 64				,   __SCREAMING_SNAKE_CASE = 2				,   __SCREAMING_SNAKE_CASE = 3				,   __SCREAMING_SNAKE_CASE = 3				,   __SCREAMING_SNAKE_CASE = True				,   __SCREAMING_SNAKE_CASE = True				,   __SCREAMING_SNAKE_CASE = 128				,   __SCREAMING_SNAKE_CASE=[16, 32, 64, 128]				,   __SCREAMING_SNAKE_CASE = 7				,   __SCREAMING_SNAKE_CASE = 4				,   __SCREAMING_SNAKE_CASE = 37				,   __SCREAMING_SNAKE_CASE = "gelu"				,   __SCREAMING_SNAKE_CASE = 0.1				,   __SCREAMING_SNAKE_CASE = 0.1				,   __SCREAMING_SNAKE_CASE = 10				,   __SCREAMING_SNAKE_CASE = 0.02				,   __SCREAMING_SNAKE_CASE = 2				,   __SCREAMING_SNAKE_CASE = 1				,   __SCREAMING_SNAKE_CASE = 128				,   __SCREAMING_SNAKE_CASE = [2, 2, 2, 2]				,   __SCREAMING_SNAKE_CASE = 2				,   __SCREAMING_SNAKE_CASE = 2				,   ):






        '''simple docstring'''

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

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

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

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

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

       def 						_SCREAMING_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=__SCREAMING_SNAKE_CASE				,   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				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       ):






        '''simple docstring'''

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

       def 						_SCREAMING_SNAKE_CASE(     self				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       ):






        '''simple docstring'''

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

        # test greyscale images
        snake_case__      							=			1
        snake_case__      							=			EfficientFormerForImageClassification(__SCREAMING_SNAKE_CASE       )
        model.to(__SCREAMING_SNAKE_CASE       )
        model.eval()

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

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

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



@require_torch
class        lowercase_			(       __lowerCAmelCase			,__lowerCAmelCase			,unittest.TestCase							):
       snake_case__			:							str										=       (
           (
               EfficientFormerModel,
               EfficientFormerForImageClassificationWithTeacher,
               EfficientFormerForImageClassification,
           )
           if is_torch_available()
           else ()
       )
       snake_case__			:							Optional[Any]										=       (
           {
               """feature-extraction""": EfficientFormerModel,
               """image-classification""": (
                   EfficientFormerForImageClassification,
                   EfficientFormerForImageClassificationWithTeacher,
               ),
           }
           if is_torch_available()
           else {}
       )
       snake_case__			:							Tuple										=       False

       snake_case__			:							List[Any]										=       False
       snake_case__			:							Union[str, Any]										=       False
       snake_case__			:							Optional[Any]										=       False

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			EfficientFormerModelTester(self       )
        snake_case__      							=			ConfigTester(
            self				,   config_class=__SCREAMING_SNAKE_CASE				,   has_text_modality=__SCREAMING_SNAKE_CASE				,   hidden_size=37       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        self.config_tester.run_common_tests()

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






        '''simple docstring'''

        pass

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






        '''simple docstring'''

        pass

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__							,				snake_case__      							=			self.model_tester.prepare_config_and_inputs_for_common()

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

         snake_case__      							=			["pixel_values"]
         self.assertListEqual(arg_names[:1]				,   __SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        def check_hidden_states_output(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       ):
         snake_case__      							=			model_class(__SCREAMING_SNAKE_CASE       )
         model.to(__SCREAMING_SNAKE_CASE       )
         model.eval()

         with torch.no_grad():
          snake_case__      							=			model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )       )

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

         snake_case__      							=			getattr(
             self.model_tester				,   "expected_num_hidden_layers"				,   self.model_tester.num_hidden_layers + 1       )
         self.assertEqual(len(__SCREAMING_SNAKE_CASE       )				,   __SCREAMING_SNAKE_CASE       )

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

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

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

        snake_case__							,				snake_case__      							=			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
         snake_case__      							=			True
         check_hidden_states_output(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

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

         check_hidden_states_output(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE=False       ):






        '''simple docstring'''

        snake_case__      							=			super()._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   return_labels=__SCREAMING_SNAKE_CASE       )

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

        return inputs_dict

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE       )

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






        '''simple docstring'''

        snake_case__      							=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*__SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE       )

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        if not self.model_tester.is_training:
         return

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

        for model_class in self.all_model_classes:
         # EfficientFormerForImageClassificationWithTeacher supports inference-only
         if (
             model_class in get_values(__SCREAMING_SNAKE_CASE       )
             or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
         ):
          continue
         snake_case__      							=			model_class(__SCREAMING_SNAKE_CASE       )
         model.to(__SCREAMING_SNAKE_CASE       )
         model.train()
         snake_case__      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   return_labels=__SCREAMING_SNAKE_CASE       )
         snake_case__      							=			model(**__SCREAMING_SNAKE_CASE       ).loss
         loss.backward()

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__							,				snake_case__      							=			self.model_tester.prepare_config_and_inputs_for_common()

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

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

           snake_case__      							=			model_class(__SCREAMING_SNAKE_CASE       )
           model.to(__SCREAMING_SNAKE_CASE       )
           model.train()

           snake_case__      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE				,   return_labels=__SCREAMING_SNAKE_CASE       )

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

           snake_case__      							=			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=__SCREAMING_SNAKE_CASE       ) as warning_list:
            snake_case__      							=			model(**__SCREAMING_SNAKE_CASE       ).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       ):






        '''simple docstring'''

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

       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

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

        snake_case__      							=			getattr(self.model_tester				,   "seq_length"				,   __SCREAMING_SNAKE_CASE       )
        snake_case__      							=			getattr(self.model_tester				,   "encoder_seq_length"				,   __SCREAMING_SNAKE_CASE       )
        snake_case__      							=			getattr(self.model_tester				,   "key_length"				,   __SCREAMING_SNAKE_CASE       )
        snake_case__      							=			getattr(self.model_tester				,   "chunk_length"				,   __SCREAMING_SNAKE_CASE       )

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

        for model_class in self.all_model_classes:
         snake_case__      							=			True
         snake_case__      							=			False
         snake_case__      							=			True
         snake_case__      							=			model_class(__SCREAMING_SNAKE_CASE       )
         model.to(__SCREAMING_SNAKE_CASE       )
         model.eval()
         with torch.no_grad():
          snake_case__      							=			model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE				,   __SCREAMING_SNAKE_CASE       )       )
         snake_case__      							=			outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
         self.assertEqual(len(__SCREAMING_SNAKE_CASE       )				,   self.model_tester.num_attention_outputs       )

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

 '''simple docstring'''






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



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

       @cached_property
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

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

       @slow
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			EfficientFormerForImageClassification.from_pretrained("snap-research/efficientformer-l1-300"       ).to(
            __SCREAMING_SNAKE_CASE       )

        snake_case__      							=			self.default_image_processor
        snake_case__      							=			prepare_img()
        snake_case__      							=			image_processor(images=__SCREAMING_SNAKE_CASE				,   return_tensors="pt"       ).to(__SCREAMING_SNAKE_CASE       )

        # forward pass
        with torch.no_grad():
         snake_case__      							=			model(**__SCREAMING_SNAKE_CASE       )

        # verify the logits
        snake_case__      							=			(1, 1000)
        self.assertEqual(outputs.logits.shape				,   __SCREAMING_SNAKE_CASE       )

        snake_case__      							=			torch.tensor([-0.0555, 0.4825, -0.0852]       ).to(__SCREAMING_SNAKE_CASE       )
        self.assertTrue(torch.allclose(outputs.logits[0][:3]				,   __SCREAMING_SNAKE_CASE				,   atol=1e-4       )       )

       @slow
       def 						_SCREAMING_SNAKE_CASE(     self       ):






        '''simple docstring'''

        snake_case__      							=			EfficientFormerForImageClassificationWithTeacher.from_pretrained(
            "snap-research/efficientformer-l1-300"       ).to(__SCREAMING_SNAKE_CASE       )

        snake_case__      							=			self.default_image_processor
        snake_case__      							=			prepare_img()
        snake_case__      							=			image_processor(images=__SCREAMING_SNAKE_CASE				,   return_tensors="pt"       ).to(__SCREAMING_SNAKE_CASE       )

        # forward pass
        with torch.no_grad():
         snake_case__      							=			model(**__SCREAMING_SNAKE_CASE       )

        # verify the logits
        snake_case__      							=			(1, 1000)
        self.assertEqual(outputs.logits.shape				,   __SCREAMING_SNAKE_CASE       )

        snake_case__      							=			torch.tensor([-0.1312, 0.4353, -1.0499]       ).to(__SCREAMING_SNAKE_CASE       )
        self.assertTrue(torch.allclose(outputs.logits[0][:3]				,   __SCREAMING_SNAKE_CASE				,   atol=1e-4       )       )

