





import inspect
import unittest
from typing import List

import numpy as np

from transformers import EfficientFormerConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

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


if is_tf_available():
     import tensorflow as tf

     from transformers import (
         TFEfficientFormerForImageClassification,
         TFEfficientFormerForImageClassificationWithTeacher,
         TFEfficientFormerModel,
     )
     from transformers.models.efficientformer.modeling_tf_efficientformer import (
         TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
     )


if is_vision_available():
     from PIL import Image

     from transformers import EfficientFormerImageProcessor







class     __lowerCAmelCase	:







      def __init__(   self		,				lowerCAmelCase__		,				lowerCAmelCase__ = 1_3		,				lowerCAmelCase__ = 6_4		,				lowerCAmelCase__ = 2		,				lowerCAmelCase__ = 3		,				lowerCAmelCase__ = 3		,				lowerCAmelCase__ = True		,				lowerCAmelCase__ = True		,				lowerCAmelCase__ = 1_2_8		,				lowerCAmelCase__=[1_6, 3_2, 6_4, 1_2_8]		,				lowerCAmelCase__ = 7		,				lowerCAmelCase__ = 4		,				lowerCAmelCase__ = 3_7		,				lowerCAmelCase__ = "gelu"		,				lowerCAmelCase__ = 0.1		,				lowerCAmelCase__ = 0.1		,				lowerCAmelCase__ = 1_0		,				lowerCAmelCase__ = 0.02		,				lowerCAmelCase__ = 2		,				lowerCAmelCase__ = 1		,				lowerCAmelCase__ = 1_2_8		,				lowerCAmelCase__ = [2, 2, 2, 2]		,				lowerCAmelCase__ = 2		,				lowerCAmelCase__ = 2		,				)       ->						Optional[Any]:



        '''simple docstring'''




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







      def        _lowercase  (   self							)       ->						List[str]:



        '''simple docstring'''




        a__     : Union[str, Any]  =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

        a__     : Union[str, Any]  =None
        if self.use_labels:
          a__     : Optional[Any]  =ids_tensor([self.batch_size]		,				self.type_sequence_label_size							)

        a__     : Optional[int]  =self.get_config()

        return config, pixel_values, labels







      def        _lowercase  (   self							)       ->						Union[str, Any]:



        '''simple docstring'''




        return EfficientFormerConfig(
            image_size=self.image_size		,				patch_size=self.patch_size		,				num_channels=self.num_channels		,				hidden_size=self.hidden_size		,				num_hidden_layers=self.num_hidden_layers		,				num_attention_heads=self.num_attention_heads		,				intermediate_size=self.intermediate_size		,				hidden_act=self.hidden_act		,				hidden_dropout_prob=self.hidden_dropout_prob		,				attention_probs_dropout_prob=self.attention_probs_dropout_prob		,				is_decoder=lowerCAmelCase__		,				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        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							)       ->						Union[str, Any]:



        '''simple docstring'''




        a__     : Tuple  =TFEfficientFormerModel(config=lowerCAmelCase__							)
        a__     : Optional[Any]  =model(lowerCAmelCase__		,				training=lowerCAmelCase__							)
        self.parent.assertEqual(result.last_hidden_state.shape		,				(self.batch_size, self.seq_length, self.hidden_size)							)







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							)       ->						int:



        '''simple docstring'''




        a__     : List[Any]  =self.type_sequence_label_size
        a__     : str  =TFEfficientFormerForImageClassification(lowerCAmelCase__							)
        a__     : List[Any]  =model(lowerCAmelCase__		,				labels=lowerCAmelCase__		,				training=lowerCAmelCase__							)
        self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.type_sequence_label_size)							)

        # test greyscale images
        a__     : Union[str, Any]  =1
        a__     : Tuple  =TFEfficientFormerForImageClassification(lowerCAmelCase__							)

        a__     : str  =floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
        a__     : Any  =model(lowerCAmelCase__		,				labels=lowerCAmelCase__							)
        self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.type_sequence_label_size)							)







      def        _lowercase  (   self							)       ->						List[Any]:



        '''simple docstring'''




        a__     : List[str]  =self.prepare_config_and_inputs()
        a__ ,							a__ ,							a__     : Any  =config_and_inputs
        a__     : Dict  ={"pixel_values": pixel_values}
        return config, inputs_dict







