
'''simple docstring'''




import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, 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 import (
          OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
          OpenAIGPTConfig,
          OpenAIGPTDoubleHeadsModel,
          OpenAIGPTForSequenceClassification,
          OpenAIGPTLMHeadModel,
          OpenAIGPTModel,
      )





class 					A							:





     def __init__(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE=13		,     SCREAMING_SNAKE_CASE=7		,     SCREAMING_SNAKE_CASE=True		,     SCREAMING_SNAKE_CASE=True		,     SCREAMING_SNAKE_CASE=True		,     SCREAMING_SNAKE_CASE=99		,     SCREAMING_SNAKE_CASE=32		,     SCREAMING_SNAKE_CASE=5		,     SCREAMING_SNAKE_CASE=4		,     SCREAMING_SNAKE_CASE=37		,     SCREAMING_SNAKE_CASE="gelu"		,     SCREAMING_SNAKE_CASE=0.1		,     SCREAMING_SNAKE_CASE=0.1		,     SCREAMING_SNAKE_CASE=512		,     SCREAMING_SNAKE_CASE=16		,     SCREAMING_SNAKE_CASE=2		,     SCREAMING_SNAKE_CASE=0.02		,     SCREAMING_SNAKE_CASE=3		,     SCREAMING_SNAKE_CASE=4		,     SCREAMING_SNAKE_CASE=None		,     )					->							List[Any]:






         """simple docstring"""






         A			:  Union[str, Any]   =		parent
         A			:  str   =		batch_size
         A			:  Tuple   =		seq_length
         A			:  Optional[int]   =		is_training
         A			:  Union[str, Any]   =		use_token_type_ids
         A			:  Any   =		use_labels
         A			:  Optional[int]   =		vocab_size
         A			:  int   =		hidden_size
         A			:  Optional[int]   =		num_hidden_layers
         A			:  str   =		num_attention_heads
         A			:  Optional[Any]   =		intermediate_size
         A			:  Optional[int]   =		hidden_act
         A			:  Union[str, Any]   =		hidden_dropout_prob
         A			:  str   =		attention_probs_dropout_prob
         A			:  Optional[int]   =		max_position_embeddings
         A			:  Tuple   =		type_vocab_size
         A			:  List[str]   =		type_sequence_label_size
         A			:  Tuple   =		initializer_range
         A			:  Union[str, Any]   =		num_labels
         A			:  List[Any]   =		num_choices
         A			:  Dict   =		scope
         A			:  Any   =		self.vocab_size - 1





     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         A			:  Dict   =		ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size      )

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

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

         A			:  List[str]   =		OpenAIGPTConfig(
             vocab_size=self.vocab_size		,     n_embd=self.hidden_size		,     n_layer=self.num_hidden_layers		,     n_head=self.num_attention_heads		,     n_positions=self.max_position_embeddings		,     pad_token_id=self.pad_token_id		,     )

         A			:  Union[str, Any]   =		ids_tensor([self.num_hidden_layers, self.num_attention_heads]		,     2      )

         return (
             config,
             input_ids,
             head_mask,
             token_type_ids,
             sequence_labels,
             token_labels,
             choice_labels,
         )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     *SCREAMING_SNAKE_CASE      )					->							Union[str, Any]:






         """simple docstring"""






         A			:  Optional[int]   =		OpenAIGPTModel(config=SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()

         A			:  Any   =		model(SCREAMING_SNAKE_CASE		,     token_type_ids=SCREAMING_SNAKE_CASE		,     head_mask=SCREAMING_SNAKE_CASE      )
         A			:  Optional[int]   =		model(SCREAMING_SNAKE_CASE		,     token_type_ids=SCREAMING_SNAKE_CASE      )
         A			:  int   =		model(SCREAMING_SNAKE_CASE      )

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





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     *SCREAMING_SNAKE_CASE      )					->							Tuple:






         """simple docstring"""






         A			:  int   =		OpenAIGPTLMHeadModel(SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()

         A			:  Tuple   =		model(SCREAMING_SNAKE_CASE		,     token_type_ids=SCREAMING_SNAKE_CASE		,     labels=SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.loss.shape		,     ()      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     *SCREAMING_SNAKE_CASE      )					->							Tuple:






         """simple docstring"""






         A			:  List[Any]   =		OpenAIGPTDoubleHeadsModel(SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()

         A			:  Any   =		model(SCREAMING_SNAKE_CASE		,     token_type_ids=SCREAMING_SNAKE_CASE		,     labels=SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.loss.shape		,     ()      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     *SCREAMING_SNAKE_CASE      )					->							List[Any]:






         """simple docstring"""






         A			:  Dict   =		self.num_labels
         A			:  List[str]   =		OpenAIGPTForSequenceClassification(SCREAMING_SNAKE_CASE      )
         model.to(SCREAMING_SNAKE_CASE      )
         model.eval()

         A			:  List[str]   =		ids_tensor([self.batch_size]		,     self.type_sequence_label_size      )
         A			:  Any   =		model(SCREAMING_SNAKE_CASE		,     token_type_ids=SCREAMING_SNAKE_CASE		,     labels=SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)      )





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






         """simple docstring"""






         A			:  str   =		self.prepare_config_and_inputs()
         (
             (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    
         )			:  Optional[int]   =		config_and_inputs
         A			:  Tuple   =		{
             '''input_ids''': input_ids,
             '''token_type_ids''': token_type_ids,
             '''head_mask''': head_mask,
         }

         return config, inputs_dict





@require_torch
class 					A							(				__snake_case       ,     __snake_case       ,     __snake_case       ,     unittest.TestCase		):
     __magic_name__          =						(
         (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
         if is_torch_available()
         else ()
     )
     __magic_name__          =						(
         (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     __magic_name__          =						(
         {
             '''feature-extraction''': OpenAIGPTModel,
             '''text-classification''': OpenAIGPTForSequenceClassification,
             '''text-generation''': OpenAIGPTLMHeadModel,
             '''zero-shot''': OpenAIGPTForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							Any:






         """simple docstring"""






         if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
             # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
             # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
             # tiny config could not be created.
             return True

         return False





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE=False      )					->							Optional[int]:






         """simple docstring"""






         A			:  List[str]   =		super()._prepare_for_class(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     return_labels=SCREAMING_SNAKE_CASE      )

         if return_labels:
             if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                 A			:  Union[str, Any]   =		torch.zeros(
                     (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)		,     dtype=torch.long		,     device=SCREAMING_SNAKE_CASE		,     )
                 A			:  str   =		inputs_dict['''labels''']
                 A			:  List[str]   =		inputs_dict['''labels''']
                 A			:  Optional[Any]   =		torch.zeros(
                     (self.model_tester.batch_size, self.model_tester.num_choices)		,     dtype=torch.long		,     device=SCREAMING_SNAKE_CASE		,     )
                 A			:  int   =		torch.zeros(
                     self.model_tester.batch_size		,     dtype=torch.long		,     device=SCREAMING_SNAKE_CASE      )
         return inputs_dict





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






         """simple docstring"""






         A			:  Optional[Any]   =		OpenAIGPTModelTester(self      )
         A			:  List[Any]   =		ConfigTester(self		,     config_class=SCREAMING_SNAKE_CASE		,     n_embd=37      )





     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         self.config_tester.run_common_tests()





     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A			:  str   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_model(*SCREAMING_SNAKE_CASE      )





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






         """simple docstring"""






         A			:  int   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_lm_head_model(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         A			:  str   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_double_lm_head_model(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A			:  Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*SCREAMING_SNAKE_CASE      )





     @slow
     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             A			:  Dict   =		OpenAIGPTModel.from_pretrained(SCREAMING_SNAKE_CASE      )
             self.assertIsNotNone(SCREAMING_SNAKE_CASE      )





@require_torch
class 					A							(				unittest.TestCase		):





     @slow
     def 							__lowerCAmelCase				(				self      )					->							str:






         """simple docstring"""






         A			:  Tuple   =		OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''      )
         model.to(SCREAMING_SNAKE_CASE      )
         A			:  Optional[int]   =		torch.tensor([[481, 4735, 544]]		,     dtype=torch.long		,     device=SCREAMING_SNAKE_CASE      )  # the president is
         A			:  Optional[Any]   =		[
             481,
             4735,
             544,
             246,
             963,
             870,
             762,
             239,
             244,
             40477,
             244,
             249,
             719,
             881,
             487,
             544,
             240,
             244,
             603,
             481,
         ]  # the president is a very good man. " \n " i\'m sure he is, " said the

         A			:  Any   =		model.generate(SCREAMING_SNAKE_CASE		,     do_sample=SCREAMING_SNAKE_CASE      )
         self.assertListEqual(output_ids[0].tolist()		,     SCREAMING_SNAKE_CASE      )






'''simple docstring'''




import fire

from utils import calculate_rouge, save_json







def 						lowerCAmelCase_							(  snake_case__						,							snake_case__						,							snake_case__=None						,							**snake_case__		):




    '''simple docstring'''



    A			:  Optional[Any]   =		[x.strip() for x in open(snake_case__		).readlines()]
    A			:  Tuple   =		[x.strip() for x in open(snake_case__		).readlines()][: len(snake_case__		)]
    A			:  Union[str, Any]   =		calculate_rouge(snake_case__						,							snake_case__						,							**snake_case__		)
    if save_path is not None:
        save_json(snake_case__						,							snake_case__						,							indent=snake_case__		)
    return metrics  # these print nicely


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




