


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

							def __init__(			self	,						a__	,						a__ = 13	,						a__ = 64	,						a__ = 2	,						a__ = 3	,						a__ = 3	,						a__ = True	,						a__ = True	,						a__ = 1_28	,						a__=[16, 32, 64, 1_28]	,						a__ = 7	,						a__ = 4	,						a__ = 37	,						a__ = "gelu"	,						a__ = 0.1	,						a__ = 0.1	,						a__ = 10	,						a__ = 0.02	,						a__ = 2	,						a__ = 1	,						a__ = 1_28	,						a__ = [2, 2, 2, 2]	,						a__ = 2	,						a__ = 2	,						):






								'''simple docstring'''






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

							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






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

								snake_case   : Any	  =      None
								if self.use_labels:
									snake_case   : Dict	  =      ids_tensor([self.batch_size]	,						self.type_sequence_label_size					)

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

							def 				UpperCAmelCase				(			self					):






								'''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	,						a__	,						a__	,						a__					):






								'''simple docstring'''






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

							def 				UpperCAmelCase				(			self	,						a__	,						a__	,						a__					):






								'''simple docstring'''






								snake_case   : int	  =      self.type_sequence_label_size
								snake_case   : Optional[Any]	  =      EfficientFormerForImageClassification(a__					)
								model.to(a__					)
								model.eval()
								snake_case   : Dict	  =      model(a__	,						labels=a__					)
								self.parent.assertEqual(result.logits.shape	,						(self.batch_size, self.type_sequence_label_size)					)

								# test greyscale images
								snake_case   : str	  =      1
								snake_case   : Optional[Any]	  =      EfficientFormerForImageClassification(a__					)
								model.to(a__					)
								model.eval()

								snake_case   : Tuple	  =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]					)
								snake_case   : Tuple	  =      model(a__					)
								self.parent.assertEqual(result.logits.shape	,						(self.batch_size, self.type_sequence_label_size)					)



							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								snake_case   : Any	  =      self.prepare_config_and_inputs()
								(
								    snake_case  
								)   : Union[str, Any]	  =      config_and_inputs
								snake_case   : Tuple	  =      {"pixel_values": pixel_values}
								return config, inputs_dict







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

							_UpperCamelCase    		=      False
							_UpperCamelCase    		=      False
							_UpperCamelCase    		=      False

							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								snake_case   : Any	  =      EfficientFormerModelTester(self					)
								snake_case   : int	  =      ConfigTester(
								    self	,						config_class=a__	,						has_text_modality=a__	,						hidden_size=37					)

							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								self.config_tester.run_common_tests()

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






								'''simple docstring'''






								pass

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






								'''simple docstring'''






								pass

							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								snake_case   : Optional[Any]	  =      self.model_tester.prepare_config_and_inputs_for_common()

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

									snake_case   : List[str]	  =      ["pixel_values"]
									self.assertListEqual(arg_names[:1]	,						a__					)

							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								def check_hidden_states_output(a__	,						a__	,						a__					):
									snake_case   : List[Any]	  =      model_class(a__					)
									model.to(a__					)
									model.eval()

									with torch.no_grad():
										snake_case   : Any	  =      model(**self._prepare_for_class(a__	,						a__					)					)

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

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

									if hasattr(self.model_tester	,						'''encoder_seq_length'''					):
										snake_case   : Dict	  =      self.model_tester.encoder_seq_length
										if hasattr(self.model_tester	,						'''chunk_length'''					) and self.model_tester.chunk_length > 1:
											snake_case   : Dict	  =      seq_length * self.model_tester.chunk_length
									else:
										snake_case   : Dict	  =      self.model_tester.seq_length

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

									if config.is_encoder_decoder:
										snake_case   : Dict	  =      outputs.decoder_hidden_states

										self.assertIsInstance(a__	,						(list, tuple)					)
										self.assertEqual(len(a__					)	,						a__					)
										snake_case   : Tuple	  =      getattr(self.model_tester	,						'''seq_length'''	,						a__					)
										snake_case   : Optional[int]	  =      getattr(self.model_tester	,						'''decoder_seq_length'''	,						a__					)

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

								snake_case   : Union[str, Any]	  =      self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
									snake_case   : int	  =      True
									check_hidden_states_output(a__	,						a__	,						a__					)

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

									check_hidden_states_output(a__	,						a__	,						a__					)

							def 				UpperCAmelCase				(			self	,						a__	,						a__	,						a__=False					):






								'''simple docstring'''






								snake_case   : 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 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								snake_case   : Dict	  =      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					):






								'''simple docstring'''






								snake_case   : str	  =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*a__					)

							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								snake_case   : List[Any]	  =      self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*a__					)

							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								if not self.model_tester.is_training:
									return

								snake_case   : str	  =      self.model_tester.prepare_config_and_inputs_for_common()
								snake_case   : Dict	  =      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
									snake_case   : int	  =      model_class(a__					)
									model.to(a__					)
									model.train()
									snake_case   : int	  =      self._prepare_for_class(a__	,						a__	,						return_labels=a__					)
									snake_case   : Tuple	  =      model(**a__					).loss
									loss.backward()

							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								snake_case   : List[str]	  =      self.model_tester.prepare_config_and_inputs_for_common()

								snake_case   : 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']}'''					):
											snake_case   : Any	  =      problem_type["title"]
											snake_case   : List[str]	  =      problem_type["num_labels"]

											snake_case   : Union[str, Any]	  =      model_class(a__					)
											model.to(a__					)
											model.train()

											snake_case   : Any	  =      self._prepare_for_class(a__	,						a__	,						return_labels=a__					)

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

											snake_case   : Optional[Any]	  =      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:
												snake_case   : Dict	  =      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					):






								'''simple docstring'''






								for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									snake_case   : List[Any]	  =      EfficientFormerModel.from_pretrained(a__					)
									self.assertIsNotNone(a__					)



							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								snake_case   : Optional[Any]	  =      self.model_tester.prepare_config_and_inputs_for_common()
								snake_case   : Dict	  =      True

								snake_case   : Tuple	  =      getattr(self.model_tester	,						'''seq_length'''	,						a__					)
								snake_case   : Tuple	  =      getattr(self.model_tester	,						'''encoder_seq_length'''	,						a__					)
								snake_case   : Optional[int]	  =      getattr(self.model_tester	,						'''key_length'''	,						a__					)
								snake_case   : Union[str, Any]	  =      getattr(self.model_tester	,						'''chunk_length'''	,						a__					)

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

								for model_class in self.all_model_classes:
									snake_case   : List[Any]	  =      True
									snake_case   : List[Any]	  =      False
									snake_case   : int	  =      True
									snake_case   : List[str]	  =      model_class(a__					)
									model.to(a__					)
									model.eval()
									with torch.no_grad():
										snake_case   : Any	  =      model(**self._prepare_for_class(a__	,						a__					)					)
									snake_case   : Optional[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"]
									snake_case   : List[str]	  =      True
									snake_case   : str	  =      model_class(a__					)
									model.to(a__					)
									model.eval()
									with torch.no_grad():
										snake_case   : Any	  =      model(**self._prepare_for_class(a__	,						a__					)					)
									snake_case   : Optional[int]	  =      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    _UpperCAmelCase	(   )		->List[Any]:


	'''simple docstring'''
	snake_case   : str	  =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
	return image





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

							@cached_property
							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






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

							@slow
							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								snake_case   : Tuple	  =      EfficientFormerForImageClassification.from_pretrained('''snap-research/efficientformer-l1-300'''					).to(
								    a__					)

								snake_case   : Any	  =      self.default_image_processor
								snake_case   : Optional[int]	  =      prepare_img()
								snake_case   : Any	  =      image_processor(images=a__	,						return_tensors='''pt'''					).to(a__					)

								# forward pass
								with torch.no_grad():
									snake_case   : Any	  =      model(**a__					)

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

								snake_case   : List[str]	  =      torch.tensor([-0.0555, 0.4825, -0.0852]					).to(a__					)
								self.assertTrue(torch.allclose(outputs.logits[0][:3]	,						a__	,						atol=1e-4					)					)



							@slow
							def 				UpperCAmelCase				(			self					):






								'''simple docstring'''






								snake_case   : Any	  =      EfficientFormerForImageClassificationWithTeacher.from_pretrained(
								    '''snap-research/efficientformer-l1-300'''					).to(a__					)

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

								# forward pass
								with torch.no_grad():
									snake_case   : int	  =      model(**a__					)

								# verify the logits
								snake_case   : Dict	  =      (1, 10_00)
								self.assertEqual(outputs.logits.shape	,						a__					)

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










import math






def    _UpperCAmelCase	(   __lowerCamelCase       )		->str:


 '''simple docstring'''
 if number == 0:
  snake_case   : str	  =      '''Zero'''
  return words
 else:
  snake_case   : List[str]	  =      math.logaa(__lowerCamelCase       )
  snake_case   : List[Any]	  =      digits + 1
  snake_case   : List[str]	  =      {}
  snake_case   : Optional[int]	  =      ''''''
  snake_case   : Union[str, Any]	  =      '''One'''
  snake_case   : str	  =      '''Two'''
  snake_case   : Dict	  =      '''Three'''
  snake_case   : Dict	  =      '''Four'''
  snake_case   : int	  =      '''Five'''
  snake_case   : Optional[Any]	  =      '''Six'''
  snake_case   : Optional[Any]	  =      '''Seven'''
  snake_case   : Tuple	  =      '''Eight'''
  snake_case   : Dict	  =      '''Nine'''

  snake_case   : Tuple	  =      {}
  snake_case   : List[str]	  =      ''''''
  snake_case   : Tuple	  =      '''Twenty'''
  snake_case   : Optional[int]	  =      '''Thirty'''
  snake_case   : Optional[Any]	  =      '''Forty'''
  snake_case   : str	  =      '''Fifty'''
  snake_case   : int	  =      '''Sixty'''
  snake_case   : Optional[int]	  =      '''Seventy'''
  snake_case   : Dict	  =      '''Eighty'''
  snake_case   : List[str]	  =      '''Ninety'''

  snake_case   : List[str]	  =      {}
  snake_case   : int	  =      '''Ten'''
  snake_case   : Union[str, Any]	  =      '''Eleven'''
  snake_case   : Dict	  =      '''Twelve'''
  snake_case   : Any	  =      '''Thirteen'''
  snake_case   : str	  =      '''Fourteen'''
  snake_case   : Optional[int]	  =      '''Fifteen'''
  snake_case   : Any	  =      '''Sixteen'''
  snake_case   : List[Any]	  =      '''Seventeen'''
  snake_case   : List[str]	  =      '''Eighteen'''
  snake_case   : Tuple	  =      '''Nineteen'''

  snake_case   : Tuple	  =      {}
  snake_case   : List[Any]	  =      '''Hundred,'''
  snake_case   : Tuple	  =      '''Thousand,'''
  snake_case   : List[Any]	  =      '''Lakh,'''
  snake_case   : Union[str, Any]	  =      '''Crore,'''

  snake_case   : int	  =      number
  snake_case   : List[Any]	  =      ''''''
  snake_case   : Optional[int]	  =      0
  snake_case   : Optional[int]	  =      int(__lowerCamelCase       )
  while counter < digits:
   snake_case   : Optional[Any]	  =      temp_num % 10
   if counter % 2 == 0:
    snake_case   : int	  =      ''''''
    if counter in placevalue and current != 0:
     snake_case   : Any	  =      placevalue[counter]
    if counter == 2:
     snake_case   : str	  =      singles[current] + addition + words
    elif counter == 0:
     if ((temp_num % 100) // 10) == 1:
      snake_case   : Tuple	  =      teens[current] + addition + words
      snake_case   : Optional[int]	  =      temp_num // 10
      counter += 1
     else:
      snake_case   : Union[str, Any]	  =      singles[current] + addition + words

    else:
     snake_case   : List[str]	  =      doubles[current] + addition + words

   else:
    if counter == 1:
     if current == 1:
      snake_case   : str	  =      teens[number % 10] + words
     else:
      snake_case   : Tuple	  =      ''''''
      if counter in placevalue:
       snake_case   : Optional[int]	  =      placevalue[counter]
      snake_case   : List[Any]	  =      doubles[current] + addition + words
    else:
     snake_case   : List[str]	  =      ''''''
     if counter in placevalue:
      if current == 0 and ((temp_num % 100) // 10) == 0:
       snake_case   : Dict	  =      ''''''
      else:
       snake_case   : List[Any]	  =      placevalue[counter]
     if ((temp_num % 100) // 10) == 1:
      snake_case   : int	  =      teens[current] + addition + words
      snake_case   : Dict	  =      temp_num // 10
      counter += 1
     else:
      snake_case   : Optional[int]	  =      singles[current] + addition + words
   counter += 1
   snake_case   : Optional[int]	  =      temp_num // 10
 return words


if __name__ == "__main__":
  import doctest

  doctest.testmod()






