


"""simple docstring"""

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						:						str   ,							A_						:						Optional[int]   ,							A_						:						int = 1_3   ,							A_						:						int = 6_4   ,							A_						:						int = 2   ,							A_						:						int = 3   ,							A_						:						int = 3   ,							A_						:						bool = True   ,							A_						:						bool = True   ,							A_						:						int = 1_2_8   ,							A_						:						Any=[1_6, 3_2, 6_4, 1_2_8]   ,							A_						:						int = 7   ,							A_						:						int = 4   ,							A_						:						int = 3_7   ,							A_						:						str = "gelu"   ,							A_						:						float = 0.1   ,							A_						:						float = 0.1   ,							A_						:						int = 1_0   ,							A_						:						float = 0.02   ,							A_						:						int = 2   ,							A_						:						int = 1   ,							A_						:						int = 1_2_8   ,							A_						:						List[int] = [2, 2, 2, 2]   ,							A_						:						int = 2   ,							A_						:						int = 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 SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''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 SCREAMING_SNAKE_CASE_						(		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=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						:						Tuple   ,							A_						:						Dict   ,							A_						:						Tuple   ,							A_						:						str				):






       '''simple docstring'''


       __lowercase						=						TFEfficientFormerModel(config=A_				)
       __lowercase						=						model(A_   ,							training=A_				)
       self.parent.assertEqual(result.last_hidden_state.shape   ,							(self.batch_size, self.seq_length, self.hidden_size)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Any   ,							A_						:						Tuple   ,							A_						:						Tuple   ,							A_						:						str				):






       '''simple docstring'''


       __lowercase						=						self.type_sequence_label_size
       __lowercase						=						TFEfficientFormerForImageClassification(A_				)
       __lowercase						=						model(A_   ,							labels=A_   ,							training=A_				)
       self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.type_sequence_label_size)				)

       # test greyscale images
       __lowercase						=						1
       __lowercase						=						TFEfficientFormerForImageClassification(A_				)

       __lowercase						=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
       __lowercase						=						model(A_   ,							labels=A_				)
       self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.type_sequence_label_size)				)






      def SCREAMING_SNAKE_CASE_						(		self						:						Dict				):






       '''simple docstring'''


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






@require_tf
class  lowerCamelCase__    (		_a      , _a      , unittest.TestCase     ):
      a			:  Any           =		(
          (
              TFEfficientFormerModel,
              TFEfficientFormerForImageClassificationWithTeacher,
              TFEfficientFormerForImageClassification,
          )
          if is_tf_available()
          else ()
      )
      a			:  Tuple           =		(
          {
              """feature-extraction""": TFEfficientFormerModel,
              """image-classification""": (
                  TFEfficientFormerForImageClassification,
                  TFEfficientFormerForImageClassificationWithTeacher,
              ),
          }
          if is_tf_available()
          else {}
      )

      a			:  Optional[int]           =		False

      a			:  Any           =		False
      a			:  Optional[Any]           =		False
      a			:  Union[str, Any]           =		False
      a			:  List[Any]           =		False
      def SCREAMING_SNAKE_CASE_						(		self						:						Any				):






       '''simple docstring'''


       __lowercase						=						TFEfficientFormerModelTester(self				)
       __lowercase						=						ConfigTester(
           self   ,							config_class=A_   ,							has_text_modality=A_   ,							hidden_size=3_7				)
      def SCREAMING_SNAKE_CASE_						(		self						:						str				):






       '''simple docstring'''


       self.config_tester.run_common_tests()
      @unittest.skip(reason="""EfficientFormer does not use inputs_embeds"""				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


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






       '''simple docstring'''


       pass
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       __lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs_for_common()

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

        __lowercase						=						["""pixel_values"""]
        self.assertListEqual(arg_names[:1]   ,							A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       def check_hidden_states_output(A_						:						Any   ,							A_						:						List[Any]   ,							A_						:						Optional[int]				):
        __lowercase						=						model_class(A_				)

        __lowercase						=						model(**self._prepare_for_class(A_   ,							A_				)   ,							training=A_				)
        __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(A_				)   ,							A_				)

        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.asseretIsInstance(A_   ,							(list, tuple)				)
         self.assertEqual(len(A_				)   ,							A_				)
         __lowercase						=						getattr(self.model_tester   ,							"""seq_length"""   ,							A_				)
         __lowercase						=						getattr(self.model_tester   ,							"""decoder_seq_length"""   ,							A_				)

         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(A_   ,							A_   ,							A_				)

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

        check_hidden_states_output(A_   ,							A_   ,							A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]   ,							A_						:						List[str]   ,							A_						:						Union[str, Any]   ,							A_						:						List[Any]=False				):






       '''simple docstring'''


       __lowercase						=						super()._prepare_for_class(A_   ,							A_   ,							return_labels=A_				)

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

       return inputs_dict
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       __lowercase						=						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						:						int				):






       '''simple docstring'''


       __lowercase						=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       __lowercase						=						self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*A_				)
      @slow
      def SCREAMING_SNAKE_CASE_						(		self						:						Any				):






       '''simple docstring'''


       for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowercase						=						TFEfficientFormerModel.from_pretrained(A_				)
        self.assertIsNotNone(A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


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

       __lowercase						=						getattr(self.model_tester   ,							"""seq_length"""   ,							A_				)
       __lowercase						=						getattr(self.model_tester   ,							"""encoder_seq_length"""   ,							A_				)
       __lowercase						=						getattr(self.model_tester   ,							"""key_length"""   ,							A_				)
       __lowercase						=						getattr(self.model_tester   ,							"""chunk_length"""   ,							A_				)

       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(A_				)

        __lowercase						=						model(**self._prepare_for_class(A_   ,							A_				)   ,							training=A_				)
        __lowercase						=						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"]
        __lowercase						=						True
        __lowercase						=						model_class(A_				)
        __lowercase						=						model(**self._prepare_for_class(A_   ,							A_				)   ,							training=A_				)

        __lowercase						=						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 SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       __lowercase				,		__lowercase						=						self.model_tester.prepare_config_and_inputs_for_common()

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






def lowerCAmelCase_   (					):






 """simple docstring"""







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





@require_tf
@require_vision
class  lowerCamelCase__    (		unittest.TestCase     ):
      @cached_property
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       return (
           EfficientFormerImageProcessor.from_pretrained("""snap-research/efficientformer-l1-300"""				)
           if is_vision_available()
           else None
       )
      @slow
      def SCREAMING_SNAKE_CASE_						(		self						:						str				):






       '''simple docstring'''


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






      @slow
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]				):






       '''simple docstring'''


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




