






'''simple docstring'''






import logging
import math
from functools import partial
from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union

import torch

from .tensor_utils import tensor_tree_map, tree_map




def        _SCREAMING_SNAKE_CASE  (				A  :       str  )    -> List[str]:







   """simple docstring"""
   __snake_case				:      Dict     							= []
   if isinstance(SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  ):
      for v in tree.values():
         shapes.extend(_fetch_dims(SCREAMING_SNAKE_CASE_  )  )
   elif isinstance(SCREAMING_SNAKE_CASE_  ,		(list, tuple)  ):
      for t in tree:
         shapes.extend(_fetch_dims(SCREAMING_SNAKE_CASE_  )  )
   elif isinstance(SCREAMING_SNAKE_CASE_  ,		torch.Tensor  ):
      shapes.append(tree.shape  )
   else:
      raise ValueError('Not supported'  )

   return shapes




@torch.jit.ignore
def        _SCREAMING_SNAKE_CASE  (				A  :       Optional[int]  ,		A  :       List[Any]  )    -> Any:







   """simple docstring"""
   __snake_case				:      Optional[Any]     							= []
   for d in reversed(SCREAMING_SNAKE_CASE_  ):
      idx.append(flat_idx % d  )
      __snake_case				:      List[Any]     							= flat_idx // d

   return tuple(reversed(SCREAMING_SNAKE_CASE_  )  )




@torch.jit.ignore
def        _SCREAMING_SNAKE_CASE  (				A  :       Dict  ,		A  :       Union[str, Any]  ,		A  :       Optional[Any]  ,		A  :       int = None  ,		A  :       Optional[int] = None  ,		)    -> int:







   """simple docstring"""

   # start_edges and end_edges both indicate whether, starting from any given
   # dimension, the start/end index is at the top/bottom edge of the
   # corresponding tensor, modeled as a tree
   def reduce_edge_list(A  :       Optional[int]  ) -> None:
      __snake_case				:      Any     							= True
      for i in range(len(SCREAMING_SNAKE_CASE_  )  ):
         __snake_case				:      Optional[int]     							= -1 * (i + 1)
         l[reversed_idx] &= tally
         __snake_case				:      List[Any]     							= l[reversed_idx]

   if start_edges is None:
      __snake_case				:      Optional[Any]     							= [s == 0 for s in start]
      reduce_edge_list(SCREAMING_SNAKE_CASE_  )
   if end_edges is None:
      __snake_case				:      Optional[int]     							= [e == (d - 1) for e, d in zip(SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  )]
      reduce_edge_list(SCREAMING_SNAKE_CASE_  )

   # Base cases. Either start/end are empty and we're done, or the final,
   # one-dimensional tensor can be simply sliced
   if len(SCREAMING_SNAKE_CASE_  ) == 0:
      return [()]
   elif len(SCREAMING_SNAKE_CASE_  ) == 1:
      return [(slice(start[0]  ,		end[0] + 1  ),)]

   __snake_case				:      Optional[int]     							= []
   __snake_case				:      List[str]     							= []

   # Dimensions common to start and end can be selected directly
   for s, e in zip(SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  ):
      if s == e:
         path_list.append(slice(SCREAMING_SNAKE_CASE_  ,		s + 1  )  )
      else:
         break

   __snake_case				:      Optional[int]     							= tuple(SCREAMING_SNAKE_CASE_  )
   __snake_case				:      Dict     							= len(SCREAMING_SNAKE_CASE_  )

   # start == end, and we're done
   if divergence_idx == len(SCREAMING_SNAKE_CASE_  ):
      return [path]

   def upper() -> Tuple[Tuple[slice, ...], ...]:
      assert start_edges is not None
      assert end_edges is not None

      __snake_case				:      List[Any]     							= start[divergence_idx]
      return tuple(
          path + (slice(SCREAMING_SNAKE_CASE_  ,		sdi + 1  ),) + s
          for s in _get_minimal_slice_set(
              start[divergence_idx + 1 :]  ,		[d - 1 for d in dims[divergence_idx + 1 :]]  ,		dims[divergence_idx + 1 :]  ,		start_edges=start_edges[divergence_idx + 1 :]  ,		end_edges=[True for _ in end_edges[divergence_idx + 1 :]]  ,		)  )

   def lower() -> Tuple[Tuple[slice, ...], ...]:
      assert start_edges is not None
      assert end_edges is not None

      __snake_case				:      Tuple     							= end[divergence_idx]
      return tuple(
          path + (slice(SCREAMING_SNAKE_CASE_  ,		edi + 1  ),) + s
          for s in _get_minimal_slice_set(
              [0 for _ in start[divergence_idx + 1 :]]  ,		end[divergence_idx + 1 :]  ,		dims[divergence_idx + 1 :]  ,		start_edges=[True for _ in start_edges[divergence_idx + 1 :]]  ,		end_edges=end_edges[divergence_idx + 1 :]  ,		)  )

   # If both start and end are at the edges of the subtree rooted at
   # divergence_idx, we can just select the whole subtree at once
   if start_edges[divergence_idx] and end_edges[divergence_idx]:
      slices.append(path + (slice(start[divergence_idx]  ,		end[divergence_idx] + 1  ),)  )
   # If just start is at the edge, we can grab almost all of the subtree,
   # treating only the ragged bottom edge as an edge case
   elif start_edges[divergence_idx]:
      slices.append(path + (slice(start[divergence_idx]  ,		end[divergence_idx]  ),)  )
      slices.extend(lower()  )
   # Analogous to the previous case, but the top is ragged this time
   elif end_edges[divergence_idx]:
      slices.extend(upper()  )
      slices.append(path + (slice(start[divergence_idx] + 1  ,		end[divergence_idx] + 1  ),)  )
   # If both sides of the range are ragged, we need to handle both sides
   # separately. If there's contiguous meat in between them, we can index it
   # in one big chunk
   else:
      slices.extend(upper()  )
      __snake_case				:      Optional[Any]     							= end[divergence_idx] - start[divergence_idx]
      if middle_ground > 1:
         slices.append(path + (slice(start[divergence_idx] + 1  ,		end[divergence_idx]  ),)  )
      slices.extend(lower()  )

   return slices




