

'''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   UpperCamelCase__    :


		"""simple docstring"""

		def __init__( self    ,			snake_case    ,			snake_case=1_3    ,			snake_case=7    ,			snake_case=True    ,			snake_case=True    ,			snake_case=True    ,			snake_case=9_9    ,			snake_case=3_2    ,			snake_case=5    ,			snake_case=4    ,			snake_case=3_7    ,			snake_case="gelu"    ,			snake_case=0.1    ,			snake_case=0.1    ,			snake_case=5_1_2    ,			snake_case=1_6    ,			snake_case=2    ,			snake_case=0.02    ,			snake_case=3    ,			snake_case=4    ,			snake_case=None    ,			):


							'''simple docstring'''

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







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							List[str]   					=   ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size			)

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

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

							UpperCAmelCase		:							int   					=   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		:							List[str]   					=   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    ,			snake_case    ,			snake_case    ,			snake_case    ,			snake_case    ,			*snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							List[Any]   					=   OpenAIGPTModel(config=__snake_case			)
							model.to(__snake_case			)
							model.eval()

							UpperCAmelCase		:							List[str]   					=   model(__snake_case    ,			token_type_ids=__snake_case    ,			head_mask=__snake_case			)
							UpperCAmelCase		:							Dict   					=   model(__snake_case    ,			token_type_ids=__snake_case			)
							UpperCAmelCase		:							Dict   					=   model(__snake_case			)

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







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case    ,			snake_case    ,			*snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[Any]   					=   OpenAIGPTLMHeadModel(__snake_case			)
							model.to(__snake_case			)
							model.eval()

							UpperCAmelCase		:							Any   					=   model(__snake_case    ,			token_type_ids=__snake_case    ,			labels=__snake_case			)
							self.parent.assertEqual(result.loss.shape    ,			()			)
							self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size)			)







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case    ,			snake_case    ,			*snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							Any   					=   OpenAIGPTDoubleHeadsModel(__snake_case			)
							model.to(__snake_case			)
							model.eval()

							UpperCAmelCase		:							int   					=   model(__snake_case    ,			token_type_ids=__snake_case    ,			labels=__snake_case			)
							self.parent.assertEqual(result.loss.shape    ,			()			)
							self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size)			)







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case    ,			snake_case    ,			*snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							Tuple   					=   self.num_labels
							UpperCAmelCase		:							Optional[int]   					=   OpenAIGPTForSequenceClassification(__snake_case			)
							model.to(__snake_case			)
							model.eval()

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







		def       A_       ( self			):


							'''simple docstring'''

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

							return config, inputs_dict







@require_torch
class   UpperCamelCase__    (   lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_							,							unittest.TestCase   ):


		"""simple docstring"""

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







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case    ,			snake_case    ,			snake_case			):


							'''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    ,			snake_case    ,			snake_case    ,			snake_case=False			):


							'''simple docstring'''

							UpperCAmelCase		:							List[str]   					=   super()._prepare_for_class(__snake_case    ,			__snake_case    ,			return_labels=__snake_case			)

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







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Union[str, Any]   					=   OpenAIGPTModelTester(self			)
							UpperCAmelCase		:							str   					=   ConfigTester(self    ,			config_class=__snake_case    ,			n_embd=3_7			)







		def       A_       ( self			):


							'''simple docstring'''

							self.config_tester.run_common_tests()







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							List[Any]   					=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_model(*__snake_case			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Dict   					=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_lm_head_model(*__snake_case			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							str   					=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_double_lm_head_model(*__snake_case			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Any   					=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__snake_case			)







		@slow
		def       A_       ( self			):


							'''simple docstring'''

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







@require_torch
class   UpperCamelCase__    (   unittest.TestCase   ):


		"""simple docstring"""

		@slow
		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[int]   					=   OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"			)
							model.to(__snake_case			)
							UpperCAmelCase		:							Optional[Any]   					=   torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]    ,			dtype=torch.long    ,			device=__snake_case			)  # the president is
							UpperCAmelCase		:							Tuple   					=   [
							    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

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



from __future__ import annotations






def        UpperCamelCase_(							_snake_case   :  int	):




			"""simple docstring"""






			__a        =str(_snake_case	)
			return len(_snake_case	) == 9 and set(_snake_case	) == set('123456789'	)






def        UpperCamelCase_(							):




			"""simple docstring"""






			for base_num in range(9999       ,     4999       ,     -1	):
						__a        =100002 * base_num
						if is_9_pandigital(_snake_case	):
									return candidate

			for base_num in range(333       ,     99       ,     -1	):
						__a        =1002003 * base_num
						if is_9_pandigital(_snake_case	):
									return candidate

			return None


if __name__ == "__main__":
						print(f'''{solution() = }''')






