




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




					'''simple docstring'''


					def __init__(    self						,	__a						,	__a=13						,	__a=7						,	__a=True						,	__a=True						,	__a=True						,	__a=99						,	__a=32						,	__a=5						,	__a=4						,	__a=37						,	__a="gelu"						,	__a=0.1						,	__a=0.1						,	__a=512						,	__a=16						,	__a=2						,	__a=0.02						,	__a=3						,	__a=4						,	__a=None						,	):
								__lowerCamelCase					:	int	       =				parent
								__lowerCamelCase					:	Optional[Any]	       =				batch_size
								__lowerCamelCase					:	Dict	       =				seq_length
								__lowerCamelCase					:	str	       =				is_training
								__lowerCamelCase					:	List[str]	       =				use_token_type_ids
								__lowerCamelCase					:	Tuple	       =				use_labels
								__lowerCamelCase					:	Optional[Any]	       =				vocab_size
								__lowerCamelCase					:	Any	       =				hidden_size
								__lowerCamelCase					:	int	       =				num_hidden_layers
								__lowerCamelCase					:	Optional[int]	       =				num_attention_heads
								__lowerCamelCase					:	Optional[int]	       =				intermediate_size
								__lowerCamelCase					:	List[Any]	       =				hidden_act
								__lowerCamelCase					:	Optional[Any]	       =				hidden_dropout_prob
								__lowerCamelCase					:	Dict	       =				attention_probs_dropout_prob
								__lowerCamelCase					:	Optional[int]	       =				max_position_embeddings
								__lowerCamelCase					:	Union[str, Any]	       =				type_vocab_size
								__lowerCamelCase					:	List[str]	       =				type_sequence_label_size
								__lowerCamelCase					:	List[str]	       =				initializer_range
								__lowerCamelCase					:	str	       =				num_labels
								__lowerCamelCase					:	int	       =				num_choices
								__lowerCamelCase					:	int	       =				scope
								__lowerCamelCase					:	Any	       =				self.vocab_size - 1
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Any	       =				ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size     )

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

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

								__lowerCamelCase					:	List[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						,	)

								__lowerCamelCase					:	Optional[Any]	       =				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     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	*__a     ):
								__lowerCamelCase					:	Tuple	       =				OpenAIGPTModel(config=__a     )
								model.to(__a     )
								model.eval()

								__lowerCamelCase					:	Optional[Any]	       =				model(__a						,	token_type_ids=__a						,	head_mask=__a     )
								__lowerCamelCase					:	Dict	       =				model(__a						,	token_type_ids=__a     )
								__lowerCamelCase					:	Optional[int]	       =				model(__a     )

								self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)     )
					def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	*__a     ):
								__lowerCamelCase					:	int	       =				OpenAIGPTLMHeadModel(__a     )
								model.to(__a     )
								model.eval()

								__lowerCamelCase					:	List[Any]	       =				model(__a						,	token_type_ids=__a						,	labels=__a     )
								self.parent.assertEqual(result.loss.shape						,	()     )
								self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size)     )
					def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	*__a     ):
								__lowerCamelCase					:	str	       =				OpenAIGPTDoubleHeadsModel(__a     )
								model.to(__a     )
								model.eval()

								__lowerCamelCase					:	int	       =				model(__a						,	token_type_ids=__a						,	labels=__a     )
								self.parent.assertEqual(result.loss.shape						,	()     )
								self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size)     )
					def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	*__a     ):
								__lowerCamelCase					:	Dict	       =				self.num_labels
								__lowerCamelCase					:	str	       =				OpenAIGPTForSequenceClassification(__a     )
								model.to(__a     )
								model.eval()

								__lowerCamelCase					:	str	       =				ids_tensor([self.batch_size]						,	self.type_sequence_label_size     )
								__lowerCamelCase					:	Optional[int]	       =				model(__a						,	token_type_ids=__a						,	labels=__a     )
								self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels)     )





					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Dict	       =				self.prepare_config_and_inputs()
								(
								    __lowerCamelCase      
								)					:	Union[str, Any]	       =				config_and_inputs
								__lowerCamelCase					:	List[str]	       =				{
								    'input_ids': input_ids,
								    'token_type_ids': token_type_ids,
								    'head_mask': head_mask,
								}

								return config, inputs_dict



