






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


      '''simple docstring'''






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







 def  __lowercase (		self :       Any						):


      '''simple docstring'''






      UpperCamelCase__      :       Union[str, Any]	    =  ids_tensor([self.batch_size, self.seq_length]							,       self.vocab_size						)

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

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

      UpperCamelCase__      :       Dict	    =  self.get_config()

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







 def  __lowercase (		self :       str						):


      '''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  __lowercase (		self :       Union[str, Any]							,       SCREAMING_SNAKE_CASE :       Tuple							,       SCREAMING_SNAKE_CASE :       Union[str, Any]							,       SCREAMING_SNAKE_CASE :       List[Any]							,       SCREAMING_SNAKE_CASE :       Tuple							,       SCREAMING_SNAKE_CASE :       List[str]							,       SCREAMING_SNAKE_CASE :       List[Any]						):


      '''simple docstring'''






      UpperCamelCase__      :       List[Any]	    =  EsmModel(config=SCREAMING_SNAKE_CASE						)
      model.to(SCREAMING_SNAKE_CASE						)
      model.eval()
      UpperCamelCase__      :       Tuple	    =  model(SCREAMING_SNAKE_CASE							,       attention_mask=SCREAMING_SNAKE_CASE						)
      UpperCamelCase__      :       Optional[int]	    =  model(SCREAMING_SNAKE_CASE						)
      UpperCamelCase__      :       Dict	    =  model(SCREAMING_SNAKE_CASE						)

      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  __lowercase (		self :       List[Any]							,       SCREAMING_SNAKE_CASE :       Union[str, Any]							,       SCREAMING_SNAKE_CASE :       List[Any]							,       SCREAMING_SNAKE_CASE :       Dict							,       SCREAMING_SNAKE_CASE :       Optional[int]							,       SCREAMING_SNAKE_CASE :       str							,       SCREAMING_SNAKE_CASE :       str						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[Any]	    =  EsmForMaskedLM(config=SCREAMING_SNAKE_CASE						)
      model.to(SCREAMING_SNAKE_CASE						)
      model.eval()
      UpperCamelCase__      :       List[str]	    =  model(SCREAMING_SNAKE_CASE							,       attention_mask=SCREAMING_SNAKE_CASE							,       labels=SCREAMING_SNAKE_CASE						)
      self.parent.assertEqual(result.logits.shape							,       (self.batch_size, self.seq_length, self.vocab_size)						)







 def  __lowercase (		self :       Optional[int]							,       SCREAMING_SNAKE_CASE :       Dict							,       SCREAMING_SNAKE_CASE :       Tuple							,       SCREAMING_SNAKE_CASE :       Union[str, Any]							,       SCREAMING_SNAKE_CASE :       Tuple							,       SCREAMING_SNAKE_CASE :       Dict							,       SCREAMING_SNAKE_CASE :       Dict						):


      '''simple docstring'''






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







 def  __lowercase (		self :       Tuple						):


      '''simple docstring'''






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







@require_torch
class       __a  (   A__	,       A__	,       unittest.TestCase	):
 _lowerCAmelCase	:	List[str]				=			False

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







 def  __lowercase (		self :       List[str]						):


      '''simple docstring'''






      UpperCamelCase__      :       Dict	    =  EsmModelTester(self						)
      UpperCamelCase__      :       List[Any]	    =  ConfigTester(self							,       config_class=SCREAMING_SNAKE_CASE							,       hidden_size=37						)







 def  __lowercase (		self :       Any						):


      '''simple docstring'''






      self.config_tester.run_common_tests()







 def  __lowercase (		self :       Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[Any]	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE						)







 def  __lowercase (		self :       List[str]						):


      '''simple docstring'''






      UpperCamelCase__      :       str	    =  self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
           UpperCamelCase__      :       int	    =  type
           self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE						)







 def  __lowercase (		self :       Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[int]	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE						)







 def  __lowercase (		self :       int						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[int]	    =  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE						)







 @slow
 def  __lowercase (		self :       int						):


      '''simple docstring'''






      for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           UpperCamelCase__      :       Optional[int]	    =  EsmModel.from_pretrained(SCREAMING_SNAKE_CASE						)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE						)







 def  __lowercase (		self :       Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__      :       Optional[Any]	    =  self.model_tester.prepare_config_and_inputs()[0]
      UpperCamelCase__      :       Any	    =  EsmEmbeddings(config=SCREAMING_SNAKE_CASE						)

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







 def  __lowercase (		self :       int						):


      '''simple docstring'''






      UpperCamelCase__      :       List[Any]	    =  self.model_tester.prepare_config_and_inputs()[0]
      UpperCamelCase__      :       Union[str, Any]	    =  EsmEmbeddings(config=SCREAMING_SNAKE_CASE						)

      UpperCamelCase__      :       Union[str, Any]	    =  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__      :       List[str]	    =  embeddings.create_position_ids_from_inputs_embeds(SCREAMING_SNAKE_CASE						)
      self.assertEqual(position_ids.shape							,       expected_positions.shape						)
      self.assertTrue(torch.all(torch.eq(SCREAMING_SNAKE_CASE							,       SCREAMING_SNAKE_CASE						)						)						)







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


      '''simple docstring'''






      pass







 @unittest.skip("Esm does not support embedding resizing"						)
 def  __lowercase (		self :       Optional[Any]						):


      '''simple docstring'''






      pass







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


      '''simple docstring'''






      pass







@require_torch
class       __a  (   A__	):







 @slow
 def  __lowercase (		self :       Tuple						):


      '''simple docstring'''






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

           UpperCamelCase__      :       int	    =  33

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

           UpperCamelCase__      :       str	    =  torch.tensor(
               [[[8.9_2_1_5, -1_0.5_8_9_8, -6.4_6_7_1], [-6.3_9_6_7, -1_3.9_1_1_4, -1.1_2_1_2], [-7.7_8_1_2, -1_3.9_5_1_6, -3.7_4_0_6]]]						)
           self.assertTrue(torch.allclose(output[:, :3, :3]							,       SCREAMING_SNAKE_CASE							,       atol=1e-4						)						)







 @slow
 def  __lowercase (		self :       Optional[Any]						):


      '''simple docstring'''






      with torch.no_grad():
           UpperCamelCase__      :       List[str]	    =  EsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"						)
           model.eval()

           UpperCamelCase__      :       int	    =  torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]						)
           UpperCamelCase__      :       Optional[int]	    =  model(SCREAMING_SNAKE_CASE						)[0]
           # compare the actual values for a slice.
           UpperCamelCase__      :       Dict	    =  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]							,       SCREAMING_SNAKE_CASE							,       atol=1e-4						)						)







def 		SCREAMING_SNAKE_CASE							( __lowerCAmelCase ,   __lowerCAmelCase      )			->     Any:
     # "extended trapezoidal rule"
     # int(f) = dx/2 * (f1 + 2f2 + ... + fn)
     UpperCamelCase__      :       int	    =  (boundary[1] - boundary[0]) / steps
     UpperCamelCase__      :       Optional[Any]	    =  boundary[0]
     UpperCamelCase__      :       List[Any]	    =  boundary[1]
     UpperCamelCase__      :       List[Any]	    =  make_points(__lowerCAmelCase ,   __lowerCAmelCase ,   __lowerCAmelCase      )
     UpperCamelCase__      :       int	    =  0.0
     y += (h / 2.0) * f(__lowerCAmelCase      )
     for i in x_i:
          # print(i)
          y += h * f(__lowerCAmelCase      )
     y += (h / 2.0) * f(__lowerCAmelCase      )
     return y







def 		SCREAMING_SNAKE_CASE							( __lowerCAmelCase ,   __lowerCAmelCase ,   __lowerCAmelCase      )			->     List[str]:
     UpperCamelCase__      :       Optional[int]	    =  a + h
     while x < (b - h):
          yield x
          UpperCamelCase__      :       Union[str, Any]	    =  x + h







def 		SCREAMING_SNAKE_CASE							( __lowerCAmelCase      )			->     Any:  # enter your function here
     UpperCamelCase__      :       Dict	    =  (x - 0) * (x - 0)
     return y







def 		SCREAMING_SNAKE_CASE							( )			->     Dict:
     UpperCamelCase__      :       List[Any]	    =  0.0  # Lower bound of integration
     UpperCamelCase__      :       Tuple	    =  1.0  # Upper bound of integration
     UpperCamelCase__      :       Any	    =  1_0.0  # define number of steps or resolution
     UpperCamelCase__      :       List[str]	    =  [a, b]  # define boundary of integration
     UpperCamelCase__      :       Any	    =  method_a(__lowerCAmelCase ,   __lowerCAmelCase      )
     print(f'y = {y}'      )


if __name__ == "__main__":
 main()