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





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





 def     snake_case__						(      self):
     snake_case_	:      int            =  ids_tensor([self.batch_size, self.seq_length]	,    self.vocab_size)

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

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

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

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

     snake_case_	:      str            =  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_	:      int            =  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	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    *lowercase_):
     snake_case_	:      Optional[Any]            =  TFOpenAIGPTModel(config=lowercase_)
     snake_case_	:      Optional[int]            =  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
     snake_case_	:      Optional[Any]            =  model(lowercase_)

     snake_case_	:      List[Any]            =  [input_ids, input_mask]
     snake_case_	:      str            =  model(lowercase_)

     snake_case_	:      Optional[Any]            =  model(lowercase_)

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





 def     snake_case__						(      self	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    *lowercase_):
     snake_case_	:      Dict            =  TFOpenAIGPTLMHeadModel(config=lowercase_)
     snake_case_	:      Tuple            =  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
     snake_case_	:      int            =  model(lowercase_)
     self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.seq_length, self.vocab_size))





 def     snake_case__						(      self	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    *lowercase_):
     snake_case_	:      List[str]            =  TFOpenAIGPTDoubleHeadsModel(config=lowercase_)

     snake_case_	:      Union[str, Any]            =  tf.tile(tf.expand_dims(lowercase_	,    1)	,    (1, self.num_choices, 1))
     snake_case_	:      Union[str, Any]            =  tf.tile(tf.expand_dims(lowercase_	,    1)	,    (1, self.num_choices, 1))
     snake_case_	:      Optional[Any]            =  tf.tile(tf.expand_dims(lowercase_	,    1)	,    (1, self.num_choices, 1))

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





 def     snake_case__						(      self):
     snake_case_	:      Tuple            =  self.prepare_config_and_inputs()

     (
         (
         snake_case_
     )     ,	(
         snake_case_
     )     ,	(
         snake_case_
     )     ,	(
         snake_case_
     )     ,	(
         snake_case_
     )     ,	(
         snake_case_
     )     ,	(
         snake_case_
     )     ,	(
         snake_case_
     )     ,	(
         snake_case_
     )     ,	
     )	:      int            =  config_and_inputs

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






@require_tf
class 				UpperCAmelCase_			(		snake_case__      ,  snake_case__      ,  unittest.TestCase   ):
 UpperCAmelCase_						      =  (
     (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
     if is_tf_available()
     else ()
 )
 UpperCAmelCase_						      =  (
     (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 UpperCAmelCase_						      =  (
     {
         """feature-extraction""": TFOpenAIGPTModel,
         """text-classification""": TFOpenAIGPTForSequenceClassification,
         """text-generation""": TFOpenAIGPTLMHeadModel,
         """zero-shot""": TFOpenAIGPTForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 UpperCAmelCase_						      =  False
 UpperCAmelCase_						      =  False





 def     snake_case__						(      self	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_	,    lowercase_):
     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):
     snake_case_	:      int            =  TFOpenAIGPTModelTester(self)
     snake_case_	:      Optional[Any]            =  ConfigTester(self	,    config_class=lowercase_	,    n_embd=37)





 def     snake_case__						(      self):
     self.config_tester.run_common_tests()





 def     snake_case__						(      self):
     snake_case_	:      str            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_model(*lowercase_)





 def     snake_case__						(      self):
     snake_case_	:      List[str]            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_lm_head(*lowercase_)





 def     snake_case__						(      self):
     snake_case_	:      Union[str, Any]            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_double_head(*lowercase_)





 def     snake_case__						(      self):
     snake_case_	:      str            =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowercase_)





 @slow
 def     snake_case__						(      self):
     for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case_	:      Union[str, Any]            =  TFOpenAIGPTModel.from_pretrained(lowercase_)
         self.assertIsNotNone(lowercase_)






@require_tf
class 				UpperCAmelCase_			(		unittest.TestCase   ):





 @slow
 def     snake_case__						(      self):
     snake_case_	:      str            =  TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt")
     snake_case_	:      Optional[Any]            =  tf.convert_to_tensor([[4_81, 47_35, 5_44]]	,    dtype=tf.intaa)  # the president is
     snake_case_	:      Dict            =  [
         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(lowercase_	,    do_sample=lowercase_)
     self.assertListEqual(output_ids[0].numpy().tolist()	,    lowercase_)






'''simple docstring'''
from math import exp

def   UpperCamelCase_       (		__SCREAMING_SNAKE_CASE    ):
    """simple docstring"""






    return 8 * x - 2 * exp(-x    )

def   UpperCamelCase_       (		__SCREAMING_SNAKE_CASE,						__SCREAMING_SNAKE_CASE,						__SCREAMING_SNAKE_CASE    ):
    """simple docstring"""






    snake_case_	:      Optional[Any]            =  lower_bound
    snake_case_	:      int            =  upper_bound
    for _ in range(0,						__SCREAMING_SNAKE_CASE    ):
        snake_case_     ,	snake_case_	:      List[str]            =  xa, xa - (f(__SCREAMING_SNAKE_CASE    ) * (xa - xa)) / (f(__SCREAMING_SNAKE_CASE    ) - f(__SCREAMING_SNAKE_CASE    ))
    return xa


if __name__ == "__main__":
  print(f"""Example: {secant_method(1, 3, 2)}""")