@require_torch
class __lowercase(      lowercase__	,	lowercase__	,	lowercase__	,	unittest.TestCase				):




					'''simple docstring'''


					__a						:						str     		= (
					    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
					    if is_torch_available()
					    else ()
					)
					__a						:						Any     		= (
					    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
					)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
					__a						:						str     		= (
					    {
					        'feature-extraction': OpenAIGPTModel,
					        'text-classification': OpenAIGPTForSequenceClassification,
					        'text-generation': OpenAIGPTLMHeadModel,
					        'zero-shot': OpenAIGPTForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a						,	__a     ):
								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     snake_case_				(    self						,	__a						,	__a						,	__a=False     ):
								__lowerCamelCase					:	Dict	       =				super()._prepare_for_class(__a						,	__a						,	return_labels=__a     )

								if return_labels:
											if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
														__lowerCamelCase					:	int	       =				torch.zeros(
														    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)						,	dtype=torch.long						,	device=__a						,	)
														__lowerCamelCase					:	List[Any]	       =				inputs_dict['labels']
														__lowerCamelCase					:	str	       =				inputs_dict['labels']
														__lowerCamelCase					:	Any	       =				torch.zeros(
														    (self.model_tester.batch_size, self.model_tester.num_choices)						,	dtype=torch.long						,	device=__a						,	)
														__lowerCamelCase					:	Union[str, Any]	       =				torch.zeros(
														    self.model_tester.batch_size						,	dtype=torch.long						,	device=__a     )
								return inputs_dict
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Dict	       =				OpenAIGPTModelTester(self     )
								__lowerCamelCase					:	int	       =				ConfigTester(self						,	config_class=__a						,	n_embd=37     )
					def     snake_case_				(    self     ):
								self.config_tester.run_common_tests()
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Tuple	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_model(*__a     )
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Dict	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*__a     )
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	str	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_double_lm_head_model(*__a     )
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Union[str, Any]	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__a     )





					@slow
					def     snake_case_				(    self     ):
								for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCamelCase					:	Dict	       =				OpenAIGPTModel.from_pretrained(__a     )
											self.assertIsNotNone(__a     )



@require_torch
class __lowercase(      unittest.TestCase				):




					'''simple docstring'''


					@slow
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Union[str, Any]	       =				OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'     )
								model.to(__a     )
								__lowerCamelCase					:	Optional[int]	       =				torch.tensor([[481, 4735, 544]]						,	dtype=torch.long						,	device=__a     )  # the president is
								__lowerCamelCase					:	Any	       =				[
								    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					:	Union[str, Any]	       =				model.generate(__a						,	do_sample=__a     )
								self.assertListEqual(output_ids[0].tolist()						,	__a     )









"""simple docstring"""
a_				:    Dict										=     {
    '''meter''': '''m''',
    '''kilometer''': '''km''',
    '''megametre''': '''Mm''',
    '''gigametre''': '''Gm''',
    '''terametre''': '''Tm''',
    '''petametre''': '''Pm''',
    '''exametre''': '''Em''',
    '''zettametre''': '''Zm''',
    '''yottametre''': '''Ym''',
}
# Exponent of the factor(meter)
a_				:    Optional[int]										=     {
    '''m''': 0,
    '''km''': 3,
    '''Mm''': 6,
    '''Gm''': 9,
    '''Tm''': 12,
    '''Pm''': 15,
    '''Em''': 18,
    '''Zm''': 21,
    '''Ym''': 24,
}





def 						UpperCAmelCase				(			A__:    float							,				A__:    str							,				A__:    str     )  ->     float:
			__lowerCamelCase					:	List[Any]	       =				from_type.lower().strip('s'     )
			__lowerCamelCase					:	Optional[int]	       =				to_type.lower().strip('s'     )

			__lowerCamelCase					:	int	       =				UNIT_SYMBOL.get(A__							,				A__     )
			__lowerCamelCase					:	Dict	       =				UNIT_SYMBOL.get(A__							,				A__     )

			if from_sanitized not in METRIC_CONVERSION:
						__lowerCamelCase					:	Dict	       =				(
						    f'''Invalid \'from_type\' value: {from_type!r}.\n'''
						    f'''Conversion abbreviations are: {", ".join(A__     )}'''
						)
						raise ValueError(A__     )
			if to_sanitized not in METRIC_CONVERSION:
						__lowerCamelCase					:	Any	       =				(
						    f'''Invalid \'to_type\' value: {to_type!r}.\n'''
						    f'''Conversion abbreviations are: {", ".join(A__     )}'''
						)
						raise ValueError(A__     )
			__lowerCamelCase					:	Any	       =				METRIC_CONVERSION[from_sanitized]
			__lowerCamelCase					:	Tuple	       =				METRIC_CONVERSION[to_sanitized]
			__lowerCamelCase					:	Any	       =				1

			if from_exponent > to_exponent:
						__lowerCamelCase					:	Union[str, Any]	       =				from_exponent - to_exponent
			else:
						__lowerCamelCase					:	List[Any]	       =				-(to_exponent - from_exponent)

			return value * pow(10							,				A__     )


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

					testmod()



