

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






						def __init__(    self  ,      _lowercase  ,      _lowercase = 13  ,      _lowercase = 64  ,      _lowercase = 2  ,      _lowercase = 3  ,      _lowercase = 3  ,      _lowercase = True  ,      _lowercase = True  ,      _lowercase = 128  ,      _lowercase=[16, 32, 64, 128]  ,      _lowercase = 7  ,      _lowercase = 4  ,      _lowercase = 37  ,      _lowercase = "gelu"  ,      _lowercase = 0.1  ,      _lowercase = 0.1  ,      _lowercase = 10  ,      _lowercase = 0.0_2  ,      _lowercase = 2  ,      _lowercase = 1  ,      _lowercase = 128  ,      _lowercase = [2, 2, 2, 2]  ,      _lowercase = 2  ,      _lowercase = 2  ,      )     -> Dict:
									a_						:						Dict 							= parent
									a_						:						Optional[int] 							= batch_size
									a_						:						str 							= image_size
									a_						:						Dict 							= patch_size
									a_						:						str 							= num_channels
									a_						:						Optional[int] 							= is_training
									a_						:						Optional[int] 							= use_labels
									a_						:						Tuple 							= hidden_size
									a_						:						Optional[int] 							= num_hidden_layers
									a_						:						List[Any] 							= num_attention_heads
									a_						:						List[Any] 							= intermediate_size
									a_						:						List[Any] 							= hidden_act
									a_						:						Tuple 							= hidden_dropout_prob
									a_						:						Optional[Any] 							= attention_probs_dropout_prob
									a_						:						Any 							= type_sequence_label_size
									a_						:						Tuple 							= initializer_range
									a_						:						Optional[Any] 							= encoder_stride
									a_						:						Tuple 							= num_attention_outputs
									a_						:						List[str] 							= embed_dim
									a_						:						Dict 							= embed_dim + 1
									a_						:						Dict 							= resolution
									a_						:						Tuple 							= depths
									a_						:						Union[str, Any] 							= hidden_sizes
									a_						:						Dict 							= dim
									a_						:						List[str] 							= mlp_expansion_ratio





						def 			UpperCamelCase__					(    self    )     -> Optional[Any]:
									a_						:						List[Any] 							= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

									a_						:						Optional[int] 							= None
									if self.use_labels:
												a_						:						Union[str, Any] 							= ids_tensor([self.batch_size]  ,      self.type_sequence_label_size    )

									a_						:						List[str] 							= self.get_config()

									return config, pixel_values, labels





						def 			UpperCamelCase__					(    self    )     -> Any:
									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=_lowercase  ,      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 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase    )     -> str:
									a_						:						int 							= TFEfficientFormerModel(config=_lowercase    )
									a_						:						int 							= model(_lowercase  ,      training=_lowercase    )
									self.parent.assertEqual(result.last_hidden_state.shape  ,      (self.batch_size, self.seq_length, self.hidden_size)    )





						def 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase    )     -> Optional[int]:
									a_						:						Dict 							= self.type_sequence_label_size
									a_						:						Union[str, Any] 							= TFEfficientFormerForImageClassification(_lowercase    )
									a_						:						List[str] 							= model(_lowercase  ,      labels=_lowercase  ,      training=_lowercase    )
									self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.type_sequence_label_size)    )

									# test greyscale images
									a_						:						Any 							= 1
									a_						:						Optional[Any] 							= TFEfficientFormerForImageClassification(_lowercase    )

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





						def 			UpperCamelCase__					(    self    )     -> Optional[Any]:
									a_						:						Any 							= self.prepare_config_and_inputs()
									a_							,					a_							,					a_						:						Any 							= config_and_inputs
									a_						:						Union[str, Any] 							= {"""pixel_values""": pixel_values}
									return config, inputs_dict







