
'''simple docstring'''



import inspect
import unittest
import warnings
from typing import List

from transformers import EfficientFormerConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import (
				    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
				    MODEL_MAPPING,
				    EfficientFormerForImageClassification,
				    EfficientFormerForImageClassificationWithTeacher,
				    EfficientFormerModel,
				)
				from transformers.models.efficientformer.modeling_efficientformer import (
				    EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
				)


if is_vision_available():
				from PIL import Image

				from transformers import EfficientFormerImageProcessor





class 			A							:



			def __init__(					self       ,     lowerCamelCase__       ,     lowerCamelCase__ = 13       ,     lowerCamelCase__ = 64       ,     lowerCamelCase__ = 2       ,     lowerCamelCase__ = 3       ,     lowerCamelCase__ = 3       ,     lowerCamelCase__ = True       ,     lowerCamelCase__ = True       ,     lowerCamelCase__ = 128       ,     lowerCamelCase__=[16, 32, 64, 128]       ,     lowerCamelCase__ = 7       ,     lowerCamelCase__ = 4       ,     lowerCamelCase__ = 37       ,     lowerCamelCase__ = "gelu"       ,     lowerCamelCase__ = 0.1       ,     lowerCamelCase__ = 0.1       ,     lowerCamelCase__ = 10       ,     lowerCamelCase__ = 0.02       ,     lowerCamelCase__ = 2       ,     lowerCamelCase__ = 1       ,     lowerCamelCase__ = 128       ,     lowerCamelCase__ = [2, 2, 2, 2]       ,     lowerCamelCase__ = 2       ,     lowerCamelCase__ = 2       ,     )			->		Tuple:




									'''simple docstring'''



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



			def A__ (					self       )			->		Tuple:




									'''simple docstring'''



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

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

									lowercase__							 =       self.get_config()
									return config, pixel_values, labels



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




									'''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 A__ (					self       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       )			->		int:




									'''simple docstring'''



									lowercase__							 =       EfficientFormerModel(config=a_       )
									model.to(a_       )
									model.eval()
									lowercase__							 =       model(a_       )
									self.parent.assertEqual(result.last_hidden_state.shape       ,     (self.batch_size, self.seq_length, self.hidden_size)       )



			def A__ (					self       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       )			->		str:




									'''simple docstring'''



									lowercase__							 =       self.type_sequence_label_size
									lowercase__							 =       EfficientFormerForImageClassification(a_       )
									model.to(a_       )
									model.eval()
									lowercase__							 =       model(a_       ,     labels=a_       )
									self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.type_sequence_label_size)       )

									# test greyscale images
									lowercase__							 =       1
									lowercase__							 =       EfficientFormerForImageClassification(a_       )
									model.to(a_       )
									model.eval()

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



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




									'''simple docstring'''



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





@require_torch
class 			A							(       __UpperCAmelCase		,						__UpperCAmelCase		,						unittest.TestCase				):
			lowerCamelCase							:							List[Any]       		=  (
			    (
			        EfficientFormerModel,
			        EfficientFormerForImageClassificationWithTeacher,
			        EfficientFormerForImageClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase							:							Optional[Any]       		=  (
			    {
			        '''feature-extraction''': EfficientFormerModel,
			        '''image-classification''': (
			            EfficientFormerForImageClassification,
			            EfficientFormerForImageClassificationWithTeacher,
			        ),
			    }
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase							:							List[str]       		=  False

			lowerCamelCase							:							Dict       		=  False
			lowerCamelCase							:							List[str]       		=  False
			lowerCamelCase							:							List[str]       		=  False



			def A__ (					self       )			->		Dict:




									'''simple docstring'''



									lowercase__							 =       EfficientFormerModelTester(self       )
									lowercase__							 =       ConfigTester(
									    self       ,     config_class=a_       ,     has_text_modality=a_       ,     hidden_size=37       )



			def A__ (					self       )			->		List[Any]:




									'''simple docstring'''



									self.config_tester.run_common_tests()



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




									'''simple docstring'''



									pass



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




									'''simple docstring'''



									pass



			def A__ (					self       )			->		Optional[Any]:




									'''simple docstring'''



									lowercase__ ,						lowercase__							 =       self.model_tester.prepare_config_and_inputs_for_common()

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

															lowercase__							 =       ["""pixel_values"""]
															self.assertListEqual(arg_names[:1]       ,     a_       )



			def A__ (					self       )			->		int:




									'''simple docstring'''



									def check_hidden_states_output(lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__       ):
															lowercase__							 =       model_class(a_       )
															model.to(a_       )
															model.eval()

															with torch.no_grad():
																					lowercase__							 =       model(**self._prepare_for_class(a_       ,     a_       )       )

															lowercase__							 =       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

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

															if hasattr(self.model_tester       ,     """encoder_seq_length"""       ):
																					lowercase__							 =       self.model_tester.encoder_seq_length
																					if hasattr(self.model_tester       ,     """chunk_length"""       ) and self.model_tester.chunk_length > 1:
																											lowercase__							 =       seq_length * self.model_tester.chunk_length
															else:
																					lowercase__							 =       self.model_tester.seq_length

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

															if config.is_encoder_decoder:
																					lowercase__							 =       outputs.decoder_hidden_states

																					self.assertIsInstance(a_       ,     (list, tuple)       )
																					self.assertEqual(len(a_       )       ,     a_       )
																					lowercase__							 =       getattr(self.model_tester       ,     """seq_length"""       ,     a_       )
																					lowercase__							 =       getattr(self.model_tester       ,     """decoder_seq_length"""       ,     a_       )

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

									lowercase__ ,						lowercase__							 =       self.model_tester.prepare_config_and_inputs_for_common()

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

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

															check_hidden_states_output(a_       ,     a_       ,     a_       )



			def A__ (					self       ,     lowerCamelCase__       ,     lowerCamelCase__       ,     lowerCamelCase__=False       )			->		Optional[Any]:




									'''simple docstring'''



									lowercase__							 =       super()._prepare_for_class(a_       ,     a_       ,     return_labels=a_       )

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

									return inputs_dict



			def A__ (					self       )			->		int:




									'''simple docstring'''



									lowercase__							 =       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 A__ (					self       )			->		int:




									'''simple docstring'''



									lowercase__							 =       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*a_       )



			def A__ (					self       )			->		int:




									'''simple docstring'''



									lowercase__							 =       self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*a_       )



			def A__ (					self       )			->		str:




									'''simple docstring'''



									if not self.model_tester.is_training:
															return

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

									for model_class in self.all_model_classes:
															# EfficientFormerForImageClassificationWithTeacher supports inference-only
															if (
															    model_class in get_values(a_       )
															    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
															):
																					continue
															lowercase__							 =       model_class(a_       )
															model.to(a_       )
															model.train()
															lowercase__							 =       self._prepare_for_class(a_       ,     a_       ,     return_labels=a_       )
															lowercase__							 =       model(**a_       ).loss
															loss.backward()



			def A__ (					self       )			->		Tuple:




									'''simple docstring'''



									lowercase__ ,						lowercase__							 =       self.model_tester.prepare_config_and_inputs_for_common()

									lowercase__							 =       [
									    {"""title""": """multi_label_classification""", """num_labels""": 2, """dtype""": torch.float},
									    {"""title""": """single_label_classification""", """num_labels""": 1, """dtype""": torch.long},
									    {"""title""": """regression""", """num_labels""": 1, """dtype""": torch.float},
									]

									for model_class in self.all_model_classes:
															if (
															    model_class
															    not in [
															        *get_values(a_       ),
															    ]
															    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
															):
																					continue

															for problem_type in problem_types:
																					with self.subTest(msg=F'''Testing {model_class} with {problem_type['title']}'''       ):
																											lowercase__							 =       problem_type["""title"""]
																											lowercase__							 =       problem_type["""num_labels"""]

																											lowercase__							 =       model_class(a_       )
																											model.to(a_       )
																											model.train()

																											lowercase__							 =       self._prepare_for_class(a_       ,     a_       ,     return_labels=a_       )

																											if problem_type["num_labels"] > 1:
																																	lowercase__							 =       inputs["""labels"""].unsqueeze(1       ).repeat(1       ,     problem_type["""num_labels"""]       )

																											lowercase__							 =       inputs["""labels"""].to(problem_type["""dtype"""]       )

																											# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																											# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																											# they have the same size." which is a symptom something in wrong for the regression problem.
																											# See https://github.com/huggingface/transformers/issues/11780
																											with warnings.catch_warnings(record=a_       ) as warning_list:
																																	lowercase__							 =       model(**a_       ).loss
																											for w in warning_list:
																																	if "Using a target size that is different to the input size" in str(w.message       ):
																																							raise ValueError(
																																							    F'''Something is going wrong in the regression problem: intercepted {w.message}'''       )

																											loss.backward()



			@slow
			def A__ (					self       )			->		Tuple:




									'''simple docstring'''



									for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowercase__							 =       EfficientFormerModel.from_pretrained(a_       )
															self.assertIsNotNone(a_       )



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




									'''simple docstring'''



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

									lowercase__							 =       getattr(self.model_tester       ,     """seq_length"""       ,     a_       )
									lowercase__							 =       getattr(self.model_tester       ,     """encoder_seq_length"""       ,     a_       )
									lowercase__							 =       getattr(self.model_tester       ,     """key_length"""       ,     a_       )
									lowercase__							 =       getattr(self.model_tester       ,     """chunk_length"""       ,     a_       )

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

									for model_class in self.all_model_classes:
															lowercase__							 =       True
															lowercase__							 =       False
															lowercase__							 =       True
															lowercase__							 =       model_class(a_       )
															model.to(a_       )
															model.eval()
															with torch.no_grad():
																					lowercase__							 =       model(**self._prepare_for_class(a_       ,     a_       )       )
															lowercase__							 =       outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
															self.assertEqual(len(a_       )       ,     self.model_tester.num_attention_outputs       )

															# check that output_attentions also work using config
															del inputs_dict["output_attentions"]
															lowercase__							 =       True
															lowercase__							 =       model_class(a_       )
															model.to(a_       )
															model.eval()
															with torch.no_grad():
																					lowercase__							 =       model(**self._prepare_for_class(a_       ,     a_       )       )
															lowercase__							 =       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  _A		(							):
						lowercase__							 =       Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	)
						return image







@require_torch
@require_vision
class 			A							(       unittest.TestCase				):



			@cached_property
			def A__ (					self       )			->		Optional[Any]:




									'''simple docstring'''



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



			@slow
			def A__ (					self       )			->		Tuple:




									'''simple docstring'''



									lowercase__							 =       EfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300"""       ).to(
									    a_       )

									lowercase__							 =       self.default_image_processor
									lowercase__							 =       prepare_img()
									lowercase__							 =       image_processor(images=a_       ,     return_tensors="""pt"""       ).to(a_       )

									# forward pass
									with torch.no_grad():
															lowercase__							 =       model(**a_       )

									# verify the logits
									lowercase__							 =       (1, 1_000)
									self.assertEqual(outputs.logits.shape       ,     a_       )

									lowercase__							 =       torch.tensor([-0.05_55, 0.48_25, -0.08_52]       ).to(a_       )
									self.assertTrue(torch.allclose(outputs.logits[0][:3]       ,     a_       ,     atol=1e-4       )       )



			@slow
			def A__ (					self       )			->		Tuple:




									'''simple docstring'''



									lowercase__							 =       EfficientFormerForImageClassificationWithTeacher.from_pretrained(
									    """snap-research/efficientformer-l1-300"""       ).to(a_       )

									lowercase__							 =       self.default_image_processor
									lowercase__							 =       prepare_img()
									lowercase__							 =       image_processor(images=a_       ,     return_tensors="""pt"""       ).to(a_       )

									# forward pass
									with torch.no_grad():
															lowercase__							 =       model(**a_       )

									# verify the logits
									lowercase__							 =       (1, 1_000)
									self.assertEqual(outputs.logits.shape       ,     a_       )

									lowercase__							 =       torch.tensor([-0.13_12, 0.43_53, -1.04_99]       ).to(a_       )
									self.assertTrue(torch.allclose(outputs.logits[0][:3]       ,     a_       ,     atol=1e-4       )       )

'''simple docstring'''



def  _A		(							lowercase__	):

						# validate:
						if not isinstance(lowercase__		,		lowercase__	) or not all(
						    isinstance(lowercase__		,		lowercase__	) and item >= 0 for item in citations	):
												raise ValueError("""The citations should be a list of non negative integers."""	)

						citations.sort()
						lowercase__							 =       len(lowercase__	)

						for i in range(lowercase__	):
												if citations[len_citations - 1 - i] <= i:
																		return i

						return len_citations


if __name__ == "__main__":
				import doctest

				doctest.testmod()






