

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





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




						'''simple docstring'''
						__a        =parent
						__a        =batch_size
						__a        =seq_length
						__a        =is_training
						__a        =use_token_type_ids
						__a        =use_labels
						__a        =vocab_size
						__a        =hidden_size
						__a        =num_hidden_layers
						__a        =num_attention_heads
						__a        =intermediate_size
						__a        =hidden_act
						__a        =hidden_dropout_prob
						__a        =attention_probs_dropout_prob
						__a        =max_position_embeddings
						__a        =type_vocab_size
						__a        =type_sequence_label_size
						__a        =initializer_range
						__a        =num_labels
						__a        =num_choices
						__a        =scope
						__a        =self.vocab_size - 1





			def 		__magic_name__    (		self					)   -> Any:




						'''simple docstring'''
						__a        =ids_tensor([self.batch_size, self.seq_length]					,  self.vocab_size					)

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

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

						__a        =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        =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 		__magic_name__    (		self					,  __snake_case					,  __snake_case					,  __snake_case					,  __snake_case					,  *__snake_case					)   -> Dict:




						'''simple docstring'''
						__a        =OpenAIGPTModel(config=__snake_case					)
						model.to(__snake_case					)
						model.eval()

						__a        =model(__snake_case					,  token_type_ids=__snake_case					,  head_mask=__snake_case					)
						__a        =model(__snake_case					,  token_type_ids=__snake_case					)
						__a        =model(__snake_case					)

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





			def 		__magic_name__    (		self					,  __snake_case					,  __snake_case					,  __snake_case					,  __snake_case					,  *__snake_case					)   -> Optional[int]:




						'''simple docstring'''
						__a        =OpenAIGPTLMHeadModel(__snake_case					)
						model.to(__snake_case					)
						model.eval()

						__a        =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 		__magic_name__    (		self					,  __snake_case					,  __snake_case					,  __snake_case					,  __snake_case					,  *__snake_case					)   -> List[Any]:




						'''simple docstring'''
						__a        =OpenAIGPTDoubleHeadsModel(__snake_case					)
						model.to(__snake_case					)
						model.eval()

						__a        =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 		__magic_name__    (		self					,  __snake_case					,  __snake_case					,  __snake_case					,  __snake_case					,  *__snake_case					)   -> Tuple:




						'''simple docstring'''
						__a        =self.num_labels
						__a        =OpenAIGPTForSequenceClassification(__snake_case					)
						model.to(__snake_case					)
						model.eval()

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





			def 		__magic_name__    (		self					)   -> List[Any]:




						'''simple docstring'''
						__a        =self.prepare_config_and_inputs()
						(
						    (
						    __a
						)      ,    (
						    __a
						)      ,    (
						    __a
						)      ,    (
						    __a
						)      ,    (
						    __a
						)      ,    (
						    __a
						)      ,    (
						    __a
						)      ,    
						)        =config_and_inputs
						__a        ={
						    'input_ids': input_ids,
						    'token_type_ids': token_type_ids,
						    'head_mask': head_mask,
						}

						return config, inputs_dict






