



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 	__snake_case :







		'''simple docstring'''






		def __init__(							self					,					a_					,					a_ = 13					,					a_ = 64					,					a_ = 2					,					a_ = 3					,					a_ = 3					,					a_ = True					,					a_ = True					,					a_ = 128					,					a_=[16, 32, 64, 128]					,					a_ = 7					,					a_ = 4					,					a_ = 37					,					a_ = "gelu"					,					a_ = 0.1					,					a_ = 0.1					,					a_ = 10					,					a_ = 0.02					,					a_ = 2					,					a_ = 1					,					a_ = 128					,					a_ = [2, 2, 2, 2]					,					a_ = 2					,					a_ = 2					,					):
						a__			 =						parent
						a__			 =						batch_size
						a__			 =						image_size
						a__			 =						patch_size
						a__			 =						num_channels
						a__			 =						is_training
						a__			 =						use_labels
						a__			 =						hidden_size
						a__			 =						num_hidden_layers
						a__			 =						num_attention_heads
						a__			 =						intermediate_size
						a__			 =						hidden_act
						a__			 =						hidden_dropout_prob
						a__			 =						attention_probs_dropout_prob
						a__			 =						type_sequence_label_size
						a__			 =						initializer_range
						a__			 =						encoder_stride
						a__			 =						num_attention_outputs
						a__			 =						embed_dim
						a__			 =						embed_dim + 1
						a__			 =						resolution
						a__			 =						depths
						a__			 =						hidden_sizes
						a__			 =						dim
						a__			 =						mlp_expansion_ratio


		def _a (							self						):
						a__			 =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

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

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


		def _a (							self						):
						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					,					a_					,					a_					,					a_						):
						a__			 =						EfficientFormerModel(config=_A						)
						model.to(_A						)
						model.eval()
						a__			 =						model(_A						)
						self.parent.assertEqual(result.last_hidden_state.shape					,					(self.batch_size, self.seq_length, self.hidden_size)						)


		def _a (							self					,					a_					,					a_					,					a_						):
						a__			 =						self.type_sequence_label_size
						a__			 =						EfficientFormerForImageClassification(_A						)
						model.to(_A						)
						model.eval()
						a__			 =						model(_A					,					labels=_A						)
						self.parent.assertEqual(result.logits.shape					,					(self.batch_size, self.type_sequence_label_size)						)

						# test greyscale images
						a__			 =						1
						a__			 =						EfficientFormerForImageClassification(_A						)
						model.to(_A						)
						model.eval()

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







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




