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 :       str  ,     __magic_name__ :       Tuple  ,     __magic_name__ :       List[Any]=13  ,     __magic_name__ :       Any=7  ,     __magic_name__ :       int=True  ,     __magic_name__ :       int=True  ,     __magic_name__ :       Any=True  ,     __magic_name__ :       Dict=99  ,     __magic_name__ :       List[Any]=32  ,     __magic_name__ :       Any=5  ,     __magic_name__ :       Tuple=4  ,     __magic_name__ :       Optional[Any]=37  ,     __magic_name__ :       Union[str, Any]="gelu"  ,     __magic_name__ :       Optional[int]=0.1  ,     __magic_name__ :       Optional[int]=0.1  ,     __magic_name__ :       int=512  ,     __magic_name__ :       Tuple=16  ,     __magic_name__ :       Optional[int]=2  ,     __magic_name__ :       List[str]=0.02  ,     __magic_name__ :       List[str]=3  ,     __magic_name__ :       Union[str, Any]=4  ,     __magic_name__ :       List[str]=None  ,     )  ->	List[Any]:
								SCREAMING_SNAKE_CASE_          =  parent
								SCREAMING_SNAKE_CASE_          =  batch_size
								SCREAMING_SNAKE_CASE_          =  seq_length
								SCREAMING_SNAKE_CASE_          =  is_training
								SCREAMING_SNAKE_CASE_          =  use_token_type_ids
								SCREAMING_SNAKE_CASE_          =  use_labels
								SCREAMING_SNAKE_CASE_          =  vocab_size
								SCREAMING_SNAKE_CASE_          =  hidden_size
								SCREAMING_SNAKE_CASE_          =  num_hidden_layers
								SCREAMING_SNAKE_CASE_          =  num_attention_heads
								SCREAMING_SNAKE_CASE_          =  intermediate_size
								SCREAMING_SNAKE_CASE_          =  hidden_act
								SCREAMING_SNAKE_CASE_          =  hidden_dropout_prob
								SCREAMING_SNAKE_CASE_          =  attention_probs_dropout_prob
								SCREAMING_SNAKE_CASE_          =  max_position_embeddings
								SCREAMING_SNAKE_CASE_          =  type_vocab_size
								SCREAMING_SNAKE_CASE_          =  type_sequence_label_size
								SCREAMING_SNAKE_CASE_          =  initializer_range
								SCREAMING_SNAKE_CASE_          =  num_labels
								SCREAMING_SNAKE_CASE_          =  num_choices
								SCREAMING_SNAKE_CASE_          =  scope
								SCREAMING_SNAKE_CASE_          =  self.vocab_size - 1







		def    __A     (							self :       int   )  ->	int:
								SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size   )

								SCREAMING_SNAKE_CASE_          =  None
								if self.use_token_type_ids:
														SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size, self.seq_length]  ,     self.type_vocab_size   )

								SCREAMING_SNAKE_CASE_          =  None
								SCREAMING_SNAKE_CASE_          =  None
								SCREAMING_SNAKE_CASE_          =  None
								if self.use_labels:
														SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size]  ,     self.type_sequence_label_size   )
														SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size, self.seq_length]  ,     self.num_labels   )
														SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size]  ,     self.num_choices   )

								SCREAMING_SNAKE_CASE_          =  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  ,     )

								SCREAMING_SNAKE_CASE_          =  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    __A     (							self :       int  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       str  ,     __magic_name__ :       Optional[int]  ,     *__magic_name__ :       Optional[Any]   )  ->	Any:
								SCREAMING_SNAKE_CASE_          =  OpenAIGPTModel(config=__magic_name__   )
								model.to(__magic_name__   )
								model.eval()

								SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     token_type_ids=__magic_name__  ,     head_mask=__magic_name__   )
								SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     token_type_ids=__magic_name__   )
								SCREAMING_SNAKE_CASE_          =  model(__magic_name__   )

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







		def    __A     (							self :       Dict  ,     __magic_name__ :       Any  ,     __magic_name__ :       int  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Any  ,     *__magic_name__ :       Union[str, Any]   )  ->	Union[str, Any]:
								SCREAMING_SNAKE_CASE_          =  OpenAIGPTLMHeadModel(__magic_name__   )
								model.to(__magic_name__   )
								model.eval()

								SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     token_type_ids=__magic_name__  ,     labels=__magic_name__   )
								self.parent.assertEqual(result.loss.shape  ,     ()   )
								self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)   )







		def    __A     (							self :       int  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Optional[int]  ,     *__magic_name__ :       List[Any]   )  ->	str:
								SCREAMING_SNAKE_CASE_          =  OpenAIGPTDoubleHeadsModel(__magic_name__   )
								model.to(__magic_name__   )
								model.eval()

								SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     token_type_ids=__magic_name__  ,     labels=__magic_name__   )
								self.parent.assertEqual(result.loss.shape  ,     ()   )
								self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)   )







		def    __A     (							self :       Any  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Any  ,     __magic_name__ :       Any  ,     *__magic_name__ :       Tuple   )  ->	Union[str, Any]:
								SCREAMING_SNAKE_CASE_          =  self.num_labels
								SCREAMING_SNAKE_CASE_          =  OpenAIGPTForSequenceClassification(__magic_name__   )
								model.to(__magic_name__   )
								model.eval()

								SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size]  ,     self.type_sequence_label_size   )
								SCREAMING_SNAKE_CASE_          =  model(__magic_name__  ,     token_type_ids=__magic_name__  ,     labels=__magic_name__   )
								self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_labels)   )







		def    __A     (							self :       List[Any]   )  ->	Union[str, Any]:
								SCREAMING_SNAKE_CASE_          =  self.prepare_config_and_inputs()
								(
								    (
								    SCREAMING_SNAKE_CASE_ 
								)     , (
								    SCREAMING_SNAKE_CASE_ 
								)     , (
								    SCREAMING_SNAKE_CASE_ 
								)     , (
								    SCREAMING_SNAKE_CASE_ 
								)     , (
								    SCREAMING_SNAKE_CASE_ 
								)     , (
								    SCREAMING_SNAKE_CASE_ 
								)     , (
								    SCREAMING_SNAKE_CASE_ 
								)     ,  
								)          =  config_and_inputs
								SCREAMING_SNAKE_CASE_          =  {
								    "input_ids": input_ids,
								    "token_type_ids": token_type_ids,
								    "head_mask": head_mask,
								}

								return config, inputs_dict



