



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



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=13  ,UpperCAmelCase_=7  ,UpperCAmelCase_=False  ,UpperCAmelCase_=True  ,UpperCAmelCase_=False  ,UpperCAmelCase_=True  ,UpperCAmelCase_=33  ,UpperCAmelCase_=32  ,UpperCAmelCase_=5  ,UpperCAmelCase_=4  ,UpperCAmelCase_=37  ,UpperCAmelCase_="gelu"  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=512  ,UpperCAmelCase_=16  ,UpperCAmelCase_=2  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=3  ,UpperCAmelCase_=4  ,UpperCAmelCase_=None  ,)	->				Tuple:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    seq_length
           lowercase__									=    is_training
           lowercase__									=    use_input_mask
           lowercase__									=    use_token_type_ids
           lowercase__									=    use_labels
           lowercase__									=    vocab_size
           lowercase__									=    hidden_size
           lowercase__									=    num_hidden_layers
           lowercase__									=    num_attention_heads
           lowercase__									=    intermediate_size
           lowercase__									=    hidden_act
           lowercase__									=    hidden_dropout_prob
           lowercase__									=    attention_probs_dropout_prob
           lowercase__									=    max_position_embeddings
           lowercase__									=    type_vocab_size
           lowercase__									=    type_sequence_label_size
           lowercase__									=    initializer_range
           lowercase__									=    num_labels
           lowercase__									=    num_choices
           lowercase__									=    scope



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size   )

           lowercase__									=    None
           if self.use_input_mask:
               lowercase__									=    random_attention_mask([self.batch_size, self.seq_length]   )

           lowercase__									=    None
           lowercase__									=    None
           lowercase__									=    None
           if self.use_labels:
               lowercase__									=    ids_tensor([self.batch_size]  ,self.type_sequence_label_size   )
               lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.num_labels   )
               lowercase__									=    ids_tensor([self.batch_size]  ,self.num_choices   )

           lowercase__									=    self.get_config()

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



       def 				_a   (					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 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Union[str, Any]:
           lowercase__									=    EsmModel(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(UpperCAmelCase_  ,attention_mask=UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_   )

           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  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				str:
           lowercase__									=    EsmForMaskedLM(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(UpperCAmelCase_  ,attention_mask=UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.vocab_size)   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Any:
           lowercase__									=    self.num_labels
           lowercase__									=    EsmForTokenClassification(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    model(UpperCAmelCase_  ,attention_mask=UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.num_labels)   )







       def 				_a   (					self   )	->				Optional[int]:
           lowercase__									=    self.prepare_config_and_inputs()
           (
               (
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							
           )									=    config_and_inputs
           lowercase__									=    {"input_ids": input_ids, "attention_mask": input_mask}
           return config, inputs_dict





@require_torch
class 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:List[Any]     =       False

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



       def 				_a   (					self   )	->				Optional[int]:
           lowercase__									=    EsmModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,hidden_size=37   )



       def 				_a   (					self   )	->				Any:
           self.config_tester.run_common_tests()



       def 				_a   (					self   )	->				List[str]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Union[str, Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
               lowercase__									=    type
               self.model_tester.create_and_check_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				int:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				List[Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase_   )



       @slow
       def 				_a   (					self   )	->				List[str]:
           for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__									=    EsmModel.from_pretrained(UpperCAmelCase_   )
               self.assertIsNotNone(UpperCAmelCase_   )



       def 				_a   (					self   )	->				Tuple:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()[0]
           lowercase__									=    EsmEmbeddings(config=UpperCAmelCase_   )

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



       def 				_a   (					self   )	->				Union[str, Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()[0]
           lowercase__									=    EsmEmbeddings(config=UpperCAmelCase_   )

           lowercase__									=    torch.empty(2  ,4  ,30   )
           lowercase__									=    [
               0 + embeddings.padding_idx + 1,
               1 + embeddings.padding_idx + 1,
               2 + embeddings.padding_idx + 1,
               3 + embeddings.padding_idx + 1,
           ]
           lowercase__									=    torch.as_tensor([expected_single_positions, expected_single_positions]   )
           lowercase__									=    embeddings.create_position_ids_from_inputs_embeds(UpperCAmelCase_   )
           self.assertEqual(position_ids.shape  ,expected_positions.shape   )
           self.assertTrue(torch.all(torch.eq(UpperCAmelCase_  ,UpperCAmelCase_   )   )   )



       @unittest.skip("Esm does not support embedding resizing"   )
       def 				_a   (					self   )	->				int:
           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 					snake_case			(UpperCamelCase ):



       @slow
       def 				_a   (					self   )	->				Tuple:
           with torch.no_grad():
               lowercase__									=    EsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"   )
               model.eval()
               lowercase__									=    torch.tensor([[0, 1, 2, 3, 4, 5]]   )
               lowercase__									=    model(UpperCAmelCase_   )[0]

               lowercase__									=    33

               lowercase__									=    torch.Size((1, 6, vocab_size)   )
               self.assertEqual(output.shape  ,UpperCAmelCase_   )

               lowercase__									=    torch.tensor(
                   [[[8.92_15, -10.58_98, -6.46_71], [-6.39_67, -13.91_14, -1.12_12], [-7.78_12, -13.95_16, -3.74_06]]]   )
               self.assertTrue(torch.allclose(output[:, :3, :3]  ,UpperCAmelCase_  ,atol=1E-4   )   )







       @slow
       def 				_a   (					self   )	->				Optional[int]:
           with torch.no_grad():
               lowercase__									=    EsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"   )
               model.eval()

               lowercase__									=    torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]   )
               lowercase__									=    model(UpperCAmelCase_   )[0]
               # compare the actual values for a slice.
               lowercase__									=    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]  ,UpperCAmelCase_  ,atol=1E-4   )   )









