


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


					def __init__(    self						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		Optional[int]  ,     SCREAMING_SNAKE_CASE						:		int=1_3  ,     SCREAMING_SNAKE_CASE						:		List[str]=7  ,     SCREAMING_SNAKE_CASE						:		List[str]=True  ,     SCREAMING_SNAKE_CASE						:		List[str]=True  ,     SCREAMING_SNAKE_CASE						:		Optional[Any]=True  ,     SCREAMING_SNAKE_CASE						:		List[str]=9_9  ,     SCREAMING_SNAKE_CASE						:		str=3_2  ,     SCREAMING_SNAKE_CASE						:		Any=5  ,     SCREAMING_SNAKE_CASE						:		str=4  ,     SCREAMING_SNAKE_CASE						:		List[Any]=3_7  ,     SCREAMING_SNAKE_CASE						:		List[str]="gelu"  ,     SCREAMING_SNAKE_CASE						:		Dict=0.1  ,     SCREAMING_SNAKE_CASE						:		Tuple=0.1  ,     SCREAMING_SNAKE_CASE						:		Dict=5_1_2  ,     SCREAMING_SNAKE_CASE						:		Optional[int]=1_6  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any]=2  ,     SCREAMING_SNAKE_CASE						:		Any=0.02  ,     SCREAMING_SNAKE_CASE						:		int=3  ,     SCREAMING_SNAKE_CASE						:		str=4  ,     SCREAMING_SNAKE_CASE						:		str=None  ,     )      ->						List[str]:




											'''simple docstring'''

											__snake_case				=		parent
											__snake_case				=		batch_size
											__snake_case				=		seq_length
											__snake_case				=		is_training
											__snake_case				=		use_token_type_ids
											__snake_case				=		use_labels
											__snake_case				=		vocab_size
											__snake_case				=		hidden_size
											__snake_case				=		num_hidden_layers
											__snake_case				=		num_attention_heads
											__snake_case				=		intermediate_size
											__snake_case				=		hidden_act
											__snake_case				=		hidden_dropout_prob
											__snake_case				=		attention_probs_dropout_prob
											__snake_case				=		max_position_embeddings
											__snake_case				=		type_vocab_size
											__snake_case				=		type_sequence_label_size
											__snake_case				=		initializer_range
											__snake_case				=		num_labels
											__snake_case				=		num_choices
											__snake_case				=		scope
											__snake_case				=		self.vocab_size - 1


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[Any]    )      ->						str:




											'''simple docstring'''

											__snake_case				=		ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size    )

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

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

											__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  ,     )

											__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 				SCREAMING_SNAKE_CASE_	(    self						:		int  ,     SCREAMING_SNAKE_CASE						:		int  ,     SCREAMING_SNAKE_CASE						:		Optional[int]  ,     SCREAMING_SNAKE_CASE						:		Optional[Any]  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any]  ,     *SCREAMING_SNAKE_CASE						:		int    )      ->						str:




											'''simple docstring'''

											__snake_case				=		OpenAIGPTModel(config=SCREAMING_SNAKE_CASE    )
											model.to(SCREAMING_SNAKE_CASE    )
											model.eval()

											__snake_case				=		model(SCREAMING_SNAKE_CASE  ,     token_type_ids=SCREAMING_SNAKE_CASE  ,     head_mask=SCREAMING_SNAKE_CASE    )
											__snake_case				=		model(SCREAMING_SNAKE_CASE  ,     token_type_ids=SCREAMING_SNAKE_CASE    )
											__snake_case				=		model(SCREAMING_SNAKE_CASE    )

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


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Any  ,     SCREAMING_SNAKE_CASE						:		Any  ,     SCREAMING_SNAKE_CASE						:		List[Any]  ,     SCREAMING_SNAKE_CASE						:		Dict  ,     SCREAMING_SNAKE_CASE						:		Optional[Any]  ,     *SCREAMING_SNAKE_CASE						:		Any    )      ->						List[Any]:




											'''simple docstring'''

											__snake_case				=		OpenAIGPTLMHeadModel(SCREAMING_SNAKE_CASE    )
											model.to(SCREAMING_SNAKE_CASE    )
											model.eval()

											__snake_case				=		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 				SCREAMING_SNAKE_CASE_	(    self						:		List[str]  ,     SCREAMING_SNAKE_CASE						:		List[Any]  ,     SCREAMING_SNAKE_CASE						:		Union[str, Any]  ,     SCREAMING_SNAKE_CASE						:		str  ,     SCREAMING_SNAKE_CASE						:		Optional[int]  ,     *SCREAMING_SNAKE_CASE						:		Tuple    )      ->						Optional[Any]:




											'''simple docstring'''

											__snake_case				=		OpenAIGPTDoubleHeadsModel(SCREAMING_SNAKE_CASE    )
											model.to(SCREAMING_SNAKE_CASE    )
											model.eval()

											__snake_case				=		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 				SCREAMING_SNAKE_CASE_	(    self						:		Dict  ,     SCREAMING_SNAKE_CASE						:		Dict  ,     SCREAMING_SNAKE_CASE						:		List[Any]  ,     SCREAMING_SNAKE_CASE						:		Dict  ,     SCREAMING_SNAKE_CASE						:		Optional[int]  ,     *SCREAMING_SNAKE_CASE						:		Dict    )      ->						int:




											'''simple docstring'''

											__snake_case				=		self.num_labels
											__snake_case				=		OpenAIGPTForSequenceClassification(SCREAMING_SNAKE_CASE    )
											model.to(SCREAMING_SNAKE_CASE    )
											model.eval()

											__snake_case				=		ids_tensor([self.batch_size]  ,     self.type_sequence_label_size    )
											__snake_case				=		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 				SCREAMING_SNAKE_CASE_	(    self						:		List[str]    )      ->						int:




											'''simple docstring'''

											__snake_case				=		self.prepare_config_and_inputs()
											(
											    (
											    __snake_case
											)		,       (
											    __snake_case
											)		,       (
											    __snake_case
											)		,       (
											    __snake_case
											)		,       (
											    __snake_case
											)		,       (
											    __snake_case
											)		,       (
											    __snake_case
											)		,       
											)				=		config_and_inputs
											__snake_case				=		{
											    "input_ids": input_ids,
											    "token_type_ids": token_type_ids,
											    "head_mask": head_mask,
											}

											return config, inputs_dict






