




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

     def __init__(     self	: List[str]						,	A__	: List[str]						,	)					->							Any:
           lowerCamelCase__   	=					parent
           lowerCamelCase__   	=					1_3
           lowerCamelCase__   	=					7
           lowerCamelCase__   	=					True
           lowerCamelCase__   	=					True
           lowerCamelCase__   	=					True
           lowerCamelCase__   	=					True
           lowerCamelCase__   	=					True
           lowerCamelCase__   	=					9_9
           lowerCamelCase__   	=					3_2
           lowerCamelCase__   	=					2
           lowerCamelCase__   	=					4
           lowerCamelCase__   	=					3_7
           lowerCamelCase__   	=					'gelu'
           lowerCamelCase__   	=					0.1
           lowerCamelCase__   	=					0.1
           lowerCamelCase__   	=					5_1_2
           lowerCamelCase__   	=					1_6
           lowerCamelCase__   	=					2
           lowerCamelCase__   	=					0.02
           lowerCamelCase__   	=					3
           lowerCamelCase__   	=					4
           lowerCamelCase__   	=					None
           lowerCamelCase__   	=					self.vocab_size - 1




     def 			__snake_case (     self	: Tuple      )					->							Any:
           lowerCamelCase__   	=					ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size      )

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

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

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

           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,
               input_mask,
               head_mask,
               token_type_ids,
               mc_token_ids,
               sequence_labels,
               token_labels,
               choice_labels,
           )




     def 			__snake_case (     self	: Optional[Any]						,	A__	: List[str]						,	A__	: List[Any]						,	A__	: List[Any]						,	A__	: Tuple						,	A__	: Dict						,	*A__	: str      )					->							Dict:
           lowerCamelCase__   	=					TFOpenAIGPTModel(config=A__      )
           lowerCamelCase__   	=					{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           lowerCamelCase__   	=					model(A__      )

           lowerCamelCase__   	=					[input_ids, input_mask]
           lowerCamelCase__   	=					model(A__      )

           lowerCamelCase__   	=					model(A__      )

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




     def 			__snake_case (     self	: str						,	A__	: Optional[Any]						,	A__	: List[str]						,	A__	: Dict						,	A__	: Tuple						,	A__	: Any						,	*A__	: List[str]      )					->							List[Any]:
           lowerCamelCase__   	=					TFOpenAIGPTLMHeadModel(config=A__      )
           lowerCamelCase__   	=					{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
           lowerCamelCase__   	=					model(A__      )
           self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size)      )




     def 			__snake_case (     self	: str						,	A__	: Optional[int]						,	A__	: List[Any]						,	A__	: Tuple						,	A__	: List[str]						,	A__	: List[str]						,	A__	: Optional[int]						,	*A__	: List[Any]      )					->							Tuple:
           lowerCamelCase__   	=					TFOpenAIGPTDoubleHeadsModel(config=A__      )

           lowerCamelCase__   	=					tf.tile(tf.expand_dims(A__						,	1      )						,	(1, self.num_choices, 1)      )
           lowerCamelCase__   	=					tf.tile(tf.expand_dims(A__						,	1      )						,	(1, self.num_choices, 1)      )
           lowerCamelCase__   	=					tf.tile(tf.expand_dims(A__						,	1      )						,	(1, self.num_choices, 1)      )

           lowerCamelCase__   	=					{
               '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,
           }
           lowerCamelCase__   	=					model(A__      )
           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 			__snake_case (     self	: Optional[int]						,	A__	: str						,	A__	: List[str]						,	A__	: Dict						,	A__	: Optional[int]						,	A__	: Any						,	*A__	: Optional[Any]      )					->							Union[str, Any]:
           lowerCamelCase__   	=					self.num_labels
           lowerCamelCase__   	=					ids_tensor([self.batch_size]						,	self.type_sequence_label_size      )
           lowerCamelCase__   	=					{
               'input_ids': input_ids,
               'attention_mask': input_mask,
               'token_type_ids': token_type_ids,
               'labels': sequence_labels,
           }
           lowerCamelCase__   	=					TFOpenAIGPTForSequenceClassification(A__      )
           lowerCamelCase__   	=					model(A__      )
           self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.num_labels)      )




     def 			__snake_case (     self	: Dict      )					->							Any:
           lowerCamelCase__   	=					self.prepare_config_and_inputs()

           (
               (
               lowerCamelCase__
           )	,		(
               lowerCamelCase__
           )	,		(
               lowerCamelCase__
           )	,		(
               lowerCamelCase__
           )	,		(
               lowerCamelCase__
           )	,		(
               lowerCamelCase__
           )	,		(
               lowerCamelCase__
           )	,		(
               lowerCamelCase__
           )	,		(
               lowerCamelCase__
           )	,		
           )   	=					config_and_inputs

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






@require_tf
class       A      (		_lowerCAmelCase      ,    _lowerCAmelCase      ,    unittest.TestCase		):
     """simple docstring"""

     _snake_case:						Union[str, Any]  =(
         (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
         if is_tf_available()
         else ()
     )
     _snake_case:						str  =(
         (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     _snake_case:						List[Any]  =(
         {
             "feature-extraction": TFOpenAIGPTModel,
             "text-classification": TFOpenAIGPTForSequenceClassification,
             "text-generation": TFOpenAIGPTLMHeadModel,
             "zero-shot": TFOpenAIGPTForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     _snake_case:						Optional[int]  =False
     _snake_case:						Optional[Any]  =False




     def 			__snake_case (     self	: Union[str, Any]						,	A__	: Dict						,	A__	: Dict						,	A__	: Union[str, Any]						,	A__	: Optional[int]						,	A__	: Optional[Any]      )					->							List[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 			__snake_case (     self	: List[str]      )					->							Dict:
           lowerCamelCase__   	=					TFOpenAIGPTModelTester(self      )
           lowerCamelCase__   	=					ConfigTester(self						,	config_class=A__						,	n_embd=3_7      )




     def 			__snake_case (     self	: Tuple      )					->							Tuple:
           self.config_tester.run_common_tests()




     def 			__snake_case (     self	: Union[str, Any]      )					->							Optional[int]:
           lowerCamelCase__   	=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*A__      )




     def 			__snake_case (     self	: Union[str, Any]      )					->							Any:
           lowerCamelCase__   	=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_lm_head(*A__      )




     def 			__snake_case (     self	: Any      )					->							Optional[Any]:
           lowerCamelCase__   	=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_double_head(*A__      )




     def 			__snake_case (     self	: List[Any]      )					->							Optional[Any]:
           lowerCamelCase__   	=					self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*A__      )




     @slow
     def 			__snake_case (     self	: Tuple      )					->							Dict:
           for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCamelCase__   	=					TFOpenAIGPTModel.from_pretrained(A__      )
                 self.assertIsNotNone(A__      )






@require_tf
class       A      (		unittest.TestCase		):
     """simple docstring"""

     @slow
     def 			__snake_case (     self	: Any      )					->							Union[str, Any]:
           lowerCamelCase__   	=					TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'      )
           lowerCamelCase__   	=					tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]						,	dtype=tf.intaa      )  # the president is
           lowerCamelCase__   	=					[
               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

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





def       lowerCAmelCase_      (	lowerCamelCase:							str    )       ->      int:


      '''simple docstring'''




      lowerCamelCase__   	=					53_81
      for x in s:
            lowerCamelCase__   	=					((hash_value << 5) + hash_value) + ord(lowerCamelCase    )
      return hash_value & 0Xffff_ffff