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        lowercase_  :



	"""simple docstring"""





	def __init__(							self		,      __SCREAMING_SNAKE_CASE		,      )						->Dict:
								lowerCAmelCase     		=		parent
								lowerCAmelCase     		=		13
								lowerCAmelCase     		=		7
								lowerCAmelCase     		=		True
								lowerCAmelCase     		=		True
								lowerCAmelCase     		=		True
								lowerCAmelCase     		=		True
								lowerCAmelCase     		=		True
								lowerCAmelCase     		=		99
								lowerCAmelCase     		=		32
								lowerCAmelCase     		=		2
								lowerCAmelCase     		=		4
								lowerCAmelCase     		=		37
								lowerCAmelCase     		=		'gelu'
								lowerCAmelCase     		=		0.1
								lowerCAmelCase     		=		0.1
								lowerCAmelCase     		=		512
								lowerCAmelCase     		=		16
								lowerCAmelCase     		=		2
								lowerCAmelCase     		=		0.0_2
								lowerCAmelCase     		=		3
								lowerCAmelCase     		=		4
								lowerCAmelCase     		=		None
								lowerCAmelCase     		=		self.vocab_size - 1


	def       SCREAMING_SNAKE_CASE_    (							self    )						->Tuple:
								lowerCAmelCase     		=		ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size    )

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

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

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

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

								lowerCAmelCase     		=		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     		=		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       SCREAMING_SNAKE_CASE_    (							self		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      *__SCREAMING_SNAKE_CASE    )						->int:
								lowerCAmelCase     		=		TFOpenAIGPTModel(config=lowercase_    )
								lowerCAmelCase     		=		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
								lowerCAmelCase     		=		model(lowercase_    )

								lowerCAmelCase     		=		[input_ids, input_mask]
								lowerCAmelCase     		=		model(lowercase_    )

								lowerCAmelCase     		=		model(lowercase_    )

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


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


	def       SCREAMING_SNAKE_CASE_    (							self		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      *__SCREAMING_SNAKE_CASE    )						->Dict:
								lowerCAmelCase     		=		TFOpenAIGPTDoubleHeadsModel(config=lowercase_    )

								lowerCAmelCase     		=		tf.tile(tf.expand_dims(lowercase_		,      1    )		,      (1, self.num_choices, 1)    )
								lowerCAmelCase     		=		tf.tile(tf.expand_dims(lowercase_		,      1    )		,      (1, self.num_choices, 1)    )
								lowerCAmelCase     		=		tf.tile(tf.expand_dims(lowercase_		,      1    )		,      (1, self.num_choices, 1)    )

								lowerCAmelCase     		=		{
								    '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,
								}
								lowerCAmelCase     		=		model(lowercase_    )
								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       SCREAMING_SNAKE_CASE_    (							self		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      *__SCREAMING_SNAKE_CASE    )						->List[Any]:
								lowerCAmelCase     		=		self.num_labels
								lowerCAmelCase     		=		ids_tensor([self.batch_size]		,      self.type_sequence_label_size    )
								lowerCAmelCase     		=		{
								    'input_ids': input_ids,
								    'attention_mask': input_mask,
								    'token_type_ids': token_type_ids,
								    'labels': sequence_labels,
								}
								lowerCAmelCase     		=		TFOpenAIGPTForSequenceClassification(lowercase_    )
								lowerCAmelCase     		=		model(lowercase_    )
								self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.num_labels)    )




	def       SCREAMING_SNAKE_CASE_    (							self    )						->Any:
								lowerCAmelCase     		=		self.prepare_config_and_inputs()

								(
								    lowerCAmelCase					
								)     		=		config_and_inputs

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