'''simple docstring'''





import json
import os
import unittest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class 					snake_case			(UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:str     =       MgpstrTokenizer
       lowerCAmelCase__	:Tuple     =       False
       lowerCAmelCase__	:Any     =       {}
       lowerCAmelCase__	:List[Any]     =       False



       def 				_a   (					self   )	->				Union[str, Any]:
           super().setUp()

           # fmt: off
           lowercase__									=    ["[GO]", "[s]", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
           # fmt: on
           lowercase__									=    dict(zip(UpperCAmelCase_  ,range(len(UpperCAmelCase_   )   )   )   )

           lowercase__									=    os.path.join(self.tmpdirname  ,VOCAB_FILES_NAMES["vocab_file"]   )
           with open(self.vocab_file  ,"w"  ,encoding="utf-8"   ) as fp:
               fp.write(json.dumps(UpperCAmelCase_   ) + "\n"   )



       def 				_a   (					self  ,**UpperCAmelCase_   )	->				Any:
           return MgpstrTokenizer.from_pretrained(self.tmpdirname  ,**UpperCAmelCase_   )



       def 				_a   (					self  ,UpperCAmelCase_   )	->				Dict:
           lowercase__									=    "tester"
           lowercase__									=    "tester"
           return input_text, output_text



       @unittest.skip("MGP-STR always lower cases letters."   )
       def 				_a   (					self   )	->				Dict:
           pass



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__									=    self.get_tokenizers(do_lower_case=UpperCAmelCase_   )
           for tokenizer in tokenizers:
               with self.subTest(F'''{tokenizer.__class__.__name__}'''   ):
                   lowercase__									=    "[SPECIAL_TOKEN]"

                   tokenizer.add_special_tokens({"cls_token": special_token}   )
                   lowercase__									=    tokenizer.encode([special_token]  ,add_special_tokens=UpperCAmelCase_   )
                   self.assertEqual(len(UpperCAmelCase_   )  ,1   )

                   lowercase__									=    tokenizer.decode(UpperCAmelCase_  ,skip_special_tokens=UpperCAmelCase_   )
                   self.assertTrue(special_token not in decoded   )



       def 				_a   (					self   )	->				str:
           lowercase__									=    self.get_tokenizers()
           for tokenizer in tokenizers:
               with self.subTest(F'''{tokenizer.__class__.__name__}'''   ):
                   lowercase__							,							lowercase__									=    self.get_input_output_texts(UpperCAmelCase_   )

                   lowercase__									=    tokenizer.tokenize(UpperCAmelCase_   )
                   lowercase__									=    tokenizer.convert_tokens_to_ids(UpperCAmelCase_   )
                   lowercase__									=    tokenizer.encode(UpperCAmelCase_  ,add_special_tokens=UpperCAmelCase_   )
                   self.assertListEqual(UpperCAmelCase_  ,UpperCAmelCase_   )

                   lowercase__									=    tokenizer.convert_ids_to_tokens(UpperCAmelCase_   )
                   self.assertNotEqual(len(UpperCAmelCase_   )  ,0   )
                   lowercase__									=    tokenizer.decode(UpperCAmelCase_   )
                   self.assertIsInstance(UpperCAmelCase_  ,UpperCAmelCase_   )

                   self.assertEqual(text_a.replace(" "  ,""   )  ,UpperCAmelCase_   )



       @unittest.skip("MGP-STR tokenizer only handles one sequence."   )
       def 				_a   (					self   )	->				Optional[int]:
           pass







       @unittest.skip("inputs cannot be pretokenized in MgpstrTokenizer"   )
       def 				_a   (					self   )	->				Any:
           pass




