

"""simple docstring"""
import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
 import torch

 from transformers import GPTaLMHeadModel

@require_torch
@require_sentencepiece
@require_tokenizers
class        _UpperCamelCase	(						unittest.TestCase	):


  """simple docstring"""





  @slow
  @unittest.skip('''Model is not available.'''      )
  def 							_UpperCAmelCase      ( self					: List[Any]      )	->			int:


         '''simple docstring'''





         __magic_name__			:		Dict        =     '''nvidia/megatron-gpt2-345m/'''
         if "MYDIR" in os.environ:
                __magic_name__			:		List[str]        =     os.path.join(os.environ['''MYDIR''']       ,       snake_case      )
         __magic_name__			:		Tuple        =     GPTaLMHeadModel.from_pretrained(snake_case      )
         model.to(snake_case      )
         model.half()

         __magic_name__			:		int        =     torch.tensor(
             [[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]       ,       device=snake_case       ,       dtype=torch.long       ,       )

         with torch.no_grad():
                __magic_name__			:		Optional[Any]        =     model(snake_case      ).logits

         __magic_name__			:		Union[str, Any]        =     torch.Size((1, 9, 5_0257)      )
         self.assertEqual(output.shape       ,       snake_case      )

         __magic_name__			:		Dict        =     torch.tensor(
             [
                 4.9414,
                 -0.2920,
                 -1.2148,
                 -4.0273,
                 -0.5161,
                 -5.2109,
                 -1.2412,
                 -1.8301,
                 -1.7734,
                 -4.7148,
                 -0.2317,
                 -1.0811,
                 -2.1777,
                 0.4141,
                 -3.7969,
                 -4.0586,
                 -2.5332,
                 -3.3809,
                 4.3867,
             ]       ,       device=snake_case       ,       dtype=torch.half       ,       )

         for i in range(19      ):
                __magic_name__       ,							__magic_name__			:		Dict        =     8 * i // 17, 2792 * i  # along the diagonal
                __magic_name__       ,							__magic_name__			:		Optional[int]        =     output[0, r, c], expected_diag[i]
                __magic_name__			:		Any        =     f"""row={r} col={c} computed={computed} expected={expected}"""
                self.assertAlmostEqual(snake_case       ,       snake_case       ,       delta=1e-4       ,       msg=snake_case      )



"""simple docstring"""
import argparse

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


logging.set_verbosity_info()







def       UpperCamelCase_					(     lowerCamelCase							:					List[str]     , lowerCamelCase							:					Optional[int]     , lowerCamelCase							:					List[str]   )						->     Optional[int]:



       """simple docstring"""







       __magic_name__			:		int        =     TaConfig.from_json_file(lowerCamelCase   )
       print(f"""Building PyTorch model from configuration: {config}"""   )
       __magic_name__			:		Union[str, Any]        =     TaForConditionalGeneration(lowerCamelCase   )

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

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


if __name__ == "__main__":
 A									=						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									=						parser.parse_args()
 convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path)
