

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





       '''simple docstring'''




       def __init__(						self       :						int    ,		lowerCAmelCase__       :						List[str]    ,		)			->    Dict:
             snake_case__					=   parent
             snake_case__					=   13
             snake_case__					=   7
             snake_case__					=   True
             snake_case__					=   True
             snake_case__					=   True
             snake_case__					=   True
             snake_case__					=   True
             snake_case__					=   99
             snake_case__					=   32
             snake_case__					=   2
             snake_case__					=   4
             snake_case__					=   37
             snake_case__					=   """gelu"""
             snake_case__					=   0.1
             snake_case__					=   0.1
             snake_case__					=   512
             snake_case__					=   16
             snake_case__					=   2
             snake_case__					=   0.02
             snake_case__					=   3
             snake_case__					=   4
             snake_case__					=   None
             snake_case__					=   self.vocab_size - 1

       def UpperCAmelCase_						(						self       :						Any							)			->    Optional[Any]:
             snake_case__					=   ids_tensor([self.batch_size, self.seq_length]    ,		self.vocab_size							)

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

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

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

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

             snake_case__					=   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__					=   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 UpperCAmelCase_						(						self       :						Tuple    ,		lowerCAmelCase__       :						Union[str, Any]    ,		lowerCAmelCase__       :						Optional[Any]    ,		lowerCAmelCase__       :						Tuple    ,		lowerCAmelCase__       :						str    ,		lowerCAmelCase__       :						Union[str, Any]    ,		*lowerCAmelCase__       :						int							)			->    Dict:
             snake_case__					=   TFOpenAIGPTModel(config=_lowerCamelCase							)
             snake_case__					=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
             snake_case__					=   model(_lowerCamelCase							)

             snake_case__					=   [input_ids, input_mask]
             snake_case__					=   model(_lowerCamelCase							)

             snake_case__					=   model(_lowerCamelCase							)

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

       def UpperCAmelCase_						(						self       :						Tuple    ,		lowerCAmelCase__       :						List[Any]    ,		lowerCAmelCase__       :						str    ,		lowerCAmelCase__       :						int    ,		lowerCAmelCase__       :						Optional[int]    ,		lowerCAmelCase__       :						Union[str, Any]    ,		*lowerCAmelCase__       :						Union[str, Any]							)			->    List[str]:
             snake_case__					=   TFOpenAIGPTLMHeadModel(config=_lowerCamelCase							)
             snake_case__					=   {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
             snake_case__					=   model(_lowerCamelCase							)
             self.parent.assertEqual(result.logits.shape    ,		(self.batch_size, self.seq_length, self.vocab_size)							)

       def UpperCAmelCase_						(						self       :						List[str]    ,		lowerCAmelCase__       :						int    ,		lowerCAmelCase__       :						Any    ,		lowerCAmelCase__       :						Optional[int]    ,		lowerCAmelCase__       :						str    ,		lowerCAmelCase__       :						int    ,		lowerCAmelCase__       :						str    ,		*lowerCAmelCase__       :						str							)			->    Union[str, Any]:
             snake_case__					=   TFOpenAIGPTDoubleHeadsModel(config=_lowerCamelCase							)

             snake_case__					=   tf.tile(tf.expand_dims(_lowerCamelCase    ,		1							)    ,		(1, self.num_choices, 1)							)
             snake_case__					=   tf.tile(tf.expand_dims(_lowerCamelCase    ,		1							)    ,		(1, self.num_choices, 1)							)
             snake_case__					=   tf.tile(tf.expand_dims(_lowerCamelCase    ,		1							)    ,		(1, self.num_choices, 1)							)

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

       def UpperCAmelCase_						(						self       :						Any							)			->    str:
             snake_case__					=   self.prepare_config_and_inputs()

             (
                 snake_case__		
             )					=   config_and_inputs

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



@require_tf
class     SCREAMING_SNAKE_CASE__     (						lowerCAmelCase__ ,	lowerCAmelCase__ ,	unittest.TestCase		):





       '''simple docstring'''




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

       def UpperCAmelCase_						(						self       :						Optional[int]    ,		lowerCAmelCase__       :						Optional[int]    ,		lowerCAmelCase__       :						int    ,		lowerCAmelCase__       :						Optional[int]    ,		lowerCAmelCase__       :						int    ,		lowerCAmelCase__       :						List[str]							)			->    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 UpperCAmelCase_						(						self       :						Dict							)			->    Union[str, Any]:
             snake_case__					=   TFOpenAIGPTModelTester(self							)
             snake_case__					=   ConfigTester(self    ,		config_class=_lowerCamelCase    ,		n_embd=37							)

       def UpperCAmelCase_						(						self       :						Union[str, Any]							)			->    Union[str, Any]:
             self.config_tester.run_common_tests()

       def UpperCAmelCase_						(						self       :						Dict							)			->    Optional[Any]:
             snake_case__					=   self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_model(*_lowerCamelCase							)

       def UpperCAmelCase_						(						self       :						Any							)			->    Tuple:
             snake_case__					=   self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_lm_head(*_lowerCamelCase							)

       def UpperCAmelCase_						(						self       :						int							)			->    List[str]:
             snake_case__					=   self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_double_head(*_lowerCamelCase							)

       def UpperCAmelCase_						(						self       :						Tuple							)			->    Optional[int]:
             snake_case__					=   self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*_lowerCamelCase							)

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

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



@require_tf
class     SCREAMING_SNAKE_CASE__     (						unittest.TestCase		):





       '''simple docstring'''




       @slow
       def UpperCAmelCase_						(						self       :						str							)			->    Union[str, Any]:
             snake_case__					=   TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""							)
             snake_case__					=   tf.convert_to_tensor([[481, 4735, 544]]    ,		dtype=tf.intaa							)  # the president is
             snake_case__					=   [
                 481,
                 4735,
                 544,
                 246,
                 963,
                 870,
                 762,
                 239,
                 244,
                 40477,
                 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

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









lowerCAmelCase						:   Optional[Any]										=    1000





def 			_lowercase    (      __UpperCamelCase     :		float ):
      return x * x





def 			_lowercase    (      __UpperCamelCase     :		int   ,       __UpperCamelCase     :		float   ,       __UpperCamelCase     :		float   ,       __UpperCamelCase     :		int = 4 ):
      assert callable(
          __UpperCamelCase ), F'''the function(object) passed should be callable your input : {function}'''
      assert isinstance(__UpperCamelCase   ,       (float, int) ), F'''a should be float or integer your input : {a}'''
      assert isinstance(function(__UpperCamelCase )   ,       (float, int) ), (
          "the function should return integer or float return type of your function, "
          F'''{type(__UpperCamelCase )}'''
      )
      assert isinstance(__UpperCamelCase   ,       (float, int) ), F'''b should be float or integer your input : {b}'''
      assert (
          isinstance(__UpperCamelCase   ,       __UpperCamelCase ) and precision > 0
      ), F'''precision should be positive integer your input : {precision}'''

      # just applying the formula of simpson for approximate integration written in
      # mentioned article in first comment of this file and above this function

      snake_case__					=   (b - a) / N_STEPS
      snake_case__					=   function(__UpperCamelCase ) + function(__UpperCamelCase )

      for i in range(1   ,       __UpperCamelCase ):
            snake_case__					=   a + h * i
            result += function(__UpperCamelCase ) * (4 if i % 2 else 2)

      result *= h / 3
      return round(__UpperCamelCase   ,       __UpperCamelCase )


if __name__ == "__main__":
       import doctest

       doctest.testmod()