@require_torch
class 	__snake_case (				A__     ,A__     ,unittest.TestCase):







		'''simple docstring'''






		UpperCamelCase__			: Optional[int]             =   (
		    (
		        EfficientFormerModel,
		        EfficientFormerForImageClassificationWithTeacher,
		        EfficientFormerForImageClassification,
		    )
		    if is_torch_available()
		    else ()
		)
		UpperCamelCase__			: Tuple             =   (
		    {
		        """feature-extraction""": EfficientFormerModel,
		        """image-classification""": (
		            EfficientFormerForImageClassification,
		            EfficientFormerForImageClassificationWithTeacher,
		        ),
		    }
		    if is_torch_available()
		    else {}
		)
		UpperCamelCase__			: Optional[int]             =   False

		UpperCamelCase__			: Any             =   False
		UpperCamelCase__			: Dict             =   False
		UpperCamelCase__			: List[str]             =   False


		def _a (							self						):
						a__			 =						EfficientFormerModelTester(self						)
						a__			 =						ConfigTester(
						    self					,					config_class=_A					,					has_text_modality=_A					,					hidden_size=37						)


		def _a (							self						):
						self.config_tester.run_common_tests()


		@unittest.skip(reason="""EfficientFormer does not use inputs_embeds"""						)
		def _a (							self						):
						pass


		@unittest.skip(reason="""EfficientFormer does not support input and output embeddings"""						)
		def _a (							self						):
						pass


		def _a (							self						):
						a__			 =						self.model_tester.prepare_config_and_inputs_for_common()

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

										a__			 =						["pixel_values"]
										self.assertListEqual(arg_names[:1]					,					_A						)


		def _a (							self						):






						def check_hidden_states_output(a_					,					a_					,					a_						):
										a__			 =						model_class(_A						)
										model.to(_A						)
										model.eval()

										with torch.no_grad():
														a__			 =						model(**self._prepare_for_class(_A					,					_A						)						)

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

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

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

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

						a__			 =						self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										a__			 =						True
										check_hidden_states_output(_A					,					_A					,					_A						)

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

										check_hidden_states_output(_A					,					_A					,					_A						)


		def _a (							self					,					a_					,					a_					,					a_=False						):
						a__			 =						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						):
						a__			 =						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						):
						a__			 =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_image_modeling(*_A						)


		def _a (							self						):
						a__			 =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*_A						)


		def _a (							self						):






						if not self.model_tester.is_training:
										return

						a__			 =						self.model_tester.prepare_config_and_inputs_for_common()
						a__			 =						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
										a__			 =						model_class(_A						)
										model.to(_A						)
										model.train()
										a__			 =						self._prepare_for_class(_A					,					_A					,					return_labels=_A						)
										a__			 =						model(**_A						).loss
										loss.backward()


		def _a (							self						):
						a__			 =						self.model_tester.prepare_config_and_inputs_for_common()

						a__			 =						[
						    {"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"]}'''						):
																		a__			 =						problem_type["title"]
																		a__			 =						problem_type["num_labels"]

																		a__			 =						model_class(_A						)
																		model.to(_A						)
																		model.train()

																		a__			 =						self._prepare_for_class(_A					,					_A					,					return_labels=_A						)

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

																		a__			 =						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:
																						a__			 =						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						):






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







		def _a (							self						):
						a__			 =						self.model_tester.prepare_config_and_inputs_for_common()
						a__			 =						True

						a__			 =						getattr(self.model_tester					,					"""seq_length"""					,					_A						)
						a__			 =						getattr(self.model_tester					,					"""encoder_seq_length"""					,					_A						)
						a__			 =						getattr(self.model_tester					,					"""key_length"""					,					_A						)
						a__			 =						getattr(self.model_tester					,					"""chunk_length"""					,					_A						)

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

						for model_class in self.all_model_classes:
										a__			 =						True
										a__			 =						False
										a__			 =						True
										a__			 =						model_class(_A						)
										model.to(_A						)
										model.eval()
										with torch.no_grad():
														a__			 =						model(**self._prepare_for_class(_A					,					_A						)						)
										a__			 =						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"]
										a__			 =						True
										a__			 =						model_class(_A						)
										model.to(_A						)
										model.eval()
										with torch.no_grad():
														a__			 =						model(**self._prepare_for_class(_A					,					_A						)						)
										a__			 =						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_ (		):

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




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







		'''simple docstring'''






		@cached_property
		def _a (							self						):
						return (
						    EfficientFormerImageProcessor.from_pretrained("""snap-research/efficientformer-l1-300"""						)
						    if is_vision_available()
						    else None
						)


		@slow
		def _a (							self						):
						a__			 =						EfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300"""						).to(
						    _A						)

						a__			 =						self.default_image_processor
						a__			 =						prepare_img()
						a__			 =						image_processor(images=_A					,					return_tensors="""pt"""						).to(_A						)

						# forward pass
						with torch.no_grad():
										a__			 =						model(**_A						)

						# verify the logits
						a__			 =						(1, 1_000)
						self.assertEqual(outputs.logits.shape					,					_A						)

						a__			 =						torch.tensor([-0.0_555, 0.4_825, -0.0_852]						).to(_A						)
						self.assertTrue(torch.allclose(outputs.logits[0][:3]					,					_A					,					atol=1E-4						)						)







		@slow
		def _a (							self						):
						a__			 =						EfficientFormerForImageClassificationWithTeacher.from_pretrained(
						    """snap-research/efficientformer-l1-300"""						).to(_A						)

						a__			 =						self.default_image_processor
						a__			 =						prepare_img()
						a__			 =						image_processor(images=_A					,					return_tensors="""pt"""						).to(_A						)

						# forward pass
						with torch.no_grad():
										a__			 =						model(**_A						)

						# verify the logits
						a__			 =						(1, 1_000)
						self.assertEqual(outputs.logits.shape					,					_A						)

						a__			 =						torch.tensor([-0.1_312, 0.4_353, -1.0_499]						).to(_A						)
						self.assertTrue(torch.allclose(outputs.logits[0][:3]					,					_A					,					atol=1E-4						)						)






def      A_ (		__a       :			float						):

				"""simple docstring"""
				return int(__a						) if x - int(__a						) >= 0 else int(__a						) - 1


if __name__ == "__main__":
	import doctest

	doctest.testmod()

