

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							:




				def __init__(   self ,  snake_case ,  snake_case=13 ,  snake_case=7 ,  snake_case=True ,  snake_case=True ,  snake_case=True ,  snake_case=99 ,  snake_case=32 ,  snake_case=5 ,  snake_case=4 ,  snake_case=37 ,  snake_case="gelu" ,  snake_case=0.1 ,  snake_case=0.1 ,  snake_case=512 ,  snake_case=16 ,  snake_case=2 ,  snake_case=0.02 ,  snake_case=3 ,  snake_case=4 ,  snake_case=None ,  ):
											snake_case_     							=       parent
											snake_case_     							=       batch_size
											snake_case_     							=       seq_length
											snake_case_     							=       is_training
											snake_case_     							=       use_token_type_ids
											snake_case_     							=       use_labels
											snake_case_     							=       vocab_size
											snake_case_     							=       hidden_size
											snake_case_     							=       num_hidden_layers
											snake_case_     							=       num_attention_heads
											snake_case_     							=       intermediate_size
											snake_case_     							=       hidden_act
											snake_case_     							=       hidden_dropout_prob
											snake_case_     							=       attention_probs_dropout_prob
											snake_case_     							=       max_position_embeddings
											snake_case_     							=       type_vocab_size
											snake_case_     							=       type_sequence_label_size
											snake_case_     							=       initializer_range
											snake_case_     							=       num_labels
											snake_case_     							=       num_choices
											snake_case_     							=       scope
											snake_case_     							=       self.vocab_size - 1




				def        a				(   self			):
											snake_case_     							=       ids_tensor([self.batch_size, self.seq_length] ,  self.vocab_size			)

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

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

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

											snake_case_     							=       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        a				(   self ,  snake_case ,  snake_case ,  snake_case ,  snake_case ,  *snake_case			):
											snake_case_     							=       OpenAIGPTModel(config=snake_case			)
											model.to(snake_case			)
											model.eval()

											snake_case_     							=       model(snake_case ,  token_type_ids=snake_case ,  head_mask=snake_case			)
											snake_case_     							=       model(snake_case ,  token_type_ids=snake_case			)
											snake_case_     							=       model(snake_case			)

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




				def        a				(   self ,  snake_case ,  snake_case ,  snake_case ,  snake_case ,  *snake_case			):
											snake_case_     							=       OpenAIGPTLMHeadModel(snake_case			)
											model.to(snake_case			)
											model.eval()

											snake_case_     							=       model(snake_case ,  token_type_ids=snake_case ,  labels=snake_case			)
											self.parent.assertEqual(result.loss.shape ,  ()			)
											self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.vocab_size)			)




				def        a				(   self ,  snake_case ,  snake_case ,  snake_case ,  snake_case ,  *snake_case			):
											snake_case_     							=       OpenAIGPTDoubleHeadsModel(snake_case			)
											model.to(snake_case			)
											model.eval()

											snake_case_     							=       model(snake_case ,  token_type_ids=snake_case ,  labels=snake_case			)
											self.parent.assertEqual(result.loss.shape ,  ()			)
											self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.vocab_size)			)




				def        a				(   self ,  snake_case ,  snake_case ,  snake_case ,  snake_case ,  *snake_case			):
											snake_case_     							=       self.num_labels
											snake_case_     							=       OpenAIGPTForSequenceClassification(snake_case			)
											model.to(snake_case			)
											model.eval()

											snake_case_     							=       ids_tensor([self.batch_size] ,  self.type_sequence_label_size			)
											snake_case_     							=       model(snake_case ,  token_type_ids=snake_case ,  labels=snake_case			)
											self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.num_labels)			)




				def        a				(   self			):
											snake_case_     							=       self.prepare_config_and_inputs()
											(
											    (
											    snake_case_
											)							,       (
											    snake_case_
											)							,       (
											    snake_case_
											)							,       (
											    snake_case_
											)							,       (
											    snake_case_
											)							,       (
											    snake_case_
											)							,       (
											    snake_case_
											)							,       
											)     							=       config_and_inputs
											snake_case_     							=       {
											    '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     ):
				__SCREAMING_SNAKE_CASE   :					List[str]			     =     (
				    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
				    if is_torch_available()
				    else ()
				)
				__SCREAMING_SNAKE_CASE   :					List[Any]			     =     (
				    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
				)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
				__SCREAMING_SNAKE_CASE   :					Union[str, Any]			     =     (
				    {
				        '''feature-extraction''': OpenAIGPTModel,
				        '''text-classification''': OpenAIGPTForSequenceClassification,
				        '''text-generation''': OpenAIGPTLMHeadModel,
				        '''zero-shot''': OpenAIGPTForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)




				def        a				(   self ,  snake_case ,  snake_case ,  snake_case ,  snake_case ,  snake_case			):
											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        a				(   self ,  snake_case ,  snake_case ,  snake_case=False			):
											snake_case_     							=       super()._prepare_for_class(snake_case ,  snake_case ,  return_labels=snake_case			)

											if return_labels:
																		if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																									snake_case_     							=       torch.zeros(
																									    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length) ,  dtype=torch.long ,  device=snake_case ,  )
																									snake_case_     							=       inputs_dict['labels']
																									snake_case_     							=       inputs_dict['labels']
																									snake_case_     							=       torch.zeros(
																									    (self.model_tester.batch_size, self.model_tester.num_choices) ,  dtype=torch.long ,  device=snake_case ,  )
																									snake_case_     							=       torch.zeros(
																									    self.model_tester.batch_size ,  dtype=torch.long ,  device=snake_case			)
											return inputs_dict




				def        a				(   self			):
											snake_case_     							=       OpenAIGPTModelTester(self			)
											snake_case_     							=       ConfigTester(self ,  config_class=snake_case ,  n_embd=37			)




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




				def        a				(   self			):
											snake_case_     							=       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_model(*snake_case			)




				def        a				(   self			):
											snake_case_     							=       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_lm_head_model(*snake_case			)




				def        a				(   self			):
											snake_case_     							=       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_double_lm_head_model(*snake_case			)




				def        a				(   self			):
											snake_case_     							=       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*snake_case			)




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







