






'''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 							_UpperCAmelCase    :




    def __init__(      self	:							Union[str, Any]	,  a	:							Tuple	,  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	:							Dict=[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_						:     List[str]							     =  parent
       lowercase_						:     List[Any]							     =  batch_size
       lowercase_						:     Union[str, Any]							     =  image_size
       lowercase_						:     Union[str, Any]							     =  patch_size
       lowercase_						:     Dict							     =  num_channels
       lowercase_						:     Tuple							     =  is_training
       lowercase_						:     List[str]							     =  use_labels
       lowercase_						:     Dict							     =  hidden_size
       lowercase_						:     List[str]							     =  num_hidden_layers
       lowercase_						:     Any							     =  num_attention_heads
       lowercase_						:     List[Any]							     =  intermediate_size
       lowercase_						:     Optional[int]							     =  hidden_act
       lowercase_						:     Dict							     =  hidden_dropout_prob
       lowercase_						:     Optional[Any]							     =  attention_probs_dropout_prob
       lowercase_						:     Optional[int]							     =  type_sequence_label_size
       lowercase_						:     str							     =  initializer_range
       lowercase_						:     Dict							     =  encoder_stride
       lowercase_						:     Any							     =  num_attention_outputs
       lowercase_						:     List[Any]							     =  embed_dim
       lowercase_						:     List[Any]							     =  embed_dim + 1
       lowercase_						:     Optional[int]							     =  resolution
       lowercase_						:     List[str]							     =  depths
       lowercase_						:     List[str]							     =  hidden_sizes
       lowercase_						:     Optional[int]							     =  dim
       lowercase_						:     List[Any]							     =  mlp_expansion_ratio




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






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

       lowercase_						:     Any							     =  None
       if self.use_labels:
          lowercase_						:     int							     =  ids_tensor([self.batch_size]	,  self.type_sequence_label_size      )

       lowercase_						:     int							     =  self.get_config()

       return config, pixel_values, labels




    def 					lowerCAmelCase__					(      self	:							str      ):






       '''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	:							List[Any]	,  a	:							Union[str, Any]	,  a	:							Any      ):






       '''simple docstring'''






       lowercase_						:     str							     =  TFEfficientFormerModel(config=a      )
       lowercase_						:     int							     =  model(a	,  training=a      )
       self.parent.assertEqual(result.last_hidden_state.shape	,  (self.batch_size, self.seq_length, self.hidden_size)      )




    def 					lowerCAmelCase__					(      self	:							Dict	,  a	:							str	,  a	:							Union[str, Any]	,  a	:							int      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  self.type_sequence_label_size
       lowercase_						:     Dict							     =  TFEfficientFormerForImageClassification(a      )
       lowercase_						:     Optional[int]							     =  model(a	,  labels=a	,  training=a      )
       self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)      )

       # test greyscale images
       lowercase_						:     int							     =  1
       lowercase_						:     str							     =  TFEfficientFormerForImageClassification(a      )

       lowercase_						:     Any							     =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
       lowercase_						:     Dict							     =  model(a	,  labels=a      )
       self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)      )





    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       lowercase_						:     str							     =  self.prepare_config_and_inputs()
       lowercase_     ,		lowercase_     ,		lowercase_						:     Any							     =  config_and_inputs
       lowercase_						:     Dict							     =  {"pixel_values": pixel_values}
       return config, inputs_dict


