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

						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
						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,
						    head_mask,
						    token_type_ids,
						    sequence_labels,
						    token_labels,
						    choice_labels,
						)
	def      A_				(			self							:   Union[str, Any]       ,   UpperCAmelCase_							:   int       ,   UpperCAmelCase_							:   List[Any]       ,   UpperCAmelCase_							:   str       ,   UpperCAmelCase_							:   List[str]       ,   *UpperCAmelCase_							:   Union[str, Any]							):
						SCREAMING_SNAKE_CASE__													= OpenAIGPTModel(config=UpperCAmelCase_							)
						model.to(UpperCAmelCase_							)
						model.eval()

						SCREAMING_SNAKE_CASE__													= model(UpperCAmelCase_       ,   token_type_ids=UpperCAmelCase_       ,   head_mask=UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= model(UpperCAmelCase_       ,   token_type_ids=UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= model(UpperCAmelCase_							)

						self.parent.assertEqual(result.last_hidden_state.shape       ,   (self.batch_size, self.seq_length, self.hidden_size)							)
	def      A_				(			self							:   Optional[int]       ,   UpperCAmelCase_							:   int       ,   UpperCAmelCase_							:   int       ,   UpperCAmelCase_							:   Union[str, Any]       ,   UpperCAmelCase_							:   Union[str, Any]       ,   *UpperCAmelCase_							:   Union[str, Any]							):
						SCREAMING_SNAKE_CASE__													= OpenAIGPTLMHeadModel(UpperCAmelCase_							)
						model.to(UpperCAmelCase_							)
						model.eval()

						SCREAMING_SNAKE_CASE__													= model(UpperCAmelCase_       ,   token_type_ids=UpperCAmelCase_       ,   labels=UpperCAmelCase_							)
						self.parent.assertEqual(result.loss.shape       ,   ()							)
						self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.vocab_size)							)
	def      A_				(			self							:   Optional[int]       ,   UpperCAmelCase_							:   Any       ,   UpperCAmelCase_							:   Tuple       ,   UpperCAmelCase_							:   List[str]       ,   UpperCAmelCase_							:   Union[str, Any]       ,   *UpperCAmelCase_							:   List[str]							):
						SCREAMING_SNAKE_CASE__													= OpenAIGPTDoubleHeadsModel(UpperCAmelCase_							)
						model.to(UpperCAmelCase_							)
						model.eval()

						SCREAMING_SNAKE_CASE__													= model(UpperCAmelCase_       ,   token_type_ids=UpperCAmelCase_       ,   labels=UpperCAmelCase_							)
						self.parent.assertEqual(result.loss.shape       ,   ()							)
						self.parent.assertEqual(result.logits.shape       ,   (self.batch_size, self.seq_length, self.vocab_size)							)
	def      A_				(			self							:   Optional[int]       ,   UpperCAmelCase_							:   int       ,   UpperCAmelCase_							:   Tuple       ,   UpperCAmelCase_							:   int       ,   UpperCAmelCase_							:   Optional[int]       ,   *UpperCAmelCase_							:   List[Any]							):
						SCREAMING_SNAKE_CASE__													= self.num_labels
						SCREAMING_SNAKE_CASE__													= OpenAIGPTForSequenceClassification(UpperCAmelCase_							)
						model.to(UpperCAmelCase_							)
						model.eval()

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





	def      A_				(			self							:   Optional[int]							):
						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__
						)						, 
						)													= config_and_inputs
						SCREAMING_SNAKE_CASE__													= {
						    'input_ids': input_ids,
						    'token_type_ids': token_type_ids,
						    'head_mask': head_mask,
						}

						return config, inputs_dict





