



'''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 		lowerCamelCase__      :




     '''simple docstring'''



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



      '''simple docstring'''





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




     def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       int:



      '''simple docstring'''





      _lowercase					:       List[str]       =		ids_tensor([self.batch_size, self.seq_length]     ,  self.vocab_size					)

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

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

      _lowercase					:       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     ,  )

      _lowercase					:       Dict       =		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 						__UpperCAmelCase     (  self				:	Tuple     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	Dict     ,  *UpperCamelCase_				:	str					)  ->       List[Any]:



      '''simple docstring'''





      _lowercase					:       Optional[int]       =		OpenAIGPTModel(config=UpperCamelCase_					)
      model.to(UpperCamelCase_					)
      model.eval()

      _lowercase					:       str       =		model(UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  head_mask=UpperCamelCase_					)
      _lowercase					:       List[str]       =		model(UpperCamelCase_     ,  token_type_ids=UpperCamelCase_					)
      _lowercase					:       List[Any]       =		model(UpperCamelCase_					)

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




     def 						__UpperCAmelCase     (  self				:	Optional[int]     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	int     ,  *UpperCamelCase_				:	Optional[Any]					)  ->       Dict:



      '''simple docstring'''





      _lowercase					:       Tuple       =		OpenAIGPTLMHeadModel(UpperCamelCase_					)
      model.to(UpperCamelCase_					)
      model.eval()

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




     def 						__UpperCAmelCase     (  self				:	List[str]     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	str     ,  *UpperCamelCase_				:	Union[str, Any]					)  ->       Dict:



      '''simple docstring'''





      _lowercase					:       int       =		OpenAIGPTDoubleHeadsModel(UpperCamelCase_					)
      model.to(UpperCamelCase_					)
      model.eval()

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




     def 						__UpperCAmelCase     (  self				:	List[Any]     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	Any     ,  *UpperCamelCase_				:	List[str]					)  ->       Any:



      '''simple docstring'''





      _lowercase					:       Dict       =		self.num_labels
      _lowercase					:       Optional[int]       =		OpenAIGPTForSequenceClassification(UpperCamelCase_					)
      model.to(UpperCamelCase_					)
      model.eval()

      _lowercase					:       Any       =		ids_tensor([self.batch_size]     ,  self.type_sequence_label_size					)
      _lowercase					:       Any       =		model(UpperCamelCase_     ,  token_type_ids=UpperCamelCase_     ,  labels=UpperCamelCase_					)
      self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_labels)					)




     def 						__UpperCAmelCase     (  self				:	List[Any]					)  ->       Optional[Any]:



      '''simple docstring'''





      _lowercase					:       Dict       =		self.prepare_config_and_inputs()
      (
          _lowercase    
      )					:       Tuple       =		config_and_inputs
      _lowercase					:       str       =		{
          'input_ids': input_ids,
          'token_type_ids': token_type_ids,
          'head_mask': head_mask,
      }

      return config, inputs_dict






@require_torch
class 		lowerCamelCase__      (						A	,   A	,   A	,   unittest.TestCase     ):




     '''simple docstring'''



     A_						    =						(
         (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
         if is_torch_available()
         else ()
     )
     A_						    =						(
         (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     A_						    =						(
         {
             """feature-extraction""": OpenAIGPTModel,
             """text-classification""": OpenAIGPTForSequenceClassification,
             """text-generation""": OpenAIGPTLMHeadModel,
             """zero-shot""": OpenAIGPTForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )




     def 						__UpperCAmelCase     (  self				:	Any     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Any					)  ->       Union[str, 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 						__UpperCAmelCase     (  self				:	Dict     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	Optional[int]=False					)  ->       List[Any]:



      '''simple docstring'''





      _lowercase					:       List[str]       =		super()._prepare_for_class(UpperCamelCase_     ,  UpperCamelCase_     ,  return_labels=UpperCamelCase_					)

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




     def 						__UpperCAmelCase     (  self				:	List[str]					)  ->       Any:



      '''simple docstring'''





      _lowercase					:       Optional[Any]       =		OpenAIGPTModelTester(self					)
      _lowercase					:       Tuple       =		ConfigTester(self     ,  config_class=UpperCamelCase_     ,  n_embd=37					)




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



      '''simple docstring'''





      self.config_tester.run_common_tests()




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



      '''simple docstring'''





      _lowercase					:       str       =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase_					)




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



      '''simple docstring'''





      _lowercase					:       int       =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(*UpperCamelCase_					)




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



      '''simple docstring'''





      _lowercase					:       List[str]       =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_double_lm_head_model(*UpperCamelCase_					)




     def 						__UpperCAmelCase     (  self				:	Dict					)  ->       Any:



      '''simple docstring'''





      _lowercase					:       Tuple       =		self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase_					)




     @slow
     def 						__UpperCAmelCase     (  self				:	str					)  ->       int:



      '''simple docstring'''





      for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _lowercase					:       Optional[int]       =		OpenAIGPTModel.from_pretrained(UpperCamelCase_					)
       self.assertIsNotNone(UpperCamelCase_					)






@require_torch
class 		lowerCamelCase__      (						unittest.TestCase     ):




     '''simple docstring'''



     @slow
     def 						__UpperCAmelCase     (  self				:	str					)  ->       List[Any]:



      '''simple docstring'''





      _lowercase					:       Any       =		OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'					)
      model.to(UpperCamelCase_					)
      _lowercase					:       Optional[int]       =		torch.tensor([[481, 4735, 544]]     ,  dtype=torch.long     ,  device=UpperCamelCase_					)  # the president is
      _lowercase					:       Optional[int]       =		[
          481,
          4735,
          544,
          246,
          963,
          870,
          762,
          239,
          244,
          4_0477,
          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

      _lowercase					:       Optional[Any]       =		model.generate(UpperCamelCase_     ,  do_sample=UpperCamelCase_					)
      self.assertListEqual(output_ids[0].tolist()     ,  UpperCamelCase_					)





'''simple docstring'''

import argparse
from collections import defaultdict







def 						__UpperCamelCase				(							_lowercase,				_lowercase,				_lowercase,				_lowercase,				_lowercase					) ->			int:
	_lowercase					:       Optional[int]       =		f'''{file}_{class_name}_{test_name}'''
	done_test[_id] += 1

	with open(_lowercase,				'r'					) as f:
		_lowercase					:       Optional[int]       =		f.readlines()

	_lowercase					:       Dict       =		f'''class {class_name}('''
	_lowercase					:       List[Any]       =		f'''{4 * " "}def {test_name}('''
	_lowercase					:       List[str]       =		f'''{8 * " "}{correct_line.split()[0]}'''
	_lowercase					:       List[str]       =		f'''{16 * " "}{correct_line.split()[0]}'''
	_lowercase					:       Dict       =		False
	_lowercase					:       str       =		False
	_lowercase					:       List[Any]       =		False
	_lowercase					:       Union[str, Any]       =		False
	_lowercase					:       Any       =		0
	_lowercase					:       Tuple       =		0

	_lowercase					:       Optional[int]       =		[]
	for line in lines:
		if line.startswith(_lowercase					):
			_lowercase					:       int       =		True
		elif in_class and line.startswith(_lowercase					):
			_lowercase					:       List[Any]       =		True
		elif in_class and in_func and (line.startswith(_lowercase					) or line.startswith(_lowercase					)):
			_lowercase					:       str       =		len(line.split(correct_line.split()[0]					)[0]					)
			count += 1

			if count == done_test[_id]:
				_lowercase					:       List[Any]       =		True

		if in_class and in_func and in_line:
			if ")" not in line:
				continue
			else:
				_lowercase					:       Any       =		True

		if in_class and in_func and in_line and insert_line:
			new_lines.append(f'''{spaces * " "}{correct_line}'''					)
			_lowercase					:       Any       =		False
		else:
			new_lines.append(_lowercase					)

	with open(_lowercase,				'w'					) as f:
		for line in new_lines:
			f.write(_lowercase					)







def 						__UpperCamelCase				(							_lowercase,				_lowercase=None					) ->			Optional[Any]:
	if fail is not None:
		with open(_lowercase,				'r'					) as f:
			_lowercase					:       Any       =		{l.strip() for l in f.readlines()}
	else:
		_lowercase					:       str       =		None

	with open(_lowercase,				'r'					) as f:
		_lowercase					:       str       =		f.readlines()

	_lowercase					:       Union[str, Any]       =		defaultdict(_lowercase					)
	for line in correct_lines:
		_lowercase  ,    _lowercase  ,    _lowercase  ,    _lowercase					:       Union[str, Any]       =		line.split(';'					)
		if test_failures is None or "::".join([file, class_name, test_name]					) in test_failures:
			overwrite_file(_lowercase,				_lowercase,				_lowercase,				_lowercase,				_lowercase					)


if __name__ == "__main__":
		_A				:    str     						=argparse.ArgumentParser()
		parser.add_argument('''--correct_filename''', help='''filename of tests with expected result''')
		parser.add_argument('''--fail_filename''', help='''filename of test failures''', type=str, default=None)
		_A				:    Union[str, Any]     						=parser.parse_args()

		main(args.correct_filename, args.fail_filename)
