


import json
import logging
import os
import sys
from time import time
from unittest.mock import patch

from transformers.testing_utils import TestCasePlus, require_torch_tpu


logging.basicConfig(level=logging.DEBUG)

SCREAMING_SNAKE_CASE       :		Union[str, Any]         =				logging.getLogger()




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





     lowercase_       :Tuple     =       {}
     lowercase_       :Dict     =       os.path.join(_a       ,		'''all_results.json'''		)
     if os.path.exists(_a		):
          with open(_a       ,		'''r'''		) as f:
               lowercase_       :int     =       json.load(_a		)
     else:
          raise ValueError(f"can't find {path}"		)
     return results


SCREAMING_SNAKE_CASE       :		List[Any]         =				logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)






@require_torch_tpu
class      UpperCamelCase					(		lowercase__     ):


   '''simple docstring'''







   def 		UpperCamelCase  (     self		):
        import xla_spawn

        lowercase_       :List[Any]     =       self.get_auto_remove_tmp_dir()
        lowercase_       :Any     =       f"\n            ./examples/pytorch/text-classification/run_glue.py\n            --num_cores=8\n            ./examples/pytorch/text-classification/run_glue.py\n            --model_name_or_path distilbert-base-uncased\n            --output_dir {tmp_dir}\n            --overwrite_output_dir\n            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n            --do_train\n            --do_eval\n            --debug tpu_metrics_debug\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --learning_rate=1e-4\n            --max_steps=10\n            --warmup_steps=2\n            --seed=42\n            --max_seq_length=128\n            ".split()

        with patch.object(UpperCamelCase_   ,							'''argv'''   ,							UpperCamelCase_		):
             lowercase_       :Tuple     =       time()
             xla_spawn.main()
             lowercase_       :str     =       time()

             lowercase_       :Optional[int]     =       get_results(UpperCamelCase_		)
             self.assertGreaterEqual(result['''eval_accuracy''']   ,							0.75		)

             # Assert that the script takes less than 500 seconds to make sure it doesn't hang.
             self.assertLess(end - start   ,							500		)




   def 		UpperCamelCase  (     self		):
        import xla_spawn

        lowercase_       :Dict     =       '''
            ./tests/test_trainer_tpu.py
            --num_cores=8
            ./tests/test_trainer_tpu.py
            '''.split()
        with patch.object(UpperCamelCase_   ,							'''argv'''   ,							UpperCamelCase_		):
             xla_spawn.main()





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
   )