@require_torch
class 	__magic_name__    (			lowerCAmelCase_  ,    lowerCAmelCase_  ,    lowerCAmelCase_  ,    unittest.TestCase      ):
			SCREAMING_SNAKE_CASE							   =						(
			    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
			    if is_torch_available()
			    else ()
			)
			SCREAMING_SNAKE_CASE							   =						(
			    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
			)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
			SCREAMING_SNAKE_CASE							   =						(
			    {
			        'feature-extraction': OpenAIGPTModel,
			        'text-classification': OpenAIGPTForSequenceClassification,
			        'text-generation': OpenAIGPTLMHeadModel,
			        'zero-shot': OpenAIGPTForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)





			def 		__magic_name__    (		self					,  __snake_case					,  __snake_case					,  __snake_case					,  __snake_case					,  __snake_case					)   -> Tuple:




						'''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 		__magic_name__    (		self					,  __snake_case					,  __snake_case					,  __snake_case=False					)   -> str:




						'''simple docstring'''
						__a        =super()._prepare_for_class(__snake_case					,  __snake_case					,  return_labels=__snake_case					)

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





			def 		__magic_name__    (		self					)   -> Union[str, Any]:




						'''simple docstring'''
						__a        =OpenAIGPTModelTester(self					)
						__a        =ConfigTester(self					,  config_class=__snake_case					,  n_embd=37					)





			def 		__magic_name__    (		self					)   -> List[str]:




						'''simple docstring'''
						self.config_tester.run_common_tests()





			def 		__magic_name__    (		self					)   -> Optional[Any]:




						'''simple docstring'''
						__a        =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_model(*__snake_case					)





			def 		__magic_name__    (		self					)   -> Any:




						'''simple docstring'''
						__a        =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_lm_head_model(*__snake_case					)





			def 		__magic_name__    (		self					)   -> List[Any]:




						'''simple docstring'''
						__a        =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_double_lm_head_model(*__snake_case					)





			def 		__magic_name__    (		self					)   -> Dict:




						'''simple docstring'''
						__a        =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__snake_case					)





			@slow
			def 		__magic_name__    (		self					)   -> List[str]:




						'''simple docstring'''
						for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									__a        =OpenAIGPTModel.from_pretrained(__snake_case					)
									self.assertIsNotNone(__snake_case					)






@require_torch
class 	__magic_name__    (			unittest.TestCase      ):





			@slow
			def 		__magic_name__    (		self					)   -> List[Any]:




						'''simple docstring'''
						__a        =OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'					)
						model.to(__snake_case					)
						__a        =torch.tensor([[481, 4735, 544]]					,  dtype=torch.long					,  device=__snake_case					)  # the president is
						__a        =[
						    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        =model.generate(__snake_case					,  do_sample=__snake_case					)
						self.assertListEqual(output_ids[0].tolist()					,  __snake_case					)









import unittest

from transformers import GPTSwaTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


_lowerCAmelCase					:			List[Any]  				=			get_tests_dir("fixtures/test_sentencepiece_with_bytefallback.model")






@require_sentencepiece
@require_tokenizers
class 	__magic_name__    (			lowerCAmelCase_  ,    unittest.TestCase      ):
			SCREAMING_SNAKE_CASE							   =						GPTSwaTokenizer
			SCREAMING_SNAKE_CASE							   =						False
			SCREAMING_SNAKE_CASE							   =						True
			SCREAMING_SNAKE_CASE							   =						False





			def 		__magic_name__    (		self					)   -> Dict:




						'''simple docstring'''
						super().setUp()

						# We have a SentencePiece fixture for testing
						__a        =GPTSwaTokenizer(__snake_case					,  eos_token='<unk>'					,  bos_token='<unk>'					,  pad_token='<unk>'					)

						tokenizer.save_pretrained(self.tmpdirname					)





			def 		__magic_name__    (		self					,  __snake_case					)   -> Tuple:




						'''simple docstring'''
						__a        ='This is a test'
						__a        ='This is a test'
						return input_text, output_text





			def 		__magic_name__    (		self					)   -> Optional[Any]:




						'''simple docstring'''
						__a        ='<s>'
						__a        =1

						self.assertEqual(self.get_tokenizer()._convert_token_to_id(__snake_case					)					,  __snake_case					)
						self.assertEqual(self.get_tokenizer()._convert_id_to_token(__snake_case					)					,  __snake_case					)





			def 		__magic_name__    (		self					)   -> Dict:




						'''simple docstring'''
						__a        =list(self.get_tokenizer().get_vocab().keys()					)

						self.assertEqual(vocab_keys[0]					,  '<unk>'					)
						self.assertEqual(vocab_keys[1]					,  '<s>'					)
						self.assertEqual(vocab_keys[-1]					,  'j'					)
						self.assertEqual(len(__snake_case					)					,  2000					)





			def 		__magic_name__    (		self					)   -> Optional[Any]:




						'''simple docstring'''
						self.assertEqual(self.get_tokenizer().vocab_size					,  2000					)





			def 		__magic_name__    (		self					)   -> Any:




						'''simple docstring'''
						__a        =GPTSwaTokenizer(__snake_case					)

						__a        =tokenizer.tokenize('This is a test'					)
						self.assertListEqual(__snake_case					,  ['▁This', '▁is', '▁a', '▁t', 'est']					)

						self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case					)					,  [465, 287, 265, 631, 842]					)

						__a        =tokenizer.tokenize('I was born in 92000, and this is falsé.'					)
						# fmt: off
						self.assertListEqual(
						    __snake_case					,  ['▁I', '▁was', '▁bor', 'n', '▁in', '▁', '<0x39>', '2', '0', '0', '0', ',', '▁and', '▁this', '▁is', '▁f', 'al', 's', '<0xC3>', '<0xA9>', '.']					,  )
						# fmt: on

						__a        =tokenizer.convert_tokens_to_ids(__snake_case					)
						self.assertListEqual(
						    __snake_case					,  [262, 272, 1525, 286, 271, 268, 60, 916, 633, 633, 633, 259, 266, 301, 287, 384, 367, 263, 198, 172, 260]					,  )

						__a        =tokenizer.convert_ids_to_tokens(__snake_case					)
						# fmt: off
						self.assertListEqual(
						    __snake_case					,  ['▁I', '▁was', '▁bor', 'n', '▁in', '▁', '<0x39>', '2', '0', '0', '0', ',', '▁and', '▁this', '▁is', '▁f', 'al', 's', '<0xC3>', '<0xA9>', '.']					)
						# fmt: on





			def 		__magic_name__    (		self					)   -> Optional[int]:




						'''simple docstring'''
						__a        =GPTSwaTokenizer(__snake_case					)
						__a        =['This is a test', 'I was born in 92000, and this is falsé.']
						__a        =[
						    [465, 287, 265, 631, 842],
						    [262, 272, 1525, 286, 271, 268, 60, 916, 633, 633, 633, 259, 266, 301, 287, 384, 367, 263, 198, 172, 260],
						]

						# Test that encode_fast returns the same as tokenize + convert_tokens_to_ids
						for text, expected_ids in zip(__snake_case					,  __snake_case					):
									self.assertListEqual(tokenizer.encode_fast(__snake_case					)					,  __snake_case					)

						# Test that decode_fast returns the input text
						for text, token_ids in zip(__snake_case					,  __snake_case					):
									self.assertEqual(tokenizer.decode_fast(__snake_case					)					,  __snake_case					)





			@slow
			def 		__magic_name__    (		self					)   -> List[str]:




						'''simple docstring'''
						__a        =[
						    '<|python|>def fibonacci(n)\n    if n < 0:\n        print(\'Incorrect input\')',
						    'Hey there, how are you doing this fine day?',
						    'This is a text with a trailing spaces followed by a dot     .',
						    'Häj sväjs lillebrör! =)',
						    'Det är inget fel på Mr. Cool',
						]

						# fmt: off
						__a        ={'input_ids': [[6_3423, 5, 6811, 1_4954, 282, 816, 3821, 6_3466, 6_3425, 6_3462, 18, 6_3978, 678, 301, 1320, 6_3423, 6_3455, 6_3458, 18, 6_3982, 4246, 3940, 1901, 4_7789, 5547, 1_8994], [1_9630, 1100, 6_3446, 1342, 633, 544, 4488, 593, 5102, 2416, 6_3495, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1652, 428, 268, 1936, 515, 268, 5_8593, 2_2413, 9106, 546, 268, 3_3213, 6_3979, 698, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [5_5130, 6_3450, 924, 6_3449, 2249, 4062, 1558, 318, 6_3504, 2_1498, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [509, 377, 2827, 2559, 332, 6575, 6_3443, 2_6801, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'token_type_ids': [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}
						# fmt: on
						self.tokenizer_integration_test_util(
						    expected_encoding=__snake_case					,  model_name='AI-Sweden/gpt-sw3-126m'					,  sequences=__snake_case					,  )