"""simple docstring"""

import copy
from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict

from packaging import version

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


if TYPE_CHECKING:
       from ... import PreTrainedTokenizerBase, TensorType

UpperCAmelCase__					=logging.get_logger(__name__)





class  lowerCamelCase__    (		_a     ):
      a			:  Optional[int]           =		"""vision-encoder-decoder"""
      a			:  Dict           =		True
      def __init__(		self						:						Dict   ,							**A_						:						List[Any]				):






       '''simple docstring'''


       super().__init__(**A_				)
       if "encoder" not in kwargs or "decoder" not in kwargs:
        raise ValueError(
            F'''A configuraton of type {self.model_type} cannot be instantiated because '''
            F'''not both `encoder` and `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(A_   ,							**A_				)
       __lowercase						=						AutoConfig.for_model(A_   ,							**A_				)
       __lowercase						=						True
      @classmethod
      def SCREAMING_SNAKE_CASE_						(		cls						:						int   ,							A_						:						PretrainedConfig   ,							A_						:						PretrainedConfig   ,							**A_						:						Any				):






       '''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()   ,							**A_				)






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






       '''simple docstring'''


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






class  lowerCamelCase__    (		_a     ):
      a			:  Dict           =		version.parse("""1.11"""     )
      @property
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]				):






       '''simple docstring'''


       return OrderedDict(
           [
               ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
           ]				)
      @property
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       return 1e-4






      @property
      def SCREAMING_SNAKE_CASE_						(		self						:						Any				):






       '''simple docstring'''


       return OrderedDict({"""last_hidden_state""": {0: """batch""", 1: """encoder_sequence"""}}				)






class  lowerCamelCase__    (		_a     ):
      @property
      def SCREAMING_SNAKE_CASE_						(		self						:						Dict				):






       '''simple docstring'''


       __lowercase						=						OrderedDict()
       __lowercase						=						{0: """batch""", 1: """past_decoder_sequence + sequence"""}
       __lowercase						=						{0: """batch""", 1: """past_decoder_sequence + sequence"""}
       __lowercase						=						{0: """batch""", 1: """encoder_sequence"""}

       return common_inputs






      def SCREAMING_SNAKE_CASE_						(		self						:						str   ,							A_						:						"PreTrainedTokenizerBase"   ,							A_						:						int = -1   ,							A_						:						int = -1   ,							A_						:						bool = False   ,							A_						:						Optional["TensorType"] = None   ,							):






       '''simple docstring'''


       import torch

       __lowercase						=						OrderedDict()

       __lowercase						=						super().generate_dummy_inputs(
           A_   ,							batch_size=A_   ,							seq_length=A_   ,							is_pair=A_   ,							framework=A_				)

       __lowercase				,		__lowercase						=						dummy_input["""input_ids"""].shape
       __lowercase						=						(batch, encoder_sequence, self._config.encoder_hidden_size)
       __lowercase						=						dummy_input.pop("""input_ids"""				)
       __lowercase						=						dummy_input.pop("""attention_mask"""				)
       __lowercase						=						torch.zeros(A_				)

       return common_inputs






class  lowerCamelCase__    (		_a     ):
      @property
      def SCREAMING_SNAKE_CASE_						(		self						:						str				):






       '''simple docstring'''


       pass
      def SCREAMING_SNAKE_CASE_						(		self						:						str   ,							A_						:						PretrainedConfig				):






       '''simple docstring'''


       return VisionEncoderDecoderEncoderOnnxConfig(A_				)






      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple   ,							A_						:						PretrainedConfig   ,							A_						:						PretrainedConfig   ,							A_						:						str = "default"				):






       '''simple docstring'''


       __lowercase						=						encoder_config.hidden_size
       return VisionEncoderDecoderDecoderOnnxConfig(A_   ,							A_				)
