


"""simple docstring"""






from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers.models.openai.modeling_tf_openai import (
					    TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TFOpenAIGPTDoubleHeadsModel,
					    TFOpenAIGPTForSequenceClassification,
					    TFOpenAIGPTLMHeadModel,
					    TFOpenAIGPTModel,
					)






class     A:

		"""simple docstring"""





		def __init__(   self				,							SCREAMING_SNAKE_CASE__				,							)			->  Any:

					"""simple docstring"""



					_UpperCamelCase				:Optional[int]				    =  parent
					_UpperCamelCase				:Dict				    =  13
					_UpperCamelCase				:Union[str, Any]				    =  7
					_UpperCamelCase				:List[str]				    =  True
					_UpperCamelCase				:Optional[Any]				    =  True
					_UpperCamelCase				:Union[str, Any]				    =  True
					_UpperCamelCase				:Union[str, Any]				    =  True
					_UpperCamelCase				:int				    =  True
					_UpperCamelCase				:int				    =  99
					_UpperCamelCase				:List[str]				    =  32
					_UpperCamelCase				:Optional[int]				    =  2
					_UpperCamelCase				:List[str]				    =  4
					_UpperCamelCase				:List[str]				    =  37
					_UpperCamelCase				:Optional[int]				    =  '''gelu'''
					_UpperCamelCase				:Optional[Any]				    =  0.1
					_UpperCamelCase				:Dict				    =  0.1
					_UpperCamelCase				:Tuple				    =  5_12
					_UpperCamelCase				:Any				    =  16
					_UpperCamelCase				:Optional[Any]				    =  2
					_UpperCamelCase				:int				    =  0.0_2
					_UpperCamelCase				:Union[str, Any]				    =  3
					_UpperCamelCase				:str				    =  4
					_UpperCamelCase				:Any				    =  None
					_UpperCamelCase				:Dict				    =  self.vocab_size - 1
		def      _UpperCamelCase(   self      )			->  Any:

					"""simple docstring"""



					_UpperCamelCase				:Any				    =  ids_tensor([self.batch_size, self.seq_length]				,							self.vocab_size      )

					_UpperCamelCase				:List[Any]				    =  None
					if self.use_input_mask:
								_UpperCamelCase				:str				    =  random_attention_mask([self.batch_size, self.seq_length]      )

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

					_UpperCamelCase				:Optional[int]				    =  None
					if self.use_mc_token_ids:
								_UpperCamelCase				:int				    =  ids_tensor([self.batch_size, self.num_choices]				,							self.seq_length      )

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

					_UpperCamelCase				: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				,							)

					_UpperCamelCase				:Dict				    =  ids_tensor([self.num_hidden_layers, self.num_attention_heads]				,							2      )

					return (
					    config,
					    input_ids,
					    input_mask,
					    head_mask,
					    token_type_ids,
					    mc_token_ids,
					    sequence_labels,
					    token_labels,
					    choice_labels,
					)
		def      _UpperCamelCase(   self				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							*SCREAMING_SNAKE_CASE__      )			->  str:

					"""simple docstring"""



					_UpperCamelCase				:Optional[int]				    =  TFOpenAIGPTModel(config=SCREAMING_SNAKE_CASE__      )
					_UpperCamelCase				:Dict				    =  {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					_UpperCamelCase				:List[str]				    =  model(SCREAMING_SNAKE_CASE__      )

					_UpperCamelCase				:List[Any]				    =  [input_ids, input_mask]
					_UpperCamelCase				:str				    =  model(SCREAMING_SNAKE_CASE__      )

					_UpperCamelCase				:Optional[int]				    =  model(SCREAMING_SNAKE_CASE__      )

					self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)      )
		def      _UpperCamelCase(   self				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							*SCREAMING_SNAKE_CASE__      )			->  Dict:

					"""simple docstring"""



					_UpperCamelCase				:Optional[Any]				    =  TFOpenAIGPTLMHeadModel(config=SCREAMING_SNAKE_CASE__      )
					_UpperCamelCase				:Union[str, Any]				    =  {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					_UpperCamelCase				:Optional[int]				    =  model(SCREAMING_SNAKE_CASE__      )
					self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.seq_length, self.vocab_size)      )
		def      _UpperCamelCase(   self				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							*SCREAMING_SNAKE_CASE__      )			->  Optional[Any]:

					"""simple docstring"""



					_UpperCamelCase				:str				    =  TFOpenAIGPTDoubleHeadsModel(config=SCREAMING_SNAKE_CASE__      )

					_UpperCamelCase				:Optional[int]				    =  tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE__				,							1      )				,							(1, self.num_choices, 1)      )
					_UpperCamelCase				:int				    =  tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE__				,							1      )				,							(1, self.num_choices, 1)      )
					_UpperCamelCase				:Dict				    =  tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE__				,							1      )				,							(1, self.num_choices, 1)      )

					_UpperCamelCase				:str				    =  {
					    '''input_ids''': multiple_choice_inputs_ids,
					    '''mc_token_ids''': mc_token_ids,
					    '''attention_mask''': multiple_choice_input_mask,
					    '''token_type_ids''': multiple_choice_token_type_ids,
					}
					_UpperCamelCase				:int				    =  model(SCREAMING_SNAKE_CASE__      )
					self.parent.assertEqual(
					    result.logits.shape				,							(self.batch_size, self.num_choices, self.seq_length, self.vocab_size)      )
					self.parent.assertEqual(result.mc_logits.shape				,							(self.batch_size, self.num_choices)      )
		def      _UpperCamelCase(   self				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							*SCREAMING_SNAKE_CASE__      )			->  List[str]:

					"""simple docstring"""



					_UpperCamelCase				:Optional[Any]				    =  self.num_labels
					_UpperCamelCase				:Optional[Any]				    =  ids_tensor([self.batch_size]				,							self.type_sequence_label_size      )
					_UpperCamelCase				:Any				    =  {
					    '''input_ids''': input_ids,
					    '''attention_mask''': input_mask,
					    '''token_type_ids''': token_type_ids,
					    '''labels''': sequence_labels,
					}
					_UpperCamelCase				:Optional[int]				    =  TFOpenAIGPTForSequenceClassification(SCREAMING_SNAKE_CASE__      )
					_UpperCamelCase				:str				    =  model(SCREAMING_SNAKE_CASE__      )
					self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.num_labels)      )





		def      _UpperCamelCase(   self      )			->  List[Any]:

					"""simple docstring"""



					_UpperCamelCase				:int				    =  self.prepare_config_and_inputs()

					(
					    (
					    _UpperCamelCase
					)    ,			(
					    _UpperCamelCase
					)    ,			(
					    _UpperCamelCase
					)    ,			(
					    _UpperCamelCase
					)    ,			(
					    _UpperCamelCase
					)    ,			(
					    _UpperCamelCase
					)    ,			(
					    _UpperCamelCase
					)    ,			(
					    _UpperCamelCase
					)    ,			(
					    _UpperCamelCase
					)    ,			
					)				:List[Any]				    =  config_and_inputs

					_UpperCamelCase				:Any				    =  {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
					return config, inputs_dict






@require_tf
class     A(     lowerCamelCase__							,       lowerCamelCase__							,       unittest.TestCase		):

		"""simple docstring"""





		A         =       (
		    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
		    if is_tf_available()
		    else ()
		)
		A         =       (
		    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
		)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
		A         =       (
		    {
		        "feature-extraction": TFOpenAIGPTModel,
		        "text-classification": TFOpenAIGPTForSequenceClassification,
		        "text-generation": TFOpenAIGPTLMHeadModel,
		        "zero-shot": TFOpenAIGPTForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		A         =       False
		A         =       False
		def      _UpperCamelCase(   self				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__      )			->  Dict:

					"""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      _UpperCamelCase(   self      )			->  str:

					"""simple docstring"""



					_UpperCamelCase				:Optional[int]				    =  TFOpenAIGPTModelTester(self      )
					_UpperCamelCase				:int				    =  ConfigTester(self				,							config_class=SCREAMING_SNAKE_CASE__				,							n_embd=37      )
		def      _UpperCamelCase(   self      )			->  Optional[Any]:

					"""simple docstring"""



					self.config_tester.run_common_tests()
		def      _UpperCamelCase(   self      )			->  Tuple:

					"""simple docstring"""



					_UpperCamelCase				:Dict				    =  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_model(*SCREAMING_SNAKE_CASE__      )
		def      _UpperCamelCase(   self      )			->  Tuple:

					"""simple docstring"""



					_UpperCamelCase				:Any				    =  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_lm_head(*SCREAMING_SNAKE_CASE__      )
		def      _UpperCamelCase(   self      )			->  List[Any]:

					"""simple docstring"""



					_UpperCamelCase				:Union[str, Any]				    =  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_double_head(*SCREAMING_SNAKE_CASE__      )
		def      _UpperCamelCase(   self      )			->  str:

					"""simple docstring"""



					_UpperCamelCase				:Optional[Any]				    =  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*SCREAMING_SNAKE_CASE__      )





		@slow
		def      _UpperCamelCase(   self      )			->  int:

					"""simple docstring"""



					for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								_UpperCamelCase				:List[str]				    =  TFOpenAIGPTModel.from_pretrained(SCREAMING_SNAKE_CASE__      )
								self.assertIsNotNone(SCREAMING_SNAKE_CASE__      )






@require_tf
class     A(     unittest.TestCase		):

		"""simple docstring"""





		@slow
		def      _UpperCamelCase(   self      )			->  Optional[Any]:

					"""simple docstring"""



					_UpperCamelCase				:Tuple				    =  TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''      )
					_UpperCamelCase				:Optional[int]				    =  tf.convert_to_tensor([[4_81, 47_35, 5_44]]				,							dtype=tf.intaa      )  # the president is
					_UpperCamelCase				:Optional[Any]				    =  [
					    4_81,
					    47_35,
					    5_44,
					    2_46,
					    9_63,
					    8_70,
					    7_62,
					    2_39,
					    2_44,
					    4_04_77,
					    2_44,
					    2_49,
					    7_19,
					    8_81,
					    4_87,
					    5_44,
					    2_40,
					    2_44,
					    6_03,
					    4_81,
					]  # the president is a very good man. " \n " i\'m sure he is, " said the

					_UpperCamelCase				:int				    =  model.generate(SCREAMING_SNAKE_CASE__				,							do_sample=SCREAMING_SNAKE_CASE__      )
					self.assertListEqual(output_ids[0].numpy().tolist()				,							SCREAMING_SNAKE_CASE__      )





"""simple docstring"""






def 							A_					(   snake_case__		)				->     str:
			_UpperCamelCase				:Tuple				    =  word.upper()
			_UpperCamelCase				:Dict				    =  0
			_UpperCamelCase				:Union[str, Any]				    =  {}
			_UpperCamelCase				:Optional[int]				    =  []

			for letter in word:
						if letter not in letter_nums:
									_UpperCamelCase				:Dict				    =  str(snake_case__		)
									next_num += 1
						word_pattern.append(letter_nums[letter]		)
			return ".".join(snake_case__		)


if __name__ == "__main__":
					import pprint
					import time

					UpperCamelCase__							:Dict       =  time.time()
					with open("""dictionary.txt""") as in_file:
										UpperCamelCase__							:str       =  in_file.read().splitlines()

					UpperCamelCase__							:dict       =  {}
					for word in word_list:
										UpperCamelCase__							:List[Any]       =  get_word_pattern(word)
										if pattern in all_patterns:
															all_patterns[pattern].append(word)
										else:
															UpperCamelCase__							:Tuple       =  [word]

					with open("""word_patterns.txt""", """w""") as out_file:
										out_file.write(pprint.pformat(all_patterns))

					UpperCamelCase__							:Dict       =  round(time.time() - start_time, 2)
					print(F"""Done!  {len(all_patterns):,} word patterns found in {total_time} seconds.""")
					# Done!  9,581 word patterns found in 0.58 seconds.

