





import inspect
import unittest
from typing import List

import numpy as np

from transformers import EfficientFormerConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
	import tensorflow as tf

	from transformers import (
	    TFEfficientFormerForImageClassification,
	    TFEfficientFormerForImageClassificationWithTeacher,
	    TFEfficientFormerModel,
	)
	from transformers.models.efficientformer.modeling_tf_efficientformer import (
	    TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
	)


if is_vision_available():
	from PIL import Image

	from transformers import EfficientFormerImageProcessor



class    __lowerCAmelCase			:






			def __init__(self   ,				__magic_name__   ,				__magic_name__ = 13   ,				__magic_name__ = 64   ,				__magic_name__ = 2   ,				__magic_name__ = 3   ,				__magic_name__ = 3   ,				__magic_name__ = True   ,				__magic_name__ = True   ,				__magic_name__ = 128   ,				__magic_name__=[16, 32, 64, 128]   ,				__magic_name__ = 7   ,				__magic_name__ = 4   ,				__magic_name__ = 37   ,				__magic_name__ = "gelu"   ,				__magic_name__ = 0.1   ,				__magic_name__ = 0.1   ,				__magic_name__ = 10   ,				__magic_name__ = 0.02   ,				__magic_name__ = 2   ,				__magic_name__ = 1   ,				__magic_name__ = 128   ,				__magic_name__ = [2, 2, 2, 2]   ,				__magic_name__ = 2   ,				__magic_name__ = 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      lowerCamelCase    (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      lowerCamelCase    (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      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  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      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  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      lowerCamelCase    (self       )	->  Tuple:


							'''simple docstring'''



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




@require_tf
class    __lowerCAmelCase			(     lowercase_,	lowercase_,	unittest.TestCase	):
			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      lowerCamelCase    (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      lowerCamelCase    (self       )	->  int:


							'''simple docstring'''



							self.config_tester.run_common_tests()






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


							'''simple docstring'''



							pass






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


							'''simple docstring'''



							pass






			def      lowerCamelCase    (self       )	->  str:


							'''simple docstring'''



							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      lowerCamelCase    (self       )	->  Optional[Any]:


							'''simple docstring'''



							def check_hidden_states_output(__magic_name__   ,				__magic_name__   ,				__magic_name__       ):
											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_		:       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      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__=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      lowerCamelCase    (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      lowerCamelCase    (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      lowerCamelCase    (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      lowerCamelCase    (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      lowerCamelCase    (self       )	->  str:


							'''simple docstring'''



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


							'''simple docstring'''



							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 	lowerCamelCase_		(							)						->		int:






				"""simple docstring"""


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





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






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


							'''simple docstring'''



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






			@slow
			def      lowerCamelCase    (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, 1000)       )
							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      lowerCamelCase    (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, 1000)       )
							self.assertEqual(outputs.logits.shape   ,				a__       )
							snake_case_		:       Optional[Any]           =    tf.constant([-0.1_312, 0.4_353, -1.0_499]       )
							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()