@require_torch
class      UpperCamelCase(	_a  ,				_a  ,				_a  ,				unittest.TestCase ):
					snake_case_		:					int    					=						(
					    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
					    if is_torch_available()
					    else ()
					)
					snake_case_		:					Optional[Any]    					=						(
					    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
					)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
					snake_case_		:					Any    					=						(
					    {
					        """feature-extraction""": OpenAIGPTModel,
					        """text-classification""": OpenAIGPTForSequenceClassification,
					        """text-generation""": OpenAIGPTLMHeadModel,
					        """zero-shot""": OpenAIGPTForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)


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




											'''simple docstring'''

											__snake_case				=		super()._prepare_for_class(SCREAMING_SNAKE_CASE  ,     SCREAMING_SNAKE_CASE  ,     return_labels=SCREAMING_SNAKE_CASE    )

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


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




											'''simple docstring'''

											__snake_case				=		OpenAIGPTModelTester(self    )
											__snake_case				=		ConfigTester(self  ,     config_class=SCREAMING_SNAKE_CASE  ,     n_embd=3_7    )


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[int]    )      ->						Any:




											'''simple docstring'''

											self.config_tester.run_common_tests()


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




											'''simple docstring'''

											__snake_case				=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_model(*SCREAMING_SNAKE_CASE    )


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




											'''simple docstring'''

											__snake_case				=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_lm_head_model(*SCREAMING_SNAKE_CASE    )


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




											'''simple docstring'''

											__snake_case				=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_double_lm_head_model(*SCREAMING_SNAKE_CASE    )


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Tuple    )      ->						int:




											'''simple docstring'''

											__snake_case				=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*SCREAMING_SNAKE_CASE    )






					@slow
					def 				SCREAMING_SNAKE_CASE_	(    self						:		Tuple    )      ->						List[Any]:




											'''simple docstring'''

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






@require_torch
class      UpperCamelCase(	unittest.TestCase ):


					@slow
					def 				SCREAMING_SNAKE_CASE_	(    self						:		Any    )      ->						Dict:




											'''simple docstring'''

											__snake_case				=		OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"    )
											model.to(SCREAMING_SNAKE_CASE    )
											__snake_case				=		torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]  ,     dtype=torch.long  ,     device=SCREAMING_SNAKE_CASE    )  # the president is
											__snake_case				=		[
											    4_8_1,
											    4_7_3_5,
											    5_4_4,
											    2_4_6,
											    9_6_3,
											    8_7_0,
											    7_6_2,
											    2_3_9,
											    2_4_4,
											    4_0_4_7_7,
											    2_4_4,
											    2_4_9,
											    7_1_9,
											    8_8_1,
											    4_8_7,
											    5_4_4,
											    2_4_0,
											    2_4_4,
											    6_0_3,
											    4_8_1,
											]  # the president is a very good man. " \n " i\'m sure he is, " said the

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









import inspect
import unittest






class      UpperCamelCase(	unittest.TestCase ):


					def 				SCREAMING_SNAKE_CASE_	(    self						:		Dict    )      ->						List[Any]:




											'''simple docstring'''

											try:
																	import diffusers  # noqa: F401
											except ImportError:
																	assert False






					def 				SCREAMING_SNAKE_CASE_	(    self						:		Optional[int]    )      ->						Any:




											'''simple docstring'''

											import diffusers
											from diffusers.dependency_versions_table import deps

											__snake_case				=		inspect.getmembers(SCREAMING_SNAKE_CASE  ,     inspect.isclass    )

											for cls_name, cls_module in all_classes:
																	if "dummy_" in cls_module.__module__:
																							for backend in cls_module._backends:
																													if backend == "k_diffusion":
																																			__snake_case				=		"k-diffusion"
																													elif backend == "invisible_watermark":
																																			__snake_case				=		"invisible-watermark"
																													assert backend in deps, f'''{backend} is not in the deps table!'''





