




"""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 							lowercase   :







				def __init__(				self :       str   ,  _lowerCamelCase :       int   ,  _lowerCamelCase :       int = 13   ,  _lowerCamelCase :       Dict = 64   ,  _lowerCamelCase :       Union[str, Any] = 2   ,  _lowerCamelCase :       List[Any] = 3   ,  _lowerCamelCase :       Any = 3   ,  _lowerCamelCase :       Optional[int] = True   ,  _lowerCamelCase :       Tuple = True   ,  _lowerCamelCase :       Optional[int] = 1_28   ,  _lowerCamelCase :       List[Any]=[16, 32, 64, 1_28]   ,  _lowerCamelCase :       int = 7   ,  _lowerCamelCase :       Dict = 4   ,  _lowerCamelCase :       Optional[int] = 37   ,  _lowerCamelCase :       Union[str, Any] = "gelu"   ,  _lowerCamelCase :       Any = 0.1   ,  _lowerCamelCase :       str = 0.1   ,  _lowerCamelCase :       Optional[Any] = 10   ,  _lowerCamelCase :       List[Any] = 0.02   ,  _lowerCamelCase :       Tuple = 2   ,  _lowerCamelCase :       Dict = 1   ,  _lowerCamelCase :       Union[str, Any] = 1_28   ,  _lowerCamelCase :       int = [2, 2, 2, 2]   ,  _lowerCamelCase :       List[Any] = 2   ,  _lowerCamelCase :       Optional[int] = 2   ,  ):



					"""simple docstring"""

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







				def        a_	(				self :       Optional[Any]							):



					"""simple docstring"""

					A_	:					List[Any]						 =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

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

					A_	:					List[Any]						 =		self.get_config()
					return config, pixel_values, labels







				def        a_	(				self :       Tuple							):



					"""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 :       str   ,  _lowerCamelCase :       Any   ,  _lowerCamelCase :       Any   ,  _lowerCamelCase :       Dict							):



					"""simple docstring"""

					A_	:					List[Any]						 =		EfficientFormerModel(config=__A							)
					model.to(__A							)
					model.eval()
					A_	:					List[str]						 =		model(__A							)
					self.parent.assertEqual(result.last_hidden_state.shape   ,  (self.batch_size, self.seq_length, self.hidden_size)							)







				def        a_	(				self :       str   ,  _lowerCamelCase :       str   ,  _lowerCamelCase :       Any   ,  _lowerCamelCase :       str							):



					"""simple docstring"""

					A_	:					int						 =		self.type_sequence_label_size
					A_	:					int						 =		EfficientFormerForImageClassification(__A							)
					model.to(__A							)
					model.eval()
					A_	:					str						 =		model(__A   ,  labels=__A							)
					self.parent.assertEqual(result.logits.shape   ,  (self.batch_size, self.type_sequence_label_size)							)

					# test greyscale images
					A_	:					List[str]						 =		1
					A_	:					List[str]						 =		EfficientFormerForImageClassification(__A							)
					model.to(__A							)
					model.eval()

					A_	:					Any						 =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
					A_	:					Optional[Any]						 =		model(__A							)
					self.parent.assertEqual(result.logits.shape   ,  (self.batch_size, self.type_sequence_label_size)							)







				def        a_	(				self :       int							):



					"""simple docstring"""

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