@require_torch
class 						lowercase__  (						_UpperCAmelCase     ,       _UpperCAmelCase     ,       _UpperCAmelCase     ,       unittest.TestCase  ):
	A__    :   Union[str, Any]				       =(
	    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
	    if is_torch_available()
	    else ()
	)
	A__    :   int				       =(
	    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
	)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
	A__    :   Dict				       =(
	    {
	        """feature-extraction""": OpenAIGPTModel,
	        """text-classification""": OpenAIGPTForSequenceClassification,
	        """text-generation""": OpenAIGPTLMHeadModel,
	        """zero-shot""": OpenAIGPTForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	def      A_				(			self							:   List[str]       ,   UpperCAmelCase_							:   int       ,   UpperCAmelCase_							:   int       ,   UpperCAmelCase_							:   Tuple       ,   UpperCAmelCase_							:   Optional[Any]       ,   UpperCAmelCase_							:   str							):
						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							:   Optional[int]       ,   UpperCAmelCase_							:   int       ,   UpperCAmelCase_							:   Optional[int]       ,   UpperCAmelCase_							:   str=False							):
						SCREAMING_SNAKE_CASE__													= super()._prepare_for_class(UpperCAmelCase_       ,   UpperCAmelCase_       ,   return_labels=UpperCAmelCase_							)

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





	@slow
	def      A_				(			self							:   Optional[Any]							):
						for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											SCREAMING_SNAKE_CASE__													= OpenAIGPTModel.from_pretrained(UpperCAmelCase_							)
											self.assertIsNotNone(UpperCAmelCase_							)





@require_torch
class 						lowercase__  (						unittest.TestCase  ):
	@slow
	def      A_				(			self							:   Dict							):
						SCREAMING_SNAKE_CASE__													= OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'							)
						model.to(UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= torch.tensor([[481, 4735, 544]]       ,   dtype=torch.long       ,   device=UpperCAmelCase_							)  # the president is
						SCREAMING_SNAKE_CASE__													= [
						    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

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







import os
import unittest

from transformers import LxmertTokenizer, LxmertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin




@require_tokenizers
class 						lowercase__  (						_UpperCAmelCase     ,       unittest.TestCase  ):
	A__    :   Dict				       =LxmertTokenizer
	A__    :   List[Any]				       =LxmertTokenizerFast
	A__    :   Any				       =True
	A__    :   List[Any]				       =True
	def      A_				(			self							:   Optional[Any]							):
						super().setUp()

						SCREAMING_SNAKE_CASE__													= [
						    '[UNK]',
						    '[CLS]',
						    '[SEP]',
						    'want',
						    '##want',
						    '##ed',
						    'wa',
						    'un',
						    'runn',
						    '##ing',
						    ',',
						    'low',
						    'lowest',
						]
						SCREAMING_SNAKE_CASE__													= os.path.join(self.tmpdirname       ,   VOCAB_FILES_NAMES['vocab_file']							)
						with open(self.vocab_file       ,   'w'       ,   encoding='utf-8'							) as vocab_writer:
											vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]							)							)
	def      A_				(			self							:   int       ,   UpperCAmelCase_							:   int							):
						SCREAMING_SNAKE_CASE__													= 'UNwant\u00E9d,running'
						SCREAMING_SNAKE_CASE__													= 'unwanted, running'
						return input_text, output_text
	def      A_				(			self							:   Optional[Any]							):
						SCREAMING_SNAKE_CASE__													= self.tokenizer_class(self.vocab_file							)

						SCREAMING_SNAKE_CASE__													= tokenizer.tokenize('UNwant\u00E9d,running'							)
						self.assertListEqual(UpperCAmelCase_       ,   ['un', '##want', '##ed', ',', 'runn', '##ing']							)
						self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase_							)       ,   [7, 4, 5, 10, 8, 9]							)





	def      A_				(			self							:   List[str]							):
						if not self.test_rust_tokenizer:
											return

						SCREAMING_SNAKE_CASE__													= self.get_tokenizer()
						SCREAMING_SNAKE_CASE__													= self.get_rust_tokenizer()

						SCREAMING_SNAKE_CASE__													= 'I was born in 92000, and this is falsé.'

						SCREAMING_SNAKE_CASE__													= tokenizer.tokenize(UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= rust_tokenizer.tokenize(UpperCAmelCase_							)
						self.assertListEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)

						SCREAMING_SNAKE_CASE__													= tokenizer.encode(UpperCAmelCase_       ,   add_special_tokens=UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= rust_tokenizer.encode(UpperCAmelCase_       ,   add_special_tokens=UpperCAmelCase_							)
						self.assertListEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)

						SCREAMING_SNAKE_CASE__													= self.get_rust_tokenizer()
						SCREAMING_SNAKE_CASE__													= tokenizer.encode(UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= rust_tokenizer.encode(UpperCAmelCase_							)
						self.assertListEqual(UpperCAmelCase_       ,   UpperCAmelCase_							)





