


import unittest

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

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    ErnieMForInformationExtraction,
	    ErnieMForMultipleChoice,
	    ErnieMForQuestionAnswering,
	    ErnieMForSequenceClassification,
	    ErnieMForTokenClassification,
	    ErnieMModel,
	)
	from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST





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=512			,    _UpperCAmelCase=16			,    _UpperCAmelCase=2			,    _UpperCAmelCase=0.0_2			,    _UpperCAmelCase=3			,    _UpperCAmelCase=4			,    _UpperCAmelCase=None			,    ):







													"""simple docstring"""


													a_					=		parent
													a_					=		batch_size
													a_					=		seq_length
													a_					=		is_training
													a_					=		use_input_mask
													a_					=		use_labels
													a_					=		vocab_size
													a_					=		hidden_size
													a_					=		num_hidden_layers
													a_					=		num_attention_heads
													a_					=		intermediate_size
													a_					=		hidden_act
													a_					=		hidden_dropout_prob
													a_					=		attention_probs_dropout_prob
													a_					=		max_position_embeddings
													a_					=		type_vocab_size
													a_					=		type_sequence_label_size
													a_					=		initializer_range
													a_					=		num_labels
													a_					=		num_choices
													a_					=		scope


							def 						lowercase__     (   self		):







													"""simple docstring"""


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

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

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

													a_					=		self.get_config()

													return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels


							def 						lowercase__     (   self		):







													"""simple docstring"""


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

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

													return config, input_ids, input_mask


							def 						lowercase__     (   self		):







													"""simple docstring"""


													return ErnieMConfig(
													    vocab_size=self.vocab_size			,    hidden_size=self.hidden_size			,    num_hidden_layers=self.num_hidden_layers			,    num_attention_heads=self.num_attention_heads			,    intermediate_size=self.intermediate_size			,    hidden_act=self.hidden_act			,    hidden_dropout_prob=self.hidden_dropout_prob			,    attention_probs_dropout_prob=self.attention_probs_dropout_prob			,    max_position_embeddings=self.max_position_embeddings			,    type_vocab_size=self.type_vocab_size			,    initializer_range=self.initializer_range			,    )


							def 						lowercase__     (   self			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase		):







													"""simple docstring"""


													a_					=		ErnieMModel(config=_UpperCAmelCase		)
													model.to(_UpperCAmelCase		)
													model.eval()
													a_					=		model(_UpperCAmelCase			,    return_dict=_UpperCAmelCase		)
													self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, self.seq_length, self.hidden_size)		)


							def 						lowercase__     (   self			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase		):







													"""simple docstring"""


													a_					=		ErnieMForQuestionAnswering(config=_UpperCAmelCase		)
													model.to(_UpperCAmelCase		)
													model.eval()
													a_					=		model(
													    _UpperCAmelCase			,    attention_mask=_UpperCAmelCase			,    start_positions=_UpperCAmelCase			,    end_positions=_UpperCAmelCase			,    )
													self.parent.assertEqual(result.start_logits.shape			,    (self.batch_size, self.seq_length)		)
													self.parent.assertEqual(result.end_logits.shape			,    (self.batch_size, self.seq_length)		)


							def 						lowercase__     (   self			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase		):







													"""simple docstring"""


													a_					=		ErnieMForInformationExtraction(config=_UpperCAmelCase		)
													model.to(_UpperCAmelCase		)
													model.eval()
													a_					=		torch.ones_like(_UpperCAmelCase			,    dtype=torch.floataa		)
													a_					=		model(
													    _UpperCAmelCase			,    attention_mask=_UpperCAmelCase			,    start_positions=_UpperCAmelCase			,    end_positions=_UpperCAmelCase			,    )
													self.parent.assertEqual(result.start_logits.shape			,    (self.batch_size, self.seq_length)		)
													self.parent.assertEqual(result.end_logits.shape			,    (self.batch_size, self.seq_length)		)


							def 						lowercase__     (   self			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase		):







													"""simple docstring"""


													a_					=		self.num_labels
													a_					=		ErnieMForSequenceClassification(_UpperCAmelCase		)
													model.to(_UpperCAmelCase		)
													model.eval()
													a_					=		model(_UpperCAmelCase			,    attention_mask=_UpperCAmelCase			,    labels=_UpperCAmelCase		)
													self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_labels)		)


							def 						lowercase__     (   self			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase		):







													"""simple docstring"""


													a_					=		self.num_labels
													a_					=		ErnieMForTokenClassification(config=_UpperCAmelCase		)
													model.to(_UpperCAmelCase		)
													model.eval()
													input_ids.to(_UpperCAmelCase		)
													input_mask.to(_UpperCAmelCase		)
													token_labels.to(_UpperCAmelCase		)

													a_					=		model(_UpperCAmelCase			,    attention_mask=_UpperCAmelCase			,    labels=_UpperCAmelCase		)

													self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.num_labels)		)


							def 						lowercase__     (   self			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase		):







													"""simple docstring"""


													a_					=		self.num_choices
													a_					=		ErnieMForMultipleChoice(config=_UpperCAmelCase		)
													model.to(_UpperCAmelCase		)
													model.eval()
													a_					=		input_ids.unsqueeze(1		).expand(-1			,    self.num_choices			,    -1		).contiguous()
													a_					=		input_mask.unsqueeze(1		).expand(-1			,    self.num_choices			,    -1		).contiguous()
													a_					=		model(
													    _UpperCAmelCase			,    attention_mask=_UpperCAmelCase			,    labels=_UpperCAmelCase			,    )
													self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_choices)		)






							def 						lowercase__     (   self		):







													"""simple docstring"""


													a_					=		self.prepare_config_and_inputs()
													(
													    (
													    a_
													)			,   (
													    a_
													)			,   (
													    a_
													)			,   (
													    a_
													)			,   (
													    a_
													)			,   (
													    a_
													)			,   
													)					=		config_and_inputs
													a_					=		{"""input_ids""": input_ids, """attention_mask""": input_mask}
													return config, inputs_dict