@require_torch
class 							lowercase   (     _SCREAMING_SNAKE_CASE  ,						_SCREAMING_SNAKE_CASE  ,						unittest.TestCase):
				__lowerCAmelCase     :     Optional[Any]								= (
				    (
				        EfficientFormerModel,
				        EfficientFormerForImageClassificationWithTeacher,
				        EfficientFormerForImageClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				__lowerCAmelCase     :     Any								= (
				    {
				        """feature-extraction""": EfficientFormerModel,
				        """image-classification""": (
				            EfficientFormerForImageClassification,
				            EfficientFormerForImageClassificationWithTeacher,
				        ),
				    }
				    if is_torch_available()
				    else {}
				)
				__lowerCAmelCase     :     Optional[Any]								= False

				__lowerCAmelCase     :     Any								= False
				__lowerCAmelCase     :     Optional[int]								= False
				__lowerCAmelCase     :     Union[str, Any]								= False







				def        a_	(				self :       Tuple							):



					"""simple docstring"""

					A_	:					Tuple						 =		EfficientFormerModelTester(self							)
					A_	:					Tuple						 =		ConfigTester(
					    self   ,  config_class=__A   ,  has_text_modality=__A   ,  hidden_size=37							)







				def        a_	(				self :       int							):



					"""simple docstring"""

					self.config_tester.run_common_tests()







				@unittest.skip(reason='''EfficientFormer does not use inputs_embeds'''							)
				def        a_	(				self :       List[Any]							):



					"""simple docstring"""

					pass







				@unittest.skip(reason='''EfficientFormer does not support input and output embeddings'''							)
				def        a_	(				self :       int							):



					"""simple docstring"""

					pass







				def        a_	(				self :       Optional[Any]							):



					"""simple docstring"""

					A_ ,			A_	:					str						 =		self.model_tester.prepare_config_and_inputs_for_common()

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

						A_	:					List[Any]						 =		['''pixel_values''']
						self.assertListEqual(arg_names[:1]   ,  __A							)







				def        a_	(				self :       Optional[Any]							):



					"""simple docstring"""

					def check_hidden_states_output(_lowerCamelCase :       Any   ,  _lowerCamelCase :       Any   ,  _lowerCamelCase :       List[str]							):
						A_	:					Dict						 =		model_class(__A							)
						model.to(__A							)
						model.eval()

						with torch.no_grad():
							A_	:					int						 =		model(**self._prepare_for_class(__A   ,  __A							)							)

						A_	:					Union[str, Any]						 =		outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

						A_	:					Union[str, Any]						 =		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_	:					List[Any]						 =		self.model_tester.encoder_seq_length
							if hasattr(self.model_tester   ,  '''chunk_length'''							) and self.model_tester.chunk_length > 1:
								A_	:					Dict						 =		seq_length * self.model_tester.chunk_length
						else:
							A_	:					Tuple						 =		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.assertIsInstance(__A   ,  (list, tuple)							)
							self.assertEqual(len(__A							)   ,  __A							)
							A_	:					Dict						 =		getattr(self.model_tester   ,  '''seq_length'''   ,  __A							)
							A_	:					Optional[Any]						 =		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_ ,			A_	:					Union[str, Any]						 =		self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						A_	:					Optional[Any]						 =		True
						check_hidden_states_output(__A   ,  __A   ,  __A							)

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

						check_hidden_states_output(__A   ,  __A   ,  __A							)







				def        a_	(				self :       Optional[int]   ,  _lowerCamelCase :       Optional[int]   ,  _lowerCamelCase :       Any   ,  _lowerCamelCase :       str=False							):



					"""simple docstring"""

					A_	:					List[str]						 =		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 :       Any							):



					"""simple docstring"""

					A_	:					List[Any]						 =		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 :       Tuple							):



					"""simple docstring"""

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







				def        a_	(				self :       str							):



					"""simple docstring"""

					A_	:					str						 =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*__A							)







				def        a_	(				self :       Dict							):



					"""simple docstring"""

					if not self.model_tester.is_training:
						return

					A_ ,			A_	:					Any						 =		self.model_tester.prepare_config_and_inputs_for_common()
					A_	:					Tuple						 =		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_	:					Any						 =		model_class(__A							)
						model.to(__A							)
						model.train()
						A_	:					Optional[int]						 =		self._prepare_for_class(__A   ,  __A   ,  return_labels=__A							)
						A_	:					Union[str, Any]						 =		model(**__A							).loss
						loss.backward()







				def        a_	(				self :       int							):



					"""simple docstring"""

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

					A_	:					int						 =		[
					    {'''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_	:					Optional[int]						 =		problem_type['''title''']
								A_	:					List[Any]						 =		problem_type['''num_labels''']

								A_	:					Optional[int]						 =		model_class(__A							)
								model.to(__A							)
								model.train()

								A_	:					str						 =		self._prepare_for_class(__A   ,  __A   ,  return_labels=__A							)

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

								A_	:					str						 =		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_	:					int						 =		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 :       Optional[int]							):



					"""simple docstring"""

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







				def        a_	(				self :       int							):



					"""simple docstring"""

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

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

					if chunk_length is not None and hasattr(self.model_tester   ,  '''num_hashes'''							):
						A_	:					List[Any]						 =		encoder_seq_length * self.model_tester.num_hashes

					for model_class in self.all_model_classes:
						A_	:					Union[str, Any]						 =		True
						A_	:					str						 =		False
						A_	:					List[Any]						 =		True
						A_	:					Optional[int]						 =		model_class(__A							)
						model.to(__A							)
						model.eval()
						with torch.no_grad():
							A_	:					List[str]						 =		model(**self._prepare_for_class(__A   ,  __A							)							)
						A_	:					List[Any]						 =		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_	:					Any						 =		True
						A_	:					Tuple						 =		model_class(__A							)
						model.to(__A							)
						model.eval()
						with torch.no_grad():
							A_	:					List[str]						 =		model(**self._prepare_for_class(__A   ,  __A							)							)
						A_	:					Dict						 =		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 		lowercase_   (		):




	"""simple docstring"""






	A_	:					List[str]						 =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
	return image

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







				@cached_property
				def        a_	(				self :       Union[str, Any]							):



					"""simple docstring"""

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







				@slow
				def        a_	(				self :       List[Any]							):



					"""simple docstring"""

					A_	:					int						 =		EfficientFormerForImageClassification.from_pretrained('''snap-research/efficientformer-l1-300'''							).to(
					    __A							)

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

					# forward pass
					with torch.no_grad():
						A_	:					int						 =		model(**__A							)

					# verify the logits
					A_	:					List[str]						 =		(1, 10_00)
					self.assertEqual(outputs.logits.shape   ,  __A							)

					A_	:					Union[str, Any]						 =		torch.tensor([-0.0555, 0.4825, -0.0852]							).to(__A							)
					self.assertTrue(torch.allclose(outputs.logits[0][:3]   ,  __A   ,  atol=1E-4							)							)







				@slow
				def        a_	(				self :       Union[str, Any]							):



					"""simple docstring"""

					A_	:					Dict						 =		EfficientFormerForImageClassificationWithTeacher.from_pretrained(
					    '''snap-research/efficientformer-l1-300'''							).to(__A							)

					A_	:					List[str]						 =		self.default_image_processor
					A_	:					str						 =		prepare_img()
					A_	:					Optional[int]						 =		image_processor(images=__A   ,  return_tensors='''pt'''							).to(__A							)

					# forward pass
					with torch.no_grad():
						A_	:					Dict						 =		model(**__A							)

					# verify the logits
					A_	:					str						 =		(1, 10_00)
					self.assertEqual(outputs.logits.shape   ,  __A							)

					A_	:					Any						 =		torch.tensor([-0.1312, 0.4353, -1.0499]							).to(__A							)
					self.assertTrue(torch.allclose(outputs.logits[0][:3]   ,  __A   ,  atol=1E-4							)							)









"""simple docstring"""







import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import (
    TaBlock,
    TaConfig,
    TaLayerNorm,
)

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin






class  __A							(			_SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE     ):






 """simple docstring"""




 @register_to_config
 def __init__(						self					,       __A					,       __A					,       __A					,       __A					,       __A					,       __A					,       __A					,       __A					,       __A					,       __A = False					,       )   ->	Optional[Any]:
     super().__init__()

     a											=nn.Linear(__A					,       __A					,       bias=__A		)

     a											=nn.Embedding(__A					,       __A		)
     a											=False

     a											=nn.Dropout(p=__A		)

     a											=TaConfig(
         d_model=__A					,       num_heads=__A					,       d_kv=__A					,       d_ff=__A					,       feed_forward_proj=__A					,       dropout_rate=__A					,       is_decoder=__A					,       is_encoder_decoder=__A					,       )
     a											=nn.ModuleList()
     for lyr_num in range(__A		):
         a											=TaBlock(__A		)
         self.encoders.append(__A		)

     a											=TaLayerNorm(__A		)
     a											=nn.Dropout(p=__A		)





 def  SCREAMING_SNAKE_CASE				(						self					,       __A					,       __A		)   ->	str:
     a											=self.input_proj(__A		)

     # terminal relative positional encodings
     a											=encoder_inputs.shape[1]
     a											=torch.arange(__A					,       device=encoder_inputs.device		)

     a											=encoder_inputs_mask.sum(-1		)
     a											=torch.roll(input_positions.unsqueeze(0		)					,       tuple(seq_lens.tolist()		)					,       dims=0		)
     x += self.position_encoding(__A		)

     a											=self.dropout_pre(__A		)

     # inverted the attention mask
     a											=encoder_inputs.size()
     a											=self.get_extended_attention_mask(__A					,       __A		)

     for lyr in self.encoders:
         a											=lyr(__A					,       __A		)[0]
     a											=self.layer_norm(__A		)

     return self.dropout_post(__A		), encoder_inputs_mask