


import math
import os
import unittest

from transformers import MegatronBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import (
           MODEL_FOR_PRETRAINING_MAPPING,
           MegatronBertForCausalLM,
           MegatronBertForMaskedLM,
           MegatronBertForMultipleChoice,
           MegatronBertForNextSentencePrediction,
           MegatronBertForPreTraining,
           MegatronBertForQuestionAnswering,
           MegatronBertForSequenceClassification,
           MegatronBertForTokenClassification,
           MegatronBertModel,
       )





class  lowerCAmelCase__:






    '''simple docstring'''





    def __init__(	self				:   int       ,   __snake_case				:   Tuple       ,   __snake_case				:   str=13       ,   __snake_case				:   Dict=7       ,   __snake_case				:   Dict=True       ,   __snake_case				:   List[str]=True       ,   __snake_case				:   List[str]=True       ,   __snake_case				:   int=True       ,   __snake_case				:   Optional[Any]=99       ,   __snake_case				:   List[str]=64       ,   __snake_case				:   Optional[int]=32       ,   __snake_case				:   Any=5       ,   __snake_case				:   Any=4       ,   __snake_case				:   List[Any]=37       ,   __snake_case				:   Optional[int]="gelu"       ,   __snake_case				:   int=0.1       ,   __snake_case				:   List[Any]=0.1       ,   __snake_case				:   int=512       ,   __snake_case				:   Dict=16       ,   __snake_case				:   Optional[Any]=2       ,   __snake_case				:   Optional[Any]=0.02       ,   __snake_case				:   List[str]=3       ,   __snake_case				:   Any=4       ,   __snake_case				:   Optional[int]=None       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      Any						=   parent
       UpperCAmelCase_							:      str						=   batch_size
       UpperCAmelCase_							:      Tuple						=   seq_length
       UpperCAmelCase_							:      str						=   is_training
       UpperCAmelCase_							:      Tuple						=   use_input_mask
       UpperCAmelCase_							:      Union[str, Any]						=   use_token_type_ids
       UpperCAmelCase_							:      Tuple						=   use_labels
       UpperCAmelCase_							:      List[str]						=   vocab_size
       UpperCAmelCase_							:      List[str]						=   hidden_size
       UpperCAmelCase_							:      Dict						=   embedding_size
       UpperCAmelCase_							:      Tuple						=   num_hidden_layers
       UpperCAmelCase_							:      List[Any]						=   num_attention_heads
       UpperCAmelCase_							:      Tuple						=   intermediate_size
       UpperCAmelCase_							:      Dict						=   hidden_act
       UpperCAmelCase_							:      Tuple						=   hidden_dropout_prob
       UpperCAmelCase_							:      str						=   attention_probs_dropout_prob
       UpperCAmelCase_							:      Union[str, Any]						=   max_position_embeddings
       UpperCAmelCase_							:      str						=   type_vocab_size
       UpperCAmelCase_							:      Optional[int]						=   type_sequence_label_size
       UpperCAmelCase_							:      Optional[Any]						=   initializer_range
       UpperCAmelCase_							:      Union[str, Any]						=   num_labels
       UpperCAmelCase_							:      Optional[int]						=   num_choices
       UpperCAmelCase_							:      int						=   scope





    def 					_lowerCamelCase (	self				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      Dict						=   ids_tensor([self.batch_size, self.seq_length]       ,   self.vocab_size	)

       UpperCAmelCase_							:      Optional[int]						=   None
       if self.use_input_mask:
          UpperCAmelCase_							:      Optional[Any]						=   random_attention_mask([self.batch_size, self.seq_length]	)

       UpperCAmelCase_							:      Optional[int]						=   None
       if self.use_token_type_ids:
          UpperCAmelCase_							:      Optional[Any]						=   ids_tensor([self.batch_size, self.seq_length]       ,   self.type_vocab_size	)

       UpperCAmelCase_							:      Optional[int]						=   None
       UpperCAmelCase_							:      str						=   None
       UpperCAmelCase_							:      Union[str, Any]						=   None
       if self.use_labels:
          UpperCAmelCase_							:      Tuple						=   ids_tensor([self.batch_size]       ,   self.type_sequence_label_size	)
          UpperCAmelCase_							:      Optional[Any]						=   ids_tensor([self.batch_size, self.seq_length]       ,   self.num_labels	)
          UpperCAmelCase_							:      Optional[int]						=   ids_tensor([self.batch_size]       ,   self.num_choices	)

       UpperCAmelCase_							:      List[str]						=   self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       return MegatronBertConfig(
           vocab_size=self.vocab_size       ,   hidden_size=self.hidden_size       ,   num_hidden_layers=self.num_hidden_layers       ,   num_attention_heads=self.num_attention_heads       ,   intermediate_size=self.intermediate_size       ,   embedding_size=self.embedding_size       ,   hidden_act=self.hidden_act       ,   hidden_dropout_prob=self.hidden_dropout_prob       ,   attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,   max_position_embeddings=self.max_position_embeddings       ,   type_vocab_size=self.type_vocab_size       ,   is_decoder=__snake_case       ,   initializer_range=self.initializer_range       ,   )





    def 					_lowerCamelCase (	self				:   int       ,   __snake_case				:   Tuple       ,   __snake_case				:   Tuple       ,   __snake_case				:   List[Any]       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Dict       ,   __snake_case				:   int       ,   __snake_case				:   int	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[str]						=   MegatronBertModel(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      Union[str, Any]						=   model(__snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case	)
       UpperCAmelCase_							:      int						=   model(__snake_case       ,   token_type_ids=__snake_case	)
       UpperCAmelCase_							:      Dict						=   model(__snake_case	)

       self.parent.assertEqual(result.last_hidden_state.shape       ,   (self.batch_size, self.seq_length, self.hidden_size)	)
       self.parent.assertEqual(result.pooler_output.shape       ,   (self.batch_size, self.hidden_size)	)





    def 					_lowerCamelCase (	self				:   Tuple       ,   __snake_case				:   List[str]       ,   __snake_case				:   Tuple       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   int       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   List[Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Any						=   MegatronBertForMaskedLM(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      Optional[int]						=   model(__snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   labels=__snake_case	)
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.vocab_size)	)





    def 					_lowerCamelCase (	self				:   Optional[Any]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   Tuple       ,   __snake_case				:   List[Any]       ,   __snake_case				:   int       ,   __snake_case				:   Tuple       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[str]						=   MegatronBertForCausalLM(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      int						=   model(__snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   labels=__snake_case	)
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.vocab_size)	)





    def 					_lowerCamelCase (	self				:   int       ,   __snake_case				:   Tuple       ,   __snake_case				:   Dict       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   int       ,   __snake_case				:   Tuple       ,   __snake_case				:   Union[str, Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      str						=   MegatronBertForNextSentencePrediction(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      List[Any]						=   model(
           __snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   labels=__snake_case       ,   )
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, 2)	)





    def 					_lowerCamelCase (	self				:   Optional[int]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   str       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   List[str]       ,   __snake_case				:   List[Any]       ,   __snake_case				:   Optional[Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   MegatronBertForPreTraining(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      int						=   model(
           __snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   labels=__snake_case       ,   next_sentence_label=__snake_case       ,   )
       self.parent.assertEqual(result.prediction_logits.shape       ,   (self.batch_size, self.seq_length, self.vocab_size)	)
       self.parent.assertEqual(result.seq_relationship_logits.shape       ,   (self.batch_size, 2)	)





    def 					_lowerCamelCase (	self				:   Dict       ,   __snake_case				:   str       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   str       ,   __snake_case				:   Dict       ,   __snake_case				:   int       ,   __snake_case				:   Tuple	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   MegatronBertForQuestionAnswering(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      Optional[int]						=   model(
           __snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   start_positions=__snake_case       ,   end_positions=__snake_case       ,   )
       self.parent.assertEqual(result.start_logits.shape       ,   (self.batch_size, self.seq_length)	)
       self.parent.assertEqual(result.end_logits.shape       ,   (self.batch_size, self.seq_length)	)





    def 					_lowerCamelCase (	self				:   Any       ,   __snake_case				:   Tuple       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   List[str]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   str       ,   __snake_case				:   int       ,   __snake_case				:   List[str]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   self.num_labels
       UpperCAmelCase_							:      List[str]						=   MegatronBertForSequenceClassification(__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      Tuple						=   model(__snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   labels=__snake_case	)
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_labels)	)





    def 					_lowerCamelCase (	self				:   List[Any]       ,   __snake_case				:   int       ,   __snake_case				:   str       ,   __snake_case				:   Tuple       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Tuple       ,   __snake_case				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[str]						=   self.num_labels
       UpperCAmelCase_							:      Union[str, Any]						=   MegatronBertForTokenClassification(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      Optional[int]						=   model(__snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   labels=__snake_case	)
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.num_labels)	)





    def 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   Tuple       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Dict       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Any       ,   __snake_case				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[Any]						=   self.num_choices
       UpperCAmelCase_							:      Any						=   MegatronBertForMultipleChoice(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      Dict						=   input_ids.unsqueeze(1	).expand(-1       ,   self.num_choices       ,   -1	).contiguous()
       UpperCAmelCase_							:      Union[str, Any]						=   token_type_ids.unsqueeze(1	).expand(-1       ,   self.num_choices       ,   -1	).contiguous()
       UpperCAmelCase_							:      List[Any]						=   input_mask.unsqueeze(1	).expand(-1       ,   self.num_choices       ,   -1	).contiguous()
       UpperCAmelCase_							:      str						=   model(
           __snake_case       ,   attention_mask=__snake_case       ,   token_type_ids=__snake_case       ,   labels=__snake_case       ,   )
       self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.num_choices)	)





    def 					_lowerCamelCase (	self				:   Any	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   self.prepare_config_and_inputs()
       (
           (
           UpperCAmelCase_					
       )		,      (
           UpperCAmelCase_					
       )		,      (
           UpperCAmelCase_					
       )		,      (
           UpperCAmelCase_					
       )		,      (
           UpperCAmelCase_					
       )		,      (
           UpperCAmelCase_					
       )		,      (
           UpperCAmelCase_					
       )		,      					
       )							:      Any						=   config_and_inputs
       UpperCAmelCase_							:      Optional[int]						=   {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
       return config, inputs_dict





@require_torch
class  lowerCAmelCase__(    lowercase_    ,    lowercase_    ,    unittest.TestCase						):






    '''simple docstring'''





    A_  :     int			= (
        (
            MegatronBertModel,
            MegatronBertForMaskedLM,
            MegatronBertForCausalLM,
            MegatronBertForMultipleChoice,
            MegatronBertForNextSentencePrediction,
            MegatronBertForPreTraining,
            MegatronBertForQuestionAnswering,
            MegatronBertForSequenceClassification,
            MegatronBertForTokenClassification,
        )
        if is_torch_available()
        else ()
    )
    A_  :     List[str]			= (
        {
            '''feature-extraction''': MegatronBertModel,
            '''fill-mask''': MegatronBertForMaskedLM,
            '''question-answering''': MegatronBertForQuestionAnswering,
            '''text-classification''': MegatronBertForSequenceClassification,
            '''text-generation''': MegatronBertForCausalLM,
            '''token-classification''': MegatronBertForTokenClassification,
            '''zero-shot''': MegatronBertForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    A_  :     Tuple			= True
    # test_resize_embeddings = False
    A_  :     Any			= False





    def 					_lowerCamelCase (	self				:   Any       ,   __snake_case				:   Tuple       ,   __snake_case				:   Dict       ,   __snake_case				:   Any=False	):





       '''simple docstring'''
       UpperCAmelCase_							:      Any						=   super()._prepare_for_class(__snake_case       ,   __snake_case       ,   return_labels=__snake_case	)

       if return_labels:
          if model_class in get_values(__snake_case	):
             UpperCAmelCase_							:      Dict						=   torch.zeros(
                 (self.model_tester.batch_size, self.model_tester.seq_length)       ,   dtype=torch.long       ,   device=__snake_case	)
             UpperCAmelCase_							:      List[Any]						=   torch.zeros(
                 self.model_tester.batch_size       ,   dtype=torch.long       ,   device=__snake_case	)
       return inputs_dict





    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   MegatronBertModelTester(self	)
       UpperCAmelCase_							:      str						=   ConfigTester(self       ,   config_class=__snake_case       ,   hidden_size=37	)





    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       self.config_tester.run_common_tests()





    def 					_lowerCamelCase (	self				:   Union[str, Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_model(*__snake_case	)





    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      str						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_masked_lm(*__snake_case	)





    def 					_lowerCamelCase (	self				:   Optional[int]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*__snake_case	)





    def 					_lowerCamelCase (	self				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*__snake_case	)





    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Dict						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_pretraining(*__snake_case	)





    def 					_lowerCamelCase (	self				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[Any]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_question_answering(*__snake_case	)





    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*__snake_case	)





    def 					_lowerCamelCase (	self				:   Union[str, Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_token_classification(*__snake_case	)





def      snake_case_						( __lowercase       ):
   return torch.tensor(
       UpperCamelCase__			,       dtype=torch.long			,       device=UpperCamelCase__			,       )


__UpperCamelCase				:  Any      					=							1E-4





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






    '''simple docstring'''





    @slow
    @unittest.skip('''Model is not available.'''	)
    def 					_lowerCamelCase (	self				:   Optional[Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[Any]						=   '''nvidia/megatron-bert-uncased-345m'''
       if "MYDIR" in os.environ:
          UpperCAmelCase_							:      Dict						=   os.path.join(os.environ['''MYDIR''']       ,   __snake_case	)
       UpperCAmelCase_							:      List[Any]						=   MegatronBertModel.from_pretrained(__snake_case	)
       model.to(__snake_case	)
       model.half()
       UpperCAmelCase_							:      int						=   _long_tensor([[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]	)
       with torch.no_grad():
          UpperCAmelCase_							:      Union[str, Any]						=   model(__snake_case	)[0]
       UpperCAmelCase_							:      Optional[Any]						=   torch.Size((1, 9, 1_024)	)
       self.assertEqual(output.shape       ,   __snake_case	)

       UpperCAmelCase_							:      Optional[int]						=   [-0.6_040, -0.2_517, -0.1_025, 0.3_420, -0.6_758, -0.0_017, -0.1_089, -0.1_990, 0.5_728]
       for ii in range(3	):
          for jj in range(3	):
             UpperCAmelCase_							:      Tuple						=   output[0, ii, jj]
             UpperCAmelCase_							:      int						=   expected[3 * ii + jj]
             UpperCAmelCase_							:      int						=   '''ii={} jj={} a={} b={}'''.format(__snake_case       ,   __snake_case       ,   __snake_case       ,   __snake_case	)
             self.assertTrue(math.isclose(__snake_case       ,   __snake_case       ,   rel_tol=__snake_case       ,   abs_tol=__snake_case	)       ,   msg=__snake_case	)







import fire

from utils import calculate_rouge, save_json
def      snake_case_						( __lowercase			,       __lowercase			,       __lowercase=None			,       **__lowercase       ):
   UpperCAmelCase_							:      Tuple						=   [x.strip() for x in open(__lowercase       ).readlines()]
   UpperCAmelCase_							:      Dict						=   [x.strip() for x in open(__lowercase       ).readlines()][: len(__lowercase       )]
   UpperCAmelCase_							:      int						=   calculate_rouge(__lowercase			,       __lowercase			,       **__lowercase       )
   if save_path is not None:
      save_json(__lowercase			,       __lowercase			,       indent=__lowercase       )
   return metrics  # these print nicely


if __name__ == "__main__":
       fire.Fire(calculate_rouge_path)