

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   __lowercase    :




			"""simple docstring"""







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



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Tuple):
									SCREAMING_SNAKE_CASE_:			Union[str, Any]  		=	ids_tensor([self.batch_size, self.seq_length]       ,						self.vocab_size)

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

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

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

									SCREAMING_SNAKE_CASE_:			List[Any]  		=	model(lowerCAmelCase__       ,						token_type_ids=lowerCAmelCase__       ,						head_mask=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	model(lowerCAmelCase__       ,						token_type_ids=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:			int  		=	model(lowerCAmelCase__)

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



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Dict       ,						lowerCAmelCase__  :					Dict       ,						lowerCAmelCase__  :					Tuple       ,						lowerCAmelCase__  :					List[Any]       ,						lowerCAmelCase__  :					Any       ,						*lowerCAmelCase__  :					List[str]):
									SCREAMING_SNAKE_CASE_:			Tuple  		=	OpenAIGPTLMHeadModel(lowerCAmelCase__)
									model.to(lowerCAmelCase__)
									model.eval()

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



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Union[str, Any]       ,						lowerCAmelCase__  :					List[Any]       ,						lowerCAmelCase__  :					int       ,						lowerCAmelCase__  :					Any       ,						lowerCAmelCase__  :					Optional[int]       ,						*lowerCAmelCase__  :					Any):
									SCREAMING_SNAKE_CASE_:			Dict  		=	OpenAIGPTDoubleHeadsModel(lowerCAmelCase__)
									model.to(lowerCAmelCase__)
									model.eval()

									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	model(lowerCAmelCase__       ,						token_type_ids=lowerCAmelCase__       ,						labels=lowerCAmelCase__)
									self.parent.assertEqual(result.loss.shape       ,						())
									self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.seq_length, self.vocab_size))



			def 	_SCREAMING_SNAKE_CASE      (			self  :					str       ,						lowerCAmelCase__  :					Union[str, Any]       ,						lowerCAmelCase__  :					List[str]       ,						lowerCAmelCase__  :					Union[str, Any]       ,						lowerCAmelCase__  :					List[Any]       ,						*lowerCAmelCase__  :					Dict):
									SCREAMING_SNAKE_CASE_:			List[str]  		=	self.num_labels
									SCREAMING_SNAKE_CASE_:			Union[str, Any]  		=	OpenAIGPTForSequenceClassification(lowerCAmelCase__)
									model.to(lowerCAmelCase__)
									model.eval()

									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	ids_tensor([self.batch_size]       ,						self.type_sequence_label_size)
									SCREAMING_SNAKE_CASE_:			Tuple  		=	model(lowerCAmelCase__       ,						token_type_ids=lowerCAmelCase__       ,						labels=lowerCAmelCase__)
									self.parent.assertEqual(result.logits.shape       ,						(self.batch_size, self.num_labels))




			def 	_SCREAMING_SNAKE_CASE      (			self  :					Any):
									SCREAMING_SNAKE_CASE_:			int  		=	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_
									)		,   
									):			Union[str, Any]  		=	config_and_inputs
									SCREAMING_SNAKE_CASE_:			int  		=	{
									    "input_ids": input_ids,
									    "token_type_ids": token_type_ids,
									    "head_mask": head_mask,
									}

									return config, inputs_dict







@require_torch
class   __lowercase    (			UpperCAmelCase_		,	UpperCAmelCase_		,	UpperCAmelCase_		,	unittest.TestCase     ):




			"""simple docstring"""







			_UpperCAmelCase       :							Optional[int]      							=					(
			    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
			    if is_torch_available()
			    else ()
			)
			_UpperCAmelCase       :							Optional[Any]      							=					(
			    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
			)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
			_UpperCAmelCase       :							List[str]      							=					(
			    {
			        '''feature-extraction''': OpenAIGPTModel,
			        '''text-classification''': OpenAIGPTForSequenceClassification,
			        '''text-generation''': OpenAIGPTLMHeadModel,
			        '''zero-shot''': OpenAIGPTForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)



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

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



			def 	_SCREAMING_SNAKE_CASE      (			self  :					int):
									SCREAMING_SNAKE_CASE_:			Dict  		=	OpenAIGPTModelTester(self)
									SCREAMING_SNAKE_CASE_:			str  		=	ConfigTester(self       ,						config_class=lowerCAmelCase__       ,						n_embd=37)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[str]):
									self.config_tester.run_common_tests()



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Tuple):
									SCREAMING_SNAKE_CASE_:			Tuple  		=	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					int):
									SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Tuple):
									SCREAMING_SNAKE_CASE_:			int  		=	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Dict):
									SCREAMING_SNAKE_CASE_:			Union[str, Any]  		=	self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__)




			@slow
			def 	_SCREAMING_SNAKE_CASE      (			self  :					Union[str, Any]):
									for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	OpenAIGPTModel.from_pretrained(lowerCAmelCase__)
															self.assertIsNotNone(lowerCAmelCase__)







@require_torch
class   __lowercase    (			unittest.TestCase     ):




			"""simple docstring"""







			@slow
			def 	_SCREAMING_SNAKE_CASE      (			self  :					Dict):
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	OpenAIGPTLMHeadModel.from_pretrained("openai-gpt")
									model.to(lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:			Any  		=	torch.tensor([[481, 4735, 544]]       ,						dtype=torch.long       ,						device=lowerCAmelCase__)  # the president is
									SCREAMING_SNAKE_CASE_:			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

									SCREAMING_SNAKE_CASE_:			List[Any]  		=	model.generate(lowerCAmelCase__       ,						do_sample=lowerCAmelCase__)
									self.assertListEqual(output_ids[0].tolist()       ,						lowerCAmelCase__)









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCAmelCase							:  Optional[int]            =						{
    """configuration_nllb_moe""": [
        """NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """NllbMoeConfig""",
    ]
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCAmelCase							:  Optional[int]            =						[
						    """NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """NllbMoeForConditionalGeneration""",
						    """NllbMoeModel""",
						    """NllbMoePreTrainedModel""",
						    """NllbMoeTop2Router""",
						    """NllbMoeSparseMLP""",
						]


if TYPE_CHECKING:
						from .configuration_nllb_moe import (
						    NLLB_MOE_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    NllbMoeConfig,
						)

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_nllb_moe import (
												    NLLB_MOE_PRETRAINED_MODEL_ARCHIVE_LIST,
												    NllbMoeForConditionalGeneration,
												    NllbMoeModel,
												    NllbMoePreTrainedModel,
												    NllbMoeSparseMLP,
												    NllbMoeTopaRouter,
												)


else:
						import sys

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






