


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





				"""simple docstring"""







				def __init__(    self							,		UpperCAmelCase__							,		):
								SCREAMING_SNAKE_CASE__  				=					parent
								SCREAMING_SNAKE_CASE__  				=					13
								SCREAMING_SNAKE_CASE__  				=					7
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					True
								SCREAMING_SNAKE_CASE__  				=					99
								SCREAMING_SNAKE_CASE__  				=					32
								SCREAMING_SNAKE_CASE__  				=					2
								SCREAMING_SNAKE_CASE__  				=					4
								SCREAMING_SNAKE_CASE__  				=					37
								SCREAMING_SNAKE_CASE__  				=					"gelu"
								SCREAMING_SNAKE_CASE__  				=					0.1
								SCREAMING_SNAKE_CASE__  				=					0.1
								SCREAMING_SNAKE_CASE__  				=					512
								SCREAMING_SNAKE_CASE__  				=					16
								SCREAMING_SNAKE_CASE__  				=					2
								SCREAMING_SNAKE_CASE__  				=					0.02
								SCREAMING_SNAKE_CASE__  				=					3
								SCREAMING_SNAKE_CASE__  				=					4
								SCREAMING_SNAKE_CASE__  				=					None
								SCREAMING_SNAKE_CASE__  				=					self.vocab_size - 1



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					ids_tensor([self.batch_size, self.seq_length]							,		self.vocab_size  )

								SCREAMING_SNAKE_CASE__  				=					None
								if self.use_input_mask:
												SCREAMING_SNAKE_CASE__  				=					random_attention_mask([self.batch_size, self.seq_length]  )

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

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

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

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

								SCREAMING_SNAKE_CASE__  				=					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 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		*UpperCAmelCase__  ):
								SCREAMING_SNAKE_CASE__  				=					TFOpenAIGPTModel(config=UpperCAmelCase__  )
								SCREAMING_SNAKE_CASE__  				=					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )

								SCREAMING_SNAKE_CASE__  				=					[input_ids, input_mask]
								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )

								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )

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



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		*UpperCAmelCase__  ):
								SCREAMING_SNAKE_CASE__  				=					TFOpenAIGPTLMHeadModel(config=UpperCAmelCase__  )
								SCREAMING_SNAKE_CASE__  				=					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )
								self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.seq_length, self.vocab_size)  )



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		*UpperCAmelCase__  ):
								SCREAMING_SNAKE_CASE__  				=					TFOpenAIGPTDoubleHeadsModel(config=UpperCAmelCase__  )

								SCREAMING_SNAKE_CASE__  				=					tf.tile(tf.expand_dims(UpperCAmelCase__							,		1  )							,		(1, self.num_choices, 1)  )
								SCREAMING_SNAKE_CASE__  				=					tf.tile(tf.expand_dims(UpperCAmelCase__							,		1  )							,		(1, self.num_choices, 1)  )
								SCREAMING_SNAKE_CASE__  				=					tf.tile(tf.expand_dims(UpperCAmelCase__							,		1  )							,		(1, self.num_choices, 1)  )

								SCREAMING_SNAKE_CASE__  				=					{
								    "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,
								}
								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )
								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 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		*UpperCAmelCase__  ):
								SCREAMING_SNAKE_CASE__  				=					self.num_labels
								SCREAMING_SNAKE_CASE__  				=					ids_tensor([self.batch_size]							,		self.type_sequence_label_size  )
								SCREAMING_SNAKE_CASE__  				=					{
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								    "labels": sequence_labels,
								}
								SCREAMING_SNAKE_CASE__  				=					TFOpenAIGPTForSequenceClassification(UpperCAmelCase__  )
								SCREAMING_SNAKE_CASE__  				=					model(UpperCAmelCase__  )
								self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.num_labels)  )






				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.prepare_config_and_inputs()

								(
								    (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   (
								    SCREAMING_SNAKE_CASE__
								)				,   
								)  				=					config_and_inputs

								SCREAMING_SNAKE_CASE__  				=					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
								return config, inputs_dict


@require_tf
class       lowerCamelCase_		(      lowercase							,	lowercase							,	unittest.TestCase ):





				"""simple docstring"""







				_lowerCAmelCase   :   Union[str, Any]   				=       (
				    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
				    if is_tf_available()
				    else ()
				)
				_lowerCAmelCase   :   Optional[Any]   				=       (
				    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
				)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
				_lowerCAmelCase   :   Optional[Any]   				=       (
				    {
				        "feature-extraction": TFOpenAIGPTModel,
				        "text-classification": TFOpenAIGPTForSequenceClassification,
				        "text-generation": TFOpenAIGPTLMHeadModel,
				        "zero-shot": TFOpenAIGPTForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				_lowerCAmelCase   :   Dict   				=       False
				_lowerCAmelCase   :   Optional[int]   				=       False



				def 	lowerCAmelCase__		(    self							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__							,		UpperCAmelCase__  ):
								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 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					TFOpenAIGPTModelTester(self  )
								SCREAMING_SNAKE_CASE__  				=					ConfigTester(self							,		config_class=UpperCAmelCase__							,		n_embd=37  )



				def 	lowerCAmelCase__		(    self  ):
								self.config_tester.run_common_tests()



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase__  )



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_lm_head(*UpperCAmelCase__  )



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_double_head(*UpperCAmelCase__  )



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase__  )






				@slow
				def 	lowerCAmelCase__		(    self  ):
								for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												SCREAMING_SNAKE_CASE__  				=					TFOpenAIGPTModel.from_pretrained(UpperCAmelCase__  )
												self.assertIsNotNone(UpperCAmelCase__  )


@require_tf
class       lowerCamelCase_		(      unittest.TestCase ):





				"""simple docstring"""







				@slow
				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"  )
								SCREAMING_SNAKE_CASE__  				=					tf.convert_to_tensor([[481, 4735, 544]]							,		dtype=tf.intaa  )  # the president is
								SCREAMING_SNAKE_CASE__  				=					[
								    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

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




"""simple docstring"""






def __lowercase  (   lowerCamelCase_	:   str	):
				SCREAMING_SNAKE_CASE__  				=					str(lowerCamelCase_	).strip()
				if not oct_string:
								raise ValueError("Empty string was passed to the function"	)
				SCREAMING_SNAKE_CASE__  				=					oct_string[0] == "-"
				if is_negative:
								SCREAMING_SNAKE_CASE__  				=					oct_string[1:]
				if not oct_string.isdigit() or not all(0 <= int(lowerCamelCase_	) <= 7 for char in oct_string	):
								raise ValueError("Non-octal value was passed to the function"	)
				SCREAMING_SNAKE_CASE__  				=					0
				for char in oct_string:
								SCREAMING_SNAKE_CASE__  				=					8 * decimal_number + int(lowerCamelCase_	)
				if is_negative:
								SCREAMING_SNAKE_CASE__  				=					-decimal_number
				return decimal_number


if __name__ == "__main__":
	from doctest import testmod

	testmod()
