

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__:
						"""simple docstring"""






						def __init__(						self     ,			_lowercase     ,			_lowercase = 13     ,			_lowercase = 64     ,			_lowercase = 2     ,			_lowercase = 3     ,			_lowercase = 3     ,			_lowercase = True     ,			_lowercase = True     ,			_lowercase = 128     ,			_lowercase=[16, 32, 64, 128]     ,			_lowercase = 7     ,			_lowercase = 4     ,			_lowercase = 37     ,			_lowercase = "gelu"     ,			_lowercase = 0.1     ,			_lowercase = 0.1     ,			_lowercase = 10     ,			_lowercase = 0.0_2     ,			_lowercase = 2     ,			_lowercase = 1     ,			_lowercase = 128     ,			_lowercase = [2, 2, 2, 2]     ,			_lowercase = 2     ,			_lowercase = 2     ,			)    ->   Optional[Any]:
									a_	:	Union[str, Any]          =   parent
									a_	:	Optional[Any]          =   batch_size
									a_	:	Optional[Any]          =   image_size
									a_	:	List[str]          =   patch_size
									a_	:	List[Any]          =   num_channels
									a_	:	List[Any]          =   is_training
									a_	:	Any          =   use_labels
									a_	:	str          =   hidden_size
									a_	:	Any          =   num_hidden_layers
									a_	:	List[Any]          =   num_attention_heads
									a_	:	List[Any]          =   intermediate_size
									a_	:	Optional[int]          =   hidden_act
									a_	:	Optional[Any]          =   hidden_dropout_prob
									a_	:	List[str]          =   attention_probs_dropout_prob
									a_	:	Dict          =   type_sequence_label_size
									a_	:	Dict          =   initializer_range
									a_	:	Dict          =   encoder_stride
									a_	:	List[str]          =   num_attention_outputs
									a_	:	List[str]          =   embed_dim
									a_	:	Union[str, Any]          =   embed_dim + 1
									a_	:	List[Any]          =   resolution
									a_	:	Optional[int]          =   depths
									a_	:	str          =   hidden_sizes
									a_	:	Tuple          =   dim
									a_	:	Tuple          =   mlp_expansion_ratio





						def  UpperCamelCase__				(						self						)    ->   str:
									a_	:	List[str]          =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

									a_	:	Union[str, Any]          =   None
									if self.use_labels:
												a_	:	Any          =   ids_tensor([self.batch_size]     ,			self.type_sequence_label_size						)

									a_	:	int          =   self.get_config()
									return config, pixel_values, labels





						def  UpperCamelCase__				(						self						)    ->   Tuple:
									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=_lowercase     ,			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  UpperCamelCase__				(						self     ,			_lowercase     ,			_lowercase     ,			_lowercase						)    ->   Tuple:
									a_	:	Union[str, Any]          =   EfficientFormerModel(config=_lowercase						)
									model.to(_lowercase						)
									model.eval()
									a_	:	str          =   model(_lowercase						)
									self.parent.assertEqual(result.last_hidden_state.shape     ,			(self.batch_size, self.seq_length, self.hidden_size)						)





						def  UpperCamelCase__				(						self     ,			_lowercase     ,			_lowercase     ,			_lowercase						)    ->   Dict:
									a_	:	Optional[Any]          =   self.type_sequence_label_size
									a_	:	Dict          =   EfficientFormerForImageClassification(_lowercase						)
									model.to(_lowercase						)
									model.eval()
									a_	:	List[str]          =   model(_lowercase     ,			labels=_lowercase						)
									self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.type_sequence_label_size)						)

									# test greyscale images
									a_	:	str          =   1
									a_	:	Any          =   EfficientFormerForImageClassification(_lowercase						)
									model.to(_lowercase						)
									model.eval()

									a_	:	List[str]          =   floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
									a_	:	Tuple          =   model(_lowercase						)
									self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.type_sequence_label_size)						)





						def  UpperCamelCase__				(						self						)    ->   Union[str, Any]:
									a_	:	List[str]          =   self.prepare_config_and_inputs()
									(
									    (
									    a_
									)       ,				(
									    a_
									)       ,				(
									    a_
									)       ,				
									)	:	Union[str, Any]          =   config_and_inputs
									a_	:	str          =   {"""pixel_values""": pixel_values}
									return config, inputs_dict







