



'''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 			_SCREAMING_SNAKE_CASE				:



			def __init__(     self		:						Union[str, Any]    ,			__UpperCamelCase		:						int    ,			__UpperCamelCase		:						int = 13    ,			__UpperCamelCase		:						int = 64    ,			__UpperCamelCase		:						int = 2    ,			__UpperCamelCase		:						int = 3    ,			__UpperCamelCase		:						int = 3    ,			__UpperCamelCase		:						bool = True    ,			__UpperCamelCase		:						bool = True    ,			__UpperCamelCase		:						int = 128    ,			__UpperCamelCase		:						Tuple=[16, 32, 64, 128]    ,			__UpperCamelCase		:						int = 7    ,			__UpperCamelCase		:						int = 4    ,			__UpperCamelCase		:						int = 37    ,			__UpperCamelCase		:						str = "gelu"    ,			__UpperCamelCase		:						float = 0.1    ,			__UpperCamelCase		:						float = 0.1    ,			__UpperCamelCase		:						int = 10    ,			__UpperCamelCase		:						float = 0.02    ,			__UpperCamelCase		:						int = 2    ,			__UpperCamelCase		:						int = 1    ,			__UpperCamelCase		:						int = 128    ,			__UpperCamelCase		:						List[int] = [2, 2, 2, 2]    ,			__UpperCamelCase		:						int = 2    ,			__UpperCamelCase		:						int = 2    ,			)      ->      Optional[int]:
							"""simple docstring"""





							snake_case__      :   Any      =    parent
							snake_case__      :   Any      =    batch_size
							snake_case__      :   Any      =    image_size
							snake_case__      :   int      =    patch_size
							snake_case__      :   Any      =    num_channels
							snake_case__      :   Optional[int]      =    is_training
							snake_case__      :   int      =    use_labels
							snake_case__      :   Any      =    hidden_size
							snake_case__      :   List[str]      =    num_hidden_layers
							snake_case__      :   List[Any]      =    num_attention_heads
							snake_case__      :   Tuple      =    intermediate_size
							snake_case__      :   Union[str, Any]      =    hidden_act
							snake_case__      :   Dict      =    hidden_dropout_prob
							snake_case__      :   Any      =    attention_probs_dropout_prob
							snake_case__      :   str      =    type_sequence_label_size
							snake_case__      :   Union[str, Any]      =    initializer_range
							snake_case__      :   Optional[int]      =    encoder_stride
							snake_case__      :   str      =    num_attention_outputs
							snake_case__      :   List[Any]      =    embed_dim
							snake_case__      :   int      =    embed_dim + 1
							snake_case__      :   Union[str, Any]      =    resolution
							snake_case__      :   Union[str, Any]      =    depths
							snake_case__      :   Optional[int]      =    hidden_sizes
							snake_case__      :   List[Any]      =    dim
							snake_case__      :   Tuple      =    mlp_expansion_ratio



			def 				lowerCAmelCase (     self		:						Optional[int]			)      ->      Any:
							"""simple docstring"""





							snake_case__      :   Tuple      =    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__      :   List[str]      =    ids_tensor([self.batch_size]    ,			self.type_sequence_label_size			)

							snake_case__      :   Any      =    self.get_config()
							return config, pixel_values, labels



			def 				lowerCAmelCase (     self		:						str			)      ->      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=__UpperCamelCase    ,			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		:						Any    ,			__UpperCamelCase		:						List[str]    ,			__UpperCamelCase		:						Tuple    ,			__UpperCamelCase		:						Union[str, Any]			)      ->      Tuple:
							"""simple docstring"""





							snake_case__      :   Dict      =    EfficientFormerModel(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							snake_case__      :   Any      =    model(__UpperCamelCase			)
							self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size)			)



			def 				lowerCAmelCase (     self		:						int    ,			__UpperCamelCase		:						Optional[int]    ,			__UpperCamelCase		:						Tuple    ,			__UpperCamelCase		:						Dict			)      ->      str:
							"""simple docstring"""





							snake_case__      :   Union[str, Any]      =    self.type_sequence_label_size
							snake_case__      :   int      =    EfficientFormerForImageClassification(__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							snake_case__      :   Optional[Any]      =    model(__UpperCamelCase    ,			labels=__UpperCamelCase			)
							self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.type_sequence_label_size)			)

							# test greyscale images
							snake_case__      :   Dict      =    1
							snake_case__      :   List[Any]      =    EfficientFormerForImageClassification(__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()

							snake_case__      :   int      =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]			)
							snake_case__      :   List[str]      =    model(__UpperCamelCase			)
							self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.type_sequence_label_size)			)







			def 				lowerCAmelCase (     self		:						List[str]			)      ->      Optional[int]:
							"""simple docstring"""





							snake_case__      :   int      =    self.prepare_config_and_inputs()
							(
							    (
							    snake_case__
							)  ,      (
							    snake_case__
							)  ,      (
							    snake_case__
							)  ,      
							)      :   int      =    config_and_inputs
							snake_case__      :   List[Any]      =    {'''pixel_values''': pixel_values}
							return config, inputs_dict






@require_torch
class 			_SCREAMING_SNAKE_CASE				(lowercase__,	lowercase__,	unittest.TestCase      ):
			A__								=  (
			    (
			        EfficientFormerModel,
			        EfficientFormerForImageClassificationWithTeacher,
			        EfficientFormerForImageClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			A__								=  (
			    {
			        'feature-extraction': EfficientFormerModel,
			        'image-classification': (
			            EfficientFormerForImageClassification,
			            EfficientFormerForImageClassificationWithTeacher,
			        ),
			    }
			    if is_torch_available()
			    else {}
			)
			A__								=  False

			A__								=  False
			A__								=  False
			A__								=  False



			def 				lowerCAmelCase (     self		:						Optional[int]			)      ->      int:
							"""simple docstring"""





							snake_case__      :   Optional[int]      =    EfficientFormerModelTester(self			)
							snake_case__      :   Union[str, Any]      =    ConfigTester(
							    self    ,			config_class=__UpperCamelCase    ,			has_text_modality=__UpperCamelCase    ,			hidden_size=37			)



			def 				lowerCAmelCase (     self		:						Union[str, Any]			)      ->      Dict:
							"""simple docstring"""





							self.config_tester.run_common_tests()



			@unittest.skip(reason='''EfficientFormer does not use inputs_embeds'''			)
			def 				lowerCAmelCase (     self		:						List[str]			)      ->      str:
							"""simple docstring"""





							pass



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





							pass



			def 				lowerCAmelCase (     self		:						int			)      ->      Any:
							"""simple docstring"""





							snake_case__  ,      snake_case__      :   Any      =    self.model_tester.prepare_config_and_inputs_for_common()

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

											snake_case__      :   Union[str, Any]      =    ['''pixel_values''']
											self.assertListEqual(arg_names[:1]    ,			__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						List[Any]			)      ->      Any:
							"""simple docstring"""





							def check_hidden_states_output(__UpperCamelCase		:						List[Any]    ,			__UpperCamelCase		:						Union[str, Any]    ,			__UpperCamelCase		:						List[str]			):
											snake_case__      :   str      =    model_class(__UpperCamelCase			)
											model.to(__UpperCamelCase			)
											model.eval()

											with torch.no_grad():
															snake_case__      :   List[Any]      =    model(**self._prepare_for_class(__UpperCamelCase    ,			__UpperCamelCase			)			)

											snake_case__      :   Dict      =    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

											snake_case__      :   List[str]      =    getattr(
											    self.model_tester    ,			'''expected_num_hidden_layers'''    ,			self.model_tester.num_hidden_layers + 1			)
											self.assertEqual(len(__UpperCamelCase			)    ,			__UpperCamelCase			)

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

															self.assertIsInstance(__UpperCamelCase    ,			(list, tuple)			)
															self.assertEqual(len(__UpperCamelCase			)    ,			__UpperCamelCase			)
															snake_case__      :   Any      =    getattr(self.model_tester    ,			'''seq_length'''    ,			__UpperCamelCase			)
															snake_case__      :   Optional[Any]      =    getattr(self.model_tester    ,			'''decoder_seq_length'''    ,			__UpperCamelCase			)

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

							snake_case__  ,      snake_case__      :   List[Any]      =    self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
											snake_case__      :   Dict      =    True
											check_hidden_states_output(__UpperCamelCase    ,			__UpperCamelCase    ,			__UpperCamelCase			)

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											snake_case__      :   List[Any]      =    True

											check_hidden_states_output(__UpperCamelCase    ,			__UpperCamelCase    ,			__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						List[str]    ,			__UpperCamelCase		:						Any    ,			__UpperCamelCase		:						int    ,			__UpperCamelCase		:						str=False			)      ->      int:
							"""simple docstring"""





							snake_case__      :   int      =    super()._prepare_for_class(__UpperCamelCase    ,			__UpperCamelCase    ,			return_labels=__UpperCamelCase			)

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

							return inputs_dict



			def 				lowerCAmelCase (     self		:						Dict			)      ->      Any:
							"""simple docstring"""





							snake_case__      :   Optional[int]      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__UpperCamelCase			)



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





							snake_case__      :   Union[str, Any]      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						Any			)      ->      int:
							"""simple docstring"""





							snake_case__      :   str      =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase			)



			def 				lowerCAmelCase (     self		:						Dict			)      ->      Union[str, Any]:
							"""simple docstring"""





							if not self.model_tester.is_training:
											return

							snake_case__  ,      snake_case__      :   Dict      =    self.model_tester.prepare_config_and_inputs_for_common()
							snake_case__      :   Optional[int]      =    True

							for model_class in self.all_model_classes:
											# EfficientFormerForImageClassificationWithTeacher supports inference-only
											if (
											    model_class in get_values(__UpperCamelCase			)
											    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
											):
															continue
											snake_case__      :   Tuple      =    model_class(__UpperCamelCase			)
											model.to(__UpperCamelCase			)
											model.train()
											snake_case__      :   Dict      =    self._prepare_for_class(__UpperCamelCase    ,			__UpperCamelCase    ,			return_labels=__UpperCamelCase			)
											snake_case__      :   List[str]      =    model(**__UpperCamelCase			).loss
											loss.backward()



			def 				lowerCAmelCase (     self		:						Any			)      ->      str:
							"""simple docstring"""





							snake_case__  ,      snake_case__      :   Tuple      =    self.model_tester.prepare_config_and_inputs_for_common()

							snake_case__      :   Dict      =    [
							    {'''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(__UpperCamelCase			),
											    ]
											    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
											):
															continue

											for problem_type in problem_types:
															with self.subTest(msg=F'''Testing {model_class} with {problem_type["title"]}'''			):
																			snake_case__      :   Optional[int]      =    problem_type['''title''']
																			snake_case__      :   Union[str, Any]      =    problem_type['''num_labels''']

																			snake_case__      :   List[Any]      =    model_class(__UpperCamelCase			)
																			model.to(__UpperCamelCase			)
																			model.train()

																			snake_case__      :   Tuple      =    self._prepare_for_class(__UpperCamelCase    ,			__UpperCamelCase    ,			return_labels=__UpperCamelCase			)

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

																			snake_case__      :   Dict      =    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=__UpperCamelCase			) as warning_list:
																							snake_case__      :   Optional[Any]      =    model(**__UpperCamelCase			).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 				lowerCAmelCase (     self		:						List[Any]			)      ->      Any:
							"""simple docstring"""





							for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											snake_case__      :   int      =    EfficientFormerModel.from_pretrained(__UpperCamelCase			)
											self.assertIsNotNone(__UpperCamelCase			)







			def 				lowerCAmelCase (     self		:						str			)      ->      List[str]:
							"""simple docstring"""





							snake_case__  ,      snake_case__      :   List[str]      =    self.model_tester.prepare_config_and_inputs_for_common()
							snake_case__      :   int      =    True

							snake_case__      :   str      =    getattr(self.model_tester    ,			'''seq_length'''    ,			__UpperCamelCase			)
							snake_case__      :   Tuple      =    getattr(self.model_tester    ,			'''encoder_seq_length'''    ,			__UpperCamelCase			)
							snake_case__      :   Optional[int]      =    getattr(self.model_tester    ,			'''key_length'''    ,			__UpperCamelCase			)
							snake_case__      :   int      =    getattr(self.model_tester    ,			'''chunk_length'''    ,			__UpperCamelCase			)

							if chunk_length is not None and hasattr(self.model_tester    ,			'''num_hashes'''			):
											snake_case__      :   Tuple      =    encoder_seq_length * self.model_tester.num_hashes

							for model_class in self.all_model_classes:
											snake_case__      :   Any      =    True
											snake_case__      :   int      =    False
											snake_case__      :   int      =    True
											snake_case__      :   List[Any]      =    model_class(__UpperCamelCase			)
											model.to(__UpperCamelCase			)
											model.eval()
											with torch.no_grad():
															snake_case__      :   Union[str, Any]      =    model(**self._prepare_for_class(__UpperCamelCase    ,			__UpperCamelCase			)			)
											snake_case__      :   Optional[int]      =    outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
											self.assertEqual(len(__UpperCamelCase			)    ,			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__      :   List[str]      =    model_class(__UpperCamelCase			)
											model.to(__UpperCamelCase			)
											model.eval()
											with torch.no_grad():
															snake_case__      :   Tuple      =    model(**self._prepare_for_class(__UpperCamelCase    ,			__UpperCamelCase			)			)
											snake_case__      :   List[Any]      =    outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
											self.assertEqual(len(__UpperCamelCase			)    ,			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				(				)		->						str:
				snake_case__      :   Any      =    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
				return image






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



			@cached_property
			def 				lowerCAmelCase (     self		:						Optional[Any]			)      ->      List[Any]:
							"""simple docstring"""





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



			@slow
			def 				lowerCAmelCase (     self		:						Optional[int]			)      ->      Union[str, Any]:
							"""simple docstring"""





							snake_case__      :   str      =    EfficientFormerForImageClassification.from_pretrained('''snap-research/efficientformer-l1-300'''			).to(
							    __UpperCamelCase			)

							snake_case__      :   Union[str, Any]      =    self.default_image_processor
							snake_case__      :   str      =    prepare_img()
							snake_case__      :   str      =    image_processor(images=__UpperCamelCase    ,			return_tensors='''pt'''			).to(__UpperCamelCase			)

							# forward pass
							with torch.no_grad():
											snake_case__      :   List[Any]      =    model(**__UpperCamelCase			)

							# verify the logits
							snake_case__      :   Tuple      =    (1, 1000)
							self.assertEqual(outputs.logits.shape    ,			__UpperCamelCase			)

							snake_case__      :   Any      =    torch.tensor([-0.0555, 0.4825, -0.0852]			).to(__UpperCamelCase			)
							self.assertTrue(torch.allclose(outputs.logits[0][:3]    ,			__UpperCamelCase    ,			atol=1e-4			)			)







			@slow
			def 				lowerCAmelCase (     self		:						Tuple			)      ->      int:
							"""simple docstring"""





							snake_case__      :   Union[str, Any]      =    EfficientFormerForImageClassificationWithTeacher.from_pretrained(
							    '''snap-research/efficientformer-l1-300'''			).to(__UpperCamelCase			)

							snake_case__      :   Tuple      =    self.default_image_processor
							snake_case__      :   int      =    prepare_img()
							snake_case__      :   List[Any]      =    image_processor(images=__UpperCamelCase    ,			return_tensors='''pt'''			).to(__UpperCamelCase			)

							# forward pass
							with torch.no_grad():
											snake_case__      :   str      =    model(**__UpperCamelCase			)

							# verify the logits
							snake_case__      :   int      =    (1, 1000)
							self.assertEqual(outputs.logits.shape    ,			__UpperCamelCase			)

							snake_case__      :   List[Any]      =    torch.tensor([-0.1312, 0.4353, -1.0499]			).to(__UpperCamelCase			)
							self.assertTrue(torch.allclose(outputs.logits[0][:3]    ,			__UpperCamelCase    ,			atol=1e-4			)			)







"""simple docstring"""





from __future__ import annotations



def 							_lowerCAmelCase   (    UpperCAmelCase  :				list[int]   ,				UpperCAmelCase  :				int					):


					'''simple docstring'''






					UpperCamelCase__		:     dict[int, int]								={}
					for index, val in enumerate(UpperCAmelCase					):
										UpperCamelCase__		:     List[Any]								=target - val
										if compl in chk_map:
															return [chk_map[compl], index]
										UpperCamelCase__		:     Any								=index
					return []


if __name__ == "__main__":
				import doctest

				doctest.testmod()
				print(F'''{two_sum([2, 7, 1_1, 1_5], 9) = }''')