@require_tf
class 							_UpperCAmelCase    (		snake_case					,       snake_case					,       unittest.TestCase      ):
    __lowerCamelCase: Optional[Any]									=     (
        (
            TFEfficientFormerModel,
            TFEfficientFormerForImageClassificationWithTeacher,
            TFEfficientFormerForImageClassification,
        )
        if is_tf_available()
        else ()
    )
    __lowerCamelCase: Tuple									=     (
        {
            'feature-extraction': TFEfficientFormerModel,
            'image-classification': (
                TFEfficientFormerForImageClassification,
                TFEfficientFormerForImageClassificationWithTeacher,
            ),
        }
        if is_tf_available()
        else {}
    )

    __lowerCamelCase: List[Any]									=     False

    __lowerCamelCase: Tuple									=     False
    __lowerCamelCase: int									=     False
    __lowerCamelCase: Optional[Any]									=     False
    __lowerCamelCase: List[str]									=     False




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     int							     =  TFEfficientFormerModelTester(self      )
       lowercase_						:     str							     =  ConfigTester(
           self	,  config_class=a	,  has_text_modality=a	,  hidden_size=3_7      )




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''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	:							Dict      ):






       '''simple docstring'''






       pass




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       lowercase_     ,		lowercase_						:     Optional[Any]							     =  self.model_tester.prepare_config_and_inputs_for_common()

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

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




    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       def check_hidden_states_output(a	:							str	,  a	:							List[Any]	,  a	:							List[str]      ):
          lowercase_						:     int							     =  model_class(a      )

          lowercase_						:     List[Any]							     =  model(**self._prepare_for_class(a	,  a      )	,  training=a      )
          lowercase_						:     Union[str, Any]							     =  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

             self.asseretIsInstance(a	,  (list, tuple)      )
             self.assertEqual(len(a      )	,  a      )
             lowercase_						:     Optional[Any]							     =  getattr(self.model_tester	,  "seq_length"	,  a      )
             lowercase_						:     Dict							     =  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_						:     Union[str, Any]							     =  self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
          lowercase_						:     Optional[int]							     =  True
          check_hidden_states_output(a	,  a	,  a      )

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

          check_hidden_states_output(a	,  a	,  a      )




    def 					lowerCAmelCase__					(      self	:							Tuple	,  a	:							Optional[Any]	,  a	:							Dict	,  a	:							str=False      ):






       '''simple docstring'''






       lowercase_						:     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	:							Any      ):






       '''simple docstring'''






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






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*a      )




    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*a      )




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






       '''simple docstring'''






       for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowercase_						:     Dict							     =  TFEfficientFormerModel.from_pretrained(a      )
          self.assertIsNotNone(a      )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_     ,		lowercase_						:     Dict							     =  self.model_tester.prepare_config_and_inputs_for_common()
       lowercase_						:     Any							     =  True

       lowercase_						:     Tuple							     =  getattr(self.model_tester	,  "seq_length"	,  a      )
       lowercase_						:     int							     =  getattr(self.model_tester	,  "encoder_seq_length"	,  a      )
       lowercase_						:     List[Any]							     =  getattr(self.model_tester	,  "key_length"	,  a      )
       lowercase_						:     Optional[Any]							     =  getattr(self.model_tester	,  "chunk_length"	,  a      )

       if chunk_length is not None and hasattr(self.model_tester	,  "num_hashes"      ):
          lowercase_						:     List[str]							     =  encoder_seq_length * self.model_tester.num_hashes

       for model_class in self.all_model_classes:
          lowercase_						:     str							     =  True
          lowercase_						:     List[Any]							     =  False
          lowercase_						:     Dict							     =  True
          lowercase_						:     List[Any]							     =  model_class(a      )

          lowercase_						:     str							     =  model(**self._prepare_for_class(a	,  a      )	,  training=a      )
          lowercase_						:     Optional[int]							     =  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_						:     Optional[int]							     =  True
          lowercase_						:     int							     =  model_class(a      )
          lowercase_						:     Optional[Any]							     =  model(**self._prepare_for_class(a	,  a      )	,  training=a      )

          lowercase_						:     List[str]							     =  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	:							List[Any]      ):






       '''simple docstring'''






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

       for model_class in self.all_model_classes:
          # Prepare our model
          lowercase_						:     Optional[int]							     =  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_						:     Optional[int]							     =  {
              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_						:     Optional[int]							     =  model(a      )
          self.assertTrue(outputs_dict is not None      )




def 		__SCREAMING_SNAKE_CASE       (	):


   """simple docstring"""




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





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




    @cached_property
    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






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




    @slow
    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       lowercase_						:     Tuple							     =  TFEfficientFormerForImageClassification.from_pretrained("snap-research/efficientformer-l1-300"      )
       lowercase_						:     Dict							     =  self.default_image_processor
       lowercase_						:     Dict							     =  prepare_img()
       lowercase_						:     Optional[int]							     =  image_processor(images=a	,  return_tensors="tf"      )
       # forward pass
       lowercase_						:     Optional[int]							     =  model(**a	,  training=a      )
       # verify the logits
       lowercase_						:     Tuple							     =  tf.TensorShape((1, 1_0_0_0)      )
       self.assertEqual(outputs.logits.shape	,  a      )
       lowercase_						:     Tuple							     =  tf.constant([-0.0555, 0.4825, -0.0852]      )
       self.assertTrue(np.allclose(outputs.logits[0, :3]	,  a	,  atol=1e-4      )      )





    @slow
    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






       lowercase_						:     Dict							     =  TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
           "snap-research/efficientformer-l1-300"      )
       lowercase_						:     Union[str, Any]							     =  self.default_image_processor
       lowercase_						:     Union[str, Any]							     =  prepare_img()
       lowercase_						:     List[Any]							     =  image_processor(images=a	,  return_tensors="tf"      )
       # forward pass
       lowercase_						:     Dict							     =  model(**a	,  training=a      )
       # verify the logits
       lowercase_						:     Dict							     =  tf.TensorShape((1, 1_0_0_0)      )
       self.assertEqual(outputs.logits.shape	,  a      )
       lowercase_						:     Union[str, Any]							     =  tf.constant([-0.1312, 0.4353, -1.0499]      )
       self.assertTrue(np.allclose(outputs.logits[0, :3]	,  a	,  atol=1e-4      )      )












