


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        snake_case_   :
					def __init__(      self   :str			,__snake_case   :Union[str, Any]			,__snake_case   :str=13			,__snake_case   :Optional[int]=7			,__snake_case   :List[str]=True			,__snake_case   :Union[str, Any]=True			,__snake_case   :Any=True			,__snake_case   :Any=99			,__snake_case   :Union[str, Any]=32			,__snake_case   :Dict=5			,__snake_case   :str=4			,__snake_case   :Tuple=37			,__snake_case   :List[str]="gelu"			,__snake_case   :Optional[Any]=0.1			,__snake_case   :Dict=0.1			,__snake_case   :str=5_12			,__snake_case   :Any=16			,__snake_case   :Union[str, Any]=2			,__snake_case   :Optional[int]=0.02			,__snake_case   :List[Any]=3			,__snake_case   :str=4			,__snake_case   :Any=None			,)					->    List[Any]:
												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  lowerCamelCase__(      self   :str      )					->    Tuple:
												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  lowerCamelCase__(      self   :List[str]			,__snake_case   :List[str]			,__snake_case   :Optional[int]			,__snake_case   :Optional[int]			,__snake_case   :Dict			,*__snake_case   :Optional[int]      )					->    List[str]:
												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  lowerCamelCase__(      self   :Tuple			,__snake_case   :Optional[int]			,__snake_case   :Union[str, Any]			,__snake_case   :str			,__snake_case   :Any			,*__snake_case   :List[str]      )					->    Optional[int]:
												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  lowerCamelCase__(      self   :int			,__snake_case   :Dict			,__snake_case   :Tuple			,__snake_case   :List[str]			,__snake_case   :Tuple			,*__snake_case   :Optional[int]      )					->    Tuple:
												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  lowerCamelCase__(      self   :List[str]			,__snake_case   :List[Any]			,__snake_case   :List[Any]			,__snake_case   :str			,__snake_case   :str			,*__snake_case   :Any      )					->    str:
												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  lowerCamelCase__(      self   :Tuple      )					->    str:
												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        snake_case_   (lowerCamelCase_	,						lowerCamelCase_	,						lowerCamelCase_	,						unittest.TestCase      ):
					UpperCAmelCase__					:    List[Any]          =			(
					    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
					    if is_torch_available()
					    else ()
					)
					UpperCAmelCase__					:    int          =			(
					    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
					)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
					UpperCAmelCase__					:    Any          =			(
					    {
					        '''feature-extraction''': OpenAIGPTModel,
					        '''text-classification''': OpenAIGPTForSequenceClassification,
					        '''text-generation''': OpenAIGPTLMHeadModel,
					        '''zero-shot''': OpenAIGPTForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					def  lowerCamelCase__(      self   :str			,__snake_case   :Union[str, Any]			,__snake_case   :Dict			,__snake_case   :Optional[Any]			,__snake_case   :Tuple			,__snake_case   :Optional[int]      )					->    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  lowerCamelCase__(      self   :Optional[Any]			,__snake_case   :Any			,__snake_case   :Optional[int]			,__snake_case   :Tuple=False      )					->    Tuple:
												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  lowerCamelCase__(      self   :str      )					->    Tuple:
												a__             =     OpenAIGPTModelTester(self      )
												a__             =     ConfigTester(self			,config_class=__snake_case			,n_embd=37      )
					def  lowerCamelCase__(      self   :Union[str, Any]      )					->    Dict:
												self.config_tester.run_common_tests()
					def  lowerCamelCase__(      self   :Optional[Any]      )					->    Optional[Any]:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_model(*__snake_case      )
					def  lowerCamelCase__(      self   :Optional[int]      )					->    Tuple:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_lm_head_model(*__snake_case      )
					def  lowerCamelCase__(      self   :Optional[Any]      )					->    List[str]:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_double_lm_head_model(*__snake_case      )
					def  lowerCamelCase__(      self   :Dict      )					->    Optional[int]:
												a__             =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__snake_case      )



					@slow
					def  lowerCamelCase__(      self   :List[Any]      )					->    Union[str, Any]:
												for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			a__             =     OpenAIGPTModel.from_pretrained(__snake_case      )
																			self.assertIsNotNone(__snake_case      )







@require_torch
class        snake_case_   (unittest.TestCase      ):
					@slow
					def  lowerCamelCase__(      self   :int      )					->    int:
												a__             =     OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'      )
												model.to(__snake_case      )
												a__             =     torch.tensor([[4_81, 47_35, 5_44]]			,dtype=torch.long			,device=__snake_case      )  # the president is
												a__             =     [
												    4_81,
												    47_35,
												    5_44,
												    2_46,
												    9_63,
												    8_70,
												    7_62,
												    2_39,
												    2_44,
												    4_04_77,
												    2_44,
												    2_49,
												    7_19,
												    8_81,
												    4_87,
												    5_44,
												    2_40,
												    2_44,
												    6_03,
												    4_81,
												]  # 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 json
import os
import unittest

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin







class        snake_case_   (lowerCamelCase_	,						unittest.TestCase      ):
					UpperCAmelCase__					:    Tuple          =			XLMTokenizer
					UpperCAmelCase__					:    List[str]          =			False
					def  lowerCamelCase__(      self   :Any      )					->    Optional[Any]:
												super().setUp()

												# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
												a__             =     [
												    'l',
												    'o',
												    'w',
												    'e',
												    'r',
												    's',
												    't',
												    'i',
												    'd',
												    'n',
												    'w</w>',
												    'r</w>',
												    't</w>',
												    'lo',
												    'low',
												    'er</w>',
												    'low</w>',
												    'lowest</w>',
												    'newer</w>',
												    'wider</w>',
												    '<unk>',
												]
												a__             =     dict(zip(__snake_case			,range(len(__snake_case      )      )      )      )
												a__             =     ['l o 123', 'lo w 1456', 'e r</w> 1789', '']

												a__             =     os.path.join(self.tmpdirname			,VOCAB_FILES_NAMES['vocab_file']      )
												a__             =     os.path.join(self.tmpdirname			,VOCAB_FILES_NAMES['merges_file']      )
												with open(self.vocab_file			,'w'      ) as fp:
																			fp.write(json.dumps(__snake_case      )      )
												with open(self.merges_file			,'w'      ) as fp:
																			fp.write('\n'.join(__snake_case      )      )
					def  lowerCamelCase__(      self   :Any			,__snake_case   :int      )					->    Optional[int]:
												a__             =     'lower newer'
												a__             =     'lower newer'
												return input_text, output_text
					def  lowerCamelCase__(      self   :Tuple      )					->    Tuple:
												a__             =     XLMTokenizer(self.vocab_file			,self.merges_file      )

												a__             =     'lower'
												a__             =     ['low', 'er</w>']
												a__             =     tokenizer.tokenize(__snake_case      )
												self.assertListEqual(__snake_case			,__snake_case      )

												a__             =     tokens + ['<unk>']
												a__             =     [14, 15, 20]
												self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case      )			,__snake_case      )



					@slow
					def  lowerCamelCase__(      self   :List[str]      )					->    Optional[Any]:
												a__             =     XLMTokenizer.from_pretrained('xlm-mlm-en-2048'      )

												a__             =     tokenizer.encode('sequence builders'			,add_special_tokens=__snake_case      )
												a__             =     tokenizer.encode('multi-sequence build'			,add_special_tokens=__snake_case      )

												a__             =     tokenizer.build_inputs_with_special_tokens(__snake_case      )
												a__             =     tokenizer.build_inputs_with_special_tokens(__snake_case			,__snake_case      )

												assert encoded_sentence == [0] + text + [1]
												assert encoded_pair == [0] + text + [1] + text_a + [1]




