




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  __magic_name__  :





   """simple docstring"""




   def __init__(	self					:  List[Any]		,  _lowercase					:  Tuple		,  _lowercase					:  Tuple=99		,  _lowercase					:  List[str]=13		,  _lowercase					:  Optional[Any]=16		,  _lowercase					:  Union[str, Any]=7		,  _lowercase					:  int=True		,  _lowercase					:  Dict=True		,  _lowercase					:  List[str]=True		,  _lowercase					:  Union[str, Any]=False		,  _lowercase					:  Optional[Any]=True		,  _lowercase					:  Tuple=2		,  _lowercase					:  str=32		,  _lowercase					:  Dict=4		,  _lowercase					:  int=4		,  _lowercase					:  int=30		,  _lowercase					:  Any=0		,  _lowercase					:  str=1		,  _lowercase					:  List[str]=2		,  _lowercase					:  Optional[int]=None		,  ):



    """simple docstring"""



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

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

    _UpperCamelCase:     Union[str, Any]							= None
    _UpperCamelCase:     Tuple							= decoder_seq_length
    _UpperCamelCase:     int							= 2
    _UpperCamelCase:     Optional[Any]							= 1



   def   lowerCAmelCase						(	self					:  Any							):



    """simple docstring"""



    _UpperCamelCase:     int							= ids_tensor([self.batch_size, self.decoder_seq_length]		,  self.vocab_size							)

    _UpperCamelCase:     Dict							= None
    if self.use_attention_mask:
     _UpperCamelCase:     Optional[Any]							= ids_tensor([self.batch_size, self.decoder_seq_length]		,  vocab_size=2							)

    _UpperCamelCase:     Tuple							= None
    if self.use_labels:
     _UpperCamelCase:     List[Any]							= ids_tensor([self.batch_size, self.decoder_seq_length]		,  self.vocab_size							)

    _UpperCamelCase:     int							= 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   lowerCAmelCase						(	self					:  Any		,  _lowercase					:  Tuple		,  _lowercase					:  Any		,  _lowercase					:  str		,  _lowercase					:  Any		,  ):



    """simple docstring"""



    _UpperCamelCase:     Optional[int]							= True
    _UpperCamelCase:     Optional[int]							= TrOCRDecoder(config=lowercase__							).to(lowercase__							).eval()
    _UpperCamelCase:     List[str]							= input_ids[:2]

    input_ids[input_ids == 0] += 1
    # first forward pass
    _UpperCamelCase:     Tuple							= model(lowercase__		,  use_cache=lowercase__							)
    _UpperCamelCase:     Optional[Any]							= model(lowercase__							)
    _UpperCamelCase:     Optional[Any]							= model(lowercase__		,  use_cache=lowercase__							)

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

    _UpperCamelCase:     List[str]							= outputs["""past_key_values"""]

    # create hypothetical next token and extent to next_input_ids
    _UpperCamelCase:     int							= ids_tensor((2, 1)		,  config.vocab_size - 1							) + 1

    # append to next input_ids and
    _UpperCamelCase:     Dict							= torch.cat([input_ids, next_tokens]		,  dim=-1							)

    _UpperCamelCase:     Optional[Any]							= model(lowercase__							)["""last_hidden_state"""]
    _UpperCamelCase:     List[Any]							= model(lowercase__		,  past_key_values=lowercase__							)["""last_hidden_state"""]

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

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



   def   lowerCAmelCase						(	self					:  List[Any]							):



    """simple docstring"""



    _UpperCamelCase:     Optional[Any]							= self.prepare_config_and_inputs()
    _UpperCamelCase:     List[str]							= config_and_inputs

    _UpperCamelCase:     Union[str, Any]							= {"""input_ids""": input_ids, """attention_mask""": attention_mask}
    return config, inputs_dict




