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





				def __init__(		self :		List[str]					,      snake_case_ :		Tuple					,      ):


								"""simple docstring"""

								A     :  Any   =				parent
								A     :  Dict   =				13
								A     :  Union[str, Any]   =				7
								A     :  str   =				True
								A     :  int   =				True
								A     :  Tuple   =				True
								A     :  Optional[Any]   =				True
								A     :  Any   =				True
								A     :  Any   =				99
								A     :  Tuple   =				32
								A     :  List[Any]   =				2
								A     :  int   =				4
								A     :  Dict   =				37
								A     :  Optional[Any]   =				'''gelu'''
								A     :  str   =				0.1
								A     :  List[str]   =				0.1
								A     :  Optional[Any]   =				512
								A     :  Tuple   =				16
								A     :  List[str]   =				2
								A     :  Dict   =				0.02
								A     :  List[str]   =				3
								A     :  Optional[Any]   =				4
								A     :  int   =				None
								A     :  List[Any]   =				self.vocab_size - 1





				def        _UpperCAmelCase						(		self :		Union[str, Any]							):


								"""simple docstring"""

								A     :  Tuple   =				ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size							)

								A     :  Tuple   =				None
								if self.use_input_mask:
												A     :  Any   =				random_attention_mask([self.batch_size, self.seq_length]							)

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

								A     :  Any   =				None
								if self.use_mc_token_ids:
												A     :  List[Any]   =				ids_tensor([self.batch_size, self.num_choices]					,      self.seq_length							)

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

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

								A     :  Union[str, Any]   =				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 :		Union[str, Any]					,      snake_case_ :		List[Any]					,      snake_case_ :		int					,      snake_case_ :		Union[str, Any]					,      snake_case_ :		int					,      snake_case_ :		List[str]					,      *snake_case_ :		str							):


								"""simple docstring"""

								A     :  List[Any]   =				TFOpenAIGPTModel(config=_snake_case							)
								A     :  Union[str, Any]   =				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
								A     :  List[str]   =				model(_snake_case							)

								A     :  List[str]   =				[input_ids, input_mask]
								A     :  Dict   =				model(_snake_case							)

								A     :  Optional[Any]   =				model(_snake_case							)

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





				def        _UpperCAmelCase						(		self :		Union[str, Any]					,      snake_case_ :		int					,      snake_case_ :		Optional[Any]					,      snake_case_ :		List[Any]					,      snake_case_ :		Any					,      snake_case_ :		Union[str, Any]					,      *snake_case_ :		Optional[int]							):


								"""simple docstring"""

								A     :  Optional[int]   =				TFOpenAIGPTLMHeadModel(config=_snake_case							)
								A     :  Tuple   =				{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
								A     :  Union[str, Any]   =				model(_snake_case							)
								self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.seq_length, self.vocab_size)							)





				def        _UpperCAmelCase						(		self :		str					,      snake_case_ :		Optional[int]					,      snake_case_ :		Any					,      snake_case_ :		Optional[int]					,      snake_case_ :		Tuple					,      snake_case_ :		Optional[int]					,      snake_case_ :		Optional[int]					,      *snake_case_ :		List[str]							):


								"""simple docstring"""

								A     :  List[Any]   =				TFOpenAIGPTDoubleHeadsModel(config=_snake_case							)

								A     :  List[Any]   =				tf.tile(tf.expand_dims(_snake_case					,      1							)					,      (1, self.num_choices, 1)							)
								A     :  str   =				tf.tile(tf.expand_dims(_snake_case					,      1							)					,      (1, self.num_choices, 1)							)
								A     :  int   =				tf.tile(tf.expand_dims(_snake_case					,      1							)					,      (1, self.num_choices, 1)							)

								A     :  List[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,
								}
								A     :  Tuple   =				model(_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 :		Optional[int]					,      snake_case_ :		int					,      snake_case_ :		Tuple					,      snake_case_ :		Optional[Any]					,      snake_case_ :		Optional[int]					,      snake_case_ :		Tuple					,      *snake_case_ :		Union[str, Any]							):


								"""simple docstring"""

								A     :  List[str]   =				self.num_labels
								A     :  Dict   =				ids_tensor([self.batch_size]					,      self.type_sequence_label_size							)
								A     :  str   =				{
								    '''input_ids''': input_ids,
								    '''attention_mask''': input_mask,
								    '''token_type_ids''': token_type_ids,
								    '''labels''': sequence_labels,
								}
								A     :  Dict   =				TFOpenAIGPTForSequenceClassification(_snake_case							)
								A     :  str   =				model(_snake_case							)
								self.parent.assertEqual(result.logits.shape					,      (self.batch_size, self.num_labels)							)






				def        _UpperCAmelCase						(		self :		Any							):


								"""simple docstring"""

								A     :  Tuple   =				self.prepare_config_and_inputs()

								(
								    A							
								)     :  List[str]   =				config_and_inputs

								A     :  int   =				{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
								return config, inputs_dict



@require_tf
class     _SCREAMING_SNAKE_CASE							(		a_,    a_,    unittest.TestCase							):
				lowerCamelCase_       =     (
				    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
				    if is_tf_available()
				    else ()
				)
				lowerCamelCase_       =     (
				    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
				)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
				lowerCamelCase_       =     (
				    {
				        'feature-extraction': TFOpenAIGPTModel,
				        'text-classification': TFOpenAIGPTForSequenceClassification,
				        'text-generation': TFOpenAIGPTLMHeadModel,
				        'zero-shot': TFOpenAIGPTForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				lowerCamelCase_       =     False
				lowerCamelCase_       =     False





				def        _UpperCAmelCase						(		self :		List[Any]					,      snake_case_ :		Union[str, Any]					,      snake_case_ :		Optional[int]					,      snake_case_ :		str					,      snake_case_ :		Dict					,      snake_case_ :		int							):


								"""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 :		int							):


								"""simple docstring"""

								A     :  Dict   =				TFOpenAIGPTModelTester(self							)
								A     :  Optional[int]   =				ConfigTester(self					,      config_class=_snake_case					,      n_embd=37							)





				def        _UpperCAmelCase						(		self :		Any							):


								"""simple docstring"""

								self.config_tester.run_common_tests()





				def        _UpperCAmelCase						(		self :		List[str]							):


								"""simple docstring"""

								A     :  int   =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_model(*_snake_case							)





				def        _UpperCAmelCase						(		self :		Union[str, Any]							):


								"""simple docstring"""

								A     :  Optional[int]   =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_lm_head(*_snake_case							)





				def        _UpperCAmelCase						(		self :		Dict							):


								"""simple docstring"""

								A     :  Dict   =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_double_head(*_snake_case							)





				def        _UpperCAmelCase						(		self :		Any							):


								"""simple docstring"""

								A     :  Optional[int]   =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_snake_case							)






				@slow
				def        _UpperCAmelCase						(		self :		Tuple							):


								"""simple docstring"""

								for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												A     :  int   =				TFOpenAIGPTModel.from_pretrained(_snake_case							)
												self.assertIsNotNone(_snake_case							)



@require_tf
class     _SCREAMING_SNAKE_CASE							(		unittest.TestCase							):





				@slow
				def        _UpperCAmelCase						(		self :		Any							):


								"""simple docstring"""

								A     :  Union[str, Any]   =				TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''							)
								A     :  Optional[Any]   =				tf.convert_to_tensor([[481, 4735, 544]]					,      dtype=tf.intaa							)  # the president is
								A     :  Union[str, Any]   =				[
								    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

								A     :  Union[str, Any]   =				model.generate(_snake_case					,      do_sample=_snake_case							)
								self.assertListEqual(output_ids[0].numpy().tolist()					,      _snake_case							)

UpperCamelCase_  =	{
    "0": 1,
    "1": 1,
    "2": 2,
    "3": 6,
    "4": 24,
    "5": 1_20,
    "6": 7_20,
    "7": 50_40,
    "8": 4_03_20,
    "9": 36_28_80,
}

UpperCamelCase_  =	{1_45: 1_45}

UpperCamelCase_  =	{
    1_45: 0,
    1_69: 3,
    3_63_01: 3,
    14_54: 3,
    8_71: 2,
    4_53_61: 2,
    8_72: 2,
}





def 							_lowerCamelCase			(					lowerCamelCase_:  int		):



				'''simple docstring'''







				if n in CACHE_SUM_DIGIT_FACTORIALS:
								return CACHE_SUM_DIGIT_FACTORIALS[n]
				A     :  Tuple   =				sum(DIGIT_FACTORIALS[let] for let in str(lowerCamelCase_		)		)
				A     :  Dict   =				ret
				return ret





def 							_lowerCamelCase			(					lowerCamelCase_:  int				,	lowerCamelCase_:  set | None = None		):



				'''simple docstring'''







				A     :  Optional[int]   =				previous or set()
				if n in CHAIN_LENGTH_CACHE:
								return CHAIN_LENGTH_CACHE[n]
				A     :  Dict   =				sum_digit_factorials(lowerCamelCase_		)
				if next_number in previous:
								A     :  Optional[int]   =				0
								return 0
				else:
								previous.add(lowerCamelCase_		)
								A     :  str   =				1 + chain_length(lowerCamelCase_				,	lowerCamelCase_		)
								A     :  Any   =				ret
								return ret





def 							_lowerCamelCase			(					lowerCamelCase_:  int = 60				,	lowerCamelCase_:  int = 100_0000		):



				'''simple docstring'''







				return sum(1 for i in range(1				,	lowerCamelCase_		) if chain_length(lowerCamelCase_		) == num_terms		)


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