
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 						UpperCAmelCase	:



   def __init__(self	:      int					,		snake_case__	:      Optional[int]					,		)	->			List[Any]:


          '''simple docstring'''


          snake_case :     List[Any]           =   parent
          snake_case :     Dict           =   13
          snake_case :     Dict           =   7
          snake_case :     Dict           =   True
          snake_case :     List[Any]           =   True
          snake_case :     str           =   True
          snake_case :     Union[str, Any]           =   True
          snake_case :     Optional[Any]           =   True
          snake_case :     Union[str, Any]           =   99
          snake_case :     Any           =   32
          snake_case :     Union[str, Any]           =   2
          snake_case :     Union[str, Any]           =   4
          snake_case :     str           =   37
          snake_case :     int           =   """gelu"""
          snake_case :     Optional[Any]           =   0.1
          snake_case :     List[Any]           =   0.1
          snake_case :     List[str]           =   5_12
          snake_case :     Optional[Any]           =   16
          snake_case :     str           =   2
          snake_case :     List[str]           =   0.02
          snake_case :     Any           =   3
          snake_case :     str           =   4
          snake_case :     Any           =   None
          snake_case :     Any           =   self.vocab_size - 1



   def        _SCREAMING_SNAKE_CASE     (self	:      Tuple				)	->			Any:


          '''simple docstring'''


          snake_case :     List[str]           =   ids_tensor([self.batch_size, self.seq_length]					,		self.vocab_size				)

          snake_case :     Tuple           =   None
          if self.use_input_mask:
                 snake_case :     List[str]           =   random_attention_mask([self.batch_size, self.seq_length]				)

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

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

          snake_case :     str           =   None
          snake_case :     Dict           =   None
          snake_case :     Optional[Any]           =   None
          if self.use_labels:
                 snake_case :     Union[str, Any]           =   ids_tensor([self.batch_size]					,		self.type_sequence_label_size				)
                 snake_case :     Dict           =   ids_tensor([self.batch_size, self.seq_length]					,		self.num_labels				)
                 snake_case :     str           =   ids_tensor([self.batch_size]					,		self.num_choices				)

          snake_case :     Any           =   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					,		)

          snake_case :     Tuple           =   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        _SCREAMING_SNAKE_CASE     (self	:      Union[str, Any]					,		snake_case__	:      Tuple					,		snake_case__	:      List[str]					,		snake_case__	:      int					,		snake_case__	:      Tuple					,		snake_case__	:      Dict					,		*snake_case__	:      Tuple				)	->			List[str]:


          '''simple docstring'''


          snake_case :     Any           =   TFOpenAIGPTModel(config=_lowerCamelCase				)
          snake_case :     Any           =   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          snake_case :     Optional[int]           =   model(_lowerCamelCase				)

          snake_case :     List[str]           =   [input_ids, input_mask]
          snake_case :     Dict           =   model(_lowerCamelCase				)

          snake_case :     List[str]           =   model(_lowerCamelCase				)

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



   def        _SCREAMING_SNAKE_CASE     (self	:      Any					,		snake_case__	:      int					,		snake_case__	:      Tuple					,		snake_case__	:      Optional[Any]					,		snake_case__	:      List[Any]					,		snake_case__	:      int					,		*snake_case__	:      List[str]				)	->			Optional[Any]:


          '''simple docstring'''


          snake_case :     Union[str, Any]           =   TFOpenAIGPTLMHeadModel(config=_lowerCamelCase				)
          snake_case :     Tuple           =   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
          snake_case :     str           =   model(_lowerCamelCase				)
          self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.seq_length, self.vocab_size)				)



   def        _SCREAMING_SNAKE_CASE     (self	:      Any					,		snake_case__	:      str					,		snake_case__	:      Union[str, Any]					,		snake_case__	:      Tuple					,		snake_case__	:      int					,		snake_case__	:      int					,		snake_case__	:      str					,		*snake_case__	:      List[str]				)	->			Optional[Any]:


          '''simple docstring'''


          snake_case :     int           =   TFOpenAIGPTDoubleHeadsModel(config=_lowerCamelCase				)

          snake_case :     Optional[Any]           =   tf.tile(tf.expand_dims(_lowerCamelCase					,		1				)					,		(1, self.num_choices, 1)				)
          snake_case :     Any           =   tf.tile(tf.expand_dims(_lowerCamelCase					,		1				)					,		(1, self.num_choices, 1)				)
          snake_case :     Any           =   tf.tile(tf.expand_dims(_lowerCamelCase					,		1				)					,		(1, self.num_choices, 1)				)

          snake_case :     List[Any]           =   {
              """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,
          }
          snake_case :     int           =   model(_lowerCamelCase				)
          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        _SCREAMING_SNAKE_CASE     (self	:      Optional[int]					,		snake_case__	:      Dict					,		snake_case__	:      Union[str, Any]					,		snake_case__	:      List[str]					,		snake_case__	:      Any					,		snake_case__	:      Union[str, Any]					,		*snake_case__	:      int				)	->			Optional[int]:


          '''simple docstring'''


          snake_case :     List[Any]           =   self.num_labels
          snake_case :     int           =   ids_tensor([self.batch_size]					,		self.type_sequence_label_size				)
          snake_case :     Any           =   {
              """input_ids""": input_ids,
              """attention_mask""": input_mask,
              """token_type_ids""": token_type_ids,
              """labels""": sequence_labels,
          }
          snake_case :     Tuple           =   TFOpenAIGPTForSequenceClassification(_lowerCamelCase				)
          snake_case :     Union[str, Any]           =   model(_lowerCamelCase				)
          self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.num_labels)				)



   def        _SCREAMING_SNAKE_CASE     (self	:      int				)	->			List[Any]:


          '''simple docstring'''


          snake_case :     int           =   self.prepare_config_and_inputs()

          (
              snake_case		
          ) :     Optional[Any]           =   config_and_inputs

          snake_case :     Tuple           =   {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
          return config, inputs_dict






@require_tf
class 						UpperCAmelCase	(		lowerCAmelCase__ ,lowerCAmelCase__ ,unittest.TestCase				):
   A__      :	Dict		  =  (
       (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__      :	Tuple		  =  (
       {
           "feature-extraction": TFOpenAIGPTModel,
           "text-classification": TFOpenAIGPTForSequenceClassification,
           "text-generation": TFOpenAIGPTLMHeadModel,
           "zero-shot": TFOpenAIGPTForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   A__      :	List[Any]		  =  False
   A__      :	int		  =  False



   def        _SCREAMING_SNAKE_CASE     (self	:      List[Any]					,		snake_case__	:      Optional[int]					,		snake_case__	:      Union[str, Any]					,		snake_case__	:      Optional[int]					,		snake_case__	:      Any					,		snake_case__	:      str				)	->			Optional[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        _SCREAMING_SNAKE_CASE     (self	:      Tuple				)	->			Tuple:


          '''simple docstring'''


          snake_case :     List[str]           =   TFOpenAIGPTModelTester(self				)
          snake_case :     Optional[int]           =   ConfigTester(self					,		config_class=_lowerCamelCase					,		n_embd=37				)



   def        _SCREAMING_SNAKE_CASE     (self	:      List[Any]				)	->			Tuple:


          '''simple docstring'''


          self.config_tester.run_common_tests()



   def        _SCREAMING_SNAKE_CASE     (self	:      Any				)	->			List[Any]:


          '''simple docstring'''


          snake_case :     Tuple           =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_model(*_lowerCamelCase				)



   def        _SCREAMING_SNAKE_CASE     (self	:      int				)	->			List[str]:


          '''simple docstring'''


          snake_case :     Union[str, Any]           =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_lm_head(*_lowerCamelCase				)



   def        _SCREAMING_SNAKE_CASE     (self	:      Optional[int]				)	->			Optional[int]:


          '''simple docstring'''


          snake_case :     Dict           =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_double_head(*_lowerCamelCase				)



   def        _SCREAMING_SNAKE_CASE     (self	:      str				)	->			int:


          '''simple docstring'''


          snake_case :     Optional[int]           =   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCamelCase				)



   @slow
   def        _SCREAMING_SNAKE_CASE     (self	:      Any				)	->			List[Any]:


          '''simple docstring'''


          for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 snake_case :     int           =   TFOpenAIGPTModel.from_pretrained(_lowerCamelCase				)
                 self.assertIsNotNone(_lowerCamelCase				)






@require_tf
class 						UpperCAmelCase	(		unittest.TestCase				):



   @slow
   def        _SCREAMING_SNAKE_CASE     (self	:      Dict				)	->			Any:


          '''simple docstring'''


          snake_case :     int           =   TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"				)
          snake_case :     Optional[int]           =   tf.convert_to_tensor([[4_81, 47_35, 5_44]]					,		dtype=tf.intaa				)  # the president is
          snake_case :     Union[str, Any]           =   [
              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

          snake_case :     Any           =   model.generate(_lowerCamelCase					,		do_sample=_lowerCamelCase				)
          self.assertListEqual(output_ids[0].numpy().tolist()					,		_lowerCamelCase				)







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 						UpperCAmelCase	(		A_				):



   def        _SCREAMING_SNAKE_CASE     (self	:      List[str]					,		snake_case__	:      Dict					,		snake_case__	:      Tuple					,		snake_case__	:      Union[str, Any]					,		snake_case__	:      List[str]					,		snake_case__	:      List[Any]				)	->			Any:


          '''simple docstring'''


          return True



   def        _SCREAMING_SNAKE_CASE     (self	:      str				)	->			Dict:


          '''simple docstring'''


          snake_case :     Union[str, Any]           =   CpmTokenizer.from_pretrained("TsinghuaAI/CPM-Generate"				)
          snake_case :     str           =   "Hugging Face大法好，谁用谁知道。"
          snake_case :     Optional[Any]           =   "Hugging Face大法好,谁用谁知道。<unk>"
          snake_case :     int           =   "▁Hu gg ing ▁ ▂ ▁F ace ▁大法 ▁好 ▁ , ▁谁 ▁用 ▁谁 ▁知 道 ▁ 。".split()

          snake_case :     Union[str, Any]           =   tokenizer.tokenize(snake_case__				)
          self.assertListEqual(snake_case__					,		snake_case__				)

          snake_case :     Tuple           =   tokens + [tokenizer.unk_token]

          snake_case :     int           =   [1_37_89, 1_32_83, 14_21, 8, 10, 11_64, 1_36_08, 1_65_28, 63, 8, 9, 4_40, 1_08, 4_40, 1_21, 90, 8, 12, 0]
          self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__				)					,		snake_case__				)

          snake_case :     Dict           =   tokenizer.decode(snake_case__				)
          self.assertEqual(snake_case__					,		snake_case__				)





