



"""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  __SCREAMING_SNAKE_CASE			:


					'''simple docstring'''





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



					def 	snake_case ( self							:						Optional[Any]  )->      List[Any]:
								lowerCamelCase__	:	Optional[int]				=ids_tensor([self.batch_size, self.seq_length],						self.vocab_size  )

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

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

								lowerCamelCase__	:	Tuple				=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,						)

								lowerCamelCase__	:	List[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 	snake_case ( self							:						Any,						lowerCamelCase							:						Optional[int],						lowerCamelCase							:						str,						lowerCamelCase							:						List[Any],						lowerCamelCase							:						Any,						*lowerCamelCase							:						str  )->      Any:
								lowerCamelCase__	:	str				=OpenAIGPTModel(config=UpperCAmelCase__  )
								model.to(UpperCAmelCase__  )
								model.eval()

								lowerCamelCase__	:	Optional[Any]				=model(UpperCAmelCase__,						token_type_ids=UpperCAmelCase__,						head_mask=UpperCAmelCase__  )
								lowerCamelCase__	:	int				=model(UpperCAmelCase__,						token_type_ids=UpperCAmelCase__  )
								lowerCamelCase__	:	Optional[Any]				=model(UpperCAmelCase__  )

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



					def 	snake_case ( self							:						int,						lowerCamelCase							:						List[str],						lowerCamelCase							:						Union[str, Any],						lowerCamelCase							:						Tuple,						lowerCamelCase							:						Dict,						*lowerCamelCase							:						Tuple  )->      Optional[Any]:
								lowerCamelCase__	:	List[str]				=OpenAIGPTLMHeadModel(UpperCAmelCase__  )
								model.to(UpperCAmelCase__  )
								model.eval()

								lowerCamelCase__	:	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 	snake_case ( self							:						List[str],						lowerCamelCase							:						Tuple,						lowerCamelCase							:						Optional[int],						lowerCamelCase							:						List[str],						lowerCamelCase							:						int,						*lowerCamelCase							:						Optional[Any]  )->      Union[str, Any]:
								lowerCamelCase__	:	str				=OpenAIGPTDoubleHeadsModel(UpperCAmelCase__  )
								model.to(UpperCAmelCase__  )
								model.eval()

								lowerCamelCase__	:	int				=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 	snake_case ( self							:						Optional[Any],						lowerCamelCase							:						Any,						lowerCamelCase							:						Any,						lowerCamelCase							:						int,						lowerCamelCase							:						Union[str, Any],						*lowerCamelCase							:						Optional[int]  )->      Dict:
								lowerCamelCase__	:	Dict				=self.num_labels
								lowerCamelCase__	:	List[str]				=OpenAIGPTForSequenceClassification(UpperCAmelCase__  )
								model.to(UpperCAmelCase__  )
								model.eval()

								lowerCamelCase__	:	Union[str, Any]				=ids_tensor([self.batch_size],						self.type_sequence_label_size  )
								lowerCamelCase__	:	str				=model(UpperCAmelCase__,						token_type_ids=UpperCAmelCase__,						labels=UpperCAmelCase__  )
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.num_labels)  )




					def 	snake_case ( self							:						List[str]  )->      str:
								lowerCamelCase__	:	int				=self.prepare_config_and_inputs()
								(
								    lowerCamelCase__	
								)	:	List[Any]				=config_and_inputs
								lowerCamelCase__	:	Tuple				={
								    '''input_ids''': input_ids,
								    '''token_type_ids''': token_type_ids,
								    '''head_mask''': head_mask,
								}

								return config, inputs_dict





@require_torch
class  __SCREAMING_SNAKE_CASE			(   lowerCAmelCase_	,     lowerCAmelCase_	,     lowerCAmelCase_	,     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 	snake_case ( self							:						Optional[int],						lowerCamelCase							:						int,						lowerCamelCase							:						Tuple,						lowerCamelCase							:						Optional[int],						lowerCamelCase							:						Dict,						lowerCamelCase							:						List[str]  )->      Optional[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 	snake_case ( self							:						Dict,						lowerCamelCase							:						List[Any],						lowerCamelCase							:						List[Any],						lowerCamelCase							:						Optional[Any]=False  )->      Union[str, Any]:
								lowerCamelCase__	:	str				=super()._prepare_for_class(UpperCAmelCase__,						UpperCAmelCase__,						return_labels=UpperCAmelCase__  )

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



					def 	snake_case ( self							:						str  )->      Tuple:
								lowerCamelCase__	:	Optional[Any]				=OpenAIGPTModelTester(self  )
								lowerCamelCase__	:	int				=ConfigTester(self,						config_class=UpperCAmelCase__,						n_embd=37  )



					def 	snake_case ( self							:						Optional[int]  )->      List[Any]:
								self.config_tester.run_common_tests()



					def 	snake_case ( self							:						int  )->      Tuple:
								lowerCamelCase__	:	Tuple				=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase__  )



					def 	snake_case ( self							:						str  )->      Union[str, Any]:
								lowerCamelCase__	:	Any				=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase__  )



					def 	snake_case ( self							:						Any  )->      Dict:
								lowerCamelCase__	:	List[str]				=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_double_lm_head_model(*UpperCAmelCase__  )



					def 	snake_case ( self							:						List[str]  )->      Optional[int]:
								lowerCamelCase__	:	Dict				=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase__  )




					@slow
					def 	snake_case ( self							:						Union[str, Any]  )->      List[str]:
								for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCamelCase__	:	Any				=OpenAIGPTModel.from_pretrained(UpperCAmelCase__  )
											self.assertIsNotNone(UpperCAmelCase__  )





@require_torch
class  __SCREAMING_SNAKE_CASE			(   unittest.TestCase     ):


					'''simple docstring'''





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

								lowerCamelCase__	:	Tuple				=model.generate(UpperCAmelCase__,						do_sample=UpperCAmelCase__  )
								self.assertListEqual(output_ids[0].tolist(),						UpperCAmelCase__  )









"""simple docstring"""






def   snake_case__     (	__lowerCamelCase       :					list[list[int]]							,	__lowerCamelCase       :					int							,	__lowerCamelCase       :					int							,	__lowerCamelCase       :					list[int]			):
			"""simple docstring"""

			# 1. Validate that path exists between current and next vertices
			if graph[path[curr_ind - 1]][next_ver] == 0:
						return False

			# 2. Validate that next vertex is not already in path
			return not any(vertex == next_ver for vertex in path			)






def   snake_case__     (	__lowerCamelCase       :					list[list[int]]							,	__lowerCamelCase       :					list[int]							,	__lowerCamelCase       :					int			):
			"""simple docstring"""

			# Base Case
			if curr_ind == len(__lowerCamelCase			):
						# return whether path exists between current and starting vertices
						return graph[path[curr_ind - 1]][path[0]] == 1

			# Recursive Step
			for next_ver in range(0							,	len(__lowerCamelCase			)			):
						if valid_connection(__lowerCamelCase							,	__lowerCamelCase							,	__lowerCamelCase							,	__lowerCamelCase			):
									# Insert current vertex  into path as next transition
									lowerCamelCase__	:	Tuple				=next_ver
									# Validate created path
									if util_hamilton_cycle(__lowerCamelCase							,	__lowerCamelCase							,	curr_ind + 1			):
												return True
									# Backtrack
									lowerCamelCase__	:	int				=-1
			return False






def   snake_case__     (	__lowerCamelCase       :					list[list[int]]							,	__lowerCamelCase       :					int = 0			):
			"""simple docstring"""
			lowerCamelCase__	:	Tuple				=[-1] * (len(__lowerCamelCase			) + 1)
			# initialize start and end of path with starting index
			lowerCamelCase__	:	Union[str, Any]				=start_index
			# evaluate and if we find answer return path either return empty array
			return path if util_hamilton_cycle(__lowerCamelCase							,	__lowerCamelCase							,	1			) else []