@require_torch
class  __magic_name__  (		_UpperCAmelCase      ,	_UpperCAmelCase      ,	_UpperCAmelCase      ,	unittest.TestCase    ):





   """simple docstring"""




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



   def   lowerCAmelCase						(	self					:  List[Any]							):



    """simple docstring"""



    _UpperCamelCase:     Dict							= TrOCRStandaloneDecoderModelTester(self		,  is_training=lowercase__							)
    _UpperCamelCase:     Tuple							= ConfigTester(self		,  config_class=lowercase__							)



   def   lowerCAmelCase						(	self					:  Dict							):



    """simple docstring"""



    pass



   def   lowerCAmelCase						(	self					:  Any							):



    """simple docstring"""



    pass



   def   lowerCAmelCase						(	self					:  List[Any]							):



    """simple docstring"""



    pass



   def   lowerCAmelCase						(	self					:  Optional[int]							):



    """simple docstring"""



    self.config_tester.run_common_tests()



   def   lowerCAmelCase						(	self					:  Dict							):



    """simple docstring"""



    _UpperCamelCase:     Optional[Any]							= self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_decoder_model_past(*lowercase__							)



   def   lowerCAmelCase						(	self					:  Optional[Any]							):



    """simple docstring"""



    return



   @unittest.skip('''The model doesn\'t support left padding'''							)  # and it's not used enough to be worth fixing :)
   def   lowerCAmelCase						(	self					:  Optional[int]							):



    """simple docstring"""



    pass
import argparse

import torch
from torch import nn

from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration




def       lowerCAmelCase_     (		lowercase:							Any		)    ->				Optional[Any]:







 '''simple docstring'''
 _UpperCamelCase:     Tuple							= [
     '''encoder.version''',
     '''decoder.version''',
     '''model.encoder.version''',
     '''model.decoder.version''',
     '''decoder.output_projection.weight''',
     '''_float_tensor''',
     '''encoder.embed_positions._float_tensor''',
     '''decoder.embed_positions._float_tensor''',
 ]
 for k in ignore_keys:
  state_dict.pop(lowercase  ,					lowercase		)




def       lowerCAmelCase_     (		lowercase:							int		)    ->				Dict:







 '''simple docstring'''
 _UpperCamelCase	,							_UpperCamelCase:     Optional[int]							= emb.weight.shape
 _UpperCamelCase:     int							= nn.Linear(lowercase  ,					lowercase  ,					bias=lowercase		)
 _UpperCamelCase:     Any							= emb.weight.data
 return lin_layer




def       lowerCAmelCase_     (		lowercase:							Optional[Any]		)    ->				Any:







 '''simple docstring'''
 _UpperCamelCase:     Optional[int]							= torch.load(lowercase  ,					map_location='''cpu'''		)
 _UpperCamelCase:     Any							= mam_aaa['''args'''] or mam_aaa['''cfg''']['''model''']
 _UpperCamelCase:     List[Any]							= mam_aaa['''model''']
 remove_ignore_keys_(lowercase		)
 _UpperCamelCase:     Tuple							= state_dict['''encoder.embed_tokens.weight'''].shape[0]

 _UpperCamelCase:     List[Any]							= MaMaaaConfig(
     vocab_size=lowercase  ,					max_position_embeddings=1_024  ,					encoder_layers=args.encoder_layers  ,					decoder_layers=args.decoder_layers  ,					encoder_attention_heads=args.encoder_attention_heads  ,					decoder_attention_heads=args.decoder_attention_heads  ,					encoder_ffn_dim=args.encoder_ffn_embed_dim  ,					decoder_ffn_dim=args.decoder_ffn_embed_dim  ,					d_model=args.encoder_embed_dim  ,					encoder_layerdrop=args.encoder_layerdrop  ,					decoder_layerdrop=args.decoder_layerdrop  ,					dropout=args.dropout  ,					attention_dropout=args.attention_dropout  ,					activation_dropout=args.activation_dropout  ,					activation_function='''relu'''  ,					)

 _UpperCamelCase:     List[Any]							= state_dict['''decoder.embed_tokens.weight''']
 _UpperCamelCase:     Optional[int]							= MaMaaaForConditionalGeneration(lowercase		)
 model.model.load_state_dict(lowercase  ,					strict=lowercase		)
 _UpperCamelCase:     Optional[Any]							= make_linear_from_emb(model.model.shared		)

 return model


if __name__ == "__main__":
  UpperCAmelCase_			    =							argparse.ArgumentParser()
  # Required parameters
  parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''')
  parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
  UpperCAmelCase_			    =							parser.parse_args()
  UpperCAmelCase_			    =							convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß)
  model.save_pretrained(args.pytorch_dump_folder_path)