


'''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 							__UpperCamelCase				:




	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'''





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




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





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

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

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

						__a			:       List[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'''





						__a			:       List[str]					    =  EsmModel(config=__a							)
						model.to(__a							)
						model.eval()
						__a			:       List[str]					    =  model(__a						,				attention_mask=__a							)
						__a			:       Tuple					    =  model(__a							)
						__a			:       Optional[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'''





						__a			:       List[str]					    =  EsmForMaskedLM(config=__a							)
						model.to(__a							)
						model.eval()
						__a			:       Dict					    =  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'''





						__a			:       Any					    =  self.num_labels
						__a			:       Tuple					    =  EsmForTokenClassification(config=__a							)
						model.to(__a							)
						model.eval()
						__a			:       Any					    =  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'''





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





@require_torch
class 							__UpperCamelCase				(		A__		,      A__		,      unittest.TestCase					):
	A_				=    False

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




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       List[Any]					    =  EsmModelTester(self							)
						__a			:       Any					    =  ConfigTester(self						,				config_class=__a						,				hidden_size=37							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						self.config_tester.run_common_tests()




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Dict					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Dict					    =  self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
											__a			:       Union[str, Any]					    =  type
											self.model_tester.create_and_check_model(*__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Tuple					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       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]:
											__a			:       List[str]					    =  EsmModel.from_pretrained(__a							)
											self.assertIsNotNone(__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Union[str, Any]					    =  self.model_tester.prepare_config_and_inputs()[0]
						__a			:       Optional[int]					    =  EsmEmbeddings(config=__a							)

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





						__a			:       Dict					    =  self.model_tester.prepare_config_and_inputs()[0]
						__a			:       Any					    =  EsmEmbeddings(config=__a							)

						__a			:       Dict					    =  torch.empty(2						,				4						,				30							)
						__a			:       List[str]					    =  [
						    0 + embeddings.padding_idx + 1,
						    1 + embeddings.padding_idx + 1,
						    2 + embeddings.padding_idx + 1,
						    3 + embeddings.padding_idx + 1,
						]
						__a			:       Any					    =  torch.as_tensor([expected_single_positions, expected_single_positions]							)
						__a			:       Dict					    =  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 							__UpperCamelCase				(		A__					):




	@slow
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						with torch.no_grad():
											__a			:       Optional[int]					    =  EsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D'							)
											model.eval()
											__a			:       Tuple					    =  torch.tensor([[0, 1, 2, 3, 4, 5]]							)
											__a			:       Dict					    =  model(__a							)[0]

											__a			:       List[str]					    =  33

											__a			:       Tuple					    =  torch.Size((1, 6, vocab_size)							)
											self.assertEqual(output.shape						,				__a							)

											__a			:       Optional[Any]					    =  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       __UpperCAmelCase							(   self							):

						'''simple docstring'''





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

											__a			:       Optional[int]					    =  torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]							)
											__a			:       List[str]					    =  model(__a							)[0]
											# compare the actual values for a slice.
											__a			:       List[Any]					    =  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							)							)











lowerCamelCase	:							List[Any]				=[
    (1000, '''M'''),
    (900, '''CM'''),
    (500, '''D'''),
    (400, '''CD'''),
    (100, '''C'''),
    (90, '''XC'''),
    (50, '''L'''),
    (40, '''XL'''),
    (10, '''X'''),
    (9, '''IX'''),
    (5, '''V'''),
    (4, '''IV'''),
    (1, '''I'''),
]







def 		SCREAMING_SNAKE_CASE							(					__lowerCAmelCase			) ->	int:
     UpperCamelCase__			:   Optional[int]       				=  {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
     UpperCamelCase__			:   Any       				=  0
     UpperCamelCase__			:   Any       				=  0
     while place < len(__lowerCAmelCase			):
          if (place + 1 < len(__lowerCAmelCase			)) and (vals[roman[place]] < vals[roman[place + 1]]):
               total += vals[roman[place + 1]] - vals[roman[place]]
               place += 2
          else:
               total += vals[roman[place]]
               place += 1
     return total







def 		SCREAMING_SNAKE_CASE							(					__lowerCAmelCase			) ->	str:
     UpperCamelCase__			:   Optional[int]       				=  []
     for arabic, roman in ROMAN:
          ((UpperCamelCase__)				,     (UpperCamelCase__))			:   Optional[int]       				=  divmod(__lowerCAmelCase					,    __lowerCAmelCase			)
          result.append(roman * factor			)
          if number == 0:
               break
     return "".join(__lowerCAmelCase			)


if __name__ == "__main__":
 import doctest

 doctest.testmod()