@require_torch
class 					A__(a_,     a_,     unittest.TestCase       ):
						"""simple docstring"""






						_A						:							Optional[int]									= (
						    (
						        EfficientFormerModel,
						        EfficientFormerForImageClassificationWithTeacher,
						        EfficientFormerForImageClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						_A						:							Dict									= (
						    {
						        '''feature-extraction''': EfficientFormerModel,
						        '''image-classification''': (
						            EfficientFormerForImageClassification,
						            EfficientFormerForImageClassificationWithTeacher,
						        ),
						    }
						    if is_torch_available()
						    else {}
						)
						_A						:							str									= False

						_A						:							Dict									= False
						_A						:							Union[str, Any]									= False
						_A						:							Optional[int]									= False





						def  UpperCamelCase__				(						self						)    ->   List[Any]:
									a_	:	Optional[int]          =   EfficientFormerModelTester(self						)
									a_	:	Union[str, Any]          =   ConfigTester(
									    self     ,			config_class=_lowercase     ,			has_text_modality=_lowercase     ,			hidden_size=37						)





						def  UpperCamelCase__				(						self						)    ->   Optional[int]:
									self.config_tester.run_common_tests()





						@unittest.skip(reason="""EfficientFormer does not use inputs_embeds"""						)
						def  UpperCamelCase__				(						self						)    ->   int:
									pass





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





						def  UpperCamelCase__				(						self						)    ->   Tuple:
									a_       ,				a_	:	Union[str, Any]          =   self.model_tester.prepare_config_and_inputs_for_common()

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

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





						def  UpperCamelCase__				(						self						)    ->   int:
									def check_hidden_states_output(_lowercase     ,			_lowercase     ,			_lowercase						):
												a_	:	List[str]          =   model_class(_lowercase						)
												model.to(_lowercase						)
												model.eval()

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

												a_	:	Optional[Any]          =   outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

												a_	:	List[Any]          =   getattr(
												    self.model_tester     ,			"""expected_num_hidden_layers"""     ,			self.model_tester.num_hidden_layers + 1						)
												self.assertEqual(len(_lowercase						)     ,			_lowercase						)

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

															self.assertIsInstance(_lowercase     ,			(list, tuple)						)
															self.assertEqual(len(_lowercase						)     ,			_lowercase						)
															a_	:	List[str]          =   getattr(self.model_tester     ,			"""seq_length"""     ,			_lowercase						)
															a_	:	Union[str, Any]          =   getattr(self.model_tester     ,			"""decoder_seq_length"""     ,			_lowercase						)

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

									a_       ,				a_	:	Dict          =   self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												a_	:	Union[str, Any]          =   True
												check_hidden_states_output(_lowercase     ,			_lowercase     ,			_lowercase						)

												# check that output_hidden_states also work using config
												del inputs_dict["output_hidden_states"]
												a_	:	Dict          =   True

												check_hidden_states_output(_lowercase     ,			_lowercase     ,			_lowercase						)





						def  UpperCamelCase__				(						self     ,			_lowercase     ,			_lowercase     ,			_lowercase=False						)    ->   int:
									a_	:	Any          =   super()._prepare_for_class(_lowercase     ,			_lowercase     ,			return_labels=_lowercase						)

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

									return inputs_dict





						def  UpperCamelCase__				(						self						)    ->   Tuple:
									a_	:	List[Any]          =   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowercase						)





						@unittest.skip(reason="""EfficientFormer does not implement masked image modeling yet"""						)
						def  UpperCamelCase__				(						self						)    ->   int:
									a_	:	str          =   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*_lowercase						)





						def  UpperCamelCase__				(						self						)    ->   Dict:
									a_	:	int          =   self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_lowercase						)





						def  UpperCamelCase__				(						self						)    ->   Union[str, Any]:
									if not self.model_tester.is_training:
												return

									a_       ,				a_	:	str          =   self.model_tester.prepare_config_and_inputs_for_common()
									a_	:	List[Any]          =   True

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





						def  UpperCamelCase__				(						self						)    ->   Optional[Any]:
									a_       ,				a_	:	Union[str, Any]          =   self.model_tester.prepare_config_and_inputs_for_common()

									a_	:	List[str]          =   [
									    {"""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(_lowercase						),
												    ]
												    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_	:	Dict          =   problem_type["""title"""]
																		a_	:	List[Any]          =   problem_type["""num_labels"""]

																		a_	:	List[str]          =   model_class(_lowercase						)
																		model.to(_lowercase						)
																		model.train()

																		a_	:	List[Any]          =   self._prepare_for_class(_lowercase     ,			_lowercase     ,			return_labels=_lowercase						)

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

																		a_	:	Optional[int]          =   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=_lowercase						) as warning_list:
																					a_	:	List[Any]          =   model(**_lowercase						).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  UpperCamelCase__				(						self						)    ->   List[str]:
									for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a_	:	Dict          =   EfficientFormerModel.from_pretrained(_lowercase						)
												self.assertIsNotNone(_lowercase						)





						def  UpperCamelCase__				(						self						)    ->   Dict:
									a_       ,				a_	:	Union[str, Any]          =   self.model_tester.prepare_config_and_inputs_for_common()
									a_	:	List[Any]          =   True

									a_	:	Optional[int]          =   getattr(self.model_tester     ,			"""seq_length"""     ,			_lowercase						)
									a_	:	Optional[Any]          =   getattr(self.model_tester     ,			"""encoder_seq_length"""     ,			_lowercase						)
									a_	:	int          =   getattr(self.model_tester     ,			"""key_length"""     ,			_lowercase						)
									a_	:	Any          =   getattr(self.model_tester     ,			"""chunk_length"""     ,			_lowercase						)

									if chunk_length is not None and hasattr(self.model_tester     ,			"""num_hashes"""						):
												a_	:	Union[str, Any]          =   encoder_seq_length * self.model_tester.num_hashes

									for model_class in self.all_model_classes:
												a_	:	Optional[int]          =   True
												a_	:	List[Any]          =   False
												a_	:	Tuple          =   True
												a_	:	Tuple          =   model_class(_lowercase						)
												model.to(_lowercase						)
												model.eval()
												with torch.no_grad():
															a_	:	Tuple          =   model(**self._prepare_for_class(_lowercase     ,			_lowercase						)						)
												a_	:	Optional[int]          =   outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
												self.assertEqual(len(_lowercase						)     ,			self.model_tester.num_attention_outputs						)

												# check that output_attentions also work using config
												del inputs_dict["output_attentions"]
												a_	:	int          =   True
												a_	:	Optional[int]          =   model_class(_lowercase						)
												model.to(_lowercase						)
												model.eval()
												with torch.no_grad():
															a_	:	Optional[Any]          =   model(**self._prepare_for_class(_lowercase     ,			_lowercase						)						)
												a_	:	Optional[int]          =   outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
												self.assertEqual(len(_lowercase						)     ,			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							(	):

			'''simple docstring'''



			a_	:	Optional[Any]          =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
			return image



@require_torch
@require_vision
class 					A__(unittest.TestCase       ):
						"""simple docstring"""






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





						@slow
						def  UpperCamelCase__				(						self						)    ->   str:
									a_	:	Union[str, Any]          =   EfficientFormerForImageClassification.from_pretrained("""snap-research/efficientformer-l1-300"""						).to(
									    _lowercase						)

									a_	:	List[Any]          =   self.default_image_processor
									a_	:	Union[str, Any]          =   prepare_img()
									a_	:	Optional[Any]          =   image_processor(images=_lowercase     ,			return_tensors="""pt"""						).to(_lowercase						)

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

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

									a_	:	Optional[int]          =   torch.tensor([-0.0_5_5_5, 0.4_8_2_5, -0.0_8_5_2]						).to(_lowercase						)
									self.assertTrue(torch.allclose(outputs.logits[0][:3]     ,			_lowercase     ,			atol=1e-4						)						)





						@slow
						def  UpperCamelCase__				(						self						)    ->   Optional[int]:
									a_	:	str          =   EfficientFormerForImageClassificationWithTeacher.from_pretrained(
									    """snap-research/efficientformer-l1-300"""						).to(_lowercase						)

									a_	:	Tuple          =   self.default_image_processor
									a_	:	str          =   prepare_img()
									a_	:	Dict          =   image_processor(images=_lowercase     ,			return_tensors="""pt"""						).to(_lowercase						)

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

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

									a_	:	Optional[Any]          =   torch.tensor([-0.1_3_1_2, 0.4_3_5_3, -1.0_4_9_9]						).to(_lowercase						)
									self.assertTrue(torch.allclose(outputs.logits[0][:3]     ,			_lowercase     ,			atol=1e-4						)						)





import json

import requests

from .fetch_github_info import AUTHENTICATED_USER_ENDPOINT, fetch_github_info


def 				_UpperCAmelCase							(	a__):

			'''simple docstring'''



			class 					A__:
									"""simple docstring"""






									def __init__(						self     ,			_lowercase						)    ->   None:
												assert isinstance(_lowercase     ,			(bytes, str)						)
												a_	:	List[Any]          =   content





									def  UpperCamelCase__				(						self						)    ->   Optional[Any]:
												return json.loads(self.content						)

			def mock_response(*a__     ,  **a__):
						assert args[0] == AUTHENTICATED_USER_ENDPOINT
						assert "Authorization" in kwargs["headers"]
						assert kwargs["headers"]["Authorization"].startswith("""token """)
						assert "Accept" in kwargs["headers"]
						return FakeResponse(B"""{\"login\":\"test\",\"id\":1}""")

			monkeypatch.setattr(a__     ,  """get"""     ,  a__)
			a_	:	Tuple          =   fetch_github_info("""token""")
			assert result["login"] == "test"
			assert result["id"] == 1


