
"""simple docstring"""




from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
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 TFViTForImageClassification, TFViTModel


if is_vision_available():
    from PIL import Image

    from transformers import ViTImageProcessor







class   _lowercase						:


     '''simple docstring'''





     def __init__(      self ,			__UpperCamelCase ,			__UpperCamelCase=13 ,			__UpperCamelCase=30 ,			__UpperCamelCase=2 ,			__UpperCamelCase=3 ,			__UpperCamelCase=True ,			__UpperCamelCase=True ,			__UpperCamelCase=32 ,			__UpperCamelCase=2 ,			__UpperCamelCase=4 ,			__UpperCamelCase=37 ,			__UpperCamelCase="gelu" ,			__UpperCamelCase=0.1 ,			__UpperCamelCase=0.1 ,			__UpperCamelCase=10 ,			__UpperCamelCase=0.02 ,			__UpperCamelCase=3 ,			__UpperCamelCase=None ,			)->					Optional[int]:
          UpperCAmelCase__							: int 							=						parent
          UpperCAmelCase__							: str 							=						batch_size
          UpperCAmelCase__							: Optional[Any] 							=						image_size
          UpperCAmelCase__							: Union[str, Any] 							=						patch_size
          UpperCAmelCase__							: Union[str, Any] 							=						num_channels
          UpperCAmelCase__							: Tuple 							=						is_training
          UpperCAmelCase__							: Union[str, Any] 							=						use_labels
          UpperCAmelCase__							: str 							=						hidden_size
          UpperCAmelCase__							: Tuple 							=						num_hidden_layers
          UpperCAmelCase__							: List[Any] 							=						num_attention_heads
          UpperCAmelCase__							: Tuple 							=						intermediate_size
          UpperCAmelCase__							: Dict 							=						hidden_act
          UpperCAmelCase__							: Union[str, Any] 							=						hidden_dropout_prob
          UpperCAmelCase__							: Tuple 							=						attention_probs_dropout_prob
          UpperCAmelCase__							: Optional[Any] 							=						type_sequence_label_size
          UpperCAmelCase__							: Tuple 							=						initializer_range
          UpperCAmelCase__							: Optional[int] 							=						scope

          # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
          UpperCAmelCase__							: int 							=						(image_size // patch_size) ** 2
          UpperCAmelCase__							: Optional[Any] 							=						num_patches + 1



     def  lowerCAmelCase__       (      self  )->					List[Any]:
          UpperCAmelCase__							: int 							=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

          UpperCAmelCase__							: List[Any] 							=						None
          if self.use_labels:
               UpperCAmelCase__							: int 							=						ids_tensor([self.batch_size] ,			self.type_sequence_label_size  )

          UpperCAmelCase__							: List[str] 							=						self.get_config()

          return config, pixel_values, labels



     def  lowerCAmelCase__       (      self  )->					Any:
          return ViTConfig(
              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 ,			)



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase  )->					Any:
          UpperCAmelCase__							: List[Any] 							=						TFViTModel(config=lowerCAmelCase_  )
          UpperCAmelCase__							: str 							=						model(lowerCAmelCase_ ,			training=lowerCAmelCase_  )
          self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)  )

          # Test with an image with different size than the one specified in config.
          UpperCAmelCase__							: Optional[Any] 							=						self.image_size // 2
          UpperCAmelCase__							: Tuple 							=						pixel_values[:, :, :image_size, :image_size]
          UpperCAmelCase__							: Union[str, Any] 							=						model(lowerCAmelCase_ ,			interpolate_pos_encoding=lowerCAmelCase_ ,			training=lowerCAmelCase_  )
          UpperCAmelCase__							: Tuple 							=						(image_size // self.patch_size) ** 2 + 1
          self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, seq_length, self.hidden_size)  )



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase  )->					Any:
          UpperCAmelCase__							: Union[str, Any] 							=						self.type_sequence_label_size
          UpperCAmelCase__							: int 							=						TFViTForImageClassification(lowerCAmelCase_  )
          UpperCAmelCase__							: Any 							=						model(lowerCAmelCase_ ,			labels=lowerCAmelCase_ ,			training=lowerCAmelCase_  )
          self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.type_sequence_label_size)  )

          # Test with an image with different size than the one specified in config.
          UpperCAmelCase__							: Tuple 							=						self.image_size // 2
          UpperCAmelCase__							: Any 							=						pixel_values[:, :, :image_size, :image_size]
          UpperCAmelCase__							: str 							=						model(lowerCAmelCase_ ,			interpolate_pos_encoding=lowerCAmelCase_ ,			training=lowerCAmelCase_  )
          self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.type_sequence_label_size)  )

          # test greyscale images
          UpperCAmelCase__							: Optional[int] 							=						1
          UpperCAmelCase__							: int 							=						TFViTForImageClassification(lowerCAmelCase_  )
          UpperCAmelCase__							: List[Any] 							=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
          UpperCAmelCase__							: Optional[int] 							=						model(lowerCAmelCase_  )
          self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.type_sequence_label_size)  )



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