@require_tf
class        A__(a_,  a_,  unittest.TestCase							):
						"""simple docstring"""






						_A    :				int       					=			(
						    (
						        TFEfficientFormerModel,
						        TFEfficientFormerForImageClassificationWithTeacher,
						        TFEfficientFormerForImageClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						_A    :				List[Any]       					=			(
						    {
						        '''feature-extraction''': TFEfficientFormerModel,
						        '''image-classification''': (
						            TFEfficientFormerForImageClassification,
						            TFEfficientFormerForImageClassificationWithTeacher,
						        ),
						    }
						    if is_tf_available()
						    else {}
						)

						_A    :				Optional[Any]       					=			False

						_A    :				Tuple       					=			False
						_A    :				List[str]       					=			False
						_A    :				List[str]       					=			False
						_A    :				Union[str, Any]       					=			False





						def 			UpperCamelCase__					(    self    )     -> int:
									a_						:						List[Any] 							= TFEfficientFormerModelTester(self    )
									a_						:						Dict 							= ConfigTester(
									    self  ,      config_class=_lowercase  ,      has_text_modality=_lowercase  ,      hidden_size=37    )





						def 			UpperCamelCase__					(    self    )     -> Tuple:
									self.config_tester.run_common_tests()





						@unittest.skip(reason="""EfficientFormer does not use inputs_embeds"""    )
						def 			UpperCamelCase__					(    self    )     -> int:
									pass





						@unittest.skip(reason="""EfficientFormer does not support input and output embeddings"""    )
						def 			UpperCamelCase__					(    self    )     -> Union[str, Any]:
									pass





						def 			UpperCamelCase__					(    self    )     -> Any:
									a_							,					a_						:						Optional[int] 							= self.model_tester.prepare_config_and_inputs_for_common()

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

												a_						:						Optional[int] 							= ["""pixel_values"""]
												self.assertListEqual(arg_names[:1]  ,      _lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Dict:
									def check_hidden_states_output(_lowercase  ,      _lowercase  ,      _lowercase    ):
												a_						:						List[str] 							= model_class(_lowercase    )

												a_						:						Optional[Any] 							= model(**self._prepare_for_class(_lowercase  ,      _lowercase    )  ,      training=_lowercase    )
												a_						:						Tuple 							= outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

												a_						:						Any 							= getattr(
												    self.model_tester  ,      """expected_num_hidden_layers"""  ,      self.model_tester.num_hidden_layers + 1    )
												self.assertEqual(len(_lowercase    )  ,      _lowercase    )

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

															self.asseretIsInstance(_lowercase  ,      (list, tuple)    )
															self.assertEqual(len(_lowercase    )  ,      _lowercase    )
															a_						:						List[Any] 							= getattr(self.model_tester  ,      """seq_length"""  ,      _lowercase    )
															a_						:						List[Any] 							= getattr(self.model_tester  ,      """decoder_seq_length"""  ,      _lowercase    )

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

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

									for model_class in self.all_model_classes:
												a_						:						Any 							= True
												check_hidden_states_output(_lowercase  ,      _lowercase  ,      _lowercase    )

												# check that output_hidden_states also work using config
												del inputs_dict["output_hidden_states"]
												a_						:						Union[str, Any] 							= True

												check_hidden_states_output(_lowercase  ,      _lowercase  ,      _lowercase    )





						def 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase=False    )     -> int:
									a_						:						Optional[Any] 							= super()._prepare_for_class(_lowercase  ,      _lowercase  ,      return_labels=_lowercase    )

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

									return inputs_dict





						def 			UpperCamelCase__					(    self    )     -> Union[str, Any]:
									a_						:						Optional[Any] 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowercase    )





						@unittest.skip(reason="""EfficientFormer does not implement masked image modeling yet"""    )
						def 			UpperCamelCase__					(    self    )     -> List[str]:
									a_						:						Union[str, Any] 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									a_						:						str 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_lowercase    )





						@slow
						def 			UpperCamelCase__					(    self    )     -> Dict:
									for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a_						:						Tuple 							= TFEfficientFormerModel.from_pretrained(_lowercase    )
												self.assertIsNotNone(_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_							,					a_						:						Dict 							= self.model_tester.prepare_config_and_inputs_for_common()
									a_						:						Dict 							= True

									a_						:						Dict 							= getattr(self.model_tester  ,      """seq_length"""  ,      _lowercase    )
									a_						:						Any 							= getattr(self.model_tester  ,      """encoder_seq_length"""  ,      _lowercase    )
									a_						:						Optional[int] 							= getattr(self.model_tester  ,      """key_length"""  ,      _lowercase    )
									a_						:						Dict 							= getattr(self.model_tester  ,      """chunk_length"""  ,      _lowercase    )

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

									for model_class in self.all_model_classes:
												a_						:						Optional[int] 							= True
												a_						:						str 							= False
												a_						:						List[Any] 							= True
												a_						:						Union[str, Any] 							= model_class(_lowercase    )

												a_						:						List[str] 							= model(**self._prepare_for_class(_lowercase  ,      _lowercase    )  ,      training=_lowercase    )
												a_						:						str 							= outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
												self.assertEqual(len(_lowercase    )  ,      self.model_tester.num_attention_outputs    )

												# check that output_attentions also work using config
												del inputs_dict["output_attentions"]
												a_						:						int 							= True
												a_						:						Any 							= model_class(_lowercase    )
												a_						:						Dict 							= model(**self._prepare_for_class(_lowercase  ,      _lowercase    )  ,      training=_lowercase    )

												a_						:						Optional[Any] 							= outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
												self.assertEqual(len(_lowercase    )  ,      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 			UpperCamelCase__					(    self    )     -> int:
									# We use a simplified version of this test for EfficientFormer because it requires training=False
									# and Keras refuses to let us force that during functional construction
									a_							,					a_						:						str 							= self.model_tester.prepare_config_and_inputs_for_common()

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







def 					_UpperCAmelCase				(						):

			'''simple docstring'''



			a_						:						List[Any] 							= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
			return image



@require_tf
@require_vision
class        A__(unittest.TestCase							):
						"""simple docstring"""






						@cached_property
						def 			UpperCamelCase__					(    self    )     -> Union[str, Any]:
									return (
									    EfficientFormerImageProcessor.from_pretrained("""snap-research/efficientformer-l1-300"""    )
									    if is_vision_available()
									    else None
									)





						@slow
						def 			UpperCamelCase__					(    self    )     -> int:
									a_						:						int 							= TFEfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300"""    )
									a_						:						List[str] 							= self.default_image_processor
									a_						:						List[str] 							= prepare_img()
									a_						:						List[str] 							= image_processor(images=_lowercase  ,      return_tensors="""tf"""    )
									# forward pass
									a_						:						Dict 							= model(**_lowercase  ,      training=_lowercase    )
									# verify the logits
									a_						:						List[Any] 							= tf.TensorShape((1, 1_000)    )
									self.assertEqual(outputs.logits.shape  ,      _lowercase    )
									a_						:						List[Any] 							= tf.constant([-0.0_5_5_5, 0.4_8_2_5, -0.0_8_5_2]    )
									self.assertTrue(np.allclose(outputs.logits[0, :3]  ,      _lowercase  ,      atol=1e-4    )    )





						@slow
						def 			UpperCamelCase__					(    self    )     -> Dict:
									a_						:						str 							= TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
									    """snap-research/efficientformer-l1-300"""    )
									a_						:						Optional[Any] 							= self.default_image_processor
									a_						:						Optional[int] 							= prepare_img()
									a_						:						int 							= image_processor(images=_lowercase  ,      return_tensors="""tf"""    )
									# forward pass
									a_						:						Union[str, Any] 							= model(**_lowercase  ,      training=_lowercase    )
									# verify the logits
									a_						:						Dict 							= tf.TensorShape((1, 1_000)    )
									self.assertEqual(outputs.logits.shape  ,      _lowercase    )
									a_						:						int 							= tf.constant([-0.1_3_1_2, 0.4_3_5_3, -1.0_4_9_9]    )
									self.assertTrue(np.allclose(outputs.logits[0, :3]  ,      _lowercase  ,      atol=1e-4    )    )





from __future__ import annotations


def 					_UpperCAmelCase				(						a__):

			'''simple docstring'''



			if len(a__) == 0:
						return []
			a_							,					a_						:						List[Any] 							= min(a__), max(a__)
			a_						:						Tuple 							= int(max_value - min_value) + 1
			a_						:						list[list] 							= [[] for _ in range(a__)]

			for i in my_list:
						buckets[int(i - min_value)].append(a__)

			return [v for bucket in buckets for v in sorted(a__)]


if __name__ == "__main__":
	from doctest import testmod

	testmod()
	assert bucket_sort([4, 5, 3, 2, 1]) == [1, 2, 3, 4, 5]
	assert bucket_sort([0, 1, -10, 15, 2, -2]) == [-10, -2, 0, 1, 2, 15]


