





import unittest

from transformers import TrOCRConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM





@require_torch
class 			A__   :




   """simple docstring"""



   def __init__( self ,  lowercase ,  lowercase=99 ,  lowercase=13 ,  lowercase=16 ,  lowercase=7 ,  lowercase=True ,  lowercase=True ,  lowercase=True ,  lowercase=False ,  lowercase=True ,  lowercase=2 ,  lowercase=32 ,  lowercase=4 ,  lowercase=4 ,  lowercase=30 ,  lowercase=0 ,  lowercase=1 ,  lowercase=2 ,  lowercase=None ,  )      ->						List[Any]:







          '''simple docstring'''

          a__						:     Optional[Any]			=    parent
          a__						:     Any			=    batch_size
          a__						:     Any			=    decoder_seq_length
          # For common tests
          a__						:     Any			=    self.decoder_seq_length
          a__						:     Optional[int]			=    is_training
          a__						:     List[str]			=    use_attention_mask
          a__						:     Tuple			=    use_labels

          a__						:     int			=    vocab_size
          a__						:     Any			=    d_model
          a__						:     Dict			=    d_model
          a__						:     List[str]			=    decoder_layers
          a__						:     Union[str, Any]			=    decoder_layers
          a__						:     int			=    decoder_ffn_dim
          a__						:     List[Any]			=    decoder_attention_heads
          a__						:     Dict			=    decoder_attention_heads
          a__						:     Optional[int]			=    eos_token_id
          a__						:     Dict			=    bos_token_id
          a__						:     List[str]			=    pad_token_id
          a__						:     int			=    decoder_start_token_id
          a__						:     List[Any]			=    use_cache
          a__						:     List[str]			=    max_position_embeddings

          a__						:     Dict			=    None
          a__						:     Union[str, Any]			=    decoder_seq_length
          a__						:     Union[str, Any]			=    2
          a__						:     Union[str, Any]			=    1

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







          '''simple docstring'''

          a__						:     Dict			=    ids_tensor([self.batch_size, self.decoder_seq_length] ,  self.vocab_size)

          a__						:     List[str]			=    None
          if self.use_attention_mask:
                 a__						:     Optional[int]			=    ids_tensor([self.batch_size, self.decoder_seq_length] ,  vocab_size=2)

          a__						:     Union[str, Any]			=    None
          if self.use_labels:
                 a__						:     List[str]			=    ids_tensor([self.batch_size, self.decoder_seq_length] ,  self.vocab_size)

          a__						:     Union[str, Any]			=    TrOCRConfig(
              vocab_size=self.vocab_size ,  d_model=self.d_model ,  decoder_layers=self.decoder_layers ,  decoder_ffn_dim=self.decoder_ffn_dim ,  decoder_attention_heads=self.decoder_attention_heads ,  eos_token_id=self.eos_token_id ,  bos_token_id=self.bos_token_id ,  use_cache=self.use_cache ,  pad_token_id=self.pad_token_id ,  decoder_start_token_id=self.decoder_start_token_id ,  max_position_embeddings=self.max_position_embeddings ,  )

          return (config, input_ids, attention_mask, lm_labels)

   def 							__lowercase		( self ,  lowercase ,  lowercase ,  lowercase ,  lowercase ,  )      ->						str:







          '''simple docstring'''

          a__						:     Optional[int]			=    True
          a__						:     List[Any]			=    TrOCRDecoder(config=snake_case__).to(snake_case__).eval()
          a__						:     Dict			=    input_ids[:2]

          input_ids[input_ids == 0] += 1
          # first forward pass
          a__						:     List[str]			=    model(snake_case__ ,  use_cache=snake_case__)
          a__						:     Any			=    model(snake_case__)
          a__						:     Any			=    model(snake_case__ ,  use_cache=snake_case__)

          self.parent.assertTrue(len(snake_case__) == len(snake_case__))
          self.parent.assertTrue(len(snake_case__) == len(snake_case__) + 1)

          a__						:     List[Any]			=    outputs["past_key_values"]

          # create hypothetical next token and extent to next_input_ids
          a__						:     Optional[Any]			=    ids_tensor((2, 1) ,  config.vocab_size - 1) + 1

          # append to next input_ids and
          a__						:     Union[str, Any]			=    torch.cat([input_ids, next_tokens] ,  dim=-1)

          a__						:     str			=    model(snake_case__)["last_hidden_state"]
          a__						:     str			=    model(snake_case__ ,  past_key_values=snake_case__)["last_hidden_state"]

          # select random slice
          a__						:     int			=    ids_tensor((1,) ,  output_from_past.shape[-1]).item()
          a__						:     str			=    output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
          a__						:     Optional[Any]			=    output_from_past[:, 0, random_slice_idx].detach()

          # test that outputs are equal for slice
          assert torch.allclose(snake_case__ ,  snake_case__ ,  atol=1e-3)

   def 							__lowercase		( self)      ->						Tuple:







          '''simple docstring'''

          a__						:     List[Any]			=    self.prepare_config_and_inputs()
          a__						:     Dict			=    config_and_inputs

          a__						:     List[Any]			=    {"input_ids": input_ids, "attention_mask": attention_mask}
          return config, inputs_dict





@require_torch
class 			A__   (							A_			,  A_			,  A_			,  unittest.TestCase     ):




   """simple docstring"""



   __A       :    int	  =  (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
   __A       :    Union[str, Any]	  =  (TrOCRForCausalLM,) if is_torch_available() else ()
   __A       :    int	  =  {"text-generation": TrOCRForCausalLM} if is_torch_available() else {}
   __A       :    int	  =  True
   __A       :    Optional[Any]	  =  False

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







          '''simple docstring'''

          a__						:     Optional[Any]			=    TrOCRStandaloneDecoderModelTester(self ,  is_training=snake_case__)
          a__						:     int			=    ConfigTester(self ,  config_class=snake_case__)

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







          '''simple docstring'''

          pass

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







          '''simple docstring'''

          pass

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







          '''simple docstring'''

          pass

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







          '''simple docstring'''

          self.config_tester.run_common_tests()

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







          '''simple docstring'''

          a__						:     Any			=    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_decoder_model_past(*snake_case__)

   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          return

   @unittest.skip('The model doesn\'t support left padding')  # and it's not used enough to be worth fixing :)
   def 							__lowercase		( self)      ->						Any:







          '''simple docstring'''

          pass









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)


