



'''simple docstring'''





import unittest

from transformers import DonutProcessor


A					:       List[Any]									=     'naver-clova-ix/donut-base'





class 				lowerCAmelCase_						(			unittest.TestCase		):





    def 	__snake_case  (							self							:							Optional[Any]      ):



       '''simple docstring'''







       snake_case       :				int		    =DonutProcessor.from_pretrained(__a      )






    def 	__snake_case  (							self							:							Dict      ):



       '''simple docstring'''







       snake_case       :				Optional[Any]		    ={
           '''name''': '''John Doe''',
           '''age''': '''99''',
           '''city''': '''Atlanta''',
           '''state''': '''GA''',
           '''zip''': '''30301''',
           '''phone''': '''123-4567''',
           '''nicknames''': [{'''nickname''': '''Johnny'''}, {'''nickname''': '''JD'''}],
       }

       snake_case       :				Tuple		    =(
           '''<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>'''
           '''<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>'''
           '''<s_nicknames><s_nickname>Johnny</s_nickname>'''
           '''<sep/><s_nickname>JD</s_nickname></s_nicknames>'''
       )
       snake_case       :				Any		    =self.processor.tokenajson(__a      )

       self.assertDictEqual(__a,							__a      )










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  lowerCamelCase__	:
     """simple docstring"""



     def __init__(self					,      __a					,      __a=13					,      __a=7					,      __a=True					,      __a=True					,      __a=True					,      __a=99					,      __a=32					,      __a=5					,      __a=4					,      __a=37					,      __a="gelu"					,      __a=0.1					,      __a=0.1					,      __a=5_12					,      __a=16					,      __a=2					,      __a=0.02					,      __a=3					,      __a=4					,      __a=None					,      ):






           '''simple docstring'''



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







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		ids_tensor([self.batch_size, self.seq_length]					,      self.vocab_size     )

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

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

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

           lowerCamelCase             =		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					,      __a					,      __a					,      __a					,      __a					,      *__a     ):






           '''simple docstring'''



           lowerCamelCase             =		OpenAIGPTModel(config=__a     )
           model.to(__a     )
           model.eval()

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

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







     def     _a   (self					,      __a					,      __a					,      __a					,      __a					,      *__a     ):






           '''simple docstring'''



           lowerCamelCase             =		OpenAIGPTLMHeadModel(__a     )
           model.to(__a     )
           model.eval()

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







     def     _a   (self					,      __a					,      __a					,      __a					,      __a					,      *__a     ):






           '''simple docstring'''



           lowerCamelCase             =		OpenAIGPTDoubleHeadsModel(__a     )
           model.to(__a     )
           model.eval()

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







     def     _a   (self					,      __a					,      __a					,      __a					,      __a					,      *__a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.num_labels
           lowerCamelCase             =		OpenAIGPTForSequenceClassification(__a     )
           model.to(__a     )
           model.eval()

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







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.prepare_config_and_inputs()
           (
               (
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				(
               lowerCamelCase
           )      ,				
           )             =		config_and_inputs
           lowerCamelCase             =		{
               "input_ids": input_ids,
               "token_type_ids": token_type_ids,
               "head_mask": head_mask,
           }

           return config, inputs_dict





@require_torch
class  lowerCamelCase__	(    UpperCAmelCase_							,     UpperCAmelCase_							,     UpperCAmelCase_							,     unittest.TestCase):
     """simple docstring"""



     _A      =      (
         (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
         if is_torch_available()
         else ()
     )
     _A      =      (
         (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     _A      =      (
         {
             'feature-extraction': OpenAIGPTModel,
             'text-classification': OpenAIGPTForSequenceClassification,
             'text-generation': OpenAIGPTLMHeadModel,
             'zero-shot': OpenAIGPTForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )







     def     _a   (self					,      __a					,      __a					,      __a					,      __a					,      __a     ):






           '''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     _a   (self					,      __a					,      __a					,      __a=False     ):






           '''simple docstring'''



           lowerCamelCase             =		super()._prepare_for_class(__a					,      __a					,      return_labels=__a     )

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







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		OpenAIGPTModelTester(self     )
           lowerCamelCase             =		ConfigTester(self					,      config_class=__a					,      n_embd=37     )







     def     _a   (self     ):






           '''simple docstring'''



           self.config_tester.run_common_tests()







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*__a     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head_model(*__a     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_double_lm_head_model(*__a     )







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__a     )







     @slow
     def     _a   (self     ):






           '''simple docstring'''



           for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCamelCase             =		OpenAIGPTModel.from_pretrained(__a     )
                 self.assertIsNotNone(__a     )





@require_torch
class  lowerCamelCase__	(    unittest.TestCase):
     """simple docstring"""



     @slow
     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"     )
           model.to(__a     )
           lowerCamelCase             =		torch.tensor([[4_81, 47_35, 5_44]]					,      dtype=torch.long					,      device=__a     )  # the president is
           lowerCamelCase             =		[
               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

           lowerCamelCase             =		model.generate(__a					,      do_sample=__a     )
           self.assertListEqual(output_ids[0].tolist()					,      __a     )