




'''simple docstring'''







from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers.models.openai.modeling_tf_openai import (
       TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFOpenAIGPTDoubleHeadsModel,
       TFOpenAIGPTForSequenceClassification,
       TFOpenAIGPTLMHeadModel,
       TFOpenAIGPTModel,
   )


class      _a    :
       def __init__(      self		:      Optional[Any]  ,							lowercase		:      Dict  ,							):



           '''simple docstring'''

           UpperCAmelCase     			=						parent
           UpperCAmelCase     			=						13
           UpperCAmelCase     			=						7
           UpperCAmelCase     			=						True
           UpperCAmelCase     			=						True
           UpperCAmelCase     			=						True
           UpperCAmelCase     			=						True
           UpperCAmelCase     			=						True
           UpperCAmelCase     			=						99
           UpperCAmelCase     			=						32
           UpperCAmelCase     			=						2
           UpperCAmelCase     			=						4
           UpperCAmelCase     			=						37
           UpperCAmelCase     			=						'''gelu'''
           UpperCAmelCase     			=						0.1
           UpperCAmelCase     			=						0.1
           UpperCAmelCase     			=						512
           UpperCAmelCase     			=						16
           UpperCAmelCase     			=						2
           UpperCAmelCase     			=						0.02
           UpperCAmelCase     			=						3
           UpperCAmelCase     			=						4
           UpperCAmelCase     			=						None
           UpperCAmelCase     			=						self.vocab_size - 1
       def        A (      self		:      str							):



           '''simple docstring'''

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

           UpperCAmelCase     			=						None
           if self.use_input_mask:
               UpperCAmelCase     			=						random_attention_mask([self.batch_size, self.seq_length]							)

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

           UpperCAmelCase     			=						None
           if self.use_mc_token_ids:
               UpperCAmelCase     			=						ids_tensor([self.batch_size, self.num_choices]  ,							self.seq_length							)

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

           UpperCAmelCase     			=						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     			=						ids_tensor([self.num_hidden_layers, self.num_attention_heads]  ,							2							)

           return (
               config,
               input_ids,
               input_mask,
               head_mask,
               token_type_ids,
               mc_token_ids,
               sequence_labels,
               token_labels,
               choice_labels,
           )
       def        A (      self		:      str  ,							lowercase		:      Any  ,							lowercase		:      Optional[int]  ,							lowercase		:      Tuple  ,							lowercase		:      Tuple  ,							lowercase		:      List[Any]  ,							*lowercase		:      Tuple							):



           '''simple docstring'''

           UpperCAmelCase     			=						TFOpenAIGPTModel(config=_SCREAMING_SNAKE_CASE							)
           UpperCAmelCase     			=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           UpperCAmelCase     			=						model(_SCREAMING_SNAKE_CASE							)

           UpperCAmelCase     			=						[input_ids, input_mask]
           UpperCAmelCase     			=						model(_SCREAMING_SNAKE_CASE							)

           UpperCAmelCase     			=						model(_SCREAMING_SNAKE_CASE							)

           self.parent.assertEqual(result.last_hidden_state.shape  ,							(self.batch_size, self.seq_length, self.hidden_size)							)
       def        A (      self		:      List[str]  ,							lowercase		:      str  ,							lowercase		:      Dict  ,							lowercase		:      Any  ,							lowercase		:      List[Any]  ,							lowercase		:      Optional[Any]  ,							*lowercase		:      Any							):



           '''simple docstring'''

           UpperCAmelCase     			=						TFOpenAIGPTLMHeadModel(config=_SCREAMING_SNAKE_CASE							)
           UpperCAmelCase     			=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
           UpperCAmelCase     			=						model(_SCREAMING_SNAKE_CASE							)
           self.parent.assertEqual(result.logits.shape  ,							(self.batch_size, self.seq_length, self.vocab_size)							)
       def        A (      self		:      Union[str, Any]  ,							lowercase		:      List[str]  ,							lowercase		:      Any  ,							lowercase		:      Dict  ,							lowercase		:      Optional[int]  ,							lowercase		:      str  ,							lowercase		:      str  ,							*lowercase		:      int							):



           '''simple docstring'''

           UpperCAmelCase     			=						TFOpenAIGPTDoubleHeadsModel(config=_SCREAMING_SNAKE_CASE							)

           UpperCAmelCase     			=						tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE  ,							1							)  ,							(1, self.num_choices, 1)							)
           UpperCAmelCase     			=						tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE  ,							1							)  ,							(1, self.num_choices, 1)							)
           UpperCAmelCase     			=						tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE  ,							1							)  ,							(1, self.num_choices, 1)							)

           UpperCAmelCase     			=						{
               '''input_ids''': multiple_choice_inputs_ids,
               '''mc_token_ids''': mc_token_ids,
               '''attention_mask''': multiple_choice_input_mask,
               '''token_type_ids''': multiple_choice_token_type_ids,
           }
           UpperCAmelCase     			=						model(_SCREAMING_SNAKE_CASE							)
           self.parent.assertEqual(
               result.logits.shape  ,							(self.batch_size, self.num_choices, self.seq_length, self.vocab_size)							)
           self.parent.assertEqual(result.mc_logits.shape  ,							(self.batch_size, self.num_choices)							)
       def        A (      self		:      List[str]  ,							lowercase		:      List[str]  ,							lowercase		:      Optional[Any]  ,							lowercase		:      Tuple  ,							lowercase		:      Union[str, Any]  ,							lowercase		:      int  ,							*lowercase		:      Any							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.num_labels
           UpperCAmelCase     			=						ids_tensor([self.batch_size]  ,							self.type_sequence_label_size							)
           UpperCAmelCase     			=						{
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
               '''labels''': sequence_labels,
           }
           UpperCAmelCase     			=						TFOpenAIGPTForSequenceClassification(_SCREAMING_SNAKE_CASE							)
           UpperCAmelCase     			=						model(_SCREAMING_SNAKE_CASE							)
           self.parent.assertEqual(result.logits.shape  ,							(self.batch_size, self.num_labels)							)







       def        A (      self		:      Optional[Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.prepare_config_and_inputs()

           (
               (
               UpperCAmelCase
           )			,	(
               UpperCAmelCase
           )			,	(
               UpperCAmelCase
           )			,	(
               UpperCAmelCase
           )			,	(
               UpperCAmelCase
           )			,	(
               UpperCAmelCase
           )			,	(
               UpperCAmelCase
           )			,	(
               UpperCAmelCase
           )			,	(
               UpperCAmelCase
           )			,	
           )     			=						config_and_inputs

           UpperCAmelCase     			=						{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
           return config, inputs_dict






@require_tf
class      _a    (      __a				,				__a				,				unittest.TestCase					):
       __a							:	List[Any]  							=   (
           (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
           if is_tf_available()
           else ()
       )
       __a							:	Any  							=   (
           (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
       )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
       __a							:	List[str]  							=   (
           {
               "feature-extraction": TFOpenAIGPTModel,
               "text-classification": TFOpenAIGPTForSequenceClassification,
               "text-generation": TFOpenAIGPTLMHeadModel,
               "zero-shot": TFOpenAIGPTForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       __a							:	List[Any]  							=   False
       __a							:	Optional[int]  							=   False
       def        A (      self		:      Optional[int]  ,							lowercase		:      Any  ,							lowercase		:      str  ,							lowercase		:      List[Any]  ,							lowercase		:      Dict  ,							lowercase		:      int							):



           '''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		:      int							):



           '''simple docstring'''

           UpperCAmelCase     			=						TFOpenAIGPTModelTester(self							)
           UpperCAmelCase     			=						ConfigTester(self  ,							config_class=_SCREAMING_SNAKE_CASE  ,							n_embd=37							)
       def        A (      self		:      Tuple							):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*_SCREAMING_SNAKE_CASE							)
       def        A (      self		:      str							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_lm_head(*_SCREAMING_SNAKE_CASE							)
       def        A (      self		:      List[str]							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_double_head(*_SCREAMING_SNAKE_CASE							)
       def        A (      self		:      List[str]							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_SCREAMING_SNAKE_CASE							)







       @slow
       def        A (      self		:      Tuple							):



           '''simple docstring'''

           for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase     			=						TFOpenAIGPTModel.from_pretrained(_SCREAMING_SNAKE_CASE							)
               self.assertIsNotNone(_SCREAMING_SNAKE_CASE							)






@require_tf
class      _a    (      unittest.TestCase					):
       @slow
       def        A (      self		:      int							):



           '''simple docstring'''

           UpperCAmelCase     			=						TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''							)
           UpperCAmelCase     			=						tf.convert_to_tensor([[481, 4_735, 544]]  ,							dtype=tf.intaa							)  # the president is
           UpperCAmelCase     			=						[
               481,
               4_735,
               544,
               246,
               963,
               870,
               762,
               239,
               244,
               40_477,
               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

           UpperCAmelCase     			=						model.generate(_SCREAMING_SNAKE_CASE  ,							do_sample=_SCREAMING_SNAKE_CASE							)
           self.assertListEqual(output_ids[0].numpy().tolist()  ,							_SCREAMING_SNAKE_CASE							)





"""simple docstring"""







from transformers.models.cpm.tokenization_cpm import CpmTokenizer
from transformers.testing_utils import custom_tokenizers

from ..xlnet.test_modeling_xlnet import XLNetModelTest






@custom_tokenizers
class      _SCREAMING_SNAKE_CASE							(			UpperCAmelCase				):



   '''simple docstring'''



   def    _A				(	self ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE	):


      '''simple docstring'''

      return True







   def    _A				(	self	):


      '''simple docstring'''

      __lowerCamelCase    		=					CpmTokenizer.from_pretrained('TsinghuaAI/CPM-Generate'	)
      __lowerCamelCase    		=					'Hugging Face大法好，谁用谁知道。'
      __lowerCamelCase    		=					'Hugging Face大法好,谁用谁知道。<unk>'
      __lowerCamelCase    		=					'▁Hu gg ing ▁ ▂ ▁F ace ▁大法 ▁好 ▁ , ▁谁 ▁用 ▁谁 ▁知 道 ▁ 。'.split()

      __lowerCamelCase    		=					tokenizer.tokenize(_SCREAMING_SNAKE_CASE	)
      self.assertListEqual(_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE	)

      __lowerCamelCase    		=					tokens + [tokenizer.unk_token]

      __lowerCamelCase    		=					[13789, 13283, 1421, 8, 10, 1164, 13608, 16528, 63, 8, 9, 440, 108, 440, 121, 90, 8, 12, 0]
      self.assertListEqual(tokenizer.convert_tokens_to_ids(_SCREAMING_SNAKE_CASE	) ,				_SCREAMING_SNAKE_CASE	)

      __lowerCamelCase    		=					tokenizer.decode(_SCREAMING_SNAKE_CASE	)
      self.assertEqual(_SCREAMING_SNAKE_CASE ,				_SCREAMING_SNAKE_CASE	)






