





import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402





class 			A__   (							unittest.TestCase     ):




   """simple docstring"""



   def 							__lowercase		( self)      ->						Union[str, Any]:







          '''simple docstring'''

          a__						:     Optional[int]			=    0

   def 							__lowercase		( self)      ->						str:







          '''simple docstring'''

          a__						:     int			=    AutoImageProcessor.from_pretrained('openai/clip-vit-base-patch32')
          self.assertIsInstance(__UpperCamelCase ,  __UpperCamelCase)

   def 							__lowercase		( self)      ->						List[Any]:







          '''simple docstring'''

          with tempfile.TemporaryDirectory() as tmpdirname:
                 a__						:     Dict			=    Path(__UpperCamelCase) / 'preprocessor_config.json'
                 a__						:     Dict			=    Path(__UpperCamelCase) / 'config.json'
                 json.dump(
                     {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'} ,  open(__UpperCamelCase ,  'w') ,  )
                 json.dump({'model_type': 'clip'} ,  open(__UpperCamelCase ,  'w'))

                 a__						:     str			=    AutoImageProcessor.from_pretrained(__UpperCamelCase)
                 self.assertIsInstance(__UpperCamelCase ,  __UpperCamelCase)

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          with tempfile.TemporaryDirectory() as tmpdirname:
                 a__						:     List[str]			=    Path(__UpperCamelCase) / 'preprocessor_config.json'
                 a__						:     Optional[Any]			=    Path(__UpperCamelCase) / 'config.json'
                 json.dump(
                     {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'} ,  open(__UpperCamelCase ,  'w') ,  )
                 json.dump({'model_type': 'clip'} ,  open(__UpperCamelCase ,  'w'))

                 a__						:     Tuple			=    AutoImageProcessor.from_pretrained(__UpperCamelCase)
                 self.assertIsInstance(__UpperCamelCase ,  __UpperCamelCase)

   def 							__lowercase		( self)      ->						Union[str, Any]:







          '''simple docstring'''

          with tempfile.TemporaryDirectory() as tmpdirname:
                 a__						:     str			=    CLIPConfig()

                 # Create a dummy config file with image_proceesor_type
                 a__						:     List[Any]			=    Path(__UpperCamelCase) / 'preprocessor_config.json'
                 a__						:     Dict			=    Path(__UpperCamelCase) / 'config.json'
                 json.dump(
                     {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'} ,  open(__UpperCamelCase ,  'w') ,  )
                 json.dump({'model_type': 'clip'} ,  open(__UpperCamelCase ,  'w'))

                 # remove image_processor_type to make sure config.json alone is enough to load image processor locally
                 a__						:     List[str]			=    AutoImageProcessor.from_pretrained(__UpperCamelCase).to_dict()

                 config_dict.pop('image_processor_type')
                 a__						:     Optional[int]			=    CLIPImageProcessor(**__UpperCamelCase)

                 # save in new folder
                 model_config.save_pretrained(__UpperCamelCase)
                 config.save_pretrained(__UpperCamelCase)

                 a__						:     Tuple			=    AutoImageProcessor.from_pretrained(__UpperCamelCase)

                 # make sure private variable is not incorrectly saved
                 a__						:     Optional[Any]			=    json.loads(config.to_json_string())
                 self.assertTrue('_processor_class' not in dict_as_saved)

          self.assertIsInstance(__UpperCamelCase ,  __UpperCamelCase)

   def 							__lowercase		( self)      ->						Union[str, Any]:







          '''simple docstring'''

          with tempfile.TemporaryDirectory() as tmpdirname:
                 a__						:     Dict			=    Path(__UpperCamelCase) / 'preprocessor_config.json'
                 json.dump(
                     {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'} ,  open(__UpperCamelCase ,  'w') ,  )

                 a__						:     int			=    AutoImageProcessor.from_pretrained(__UpperCamelCase)
                 self.assertIsInstance(__UpperCamelCase ,  __UpperCamelCase)

   def 							__lowercase		( self)      ->						List[str]:







          '''simple docstring'''

          with self.assertRaisesRegex(
              __UpperCamelCase ,  'clip-base is not a local folder and is not a valid model identifier'):
                 a__						:     List[Any]			=    AutoImageProcessor.from_pretrained('clip-base')

   def 							__lowercase		( self)      ->						List[str]:







          '''simple docstring'''

          with self.assertRaisesRegex(
              __UpperCamelCase ,  r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'):
                 a__						:     str			=    AutoImageProcessor.from_pretrained(__UpperCamelCase ,  revision='aaaaaa')

   def 							__lowercase		( self)      ->						Optional[Any]:







          '''simple docstring'''

          with self.assertRaisesRegex(
              __UpperCamelCase ,  'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.' ,  ):
                 a__						:     List[str]			=    AutoImageProcessor.from_pretrained('hf-internal-testing/config-no-model')

   def 							__lowercase		( self)      ->						Union[str, Any]:







          '''simple docstring'''

          with self.assertRaises(__UpperCamelCase):
                 a__						:     Optional[int]			=    AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor')
          # If remote code is disabled, we can't load this config.
          with self.assertRaises(__UpperCamelCase):
                 a__						:     List[str]			=    AutoImageProcessor.from_pretrained(
                     'hf-internal-testing/test_dynamic_image_processor' ,  trust_remote_code=__UpperCamelCase)

          a__						:     Any			=    AutoImageProcessor.from_pretrained(
              'hf-internal-testing/test_dynamic_image_processor' ,  trust_remote_code=__UpperCamelCase)
          self.assertEqual(image_processor.__class__.__name__ ,  'NewImageProcessor')

          # Test image processor can be reloaded.
          with tempfile.TemporaryDirectory() as tmp_dir:
                 image_processor.save_pretrained(__UpperCamelCase)
                 a__						:     Dict			=    AutoImageProcessor.from_pretrained(__UpperCamelCase ,  trust_remote_code=__UpperCamelCase)
          self.assertEqual(reloaded_image_processor.__class__.__name__ ,  'NewImageProcessor')

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          try:
                 AutoConfig.register('custom' ,  __UpperCamelCase)
                 AutoImageProcessor.register(__UpperCamelCase ,  __UpperCamelCase)
                 # Trying to register something existing in the Transformers library will raise an error
                 with self.assertRaises(__UpperCamelCase):
                        AutoImageProcessor.register(__UpperCamelCase ,  __UpperCamelCase)

                 with tempfile.TemporaryDirectory() as tmpdirname:
                        a__						:     Union[str, Any]			=    Path(__UpperCamelCase) / 'preprocessor_config.json'
                        a__						:     int			=    Path(__UpperCamelCase) / 'config.json'
                        json.dump(
                            {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'} ,  open(__UpperCamelCase ,  'w') ,  )
                        json.dump({'model_type': 'clip'} ,  open(__UpperCamelCase ,  'w'))

                        a__						:     Dict			=    CustomImageProcessor.from_pretrained(__UpperCamelCase)

                 # Now that the config is registered, it can be used as any other config with the auto-API
                 with tempfile.TemporaryDirectory() as tmp_dir:
                        image_processor.save_pretrained(__UpperCamelCase)
                        a__						:     List[str]			=    AutoImageProcessor.from_pretrained(__UpperCamelCase)
                        self.assertIsInstance(__UpperCamelCase ,  __UpperCamelCase)

          finally:
                 if "custom" in CONFIG_MAPPING._extra_content:
                        del CONFIG_MAPPING._extra_content["custom"]
                 if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
                        del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]

   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          class 			A__   (							_lowercase     ):




             """simple docstring"""



             __A       :    str	  =  True

          try:
                 AutoConfig.register('custom' ,  __UpperCamelCase)
                 AutoImageProcessor.register(__UpperCamelCase ,  __UpperCamelCase)
                 # If remote code is not set, the default is to use local
                 a__						:     Union[str, Any]			=    AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor')
                 self.assertEqual(image_processor.__class__.__name__ ,  'NewImageProcessor')
                 self.assertTrue(image_processor.is_local)

                 # If remote code is disabled, we load the local one.
                 a__						:     Optional[Any]			=    AutoImageProcessor.from_pretrained(
                     'hf-internal-testing/test_dynamic_image_processor' ,  trust_remote_code=__UpperCamelCase)
                 self.assertEqual(image_processor.__class__.__name__ ,  'NewImageProcessor')
                 self.assertTrue(image_processor.is_local)

                 # If remote is enabled, we load from the Hub
                 a__						:     Optional[int]			=    AutoImageProcessor.from_pretrained(
                     'hf-internal-testing/test_dynamic_image_processor' ,  trust_remote_code=__UpperCamelCase)
                 self.assertEqual(image_processor.__class__.__name__ ,  'NewImageProcessor')
                 self.assertTrue(not hasattr(__UpperCamelCase ,  'is_local'))

          finally:
                 if "custom" in CONFIG_MAPPING._extra_content:
                        del CONFIG_MAPPING._extra_content["custom"]
                 if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
                        del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]









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_     (		A__		,				A__		,				A__    )  ->		Any:
       # Construct model
       if gpta_config_file == "":
              a__						:     Optional[int]			=    GPTaConfig()
       else:
              a__						:     List[str]			=    GPTaConfig.from_json_file(A__    )
       a__						:     List[str]			=    GPTaModel(A__    )

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

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


if __name__ == "__main__":
    lowercase				:	Dict      						=  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."""
        ),
    )
    lowercase				:	int      						=  parser.parse_args()
    convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)


