


'''simple docstring'''






from __future__ import annotations

import unittest

from transformers import EsmConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import numpy
							import tensorflow as tf

							from transformers.models.esm.modeling_tf_esm import (
							    TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TFEsmForMaskedLM,
							    TFEsmForSequenceClassification,
							    TFEsmForTokenClassification,
							    TFEsmModel,
							)




class 	a__       :





							'''simple docstring'''






							def __init__(	self      ,					lowerCamelCase_      ,					) ->							Any:
														lowerCAmelCase__      =			parent
														lowerCAmelCase__      =			13
														lowerCAmelCase__      =			7
														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			99
														lowerCAmelCase__      =			32
														lowerCAmelCase__      =			2
														lowerCAmelCase__      =			4
														lowerCAmelCase__      =			37
														lowerCAmelCase__      =			'''gelu'''
														lowerCAmelCase__      =			0.1
														lowerCAmelCase__      =			0.1
														lowerCAmelCase__      =			5_12
														lowerCAmelCase__      =			16
														lowerCAmelCase__      =			2
														lowerCAmelCase__      =			0.02
														lowerCAmelCase__      =			3
														lowerCAmelCase__      =			4
														lowerCAmelCase__      =			None







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														lowerCAmelCase__      =			ids_tensor([self.batch_size, self.seq_length]      ,					self.vocab_size     )

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

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

														lowerCAmelCase__      =			EsmConfig(
														    vocab_size=self.vocab_size      ,					hidden_size=self.hidden_size      ,					num_hidden_layers=self.num_hidden_layers      ,					pad_token_id=1      ,					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      ,					)

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







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[int]:
														(
														    (
														    lowerCAmelCase__      
														)     ,      (
														    lowerCAmelCase__      
														)     ,      (
														    lowerCAmelCase__      
														)     ,      (
														    lowerCAmelCase__      
														)     ,      (
														    lowerCAmelCase__      
														)     ,      (
														    lowerCAmelCase__      
														)     ,            
														)      =			self.prepare_config_and_inputs()

														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			floats_tensor([self.batch_size, self.seq_length, self.hidden_size]     )
														lowerCAmelCase__      =			ids_tensor([self.batch_size, self.seq_length]      ,					vocab_size=2     )

														return (
														    config,
														    input_ids,
														    input_mask,
														    sequence_labels,
														    token_labels,
														    choice_labels,
														    encoder_hidden_states,
														    encoder_attention_mask,
														)







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							str:
														lowerCAmelCase__      =			TFEsmModel(config=lowerCamelCase_     )
														lowerCAmelCase__      =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
														lowerCAmelCase__      =			model(lowerCamelCase_     )

														lowerCAmelCase__      =			[input_ids, input_mask]
														lowerCAmelCase__      =			model(lowerCamelCase_     )

														lowerCAmelCase__      =			model(lowerCamelCase_     )

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







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					) ->							Tuple:
														lowerCAmelCase__      =			True

														lowerCAmelCase__      =			TFEsmModel(config=lowerCamelCase_     )
														lowerCAmelCase__      =			{
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''encoder_hidden_states''': encoder_hidden_states,
														    '''encoder_attention_mask''': encoder_attention_mask,
														}
														lowerCAmelCase__      =			model(lowerCamelCase_     )

														lowerCAmelCase__      =			[input_ids, input_mask]
														lowerCAmelCase__      =			model(lowerCamelCase_      ,					encoder_hidden_states=lowerCamelCase_     )

														# Also check the case where encoder outputs are not passed
														lowerCAmelCase__      =			model(lowerCamelCase_      ,					attention_mask=lowerCamelCase_     )

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







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							Dict:
														lowerCAmelCase__      =			TFEsmForMaskedLM(config=lowerCamelCase_     )
														lowerCAmelCase__      =			model([input_ids, input_mask]     )
														self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.seq_length, self.vocab_size)     )







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							str:
														lowerCAmelCase__      =			self.num_labels
														lowerCAmelCase__      =			TFEsmForTokenClassification(config=lowerCamelCase_     )
														lowerCAmelCase__      =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
														lowerCAmelCase__      =			model(lowerCamelCase_     )
														self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.seq_length, self.num_labels)     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														lowerCAmelCase__      =			self.prepare_config_and_inputs()
														(
														    (
														    lowerCAmelCase__      
														)     ,      (
														    lowerCAmelCase__      
														)     ,      (
														    lowerCAmelCase__      
														)     ,      (
														    lowerCAmelCase__      
														)     ,      (
														    lowerCAmelCase__      
														)     ,      (
														    lowerCAmelCase__      
														)     ,            
														)      =			config_and_inputs
														lowerCAmelCase__      =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
														return config, inputs_dict




@require_tf
class 	a__       (							a__       ,       a__       ,       unittest.TestCase						):





							'''simple docstring'''






							lowercase__    :     Any					=   (
							    (
							        TFEsmModel,
							        TFEsmForMaskedLM,
							        TFEsmForSequenceClassification,
							        TFEsmForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							lowercase__    :     Dict					=   (
							    {
							        "feature-extraction": TFEsmModel,
							        "fill-mask": TFEsmForMaskedLM,
							        "text-classification": TFEsmForSequenceClassification,
							        "token-classification": TFEsmForTokenClassification,
							        "zero-shot": TFEsmForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							lowercase__    :     List[str]					=   False
							lowercase__    :     Union[str, Any]					=   False







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														lowerCAmelCase__      =			TFEsmModelTester(self     )
														lowerCAmelCase__      =			ConfigTester(self      ,					config_class=lowerCamelCase_      ,					hidden_size=37     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														self.config_tester.run_common_tests()







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_decoder()
														self.model_tester.create_and_check_model_as_decoder(*lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_     )







							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCAmelCase__      =			TFEsmModel.from_pretrained(lowerCamelCase_     )
																					self.assertIsNotNone(lowerCamelCase_     )







							@unittest.skip('''Protein models do not support embedding resizing.'''     )
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														pass







							@unittest.skip('''Protein models do not support embedding resizing.'''     )
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														pass







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														lowerCAmelCase__     ,      lowerCAmelCase__      =			self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCAmelCase__      =			model_class(lowerCamelCase_     )
																					assert isinstance(model.get_input_embeddings()      ,					tf.keras.layers.Layer     )
																					if model_class is TFEsmForMaskedLM:
																												# Output embedding test differs from the main test because they're a matrix, not a layer
																												lowerCAmelCase__      =			model.get_bias()
																												assert isinstance(lowerCamelCase_      ,					lowerCamelCase_     )
																												for k, v in name.items():
																																			assert isinstance(lowerCamelCase_      ,					tf.Variable     )
																					else:
																												lowerCAmelCase__      =			model.get_output_embeddings()
																												assert x is None
																												lowerCAmelCase__      =			model.get_bias()
																												assert name is None




@require_tf
class 	a__       (							unittest.TestCase						):





							'''simple docstring'''






							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														lowerCAmelCase__      =			TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D'''     )

														lowerCAmelCase__      =			tf.constant([[0, 1, 2, 3, 4, 5]]     )
														lowerCAmelCase__      =			model(lowerCamelCase_     )[0]
														lowerCAmelCase__      =			[1, 6, 33]
														self.assertEqual(list(output.numpy().shape     )      ,					lowerCamelCase_     )
														# compare the actual values for a slice.
														lowerCAmelCase__      =			tf.constant(
														    [
														        [
														            [8.921_518, -10.589_814, -6.4_671_307],
														            [-6.3_967_156, -13.911_377, -1.1_211_915],
														            [-7.781_247, -13.951_557, -3.740_592],
														        ]
														    ]     )
														self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()      ,					expected_slice.numpy()      ,					atol=1e-2     )     )







							@slow
							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														lowerCAmelCase__      =			TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D'''     )

														lowerCAmelCase__      =			tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]     )
														lowerCAmelCase__      =			model(lowerCamelCase_     )[0]
														# compare the actual values for a slice.
														lowerCAmelCase__      =			tf.constant(
														    [
														        [
														            [0.14_443_092, 0.54_125_327, 0.3_247_739],
														            [0.30_340_484, 0.00_526_676, 0.31_077_722],
														            [0.32_278_043, -0.24_987_096, 0.3_414_628],
														        ]
														    ]     )
														self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()      ,					expected_slice.numpy()      ,					atol=1e-4     )     )



'''simple docstring'''






from math import pi, sqrt, tan


def       _snake_case     (  A )		->					float:
							if side_length < 0:
														raise ValueError('''surface_area_cube() only accepts non-negative values''' )
							return 6 * side_length**2


def       _snake_case     (  A				,					A				,					A )		->					float:
							if length < 0 or breadth < 0 or height < 0:
														raise ValueError('''surface_area_cuboid() only accepts non-negative values''' )
							return 2 * ((length * breadth) + (breadth * height) + (length * height))


def       _snake_case     (  A )		->					float:
							if radius < 0:
														raise ValueError('''surface_area_sphere() only accepts non-negative values''' )
							return 4 * pi * radius**2


def       _snake_case     (  A )		->					float:
							if radius < 0:
														raise ValueError('''surface_area_hemisphere() only accepts non-negative values''' )
							return 3 * pi * radius**2


def       _snake_case     (  A				,					A )		->					float:
							if radius < 0 or height < 0:
														raise ValueError('''surface_area_cone() only accepts non-negative values''' )
							return pi * radius * (radius + (height**2 + radius**2) ** 0.5)


def       _snake_case     (  A				,					A				,					A )		->					float:
							if radius_a < 0 or radius_a < 0 or height < 0:
														raise ValueError(
														    '''surface_area_conical_frustum() only accepts non-negative values''' )
							lowerCAmelCase__      =			(height**2 + (radius_a - radius_a) ** 2) ** 0.5
							return pi * ((slant_height * (radius_a + radius_a)) + radius_a**2 + radius_a**2)


def       _snake_case     (  A				,					A )		->					float:
							if radius < 0 or height < 0:
														raise ValueError('''surface_area_cylinder() only accepts non-negative values''' )
							return 2 * pi * radius * (height + radius)


def       _snake_case     (  A				,					A )		->					float:
							if torus_radius < 0 or tube_radius < 0:
														raise ValueError('''surface_area_torus() only accepts non-negative values''' )
							if torus_radius < tube_radius:
														raise ValueError(
														    '''surface_area_torus() does not support spindle or self intersecting tori''' )
							return 4 * pow(A				,					2 ) * torus_radius * tube_radius


def       _snake_case     (  A				,					A )		->					float:
							if length < 0 or width < 0:
														raise ValueError('''area_rectangle() only accepts non-negative values''' )
							return length * width


def       _snake_case     (  A )		->					float:
							if side_length < 0:
														raise ValueError('''area_square() only accepts non-negative values''' )
							return side_length**2


def       _snake_case     (  A				,					A )		->					float:
							if base < 0 or height < 0:
														raise ValueError('''area_triangle() only accepts non-negative values''' )
							return (base * height) / 2


def       _snake_case     (  A				,					A				,					A )		->					float:
							if sidea < 0 or sidea < 0 or sidea < 0:
														raise ValueError('''area_triangle_three_sides() only accepts non-negative values''' )
							elif sidea + sidea < sidea or sidea + sidea < sidea or sidea + sidea < sidea:
														raise ValueError('''Given three sides do not form a triangle''' )
							lowerCAmelCase__      =			(sidea + sidea + sidea) / 2
							lowerCAmelCase__      =			sqrt(
							    semi_perimeter
							    * (semi_perimeter - sidea)
							    * (semi_perimeter - sidea)
							    * (semi_perimeter - sidea) )
							return area


def       _snake_case     (  A				,					A )		->					float:
							if base < 0 or height < 0:
														raise ValueError('''area_parallelogram() only accepts non-negative values''' )
							return base * height


def       _snake_case     (  A				,					A				,					A )		->					float:
							if basea < 0 or basea < 0 or height < 0:
														raise ValueError('''area_trapezium() only accepts non-negative values''' )
							return 1 / 2 * (basea + basea) * height


def       _snake_case     (  A )		->					float:
							if radius < 0:
														raise ValueError('''area_circle() only accepts non-negative values''' )
							return pi * radius**2


def       _snake_case     (  A				,					A )		->					float:
							if radius_x < 0 or radius_y < 0:
														raise ValueError('''area_ellipse() only accepts non-negative values''' )
							return pi * radius_x * radius_y


def       _snake_case     (  A				,					A )		->					float:
							if diagonal_a < 0 or diagonal_a < 0:
														raise ValueError('''area_rhombus() only accepts non-negative values''' )
							return 1 / 2 * diagonal_a * diagonal_a


def       _snake_case     (  A				,					A )		->					float:
							if not isinstance(A				,					A ) or sides < 3:
														raise ValueError(
														    '''area_reg_polygon() only accepts integers greater than or \
