



'''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      :       Union[str, Any]				,lowerCamelCase__      :       str				,lowerCamelCase__      :       Union[str, Any] = 13				,lowerCamelCase__      :       Optional[Any] = 64				,lowerCamelCase__      :       List[str] = 2				,lowerCamelCase__      :       str = 3				,lowerCamelCase__      :       Any = 3				,lowerCamelCase__      :       Union[str, Any] = True				,lowerCamelCase__      :       Dict = True				,lowerCamelCase__      :       int = 128				,lowerCamelCase__      :       Dict=[16, 32, 64, 128]				,lowerCamelCase__      :       str = 7				,lowerCamelCase__      :       Optional[int] = 4				,lowerCamelCase__      :       List[str] = 37				,lowerCamelCase__      :       str = "gelu"				,lowerCamelCase__      :       Optional[Any] = 0.1				,lowerCamelCase__      :       Any = 0.1				,lowerCamelCase__      :       Any = 10				,lowerCamelCase__      :       Optional[Any] = 0.0_2				,lowerCamelCase__      :       Union[str, Any] = 2				,lowerCamelCase__      :       Dict = 1				,lowerCamelCase__      :       List[str] = 128				,lowerCamelCase__      :       Any = [2, 2, 2, 2]				,lowerCamelCase__      :       Optional[int] = 2				,lowerCamelCase__      :       Optional[Any] = 2				,):







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





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







							'''simple docstring'''
							_UpperCamelCase      :	str				       =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

							_UpperCamelCase      :	str				       =     self.get_config()
							return config, pixel_values, labels





	def 			UpperCamelCase_						(				self      :       Optional[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=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 			UpperCamelCase_						(				self      :       Any				,lowerCamelCase__      :       str				,lowerCamelCase__      :       str				,lowerCamelCase__      :       Dict				):







							'''simple docstring'''
							_UpperCamelCase      :	Optional[Any]				       =     EfficientFormerModel(config=a_				)
							model.to(a_				)
							model.eval()
							_UpperCamelCase      :	Dict				       =     model(a_				)
							self.parent.assertEqual(result.last_hidden_state.shape				,(self.batch_size, self.seq_length, self.hidden_size)				)





	def 			UpperCamelCase_						(				self      :       Optional[Any]				,lowerCamelCase__      :       Union[str, Any]				,lowerCamelCase__      :       Optional[Any]				,lowerCamelCase__      :       Any				):







							'''simple docstring'''
							_UpperCamelCase      :	List[Any]				       =     self.type_sequence_label_size
							_UpperCamelCase      :	List[Any]				       =     EfficientFormerForImageClassification(a_				)
							model.to(a_				)
							model.eval()
							_UpperCamelCase      :	Optional[Any]				       =     model(a_				,labels=a_				)
							self.parent.assertEqual(result.logits.shape				,(self.batch_size, self.type_sequence_label_size)				)

							# test greyscale images
							_UpperCamelCase      :	Optional[int]				       =     1
							_UpperCamelCase      :	List[Any]				       =     EfficientFormerForImageClassification(a_				)
							model.to(a_				)
							model.eval()

							_UpperCamelCase      :	int				       =     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
							_UpperCamelCase      :	Optional[int]				       =     model(a_				)
							self.parent.assertEqual(result.logits.shape				,(self.batch_size, self.type_sequence_label_size)				)





	def 			UpperCamelCase_						(				self      :       Dict				):







							'''simple docstring'''
							_UpperCamelCase      :	Dict				       =     self.prepare_config_and_inputs()
							(
							    (
							    _UpperCamelCase      
							)       ,    (
							    _UpperCamelCase      
							)       ,    (
							    _UpperCamelCase      
							)       ,          
							)      :	Tuple				       =     config_and_inputs
							_UpperCamelCase      :	str				       =     {'pixel_values': pixel_values}
							return config, inputs_dict






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

	lowercase__						       =    False
	lowercase__						       =    False
	lowercase__						       =    False





	def 			UpperCamelCase_						(				self      :       Any				):







							'''simple docstring'''
							_UpperCamelCase      :	Tuple				       =     EfficientFormerModelTester(self				)
							_UpperCamelCase      :	Any				       =     ConfigTester(
							    self				,config_class=a_				,has_text_modality=a_				,hidden_size=37				)





	def 			UpperCamelCase_						(				self      :       Dict				):







							'''simple docstring'''
							self.config_tester.run_common_tests()





	@unittest.skip(reason='EfficientFormer does not use inputs_embeds'				)
	def 			UpperCamelCase_						(				self      :       Dict				):







							'''simple docstring'''
							pass





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







							'''simple docstring'''
							pass





	def 			UpperCamelCase_						(				self      :       int				):







							'''simple docstring'''
							_UpperCamelCase       ,    _UpperCamelCase      :	List[Any]				       =     self.model_tester.prepare_config_and_inputs_for_common()

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

													_UpperCamelCase      :	List[str]				       =     ['pixel_values']
													self.assertListEqual(arg_names[:1]				,a_				)





	def 			UpperCamelCase_						(				self      :       Any				):







							'''simple docstring'''
							def check_hidden_states_output(lowerCamelCase__      :       Any				,lowerCamelCase__      :       List[Any]				,lowerCamelCase__      :       Any				):
													_UpperCamelCase      :	str				       =     model_class(a_				)
													model.to(a_				)
													model.eval()

													with torch.no_grad():
																			_UpperCamelCase      :	int				       =     model(**self._prepare_for_class(a_				,a_				)				)

													_UpperCamelCase      :	Tuple				       =     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

													_UpperCamelCase      :	str				       =     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'				):
																			_UpperCamelCase      :	Tuple				       =     self.model_tester.encoder_seq_length
																			if hasattr(self.model_tester				,'chunk_length'				) and self.model_tester.chunk_length > 1:
																									_UpperCamelCase      :	Optional[int]				       =     seq_length * self.model_tester.chunk_length
													else:
																			_UpperCamelCase      :	Union[str, 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:
																			_UpperCamelCase      :	Tuple				       =     outputs.decoder_hidden_states

																			self.assertIsInstance(a_				,(list, tuple)				)
																			self.assertEqual(len(a_				)				,a_				)
																			_UpperCamelCase      :	Tuple				       =     getattr(self.model_tester				,'seq_length'				,a_				)
																			_UpperCamelCase      :	Union[str, 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]				,)

							_UpperCamelCase       ,    _UpperCamelCase      :	Optional[int]				       =     self.model_tester.prepare_config_and_inputs_for_common()

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

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

													check_hidden_states_output(a_				,a_				,a_				)





	def 			UpperCamelCase_						(				self      :       Any				,lowerCamelCase__      :       int				,lowerCamelCase__      :       Dict				,lowerCamelCase__      :       Optional[Any]=False				):







							'''simple docstring'''
							_UpperCamelCase      :	Any				       =     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 			UpperCamelCase_						(				self      :       List[Any]				):







							'''simple docstring'''
							_UpperCamelCase      :	Tuple				       =     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 			UpperCamelCase_						(				self      :       Tuple				):







							'''simple docstring'''
							_UpperCamelCase      :	List[Any]				       =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_image_modeling(*a_				)





	def 			UpperCamelCase_						(				self      :       Optional[Any]				):







							'''simple docstring'''
							_UpperCamelCase      :	int				       =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*a_				)





	def 			UpperCamelCase_						(				self      :       Optional[Any]				):







							'''simple docstring'''
							if not self.model_tester.is_training:
													return

							_UpperCamelCase       ,    _UpperCamelCase      :	Tuple				       =     self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCamelCase      :	str				       =     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
													_UpperCamelCase      :	List[Any]				       =     model_class(a_				)
													model.to(a_				)
													model.train()
													_UpperCamelCase      :	Tuple				       =     self._prepare_for_class(a_				,a_				,return_labels=a_				)
													_UpperCamelCase      :	List[Any]				       =     model(**a_				).loss
													loss.backward()





	def 			UpperCamelCase_						(				self      :       Any				):







							'''simple docstring'''
							_UpperCamelCase       ,    _UpperCamelCase      :	List[str]				       =     self.model_tester.prepare_config_and_inputs_for_common()

							_UpperCamelCase      :	List[Any]				       =     [
							    {'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"]}'				):
																									_UpperCamelCase      :	Dict				       =     problem_type['title']
																									_UpperCamelCase      :	Union[str, Any]				       =     problem_type['num_labels']

																									_UpperCamelCase      :	Union[str, Any]				       =     model_class(a_				)
																									model.to(a_				)
																									model.train()

																									_UpperCamelCase      :	Optional[int]				       =     self._prepare_for_class(a_				,a_				,return_labels=a_				)

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

																									_UpperCamelCase      :	List[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:
																															_UpperCamelCase      :	Tuple				       =     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 			UpperCamelCase_						(				self      :       Any				):







							'''simple docstring'''
							for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_UpperCamelCase      :	List[str]				       =     EfficientFormerModel.from_pretrained(a_				)
													self.assertIsNotNone(a_				)





	def 			UpperCamelCase_						(				self      :       Any				):







							'''simple docstring'''
							_UpperCamelCase       ,    _UpperCamelCase      :	Tuple				       =     self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCamelCase      :	List[Any]				       =     True

							_UpperCamelCase      :	List[Any]				       =     getattr(self.model_tester				,'seq_length'				,a_				)
							_UpperCamelCase      :	List[Any]				       =     getattr(self.model_tester				,'encoder_seq_length'				,a_				)
							_UpperCamelCase      :	Any				       =     getattr(self.model_tester				,'key_length'				,a_				)
							_UpperCamelCase      :	List[Any]				       =     getattr(self.model_tester				,'chunk_length'				,a_				)

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

							for model_class in self.all_model_classes:
													_UpperCamelCase      :	Dict				       =     True
													_UpperCamelCase      :	List[str]				       =     False
													_UpperCamelCase      :	str				       =     True
													_UpperCamelCase      :	Any				       =     model_class(a_				)
													model.to(a_				)
													model.eval()
													with torch.no_grad():
																			_UpperCamelCase      :	List[Any]				       =     model(**self._prepare_for_class(a_				,a_				)				)
													_UpperCamelCase      :	int				       =     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"]
													_UpperCamelCase      :	Optional[Any]				       =     True
													_UpperCamelCase      :	int				       =     model_class(a_				)
													model.to(a_				)
													model.eval()
													with torch.no_grad():
																			_UpperCamelCase      :	Union[str, Any]				       =     model(**self._prepare_for_class(a_				,a_				)				)
													_UpperCamelCase      :	List[Any]				       =     outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
													self.assertEqual(len(a_				)				,self.model_tester.num_attention_outputs				)

													if chunk_length is not None:
																			self.assertListEqual(
																			    list(attentions[0].shape[-4:]				)				,[self.model_tester.num_attention_heads, encoder_seq_length, chunk_length, encoder_key_length]				,)
													else:
																			self.assertListEqual(
																			    list(attentions[0].shape[-3:]				)				,[self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]				,)






def A__					(  ):
						_UpperCamelCase      :	Union[str, Any]				       =     Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
						return image






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





	@cached_property
	def 			UpperCamelCase_						(				self      :       Any				):







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





	@slow
	def 			UpperCamelCase_						(				self      :       Optional[Any]				):







							'''simple docstring'''
							_UpperCamelCase      :	str				       =     EfficientFormerForImageClassification.from_pretrained('snap-research/efficientformer-l1-300'				).to(
							    a_				)

							_UpperCamelCase      :	Tuple				       =     self.default_image_processor
							_UpperCamelCase      :	List[str]				       =     prepare_img()
							_UpperCamelCase      :	List[str]				       =     image_processor(images=a_				,return_tensors='pt'				).to(a_				)

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

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

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





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







							'''simple docstring'''
							_UpperCamelCase      :	List[Any]				       =     EfficientFormerForImageClassificationWithTeacher.from_pretrained(
							    'snap-research/efficientformer-l1-300'				).to(a_				)

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

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

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

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







'''simple docstring'''
def A__					(  UpperCAmelCase_							):
						_UpperCamelCase      :	Optional[Any]				       =     word.upper()
						_UpperCamelCase      :	Optional[Any]				       =     0
						_UpperCamelCase      :	Dict				       =     {}
						_UpperCamelCase      :	List[str]				       =     []

						for letter in word:
												if letter not in letter_nums:
																		_UpperCamelCase      :	int				       =     str(UpperCAmelCase_							)
																		next_num += 1
												word_pattern.append(letter_nums[letter]							)
						return ".".join(UpperCAmelCase_							)


if __name__ == "__main__":
					import pprint
					import time

					snake_case_				:			List[Any]						 =						time.time()
					with open('dictionary.txt') as in_file:
										snake_case_				:			Any						 =						in_file.read().splitlines()

					snake_case_				:			dict						 =						{}
					for word in word_list:
										snake_case_				:			Optional[int]						 =						get_word_pattern(word)
										if pattern in all_patterns:
															all_patterns[pattern].append(word)
										else:
															snake_case_				:			Any						 =						[word]

					with open('word_patterns.txt', 'w') as out_file:
										out_file.write(pprint.pformat(all_patterns))

					snake_case_				:			List[str]						 =						round(time.time() - start_time, 2)
					print(F"""Done!  {len(all_patterns):,} word patterns found in {total_time} seconds.""")
					# Done!  9,581 word patterns found in 0.58 seconds.