@require_tf
class   _lowercase						(       __UpperCAmelCase	,   __UpperCAmelCase	,   unittest.TestCase				):


     '''simple docstring'''





     _A								=							(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
     _A								=							(
         {'feature-extraction': TFViTModel, 'image-classification': TFViTForImageClassification}
         if is_tf_available()
         else {}
     )

     _A								=							False
     _A								=							False
     _A								=							False



     def  lowerCAmelCase__       (      self  )->					str:
          UpperCAmelCase__							: List[Any] 							=						TFViTModelTester(self  )
          UpperCAmelCase__							: Tuple 							=						ConfigTester(self ,			config_class=lowerCAmelCase_ ,			has_text_modality=lowerCAmelCase_ ,			hidden_size=37  )



     def  lowerCAmelCase__       (      self  )->					int:
          self.config_tester.run_common_tests()



     @unittest.skip(reason="ViT does not use inputs_embeds"  )
     def  lowerCAmelCase__       (      self  )->					Dict:
          pass



     @unittest.skip(reason="ViT does not use inputs_embeds"  )
     def  lowerCAmelCase__       (      self  )->					str:
          pass



     def  lowerCAmelCase__       (      self  )->					List[Any]:
          UpperCAmelCase__							,	UpperCAmelCase__							: Optional[Any] 							=						self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               UpperCAmelCase__							: List[str] 							=						model_class(lowerCAmelCase_  )
               self.assertIsInstance(model.get_input_embeddings() ,			(tf.keras.layers.Layer)  )
               UpperCAmelCase__							: Dict 							=						model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(lowerCAmelCase_ ,			tf.keras.layers.Layer  )  )



     def  lowerCAmelCase__       (      self  )->					Union[str, Any]:
          UpperCAmelCase__							,	UpperCAmelCase__							: List[Any] 							=						self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               UpperCAmelCase__							: Dict 							=						model_class(lowerCAmelCase_  )
               UpperCAmelCase__							: List[str] 							=						inspect.signature(model.call  )
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               UpperCAmelCase__							: Tuple 							=						[*signature.parameters.keys()]

               UpperCAmelCase__							: str 							=						["pixel_values"]
               self.assertListEqual(arg_names[:1] ,			lowerCAmelCase_  )



     def  lowerCAmelCase__       (      self  )->					str:
          UpperCAmelCase__							: str 							=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowerCAmelCase_  )



     def  lowerCAmelCase__       (      self  )->					int:
          UpperCAmelCase__							: List[str] 							=						self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_  )



     @slow
     def  lowerCAmelCase__       (      self  )->					List[str]:
          UpperCAmelCase__							: Any 							=						TFViTModel.from_pretrained("google/vit-base-patch16-224"  )
          self.assertIsNotNone(lowerCAmelCase_  )





def 							a__ (				):






     '''simple docstring'''
     UpperCAmelCase__							: List[Any] 							=						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
     return image







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


     '''simple docstring'''





     @cached_property
     def  lowerCAmelCase__       (      self  )->					str:
          return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"  ) if is_vision_available() else None



     @slow
     def  lowerCAmelCase__       (      self  )->					Dict:
          UpperCAmelCase__							: Any 							=						TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224"  )

          UpperCAmelCase__							: int 							=						self.default_image_processor
          UpperCAmelCase__							: str 							=						prepare_img()
          UpperCAmelCase__							: Optional[Any] 							=						image_processor(images=lowerCAmelCase_ ,			return_tensors="tf"  )

          # forward pass
          UpperCAmelCase__							: Tuple 							=						model(**lowerCAmelCase_  )

          # verify the logits
          UpperCAmelCase__							: Optional[Any] 							=						tf.TensorShape((1, 10_00)  )
          self.assertEqual(outputs.logits.shape ,			lowerCAmelCase_  )

          UpperCAmelCase__							: Any 							=						tf.constant([-0.2744, 0.8215, -0.0836]  )

          tf.debugging.assert_near(outputs.logits[0, :3] ,			lowerCAmelCase_ ,			atol=1E-4  )







"""simple docstring"""




import argparse

import torch

from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()



def 							a__ (				lowerCAmelCase	:       List[Any]       ,    lowerCAmelCase	:       Any       ,    lowerCAmelCase	:       List[Any]		):






     '''simple docstring'''
     # Construct model
     if gpta_config_file == "":
          UpperCAmelCase__							: Optional[int] 							=						GPTaConfig()
     else:
          UpperCAmelCase__							: Dict 							=						GPTaConfig.from_json_file(lowerCAmelCase		)
     UpperCAmelCase__							: Optional[Any] 							=						GPTaModel(lowerCAmelCase		)

     # Load weights from numpy
     load_tf_weights_in_gpta(lowerCAmelCase       ,    lowerCAmelCase       ,    lowerCAmelCase		)

     # Save pytorch-model
     UpperCAmelCase__							: Optional[int] 							=						pytorch_dump_folder_path + "/" + WEIGHTS_NAME
     UpperCAmelCase__							: Any 							=						pytorch_dump_folder_path + "/" + CONFIG_NAME
     print(F"Save PyTorch model to {pytorch_weights_dump_path}"		)
     torch.save(model.state_dict()       ,    lowerCAmelCase		)
     print(F"Save configuration file to {pytorch_config_dump_path}"		)
     with open(lowerCAmelCase       ,    "w"       ,    encoding="utf-8"		) as f:
          f.write(config.to_json_string()		)


if __name__ == "__main__":
    A__      :	Optional[int]          =    argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        """--gpt2_checkpoint_path""", default=None, type=str, required=True, help="""Path to the TensorFlow checkpoint path."""
    )
    parser.add_argument(
        """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
    )
    parser.add_argument(
        """--gpt2_config_file""",
        default="""""",
        type=str,
        help=(
            """An optional config json file corresponding to the pre-trained OpenAI model. \n"""
            """This specifies the model architecture."""
        ),
    )
    A__      :	Optional[Any]          =    parser.parse_args()
    convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)



