




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

     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		,     )						->							List[Any]:
           _A							:     Tuple			=						parent
           _A							:     Any			=						batch_size
           _A							:     int			=						seq_length
           _A							:     List[Any]			=						is_training
           _A							:     List[Any]			=						use_input_mask
           _A							:     int			=						use_token_type_ids
           _A							:     List[Any]			=						use_labels
           _A							:     List[str]			=						vocab_size
           _A							:     Optional[Any]			=						hidden_size
           _A							:     Union[str, Any]			=						num_hidden_layers
           _A							:     int			=						num_attention_heads
           _A							:     str			=						intermediate_size
           _A							:     Optional[int]			=						hidden_act
           _A							:     int			=						hidden_dropout_prob
           _A							:     List[str]			=						attention_probs_dropout_prob
           _A							:     Any			=						max_position_embeddings
           _A							:     List[str]			=						type_vocab_size
           _A							:     Optional[int]			=						type_sequence_label_size
           _A							:     Any			=						initializer_range
           _A							:     Dict			=						num_labels
           _A							:     Union[str, Any]			=						num_choices
           _A							:     str			=						scope

     def 					a__	(  self				)						->							List[str]:
           _A							:     Tuple			=						ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size				)

           _A							:     Optional[int]			=						None
           if self.use_input_mask:
                 _A							:     Tuple			=						random_attention_mask([self.batch_size, self.seq_length]				)

           _A							:     str			=						None
           _A							:     Any			=						None
           _A							:     int			=						None
           if self.use_labels:
                 _A							:     Optional[Any]			=						ids_tensor([self.batch_size]		,     self.type_sequence_label_size				)
                 _A							:     int			=						ids_tensor([self.batch_size, self.seq_length]		,     self.num_labels				)
                 _A							:     int			=						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 					a__	(  self				)						->							Optional[Any]:
           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 					a__	(  self		,     _a		,     _a		,     _a		,     _a		,     _a		,     _a				)						->							Union[str, Any]:
           _A							:     Tuple			=						EsmModel(config=snake_case__				)
           model.to(snake_case__				)
           model.eval()
           _A							:     List[Any]			=						model(snake_case__		,     attention_mask=snake_case__				)
           _A							:     Optional[int]			=						model(snake_case__				)
           _A							:     Tuple			=						model(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 					a__	(  self		,     _a		,     _a		,     _a		,     _a		,     _a		,     _a				)						->							Optional[Any]:
           _A							:     Dict			=						EsmForMaskedLM(config=snake_case__				)
           model.to(snake_case__				)
           model.eval()
           _A							:     int			=						model(snake_case__		,     attention_mask=snake_case__		,     labels=snake_case__				)
           self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)				)

     def 					a__	(  self		,     _a		,     _a		,     _a		,     _a		,     _a		,     _a				)						->							List[str]:
           _A							:     List[str]			=						self.num_labels
           _A							:     Optional[Any]			=						EsmForTokenClassification(config=snake_case__				)
           model.to(snake_case__				)
           model.eval()
           _A							:     Optional[Any]			=						model(snake_case__		,     attention_mask=snake_case__		,     labels=snake_case__				)
           self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)				)



     def 					a__	(  self				)						->							Tuple:
           _A							:     int			=						self.prepare_config_and_inputs()
           (
               _A  
           )							:     Union[str, Any]			=						config_and_inputs
           _A							:     str			=						{"input_ids": input_ids, "attention_mask": input_mask}
           return config, inputs_dict







@require_torch
class 						lowercase							(   UpperCamelCase__,UpperCamelCase__,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 					a__	(  self				)						->							Any:
           _A							:     Optional[int]			=						EsmModelTester(self				)
           _A							:     int			=						ConfigTester(self		,     config_class=snake_case__		,     hidden_size=37				)

     def 					a__	(  self				)						->							Union[str, Any]:
           self.config_tester.run_common_tests()

     def 					a__	(  self				)						->							List[Any]:
           _A							:     Dict			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*snake_case__				)

     def 					a__	(  self				)						->							Tuple:
           _A							:     Optional[int]			=						self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                 _A							:     Optional[Any]			=						type
                 self.model_tester.create_and_check_model(*snake_case__				)

     def 					a__	(  self				)						->							int:
           _A							:     Dict			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*snake_case__				)

     def 					a__	(  self				)						->							Optional[int]:
           _A							:     Optional[Any]			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*snake_case__				)

     @slow
     def 					a__	(  self				)						->							List[Any]:





           for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _A							:     int			=						EsmModel.from_pretrained(snake_case__				)
                 self.assertIsNotNone(snake_case__				)

     def 					a__	(  self				)						->							Union[str, Any]:
           _A							:     Optional[Any]			=						self.model_tester.prepare_config_and_inputs()[0]
           _A							:     int			=						EsmEmbeddings(config=snake_case__				)

           _A							:     Union[str, Any]			=						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							:     Optional[Any]			=						create_position_ids_from_input_ids(snake_case__		,     model.padding_idx				)
           self.assertEqual(position_ids.shape		,     expected_positions.shape				)
           self.assertTrue(torch.all(torch.eq(snake_case__		,     snake_case__				)				)				)

     def 					a__	(  self				)						->							int:
           _A							:     str			=						self.model_tester.prepare_config_and_inputs()[0]
           _A							:     List[str]			=						EsmEmbeddings(config=snake_case__				)

           _A							:     int			=						torch.empty(2		,     4		,     30				)
           _A							:     List[Any]			=						[
               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							:     Optional[int]			=						embeddings.create_position_ids_from_inputs_embeds(snake_case__				)
           self.assertEqual(position_ids.shape		,     expected_positions.shape				)
           self.assertTrue(torch.all(torch.eq(snake_case__		,     snake_case__				)				)				)

     @unittest.skip("""Esm does not support embedding resizing"""				)
     def 					a__	(  self				)						->							str:
           pass

     @unittest.skip("""Esm does not support embedding resizing"""				)
     def 					a__	(  self				)						->							str:
           pass



     @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""				)
     def 					a__	(  self				)						->							Union[str, Any]:
           pass







@require_torch
class 						lowercase							(   UpperCamelCase__		):

     @slow
     def 					a__	(  self				)						->							int:





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

                 _A							:     List[str]			=						33

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

                 _A							:     str			=						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]		,     snake_case__		,     atol=1e-4				)				)



     @slow
     def 					a__	(  self				)						->							Optional[Any]:





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

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





"""simple docstring"""







lowerCAmelCase__      				=   [
    '''Audio''',
    '''Array2D''',
    '''Array3D''',
    '''Array4D''',
    '''Array5D''',
    '''ClassLabel''',
    '''Features''',
    '''Sequence''',
    '''Value''',
    '''Image''',
    '''Translation''',
    '''TranslationVariableLanguages''',
]
from .audio import Audio
from .features import ArrayaD, ArrayaD, ArrayaD, ArrayaD, ClassLabel, Features, Sequence, Value
from .image import Image
from .translation import Translation, TranslationVariableLanguages





