

import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    OpenAIGPTConfig,
		    OpenAIGPTDoubleHeadsModel,
		    OpenAIGPTForSequenceClassification,
		    OpenAIGPTLMHeadModel,
		    OpenAIGPTModel,
		)







class 					__lowercase  :





							"""simple docstring"""

							def __init__(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase=13    ,						__UpperCAmelCase=7    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=99    ,						__UpperCAmelCase=32    ,						__UpperCAmelCase=5    ,						__UpperCAmelCase=4    ,						__UpperCAmelCase=37    ,						__UpperCAmelCase="gelu"    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=5_12    ,						__UpperCAmelCase=16    ,						__UpperCAmelCase=2    ,						__UpperCAmelCase=0.0_2    ,						__UpperCAmelCase=3    ,						__UpperCAmelCase=4    ,						__UpperCAmelCase=None    ,						)		->       Tuple:
														A      :					Dict      		=					parent
														A      :					int      		=					batch_size
														A      :					Optional[int]      		=					seq_length
														A      :					Optional[Any]      		=					is_training
														A      :					Dict      		=					use_token_type_ids
														A      :					Optional[Any]      		=					use_labels
														A      :					List[Any]      		=					vocab_size
														A      :					Tuple      		=					hidden_size
														A      :					Dict      		=					num_hidden_layers
														A      :					List[str]      		=					num_attention_heads
														A      :					Optional[Any]      		=					intermediate_size
														A      :					Any      		=					hidden_act
														A      :					Optional[Any]      		=					hidden_dropout_prob
														A      :					Optional[int]      		=					attention_probs_dropout_prob
														A      :					Union[str, Any]      		=					max_position_embeddings
														A      :					List[str]      		=					type_vocab_size
														A      :					Tuple      		=					type_sequence_label_size
														A      :					Optional[int]      		=					initializer_range
														A      :					int      		=					num_labels
														A      :					Tuple      		=					num_choices
														A      :					Any      		=					scope
														A      :					Any      		=					self.vocab_size - 1




							def       snake_case			(					self						)		->       Optional[int]:
														A      :					int      		=					ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size						)

														A      :					Optional[Any]      		=					None
														if self.use_token_type_ids:
																					A      :					str      		=					ids_tensor([self.batch_size, self.seq_length]    ,						self.type_vocab_size						)

														A      :					int      		=					None
														A      :					Dict      		=					None
														A      :					Tuple      		=					None
														if self.use_labels:
																					A      :					Any      		=					ids_tensor([self.batch_size]    ,						self.type_sequence_label_size						)
																					A      :					str      		=					ids_tensor([self.batch_size, self.seq_length]    ,						self.num_labels						)
																					A      :					List[Any]      		=					ids_tensor([self.batch_size]    ,						self.num_choices						)

														A      :					int      		=					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    ,						)

														A      :					int      		=					ids_tensor([self.num_hidden_layers, self.num_attention_heads]    ,						2						)

														return (
														    config,
														    input_ids,
														    head_mask,
														    token_type_ids,
														    sequence_labels,
														    token_labels,
														    choice_labels,
														)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						*__UpperCAmelCase						)		->       Dict:
														A      :					List[str]      		=					OpenAIGPTModel(config=__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()

														A      :					Optional[Any]      		=					model(__UpperCAmelCase    ,						token_type_ids=__UpperCAmelCase    ,						head_mask=__UpperCAmelCase						)
														A      :					Union[str, Any]      		=					model(__UpperCAmelCase    ,						token_type_ids=__UpperCAmelCase						)
														A      :					Tuple      		=					model(__UpperCAmelCase						)

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




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						*__UpperCAmelCase						)		->       Optional[int]:
														A      :					Optional[int]      		=					OpenAIGPTLMHeadModel(__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()

														A      :					Union[str, Any]      		=					model(__UpperCAmelCase    ,						token_type_ids=__UpperCAmelCase    ,						labels=__UpperCAmelCase						)
														self.parent.assertEqual(result.loss.shape    ,						()						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						*__UpperCAmelCase						)		->       Any:
														A      :					int      		=					OpenAIGPTDoubleHeadsModel(__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()

														A      :					List[str]      		=					model(__UpperCAmelCase    ,						token_type_ids=__UpperCAmelCase    ,						labels=__UpperCAmelCase						)
														self.parent.assertEqual(result.loss.shape    ,						()						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						*__UpperCAmelCase						)		->       Dict:
														A      :					Optional[Any]      		=					self.num_labels
														A      :					Optional[Any]      		=					OpenAIGPTForSequenceClassification(__UpperCAmelCase						)
														model.to(__UpperCAmelCase						)
														model.eval()

														A      :					Tuple      		=					ids_tensor([self.batch_size]    ,						self.type_sequence_label_size						)
														A      :					Tuple      		=					model(__UpperCAmelCase    ,						token_type_ids=__UpperCAmelCase    ,						labels=__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)						)






							def       snake_case			(					self						)		->       Optional[int]:
														A      :					List[str]      		=					self.prepare_config_and_inputs()
														(
														    (
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		
														)      :					Tuple      		=					config_and_inputs
														A      :					List[Any]      		=					{
														    '''input_ids''': input_ids,
														    '''token_type_ids''': token_type_ids,
														    '''head_mask''': head_mask,
														}

														return config, inputs_dict







@require_torch
class 					__lowercase  (						_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE		,       unittest.TestCase				):





							"""simple docstring"""

							UpperCAmelCase_						:		Dict								=	(
							    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
							    if is_torch_available()
							    else ()
							)
							UpperCAmelCase_						:		Optional[int]								=	(
							    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
							)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
							UpperCAmelCase_						:		Union[str, Any]								=	(
							    {
							        '''feature-extraction''': OpenAIGPTModel,
							        '''text-classification''': OpenAIGPTForSequenceClassification,
							        '''text-generation''': OpenAIGPTLMHeadModel,
							        '''zero-shot''': OpenAIGPTForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase						)		->       Optional[Any]:
														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    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase=False						)		->       List[str]:
														A      :					Union[str, Any]      		=					super()._prepare_for_class(__UpperCAmelCase    ,						__UpperCAmelCase    ,						return_labels=__UpperCAmelCase						)

														if return_labels:
																					if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
																												A      :					Dict      		=					torch.zeros(
																												    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)    ,						dtype=torch.long    ,						device=__UpperCAmelCase    ,						)
																												A      :					Tuple      		=					inputs_dict['''labels''']
																												A      :					Dict      		=					inputs_dict['''labels''']
																												A      :					List[str]      		=					torch.zeros(
																												    (self.model_tester.batch_size, self.model_tester.num_choices)    ,						dtype=torch.long    ,						device=__UpperCAmelCase    ,						)
																												A      :					str      		=					torch.zeros(
																												    self.model_tester.batch_size    ,						dtype=torch.long    ,						device=__UpperCAmelCase						)
														return inputs_dict




							def       snake_case			(					self						)		->       Any:
														A      :					Any      		=					OpenAIGPTModelTester(self						)
														A      :					List[Any]      		=					ConfigTester(self    ,						config_class=__UpperCAmelCase    ,						n_embd=37						)




							def       snake_case			(					self						)		->       Any:
														self.config_tester.run_common_tests()




							def       snake_case			(					self						)		->       Dict:
														A      :					Tuple      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_openai_gpt_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       List[str]:
														A      :					Union[str, Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_lm_head_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       List[str]:
														A      :					Optional[int]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_double_lm_head_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Any:
														A      :					List[str]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__UpperCAmelCase						)






							@slow
							def       snake_case			(					self						)		->       List[Any]:
														for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					A      :					List[Any]      		=					OpenAIGPTModel.from_pretrained(__UpperCAmelCase						)
																					self.assertIsNotNone(__UpperCAmelCase						)







@require_torch
class 					__lowercase  (						unittest.TestCase				):





							"""simple docstring"""

							@slow
							def       snake_case			(					self						)		->       Optional[int]:
														A      :					Optional[Any]      		=					OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''						)
														model.to(__UpperCAmelCase						)
														A      :					Any      		=					torch.tensor([[4_81, 47_35, 5_44]]    ,						dtype=torch.long    ,						device=__UpperCAmelCase						)  # the president is
														A      :					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

														A      :					str      		=					model.generate(__UpperCAmelCase    ,						do_sample=__UpperCAmelCase						)
														self.assertListEqual(output_ids[0].tolist()    ,						__UpperCAmelCase						)









