




"""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   :     int   ,	UpperCamelCase_   :     Tuple   ,	UpperCamelCase_   :     Any=13   ,	UpperCamelCase_   :     List[str]=7   ,	UpperCamelCase_   :     Optional[Any]=True   ,	UpperCamelCase_   :     Dict=True   ,	UpperCamelCase_   :     str=True   ,	UpperCamelCase_   :     Any=99   ,	UpperCamelCase_   :     List[Any]=32   ,	UpperCamelCase_   :     Union[str, Any]=5   ,	UpperCamelCase_   :     str=4   ,	UpperCamelCase_   :     Union[str, Any]=37   ,	UpperCamelCase_   :     Any="gelu"   ,	UpperCamelCase_   :     List[Any]=0.1   ,	UpperCamelCase_   :     Tuple=0.1   ,	UpperCamelCase_   :     Any=512   ,	UpperCamelCase_   :     Any=16   ,	UpperCamelCase_   :     Optional[Any]=2   ,	UpperCamelCase_   :     Optional[int]=0.02   ,	UpperCamelCase_   :     Tuple=3   ,	UpperCamelCase_   :     Optional[int]=4   ,	UpperCamelCase_   :     Any=None   ,	):


						"""simple docstring"""




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


			def    a_  (   self   :     List[str]):


						"""simple docstring"""




						__UpperCAmelCase						:			Tuple												=       ids_tensor([self.batch_size, self.seq_length]   ,	self.vocab_size)

						__UpperCAmelCase						:			Union[str, Any]												=       None
						if self.use_token_type_ids:
									__UpperCAmelCase						:			Dict												=       ids_tensor([self.batch_size, self.seq_length]   ,	self.type_vocab_size)

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

						__UpperCAmelCase						:			Dict												=       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   ,	)

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


						"""simple docstring"""




						__UpperCAmelCase						:			int												=       OpenAIGPTModel(config=UpperCamelCase_)
						model.to(UpperCamelCase_)
						model.eval()

						__UpperCAmelCase						:			Optional[int]												=       model(UpperCamelCase_   ,	token_type_ids=UpperCamelCase_   ,	head_mask=UpperCamelCase_)
						__UpperCAmelCase						:			Optional[int]												=       model(UpperCamelCase_   ,	token_type_ids=UpperCamelCase_)
						__UpperCAmelCase						:			Optional[int]												=       model(UpperCamelCase_)

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


			def    a_  (   self   :     str   ,	UpperCamelCase_   :     Dict   ,	UpperCamelCase_   :     List[Any]   ,	UpperCamelCase_   :     int   ,	UpperCamelCase_   :     Tuple   ,	*UpperCamelCase_   :     Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase						:			Any												=       OpenAIGPTLMHeadModel(UpperCamelCase_)
						model.to(UpperCamelCase_)
						model.eval()

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


						"""simple docstring"""




						__UpperCAmelCase						:			Any												=       OpenAIGPTDoubleHeadsModel(UpperCamelCase_)
						model.to(UpperCamelCase_)
						model.eval()

						__UpperCAmelCase						:			Optional[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    a_  (   self   :     Tuple   ,	UpperCamelCase_   :     Optional[Any]   ,	UpperCamelCase_   :     Dict   ,	UpperCamelCase_   :     Optional[int]   ,	UpperCamelCase_   :     Optional[Any]   ,	*UpperCamelCase_   :     Optional[int]):


						"""simple docstring"""




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

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


			def    a_  (   self   :     Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase						:			Union[str, Any]												=       self.prepare_config_and_inputs()
						(
						    (
						    __UpperCAmelCase
						)						,  (
						    __UpperCAmelCase
						)						,  (
						    __UpperCAmelCase
						)						,  (
						    __UpperCAmelCase
						)						,  (
						    __UpperCAmelCase
						)						,  (
						    __UpperCAmelCase
						)						,  (
						    __UpperCAmelCase
						)						,  
						)						:			str												=       config_and_inputs
						__UpperCAmelCase						:			Any												=       {
						    "input_ids": input_ids,
						    "token_type_ids": token_type_ids,
						    "head_mask": head_mask,
						}

						return config, inputs_dict






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


			def    a_  (   self   :     Any   ,	UpperCamelCase_   :     Any   ,	UpperCamelCase_   :     Dict   ,	UpperCamelCase_   :     Optional[int]   ,	UpperCamelCase_   :     int   ,	UpperCamelCase_   :     str):


						"""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    a_  (   self   :     Optional[Any]   ,	UpperCamelCase_   :     Dict   ,	UpperCamelCase_   :     Optional[Any]   ,	UpperCamelCase_   :     Optional[Any]=False):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[Any]												=       super()._prepare_for_class(UpperCamelCase_   ,	UpperCamelCase_   ,	return_labels=UpperCamelCase_)

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


			def    a_  (   self   :     int):


						"""simple docstring"""




						__UpperCAmelCase						:			str												=       OpenAIGPTModelTester(self)
						__UpperCAmelCase						:			Optional[int]												=       ConfigTester(self   ,	config_class=UpperCamelCase_   ,	n_embd=37)


			def    a_  (   self   :     Dict):


						"""simple docstring"""




						self.config_tester.run_common_tests()


			def    a_  (   self   :     List[Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			List[Any]												=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase_)


			def    a_  (   self   :     Dict):


						"""simple docstring"""




						__UpperCAmelCase						:			Any												=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_lm_head_model(*UpperCamelCase_)


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


						"""simple docstring"""




						__UpperCAmelCase						:			List[Any]												=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_double_lm_head_model(*UpperCamelCase_)


			def    a_  (   self   :     Dict):


						"""simple docstring"""




						__UpperCAmelCase						:			str												=       self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase_)


			@slow
			def    a_  (   self   :     Tuple):


						"""simple docstring"""




						for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__UpperCAmelCase						:			List[str]												=       OpenAIGPTModel.from_pretrained(UpperCamelCase_)
									self.assertIsNotNone(UpperCamelCase_)