@require_tf
class     __lowerCAmelCase	(   UpperCamelCase__						,    UpperCamelCase__						,    unittest.TestCase):
      _lowercase    : Tuple            =      (
          (
              TFEfficientFormerModel,
              TFEfficientFormerForImageClassificationWithTeacher,
              TFEfficientFormerForImageClassification,
          )
          if is_tf_available()
          else ()
      )
      _lowercase    : Dict            =      (
          {
              """feature-extraction""": TFEfficientFormerModel,
              """image-classification""": (
                  TFEfficientFormerForImageClassification,
                  TFEfficientFormerForImageClassificationWithTeacher,
              ),
          }
          if is_tf_available()
          else {}
      )

      _lowercase    : Union[str, Any]            =      False

      _lowercase    : Dict            =      False
      _lowercase    : str            =      False
      _lowercase    : Optional[Any]            =      False
      _lowercase    : Optional[Any]            =      False







      def        _lowercase  (   self							)       ->						Union[str, Any]:



        '''simple docstring'''




        a__     : List[str]  =TFEfficientFormerModelTester(self							)
        a__     : Union[str, Any]  =ConfigTester(
            self		,				config_class=lowerCAmelCase__		,				has_text_modality=lowerCAmelCase__		,				hidden_size=3_7							)







      def        _lowercase  (   self							)       ->						List[str]:



        '''simple docstring'''




        self.config_tester.run_common_tests()







      @unittest.skip(reason="EfficientFormer does not use inputs_embeds"							)
      def        _lowercase  (   self							)       ->						str:



        '''simple docstring'''




        pass







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



        '''simple docstring'''




        pass







      def        _lowercase  (   self							)       ->						Optional[Any]:



        '''simple docstring'''




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

        for model_class in self.all_model_classes:
          a__     : Any  =model_class(lowerCAmelCase__							)
          a__     : str  =inspect.signature(model.call							)
          # signature.parameters is an OrderedDict => so arg_names order is deterministic
          a__     : Any  =[*signature.parameters.keys()]

          a__     : Union[str, Any]  =["pixel_values"]
          self.assertListEqual(arg_names[:1]		,				lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						int:



        '''simple docstring'''




        def check_hidden_states_output(lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							):
          a__     : List[Any]  =model_class(lowerCAmelCase__							)

          a__     : Dict  =model(**self._prepare_for_class(lowerCAmelCase__		,				lowerCAmelCase__							)		,				training=lowerCAmelCase__							)
          a__     : Any  =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

          a__     : Optional[int]  =getattr(
              self.model_tester		,				"expected_num_hidden_layers"		,				self.model_tester.num_hidden_layers + 1							)
          self.assertEqual(len(lowerCAmelCase__							)		,				lowerCAmelCase__							)

          if hasattr(self.model_tester		,				"encoder_seq_length"							):
            a__     : str  =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__     : str  =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__     : List[Any]  =outputs.decoder_hidden_states

            self.asseretIsInstance(lowerCAmelCase__		,				(list, tuple)							)
            self.assertEqual(len(lowerCAmelCase__							)		,				lowerCAmelCase__							)
            a__     : Dict  =getattr(self.model_tester		,				"seq_length"		,				lowerCAmelCase__							)
            a__     : str  =getattr(self.model_tester		,				"decoder_seq_length"		,				lowerCAmelCase__							)

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

        a__ ,							a__     : Any  =self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          a__     : Any  =True
          check_hidden_states_output(lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							)

          # check that output_hidden_states also work using config
          del inputs_dict["output_hidden_states"]
          a__     : Any  =True

          check_hidden_states_output(lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							)







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__=False							)       ->						Tuple:



        '''simple docstring'''




        a__     : List[str]  =super()._prepare_for_class(lowerCAmelCase__		,				lowerCAmelCase__		,				return_labels=lowerCAmelCase__							)

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

        return inputs_dict







      def        _lowercase  (   self							)       ->						str:



        '''simple docstring'''




        a__     : str  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__							)







      @unittest.skip(reason="EfficientFormer does not implement masked image modeling yet"							)
      def        _lowercase  (   self							)       ->						Union[str, Any]:



        '''simple docstring'''




        a__     : Optional[Any]  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						List[str]:



        '''simple docstring'''




        a__     : Optional[int]  =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase__							)







      @slow
      def        _lowercase  (   self							)       ->						Optional[Any]:



        '''simple docstring'''




        for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          a__     : List[Any]  =TFEfficientFormerModel.from_pretrained(lowerCAmelCase__							)
          self.assertIsNotNone(lowerCAmelCase__							)







      def        _lowercase  (   self							)       ->						Any:



        '''simple docstring'''




        a__ ,							a__     : Union[str, Any]  =self.model_tester.prepare_config_and_inputs_for_common()
        a__     : List[Any]  =True

        a__     : Tuple  =getattr(self.model_tester		,				"seq_length"		,				lowerCAmelCase__							)
        a__     : Any  =getattr(self.model_tester		,				"encoder_seq_length"		,				lowerCAmelCase__							)
        a__     : str  =getattr(self.model_tester		,				"key_length"		,				lowerCAmelCase__							)
        a__     : Union[str, Any]  =getattr(self.model_tester		,				"chunk_length"		,				lowerCAmelCase__							)

        if chunk_length is not None and hasattr(self.model_tester		,				"num_hashes"							):
          a__     : Optional[int]  =encoder_seq_length * self.model_tester.num_hashes

        for model_class in self.all_model_classes:
          a__     : str  =True
          a__     : Dict  =False
          a__     : str  =True
          a__     : Tuple  =model_class(lowerCAmelCase__							)

          a__     : Tuple  =model(**self._prepare_for_class(lowerCAmelCase__		,				lowerCAmelCase__							)		,				training=lowerCAmelCase__							)
          a__     : List[Any]  =outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
          self.assertEqual(len(lowerCAmelCase__							)		,				self.model_tester.num_attention_outputs							)

          # check that output_attentions also work using config
          del inputs_dict["output_attentions"]
          a__     : List[Any]  =True
          a__     : List[Any]  =model_class(lowerCAmelCase__							)
          a__     : Union[str, Any]  =model(**self._prepare_for_class(lowerCAmelCase__		,				lowerCAmelCase__							)		,				training=lowerCAmelCase__							)

          a__     : Union[str, Any]  =outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
          self.assertEqual(len(lowerCAmelCase__							)		,				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        _lowercase  (   self							)       ->						int:



        '''simple docstring'''




        a__ ,							a__     : Dict  =self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          # Prepare our model
          a__     : List[Any]  =model_class(lowerCAmelCase__							)
          # These are maximally general inputs for the model, with multiple None dimensions
          # Hopefully this will catch any conditionals that fail for flexible shapes
          a__     : Any  ={
              key: tf.keras.Input(shape=val.shape[1:]		,				dtype=val.dtype		,				name=lowerCAmelCase__							)
              for key, val in model.input_signature.items()
              if key in model.dummy_inputs
          }
          a__     : str  =model(lowerCAmelCase__							)
          self.assertTrue(outputs_dict is not None							)
def 					_A	(		):





  """simple docstring"""



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







@require_tf
@require_vision
class     __lowerCAmelCase	(   unittest.TestCase):







      @cached_property
      def        _lowercase  (   self							)       ->						Tuple:



        '''simple docstring'''




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







      @slow
      def        _lowercase  (   self							)       ->						Any:



        '''simple docstring'''




        a__     : List[Any]  =TFEfficientFormerForImageClassification.from_pretrained("snap-research/efficientformer-l1-300"							)
        a__     : Union[str, Any]  =self.default_image_processor
        a__     : str  =prepare_img()
        a__     : str  =image_processor(images=lowerCAmelCase__		,				return_tensors="tf"							)
        # forward pass
        a__     : Any  =model(**lowerCAmelCase__		,				training=lowerCAmelCase__							)
        # verify the logits
        a__     : List[str]  =tf.TensorShape((1, 1_0_0_0)							)
        self.assertEqual(outputs.logits.shape		,				lowerCAmelCase__							)
        a__     : Tuple  =tf.constant([-0.05_55, 0.48_25, -0.08_52]							)
        self.assertTrue(np.allclose(outputs.logits[0, :3]		,				lowerCAmelCase__		,				atol=1E-4							)							)







      @slow
      def        _lowercase  (   self							)       ->						Optional[int]:



        '''simple docstring'''




        a__     : List[Any]  =TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
            "snap-research/efficientformer-l1-300"							)
        a__     : List[str]  =self.default_image_processor
        a__     : Any  =prepare_img()
        a__     : str  =image_processor(images=lowerCAmelCase__		,				return_tensors="tf"							)
        # forward pass
        a__     : Any  =model(**lowerCAmelCase__		,				training=lowerCAmelCase__							)
        # verify the logits
        a__     : int  =tf.TensorShape((1, 1_0_0_0)							)
        self.assertEqual(outputs.logits.shape		,				lowerCAmelCase__							)
        a__     : Tuple  =tf.constant([-0.13_12, 0.43_53, -1.04_99]							)
        self.assertTrue(np.allclose(outputs.logits[0, :3]		,				lowerCAmelCase__		,				atol=1E-4							)							)













