'''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     ,			_lowerCamelCase     ,			_lowerCamelCase=1_3     ,			_lowerCamelCase=7     ,			_lowerCamelCase=True     ,			_lowerCamelCase=True     ,			_lowerCamelCase=True     ,			_lowerCamelCase=9_9     ,			_lowerCamelCase=3_2     ,			_lowerCamelCase=5     ,			_lowerCamelCase=4     ,			_lowerCamelCase=3_7     ,			_lowerCamelCase="gelu"     ,			_lowerCamelCase=0.1     ,			_lowerCamelCase=0.1     ,			_lowerCamelCase=5_1_2     ,			_lowerCamelCase=1_6     ,			_lowerCamelCase=2     ,			_lowerCamelCase=0.0_2     ,			_lowerCamelCase=3     ,			_lowerCamelCase=4     ,			_lowerCamelCase=None     ,			):
         __UpperCamelCase	:	Dict		    =  parent
         __UpperCamelCase	:	Optional[Any]		    =  batch_size
         __UpperCamelCase	:	Dict		    =  seq_length
         __UpperCamelCase	:	Any		    =  is_training
         __UpperCamelCase	:	str		    =  use_token_type_ids
         __UpperCamelCase	:	Dict		    =  use_labels
         __UpperCamelCase	:	Dict		    =  vocab_size
         __UpperCamelCase	:	Optional[int]		    =  hidden_size
         __UpperCamelCase	:	Tuple		    =  num_hidden_layers
         __UpperCamelCase	:	str		    =  num_attention_heads
         __UpperCamelCase	:	Dict		    =  intermediate_size
         __UpperCamelCase	:	List[str]		    =  hidden_act
         __UpperCamelCase	:	Union[str, Any]		    =  hidden_dropout_prob
         __UpperCamelCase	:	Optional[Any]		    =  attention_probs_dropout_prob
         __UpperCamelCase	:	List[Any]		    =  max_position_embeddings
         __UpperCamelCase	:	str		    =  type_vocab_size
         __UpperCamelCase	:	Any		    =  type_sequence_label_size
         __UpperCamelCase	:	List[str]		    =  initializer_range
         __UpperCamelCase	:	List[str]		    =  num_labels
         __UpperCamelCase	:	List[str]		    =  num_choices
         __UpperCamelCase	:	Union[str, Any]		    =  scope
         __UpperCamelCase	:	List[str]		    =  self.vocab_size - 1







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	int		    =  ids_tensor([self.batch_size, self.seq_length]     ,			self.vocab_size    )

         __UpperCamelCase	:	Dict		    =  None
         if self.use_token_type_ids:
             __UpperCamelCase	:	List[str]		    =  ids_tensor([self.batch_size, self.seq_length]     ,			self.type_vocab_size    )

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

         __UpperCamelCase	:	int		    =  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     ,			)

         __UpperCamelCase	:	int		    =  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     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			*_lowerCamelCase    ):
         __UpperCamelCase	:	Tuple		    =  OpenAIGPTModel(config=_lowerCamelCase    )
         model.to(_lowerCamelCase    )
         model.eval()

         __UpperCamelCase	:	Tuple		    =  model(_lowerCamelCase     ,			token_type_ids=_lowerCamelCase     ,			head_mask=_lowerCamelCase    )
         __UpperCamelCase	:	Any		    =  model(_lowerCamelCase     ,			token_type_ids=_lowerCamelCase    )
         __UpperCamelCase	:	Optional[Any]		    =  model(_lowerCamelCase    )

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







     def lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			*_lowerCamelCase    ):
         __UpperCamelCase	:	Optional[int]		    =  OpenAIGPTLMHeadModel(_lowerCamelCase    )
         model.to(_lowerCamelCase    )
         model.eval()

         __UpperCamelCase	:	Dict		    =  model(_lowerCamelCase     ,			token_type_ids=_lowerCamelCase     ,			labels=_lowerCamelCase    )
         self.parent.assertEqual(result.loss.shape     ,			()    )
         self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.seq_length, self.vocab_size)    )







     def lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			*_lowerCamelCase    ):
         __UpperCamelCase	:	int		    =  OpenAIGPTDoubleHeadsModel(_lowerCamelCase    )
         model.to(_lowerCamelCase    )
         model.eval()

         __UpperCamelCase	:	int		    =  model(_lowerCamelCase     ,			token_type_ids=_lowerCamelCase     ,			labels=_lowerCamelCase    )
         self.parent.assertEqual(result.loss.shape     ,			()    )
         self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.seq_length, self.vocab_size)    )







     def lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			*_lowerCamelCase    ):
         __UpperCamelCase	:	List[Any]		    =  self.num_labels
         __UpperCamelCase	:	List[str]		    =  OpenAIGPTForSequenceClassification(_lowerCamelCase    )
         model.to(_lowerCamelCase    )
         model.eval()

         __UpperCamelCase	:	Any		    =  ids_tensor([self.batch_size]     ,			self.type_sequence_label_size    )
         __UpperCamelCase	:	Dict		    =  model(_lowerCamelCase     ,			token_type_ids=_lowerCamelCase     ,			labels=_lowerCamelCase    )
         self.parent.assertEqual(result.logits.shape     ,			(self.batch_size, self.num_labels)    )







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Optional[int]		    =  self.prepare_config_and_inputs()
         (
             __UpperCamelCase					
         )	:	Tuple		    =  config_and_inputs
         __UpperCamelCase	:	str		    =  {
             'input_ids': input_ids,
             'token_type_ids': token_type_ids,
             'head_mask': head_mask,
         }

         return config, inputs_dict






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







     """simple docstring"""






     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 lowerCAmelCase     (		self     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase    ):
         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     ,			_lowerCamelCase     ,			_lowerCamelCase     ,			_lowerCamelCase=False    ):
         __UpperCamelCase	:	Any		    =  super()._prepare_for_class(_lowerCamelCase     ,			_lowerCamelCase     ,			return_labels=_lowerCamelCase    )

         if return_labels:
             if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                 __UpperCamelCase	:	List[Any]		    =  torch.zeros(
                     (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)     ,			dtype=torch.long     ,			device=_lowerCamelCase     ,			)
                 __UpperCamelCase	:	Any		    =  inputs_dict['labels']
                 __UpperCamelCase	:	Tuple		    =  inputs_dict['labels']
                 __UpperCamelCase	:	Optional[int]		    =  torch.zeros(
                     (self.model_tester.batch_size, self.model_tester.num_choices)     ,			dtype=torch.long     ,			device=_lowerCamelCase     ,			)
                 __UpperCamelCase	:	Any		    =  torch.zeros(
                     self.model_tester.batch_size     ,			dtype=torch.long     ,			device=_lowerCamelCase    )
         return inputs_dict







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Union[str, Any]		    =  OpenAIGPTModelTester(self    )
         __UpperCamelCase	:	List[str]		    =  ConfigTester(self     ,			config_class=_lowerCamelCase     ,			n_embd=3_7    )







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







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	List[str]		    =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_model(*_lowerCamelCase    )







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Tuple		    =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_lm_head_model(*_lowerCamelCase    )







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Tuple		    =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_double_lm_head_model(*_lowerCamelCase    )







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Tuple		    =  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCamelCase    )







     @slow
     def lowerCAmelCase     (		self    ):
         for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __UpperCamelCase	:	Any		    =  OpenAIGPTModel.from_pretrained(_lowerCamelCase    )
             self.assertIsNotNone(_lowerCamelCase    )






@require_torch
class 			__lowercase		(						unittest.TestCase						):







     """simple docstring"""






     @slow
     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Dict		    =  OpenAIGPTLMHeadModel.from_pretrained('openai-gpt'    )
         model.to(_lowerCamelCase    )
         __UpperCamelCase	:	Tuple		    =  torch.tensor([[4_8_1, 4_7_3_5, 5_4_4]]     ,			dtype=torch.long     ,			device=_lowerCamelCase    )  # the president is
         __UpperCamelCase	:	Union[str, Any]		    =  [
             4_8_1,
             4_7_3_5,
             5_4_4,
             2_4_6,
             9_6_3,
             8_7_0,
             7_6_2,
             2_3_9,
             2_4_4,
             4_0_4_7_7,
             2_4_4,
             2_4_9,
             7_1_9,
             8_8_1,
             4_8_7,
             5_4_4,
             2_4_0,
             2_4_4,
             6_0_3,
             4_8_1,
         ]  # the president is a very good man. " \n " i\'m sure he is, " said the

         __UpperCamelCase	:	Union[str, Any]		    =  model.generate(_lowerCamelCase     ,			do_sample=_lowerCamelCase    )
         self.assertListEqual(output_ids[0].tolist()     ,			_lowerCamelCase    )


'''simple docstring'''
import os
import unittest

from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast
from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin






@require_tokenizers
class 			__lowercase		(						_lowerCamelCase			,							unittest.TestCase						):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		   =						LayoutLMTokenizer
     SCREAMING_SNAKE_CASE__		   =						LayoutLMTokenizerFast
     SCREAMING_SNAKE_CASE__		   =						True
     SCREAMING_SNAKE_CASE__		   =						True







     def lowerCAmelCase     (		self    ):
         super().setUp()

         __UpperCamelCase	:	List[str]		    =  [
             '[UNK]',
             '[CLS]',
             '[SEP]',
             'want',
             '##want',
             '##ed',
             'wa',
             'un',
             'runn',
             '##ing',
             ',',
             'low',
             'lowest',
         ]
         __UpperCamelCase	:	Union[str, Any]		    =  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 lowerCAmelCase     (		self     ,			**_lowerCamelCase    ):
         return LayoutLMTokenizer.from_pretrained(self.tmpdirname     ,			**_lowerCamelCase    )







     def lowerCAmelCase     (		self     ,			_lowerCamelCase    ):
         __UpperCamelCase	:	Dict		    =  'UNwant\u00E9d,running'
         __UpperCamelCase	:	Union[str, Any]		    =  'unwanted, running'
         return input_text, output_text







     def lowerCAmelCase     (		self    ):
         __UpperCamelCase	:	Dict		    =  self.tokenizer_class(self.vocab_file    )

         __UpperCamelCase	:	Optional[Any]		    =  tokenizer.tokenize('UNwant\u00E9d,running'    )
         self.assertListEqual(_lowerCamelCase     ,			['un', '##want', '##ed', ',', 'runn', '##ing']    )
         self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCamelCase    )     ,			[7, 4, 5, 1_0, 8, 9]    )







     def lowerCAmelCase     (		self    ):
         pass