@torch.jit.ignore
def        _SCREAMING_SNAKE_CASE  (				A  :       Dict  ,		A  :       Tuple  ,		A  :       Tuple  ,		A  :       int  )    -> Any:







   """simple docstring"""
   __snake_case				:      str     							= t.shape[:no_batch_dims]
   __snake_case				:      str     							= list(_flat_idx_to_idx(SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  )  )
   # _get_minimal_slice_set is inclusive
   __snake_case				:      List[Any]     							= list(_flat_idx_to_idx(flat_end - 1  ,		SCREAMING_SNAKE_CASE_  )  )

   # Get an ordered list of slices to perform
   __snake_case				:      List[str]     							= _get_minimal_slice_set(
       SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  ,		)

   __snake_case				:      Optional[int]     							= [t[s] for s in slices]

   return torch.cat([s.view((-1,) + t.shape[no_batch_dims:]  ) for s in sliced_tensors]  )




def        _SCREAMING_SNAKE_CASE  (				A  :       Tuple  ,		A  :       Any  ,		A  :       Union[str, Any]  ,		A  :       List[str]  ,		A  :       Dict = False  ,		A  :       int = None  ,		A  :       Optional[Any] = False  ,		)    -> str:







   """simple docstring"""
   if not (len(SCREAMING_SNAKE_CASE_  ) > 0):
      raise ValueError('Must provide at least one input'  )

   __snake_case				:      List[Any]     							= [shape[:no_batch_dims] for shape in _fetch_dims(SCREAMING_SNAKE_CASE_  )]
   __snake_case				:      Any     							= tuple([max(SCREAMING_SNAKE_CASE_  ) for s in zip(*SCREAMING_SNAKE_CASE_  )]  )

   def _prep_inputs(A  :       Union[str, Any]  ) -> torch.Tensor:
      if not low_mem:
         if not sum(t.shape[:no_batch_dims]  ) == no_batch_dims:
            __snake_case				:      List[Any]     							= t.expand(orig_batch_dims + t.shape[no_batch_dims:]  )
         __snake_case				:      Any     							= t.reshape(-1  ,		*t.shape[no_batch_dims:]  )
      else:
         __snake_case				:      int     							= t.expand(orig_batch_dims + t.shape[no_batch_dims:]  )
      return t

   __snake_case				:      Union[str, Any]     							= tensor_tree_map(_prep_inputs  ,		SCREAMING_SNAKE_CASE_  )
   __snake_case				:      Tuple     							= None
   if _out is not None:
      __snake_case				:      Union[str, Any]     							= tensor_tree_map(lambda A  : t.view([-1] + list(t.shape[no_batch_dims:]  )  )  ,		_out  )

   __snake_case				:      Dict     							= 1
   for d in orig_batch_dims:
      flat_batch_dim *= d

   __snake_case				:      Union[str, Any]     							= flat_batch_dim // chunk_size + (flat_batch_dim % chunk_size != 0)

   def _select_chunk(A  :       List[str]  ) -> torch.Tensor:
      return t[i : i + chunk_size] if t.shape[0] != 1 else t

   __snake_case				:      Any     							= 0
   __snake_case				:      List[str]     							= prepped_outputs
   for _ in range(SCREAMING_SNAKE_CASE_  ):
      # Chunk the input
      if not low_mem:
         __snake_case				:      Union[str, Any]     							= _select_chunk
      else:
         __snake_case				:      Union[str, Any]     							= partial(
             _chunk_slice  ,		flat_start=SCREAMING_SNAKE_CASE_  ,		flat_end=min(SCREAMING_SNAKE_CASE_  ,		i + chunk_size  )  ,		no_batch_dims=len(SCREAMING_SNAKE_CASE_  )  ,		)

      __snake_case				:      List[Any]     							= tensor_tree_map(SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  )

      # Run the layer on the chunk
      __snake_case				:      Tuple     							= layer(**SCREAMING_SNAKE_CASE_  )

      # Allocate space for the output
      if out is None:
         __snake_case				:      Optional[int]     							= tensor_tree_map(lambda A  : t.new_zeros((flat_batch_dim,) + t.shape[1:]  )  ,		SCREAMING_SNAKE_CASE_  )

      # Put the chunk in its pre-allocated space
      if isinstance(SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  ):

         def assign(A  :       int  ,		A  :       int  ) -> None:
            for k, v in da.items():
               if isinstance(SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  ):
                  assign(SCREAMING_SNAKE_CASE_  ,		da[k]  )
               else:
                  if _add_into_out:
                     v[i : i + chunk_size] += da[k]
                  else:
                     __snake_case				:      List[str]     							= da[k]

         assign(SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  )
      elif isinstance(SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  ):
         for xa, xa in zip(SCREAMING_SNAKE_CASE_  ,		SCREAMING_SNAKE_CASE_  ):
            if _add_into_out:
               xa[i : i + chunk_size] += xa
            else:
               __snake_case				:      Union[str, Any]     							= xa
      elif isinstance(SCREAMING_SNAKE_CASE_  ,		torch.Tensor  ):
         if _add_into_out:
            out[i : i + chunk_size] += output_chunk
         else:
            __snake_case				:      Tuple     							= output_chunk
      else:
         raise ValueError('Not supported'  )

      i += chunk_size

   __snake_case				:      str     							= tensor_tree_map(lambda A  : t.view(orig_batch_dims + t.shape[1:]  )  ,		SCREAMING_SNAKE_CASE_  )

   return out





