



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)

A_		:     Optional[int]        =     logging.getLogger()


def 	UpperCAmelCase__      (      UpperCAmelCase__ :Union[str, Any]					):






 '''simple docstring'''

 a						=		{}
 a						=		os.path.join(UpperCAmelCase__ ,					"all_results.json"					)
 if os.path.exists(UpperCAmelCase__					):
  with open(UpperCAmelCase__ ,					"r"					) as f:
   a						=		json.load(UpperCAmelCase__					)
 else:
  raise ValueError(F"""can't find {path}"""					)
 return results


A_		:     Any        =     logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)




@require_torch_tpu
class 		_lowercase		(       UpperCAmelCase__	):

   def 		A							(      self			:       Any							)       ->				Tuple:


    """simple docstring"""






    import xla_spawn

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

    with patch.object(__lowerCAmelCase							,   "argv"							,   __lowerCAmelCase							):
     a						=		time()
     xla_spawn.main()
     a						=		time()

     a						=		get_results(__lowerCAmelCase							)
     self.assertGreaterEqual(result["eval_accuracy"]							,   0.7_5							)

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




   def 		A							(      self			:       Tuple							)       ->				Tuple:


    """simple docstring"""






    import xla_spawn

    a						=		"\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            ".split()
    with patch.object(__lowerCAmelCase							,   "argv"							,   __lowerCAmelCase							):
     xla_spawn.main()






import argparse

from transformers import TaConfig, TaForConditionalGeneration, load_tf_weights_in_ta
from transformers.utils import logging


logging.set_verbosity_info()


def 	UpperCAmelCase__      (      UpperCAmelCase__ :Optional[int] ,					UpperCAmelCase__ :List[str] ,					UpperCAmelCase__ :Any					):






 '''simple docstring'''

 a						=		TaConfig.from_json_file(UpperCAmelCase__					)
 print(F"""Building PyTorch model from configuration: {config}"""					)
 a						=		TaForConditionalGeneration(UpperCAmelCase__					)

 # Load weights from tf checkpoint
 load_tf_weights_in_ta(UpperCAmelCase__ ,					UpperCAmelCase__ ,					UpperCAmelCase__					)

 # Save pytorch-model
 print(F"""Save PyTorch model to {pytorch_dump_path}"""					)
 model.save_pretrained(UpperCAmelCase__					)


if __name__ == "__main__":
     A_		:     Union[str, Any]        =     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 T5 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.'''
     )
     A_		:     Tuple        =     parser.parse_args()
     convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)

