

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




	def __init__(						self		:  str						,		lowerCAmelCase		:  Dict						,		lowerCAmelCase		:  int = 13						,		lowerCAmelCase		:  int = 64						,		lowerCAmelCase		:  int = 2						,		lowerCAmelCase		:  int = 3						,		lowerCAmelCase		:  int = 3						,		lowerCAmelCase		:  bool = True						,		lowerCAmelCase		:  bool = True						,		lowerCAmelCase		:  int = 128						,		lowerCAmelCase		:  Optional[int]=[16, 32, 64, 128]						,		lowerCAmelCase		:  int = 7						,		lowerCAmelCase		:  int = 4						,		lowerCAmelCase		:  int = 37						,		lowerCAmelCase		:  str = "gelu"						,		lowerCAmelCase		:  float = 0.1						,		lowerCAmelCase		:  float = 0.1						,		lowerCAmelCase		:  int = 10						,		lowerCAmelCase		:  float = 0.02						,		lowerCAmelCase		:  int = 2						,		lowerCAmelCase		:  int = 1						,		lowerCAmelCase		:  int = 128						,		lowerCAmelCase		:  List[int] = [2, 2, 2, 2]						,		lowerCAmelCase		:  int = 2						,		lowerCAmelCase		:  int = 2						,		):


		'''simple docstring'''







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




	def  __A   (						self		:  Any							):


		'''simple docstring'''







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

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

		UpperCAmelCase_    			=     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=lowerCAmelCase						,		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		:  Optional[Any]						,		lowerCAmelCase		:  Optional[int]						,		lowerCAmelCase		:  List[Any]						,		lowerCAmelCase		:  List[str]							):


		'''simple docstring'''







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




	def  __A   (						self		:  Optional[int]						,		lowerCAmelCase		:  Optional[Any]						,		lowerCAmelCase		:  Tuple						,		lowerCAmelCase		:  Optional[int]							):


		'''simple docstring'''







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

		# test greyscale images
		UpperCAmelCase_    			=     1
		UpperCAmelCase_    			=     EfficientFormerForImageClassification(lowerCAmelCase							)
		model.to(lowerCAmelCase							)
		model.eval()

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




	def  __A   (						self		:  Dict							):


		'''simple docstring'''







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


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

	SCREAMING_SNAKE_CASE__      			=       False
	SCREAMING_SNAKE_CASE__      			=       False
	SCREAMING_SNAKE_CASE__      			=       False




	def  __A   (						self		:  List[str]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     EfficientFormerModelTester(self							)
		UpperCAmelCase_    			=     ConfigTester(
		    self						,		config_class=lowerCAmelCase						,		has_text_modality=lowerCAmelCase						,		hidden_size=37							)




	def  __A   (						self		:  Union[str, Any]							):


		'''simple docstring'''







		self.config_tester.run_common_tests()




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


		'''simple docstring'''







		pass




	@unittest.skip(reason="EfficientFormer does not support input and output embeddings"							)
	def  __A   (						self		:  List[Any]							):


		'''simple docstring'''







		pass




	def  __A   (						self		:  Optional[int]							):


		'''simple docstring'''







		UpperCAmelCase_			,				UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs_for_common()

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

			UpperCAmelCase_    			=     ["pixel_values"]
			self.assertListEqual(arg_names[:1]						,		lowerCAmelCase							)




	def  __A   (						self		:  str							):


		'''simple docstring'''







		def check_hidden_states_output(lowerCAmelCase		:  Tuple						,		lowerCAmelCase		:  int						,		lowerCAmelCase		:  Dict							):
			UpperCAmelCase_    			=     model_class(lowerCAmelCase							)
			model.to(lowerCAmelCase							)
			model.eval()

			with torch.no_grad():
				UpperCAmelCase_    			=     model(**self._prepare_for_class(lowerCAmelCase						,		lowerCAmelCase							)							)

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

			UpperCAmelCase_    			=     getattr(
			    self.model_tester						,		"expected_num_hidden_layers"						,		self.model_tester.num_hidden_layers + 1							)
			self.assertEqual(len(lowerCAmelCase							)						,		lowerCAmelCase							)

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

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

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

		UpperCAmelCase_			,				UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			UpperCAmelCase_    			=     True
			check_hidden_states_output(lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase							)

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

			check_hidden_states_output(lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase							)




	def  __A   (						self		:  int						,		lowerCAmelCase		:  str						,		lowerCAmelCase		:  Dict						,		lowerCAmelCase		:  Optional[int]=False							):


		'''simple docstring'''







		UpperCAmelCase_    			=     super()._prepare_for_class(lowerCAmelCase						,		lowerCAmelCase						,		return_labels=lowerCAmelCase							)

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

		return inputs_dict




	def  __A   (						self		:  List[Any]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*lowerCAmelCase							)




	@unittest.skip(reason="EfficientFormer does not implement masked image modeling yet"							)
	def  __A   (						self		:  List[Any]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_masked_image_modeling(*lowerCAmelCase							)




	def  __A   (						self		:  Any							):


		'''simple docstring'''







		UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase							)




	def  __A   (						self		:  List[str]							):


		'''simple docstring'''







		if not self.model_tester.is_training:
			return

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

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




	def  __A   (						self		:  Any							):


		'''simple docstring'''







		UpperCAmelCase_			,				UpperCAmelCase_    			=     self.model_tester.prepare_config_and_inputs_for_common()

		UpperCAmelCase_    			=     [
		    {"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(lowerCAmelCase							),
			    ]
			    or model_class.__name__ == "EfficientFormerForImageClassificationWithTeacher"
			):
				continue

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

					UpperCAmelCase_    			=     model_class(lowerCAmelCase							)
					model.to(lowerCAmelCase							)
					model.train()

					UpperCAmelCase_    			=     self._prepare_for_class(lowerCAmelCase						,		lowerCAmelCase						,		return_labels=lowerCAmelCase							)

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

					UpperCAmelCase_    			=     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=lowerCAmelCase							) as warning_list:
						UpperCAmelCase_    			=     model(**lowerCAmelCase							).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]:
			UpperCAmelCase_    			=     EfficientFormerModel.from_pretrained(lowerCAmelCase							)
			self.assertIsNotNone(lowerCAmelCase							)




	def  __A   (						self		:  Optional[int]							):


		'''simple docstring'''







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

		UpperCAmelCase_    			=     getattr(self.model_tester						,		"seq_length"						,		lowerCAmelCase							)
		UpperCAmelCase_    			=     getattr(self.model_tester						,		"encoder_seq_length"						,		lowerCAmelCase							)
		UpperCAmelCase_    			=     getattr(self.model_tester						,		"key_length"						,		lowerCAmelCase							)
		UpperCAmelCase_    			=     getattr(self.model_tester						,		"chunk_length"						,		lowerCAmelCase							)

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

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

			# check that output_attentions also work using config
			del inputs_dict["output_attentions"]
			UpperCAmelCase_    			=     True
			UpperCAmelCase_    			=     model_class(lowerCAmelCase							)
			model.to(lowerCAmelCase							)
			model.eval()
			with torch.no_grad():
				UpperCAmelCase_    			=     model(**self._prepare_for_class(lowerCAmelCase						,		lowerCAmelCase							)							)
			UpperCAmelCase_    			=     outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
			self.assertEqual(len(lowerCAmelCase							)						,		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  __lowerCAmelCase (  ):
	UpperCAmelCase_    			=     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
	return image




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




	@cached_property
	def  __A   (						self		:  Any							):


		'''simple docstring'''







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




	@slow
	def  __A   (						self		:  List[str]							):


		'''simple docstring'''







		UpperCAmelCase_    			=     EfficientFormerForImageClassification.from_pretrained("snap-research/efficientformer-l1-300"							).to(
		    lowerCAmelCase							)

		UpperCAmelCase_    			=     self.default_image_processor
		UpperCAmelCase_    			=     prepare_img()
		UpperCAmelCase_    			=     image_processor(images=lowerCAmelCase						,		return_tensors="pt"							).to(lowerCAmelCase							)

		# forward pass
		with torch.no_grad():
			UpperCAmelCase_    			=     model(**lowerCAmelCase							)

		# verify the logits
		UpperCAmelCase_    			=     (1, 1_000)
		self.assertEqual(outputs.logits.shape						,		lowerCAmelCase							)

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




	@slow
	def  __A   (						self		:  Tuple							):


		'''simple docstring'''







		UpperCAmelCase_    			=     EfficientFormerForImageClassificationWithTeacher.from_pretrained(
		    "snap-research/efficientformer-l1-300"							).to(lowerCAmelCase							)

		UpperCAmelCase_    			=     self.default_image_processor
		UpperCAmelCase_    			=     prepare_img()
		UpperCAmelCase_    			=     image_processor(images=lowerCAmelCase						,		return_tensors="pt"							).to(lowerCAmelCase							)

		# forward pass
		with torch.no_grad():
			UpperCAmelCase_    			=     model(**lowerCAmelCase							)

		# verify the logits
		UpperCAmelCase_    			=     (1, 1_000)
		self.assertEqual(outputs.logits.shape						,		lowerCAmelCase							)

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


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


_a:      Dict								=     {
    """configuration_nystromformer""": ["""NYSTROMFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """NystromformerConfig"""],
}

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_a:      Any								=     [
				    """NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """NystromformerForMaskedLM""",
				    """NystromformerForMultipleChoice""",
				    """NystromformerForQuestionAnswering""",
				    """NystromformerForSequenceClassification""",
				    """NystromformerForTokenClassification""",
				    """NystromformerLayer""",
				    """NystromformerModel""",
				    """NystromformerPreTrainedModel""",
				]


if TYPE_CHECKING:
				from .configuration_nystromformer import NYSTROMFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, NystromformerConfig

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_nystromformer import (
								    NYSTROMFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
								    NystromformerForMaskedLM,
								    NystromformerForMultipleChoice,
								    NystromformerForQuestionAnswering,
								    NystromformerForSequenceClassification,
								    NystromformerForTokenClassification,
								    NystromformerLayer,
								    NystromformerModel,
								    NystromformerPreTrainedModel,
								)


else:
				import sys

				_a:      Optional[Any]								=     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)