



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     lowerCAmelCase_		:
       def __init__(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_=13,      SCREAMING_SNAKE_CASE_=7,      SCREAMING_SNAKE_CASE_=False,      SCREAMING_SNAKE_CASE_=True,      SCREAMING_SNAKE_CASE_=False,      SCREAMING_SNAKE_CASE_=True,      SCREAMING_SNAKE_CASE_=33,      SCREAMING_SNAKE_CASE_=32,      SCREAMING_SNAKE_CASE_=5,      SCREAMING_SNAKE_CASE_=4,      SCREAMING_SNAKE_CASE_=37,      SCREAMING_SNAKE_CASE_="gelu",      SCREAMING_SNAKE_CASE_=0.1,      SCREAMING_SNAKE_CASE_=0.1,      SCREAMING_SNAKE_CASE_=512,      SCREAMING_SNAKE_CASE_=16,      SCREAMING_SNAKE_CASE_=2,      SCREAMING_SNAKE_CASE_=0.02,      SCREAMING_SNAKE_CASE_=3,      SCREAMING_SNAKE_CASE_=4,      SCREAMING_SNAKE_CASE_=None,      )					->		Dict:
          UpperCamelCase   :  Dict						     =     parent
          UpperCamelCase   :  List[Any]						     =     batch_size
          UpperCamelCase   :  Tuple						     =     seq_length
          UpperCamelCase   :  Optional[Any]						     =     is_training
          UpperCamelCase   :  str						     =     use_input_mask
          UpperCamelCase   :  Optional[int]						     =     use_token_type_ids
          UpperCamelCase   :  List[str]						     =     use_labels
          UpperCamelCase   :  Union[str, Any]						     =     vocab_size
          UpperCamelCase   :  List[Any]						     =     hidden_size
          UpperCamelCase   :  Optional[Any]						     =     num_hidden_layers
          UpperCamelCase   :  Dict						     =     num_attention_heads
          UpperCamelCase   :  Dict						     =     intermediate_size
          UpperCamelCase   :  List[Any]						     =     hidden_act
          UpperCamelCase   :  str						     =     hidden_dropout_prob
          UpperCamelCase   :  str						     =     attention_probs_dropout_prob
          UpperCamelCase   :  Dict						     =     max_position_embeddings
          UpperCamelCase   :  Optional[int]						     =     type_vocab_size
          UpperCamelCase   :  Optional[int]						     =     type_sequence_label_size
          UpperCamelCase   :  List[Any]						     =     initializer_range
          UpperCamelCase   :  Union[str, Any]						     =     num_labels
          UpperCamelCase   :  int						     =     num_choices
          UpperCamelCase   :  Union[str, Any]						     =     scope
       def  snake_case_	(      self	)					->		List[Any]:
          UpperCamelCase   :  int						     =     ids_tensor([self.batch_size, self.seq_length],      self.vocab_size	)

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

          UpperCamelCase   :  Optional[Any]						     =     None
          UpperCamelCase   :  List[Any]						     =     None
          UpperCamelCase   :  Any						     =     None
          if self.use_labels:
             UpperCamelCase   :  Optional[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   :  List[Any]						     =     ids_tensor([self.batch_size],      self.num_choices	)

          UpperCamelCase   :  str						     =     self.get_config()

          return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
       def  snake_case_	(      self	)					->		Tuple:
          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  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)					->		str:
          UpperCamelCase   :  str						     =     EsmModel(config=__A	)
          model.to(__A	)
          model.eval()
          UpperCamelCase   :  Dict						     =     model(__A,      attention_mask=__A	)
          UpperCamelCase   :  Any						     =     model(__A	)
          UpperCamelCase   :  List[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  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)					->		List[str]:
          UpperCamelCase   :  List[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  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)					->		int:
          UpperCamelCase   :  List[Any]						     =     self.num_labels
          UpperCamelCase   :  Optional[int]						     =     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  snake_case_	(      self	)					->		List[Any]:
          UpperCamelCase   :  Any						     =     self.prepare_config_and_inputs()
          (
              UpperCamelCase	
          )   :  Optional[int]						     =     config_and_inputs
          UpperCamelCase   :  Optional[Any]						     =     {"""input_ids""": input_ids, """attention_mask""": input_mask}
          return config, inputs_dict