@require_tf
class        lowercase_  (  UpperCamelCase_				,					UpperCamelCase_				,					unittest.TestCase						):



	"""simple docstring"""





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


	def       SCREAMING_SNAKE_CASE_    (							self		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE		,      __SCREAMING_SNAKE_CASE    )						->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       SCREAMING_SNAKE_CASE_    (							self    )						->Tuple:
								lowerCAmelCase     		=		TFOpenAIGPTModelTester(self    )
								lowerCAmelCase     		=		ConfigTester(self		,      config_class=lowercase_		,      n_embd=37    )


	def       SCREAMING_SNAKE_CASE_    (							self    )						->List[str]:
								self.config_tester.run_common_tests()


	def       SCREAMING_SNAKE_CASE_    (							self    )						->List[Any]:
								lowerCAmelCase     		=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_model(*lowercase_    )


	def       SCREAMING_SNAKE_CASE_    (							self    )						->Optional[int]:
								lowerCAmelCase     		=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_lm_head(*lowercase_    )


	def       SCREAMING_SNAKE_CASE_    (							self    )						->List[Any]:
								lowerCAmelCase     		=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_double_head(*lowercase_    )


	def       SCREAMING_SNAKE_CASE_    (							self    )						->Optional[int]:
								lowerCAmelCase     		=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowercase_    )




	@slow
	def       SCREAMING_SNAKE_CASE_    (							self    )						->Any:




								for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCAmelCase     		=		TFOpenAIGPTModel.from_pretrained(lowercase_    )
															self.assertIsNotNone(lowercase_    )




@require_tf
class        lowercase_  (  unittest.TestCase						):



	"""simple docstring"""





	@slow
	def       SCREAMING_SNAKE_CASE_    (							self    )						->Union[str, Any]:
								lowerCAmelCase     		=		TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''    )
								lowerCAmelCase     		=		tf.convert_to_tensor([[481, 4735, 544]]		,      dtype=tf.intaa    )  # the president is
								lowerCAmelCase     		=		[
								    481,
								    4735,
								    544,
								    246,
								    963,
								    870,
								    762,
								    239,
								    244,
								    40477,
								    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     		=		model.generate(lowercase_		,      do_sample=lowercase_    )
								self.assertListEqual(output_ids[0].numpy().tolist()		,      lowercase_    )




lowercase__      :    dict[str, float]          =     {
    "kilogram": 1,
    "gram": pow(1_0, 3),
    "milligram": pow(1_0, 6),
    "metric-ton": pow(1_0, -3),
    "long-ton": 0.0_0_0_9_8_4_2_0_7_3,
    "short-ton": 0.0_0_1_1_0_2_3_1_2_2,
    "pound": 2.2_0_4_6_2_4_4_2_0_2,
    "stone": 0.1_5_7_4_7_3_1_7_2_8,
    "ounce": 3_5.2_7_3_9_9_0_7_2_3,
    "carrat": 5_0_0_0,
    "atomic-mass-unit": 6.0_22_13_66_52e26,
}

lowercase__      :    dict[str, float]          =     {
    "kilogram": 1,
    "gram": pow(1_0, -3),
    "milligram": pow(1_0, -6),
    "metric-ton": pow(1_0, 3),
    "long-ton": 1_0_1_6.0_4_6_0_8,
    "short-ton": 9_0_7.1_8_4,
    "pound": 0.4_5_3_5_9_2,
    "stone": 6.3_5_0_2_9,
    "ounce": 0.0_2_8_3_4_9_5,
    "carrat": 0.0_0_0_2,
    "atomic-mass-unit": 1.6_60_54_01_99e-27,
}

def        SCREAMING_SNAKE_CASE_					(						snake_case__       ,  snake_case__       ,  snake_case__							)					->     float:
							if to_type not in KILOGRAM_CHART or from_type not in WEIGHT_TYPE_CHART:
														lowerCAmelCase     		=		(
														    f"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n"
														    f"Supported values are: {', '.join(snake_case__							)}"
														)
														raise ValueError(snake_case__							)
							return value * KILOGRAM_CHART[to_type] * WEIGHT_TYPE_CHART[from_type]


if __name__ == "__main__":
	import doctest

	doctest.testmod()



