


from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_torch_available():
   import torch

if is_torch_tpu_available(check_device=False):
   import torch_xla.core.xla_model as xm


SCREAMING_SNAKE_CASE       :		str         =				logging.get_logger(__name__)






@dataclass
class      UpperCamelCase					(		lowercase__     ):


   '''simple docstring'''







   lowercase					:	Optional[Any]									=[
       """no_inference""",
       """no_cuda""",
       """no_tpu""",
       """no_speed""",
       """no_memory""",
       """no_env_print""",
       """no_multi_process""",
   ]
   def __init__(     self   ,							**UpperCamelCase_		):
        for deprecated_arg in self.deprecated_args:
             if deprecated_arg in kwargs:
                  lowercase_       :Dict     =       deprecated_arg[3:]
                  setattr(self   ,							UpperCamelCase_   ,							not kwargs.pop(UpperCamelCase_		)		)
                  logger.warning(
                      f"{deprecated_arg} is depreciated. Please use --no_{positive_arg} or"
                      f" {positive_arg}={kwargs[positive_arg]}"		)

        lowercase_       :Tuple     =       kwargs.pop('''torchscript'''   ,							self.torchscript		)
        lowercase_       :List[str]     =       kwargs.pop('''torch_xla_tpu_print_metrics'''   ,							self.torch_xla_tpu_print_metrics		)
        lowercase_       :Union[str, Any]     =       kwargs.pop('''fp16_opt_level'''   ,							self.fpaa_opt_level		)
        super().__init__(**UpperCamelCase_		)

   lowercase					:	bool									=field(default=lowercase__						,      metadata={"""help""": """Trace the models using torchscript"""}     )
   lowercase					:	bool									=field(default=lowercase__						,      metadata={"""help""": """Print Xla/PyTorch tpu metrics"""}     )
   lowercase					:	str									=field(
       default="""O1"""						,      metadata={
           """help""": (
               """For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']. """
               """See details at https://nvidia.github.io/apex/amp.html"""
           )
       }						,      )
   @cached_property
   def 		UpperCamelCase  (     self		):
        requires_backends(self   ,							['''torch''']		)
        logger.info('''PyTorch: setting up devices'''		)
        if not self.cuda:
             lowercase_       :Union[str, Any]     =       torch.device('''cpu'''		)
             lowercase_       :List[Any]     =       0
        elif is_torch_tpu_available():
             lowercase_       :Any     =       xm.xla_device()
             lowercase_       :Union[str, Any]     =       0
        else:
             lowercase_       :Optional[int]     =       torch.device('''cuda''' if torch.cuda.is_available() else '''cpu'''		)
             lowercase_       :Optional[int]     =       torch.cuda.device_count()
        return device, n_gpu
   @property
   def 		UpperCamelCase  (     self		):
        return is_torch_tpu_available() and self.tpu
   @property
   def 		UpperCamelCase  (     self		):
        requires_backends(self   ,							['''torch''']		)
        # TODO(PVP): currently only single GPU is supported
        return torch.cuda.current_device()
   @property
   def 		UpperCamelCase  (     self		):
        requires_backends(self   ,							['''torch''']		)
        return self._setup_devices[0]
   @property
   def 		UpperCamelCase  (     self		):
        requires_backends(self   ,							['''torch''']		)
        return self._setup_devices[1]




   @property
   def 		UpperCamelCase  (     self		):
        return self.n_gpu > 0





import argparse

import torch

from transformers import OpenAIGPTConfig, OpenAIGPTModel, load_tf_weights_in_openai_gpt
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()




def     UpperCamelCase    (      _a       ,		_a       ,		_a		)					->							List[str]:
     '''simple docstring'''





     if openai_config_file == "":
          lowercase_       :str     =       OpenAIGPTConfig()
     else:
          lowercase_       :int     =       OpenAIGPTConfig.from_json_file(_a		)
     lowercase_       :int     =       OpenAIGPTModel(_a		)

     # Load weights from numpy
     load_tf_weights_in_openai_gpt(_a       ,		_a       ,		_a		)

     # Save pytorch-model
     lowercase_       :Optional[int]     =       pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME
     lowercase_       :List[str]     =       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__":
   SCREAMING_SNAKE_CASE       :		List[str]         =				argparse.ArgumentParser()
   # Required parameters
   parser.add_argument(
       "--openai_checkpoint_folder_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(
       "--openai_config_file",
       default="",
       type=str,
       help=(
           "An optional config json file corresponding to the pre-trained OpenAI model. \n"
           "This specifies the model architecture."
       ),
   )
   SCREAMING_SNAKE_CASE       :		Optional[Any]         =				parser.parse_args()
   convert_openai_checkpoint_to_pytorch(
       args.openai_checkpoint_folder_path, args.openai_config_file, args.pytorch_dump_folder_path
   )