'''simple docstring'''
import flax.linen as nn
import jax
import jax.numpy as jnp


class 							_UpperCAmelCase    (		nn.Module      ):
    __lowerCamelCase: int
    __lowerCamelCase: jnp.dtype									=     jnp.floataa




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  nn.Conv(
           self.out_channels	,  kernel_size=(3, 3)	,  strides=(1, 1)	,  padding=((1, 1), (1, 1))	,  dtype=self.dtype	,  )





    def __call__(      self	:							List[str]	,  a	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_     ,		lowercase_     ,		lowercase_     ,		lowercase_						:     Optional[Any]							     =  hidden_states.shape
       lowercase_						:     Tuple							     =  jax.image.resize(
           a	,  shape=(batch, height * 2, width * 2, channels)	,  method="nearest"	,  )
       lowercase_						:     List[Any]							     =  self.conv(a      )
       return hidden_states


class 							_UpperCAmelCase    (		nn.Module      ):
    __lowerCamelCase: int
    __lowerCamelCase: jnp.dtype									=     jnp.floataa




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     Any							     =  nn.Conv(
           self.out_channels	,  kernel_size=(3, 3)	,  strides=(2, 2)	,  padding=((1, 1), (1, 1))	,  dtype=self.dtype	,  )





    def __call__(      self	:							Any	,  a	:							int      ):






       '''simple docstring'''






       lowercase_						:     Any							     =  self.conv(a      )
       return hidden_states


class 							_UpperCAmelCase    (		nn.Module      ):
    __lowerCamelCase: int
    __lowerCamelCase: int									=     None
    __lowerCamelCase: float									=     0.0
    __lowerCamelCase: bool									=     None
    __lowerCamelCase: jnp.dtype									=     jnp.floataa




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  self.in_channels if self.out_channels is None else self.out_channels

       lowercase_						:     Optional[int]							     =  nn.GroupNorm(num_groups=3_2	,  epsilon=1e-5      )
       lowercase_						:     Tuple							     =  nn.Conv(
           a	,  kernel_size=(3, 3)	,  strides=(1, 1)	,  padding=((1, 1), (1, 1))	,  dtype=self.dtype	,  )

       lowercase_						:     List[str]							     =  nn.Dense(a	,  dtype=self.dtype      )

       lowercase_						:     Optional[int]							     =  nn.GroupNorm(num_groups=3_2	,  epsilon=1e-5      )
       lowercase_						:     Any							     =  nn.Dropout(self.dropout_prob      )
       lowercase_						:     Dict							     =  nn.Conv(
           a	,  kernel_size=(3, 3)	,  strides=(1, 1)	,  padding=((1, 1), (1, 1))	,  dtype=self.dtype	,  )

       lowercase_						:     Tuple							     =  self.in_channels != out_channels if self.use_nin_shortcut is None else self.use_nin_shortcut

       lowercase_						:     Optional[Any]							     =  None
       if use_nin_shortcut:
          lowercase_						:     Union[str, Any]							     =  nn.Conv(
              a	,  kernel_size=(1, 1)	,  strides=(1, 1)	,  padding="VALID"	,  dtype=self.dtype	,  )





    def __call__(      self	:							List[str]	,  a	:							str	,  a	:							Dict	,  a	:							List[str]=True      ):






       '''simple docstring'''






       lowercase_						:     Dict							     =  hidden_states
       lowercase_						:     int							     =  self.norma(a      )
       lowercase_						:     List[Any]							     =  nn.swish(a      )
       lowercase_						:     Dict							     =  self.conva(a      )

       lowercase_						:     Optional[int]							     =  self.time_emb_proj(nn.swish(a      )      )
       lowercase_						:     Tuple							     =  jnp.expand_dims(jnp.expand_dims(a	,  1      )	,  1      )
       lowercase_						:     List[str]							     =  hidden_states + temb

       lowercase_						:     Optional[Any]							     =  self.norma(a      )
       lowercase_						:     Any							     =  nn.swish(a      )
       lowercase_						:     List[str]							     =  self.dropout(a	,  a      )
       lowercase_						:     int							     =  self.conva(a      )

       if self.conv_shortcut is not None:
          lowercase_						:     List[str]							     =  self.conv_shortcut(a      )

       return hidden_states + residual