@require_torch
class   lowerCamelCase					(SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      SCREAMING_SNAKE_CASE__  ,      unittest.TestCase					):
		"""simple docstring"""







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







		def    __A     (							self :       Dict  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       Any  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       Dict   )  ->	Dict:
								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    __A     (							self :       List[Any]  ,     __magic_name__ :       Any  ,     __magic_name__ :       int  ,     __magic_name__ :       str=False   )  ->	Optional[int]:
								SCREAMING_SNAKE_CASE_          =  super()._prepare_for_class(__magic_name__  ,     __magic_name__  ,     return_labels=__magic_name__   )

								if return_labels:
														if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																				SCREAMING_SNAKE_CASE_          =  torch.zeros(
																				    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)  ,     dtype=torch.long  ,     device=__magic_name__  ,     )
																				SCREAMING_SNAKE_CASE_          =  inputs_dict["labels"]
																				SCREAMING_SNAKE_CASE_          =  inputs_dict["labels"]
																				SCREAMING_SNAKE_CASE_          =  torch.zeros(
																				    (self.model_tester.batch_size, self.model_tester.num_choices)  ,     dtype=torch.long  ,     device=__magic_name__  ,     )
																				SCREAMING_SNAKE_CASE_          =  torch.zeros(
																				    self.model_tester.batch_size  ,     dtype=torch.long  ,     device=__magic_name__   )
								return inputs_dict







		def    __A     (							self :       Any   )  ->	Any:
								SCREAMING_SNAKE_CASE_          =  OpenAIGPTModelTester(self   )
								SCREAMING_SNAKE_CASE_          =  ConfigTester(self  ,     config_class=__magic_name__  ,     n_embd=37   )







		def    __A     (							self :       List[Any]   )  ->	str:
								self.config_tester.run_common_tests()







		def    __A     (							self :       Union[str, Any]   )  ->	int:
								SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_model(*__magic_name__   )







		def    __A     (							self :       List[str]   )  ->	Optional[int]:
								SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*__magic_name__   )







		def    __A     (							self :       Union[str, Any]   )  ->	Dict:
								SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_double_lm_head_model(*__magic_name__   )







		def    __A     (							self :       Dict   )  ->	Optional[Any]:
								SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__magic_name__   )







		@slow
		def    __A     (							self :       List[Any]   )  ->	List[str]:
								for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														SCREAMING_SNAKE_CASE_          =  OpenAIGPTModel.from_pretrained(__magic_name__   )
														self.assertIsNotNone(__magic_name__   )



@require_torch
class   lowerCamelCase					(unittest.TestCase					):
		"""simple docstring"""







		@slow
		def    __A     (							self :       Any   )  ->	List[Any]:
								SCREAMING_SNAKE_CASE_          =  OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"   )
								model.to(__magic_name__   )
								SCREAMING_SNAKE_CASE_          =  torch.tensor([[481, 4_735, 544]]  ,     dtype=torch.long  ,     device=__magic_name__   )  # the president is
								SCREAMING_SNAKE_CASE_          =  [
								    481,
								    4_735,
								    544,
								    246,
								    963,
								    870,
								    762,
								    239,
								    244,
								    40_477,
								    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

								SCREAMING_SNAKE_CASE_          =  model.generate(__magic_name__  ,     do_sample=__magic_name__   )
								self.assertListEqual(output_ids[0].tolist()  ,     __magic_name__   )



from typing import Optional
from urllib.parse import quote

import huggingface_hub as hfh
from packaging import version


def      a__ (		__UpperCamelCase     ,      __UpperCamelCase     ,      __UpperCamelCase = None				):
      if version.parse(hfh.__version__				).release < version.parse("0.11.0"				).release:
            # old versions of hfh don't url-encode the file path
            SCREAMING_SNAKE_CASE_          =  quote(__UpperCamelCase				)
      return hfh.hf_hub_url(__UpperCamelCase     ,      __UpperCamelCase     ,      repo_type="dataset"     ,      revision=__UpperCamelCase				)


