






from ..utils import DummyObject, requires_backends





class 					A__     (     metaclass=__snake_case ):

     '''simple docstring'''






     snake_case__					=					["""torch""", """transformers""", """onnx"""]







     def __init__( self      :    List[Any]   ,						*_SCREAMING_SNAKE_CASE      :    Dict   ,						**_SCREAMING_SNAKE_CASE      :    Dict ):






           """simple docstring"""




           requires_backends(self   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Optional[Any]   ,						*_SCREAMING_SNAKE_CASE      :    List[Any]   ,						**_SCREAMING_SNAKE_CASE      :    Optional[int] ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Any   ,						*_SCREAMING_SNAKE_CASE      :    int   ,						**_SCREAMING_SNAKE_CASE      :    List[str] ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )





class 					A__     (     metaclass=__snake_case ):

     '''simple docstring'''






     snake_case__					=					["""torch""", """transformers""", """onnx"""]







     def __init__( self      :    str   ,						*_SCREAMING_SNAKE_CASE      :    List[str]   ,						**_SCREAMING_SNAKE_CASE      :    Tuple ):






           """simple docstring"""




           requires_backends(self   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Dict   ,						*_SCREAMING_SNAKE_CASE      :    int   ,						**_SCREAMING_SNAKE_CASE      :    str ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Dict   ,						*_SCREAMING_SNAKE_CASE      :    Union[str, Any]   ,						**_SCREAMING_SNAKE_CASE      :    int ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )





class 					A__     (     metaclass=__snake_case ):

     '''simple docstring'''






     snake_case__					=					["""torch""", """transformers""", """onnx"""]







     def __init__( self      :    Any   ,						*_SCREAMING_SNAKE_CASE      :    str   ,						**_SCREAMING_SNAKE_CASE      :    Dict ):






           """simple docstring"""




           requires_backends(self   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    List[Any]   ,						*_SCREAMING_SNAKE_CASE      :    Dict   ,						**_SCREAMING_SNAKE_CASE      :    Any ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Optional[Any]   ,						*_SCREAMING_SNAKE_CASE      :    Any   ,						**_SCREAMING_SNAKE_CASE      :    Union[str, Any] ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )





class 					A__     (     metaclass=__snake_case ):

     '''simple docstring'''






     snake_case__					=					["""torch""", """transformers""", """onnx"""]







     def __init__( self      :    List[Any]   ,						*_SCREAMING_SNAKE_CASE      :    str   ,						**_SCREAMING_SNAKE_CASE      :    int ):






           """simple docstring"""




           requires_backends(self   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Union[str, Any]   ,						*_SCREAMING_SNAKE_CASE      :    Tuple   ,						**_SCREAMING_SNAKE_CASE      :    List[Any] ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Optional[int]   ,						*_SCREAMING_SNAKE_CASE      :    int   ,						**_SCREAMING_SNAKE_CASE      :    Optional[Any] ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )





class 					A__     (     metaclass=__snake_case ):

     '''simple docstring'''






     snake_case__					=					["""torch""", """transformers""", """onnx"""]







     def __init__( self      :    Optional[Any]   ,						*_SCREAMING_SNAKE_CASE      :    Any   ,						**_SCREAMING_SNAKE_CASE      :    List[str] ):






           """simple docstring"""




           requires_backends(self   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    List[Any]   ,						*_SCREAMING_SNAKE_CASE      :    Any   ,						**_SCREAMING_SNAKE_CASE      :    Tuple ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Any   ,						*_SCREAMING_SNAKE_CASE      :    int   ,						**_SCREAMING_SNAKE_CASE      :    str ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )





class 					A__     (     metaclass=__snake_case ):

     '''simple docstring'''






     snake_case__					=					["""torch""", """transformers""", """onnx"""]







     def __init__( self      :    Any   ,						*_SCREAMING_SNAKE_CASE      :    Any   ,						**_SCREAMING_SNAKE_CASE      :    Optional[Any] ):






           """simple docstring"""




           requires_backends(self   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Union[str, Any]   ,						*_SCREAMING_SNAKE_CASE      :    Optional[Any]   ,						**_SCREAMING_SNAKE_CASE      :    List[Any] ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )







     @classmethod
     def   _SCREAMING_SNAKE_CASE     ( cls      :    Tuple   ,						*_SCREAMING_SNAKE_CASE      :    Optional[int]   ,						**_SCREAMING_SNAKE_CASE      :    Tuple ):






           """simple docstring"""




           requires_backends(cls   ,						['torch', 'transformers', 'onnx'] )











from transformers import BertTokenizer, EncoderDecoderModel, SeqaSeqTrainer, SeqaSeqTrainingArguments
from transformers.testing_utils import TestCasePlus, require_torch, slow
from transformers.utils import is_datasets_available


if is_datasets_available():
       import datasets





