




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







   """simple docstring"""




   def __init__(		self					,				lowerCamelCase_					,				lowerCamelCase_ = 1_3					,				lowerCamelCase_ = 6_4					,				lowerCamelCase_ = 2					,				lowerCamelCase_ = 3					,				lowerCamelCase_ = 3					,				lowerCamelCase_ = True					,				lowerCamelCase_ = True					,				lowerCamelCase_ = 1_2_8					,				lowerCamelCase_=[1_6, 3_2, 6_4, 1_2_8]					,				lowerCamelCase_ = 7					,				lowerCamelCase_ = 4					,				lowerCamelCase_ = 3_7					,				lowerCamelCase_ = "gelu"					,				lowerCamelCase_ = 0.1					,				lowerCamelCase_ = 0.1					,				lowerCamelCase_ = 1_0					,				lowerCamelCase_ = 0.02					,				lowerCamelCase_ = 2					,				lowerCamelCase_ = 1					,				lowerCamelCase_ = 1_2_8					,				lowerCamelCase_ = [2, 2, 2, 2]					,				lowerCamelCase_ = 2					,				lowerCamelCase_ = 2					,				)							->      Tuple:
       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      UpperCAmelCase__ (		self)							->      int:
       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      UpperCAmelCase__ (		self)							->      Union[str, Any]:
       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      UpperCAmelCase__ (		self					,				lowerCamelCase_					,				lowerCamelCase_					,				lowerCamelCase_)							->      Union[str, Any]:
       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      UpperCAmelCase__ (		self					,				lowerCamelCase_					,				lowerCamelCase_					,				lowerCamelCase_)							->      Optional[int]:
       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      UpperCAmelCase__ (		self)							->      str:
       UpperCamelCase										=  self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase
       )					,  (
           UpperCamelCase
       )					,  (
           UpperCamelCase
       )					,  
       )										=  config_and_inputs
       UpperCamelCase										=  {'''pixel_values''': pixel_values}
       return config, inputs_dict

@require_torch
class 							snake_case_ (				lowerCamelCase_  ,   lowerCamelCase_  ,   unittest.TestCase ):







   """simple docstring"""




   A_										=					(
       (
           EfficientFormerModel,
           EfficientFormerForImageClassificationWithTeacher,
           EfficientFormerForImageClassification,
       )
       if is_torch_available()
       else ()
   )
   A_										=					(
       {
           '''feature-extraction''': EfficientFormerModel,
           '''image-classification''': (
               EfficientFormerForImageClassification,
               EfficientFormerForImageClassificationWithTeacher,
           ),
       }
       if is_torch_available()
       else {}
   )
   A_										=					False

   A_										=					False
   A_										=					False
   A_										=					False







   def      UpperCAmelCase__ (		self)							->      List[Any]:
       UpperCamelCase										=  EfficientFormerModelTester(self)
       UpperCamelCase										=  ConfigTester(
           self					,				config_class=lowerCamelCase_					,				has_text_modality=lowerCamelCase_					,				hidden_size=3_7)







   def      UpperCAmelCase__ (		self)							->      Dict:
       self.config_tester.run_common_tests()







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







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







   def      UpperCAmelCase__ (		self)							->      Tuple:
       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      UpperCAmelCase__ (		self)							->      Union[str, Any]:
       def check_hidden_states_output(lowerCamelCase_					,				lowerCamelCase_					,				lowerCamelCase_):
           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      UpperCAmelCase__ (		self					,				lowerCamelCase_					,				lowerCamelCase_					,				lowerCamelCase_=False)							->      List[str]:
       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      UpperCAmelCase__ (		self)							->      Optional[int]:
       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      UpperCAmelCase__ (		self)							->      Union[str, Any]:
       UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*lowerCamelCase_)







   def      UpperCAmelCase__ (		self)							->      Any:
       UpperCamelCase										=  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_)







   def      UpperCAmelCase__ (		self)							->      str:
       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      UpperCAmelCase__ (		self)							->      int:
       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      UpperCAmelCase__ (		self)							->      int:
       for model_name in EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase										=  EfficientFormerModel.from_pretrained(lowerCamelCase_)
           self.assertIsNotNone(lowerCamelCase_)







   def      UpperCAmelCase__ (		self)							->      str:
       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     __snake_case   (							):


    """simple docstring"""







    UpperCamelCase										=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
    return image


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







   """simple docstring"""




   @cached_property
   def      UpperCAmelCase__ (		self)							->      List[Any]:
       return (
           EfficientFormerImageProcessor.from_pretrained('''snap-research/efficientformer-l1-300''')
           if is_vision_available()
           else None
       )







   @slow
   def      UpperCAmelCase__ (		self)							->      List[str]:
       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_0_0_0)
       self.assertEqual(outputs.logits.shape					,				lowerCamelCase_)

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







   @slow
   def      UpperCAmelCase__ (		self)							->      List[Any]:
       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_0_0_0)
       self.assertEqual(outputs.logits.shape					,				lowerCamelCase_)

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





"""simple docstring"""







import argparse

from . import (
    BertConfig,
    BertGenerationConfig,
    BertGenerationDecoder,
    BertGenerationEncoder,
    load_tf_weights_in_bert_generation,
    logging,
)


logging.set_verbosity_info()





def     __snake_case   (							_lowercase ,_lowercase ,_lowercase ,_lowercase ,_lowercase						):


    """simple docstring"""







    UpperCamelCase										=  BertConfig.from_pretrained(
        '''bert-large-cased''' ,vocab_size=_lowercase ,max_position_embeddings=512 ,is_decoder=_lowercase ,add_cross_attention=_lowercase ,)
    UpperCamelCase										=  bert_config.to_dict()
    del bert_config_dict["type_vocab_size"]
    UpperCamelCase										=  BertGenerationConfig(**_lowercase						)
    if is_encoder:
        UpperCamelCase										=  BertGenerationEncoder(_lowercase						)
    else:
        UpperCamelCase										=  BertGenerationDecoder(_lowercase						)
    print(f'Building PyTorch model from configuration: {config}'						)

    # Load weights from tf checkpoint
    load_tf_weights_in_bert_generation(
        _lowercase ,_lowercase ,model_class='''bert''' ,is_encoder_named_decoder=_lowercase ,is_encoder=_lowercase ,)

    # Save pytorch-model
    print(f'Save PyTorch model and config to {pytorch_dump_path}'						)
    model.save_pretrained(_lowercase						)


if __name__ == "__main__":
  SCREAMING_SNAKE_CASE_      =					argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      '--tf_hub_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
  )
  parser.add_argument(
      '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
  )
  parser.add_argument(
      '--is_encoder_named_decoder',
      action='store_true',
      help='If decoder has to be renamed to encoder in PyTorch model.',
  )
  parser.add_argument('--is_encoder', action='store_true', help='If model is an encoder.')
  parser.add_argument('--vocab_size', default=50358, type=int, help='Vocab size of model')
  SCREAMING_SNAKE_CASE_      =					parser.parse_args()
  convert_tf_checkpoint_to_pytorch(
      args.tf_hub_path,
      args.pytorch_dump_path,
      args.is_encoder_named_decoder,
      args.vocab_size,
      is_encoder=args.is_encoder,
  )