


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]       ,       __magic_name__							:		List[Any]       ,       __magic_name__							:		int = 1_3       ,       __magic_name__							:		int = 6_4       ,       __magic_name__							:		int = 2       ,       __magic_name__							:		int = 3       ,       __magic_name__							:		int = 3       ,       __magic_name__							:		bool = True       ,       __magic_name__							:		bool = True       ,       __magic_name__							:		int = 1_2_8       ,       __magic_name__							:		int=[1_6, 3_2, 6_4, 1_2_8]       ,       __magic_name__							:		int = 7       ,       __magic_name__							:		int = 4       ,       __magic_name__							:		int = 3_7       ,       __magic_name__							:		str = "gelu"       ,       __magic_name__							:		float = 0.1       ,       __magic_name__							:		float = 0.1       ,       __magic_name__							:		int = 1_0       ,       __magic_name__							:		float = 0.02       ,       __magic_name__							:		int = 2       ,       __magic_name__							:		int = 1       ,       __magic_name__							:		int = 1_2_8       ,       __magic_name__							:		List[int] = [2, 2, 2, 2]       ,       __magic_name__							:		int = 2       ,       __magic_name__							:		int = 2       ,       )     ->   str:







						"""simple docstring"""







						UpperCamelCase         =					parent
						UpperCamelCase         =					batch_size
						UpperCamelCase         =					image_size
						UpperCamelCase         =					patch_size
						UpperCamelCase         =					num_channels
						UpperCamelCase         =					is_training
						UpperCamelCase         =					use_labels
						UpperCamelCase         =					hidden_size
						UpperCamelCase         =					num_hidden_layers
						UpperCamelCase         =					num_attention_heads
						UpperCamelCase         =					intermediate_size
						UpperCamelCase         =					hidden_act
						UpperCamelCase         =					hidden_dropout_prob
						UpperCamelCase         =					attention_probs_dropout_prob
						UpperCamelCase         =					type_sequence_label_size
						UpperCamelCase         =					initializer_range
						UpperCamelCase         =					encoder_stride
						UpperCamelCase         =					num_attention_outputs
						UpperCamelCase         =					embed_dim
						UpperCamelCase         =					embed_dim + 1
						UpperCamelCase         =					resolution
						UpperCamelCase         =					depths
						UpperCamelCase         =					hidden_sizes
						UpperCamelCase         =					dim
						UpperCamelCase         =					mlp_expansion_ratio




			def   lowerCamelCase_					(						self							:		List[str]						)     ->   List[str]:







						"""simple docstring"""







						UpperCamelCase         =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

						UpperCamelCase         =					None
						if self.use_labels:
									UpperCamelCase         =					ids_tensor([self.batch_size]       ,       self.type_sequence_label_size						)

						UpperCamelCase         =					self.get_config()

						return config, pixel_values, labels




			def   lowerCamelCase_					(						self							:		Optional[int]						)     ->   Dict:







						"""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=__magic_name__       ,       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							:		Optional[Any]       ,       __magic_name__							:		List[str]       ,       __magic_name__							:		Optional[int]       ,       __magic_name__							:		List[str]						)     ->   List[str]:







						"""simple docstring"""







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




			def   lowerCamelCase_					(						self							:		List[Any]       ,       __magic_name__							:		List[str]       ,       __magic_name__							:		Tuple       ,       __magic_name__							:		str						)     ->   Union[str, Any]:







						"""simple docstring"""







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

						# test greyscale images
						UpperCamelCase         =					1
						UpperCamelCase         =					TFEfficientFormerForImageClassification(__magic_name__						)

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





			def   lowerCamelCase_					(						self							:		str						)     ->   Union[str, Any]:







						"""simple docstring"""







						UpperCamelCase         =					self.prepare_config_and_inputs()
						UpperCamelCase  ,  UpperCamelCase  ,  UpperCamelCase         =					config_and_inputs
						UpperCamelCase         =					{"""pixel_values""": pixel_values}
						return config, inputs_dict




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

			lowercase						= False

			lowercase						= False
			lowercase						= False
			lowercase						= False
			lowercase						= False




			def   lowerCamelCase_					(						self							:		Dict						)     ->   List[str]:







						"""simple docstring"""







						UpperCamelCase         =					TFEfficientFormerModelTester(self						)
						UpperCamelCase         =					ConfigTester(
						    self       ,       config_class=__magic_name__       ,       has_text_modality=__magic_name__       ,       hidden_size=3_7						)




			def   lowerCamelCase_					(						self							:		Union[str, Any]						)     ->   Union[str, Any]:







						"""simple docstring"""







						self.config_tester.run_common_tests()




			@unittest.skip(reason="""EfficientFormer does not use inputs_embeds"""						)
			def   lowerCamelCase_					(						self							:		Tuple						)     ->   str:







						"""simple docstring"""







						pass




			@unittest.skip(reason="""EfficientFormer does not support input and output embeddings"""						)
			def   lowerCamelCase_					(						self							:		Optional[int]						)     ->   List[Any]:







						"""simple docstring"""







						pass




			def   lowerCamelCase_					(						self							:		int						)     ->   str:







						"""simple docstring"""







						UpperCamelCase  ,  UpperCamelCase         =					self.model_tester.prepare_config_and_inputs_for_common()

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

									UpperCamelCase         =					["""pixel_values"""]
									self.assertListEqual(arg_names[:1]       ,       __magic_name__						)




			def   lowerCamelCase_					(						self							:		List[Any]						)     ->   Optional[Any]:







						"""simple docstring"""







						def check_hidden_states_output(__magic_name__							:		Optional[int]       ,       __magic_name__							:		Tuple       ,       __magic_name__							:		List[Any]						):
									UpperCamelCase         =					model_class(__magic_name__						)

									UpperCamelCase         =					model(**self._prepare_for_class(__magic_name__       ,       __magic_name__						)       ,       training=__magic_name__						)
									UpperCamelCase         =					outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

									UpperCamelCase         =					getattr(
									    self.model_tester       ,       """expected_num_hidden_layers"""       ,       self.model_tester.num_hidden_layers + 1						)
									self.assertEqual(len(__magic_name__						)       ,       __magic_name__						)

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

												self.asseretIsInstance(__magic_name__       ,       (list, tuple)						)
												self.assertEqual(len(__magic_name__						)       ,       __magic_name__						)
												UpperCamelCase         =					getattr(self.model_tester       ,       """seq_length"""       ,       __magic_name__						)
												UpperCamelCase         =					getattr(self.model_tester       ,       """decoder_seq_length"""       ,       __magic_name__						)

												self.assertListEqual(
												    list(hidden_states[-1].shape[-2:]						)       ,       [decoder_seq_length, self.model_tester.hidden_size]       ,       )

						UpperCamelCase  ,  UpperCamelCase         =					self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									UpperCamelCase         =					True
									check_hidden_states_output(__magic_name__       ,       __magic_name__       ,       __magic_name__						)

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

									check_hidden_states_output(__magic_name__       ,       __magic_name__       ,       __magic_name__						)




			def   lowerCamelCase_					(						self							:		List[Any]       ,       __magic_name__							:		Dict       ,       __magic_name__							:		Tuple       ,       __magic_name__							:		Dict=False						)     ->   List[str]:







						"""simple docstring"""







						UpperCamelCase         =					super()._prepare_for_class(__magic_name__       ,       __magic_name__       ,       return_labels=__magic_name__						)

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

						return inputs_dict




			def   lowerCamelCase_					(						self							:		int						)     ->   Optional[int]:







						"""simple docstring"""







						UpperCamelCase         =					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__magic_name__						)




			@unittest.skip(reason="""EfficientFormer does not implement masked image modeling yet"""						)
			def   lowerCamelCase_					(						self							:		Union[str, Any]						)     ->   Any:







						"""simple docstring"""







						UpperCamelCase         =					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*__magic_name__						)




			def   lowerCamelCase_					(						self							:		List[Any]						)     ->   Tuple:







						"""simple docstring"""







						UpperCamelCase         =					self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*__magic_name__						)




			@slow
			def   lowerCamelCase_					(						self							:		Tuple						)     ->   Union[str, Any]:







						"""simple docstring"""







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




			def   lowerCamelCase_					(						self							:		Union[str, Any]						)     ->   Dict:







						"""simple docstring"""







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

						UpperCamelCase         =					getattr(self.model_tester       ,       """seq_length"""       ,       __magic_name__						)
						UpperCamelCase         =					getattr(self.model_tester       ,       """encoder_seq_length"""       ,       __magic_name__						)
						UpperCamelCase         =					getattr(self.model_tester       ,       """key_length"""       ,       __magic_name__						)
						UpperCamelCase         =					getattr(self.model_tester       ,       """chunk_length"""       ,       __magic_name__						)

						if chunk_length is not None and hasattr(self.model_tester       ,       """num_hashes"""						):
									UpperCamelCase         =					encoder_seq_length * self.model_tester.num_hashes

						for model_class in self.all_model_classes:
									UpperCamelCase         =					True
									UpperCamelCase         =					False
									UpperCamelCase         =					True
									UpperCamelCase         =					model_class(__magic_name__						)

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

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

									UpperCamelCase         =					outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
									self.assertEqual(len(__magic_name__						)       ,       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]						)     ->   Optional[Any]:







						"""simple docstring"""







						UpperCamelCase  ,  UpperCamelCase         =					self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
									# Prepare our model
									UpperCamelCase         =					model_class(__magic_name__						)
									# These are maximally general inputs for the model, with multiple None dimensions
									# Hopefully this will catch any conditionals that fail for flexible shapes
									UpperCamelCase         =					{
									    key: tf.keras.Input(shape=val.shape[1:]       ,       dtype=val.dtype       ,       name=__magic_name__						)
									    for key, val in model.input_signature.items()
									    if key in model.dummy_inputs
									}
									UpperCamelCase         =					model(__magic_name__						)
									self.assertTrue(outputs_dict is not None						)






def 							_lowercase    (		):





			"""simple docstring"""




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




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




			@cached_property
			def   lowerCamelCase_					(						self							:		List[str]						)     ->   Tuple:







						"""simple docstring"""







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




			@slow
			def   lowerCamelCase_					(						self							:		int						)     ->   Any:







						"""simple docstring"""







						UpperCamelCase         =					TFEfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300"""						)
						UpperCamelCase         =					self.default_image_processor
						UpperCamelCase         =					prepare_img()
						UpperCamelCase         =					image_processor(images=__magic_name__       ,       return_tensors="""tf"""						)
						# forward pass
						UpperCamelCase         =					model(**__magic_name__       ,       training=__magic_name__						)
						# verify the logits
						UpperCamelCase         =					tf.TensorShape((1, 1_0_0_0)						)
						self.assertEqual(outputs.logits.shape       ,       __magic_name__						)
						UpperCamelCase         =					tf.constant([-0.0_555, 0.4_825, -0.0_852]						)
						self.assertTrue(np.allclose(outputs.logits[0, :3]       ,       __magic_name__       ,       atol=1e-4						)						)





			@slow
			def   lowerCamelCase_					(						self							:		Optional[int]						)     ->   int:







						"""simple docstring"""







						UpperCamelCase         =					TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
						    """snap-research/efficientformer-l1-300"""						)
						UpperCamelCase         =					self.default_image_processor
						UpperCamelCase         =					prepare_img()
						UpperCamelCase         =					image_processor(images=__magic_name__       ,       return_tensors="""tf"""						)
						# forward pass
						UpperCamelCase         =					model(**__magic_name__       ,       training=__magic_name__						)
						# verify the logits
						UpperCamelCase         =					tf.TensorShape((1, 1_0_0_0)						)
						self.assertEqual(outputs.logits.shape       ,       __magic_name__						)
						UpperCamelCase         =					tf.constant([-0.1_312, 0.4_353, -1.0_499]						)
						self.assertTrue(np.allclose(outputs.logits[0, :3]       ,       __magic_name__       ,       atol=1e-4						)						)








from ...utils import is_torch_available, is_transformers_available


if is_transformers_available() and is_torch_available():
			from .pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings, VQDiffusionPipeline