class 					A__     (     __snake_case ):

     '''simple docstring'''






     @slow
     @require_torch
     def   _SCREAMING_SNAKE_CASE     ( self      :    Tuple ):






           """simple docstring"""




           UpperCamelCase					       =  EncoderDecoderModel.from_encoder_decoder_pretrained('prajjwal1/bert-tiny'   ,						'prajjwal1/bert-tiny' )
           UpperCamelCase					       =  BertTokenizer.from_pretrained('bert-base-uncased' )

           UpperCamelCase					       =  bertabert.config.encoder.vocab_size
           UpperCamelCase					       =  tokenizer.sep_token_id
           UpperCamelCase					       =  tokenizer.cls_token_id
           UpperCamelCase					       =  128

           UpperCamelCase					       =  datasets.load_dataset('cnn_dailymail'   ,						'3.0.0'   ,						split='train[:1%]' )
           UpperCamelCase					       =  datasets.load_dataset('cnn_dailymail'   ,						'3.0.0'   ,						split='validation[:1%]' )

           UpperCamelCase					       =  train_dataset.select(range(32 ) )
           UpperCamelCase					       =  val_dataset.select(range(16 ) )

           UpperCamelCase					       =  4

           def _map_to_encoder_decoder_inputs(_SCREAMING_SNAKE_CASE      :    Tuple ):
                 # Tokenizer will automatically set [BOS] <text> [EOS]
                 UpperCamelCase					       =  tokenizer(batch['article']   ,						padding='max_length'   ,						truncation=_SCREAMING_SNAKE_CASE   ,						max_length=512 )
                 UpperCamelCase					       =  tokenizer(batch['highlights']   ,						padding='max_length'   ,						truncation=_SCREAMING_SNAKE_CASE   ,						max_length=128 )
                 UpperCamelCase					       =  inputs.input_ids
                 UpperCamelCase					       =  inputs.attention_mask

                 UpperCamelCase					       =  outputs.input_ids
                 UpperCamelCase					       =  outputs.input_ids.copy()
                 UpperCamelCase					       =  [
                     [-100 if token == tokenizer.pad_token_id else token for token in labels] for labels in batch['labels']
                 ]
                 UpperCamelCase					       =  outputs.attention_mask

                 assert all(len(_SCREAMING_SNAKE_CASE ) == 512 for x in inputs.input_ids )
                 assert all(len(_SCREAMING_SNAKE_CASE ) == 128 for x in outputs.input_ids )

                 return batch

           def _compute_metrics(_SCREAMING_SNAKE_CASE      :    str ):
                 UpperCamelCase					       =  pred.label_ids
                 UpperCamelCase					       =  pred.predictions

                 # all unnecessary tokens are removed
                 UpperCamelCase					       =  tokenizer.batch_decode(_SCREAMING_SNAKE_CASE   ,						skip_special_tokens=_SCREAMING_SNAKE_CASE )
                 UpperCamelCase					       =  tokenizer.batch_decode(_SCREAMING_SNAKE_CASE   ,						skip_special_tokens=_SCREAMING_SNAKE_CASE )

                 UpperCamelCase					       =  sum([int(pred_str[i] == label_str[i] ) for i in range(len(_SCREAMING_SNAKE_CASE ) )] ) / len(_SCREAMING_SNAKE_CASE )

                 return {"accuracy": accuracy}

           # map train dataset
           UpperCamelCase					       =  train_dataset.map(
               _map_to_encoder_decoder_inputs   ,						batched=_SCREAMING_SNAKE_CASE   ,						batch_size=_SCREAMING_SNAKE_CASE   ,						remove_columns=['article', 'highlights']   ,						)
           train_dataset.set_format(
               type='torch'   ,						columns=['input_ids', 'attention_mask', 'decoder_input_ids', 'decoder_attention_mask', 'labels']   ,						)

           # same for validation dataset
           UpperCamelCase					       =  val_dataset.map(
               _map_to_encoder_decoder_inputs   ,						batched=_SCREAMING_SNAKE_CASE   ,						batch_size=_SCREAMING_SNAKE_CASE   ,						remove_columns=['article', 'highlights']   ,						)
           val_dataset.set_format(
               type='torch'   ,						columns=['input_ids', 'attention_mask', 'decoder_input_ids', 'decoder_attention_mask', 'labels']   ,						)

           UpperCamelCase					       =  self.get_auto_remove_tmp_dir()

           UpperCamelCase					       =  SeqaSeqTrainingArguments(
               output_dir=_SCREAMING_SNAKE_CASE   ,						per_device_train_batch_size=_SCREAMING_SNAKE_CASE   ,						per_device_eval_batch_size=_SCREAMING_SNAKE_CASE   ,						predict_with_generate=_SCREAMING_SNAKE_CASE   ,						evaluation_strategy='steps'   ,						do_train=_SCREAMING_SNAKE_CASE   ,						do_eval=_SCREAMING_SNAKE_CASE   ,						warmup_steps=0   ,						eval_steps=2   ,						logging_steps=2   ,						)

           # instantiate trainer
           UpperCamelCase					       =  SeqaSeqTrainer(
               model=_SCREAMING_SNAKE_CASE   ,						args=_SCREAMING_SNAKE_CASE   ,						compute_metrics=_compute_metrics   ,						train_dataset=_SCREAMING_SNAKE_CASE   ,						eval_dataset=_SCREAMING_SNAKE_CASE   ,						tokenizer=_SCREAMING_SNAKE_CASE   ,						)

           # start training
           trainer.train()