from sympy import diff, lambdify, symbols
from sympy.functions import *  # noqa: F403






def        snake_case__		(    lowerCamelCase_     ,				lowerCamelCase_     ,				lowerCamelCase_ = "x"     ,				lowerCamelCase_ = 10**-10     ,				lowerCamelCase_ = 1     ,				):
							A      :					int      		=					symbols(lowerCamelCase_		)
							A      :					List[str]      		=					lambdify(lowerCamelCase_     ,				lowerCamelCase_		)
							A      :					Tuple      		=					lambdify(lowerCamelCase_     ,				diff(lowerCamelCase_     ,				lowerCamelCase_		)		)

							A      :					Optional[int]      		=					starting_point

							while True:
														if diff_function(lowerCamelCase_		) != 0:
																					A      :					Union[str, Any]      		=					prev_guess - multiplicity * func(lowerCamelCase_		) / diff_function(
																					    lowerCamelCase_		)
														else:
																					raise ZeroDivisionError('''Could not find root'''		) from None

														# Precision is checked by comparing the difference of consecutive guesses
														if abs(next_guess - prev_guess		) < precision:
																					return next_guess

														A      :					Tuple      		=					next_guess


# Let's Execute
if __name__ == "__main__":
		# Find root of trigonometric function
		# Find value of pi
		print(F"The root of sin(x) = 0 is {newton_raphson('sin(x)', 2)}")

		# Find root of polynomial
		# Find fourth Root of 5
		print(F"The root of x**4 - 5 = 0 is {newton_raphson('x**4 -5', 0.4 +5J)}")

		# Find value of e
		print(
		    "The root of log(y) - 1 = 0 is ",
		    F"{newton_raphson('log(y) - 1', 2, variable='y')}",
		)

		# Exponential Roots
		print(
		    "The root of exp(x) - 1 = 0 is",
		    F"{newton_raphson('exp(x) - 1', 10, precision=0.0_05)}",
		)

		# Find root of cos(x)
		print(F"The root of cos(x) = 0 is {newton_raphson('cos(x)', 0)}")






