






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 					SCREAMING_SNAKE_CASE_  :
       """simple docstring"""


       def __init__(				self	:Union[str, Any],      snake_case	:int,      snake_case	:int = 13,      snake_case	:int = 64,      snake_case	:int = 2,      snake_case	:int = 3,      snake_case	:int = 3,      snake_case	:bool = True,      snake_case	:bool = True,      snake_case	:int = 128,      snake_case	:str=[16, 32, 64, 128],      snake_case	:int = 7,      snake_case	:int = 4,      snake_case	:int = 37,      snake_case	:str = "gelu",      snake_case	:float = 0.1,      snake_case	:float = 0.1,      snake_case	:int = 10,      snake_case	:float = 0.0_2,      snake_case	:int = 2,      snake_case	:int = 1,      snake_case	:int = 128,      snake_case	:List[int] = [2, 2, 2, 2],      snake_case	:int = 2,      snake_case	: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 				UpperCamelCase__						(				self	:int):



          """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 				UpperCamelCase__						(				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=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 				UpperCamelCase__						(				self	:str,      snake_case	:Any,      snake_case	:Tuple,      snake_case	:Union[str, Any]):



          """simple docstring"""

          _lowercase              =TFEfficientFormerModel(config=snake_case)
          _lowercase              =model(snake_case,      training=snake_case)
          self.parent.assertEqual(result.last_hidden_state.shape,      (self.batch_size, self.seq_length, self.hidden_size))





       def 				UpperCamelCase__						(				self	:List[str],      snake_case	:Optional[Any],      snake_case	:List[Any],      snake_case	:Optional[Any]):



          """simple docstring"""

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

          # test greyscale images
          _lowercase              =1
          _lowercase              =TFEfficientFormerForImageClassification(snake_case)

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





       def 				UpperCamelCase__						(				self	:List[Any]):



          """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 					SCREAMING_SNAKE_CASE_  (       _a  ,						_a  ,						unittest.TestCase ):
       """simple docstring"""


       __lowerCAmelCase    :		List[str]					=(
           (
               TFEfficientFormerModel,
               TFEfficientFormerForImageClassificationWithTeacher,
               TFEfficientFormerForImageClassification,
           )
           if is_tf_available()
           else ()
       )
       __lowerCAmelCase    :		Union[str, Any]					=(
           {
               '''feature-extraction''': TFEfficientFormerModel,
               '''image-classification''': (
                   TFEfficientFormerForImageClassification,
                   TFEfficientFormerForImageClassificationWithTeacher,
               ),
           }
           if is_tf_available()
           else {}
       )

       __lowerCAmelCase    :		List[Any]					=False

       __lowerCAmelCase    :		Tuple					=False
       __lowerCAmelCase    :		Optional[int]					=False
       __lowerCAmelCase    :		Union[str, Any]					=False
       __lowerCAmelCase    :		Dict					=False





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



          """simple docstring"""

          _lowercase              =TFEfficientFormerModelTester(self)
          _lowercase              =ConfigTester(
              self,      config_class=snake_case,      has_text_modality=snake_case,      hidden_size=37)





       def 				UpperCamelCase__						(				self	:List[Any]):



          """simple docstring"""

          self.config_tester.run_common_tests()





       @unittest.skip(reason='EfficientFormer does not use inputs_embeds')
       def 				UpperCamelCase__						(				self	:Optional[Any]):



          """simple docstring"""

          pass





       @unittest.skip(reason='EfficientFormer does not support input and output embeddings')
       def 				UpperCamelCase__						(				self	:Tuple):



          """simple docstring"""

          pass





       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          _lowercase       ,       _lowercase              =self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
             _lowercase              =model_class(snake_case)
             _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],      snake_case)





       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          def check_hidden_states_output(snake_case	:Optional[Any],      snake_case	:str,      snake_case	:Union[str, Any]):
             _lowercase              =model_class(snake_case)

             _lowercase              =model(**self._prepare_for_class(snake_case,      snake_case),      training=snake_case)
             _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(snake_case),      snake_case)

             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(snake_case,      (list, tuple))
                self.assertEqual(len(snake_case),      snake_case)
                _lowercase              =getattr(self.model_tester,      'seq_length',      snake_case)
                _lowercase              =getattr(self.model_tester,      'decoder_seq_length',      snake_case)

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

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

             check_hidden_states_output(snake_case,      snake_case,      snake_case)





       def 				UpperCamelCase__						(				self	:int,      snake_case	:int,      snake_case	:List[Any],      snake_case	:Union[str, Any]=False):



          """simple docstring"""

          _lowercase              =super()._prepare_for_class(snake_case,      snake_case,      return_labels=snake_case)

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

          return inputs_dict





       def 				UpperCamelCase__						(				self	:List[Any]):



          """simple docstring"""

          _lowercase              =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case)





       @unittest.skip(reason='EfficientFormer does not implement masked image modeling yet')
       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          _lowercase              =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*snake_case)





       def 				UpperCamelCase__						(				self	:Tuple):



          """simple docstring"""

          _lowercase              =self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*snake_case)





       @slow
       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             _lowercase              =TFEfficientFormerModel.from_pretrained(snake_case)
             self.assertIsNotNone(snake_case)





       def 				UpperCamelCase__						(				self	:int):



          """simple docstring"""

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

          _lowercase              =getattr(self.model_tester,      'seq_length',      snake_case)
          _lowercase              =getattr(self.model_tester,      'encoder_seq_length',      snake_case)
          _lowercase              =getattr(self.model_tester,      'key_length',      snake_case)
          _lowercase              =getattr(self.model_tester,      'chunk_length',      snake_case)

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

             _lowercase              =model(**self._prepare_for_class(snake_case,      snake_case),      training=snake_case)
             _lowercase              =outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
             self.assertEqual(len(snake_case),      self.model_tester.num_attention_outputs)

             # check that output_attentions also work using config
             del inputs_dict["output_attentions"]
             _lowercase              =True
             _lowercase              =model_class(snake_case)
             _lowercase              =model(**self._prepare_for_class(snake_case,      snake_case),      training=snake_case)

             _lowercase              =outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
             self.assertEqual(len(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 				UpperCamelCase__						(				self	:Dict):



          """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(snake_case)
             # 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=snake_case)
                 for key, val in model.input_signature.items()
                 if key in model.dummy_inputs
             }
             _lowercase              =model(snake_case)
             self.assertTrue(outputs_dict is not None)





def        _snake_case   ()					->	List[Any]:
   _lowercase              =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
   return image






@require_tf
@require_vision
class 					SCREAMING_SNAKE_CASE_  (       unittest.TestCase ):
       """simple docstring"""


       @cached_property
       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

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





       @slow
       def 				UpperCamelCase__						(				self	:Tuple):



          """simple docstring"""

          _lowercase              =TFEfficientFormerForImageClassification.from_pretrained('snap-research/efficientformer-l1-300')
          _lowercase              =self.default_image_processor
          _lowercase              =prepare_img()
          _lowercase              =image_processor(images=snake_case,      return_tensors='tf')
          # forward pass
          _lowercase              =model(**snake_case,      training=snake_case)
          # verify the logits
          _lowercase              =tf.TensorShape((1, 1000))
          self.assertEqual(outputs.logits.shape,      snake_case)
          _lowercase              =tf.constant([-0.0_5_5_5, 0.4_8_2_5, -0.0_8_5_2])
          self.assertTrue(np.allclose(outputs.logits[0, :3],      snake_case,      atol=1e-4))





       @slow
       def 				UpperCamelCase__						(				self	:List[Any]):



          """simple docstring"""

          _lowercase              =TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
              'snap-research/efficientformer-l1-300')
          _lowercase              =self.default_image_processor
          _lowercase              =prepare_img()
          _lowercase              =image_processor(images=snake_case,      return_tensors='tf')
          # forward pass
          _lowercase              =model(**snake_case,      training=snake_case)
          # verify the logits
          _lowercase              =tf.TensorShape((1, 1000))
          self.assertEqual(outputs.logits.shape,      snake_case)
          _lowercase              =tf.constant([-0.1_3_1_2, 0.4_3_5_3, -1.0_4_9_9])
          self.assertTrue(np.allclose(outputs.logits[0, :3],      snake_case,      atol=1e-4))










def        _snake_case   (_snake_case				:       list      ,    _snake_case				:       int      ,    _snake_case				:       int = 0      ,    _snake_case				:       int = 0)					->	int:
   _lowercase              =right or len(_snake_case) - 1
   if left > right:
      return -1
   elif list_data[left] == key:
      return left
   elif list_data[right] == key:
      return right
   else:
      return search(_snake_case      ,    _snake_case      ,    left + 1      ,    right - 1)


if __name__ == "__main__":
    import doctest

    doctest.testmod()