@require_torch
class 					lowercase							(			unittest.TestCase     ):




				@slow
				def        a				(   self			):
											snake_case_     							=       OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'			)
											model.to(snake_case			)
											snake_case_     							=       torch.tensor([[481, 4735, 544]] ,  dtype=torch.long ,  device=snake_case			)  # the president is
											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

											snake_case_     							=       model.generate(snake_case ,  do_sample=snake_case			)
											self.assertListEqual(output_ids[0].tolist() ,  snake_case			)







import string





def    __lowerCamelCase      (					UpperCamelCase__ ):







							'''simple docstring'''






							snake_case_     							=       ''
							for i in sequence:
														snake_case_     							=       ord(UpperCamelCase__ )
														if 65 <= extract <= 90:
																					output += chr(155 - extract )
														elif 97 <= extract <= 122:
																					output += chr(219 - extract )
														else:
																					output += i
							return output





def    __lowerCamelCase      (					UpperCamelCase__ ):







							'''simple docstring'''






							snake_case_     							=       string.ascii_letters
							snake_case_     							=       string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1]
							return "".join(
							    letters_reversed[letters.index(UpperCamelCase__ )] if c in letters else c for c in sequence )





def    __lowerCamelCase      (					):







							'''simple docstring'''






							from timeit import timeit

							print('Running performance benchmarks...' )
							snake_case_     							=       'from string import printable ; from __main__ import atbash, atbash_slow'
							print(F'''> atbash_slow(): {timeit("atbash_slow(printable)"  ,				setup=UpperCamelCase__ )} seconds''' )
							print(F'''>      atbash(): {timeit("atbash(printable)"  ,				setup=UpperCamelCase__ )} seconds''' )


if __name__ == "__main__":
					for example in ("ABCDEFGH", "123GGjj", "testStringtest", "with space"):
										print(F'''{example} encrypted in atbash: {atbash(example)}''')
					benchmark()




