




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 			a					:


						def __init__(    self  ,   A_  ,   A_=13  ,   A_=7  ,   A_=False  ,   A_=True  ,   A_=False  ,   A_=True  ,   A_=33  ,   A_=32  ,   A_=5  ,   A_=4  ,   A_=37  ,   A_="gelu"  ,   A_=0.1  ,   A_=0.1  ,   A_=512  ,   A_=16  ,   A_=2  ,   A_=0.02  ,   A_=3  ,   A_=4  ,   A_=None  ,   ):







										'''simple docstring'''


										_UpperCAmelCase    :						Tuple							=      parent
										_UpperCAmelCase    :						Any							=      batch_size
										_UpperCAmelCase    :						Tuple							=      seq_length
										_UpperCAmelCase    :						Optional[int]							=      is_training
										_UpperCAmelCase    :						List[Any]							=      use_input_mask
										_UpperCAmelCase    :						Union[str, Any]							=      use_token_type_ids
										_UpperCAmelCase    :						Tuple							=      use_labels
										_UpperCAmelCase    :						int							=      vocab_size
										_UpperCAmelCase    :						Optional[int]							=      hidden_size
										_UpperCAmelCase    :						Union[str, Any]							=      num_hidden_layers
										_UpperCAmelCase    :						List[Any]							=      num_attention_heads
										_UpperCAmelCase    :						List[str]							=      intermediate_size
										_UpperCAmelCase    :						Dict							=      hidden_act
										_UpperCAmelCase    :						Any							=      hidden_dropout_prob
										_UpperCAmelCase    :						int							=      attention_probs_dropout_prob
										_UpperCAmelCase    :						List[str]							=      max_position_embeddings
										_UpperCAmelCase    :						List[str]							=      type_vocab_size
										_UpperCAmelCase    :						Union[str, Any]							=      type_sequence_label_size
										_UpperCAmelCase    :						Any							=      initializer_range
										_UpperCAmelCase    :						str							=      num_labels
										_UpperCAmelCase    :						str							=      num_choices
										_UpperCAmelCase    :						Tuple							=      scope


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[str]							=      ids_tensor([self.batch_size, self.seq_length]  ,   self.vocab_size		)

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

										_UpperCAmelCase    :						Any							=      None
										_UpperCAmelCase    :						Optional[int]							=      None
										_UpperCAmelCase    :						Tuple							=      None
										if self.use_labels:
														_UpperCAmelCase    :						Union[str, Any]							=      ids_tensor([self.batch_size]  ,   self.type_sequence_label_size		)
														_UpperCAmelCase    :						Tuple							=      ids_tensor([self.batch_size, self.seq_length]  ,   self.num_labels		)
														_UpperCAmelCase    :						Optional[int]							=      ids_tensor([self.batch_size]  ,   self.num_choices		)

										_UpperCAmelCase    :						Optional[Any]							=      self.get_config()

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


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										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     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						Tuple							=      EsmModel(config=A_		)
										model.to(A_		)
										model.eval()
										_UpperCAmelCase    :						Union[str, Any]							=      model(A_  ,   attention_mask=A_		)
										_UpperCAmelCase    :						Union[str, Any]							=      model(A_		)
										_UpperCAmelCase    :						Any							=      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     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						Any							=      EsmForMaskedLM(config=A_		)
										model.to(A_		)
										model.eval()
										_UpperCAmelCase    :						Optional[int]							=      model(A_  ,   attention_mask=A_  ,   labels=A_		)
										self.parent.assertEqual(result.logits.shape  ,   (self.batch_size, self.seq_length, self.vocab_size)		)


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[Any]							=      self.num_labels
										_UpperCAmelCase    :						Union[str, Any]							=      EsmForTokenClassification(config=A_		)
										model.to(A_		)
										model.eval()
										_UpperCAmelCase    :						Dict							=      model(A_  ,   attention_mask=A_  ,   labels=A_		)
										self.parent.assertEqual(result.logits.shape  ,   (self.batch_size, self.seq_length, self.num_labels)		)





						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[Any]							=      self.prepare_config_and_inputs()
										(
										    _UpperCAmelCase							
										)    :						int							=      config_and_inputs
										_UpperCAmelCase    :						Union[str, Any]							=      {"input_ids": input_ids, "attention_mask": input_mask}
										return config, inputs_dict







