


import inspect
import unittest
from typing import List

import numpy as np

from transformers import EfficientFormerConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import (
       TFEfficientFormerForImageClassification,
       TFEfficientFormerForImageClassificationWithTeacher,
       TFEfficientFormerModel,
   )
   from transformers.models.efficientformer.modeling_tf_efficientformer import (
       TF_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     ,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.02     ,lowercase = 2     ,lowercase = 1     ,lowercase = 128     ,lowercase = [2, 2, 2, 2]     ,lowercase = 2     ,lowercase = 2     ,):

             """simple docstring"""





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



       def  A_   (     self):

             """simple docstring"""





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

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

             UpperCAmelCase_  :	int				      =		self.get_config()

             return config, pixel_values, labels



       def  A_   (     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=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  A_   (     self     ,lowercase     ,lowercase     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	Tuple				      =		TFEfficientFormerModel(config=lowercase)
             UpperCAmelCase_  :	Dict				      =		model(lowercase     ,training=lowercase)
             self.parent.assertEqual(result.last_hidden_state.shape     ,(self.batch_size, self.seq_length, self.hidden_size))



       def  A_   (     self     ,lowercase     ,lowercase     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	List[Any]				      =		self.type_sequence_label_size
             UpperCAmelCase_  :	Tuple				      =		TFEfficientFormerForImageClassification(lowercase)
             UpperCAmelCase_  :	Union[str, Any]				      =		model(lowercase     ,labels=lowercase     ,training=lowercase)
             self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.type_sequence_label_size))

             # test greyscale images
             UpperCAmelCase_  :	str				      =		1
             UpperCAmelCase_  :	int				      =		TFEfficientFormerForImageClassification(lowercase)

             UpperCAmelCase_  :	Optional[int]				      =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
             UpperCAmelCase_  :	List[Any]				      =		model(lowercase     ,labels=lowercase)
             self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.type_sequence_label_size))



       def  A_   (     self):

             """simple docstring"""





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


@require_tf
class   snake_case_ (lowercase__ , lowercase__ , unittest.TestCase  ):




       """simple docstring"""






       _lowerCamelCase			    =				(
           (
               TFEfficientFormerModel,
               TFEfficientFormerForImageClassificationWithTeacher,
               TFEfficientFormerForImageClassification,
           )
           if is_tf_available()
           else ()
       )
       _lowerCamelCase			    =				(
           {
               """feature-extraction""": TFEfficientFormerModel,
               """image-classification""": (
                   TFEfficientFormerForImageClassification,
                   TFEfficientFormerForImageClassificationWithTeacher,
               ),
           }
           if is_tf_available()
           else {}
       )

       _lowerCamelCase			    =				False

       _lowerCamelCase			    =				False
       _lowerCamelCase			    =				False
       _lowerCamelCase			    =				False
       _lowerCamelCase			    =				False



       def  A_   (     self):

             """simple docstring"""





             UpperCAmelCase_  :	Dict				      =		TFEfficientFormerModelTester(self)
             UpperCAmelCase_  :	Any				      =		ConfigTester(
                 self     ,config_class=lowercase     ,has_text_modality=lowercase     ,hidden_size=37)



       def  A_   (     self):

             """simple docstring"""





             self.config_tester.run_common_tests()



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

             """simple docstring"""





             pass



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

             """simple docstring"""





             pass



       def  A_   (     self):

             """simple docstring"""





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

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

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



       def  A_   (     self):

             """simple docstring"""





             def check_hidden_states_output(lowercase     ,lowercase     ,lowercase):
                   UpperCAmelCase_  :	str				      =		model_class(lowercase)

                   UpperCAmelCase_  :	List[Any]				      =		model(**self._prepare_for_class(lowercase     ,lowercase)     ,training=lowercase)
                   UpperCAmelCase_  :	Dict				      =		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(lowercase)     ,lowercase)

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

                         self.asseretIsInstance(lowercase     ,(list, tuple))
                         self.assertEqual(len(lowercase)     ,lowercase)
                         UpperCAmelCase_  :	Union[str, Any]				      =		getattr(self.model_tester     ,"seq_length"     ,lowercase)
                         UpperCAmelCase_  :	Optional[int]				      =		getattr(self.model_tester     ,"decoder_seq_length"     ,lowercase)

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

             UpperCAmelCase_  :	List[Any]				      =		self.model_tester.prepare_config_and_inputs_for_common()

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

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

                   check_hidden_states_output(lowercase     ,lowercase     ,lowercase)



       def  A_   (     self     ,lowercase     ,lowercase     ,lowercase=False):

             """simple docstring"""





             UpperCAmelCase_  :	Optional[int]				      =		super()._prepare_for_class(lowercase     ,lowercase     ,return_labels=lowercase)

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

             return inputs_dict



       def  A_   (     self):

             """simple docstring"""





             UpperCAmelCase_  :	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  A_   (     self):

             """simple docstring"""





             UpperCAmelCase_  :	List[str]				      =		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_masked_image_modeling(*lowercase)



       def  A_   (     self):

             """simple docstring"""





             UpperCAmelCase_  :	Optional[int]				      =		self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*lowercase)



       @slow
       def  A_   (     self):

             """simple docstring"""





             for model_name in TF_EFFICIENTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   UpperCAmelCase_  :	Optional[int]				      =		TFEfficientFormerModel.from_pretrained(lowercase)
                   self.assertIsNotNone(lowercase)



       def  A_   (     self):

             """simple docstring"""





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

             UpperCAmelCase_  :	Dict				      =		getattr(self.model_tester     ,"seq_length"     ,lowercase)
             UpperCAmelCase_  :	Tuple				      =		getattr(self.model_tester     ,"encoder_seq_length"     ,lowercase)
             UpperCAmelCase_  :	Dict				      =		getattr(self.model_tester     ,"key_length"     ,lowercase)
             UpperCAmelCase_  :	Optional[Any]				      =		getattr(self.model_tester     ,"chunk_length"     ,lowercase)

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

             for model_class in self.all_model_classes:
                   UpperCAmelCase_  :	Optional[Any]				      =		True
                   UpperCAmelCase_  :	Optional[int]				      =		False
                   UpperCAmelCase_  :	List[Any]				      =		True
                   UpperCAmelCase_  :	List[str]				      =		model_class(lowercase)

                   UpperCAmelCase_  :	str				      =		model(**self._prepare_for_class(lowercase     ,lowercase)     ,training=lowercase)
                   UpperCAmelCase_  :	List[Any]				      =		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"]
                   UpperCAmelCase_  :	Union[str, Any]				      =		True
                   UpperCAmelCase_  :	Optional[Any]				      =		model_class(lowercase)
                   UpperCAmelCase_  :	int				      =		model(**self._prepare_for_class(lowercase     ,lowercase)     ,training=lowercase)

                   UpperCAmelCase_  :	Any				      =		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  A_   (     self):

             """simple docstring"""





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

             for model_class in self.all_model_classes:
                   # Prepare our model
                   UpperCAmelCase_  :	Optional[int]				      =		model_class(lowercase)
                   # These are maximally general inputs for the model, with multiple None dimensions
                   # Hopefully this will catch any conditionals that fail for flexible shapes
                   UpperCAmelCase_  :	str				      =		{
                       key: tf.keras.Input(shape=val.shape[1:]     ,dtype=val.dtype     ,name=lowercase)
                       for key, val in model.input_signature.items()
                       if key in model.dummy_inputs
                   }
                   UpperCAmelCase_  :	Dict				      =		model(lowercase)
                   self.assertTrue(outputs_dict is not None)







