






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       snake_case__ :

       '''simple docstring'''



       def __init__(				self      ,    a__      ,    a__ = 13      ,    a__ = 64      ,    a__ = 2      ,    a__ = 3      ,    a__ = 3      ,    a__ = True      ,    a__ = True      ,    a__ = 1_28      ,    a__=[16, 32, 64, 1_28]      ,    a__ = 7      ,    a__ = 4      ,    a__ = 37      ,    a__ = "gelu"      ,    a__ = 0.1      ,    a__ = 0.1      ,    a__ = 10      ,    a__ = 0.02      ,    a__ = 2      ,    a__ = 1      ,    a__ = 1_28      ,    a__ = [2, 2, 2, 2]      ,    a__ = 2      ,    a__ = 2      ,    )			->   Dict:






            '''simple docstring'''





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



       def        __lowercase    (				self     )			->   int:






            '''simple docstring'''





            __snake_case      :Dict 							=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

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

            __snake_case      :Dict 							=    self.get_config()

            return config, pixel_values, labels



       def        __lowercase    (				self     )			->   Tuple:






            '''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        __lowercase    (				self      ,    a__      ,    a__      ,    a__     )			->   Optional[Any]:






            '''simple docstring'''





            __snake_case      :Union[str, Any] 							=    TFEfficientFormerModel(config=a__     )
            __snake_case      :str 							=    model(a__      ,    training=a__     )
            self.parent.assertEqual(result.last_hidden_state.shape      ,    (self.batch_size, self.seq_length, self.hidden_size)     )



       def        __lowercase    (				self      ,    a__      ,    a__      ,    a__     )			->   Tuple:






            '''simple docstring'''





            __snake_case      :str 							=    self.type_sequence_label_size
            __snake_case      :Optional[int] 							=    TFEfficientFormerForImageClassification(a__     )
            __snake_case      :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
            __snake_case      :Union[str, Any] 							=    1
            __snake_case      :int 							=    TFEfficientFormerForImageClassification(a__     )

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



       def        __lowercase    (				self     )			->   Tuple:






            '''simple docstring'''





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




@require_tf
class       snake_case__ (				lowercase_						,     lowercase_						,     unittest.TestCase):

       '''simple docstring'''
       lowerCamelCase				:					int								=			(
           (
               TFEfficientFormerModel,
               TFEfficientFormerForImageClassificationWithTeacher,
               TFEfficientFormerForImageClassification,
           )
           if is_tf_available()
           else ()
       )
       lowerCamelCase				:					Optional[Any]								=			(
           {
               "feature-extraction": TFEfficientFormerModel,
               "image-classification": (
                   TFEfficientFormerForImageClassification,
                   TFEfficientFormerForImageClassificationWithTeacher,
               ),
           }
           if is_tf_available()
           else {}
       )

       lowerCamelCase				:					Optional[int]								=			False

       lowerCamelCase				:					str								=			False
       lowerCamelCase				:					int								=			False
       lowerCamelCase				:					Union[str, Any]								=			False
       lowerCamelCase				:					List[Any]								=			False



       def        __lowercase    (				self     )			->   Dict:






            '''simple docstring'''





            __snake_case      :int 							=    TFEfficientFormerModelTester(self     )
            __snake_case      :Any 							=    ConfigTester(
                self      ,    config_class=a__      ,    has_text_modality=a__      ,    hidden_size=37     )



       def        __lowercase    (				self     )			->   int:






            '''simple docstring'''





            self.config_tester.run_common_tests()



       @unittest.skip(reason="""EfficientFormer does not use inputs_embeds"""     )
       def        __lowercase    (				self     )			->   List[Any]:






            '''simple docstring'''





            pass



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






            '''simple docstring'''





            pass



       def        __lowercase    (				self     )			->   str:






            '''simple docstring'''





            __snake_case							,			__snake_case      :Tuple 							=    self.model_tester.prepare_config_and_inputs_for_common()

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

                 __snake_case      :List[str] 							=    ["""pixel_values"""]
                 self.assertListEqual(arg_names[:1]      ,    a__     )



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






            '''simple docstring'''





            def check_hidden_states_output(a__      ,    a__      ,    a__     ):
                 __snake_case      :Any 							=    model_class(a__     )

                 __snake_case      :Tuple 							=    model(**self._prepare_for_class(a__      ,    a__     )      ,    training=a__     )
                 __snake_case      :Tuple 							=    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

                      self.asseretIsInstance(a__      ,    (list, tuple)     )
                      self.assertEqual(len(a__     )      ,    a__     )
                      __snake_case      :List[Any] 							=    getattr(self.model_tester      ,    """seq_length"""      ,    a__     )
                      __snake_case      :List[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]      ,    )

            __snake_case							,			__snake_case      :Union[str, Any] 							=    self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 __snake_case      :int 							=    True
                 check_hidden_states_output(a__      ,    a__      ,    a__     )

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

                 check_hidden_states_output(a__      ,    a__      ,    a__     )



       def        __lowercase    (				self      ,    a__      ,    a__      ,    a__=False     )			->   Any:






            '''simple docstring'''





            __snake_case      :Tuple 							=    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        __lowercase    (				self     )			->   Dict:






            '''simple docstring'''





            __snake_case      :Optional[Any] 							=    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        __lowercase    (				self     )			->   List[Any]:






            '''simple docstring'''





            __snake_case      :List[str] 							=    self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_image_modeling(*a__     )



       def        __lowercase    (				self     )			->   Dict:






            '''simple docstring'''





            __snake_case      :str 							=    self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*a__     )



       @slow
       def        __lowercase    (				self     )			->   int:






            '''simple docstring'''





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



       def        __lowercase    (				self     )			->   str:






            '''simple docstring'''





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

            __snake_case      :Dict 							=    getattr(self.model_tester      ,    """seq_length"""      ,    a__     )
            __snake_case      :str 							=    getattr(self.model_tester      ,    """encoder_seq_length"""      ,    a__     )
            __snake_case      :Optional[Any] 							=    getattr(self.model_tester      ,    """key_length"""      ,    a__     )
            __snake_case      :Optional[Any] 							=    getattr(self.model_tester      ,    """chunk_length"""      ,    a__     )

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

            for model_class in self.all_model_classes:
                 __snake_case      :List[str] 							=    True
                 __snake_case      :List[str] 							=    False
                 __snake_case      :Union[str, Any] 							=    True
                 __snake_case      :List[str] 							=    model_class(a__     )

                 __snake_case      :List[Any] 							=    model(**self._prepare_for_class(a__      ,    a__     )      ,    training=a__     )
                 __snake_case      :Tuple 							=    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"]
                 __snake_case      :Optional[Any] 							=    True
                 __snake_case      :int 							=    model_class(a__     )
                 __snake_case      :Optional[Any] 							=    model(**self._prepare_for_class(a__      ,    a__     )      ,    training=a__     )

                 __snake_case      :Dict 							=    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        __lowercase    (				self     )			->   Optional[int]:






            '''simple docstring'''





            __snake_case							,			__snake_case      :int 							=    self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 # Prepare our model
                 __snake_case      :str 							=    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
                 __snake_case      :Union[str, 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
                 }
                 __snake_case      :str 							=    model(a__     )
                 self.assertTrue(outputs_dict is not None     )






def 	UpperCamelCase	(							):




     '''simple docstring'''






     __snake_case      :Optional[int] 							=    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	)
     return image




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

       '''simple docstring'''



       @cached_property
       def        __lowercase    (				self     )			->   Optional[int]:






            '''simple docstring'''





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



       @slow
       def        __lowercase    (				self     )			->   Dict:






            '''simple docstring'''





            __snake_case      :int 							=    TFEfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300"""     )
            __snake_case      :List[Any] 							=    self.default_image_processor
            __snake_case      :List[Any] 							=    prepare_img()
            __snake_case      :str 							=    image_processor(images=a__      ,    return_tensors="""tf"""     )
            # forward pass
            __snake_case      :List[Any] 							=    model(**a__      ,    training=a__     )
            # verify the logits
            __snake_case      :Union[str, Any] 							=    tf.TensorShape((1, 10_00)     )
            self.assertEqual(outputs.logits.shape      ,    a__     )
            __snake_case      :Any 							=    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        __lowercase    (				self     )			->   str:






            '''simple docstring'''





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










def 	UpperCamelCase	(							snake_case__  : list	):




     '''simple docstring'''






     if not grid or not grid[0]:
          raise TypeError("""The grid does not contain the appropriate information"""	)

     for cell_n in range(1						,len(grid[0]	)	):
          grid[0][cell_n] += grid[0][cell_n - 1]
     __snake_case      :str 							=    grid[0]

     for row_n in range(1						,len(snake_case__	)	):
          __snake_case      :Optional[int] 							=    grid[row_n]
          __snake_case      :Optional[Any] 							=    fill_row(snake_case__						,snake_case__	)
          __snake_case      :Dict 							=    grid[row_n]

     return grid[-1][-1]






def 	UpperCamelCase	(							snake_case__  : list						,snake_case__  : list	):




     '''simple docstring'''






     current_row[0] += row_above[0]
     for cell_n in range(1						,len(snake_case__	)	):
          current_row[cell_n] += min(current_row[cell_n - 1]						,row_above[cell_n]	)

     return current_row


if __name__ == "__main__":
     import doctest

     doctest.testmod()