@require_torch
class        lowercase_				( UpperCamelCase__			,UpperCamelCase__			,unittest.TestCase):






							"""simple docstring"""







							snake_case_        =		(
							    (
							        ErnieMModel,
							        ErnieMForMultipleChoice,
							        ErnieMForQuestionAnswering,
							        ErnieMForSequenceClassification,
							        ErnieMForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							snake_case_        =		()
							snake_case_        =		(
							    {
							        '''feature-extraction''': ErnieMModel,
							        '''question-answering''': ErnieMForQuestionAnswering,
							        '''text-classification''': ErnieMForSequenceClassification,
							        '''token-classification''': ErnieMForTokenClassification,
							        '''zero-shot''': ErnieMForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							snake_case_        =		False


							def 						lowercase__     (   self			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase			,    _UpperCAmelCase		):







													"""simple docstring"""


													if pipeline_test_casse_name == "QAPipelineTests":
																			return True

													return False


							def 						lowercase__     (   self		):







													"""simple docstring"""


													a_					=		ErnieMModelTester(self		)
													a_					=		ConfigTester(self			,    config_class=_UpperCAmelCase			,    hidden_size=37		)


							def 						lowercase__     (   self		):







													"""simple docstring"""


													self.config_tester.run_common_tests()


							def 						lowercase__     (   self		):







													"""simple docstring"""


													a_					=		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_UpperCAmelCase		)


							def 						lowercase__     (   self		):







													"""simple docstring"""


													a_					=		self.model_tester.prepare_config_and_inputs()
													for type in ["absolute", "relative_key", "relative_key_query"]:
																			a_					=		type
																			self.model_tester.create_and_check_model(*_UpperCAmelCase		)


							def 						lowercase__     (   self		):







													"""simple docstring"""


													a_					=		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase		)


							def 						lowercase__     (   self		):







													"""simple docstring"""


													a_					=		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase		)


							def 						lowercase__     (   self		):







													"""simple docstring"""


													a_					=		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase		)


							def 						lowercase__     (   self		):







													"""simple docstring"""


													a_					=		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_information_extraction(*_UpperCAmelCase		)


							def 						lowercase__     (   self		):







													"""simple docstring"""


													a_					=		self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase		)






							@slow
							def 						lowercase__     (   self		):







													"""simple docstring"""


													for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			a_					=		ErnieMModel.from_pretrained(_UpperCAmelCase		)
																			self.assertIsNotNone(_UpperCAmelCase		)





@require_torch
class        lowercase_				( unittest.TestCase):






							"""simple docstring"""







							@slow
							def 						lowercase__     (   self		):







													"""simple docstring"""


													a_					=		ErnieMModel.from_pretrained("""susnato/ernie-m-base_pytorch"""		)
													model.eval()
													a_					=		torch.tensor([[0, 1, 2, 3, 4, 5]]		)
													a_					=		model(_UpperCAmelCase		)[0]

													# TODO Replace vocab size
													a_					=		768

													a_					=		torch.Size((1, 6, hidden_size)		)
													self.assertEqual(output.shape			,    _UpperCAmelCase		)

													a_					=		torch.tensor(
													    [[[-0.0_0_1_2, 0.1_2_4_5, -0.0_2_1_4], [-0.0_7_4_2, 0.0_2_4_4, -0.0_7_7_1], [-0.0_3_3_3, 0.1_1_6_4, -0.1_5_5_4]]]		)

													self.assertTrue(torch.allclose(output[:, :3, :3]			,    _UpperCAmelCase			,    atol=1e-3		)		)



from collections.abc import Callable

A_    :					Any             =Callable[[float], float]  # type alias for a real -> real function




def   lowerCamelCase_       (					UpperCAmelCase__       ,    UpperCAmelCase__       ,    UpperCAmelCase__       ,    ):



						"""simple docstring"""




						a_					=		float(UpperCAmelCase__      )
						while True:
												try:
																		a_					=		prev_guess - function(UpperCAmelCase__      ) / derivative(UpperCAmelCase__      )
												except ZeroDivisionError:
																		raise ZeroDivisionError("""Could not find root"""      ) from None
												if abs(prev_guess - next_guess      ) < 10**-5:
																		return next_guess
												a_					=		next_guess




def   lowerCamelCase_       (					UpperCAmelCase__      ):



						"""simple docstring"""




						return (x**3) - (2 * x) - 5




def   lowerCamelCase_       (					UpperCAmelCase__      ):



						"""simple docstring"""




						return 3 * (x**2) - 2


if __name__ == "__main__":
	print(newton(f, fa, 3))