


'''simple docstring'''







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							:     Optional[Any]       ,     __magic_name__							:     int       ,     __magic_name__							:     Tuple=13       ,     __magic_name__							:     int=7       ,     __magic_name__							:     Any=False       ,     __magic_name__							:     Optional[Any]=True       ,     __magic_name__							:     List[Any]=False       ,     __magic_name__							:     Tuple=True       ,     __magic_name__							:     List[Any]=33       ,     __magic_name__							:     Any=32       ,     __magic_name__							:     List[Any]=5       ,     __magic_name__							:     Any=4       ,     __magic_name__							:     Optional[Any]=37       ,     __magic_name__							:     Dict="gelu"       ,     __magic_name__							:     Tuple=0.1       ,     __magic_name__							:     Union[str, Any]=0.1       ,     __magic_name__							:     Optional[int]=5_12       ,     __magic_name__							:     Tuple=16       ,     __magic_name__							:     str=2       ,     __magic_name__							:     Tuple=0.0_2       ,     __magic_name__							:     List[Any]=3       ,     __magic_name__							:     Optional[int]=4       ,     __magic_name__							:     List[str]=None       ,     )					->       List[Any]:







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

							def     UpperCAmelCase__	(							self							:     List[str]   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  ids_tensor([self.batch_size, self.seq_length]       ,     self.vocab_size   )

													UpperCAmelCase_			:      Dict     						=  None
													if self.use_input_mask:
																			UpperCAmelCase_			:      int     						=  random_attention_mask([self.batch_size, self.seq_length]   )

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

													UpperCAmelCase_			:      Union[str, Any]     						=  self.get_config()

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

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       List[Any]:







													"""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							:     Any       ,     __magic_name__							:     int       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     Any       ,     __magic_name__							:     int       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     List[Any]   )					->       Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  EsmModel(config=__lowerCamelCase   )
													model.to(__lowerCamelCase   )
													model.eval()
													UpperCAmelCase_			:      List[str]     						=  model(__lowerCamelCase       ,     attention_mask=__lowerCamelCase   )
													UpperCAmelCase_			:      Optional[int]     						=  model(__lowerCamelCase   )
													UpperCAmelCase_			:      Any     						=  model(__lowerCamelCase   )

													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							:     List[str]       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     List[Any]       ,     __magic_name__							:     int       ,     __magic_name__							:     Any       ,     __magic_name__							:     int       ,     __magic_name__							:     List[Any]   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  EsmForMaskedLM(config=__lowerCamelCase   )
													model.to(__lowerCamelCase   )
													model.eval()
													UpperCAmelCase_			:      Union[str, Any]     						=  model(__lowerCamelCase       ,     attention_mask=__lowerCamelCase       ,     labels=__lowerCamelCase   )
													self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.vocab_size)   )

							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     str       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     Union[str, Any]   )					->       str:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  self.num_labels
													UpperCAmelCase_			:      Optional[int]     						=  EsmForTokenClassification(config=__lowerCamelCase   )
													model.to(__lowerCamelCase   )
													model.eval()
													UpperCAmelCase_			:      List[Any]     						=  model(__lowerCamelCase       ,     attention_mask=__lowerCamelCase       ,     labels=__lowerCamelCase   )
													self.parent.assertEqual(result.logits.shape       ,     (self.batch_size, self.seq_length, self.num_labels)   )



							def     UpperCAmelCase__	(							self							:     Optional[int]   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  self.prepare_config_and_inputs()
													(
													    UpperCAmelCase_     
													)			:      Any     						=  config_and_inputs
													UpperCAmelCase_			:      List[Any]     						=  {"input_ids": input_ids, "attention_mask": input_mask}
													return config, inputs_dict





@require_torch
class 					__a      (lowercase__       ,		lowercase__       ,		unittest.TestCase     ):
							__a					:   Union[str, Any]									=       False

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

							def     UpperCAmelCase__	(							self							:     List[str]   )					->       Tuple:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  EsmModelTester(self   )
													UpperCAmelCase_			:      int     						=  ConfigTester(self       ,     config_class=__lowerCamelCase       ,     hidden_size=37   )

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       Dict:







													"""simple docstring"""
													self.config_tester.run_common_tests()

							def     UpperCAmelCase__	(							self							:     Dict   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*__lowerCamelCase   )

							def     UpperCAmelCase__	(							self							:     List[str]   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  self.model_tester.prepare_config_and_inputs()
													for type in ["absolute", "relative_key", "relative_key_query"]:
																			UpperCAmelCase_			:      List[Any]     						=  type
																			self.model_tester.create_and_check_model(*__lowerCamelCase   )

							def     UpperCAmelCase__	(							self							:     str   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*__lowerCamelCase   )

							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase   )

							@slow
							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Optional[int]:







													"""simple docstring"""



													for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			UpperCAmelCase_			:      Optional[Any]     						=  EsmModel.from_pretrained(__lowerCamelCase   )
																			self.assertIsNotNone(__lowerCamelCase   )

							def     UpperCAmelCase__	(							self							:     List[str]   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  self.model_tester.prepare_config_and_inputs()[0]
													UpperCAmelCase_			:      List[str]     						=  EsmEmbeddings(config=__lowerCamelCase   )

													UpperCAmelCase_			:      Optional[int]     						=  torch.as_tensor([[12, 31, 13, model.padding_idx]]   )
													UpperCAmelCase_			:      Dict     						=  torch.as_tensor(
													    [
													        [
													            0 + model.padding_idx + 1,
													            1 + model.padding_idx + 1,
													            2 + model.padding_idx + 1,
													            model.padding_idx,
													        ]
													    ]   )
													UpperCAmelCase_			:      Any     						=  create_position_ids_from_input_ids(__lowerCamelCase       ,     model.padding_idx   )
													self.assertEqual(position_ids.shape       ,     expected_positions.shape   )
													self.assertTrue(torch.all(torch.eq(__lowerCamelCase       ,     __lowerCamelCase   )   )   )

							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  self.model_tester.prepare_config_and_inputs()[0]
													UpperCAmelCase_			:      Any     						=  EsmEmbeddings(config=__lowerCamelCase   )

													UpperCAmelCase_			:      Optional[int]     						=  torch.empty(2       ,     4       ,     30   )
													UpperCAmelCase_			:      Union[str, Any]     						=  [
													    0 + embeddings.padding_idx + 1,
													    1 + embeddings.padding_idx + 1,
													    2 + embeddings.padding_idx + 1,
													    3 + embeddings.padding_idx + 1,
													]
													UpperCAmelCase_			:      Optional[int]     						=  torch.as_tensor([expected_single_positions, expected_single_positions]   )
													UpperCAmelCase_			:      Union[str, Any]     						=  embeddings.create_position_ids_from_inputs_embeds(__lowerCamelCase   )
													self.assertEqual(position_ids.shape       ,     expected_positions.shape   )
													self.assertTrue(torch.all(torch.eq(__lowerCamelCase       ,     __lowerCamelCase   )   )   )

							@unittest.skip('''Esm does not support embedding resizing'''   )
							def     UpperCAmelCase__	(							self							:     Union[str, Any]   )					->       Optional[Any]:







													"""simple docstring"""
													pass

							@unittest.skip('''Esm does not support embedding resizing'''   )
							def     UpperCAmelCase__	(							self							:     List[Any]   )					->       Dict:







													"""simple docstring"""
													pass



							@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''   )
							def     UpperCAmelCase__	(							self							:     Tuple   )					->       str:







													"""simple docstring"""
													pass





@require_torch
class 					__a      (lowercase__     ):

							@slow
							def     UpperCAmelCase__	(							self							:     List[str]   )					->       Optional[Any]:







													"""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_			:      int     						=  model(__lowerCamelCase   )[0]

																			UpperCAmelCase_			:      Dict     						=  33

																			UpperCAmelCase_			:      int     						=  torch.Size((1, 6, vocab_size)   )
																			self.assertEqual(output.shape       ,     __lowerCamelCase   )

																			UpperCAmelCase_			:      Any     						=  torch.tensor(
																			    [[[8.9_2_1_5, -10.58_98, -6.4_6_7_1], [-6.3_9_6_7, -13.91_14, -1.1_2_1_2], [-7.7_8_1_2, -13.95_16, -3.7_4_0_6]]]   )
																			self.assertTrue(torch.allclose(output[:, :3, :3]       ,     __lowerCamelCase       ,     atol=1E-4   )   )



							@slow
							def     UpperCAmelCase__	(							self							:     Optional[Any]   )					->       Union[str, Any]:







													"""simple docstring"""



													with torch.no_grad():
																			UpperCAmelCase_			:      int     						=  EsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D'''   )
																			model.eval()

																			UpperCAmelCase_			:      Optional[int]     						=  torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]   )
																			UpperCAmelCase_			:      Optional[int]     						=  model(__lowerCamelCase   )[0]
																			# compare the actual values for a slice.
																			UpperCAmelCase_			:      Optional[Any]     						=  torch.tensor(
																			    [[[0.1_4_4_4, 0.5_4_1_3, 0.3_2_4_8], [0.3_0_3_4, 0.0_0_5_3, 0.3_1_0_8], [0.3_2_2_8, -0.2_4_9_9, 0.3_4_1_5]]]   )
																			self.assertTrue(torch.allclose(output[:, :3, :3]       ,     __lowerCamelCase       ,     atol=1E-4   )   )












'''simple docstring'''







class 					__a      :

							def __init__(							self							:     List[Any]       ,     __magic_name__							:     int   )					->       None:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  size
													UpperCAmelCase_			:      Tuple     						=  [0] * size
													UpperCAmelCase_			:      Optional[Any]     						=  [0] * size

							@staticmethod
							def     UpperCAmelCase__	(							__magic_name__							:     int   )					->       int:







													"""simple docstring"""
													return index | (index + 1)

							@staticmethod
							def     UpperCAmelCase__	(							__magic_name__							:     int   )					->       int:







													"""simple docstring"""
													return (index & (index + 1)) - 1

							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     int       ,     __magic_name__							:     int   )					->       None:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  value
													while index < self.size:
																			UpperCAmelCase_			:      str     						=  self.get_prev(__magic_name__   ) + 1
																			if current_left_border == index:
																									UpperCAmelCase_			:      List[str]     						=  value
																			else:
																									UpperCAmelCase_			:      Optional[int]     						=  max(__magic_name__       ,     __magic_name__       ,     __magic_name__   )
																			UpperCAmelCase_			:      Tuple     						=  self.get_next(__magic_name__   )



							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     int       ,     __magic_name__							:     int   )					->       int:







													"""simple docstring"""
													right -= 1  # Because of right is exclusive
													UpperCAmelCase_			:      List[str]     						=  0
													while left <= right:
																			UpperCAmelCase_			:      Optional[Any]     						=  self.get_prev(__magic_name__   )
																			if left <= current_left:
																									UpperCAmelCase_			:      Dict     						=  max(__magic_name__       ,     self.tree[right]   )
																									UpperCAmelCase_			:      Optional[Any]     						=  current_left
																			else:
																									UpperCAmelCase_			:      str     						=  max(__magic_name__       ,     self.arr[right]   )
																									right -= 1
													return result


if __name__ == "__main__":
					import doctest

					doctest.testmod()





