
import unittest

from transformers import EsmConfig, is_torch_available
from transformers.testing_utils import TestCasePlus, 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 EsmForMaskedLM, EsmForSequenceClassification, EsmForTokenClassification, EsmModel
							from transformers.models.esm.modeling_esm import (
							    ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
							    EsmEmbeddings,
							    create_position_ids_from_input_ids,
							)







class        lowercase	:




							def __init__(       self     ,A__     ,A__=1_3     ,A__=7     ,A__=False     ,A__=True     ,A__=False     ,A__=True     ,A__=3_3     ,A__=3_2     ,A__=5     ,A__=4     ,A__=3_7     ,A__="gelu"     ,A__=0.1     ,A__=0.1     ,A__=5_1_2     ,A__=1_6     ,A__=2     ,A__=0.02     ,A__=3     ,A__=4     ,A__=None     ,):
												lowercase					=		parent
												lowercase					=		batch_size
												lowercase					=		seq_length
												lowercase					=		is_training
												lowercase					=		use_input_mask
												lowercase					=		use_token_type_ids
												lowercase					=		use_labels
												lowercase					=		vocab_size
												lowercase					=		hidden_size
												lowercase					=		num_hidden_layers
												lowercase					=		num_attention_heads
												lowercase					=		intermediate_size
												lowercase					=		hidden_act
												lowercase					=		hidden_dropout_prob
												lowercase					=		attention_probs_dropout_prob
												lowercase					=		max_position_embeddings
												lowercase					=		type_vocab_size
												lowercase					=		type_sequence_label_size
												lowercase					=		initializer_range
												lowercase					=		num_labels
												lowercase					=		num_choices
												lowercase					=		scope




							def 				A__   (       self):
												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
												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					=		self.get_config()

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




							def 				A__   (       self):
												return EsmConfig(
												    vocab_size=self.vocab_size     ,hidden_size=self.hidden_size     ,pad_token_id=1     ,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 				A__   (       self     ,A__     ,A__     ,A__     ,A__     ,A__     ,A__):
												lowercase					=		EsmModel(config=A__)
												model.to(A__)
												model.eval()
												lowercase					=		model(A__     ,attention_mask=A__)
												lowercase					=		model(A__)
												lowercase					=		model(A__)

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




							def 				A__   (       self     ,A__     ,A__     ,A__     ,A__     ,A__     ,A__):
												lowercase					=		EsmForMaskedLM(config=A__)
												model.to(A__)
												model.eval()
												lowercase					=		model(A__     ,attention_mask=A__     ,labels=A__)
												self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.vocab_size))




							def 				A__   (       self     ,A__     ,A__     ,A__     ,A__     ,A__     ,A__):
												lowercase					=		self.num_labels
												lowercase					=		EsmForTokenClassification(config=A__)
												model.to(A__)
												model.eval()
												lowercase					=		model(A__     ,attention_mask=A__     ,labels=A__)
												self.parent.assertEqual(result.logits.shape     ,(self.batch_size, self.seq_length, self.num_labels))





							def 				A__   (       self):
												lowercase					=		self.prepare_config_and_inputs()
												(
												    (
												    lowercase		
												)			,		(
												    lowercase		
												)			,		(
												    lowercase		
												)			,		(
												    lowercase		
												)			,		(
												    lowercase		
												)			,		(
												    lowercase		
												)			,				
												)					=		config_and_inputs
												lowercase					=		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
												return config, inputs_dict