@require_torch
class 			a					(		UpperCAmelCase							,	UpperCAmelCase							,	unittest.TestCase			):
						_lowercase        =     False

						_lowercase        =     (
						    (
						        EsmForMaskedLM,
						        EsmModel,
						        EsmForSequenceClassification,
						        EsmForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						_lowercase        =     ()
						_lowercase        =     (
						    {
						        "feature-extraction": EsmModel,
						        "fill-mask": EsmForMaskedLM,
						        "text-classification": EsmForSequenceClassification,
						        "token-classification": EsmForTokenClassification,
						        "zero-shot": EsmForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						_lowercase        =     True


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[int]							=      EsmModelTester(self		)
										_UpperCAmelCase    :						Tuple							=      ConfigTester(self  ,   config_class=A_  ,   hidden_size=37		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										self.config_tester.run_common_tests()


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Union[str, Any]							=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*A_		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


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


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[Any]							=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*A_		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Union[str, Any]							=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*A_		)


						@slow
						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_UpperCAmelCase    :						Tuple							=      EsmModel.from_pretrained(A_		)
														self.assertIsNotNone(A_		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Dict							=      self.model_tester.prepare_config_and_inputs()[0]
										_UpperCAmelCase    :						int							=      EsmEmbeddings(config=A_		)

										_UpperCAmelCase    :						List[Any]							=      torch.as_tensor([[12, 31, 13, model.padding_idx]]		)
										_UpperCAmelCase    :						str							=      torch.as_tensor(
										    [
										        [
										            0 + model.padding_idx + 1,
										            1 + model.padding_idx + 1,
										            2 + model.padding_idx + 1,
										            model.padding_idx,
										        ]
										    ]		)
										_UpperCAmelCase    :						str							=      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     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						str							=      self.model_tester.prepare_config_and_inputs()[0]
										_UpperCAmelCase    :						List[Any]							=      EsmEmbeddings(config=A_		)

										_UpperCAmelCase    :						Union[str, Any]							=      torch.empty(2  ,   4  ,   30		)
										_UpperCAmelCase    :						List[str]							=      [
										    0 + embeddings.padding_idx + 1,
										    1 + embeddings.padding_idx + 1,
										    2 + embeddings.padding_idx + 1,
										    3 + embeddings.padding_idx + 1,
										]
										_UpperCAmelCase    :						Optional[Any]							=      torch.as_tensor([expected_single_positions, expected_single_positions]		)
										_UpperCAmelCase    :						Tuple							=      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     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										pass


						@unittest.skip("Esm does not support embedding resizing"		)
						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										pass





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







										'''simple docstring'''


										pass







@require_torch
class 			a					(		UpperCAmelCase			):


						@slow
						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										with torch.no_grad():
														_UpperCAmelCase    :						Optional[Any]							=      EsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"		)
														model.eval()
														_UpperCAmelCase    :						List[Any]							=      torch.tensor([[0, 1, 2, 3, 4, 5]]		)
														_UpperCAmelCase    :						List[str]							=      model(A_		)[0]

														_UpperCAmelCase    :						List[str]							=      33

														_UpperCAmelCase    :						Optional[int]							=      torch.Size((1, 6, vocab_size)		)
														self.assertEqual(output.shape  ,   A_		)

														_UpperCAmelCase    :						Tuple							=      torch.tensor(
														    [[[8.92_15, -10.58_98, -6.46_71], [-6.39_67, -13.91_14, -1.12_12], [-7.78_12, -13.95_16, -3.74_06]]]		)
														self.assertTrue(torch.allclose(output[:, :3, :3]  ,   A_  ,   atol=1e-4		)		)





						@slow
						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										with torch.no_grad():
														_UpperCAmelCase    :						Any							=      EsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"		)
														model.eval()

														_UpperCAmelCase    :						Any							=      torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]		)
														_UpperCAmelCase    :						List[Any]							=      model(A_		)[0]
														# compare the actual values for a slice.
														_UpperCAmelCase    :						Dict							=      torch.tensor(
														    [[[0.14_44, 0.54_13, 0.32_48], [0.30_34, 0.00_53, 0.31_08], [0.32_28, -0.24_99, 0.34_15]]]		)
														self.assertTrue(torch.allclose(output[:, :3, :3]  ,   A_  ,   atol=1e-4		)		)





from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload

class 			a					:


						def __init__(    self  ,   A_ = None		):







										'''simple docstring'''


										if components is None:
														_UpperCAmelCase    :						Dict							=      []
										_UpperCAmelCase    :						Dict							=      list(A_		)


						def __len__(    self		):







										'''simple docstring'''


										return len(self.__components		)


						def __str__(    self		):







										'''simple docstring'''


										return "(" + ",".join(map(A_  ,   self.__components		)		) + ")"


						def __add__(    self  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						int							=      len(self		)
										if size == len(A_		):
														_UpperCAmelCase    :						Union[str, Any]							=      [self.__components[i] + other.component(A_		) for i in range(A_		)]
														return Vector(A_		)
										else:
														raise Exception("must have the same size"		)


						def __sub__(    self  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[str]							=      len(self		)
										if size == len(A_		):
														_UpperCAmelCase    :						Optional[Any]							=      [self.__components[i] - other.component(A_		) for i in range(A_		)]
														return Vector(A_		)
										else:  # error case
														raise Exception("must have the same size"		)


						@overload
						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										...


						@overload
						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										...


						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										if isinstance(A_  ,   (float, int)		):
														_UpperCAmelCase    :						str							=      [c * other for c in self.__components]
														return Vector(A_		)
										elif isinstance(A_  ,   A_		) and len(self		) == len(A_		):
														_UpperCAmelCase    :						int							=      len(self		)
														_UpperCAmelCase    :						Any							=      [self.__components[i] * other.component(A_		) for i in range(A_		)]
														return sum(A_		)
										else:  # error case
														raise Exception("invalid operand!"		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										return Vector(self.__components		)


						def     _UpperCAmelCase			(    self  ,   A_		):







										'''simple docstring'''


										if isinstance(A_  ,   A_		) and -len(self.__components		) <= i < len(self.__components		):
														return self.__components[i]
										else:
														raise Exception("index out of range"		)


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_		):







										'''simple docstring'''


										assert -len(self.__components		) <= pos < len(self.__components		)
										_UpperCAmelCase    :						Any							=      value


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										if len(self.__components		) == 0:
														raise Exception("Vector is empty"		)
										_UpperCAmelCase    :						Dict							=      [c**2 for c in self.__components]
										return math.sqrt(sum(A_		)		)





						def     _UpperCAmelCase			(    self  ,   A_  ,   A_ = False		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[Any]							=      self * other
										_UpperCAmelCase    :						str							=      self.euclidean_length() * other.euclidean_length()
										if deg:
														return math.degrees(math.acos(num / den		)		)
										else:
														return math.acos(num / den		)







def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int	)				->    Vector:
				assert isinstance(lowerCAmelCase					, lowerCAmelCase	)
				return Vector([0] * dimension	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int					, lowerCAmelCase:					int	)				->    Vector:
				assert isinstance(lowerCAmelCase					, lowerCAmelCase	) and (isinstance(lowerCAmelCase					, lowerCAmelCase	))
				_UpperCAmelCase    :						Tuple							=      [0] * dimension
				_UpperCAmelCase    :						Optional[Any]							=      1
				return Vector(lowerCAmelCase	)

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					float					, lowerCAmelCase:					Vector					, lowerCAmelCase:					Vector	)				->    Vector:
				assert (
				    isinstance(lowerCAmelCase					, lowerCAmelCase	)
				    and isinstance(lowerCAmelCase					, lowerCAmelCase	)
				    and (isinstance(lowerCAmelCase					, (int, float)	))
				)
				return x * scalar + y

def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int					, lowerCAmelCase:					int					, lowerCAmelCase:					int	)				->    Vector:
				random.seed(lowerCAmelCase	)
				_UpperCAmelCase    :						Optional[int]							=      [random.randint(lowerCAmelCase					, lowerCAmelCase	) for _ in range(lowerCAmelCase	)]
				return Vector(lowerCAmelCase	)

class 			a					:


						def __init__(    self  ,   A_  ,   A_  ,   A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[Any]							=      matrix
										_UpperCAmelCase    :						Union[str, Any]							=      w
										_UpperCAmelCase    :						str							=      h


						def __str__(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[int]							=      ""
										for i in range(self.__height		):
														ans += "|"
														for j in range(self.__width		):
																		if j < self.__width - 1:
																						ans += str(self.__matrix[i][j]		) + ","
																		else:
																						ans += str(self.__matrix[i][j]		) + "|\n"
										return ans


						def __add__(    self  ,   A_		):







										'''simple docstring'''


										if self.__width == other.width() and self.__height == other.height():
														_UpperCAmelCase    :						Optional[int]							=      []
														for i in range(self.__height		):
																		_UpperCAmelCase    :						List[str]							=      [
																		    self.__matrix[i][j] + other.component(A_  ,   A_		)
																		    for j in range(self.__width		)
																		]
																		matrix.append(A_		)
														return Matrix(A_  ,   self.__width  ,   self.__height		)
										else:
														raise Exception("matrix must have the same dimension!"		)


						def __sub__(    self  ,   A_		):







										'''simple docstring'''


										if self.__width == other.width() and self.__height == other.height():
														_UpperCAmelCase    :						Optional[Any]							=      []
														for i in range(self.__height		):
																		_UpperCAmelCase    :						Tuple							=      [
																		    self.__matrix[i][j] - other.component(A_  ,   A_		)
																		    for j in range(self.__width		)
																		]
																		matrix.append(A_		)
														return Matrix(A_  ,   self.__width  ,   self.__height		)
										else:
														raise Exception("matrices must have the same dimension!"		)


						@overload
						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										...


						@overload
						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										...


						def __mul__(    self  ,   A_		):







										'''simple docstring'''


										if isinstance(A_  ,   A_		):  # matrix-vector
														if len(A_		) == self.__width:
																		_UpperCAmelCase    :						Union[str, Any]							=      zero_vector(self.__height		)
																		for i in range(self.__height		):
																						_UpperCAmelCase    :						List[Any]							=      [
																						    self.__matrix[i][j] * other.component(A_		)
																						    for j in range(self.__width		)
																						]
																						ans.change_component(A_  ,   sum(A_		)		)
																		return ans
														else:
																		raise Exception(
																		    "vector must have the same size as the "
																		    "number of columns of the matrix!"		)
										elif isinstance(A_  ,   (int, float)		):  # matrix-scalar
														_UpperCAmelCase    :						Any							=      [
														    [self.__matrix[i][j] * other for j in range(self.__width		)]
														    for i in range(self.__height		)
														]
														return Matrix(A_  ,   self.__width  ,   self.__height		)
										return None


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										return self.__height


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										return self.__width


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_		):







										'''simple docstring'''


										if 0 <= x < self.__height and 0 <= y < self.__width:
														return self.__matrix[x][y]
										else:
														raise Exception("change_component: indices out of bounds"		)


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_  ,   A_		):







										'''simple docstring'''


										if 0 <= x < self.__height and 0 <= y < self.__width:
														_UpperCAmelCase    :						Tuple							=      value
										else:
														raise Exception("change_component: indices out of bounds"		)


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_		):







										'''simple docstring'''


										if self.__height != self.__width:
														raise Exception("Matrix is not square"		)
										_UpperCAmelCase    :						Union[str, Any]							=      self.__matrix[:x] + self.__matrix[x + 1 :]
										for i in range(len(A_		)		):
														_UpperCAmelCase    :						Tuple							=      minor[i][:y] + minor[i][y + 1 :]
										return Matrix(A_  ,   self.__width - 1  ,   self.__height - 1		).determinant()


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_		):







										'''simple docstring'''


										if self.__height != self.__width:
														raise Exception("Matrix is not square"		)
										if 0 <= x < self.__height and 0 <= y < self.__width:
														return (-1) ** (x + y) * self.minor(A_  ,   A_		)
										else:
														raise Exception("Indices out of bounds"		)





						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										if self.__height != self.__width:
														raise Exception("Matrix is not square"		)
										if self.__height < 1:
														raise Exception("Matrix has no element"		)
										elif self.__height == 1:
														return self.__matrix[0][0]
										elif self.__height == 2:
														return (
														    self.__matrix[0][0] * self.__matrix[1][1]
														    - self.__matrix[0][1] * self.__matrix[1][0]
														)
										else:
														_UpperCAmelCase    :						Dict							=      [
														    self.__matrix[0][y] * self.cofactor(0  ,   A_		) for y in range(self.__width		)
														]
														return sum(A_		)







def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int	)				->    Matrix:
				_UpperCAmelCase    :						list[list[float]]							=      [[0] * n for _ in range(lowerCAmelCase	)]
				return Matrix(lowerCAmelCase					, lowerCAmelCase					, lowerCAmelCase	)





def 							__SCREAMING_SNAKE_CASE							(			lowerCAmelCase:					int					, lowerCAmelCase:					int					, lowerCAmelCase:					int					, lowerCAmelCase:					int	)				->    Matrix:
				random.seed(lowerCAmelCase	)
				_UpperCAmelCase    :						list[list[float]]							=      [
				    [random.randint(lowerCAmelCase					, lowerCAmelCase	) for _ in range(lowerCAmelCase	)] for _ in range(lowerCAmelCase	)
				]
				return Matrix(lowerCAmelCase					, lowerCAmelCase					, lowerCAmelCase	)