@require_torch
class     lowerCAmelCase_		(  A__	,   A__	,   unittest.TestCase							):
       UpperCAmelCase__    : Dict       					=  False

       UpperCAmelCase__    : List[Any]       					=  (
           (
               EsmForMaskedLM,
               EsmModel,
               EsmForSequenceClassification,
               EsmForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       UpperCAmelCase__    : Union[str, Any]       					=  ()
       UpperCAmelCase__    : List[str]       					=  (
           {
               "feature-extraction": EsmModel,
               "fill-mask": EsmForMaskedLM,
               "text-classification": EsmForSequenceClassification,
               "token-classification": EsmForTokenClassification,
               "zero-shot": EsmForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       UpperCAmelCase__    : Any       					=  True
       def  snake_case_	(      self	)					->		str:
          UpperCamelCase   :  Any						     =     EsmModelTester(self	)
          UpperCamelCase   :  List[str]						     =     ConfigTester(self,      config_class=__A,      hidden_size=37	)
       def  snake_case_	(      self	)					->		List[Any]:
          self.config_tester.run_common_tests()
       def  snake_case_	(      self	)					->		str:
          UpperCamelCase   :  Union[str, Any]						     =     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__A	)
       def  snake_case_	(      self	)					->		Dict:
          UpperCamelCase   :  Optional[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(*__A	)
       def  snake_case_	(      self	)					->		Tuple:
          UpperCamelCase   :  Any						     =     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*__A	)
       def  snake_case_	(      self	)					->		Any:
          UpperCamelCase   :  Optional[int]						     =     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*__A	)
       @slow
       def  snake_case_	(      self	)					->		Union[str, Any]:
          for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCamelCase   :  Any						     =     EsmModel.from_pretrained(__A	)
             self.assertIsNotNone(__A	)
       def  snake_case_	(      self	)					->		str:
          UpperCamelCase   :  Union[str, Any]						     =     self.model_tester.prepare_config_and_inputs()[0]
          UpperCamelCase   :  Dict						     =     EsmEmbeddings(config=__A	)

          UpperCamelCase   :  Optional[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   :  List[Any]						     =     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  snake_case_	(      self	)					->		List[str]:
          UpperCamelCase   :  Tuple						     =     self.model_tester.prepare_config_and_inputs()[0]
          UpperCamelCase   :  List[str]						     =     EsmEmbeddings(config=__A	)

          UpperCamelCase   :  int						     =     torch.empty(2,      4,      30	)
          UpperCamelCase   :  Tuple						     =     [
              0 + embeddings.padding_idx + 1,
              1 + embeddings.padding_idx + 1,
              2 + embeddings.padding_idx + 1,
              3 + embeddings.padding_idx + 1,
          ]
          UpperCamelCase   :  Dict						     =     torch.as_tensor([expected_single_positions, expected_single_positions]	)
          UpperCamelCase   :  List[str]						     =     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  snake_case_	(      self	)					->		Dict:
          pass
       @unittest.skip('Esm does not support embedding resizing'	)
       def  snake_case_	(      self	)					->		Dict:
          pass






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






@require_torch
class     lowerCAmelCase_		(  A__							):
       @slow
       def  snake_case_	(      self	)					->		List[str]:
          with torch.no_grad():
             UpperCamelCase   :  int						     =     EsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D'	)
             model.eval()
             UpperCamelCase   :  List[Any]						     =     torch.tensor([[0, 1, 2, 3, 4, 5]]	)
             UpperCamelCase   :  Optional[int]						     =     model(__A	)[0]

             UpperCamelCase   :  Optional[Any]						     =     33

             UpperCamelCase   :  Union[str, Any]						     =     torch.Size((1, 6, vocab_size)	)
             self.assertEqual(output.shape,      __A	)

             UpperCamelCase   :  List[str]						     =     torch.tensor(
                 [[[8.92_15, -10.5898, -6.46_71], [-6.39_67, -13.9114, -1.12_12], [-7.78_12, -13.9516, -3.74_06]]]	)
             self.assertTrue(torch.allclose(output[:, :3, :3],      __A,      atol=1e-4	)	)






       @slow
       def  snake_case_	(      self	)					->		List[str]:
          with torch.no_grad():
             UpperCamelCase   :  Any						     =     EsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D'	)
             model.eval()

             UpperCamelCase   :  Dict						     =     torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]	)
             UpperCamelCase   :  Union[str, Any]						     =     model(__A	)[0]
             # compare the actual values for a slice.
             UpperCamelCase   :  List[str]						     =     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 graphs.minimum_spanning_tree_kruskal import kruskal
def 		UpperCamelCase     (       )     ->					Tuple:
   UpperCamelCase   :  List[str]						     =     9
   UpperCamelCase   :  Optional[Any]						     =     [
       [0, 1, 4],
       [0, 7, 8],
       [1, 2, 8],
       [7, 8, 7],
       [7, 6, 1],
       [2, 8, 2],
       [8, 6, 6],
       [2, 3, 7],
       [2, 5, 4],
       [6, 5, 2],
       [3, 5, 14],
       [3, 4, 9],
       [5, 4, 10],
       [1, 7, 11],
   ]

   UpperCamelCase   :  int						     =     kruskal(snake_case__	,					snake_case__     )

   UpperCamelCase   :  List[str]						     =     [
       [7, 6, 1],
       [2, 8, 2],
       [6, 5, 2],
       [0, 1, 4],
       [2, 5, 4],
       [2, 3, 7],
       [0, 7, 8],
       [3, 4, 9],
   ]

   assert sorted(snake_case__     ) == sorted(snake_case__     )