import warnings

from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase     :     Dict							=	logging.get_logger(__name__)

UpperCAmelCase     :     Dict							=	{
    """RUCAIBox/mvp""": """https://huggingface.co/RUCAIBox/mvp/resolve/main/config.json""",
}







class     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : List[Any]            =      """mvp"""
      _lowercase    : Tuple            =      ["""past_key_values"""]
      _lowercase    : Dict            =      {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}







      def __init__(   self		,				lowerCAmelCase__=5_0_2_6_7		,				lowerCAmelCase__=1_0_2_4		,				lowerCAmelCase__=1_2		,				lowerCAmelCase__=4_0_9_6		,				lowerCAmelCase__=1_6		,				lowerCAmelCase__=1_2		,				lowerCAmelCase__=4_0_9_6		,				lowerCAmelCase__=1_6		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__="gelu"		,				lowerCAmelCase__=1_0_2_4		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=0.02		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=False		,				lowerCAmelCase__=True		,				lowerCAmelCase__=1		,				lowerCAmelCase__=0		,				lowerCAmelCase__=2		,				lowerCAmelCase__=True		,				lowerCAmelCase__=2		,				lowerCAmelCase__=2		,				lowerCAmelCase__=False		,				lowerCAmelCase__=1_0_0		,				lowerCAmelCase__=8_0_0		,				**lowerCAmelCase__		,				)       ->						str:



        '''simple docstring'''




        a__     : Dict  =vocab_size
        a__     : List[str]  =max_position_embeddings
        a__     : List[str]  =d_model
        a__     : Optional[Any]  =encoder_ffn_dim
        a__     : Dict  =encoder_layers
        a__     : List[str]  =encoder_attention_heads
        a__     : List[str]  =decoder_ffn_dim
        a__     : Tuple  =decoder_layers
        a__     : Tuple  =decoder_attention_heads
        a__     : Any  =dropout
        a__     : str  =attention_dropout
        a__     : str  =activation_dropout
        a__     : Optional[int]  =activation_function
        a__     : Union[str, Any]  =init_std
        a__     : Dict  =encoder_layerdrop
        a__     : List[str]  =decoder_layerdrop
        a__     : Union[str, Any]  =classifier_dropout
        a__     : Union[str, Any]  =use_cache
        a__     : Optional[Any]  =encoder_layers
        a__     : List[str]  =scale_embedding  # scale factor will be sqrt(d_model) if True
        a__     : int  =use_prompt
        a__     : Union[str, Any]  =prompt_length
        a__     : Dict  =prompt_mid_dim

        super().__init__(
            pad_token_id=lowerCAmelCase__		,				bos_token_id=lowerCAmelCase__		,				eos_token_id=lowerCAmelCase__		,				is_encoder_decoder=lowerCAmelCase__		,				decoder_start_token_id=lowerCAmelCase__		,				forced_eos_token_id=lowerCAmelCase__		,				**lowerCAmelCase__		,				)

        if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated"		,				lowerCAmelCase__							):
          a__     : Tuple  =self.bos_token_id
          warnings.warn(
              F'''Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. '''
              "The config can simply be saved and uploaded again to be fixed."							)