@require_torch
class   a__					(						unittest.TestCase ):


			@slow
			def    a_  (   self   :     Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[Any]												=       OpenAIGPTLMHeadModel.from_pretrained("openai-gpt")
						model.to(UpperCamelCase_)
						__UpperCAmelCase						:			List[Any]												=       torch.tensor([[481, 4735, 544]]   ,	dtype=torch.long   ,	device=UpperCamelCase_)  # the president is
						__UpperCAmelCase						:			List[str]												=       [
						    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

						__UpperCAmelCase						:			str												=       model.generate(UpperCamelCase_   ,	do_sample=UpperCamelCase_)
						self.assertListEqual(output_ids[0].tolist()   ,	UpperCamelCase_)



from typing import TYPE_CHECKING

from ....utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


SCREAMING_SNAKE_CASE			:						Optional[int]				  =		{
    """configuration_mctct""": ["""MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MCTCTConfig"""],
    """feature_extraction_mctct""": ["""MCTCTFeatureExtractor"""],
    """processing_mctct""": ["""MCTCTProcessor"""],
}


try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  SCREAMING_SNAKE_CASE			:						Optional[Any]				  =		[
      """MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """MCTCTForCTC""",
      """MCTCTModel""",
      """MCTCTPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_mctct import MCTCT_PRETRAINED_CONFIG_ARCHIVE_MAP, MCTCTConfig
  from .feature_extraction_mctct import MCTCTFeatureExtractor
  from .processing_mctct import MCTCTProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_mctct import MCTCT_PRETRAINED_MODEL_ARCHIVE_LIST, MCTCTForCTC, MCTCTModel, MCTCTPreTrainedModel

else:
  import sys

  SCREAMING_SNAKE_CASE			:						Union[str, Any]				  =		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


