




'''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        SCREAMING_SNAKE_CASE:

       def __init__(    self					,		lowerCamelCase__					,		)    ->					List[Any]:

           """simple docstring"""



           __lowercase							=						parent
           __lowercase							=						13
           __lowercase							=						7
           __lowercase							=						True
           __lowercase							=						True
           __lowercase							=						True
           __lowercase							=						True
           __lowercase							=						True
           __lowercase							=						99
           __lowercase							=						32
           __lowercase							=						2
           __lowercase							=						4
           __lowercase							=						37
           __lowercase							=						"""gelu"""
           __lowercase							=						0.1
           __lowercase							=						0.1
           __lowercase							=						512
           __lowercase							=						16
           __lowercase							=						2
           __lowercase							=						0.02
           __lowercase							=						3
           __lowercase							=						4
           __lowercase							=						None
           __lowercase							=						self.vocab_size - 1

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

           """simple docstring"""



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

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

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

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

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

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

           __lowercase							=						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					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		*lowerCamelCase__     )    ->					Any:

           """simple docstring"""



           __lowercase							=						TFOpenAIGPTModel(config=lowerCamelCase__     )
           __lowercase							=						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           __lowercase							=						model(lowerCamelCase__     )

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

           __lowercase							=						model(lowerCamelCase__     )

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

       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		*lowerCamelCase__     )    ->					str:

           """simple docstring"""



           __lowercase							=						TFOpenAIGPTLMHeadModel(config=lowerCamelCase__     )
           __lowercase							=						{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
           __lowercase							=						model(lowerCamelCase__     )
           self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.seq_length, self.vocab_size)     )

       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		*lowerCamelCase__     )    ->					List[Any]:

           """simple docstring"""



           __lowercase							=						TFOpenAIGPTDoubleHeadsModel(config=lowerCamelCase__     )

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

           __lowercase							=						{
               """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,
           }
           __lowercase							=						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     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		*lowerCamelCase__     )    ->					Optional[Any]:

           """simple docstring"""



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




       def     snake_case__			(    self     )    ->					List[str]:

           """simple docstring"""



           __lowercase							=						self.prepare_config_and_inputs()

           (
               (
               __lowercase
           )				,(
               __lowercase
           )				,(
               __lowercase
           )				,(
               __lowercase
           )				,(
               __lowercase
           )				,(
               __lowercase
           )				,(
               __lowercase
           )				,(
               __lowercase
           )				,(
               __lowercase
           )				,
           )							=						config_and_inputs

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






@require_tf
class        SCREAMING_SNAKE_CASE(				__A   ,      __A   ,      unittest.TestCase     ):
       snake_case_ :     int    =					(
           (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
           if is_tf_available()
           else ()
       )
       snake_case_ :     List[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_ :     Dict    =					False
       snake_case_ :     Any    =					False

       def     snake_case__			(    self					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__					,		lowerCamelCase__     )    ->					Union[str, Any]:

           """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     snake_case__			(    self     )    ->					int:

           """simple docstring"""



           __lowercase							=						TFOpenAIGPTModelTester(self     )
           __lowercase							=						ConfigTester(self					,		config_class=lowerCamelCase__					,		n_embd=37     )

       def     snake_case__			(    self     )    ->					Union[str, Any]:

           """simple docstring"""



           self.config_tester.run_common_tests()

       def     snake_case__			(    self     )    ->					int:

           """simple docstring"""



           __lowercase							=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_model(*lowerCamelCase__     )

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

           """simple docstring"""



           __lowercase							=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_lm_head(*lowerCamelCase__     )

       def     snake_case__			(    self     )    ->					int:

           """simple docstring"""



           __lowercase							=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_double_head(*lowerCamelCase__     )

       def     snake_case__			(    self     )    ->					str:

           """simple docstring"""



           __lowercase							=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCamelCase__     )




       @slow
       def     snake_case__			(    self     )    ->					List[str]:

           """simple docstring"""



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






@require_tf
class        SCREAMING_SNAKE_CASE(				unittest.TestCase     ):

       @slow
       def     snake_case__			(    self     )    ->					Any:

           """simple docstring"""



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

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










'''simple docstring'''
def 				snake_case_  (   a__		:	int			):



    """simple docstring"""




    if not isinstance(a__    ,a__			) or number < 0:
        raise ValueError("""Input must be a non-negative integer"""			)

    __lowercase							=						number & ~(number - 1)
    __lowercase							=						0
    while intermediate:
        intermediate >>= 1
        index += 1
    return index - 1


if __name__ == "__main__":
  import doctest

  doctest.testmod(verbose=True)