class a_    :







   def __init__(self					,   __a = 5_1_2					,   )			->      List[str]:
      """simple docstring"""






      __snake_case				:      int     							= max_chunk_size
      __snake_case				:      Union[str, Any]     							= None
      __snake_case				:      Dict     							= None







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a					,   __a)			->      Optional[int]:
      """simple docstring"""






      logging.info('Tuning chunk size...')

      if min_chunk_size >= self.max_chunk_size:
         return min_chunk_size

      __snake_case				:      Union[str, Any]     							= [2**l for l in range(int(math.log(self.max_chunk_size					,   2)) + 1)]
      __snake_case				:      Optional[Any]     							= [c for c in candidates if c > min_chunk_size]
      __snake_case				:      Optional[int]     							= [min_chunk_size] + candidates
      candidates[-1] += 4

      def test_chunk_size(__a) -> bool:
         try:
            with torch.no_grad():
               fn(*__lowercase					,   chunk_size=__lowercase)
            return True
         except RuntimeError:
            return False

      __snake_case				:      Tuple     							= 0
      __snake_case				:      int     							= len(__lowercase) - 1
      while i > min_viable_chunk_size_index:
         __snake_case				:      Dict     							= test_chunk_size(candidates[i])
         if not viable:
            __snake_case				:      Optional[Any]     							= (min_viable_chunk_size_index + i) // 2
         else:
            __snake_case				:      int     							= i
            __snake_case				:      Optional[Any]     							= (i + len(__lowercase) - 1) // 2

      return candidates[min_viable_chunk_size_index]







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a)			->      int:
      """simple docstring"""






      __snake_case				:      List[Any]     							= True
      for aa, aa in zip(__lowercase					,   __lowercase):
         assert type(__lowercase) == type(__lowercase)
         if isinstance(__lowercase					,   (list, tuple)):
            consistent &= self._compare_arg_caches(__lowercase					,   __lowercase)
         elif isinstance(__lowercase					,   __lowercase):
            __snake_case				:      Any     							= [v for _, v in sorted(aa.items()					,   key=lambda __a: x[0])]
            __snake_case				:      Optional[Any]     							= [v for _, v in sorted(aa.items()					,   key=lambda __a: x[0])]
            consistent &= self._compare_arg_caches(__lowercase					,   __lowercase)
         else:
            consistent &= aa == aa

      return consistent







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a					,   __a					,   )			->      int:
      """simple docstring"""






      __snake_case				:      List[str]     							= True
      __snake_case				:      Dict     							= tree_map(lambda __a: a.shape if isinstance(__lowercase					,   torch.Tensor) else a					,   __lowercase					,   __lowercase)
      if self.cached_arg_data is not None:
         # If args have changed shape/value, we need to re-tune
         assert len(self.cached_arg_data) == len(__lowercase)
         __snake_case				:      List[Any]     							= self._compare_arg_caches(self.cached_arg_data					,   __lowercase)
      else:
         # Otherwise, we can reuse the precomputed value
         __snake_case				:      Optional[Any]     							= False

      if not consistent:
         __snake_case				:      Dict     							= self._determine_favorable_chunk_size(
             __lowercase					,   __lowercase					,   __lowercase					,   )
         __snake_case				:      int     							= arg_data

      assert self.cached_chunk_size is not None

      return self.cached_chunk_size







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







   def __init__(self					,   __a					,   __a = 1_3					,   __a = 6_4					,   __a = 2					,   __a = 3					,   __a = 3					,   __a = True					,   __a = True					,   __a = 1_2_8					,   __a=[1_6, 3_2, 6_4, 1_2_8]					,   __a = 7					,   __a = 4					,   __a = 3_7					,   __a = "gelu"					,   __a = 0.1					,   __a = 0.1					,   __a = 1_0					,   __a = 0.02					,   __a = 2					,   __a = 1					,   __a = 1_2_8					,   __a = [2, 2, 2, 2]					,   __a = 2					,   __a = 2					,   )			->      str:
      """simple docstring"""






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







   def     SCREAMING_SNAKE_CASE__					(self)			->      List[Any]:
      """simple docstring"""






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

      __snake_case				:      List[str]     							= None
      if self.use_labels:
         __snake_case				:      Optional[int]     							= ids_tensor([self.batch_size]					,   self.type_sequence_label_size)

      __snake_case				:      Tuple     							= self.get_config()

      return config, pixel_values, labels







   def     SCREAMING_SNAKE_CASE__					(self)			->      List[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     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a					,   __a)			->      Optional[int]:
      """simple docstring"""






      __snake_case				:      Union[str, Any]     							= TFEfficientFormerModel(config=__a)
      __snake_case				:      int     							= 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					,   __a					,   __a					,   __a)			->      Tuple:
      """simple docstring"""






      __snake_case				:      Dict     							= self.type_sequence_label_size
      __snake_case				:      List[Any]     							= TFEfficientFormerForImageClassification(__a)
      __snake_case				:      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
      __snake_case				:      List[Any]     							= 1
      __snake_case				:      List[Any]     							= TFEfficientFormerForImageClassification(__a)

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







   def     SCREAMING_SNAKE_CASE__					(self)			->      Any:
      """simple docstring"""






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




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

   _snake_case        =							False

   _snake_case        =							False
   _snake_case        =							False
   _snake_case        =							False
   _snake_case        =							False







   def     SCREAMING_SNAKE_CASE__					(self)			->      Dict:
      """simple docstring"""






      __snake_case				:      Dict     							= TFEfficientFormerModelTester(self)
      __snake_case				:      List[Any]     							= 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)			->      Union[str, Any]:
      """simple docstring"""






      pass







   @unittest.skip(reason='EfficientFormer does not support input and output embeddings')
   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[Any]:
      """simple docstring"""






      pass







   def     SCREAMING_SNAKE_CASE__					(self)			->      Any:
      """simple docstring"""






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

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

         __snake_case				:      Dict     							= ['pixel_values']
         self.assertListEqual(arg_names[:1]					,   __a)







   def     SCREAMING_SNAKE_CASE__					(self)			->      Optional[int]:
      """simple docstring"""






      def check_hidden_states_output(__a					,   __a					,   __a):
         __snake_case				:      str     							= model_class(__a)

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

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

            self.asseretIsInstance(__a					,   (list, tuple))
            self.assertEqual(len(__a)					,   __a)
            __snake_case				:      List[str]     							= getattr(self.model_tester					,   'seq_length'					,   __a)
            __snake_case				:      Tuple     							= 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				:      Optional[int]     							= self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         __snake_case				:      List[str]     							= 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				:      str     							= True

         check_hidden_states_output(__a					,   __a					,   __a)







   def     SCREAMING_SNAKE_CASE__					(self					,   __a					,   __a					,   __a=False)			->      int:
      """simple docstring"""






      __snake_case				:      Optional[int]     							= 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)			->      Union[str, Any]:
      """simple docstring"""






      __snake_case				:      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     SCREAMING_SNAKE_CASE__					(self)			->      Tuple:
      """simple docstring"""






      __snake_case				:      int     							= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*__a)







   def     SCREAMING_SNAKE_CASE__					(self)			->      int:
      """simple docstring"""






      __snake_case				:      Any     							= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__a)







   @slow
   def     SCREAMING_SNAKE_CASE__					(self)			->      List[str]:
      """simple docstring"""






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







   def     SCREAMING_SNAKE_CASE__					(self)			->      Tuple:
      """simple docstring"""






      __snake_case							,__snake_case				:      Optional[int]     							= self.model_tester.prepare_config_and_inputs_for_common()
      __snake_case				:      Tuple     							= True

      __snake_case				:      Optional[Any]     							= getattr(self.model_tester					,   'seq_length'					,   __a)
      __snake_case				:      List[Any]     							= getattr(self.model_tester					,   'encoder_seq_length'					,   __a)
      __snake_case				:      Tuple     							= 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				:      str     							= encoder_seq_length * self.model_tester.num_hashes

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

         __snake_case				:      Tuple     							= model(**self._prepare_for_class(__a					,   __a)					,   training=__a)
         __snake_case				:      Union[str, Any]     							= 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				:      Dict     							= True
         __snake_case				:      str     							= model_class(__a)
         __snake_case				:      str     							= 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)

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






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

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




def        _SCREAMING_SNAKE_CASE  (				)    -> int:







   """simple docstring"""
   __snake_case				:      Tuple     							= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
   return image





@require_tf
@require_vision
class a_    (    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)			->      Union[str, Any]:
      """simple docstring"""






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






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