


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						:						Optional[int]    ,    __a						:						int    ,    __a						:						int = 13    ,    __a						:						int = 64    ,    __a						:						int = 2    ,    __a						:						int = 3    ,    __a						:						int = 3    ,    __a						:						bool = True    ,    __a						:						bool = True    ,    __a						:						int = 128    ,    __a						:						List[Any]=[16, 32, 64, 128]    ,    __a						:						int = 7    ,    __a						:						int = 4    ,    __a						:						int = 37    ,    __a						:						str = "gelu"    ,    __a						:						float = 0.1    ,    __a						:						float = 0.1    ,    __a						:						int = 10    ,    __a						:						float = 0.02    ,    __a						:						int = 2    ,    __a						:						int = 1    ,    __a						:						int = 128    ,    __a						:						List[int] = [2, 2, 2, 2]    ,    __a						:						int = 2    ,    __a						:						int = 2    ,    ):




         '''simple docstring'''







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




   def 							lowerCamelCase_   (   self						:						List[Any]							):




         '''simple docstring'''







         lowerCamelCase__:						str							   = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

         lowerCamelCase__:						Dict							   = self.get_config()

         return config, pixel_values, labels




   def 							lowerCamelCase_   (   self						:						List[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 							lowerCamelCase_   (   self						:						str    ,    __a						:						str    ,    __a						:						Any    ,    __a						:						Tuple							):




         '''simple docstring'''







         lowerCamelCase__:						Dict							   = TFEfficientFormerModel(config=__a							)
         lowerCamelCase__:						List[str]							   = model(__a    ,    training=__a							)
         self.parent.assertEqual(result.last_hidden_state.shape    ,    (self.batch_size, self.seq_length, self.hidden_size)							)




   def 							lowerCamelCase_   (   self						:						List[str]    ,    __a						:						Optional[Any]    ,    __a						:						int    ,    __a						:						List[Any]							):




         '''simple docstring'''







         lowerCamelCase__:						Union[str, Any]							   = self.type_sequence_label_size
         lowerCamelCase__:						Any							   = TFEfficientFormerForImageClassification(__a							)
         lowerCamelCase__:						Optional[Any]							   = model(__a    ,    labels=__a    ,    training=__a							)
         self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.type_sequence_label_size)							)

         # test greyscale images
         lowerCamelCase__:						Union[str, Any]							   = 1
         lowerCamelCase__:						List[Any]							   = TFEfficientFormerForImageClassification(__a							)

         lowerCamelCase__:						int							   = floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
         lowerCamelCase__:						Any							   = model(__a    ,    labels=__a							)
         self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.type_sequence_label_size)							)




   def 							lowerCamelCase_   (   self						:						Dict							):




         '''simple docstring'''







         lowerCamelCase__:						Tuple							   = self.prepare_config_and_inputs()
         lowerCamelCase__   ,    lowerCamelCase__   ,    lowerCamelCase__:						Union[str, Any]							   = config_and_inputs
         lowerCamelCase__:						Tuple							   = {"""pixel_values""": pixel_values}
         return config, inputs_dict



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

   __lowerCamelCase			      =  False

   __lowerCamelCase			      =  False
   __lowerCamelCase			      =  False
   __lowerCamelCase			      =  False
   __lowerCamelCase			      =  False




   def 							lowerCamelCase_   (   self						:						Any							):




         '''simple docstring'''







         lowerCamelCase__:						int							   = TFEfficientFormerModelTester(self							)
         lowerCamelCase__:						int							   = ConfigTester(
             self    ,    config_class=__a    ,    has_text_modality=__a    ,    hidden_size=37							)




   def 							lowerCamelCase_   (   self						:						Tuple							):




         '''simple docstring'''







         self.config_tester.run_common_tests()




   @unittest.skip(reason="""EfficientFormer does not use inputs_embeds"""							)
   def 							lowerCamelCase_   (   self						:						Optional[int]							):




         '''simple docstring'''







         pass




   @unittest.skip(reason="""EfficientFormer does not support input and output embeddings"""							)
   def 							lowerCamelCase_   (   self						:						Optional[int]							):




         '''simple docstring'''







         pass




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




         '''simple docstring'''







         lowerCamelCase__   ,    lowerCamelCase__:						List[Any]							   = self.model_tester.prepare_config_and_inputs_for_common()

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

               lowerCamelCase__:						Dict							   = ["""pixel_values"""]
               self.assertListEqual(arg_names[:1]    ,    __a							)




   def 							lowerCamelCase_   (   self						:						str							):




         '''simple docstring'''







         def check_hidden_states_output(__a						:						Optional[Any]    ,    __a						:						Dict    ,    __a						:						List[Any]							):
               lowerCamelCase__:						Any							   = model_class(__a							)

               lowerCamelCase__:						Optional[int]							   = model(**self._prepare_for_class(__a    ,    __a							)    ,    training=__a							)
               lowerCamelCase__:						List[Any]							   = outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

               lowerCamelCase__:						int							   = 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"""							):
                     lowerCamelCase__:						int							   = self.model_tester.encoder_seq_length
                     if hasattr(self.model_tester    ,    """chunk_length"""							) and self.model_tester.chunk_length > 1:
                           lowerCamelCase__:						List[Any]							   = seq_length * self.model_tester.chunk_length
               else:
                     lowerCamelCase__:						List[Any]							   = 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:
                     lowerCamelCase__:						Union[str, Any]							   = outputs.decoder_hidden_states

                     self.asseretIsInstance(__a    ,    (list, tuple)							)
                     self.assertEqual(len(__a							)    ,    __a							)
                     lowerCamelCase__:						str							   = getattr(self.model_tester    ,    """seq_length"""    ,    __a							)
                     lowerCamelCase__:						Optional[Any]							   = getattr(self.model_tester    ,    """decoder_seq_length"""    ,    __a							)

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

         lowerCamelCase__   ,    lowerCamelCase__:						str							   = self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
               lowerCamelCase__:						Union[str, Any]							   = True
               check_hidden_states_output(__a    ,    __a    ,    __a							)

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

               check_hidden_states_output(__a    ,    __a    ,    __a							)




   def 							lowerCamelCase_   (   self						:						Optional[int]    ,    __a						:						int    ,    __a						:						Optional[Any]    ,    __a						:						Tuple=False							):




         '''simple docstring'''







         lowerCamelCase__:						str							   = 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 							lowerCamelCase_   (   self						:						Tuple							):




         '''simple docstring'''







         lowerCamelCase__:						str							   = 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 							lowerCamelCase_   (   self						:						Any							):




         '''simple docstring'''







         lowerCamelCase__:						Union[str, Any]							   = self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*__a							)




   def 							lowerCamelCase_   (   self						:						List[str]							):




         '''simple docstring'''







         lowerCamelCase__:						Tuple							   = self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__a							)




   @slow
   def 							lowerCamelCase_   (   self						:						str							):




         '''simple docstring'''







         for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowerCamelCase__:						Optional[Any]							   = TFEfficientFormerModel.from_pretrained(__a							)
               self.assertIsNotNone(__a							)




   def 							lowerCamelCase_   (   self						:						Optional[int]							):




         '''simple docstring'''







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

         lowerCamelCase__:						List[str]							   = getattr(self.model_tester    ,    """seq_length"""    ,    __a							)
         lowerCamelCase__:						str							   = getattr(self.model_tester    ,    """encoder_seq_length"""    ,    __a							)
         lowerCamelCase__:						int							   = getattr(self.model_tester    ,    """key_length"""    ,    __a							)
         lowerCamelCase__:						Union[str, Any]							   = getattr(self.model_tester    ,    """chunk_length"""    ,    __a							)

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

         for model_class in self.all_model_classes:
               lowerCamelCase__:						List[Any]							   = True
               lowerCamelCase__:						Dict							   = False
               lowerCamelCase__:						Optional[Any]							   = True
               lowerCamelCase__:						Optional[Any]							   = model_class(__a							)

               lowerCamelCase__:						int							   = model(**self._prepare_for_class(__a    ,    __a							)    ,    training=__a							)
               lowerCamelCase__:						str							   = 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"]
               lowerCamelCase__:						List[str]							   = True
               lowerCamelCase__:						Union[str, Any]							   = model_class(__a							)
               lowerCamelCase__:						List[Any]							   = model(**self._prepare_for_class(__a    ,    __a							)    ,    training=__a							)

               lowerCamelCase__:						int							   = 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 							lowerCamelCase_   (   self						:						Optional[int]							):




         '''simple docstring'''







         lowerCamelCase__   ,    lowerCamelCase__:						List[Any]							   = self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
               # Prepare our model
               lowerCamelCase__:						Dict							   = 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
               lowerCamelCase__:						Optional[Any]							   = {
                   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
               }
               lowerCamelCase__:						Optional[Any]							   = model(__a							)
               self.assertTrue(outputs_dict is not None							)

def 		__lowerCAmelCase   (							)    ->				Dict:






      '''simple docstring'''

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



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




   @cached_property
   def 							lowerCamelCase_   (   self						:						int							):




         '''simple docstring'''







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




   @slow
   def 							lowerCamelCase_   (   self						:						Union[str, Any]							):




         '''simple docstring'''







         lowerCamelCase__:						Optional[Any]							   = TFEfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300"""							)
         lowerCamelCase__:						Optional[Any]							   = self.default_image_processor
         lowerCamelCase__:						Any							   = prepare_img()
         lowerCamelCase__:						int							   = image_processor(images=__a    ,    return_tensors="""tf"""							)
         # forward pass
         lowerCamelCase__:						Optional[int]							   = model(**__a    ,    training=__a							)
         # verify the logits
         lowerCamelCase__:						List[Any]							   = tf.TensorShape((1, 1000)							)
         self.assertEqual(outputs.logits.shape    ,    __a							)
         lowerCamelCase__:						List[Any]							   = tf.constant([-0.0_555, 0.4_825, -0.0_852]							)
         self.assertTrue(np.allclose(outputs.logits[0, :3]    ,    __a    ,    atol=1e-4							)							)




   @slow
   def 							lowerCamelCase_   (   self						:						str							):




         '''simple docstring'''







         lowerCamelCase__:						Any							   = TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
             """snap-research/efficientformer-l1-300"""							)
         lowerCamelCase__:						str							   = self.default_image_processor
         lowerCamelCase__:						Optional[Any]							   = prepare_img()
         lowerCamelCase__:						Any							   = image_processor(images=__a    ,    return_tensors="""tf"""							)
         # forward pass
         lowerCamelCase__:						List[str]							   = model(**__a    ,    training=__a							)
         # verify the logits
         lowerCamelCase__:						Dict							   = tf.TensorShape((1, 1000)							)
         self.assertEqual(outputs.logits.shape    ,    __a							)
         lowerCamelCase__:						Any							   = tf.constant([-0.1_312, 0.4_353, -1.0_499]							)
         self.assertTrue(np.allclose(outputs.logits[0, :3]    ,    __a    ,    atol=1e-4							)							)










from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A				:       List[str]      =  {
    'configuration_time_series_transformer': [
        'TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'TimeSeriesTransformerConfig',
    ],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	A				:       str      =  [
	    'TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
	    'TimeSeriesTransformerForPrediction',
	    'TimeSeriesTransformerModel',
	    'TimeSeriesTransformerPreTrainedModel',
	]


if TYPE_CHECKING:
	from .configuration_time_series_transformer import (
	    TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    TimeSeriesTransformerConfig,
	)

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_time_series_transformer import (
		    TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TimeSeriesTransformerForPrediction,
		    TimeSeriesTransformerModel,
		    TimeSeriesTransformerPreTrainedModel,
		)

else:
	import sys

	A				:       str      =  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