@require_torch
class        lowercase	(      SCREAMING_SNAKE_CASE__ ,						SCREAMING_SNAKE_CASE__ ,						unittest.TestCase	):
							lowercase_ : Any       =False

							lowercase_ : str       =(
							    (
							        EsmForMaskedLM,
							        EsmModel,
							        EsmForSequenceClassification,
							        EsmForTokenClassification,
							    )
							    if is_torch_available()
							    else ()
							)
							lowercase_ : List[Any]       =()
							lowercase_ : Optional[Any]       =(
							    {
							        '''feature-extraction''': EsmModel,
							        '''fill-mask''': EsmForMaskedLM,
							        '''text-classification''': EsmForSequenceClassification,
							        '''token-classification''': EsmForTokenClassification,
							        '''zero-shot''': EsmForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							lowercase_ : int       =True




							def 				A__   (       self):
												lowercase					=		EsmModelTester(self)
												lowercase					=		ConfigTester(self     ,config_class=A__     ,hidden_size=3_7)




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




							def 				A__   (       self):
												lowercase					=		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*A__)




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




							def 				A__   (       self):
												lowercase					=		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_lm(*A__)




							def 				A__   (       self):
												lowercase					=		self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_token_classification(*A__)




							@slow
							def 				A__   (       self):
												for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	lowercase					=		EsmModel.from_pretrained(A__)
																	self.assertIsNotNone(A__)




							def 				A__   (       self):
												lowercase					=		self.model_tester.prepare_config_and_inputs()[0]
												lowercase					=		EsmEmbeddings(config=A__)

												lowercase					=		torch.as_tensor([[1_2, 3_1, 1_3, model.padding_idx]])
												lowercase					=		torch.as_tensor(
												    [
												        [
												            0 + model.padding_idx + 1,
												            1 + model.padding_idx + 1,
												            2 + model.padding_idx + 1,
												            model.padding_idx,
												        ]
												    ])
												lowercase					=		create_position_ids_from_input_ids(A__     ,model.padding_idx)
												self.assertEqual(position_ids.shape     ,expected_positions.shape)
												self.assertTrue(torch.all(torch.eq(A__     ,A__)))




							def 				A__   (       self):
												lowercase					=		self.model_tester.prepare_config_and_inputs()[0]
												lowercase					=		EsmEmbeddings(config=A__)

												lowercase					=		torch.empty(2     ,4     ,3_0)
												lowercase					=		[
												    0 + embeddings.padding_idx + 1,
												    1 + embeddings.padding_idx + 1,
												    2 + embeddings.padding_idx + 1,
												    3 + embeddings.padding_idx + 1,
												]
												lowercase					=		torch.as_tensor([expected_single_positions, expected_single_positions])
												lowercase					=		embeddings.create_position_ids_from_inputs_embeds(A__)
												self.assertEqual(position_ids.shape     ,expected_positions.shape)
												self.assertTrue(torch.all(torch.eq(A__     ,A__)))




							@unittest.skip('''Esm does not support embedding resizing''')
							def 				A__   (       self):
												pass




							@unittest.skip('''Esm does not support embedding resizing''')
							def 				A__   (       self):
												pass





							@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.''')
							def 				A__   (       self):
												pass







@require_torch
class        lowercase	(      SCREAMING_SNAKE_CASE__	):




							@slow
							def 				A__   (       self):
												with torch.no_grad():
																	lowercase					=		EsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D''')
																	model.eval()
																	lowercase					=		torch.tensor([[0, 1, 2, 3, 4, 5]])
																	lowercase					=		model(A__)[0]

																	lowercase					=		3_3

																	lowercase					=		torch.Size((1, 6, vocab_size))
																	self.assertEqual(output.shape     ,A__)

																	lowercase					=		torch.tensor(
																	    [[[8.9215, -10.5898, -6.4671], [-6.3967, -13.9114, -1.1212], [-7.7812, -13.9516, -3.7406]]])
																	self.assertTrue(torch.allclose(output[:, :3, :3]     ,A__     ,atol=1E-4))





							@slow
							def 				A__   (       self):
												with torch.no_grad():
																	lowercase					=		EsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D''')
																	model.eval()

																	lowercase					=		torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]])
																	lowercase					=		model(A__)[0]
																	# compare the actual values for a slice.
																	lowercase					=		torch.tensor(
																	    [[[0.1444, 0.5413, 0.3248], [0.3034, 0.0053, 0.3108], [0.3228, -0.2499, 0.3415]]])
																	self.assertTrue(torch.allclose(output[:, :3, :3]     ,A__     ,atol=1E-4))



from __future__ import annotations

from decimal import Decimal
from math import *  # noqa: F403

from sympy import diff



def  UpperCamelCase					(							lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__ = 10**-10	):
					'''simple docstring'''
					lowercase					=		a
					while True:
										lowercase					=		Decimal(lowerCAmelCase__	) - (
										    Decimal(eval(lowerCAmelCase__	)	) / Decimal(eval(str(diff(lowerCAmelCase__	)	)	)	)  # noqa: S307
										)
										# This number dictates the accuracy of the answer
										if abs(eval(lowerCAmelCase__	)	) < precision:  # noqa: S307
															return float(lowerCAmelCase__	)


# 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
							print(F'The root of x**2 - 5*x + 2 = 0 is {newton_raphson("x**2 - 5*x + 2", 0.4)}')
							# Find Square Root of 5
							print(F'The root of log(x) - 1 = 0 is {newton_raphson("log(x) - 1", 2)}')
							# Exponential Roots
							print(F'The root of exp(x) - 1 = 0 is {newton_raphson("exp(x) - 1", 0)}')