def    _snake_case (       )     ->     Dict:






      '''simple docstring'''







      UpperCAmelCase_  :	Dict				      =		Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
      return image





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




       """simple docstring"""






       @cached_property
       def  A_   (     self):

             """simple docstring"""





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



       @slow
       def  A_   (     self):

             """simple docstring"""





             UpperCAmelCase_  :	List[Any]				      =		TFEfficientFormerForImageClassification.from_pretrained("snap-research/efficientformer-l1-300")
             UpperCAmelCase_  :	Any				      =		self.default_image_processor
             UpperCAmelCase_  :	Optional[Any]				      =		prepare_img()
             UpperCAmelCase_  :	Optional[Any]				      =		image_processor(images=lowercase     ,return_tensors="tf")
             # forward pass
             UpperCAmelCase_  :	int				      =		model(**lowercase     ,training=lowercase)
             # verify the logits
             UpperCAmelCase_  :	List[str]				      =		tf.TensorShape((1, 1000))
             self.assertEqual(outputs.logits.shape     ,lowercase)
             UpperCAmelCase_  :	Union[str, Any]				      =		tf.constant([-0.0555, 0.4825, -0.0852])
             self.assertTrue(np.allclose(outputs.logits[0, :3]     ,lowercase     ,atol=1E-4))



       @slow
       def  A_   (     self):

             """simple docstring"""





             UpperCAmelCase_  :	List[str]				      =		TFEfficientFormerForImageClassificationWithTeacher.from_pretrained(
                 "snap-research/efficientformer-l1-300")
             UpperCAmelCase_  :	str				      =		self.default_image_processor
             UpperCAmelCase_  :	Dict				      =		prepare_img()
             UpperCAmelCase_  :	Any				      =		image_processor(images=lowercase     ,return_tensors="tf")
             # forward pass
             UpperCAmelCase_  :	List[str]				      =		model(**lowercase     ,training=lowercase)
             # verify the logits
             UpperCAmelCase_  :	Dict				      =		tf.TensorShape((1, 1000))
             self.assertEqual(outputs.logits.shape     ,lowercase)
             UpperCAmelCase_  :	Tuple				      =		tf.constant([-0.1312, 0.4353, -1.0499])
             self.assertTrue(np.allclose(outputs.logits[0, :3]     ,lowercase     ,atol=1E-4))





import argparse

import torch

from transformers import FunnelBaseModel, FunnelConfig, FunnelModel, load_tf_weights_in_funnel
from transformers.utils import logging


logging.set_verbosity_info()







def    _snake_case (       __snake_case					,  __snake_case					,  __snake_case					,  __snake_case    )     ->     Optional[Any]:






      '''simple docstring'''







      UpperCAmelCase_  :	List[str]				      =		FunnelConfig.from_json_file(__snake_case    )
      print(F"""Building PyTorch model from configuration: {config}"""    )
      UpperCAmelCase_  :	str				      =		FunnelBaseModel(__snake_case    ) if base_model else FunnelModel(__snake_case    )

      # Load weights from tf checkpoint
      load_tf_weights_in_funnel(__snake_case					,  __snake_case					,  __snake_case    )

      # Save pytorch-model
      print(F"""Save PyTorch model to {pytorch_dump_path}"""    )
      torch.save(model.state_dict()					,  __snake_case    )


if __name__ == "__main__":
   __lowerCamelCase         =			argparse.ArgumentParser()
   # Required parameters
   parser.add_argument(
       '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
   )
   parser.add_argument(
       '''--config_file''',
       default=None,
       type=str,
       required=True,
       help='''The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.''',
   )
   parser.add_argument(
       '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
   )
   parser.add_argument(
       '''--base_model''', action='''store_true''', help='''Whether you want just the base model (no decoder) or not.'''
   )
   __lowerCamelCase         =			parser.parse_args()
   convert_tf_checkpoint_to_pytorch(
       args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.base_model
   )