equal to three as number of sides''' )
							elif length < 0:
														raise ValueError(
														    '''area_reg_polygon() only accepts non-negative values as \
length of a side''' )
							return (sides * length**2) / (4 * tan(pi / sides ))
							return (sides * length**2) / (4 * tan(pi / sides ))


if __name__ == "__main__":
		import doctest

		doctest.testmod(verbose=True)  # verbose so we can see methods missing tests

		print('''[DEMO] Areas of various geometric shapes: \n''')
		print(f"""Rectangle: {area_rectangle(10, 20) = }""")
		print(f"""Square: {area_square(10) = }""")
		print(f"""Triangle: {area_triangle(10, 10) = }""")
		print(f"""Triangle: {area_triangle_three_sides(5, 12, 13) = }""")
		print(f"""Parallelogram: {area_parallelogram(10, 20) = }""")
		print(f"""Rhombus: {area_rhombus(10, 20) = }""")
		print(f"""Trapezium: {area_trapezium(10, 20, 30) = }""")
		print(f"""Circle: {area_circle(20) = }""")
		print(f"""Ellipse: {area_ellipse(10, 20) = }""")
		print('''\nSurface Areas of various geometric shapes: \n''')
		print(f"""Cube: {surface_area_cube(20) = }""")
		print(f"""Cuboid: {surface_area_cuboid(10, 20, 30) = }""")
		print(f"""Sphere: {surface_area_sphere(20) = }""")
		print(f"""Hemisphere: {surface_area_hemisphere(20) = }""")
		print(f"""Cone: {surface_area_cone(10, 20) = }""")
		print(f"""Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }""")
		print(f"""Cylinder: {surface_area_cylinder(10, 20) = }""")
		print(f"""Torus: {surface_area_torus(20, 10) = }""")
		print(f"""Equilateral Triangle: {area_reg_polygon(3, 10) = }""")
		print(f"""Square: {area_reg_polygon(4, 10) = }""")
		print(f"""Reqular Pentagon: {area_reg_polygon(5, 10) = }""")