import tempfile
import unittest

from transformers import SPIECE_UNDERLINE, BatchEncoding, PLBartTokenizer, is_torch_available
from transformers.testing_utils import (
    get_tests_dir,
    nested_simplify,
    require_sentencepiece,
    require_tokenizers,
    require_torch,
)

from ...test_tokenization_common import TokenizerTesterMixin


_A   					=    get_tests_dir('fixtures/test_sentencepiece.model')


if is_torch_available():
						from transformers.models.plbart.modeling_plbart import shift_tokens_right

_A   					=    5_0_0_0_3
_A   					=    5_0_0_0_2

@require_sentencepiece
@require_tokenizers
class lowerCamelCase		(_SCREAMING_SNAKE_CASE    ,    unittest.TestCase      ):






					'''simple docstring'''


					a   	=		PLBartTokenizer
					a   	=		None
					a   	=		False



					def 			lowerCAmelCase_					(						self			:				int		)							->       Tuple:
									super().setUp()

									# We have a SentencePiece fixture for testing
									SCREAMING_SNAKE_CASE__		  =				PLBartTokenizer(_snake_case   , language_codes="base"   , keep_accents=_snake_case		)
									tokenizer.save_pretrained(self.tmpdirname		)



					def 			lowerCAmelCase_					(						self			:				str		)							->       Union[str, Any]:
									SCREAMING_SNAKE_CASE__		  =				PLBartTokenizer(_snake_case   , language_codes="base"   , keep_accents=_snake_case		)

									SCREAMING_SNAKE_CASE__		  =				tokenizer.tokenize("This is a test"		)
									self.assertListEqual(_snake_case   , ["▁This", "▁is", "▁a", "▁t", "est"]		)

									self.assertListEqual(
									    tokenizer.convert_tokens_to_ids(_snake_case		)   , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]   , )

									SCREAMING_SNAKE_CASE__		  =				tokenizer.tokenize("I was born in 92000, and this is falsé."		)
									self.assertListEqual(
									    _snake_case   , [
									        SPIECE_UNDERLINE + "I",
									        SPIECE_UNDERLINE + "was",
									        SPIECE_UNDERLINE + "b",
									        "or",
									        "n",
									        SPIECE_UNDERLINE + "in",
									        SPIECE_UNDERLINE + "",
									        "9",
									        "2",
									        "0",
									        "0",
									        "0",
									        ",",
									        SPIECE_UNDERLINE + "and",
									        SPIECE_UNDERLINE + "this",
									        SPIECE_UNDERLINE + "is",
									        SPIECE_UNDERLINE + "f",
									        "al",
									        "s",
									        "é",
									        ".",
									    ]   , )
									SCREAMING_SNAKE_CASE__		  =				tokenizer.convert_tokens_to_ids(_snake_case		)
									self.assertListEqual(
									    _snake_case   , [
									        value + tokenizer.fairseq_offset
									        for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
									    ]   , )

									SCREAMING_SNAKE_CASE__		  =				tokenizer.convert_ids_to_tokens(_snake_case		)
									self.assertListEqual(
									    _snake_case   , [
									        SPIECE_UNDERLINE + "I",
									        SPIECE_UNDERLINE + "was",
									        SPIECE_UNDERLINE + "b",
									        "or",
									        "n",
									        SPIECE_UNDERLINE + "in",
									        SPIECE_UNDERLINE + "",
									        "<unk>",
									        "2",
									        "0",
									        "0",
									        "0",
									        ",",
									        SPIECE_UNDERLINE + "and",
									        SPIECE_UNDERLINE + "this",
									        SPIECE_UNDERLINE + "is",
									        SPIECE_UNDERLINE + "f",
									        "al",
									        "s",
									        "<unk>",
									        ".",
									    ]   , )

									SCREAMING_SNAKE_CASE__		  =				tokenizer.vocab_size
									SCREAMING_SNAKE_CASE__		  =				[tokenizer.convert_ids_to_tokens(_snake_case		) for x in range(end - 4   , _snake_case		)]

									self.assertListEqual(_snake_case   , ["__java__", "__python__", "__en_XX__", "<mask>"]		)

									SCREAMING_SNAKE_CASE__		  =				"java.lang.Exception, python.lang.Exception, javascript, php, ruby, go"
									SCREAMING_SNAKE_CASE__		  =				tokenizer(_snake_case		).input_ids
									self.assertEqual(
									    tokenizer.decode(_snake_case   , skip_special_tokens=_snake_case   , clean_up_tokenization_spaces=_snake_case		)   , _snake_case   , )



					def 			lowerCAmelCase_					(						self			:				str		)							->       Union[str, Any]:
									SCREAMING_SNAKE_CASE__		  =				PLBartTokenizer(_snake_case   , language_codes="multi"   , keep_accents=_snake_case		)

									SCREAMING_SNAKE_CASE__		  =				tokenizer.tokenize("This is a test"		)
									self.assertListEqual(_snake_case   , ["▁This", "▁is", "▁a", "▁t", "est"]		)

									self.assertListEqual(
									    tokenizer.convert_tokens_to_ids(_snake_case		)   , [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]   , )

									SCREAMING_SNAKE_CASE__		  =				tokenizer.tokenize("I was born in 92000, and this is falsé."		)
									self.assertListEqual(
									    _snake_case   , [
									        SPIECE_UNDERLINE + "I",
									        SPIECE_UNDERLINE + "was",
									        SPIECE_UNDERLINE + "b",
									        "or",
									        "n",
									        SPIECE_UNDERLINE + "in",
									        SPIECE_UNDERLINE + "",
									        "9",
									        "2",
									        "0",
									        "0",
									        "0",
									        ",",
									        SPIECE_UNDERLINE + "and",
									        SPIECE_UNDERLINE + "this",
									        SPIECE_UNDERLINE + "is",
									        SPIECE_UNDERLINE + "f",
									        "al",
									        "s",
									        "é",
									        ".",
									    ]   , )
									SCREAMING_SNAKE_CASE__		  =				tokenizer.convert_tokens_to_ids(_snake_case		)
									self.assertListEqual(
									    _snake_case   , [
									        value + tokenizer.fairseq_offset
									        for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
									    ]   , )

									SCREAMING_SNAKE_CASE__		  =				tokenizer.convert_ids_to_tokens(_snake_case		)
									self.assertListEqual(
									    _snake_case   , [
									        SPIECE_UNDERLINE + "I",
									        SPIECE_UNDERLINE + "was",
									        SPIECE_UNDERLINE + "b",
									        "or",
									        "n",
									        SPIECE_UNDERLINE + "in",
									        SPIECE_UNDERLINE + "",
									        "<unk>",
									        "2",
									        "0",
									        "0",
									        "0",
									        ",",
									        SPIECE_UNDERLINE + "and",
									        SPIECE_UNDERLINE + "this",
									        SPIECE_UNDERLINE + "is",
									        SPIECE_UNDERLINE + "f",
									        "al",
									        "s",
									        "<unk>",
									        ".",
									    ]   , )
									SCREAMING_SNAKE_CASE__		  =				tokenizer.vocab_size
									SCREAMING_SNAKE_CASE__		  =				[tokenizer.convert_ids_to_tokens(_snake_case		) for x in range(end - 7   , _snake_case		)]

									self.assertListEqual(
									    _snake_case   , ["__java__", "__python__", "__en_XX__", "__javascript__", "__php__", "__ruby__", "__go__"]		)
									SCREAMING_SNAKE_CASE__		  =				"java.lang.Exception, python.lang.Exception, javascript, php, ruby, go"
									SCREAMING_SNAKE_CASE__		  =				tokenizer(_snake_case		).input_ids
									self.assertEqual(
									    tokenizer.decode(_snake_case   , skip_special_tokens=_snake_case   , clean_up_tokenization_spaces=_snake_case		)   , _snake_case   , )



@require_torch
@require_sentencepiece
@require_tokenizers
class lowerCamelCase		(unittest.TestCase      ):






					'''simple docstring'''


					a   	=		"uclanlp/plbart-python-en_XX"
					a   	=		[
					    "def maximum(a,b,c):NEW_LINE_INDENTreturn max([a,b,c])",
					    "def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])",
					]
					a   	=		[
					    "Returns the maximum value of a b c.",
					    "Sums the values of a b c.",
					]
					a   	=		[
					    1_3_4,
					    5_4_5_2,
					    3_3_4_6_0,
					    3_3_4_4_1,
					    3_3_4_6_3,
					    3_3_4_6_5,
					    3_3_4_6_3,
					    3_3_4_4_9,
					    9_8_8,
					    2_0,
					    3_3_4_5_6,
					    1_9,
					    3_3_4_5_6,
					    7_7_1,
					    3_9,
					    4_2_5_8,
					    8_8_9,
					    3_3_1_8,
					    3_3_4_4_1,
					    3_3_4_6_3,
					    3_3_4_6_5,
					    3_3_4_6_3,
					    3_3_4_4_9,
					    2_4_7_1,
					    2,
					    PYTHON_CODE,
					]



					@classmethod
					def 			lowerCAmelCase_					(						cls			:				int		)							->       Dict:
									SCREAMING_SNAKE_CASE__		  =				PLBartTokenizer.from_pretrained(
									    cls.checkpoint_name   , language_codes="base"   , src_lang="python"   , tgt_lang="en_XX"		)
									SCREAMING_SNAKE_CASE__		  =				1
									return cls



					def 			lowerCAmelCase_					(						self			:				Union[str, Any]		)							->       List[Any]:
									self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["__java__"]   , 50001		)
									self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["__python__"]   , 50002		)
									self.assertEqual(self.tokenizer.fairseq_tokens_to_ids["__en_XX__"]   , 50003		)



					def 			lowerCAmelCase_					(						self			:				List[Any]		)							->       Any:
									SCREAMING_SNAKE_CASE__		  =				self.tokenizer.batch_encode_plus(self.src_text		).input_ids[0]
									self.assertListEqual(self.expected_src_tokens   , _snake_case		)



					def 			lowerCAmelCase_					(						self			:				Optional[Any]		)							->       Optional[int]:
									self.assertIn(_snake_case   , self.tokenizer.all_special_ids		)
									SCREAMING_SNAKE_CASE__		  =				[EN_CODE, 9037, 33442, 57, 752, 153, 14, 56, 18, 9, 2]
									SCREAMING_SNAKE_CASE__		  =				self.tokenizer.decode(_snake_case   , skip_special_tokens=_snake_case		)
									SCREAMING_SNAKE_CASE__		  =				self.tokenizer.decode(generated_ids[1:]   , skip_special_tokens=_snake_case		)
									self.assertEqual(_snake_case   , _snake_case		)
									self.assertNotIn(self.tokenizer.eos_token   , _snake_case		)



					def 			lowerCAmelCase_					(						self			:				Any		)							->       int:
									SCREAMING_SNAKE_CASE__		  =				["def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])" * 20]
									self.assertIsInstance(src_text[0]   , _snake_case		)
									SCREAMING_SNAKE_CASE__		  =				10
									SCREAMING_SNAKE_CASE__		  =				self.tokenizer(_snake_case   , max_length=_snake_case   , truncation=_snake_case		).input_ids[0]
									self.assertEqual(ids[-2]   , 2		)
									self.assertEqual(ids[-1]   , _snake_case		)
									self.assertEqual(len(_snake_case		)   , _snake_case		)



					def 			lowerCAmelCase_					(						self			:				Optional[int]		)							->       List[Any]:
									self.assertListEqual(self.tokenizer.convert_tokens_to_ids(["<mask>", "__java__"]		)   , [50004, 50001]		)



					def 			lowerCAmelCase_					(						self			:				Optional[Any]		)							->       str:
									SCREAMING_SNAKE_CASE__		  =				tempfile.mkdtemp()
									SCREAMING_SNAKE_CASE__		  =				self.tokenizer.fairseq_tokens_to_ids
									self.tokenizer.save_pretrained(_snake_case		)
									SCREAMING_SNAKE_CASE__		  =				PLBartTokenizer.from_pretrained(_snake_case		)
									self.assertDictEqual(new_tok.fairseq_tokens_to_ids   , _snake_case		)



					@require_torch
					def 			lowerCAmelCase_					(						self			:				Tuple		)							->       List[str]:
									SCREAMING_SNAKE_CASE__		  =				self.tokenizer(self.src_text   , text_target=self.tgt_text   , padding=_snake_case   , return_tensors="pt"		)
									SCREAMING_SNAKE_CASE__		  =				shift_tokens_right(batch["labels"]   , self.tokenizer.pad_token_id		)

									# fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4
									self.assertEqual(batch.input_ids[1][-2:].tolist()   , [2, PYTHON_CODE]		)
									self.assertEqual(batch.decoder_input_ids[1][0]   , _snake_case		)
									self.assertEqual(batch.decoder_input_ids[1][-1]   , 2		)
									self.assertEqual(batch.labels[1][-2:].tolist()   , [2, EN_CODE]		)



					@require_torch
					def 			lowerCAmelCase_					(						self			:				Dict		)							->       str:
									SCREAMING_SNAKE_CASE__		  =				self.tokenizer(
									    self.src_text   , text_target=self.tgt_text   , padding=_snake_case   , truncation=_snake_case   , max_length=len(self.expected_src_tokens		)   , return_tensors="pt"   , )
									SCREAMING_SNAKE_CASE__		  =				shift_tokens_right(batch["labels"]   , self.tokenizer.pad_token_id		)

									self.assertIsInstance(_snake_case   , _snake_case		)

									self.assertEqual((2, 26)   , batch.input_ids.shape		)
									self.assertEqual((2, 26)   , batch.attention_mask.shape		)
									SCREAMING_SNAKE_CASE__		  =				batch.input_ids.tolist()[0]
									self.assertListEqual(self.expected_src_tokens   , _snake_case		)
									self.assertEqual(2   , batch.decoder_input_ids[0, -1]		)  # EOS
									# Test that special tokens are reset
									self.assertEqual(self.tokenizer.prefix_tokens   , []		)
									self.assertEqual(self.tokenizer.suffix_tokens   , [self.tokenizer.eos_token_id, PYTHON_CODE]		)



					def 			lowerCAmelCase_					(						self			:				Optional[int]		)							->       List[Any]:
									SCREAMING_SNAKE_CASE__		  =				self.tokenizer(self.src_text   , padding=_snake_case   , truncation=_snake_case   , max_length=3   , return_tensors="pt"		)
									SCREAMING_SNAKE_CASE__		  =				self.tokenizer(
									    text_target=self.tgt_text   , padding=_snake_case   , truncation=_snake_case   , max_length=10   , return_tensors="pt"		)
									SCREAMING_SNAKE_CASE__		  =				targets["input_ids"]
									SCREAMING_SNAKE_CASE__		  =				shift_tokens_right(_snake_case   , self.tokenizer.pad_token_id		)

									self.assertEqual(batch.input_ids.shape[1]   , 3		)
									self.assertEqual(batch.decoder_input_ids.shape[1]   , 10		)



					@require_torch
					def 			lowerCAmelCase_					(						self			:				Optional[int]		)							->       Optional[Any]:
									SCREAMING_SNAKE_CASE__		  =				self.tokenizer._build_translation_inputs(
									    "A test"   , return_tensors="pt"   , src_lang="en_XX"   , tgt_lang="java"		)

									self.assertEqual(
									    nested_simplify(_snake_case		)   , {
									        # A, test, EOS, en_XX
									        "input_ids": [[150, 242, 2, 50003]],
									        "attention_mask": [[1, 1, 1, 1]],
									        # java
									        "forced_bos_token_id": 50001,
									    }   , )





"""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 lowerCamelCase		:






					'''simple docstring'''


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



					def 			lowerCAmelCase_					(						self			:				Union[str, Any]		)							->       str:
									SCREAMING_SNAKE_CASE__		  =				ids_tensor([self.batch_size, self.seq_length]   , self.vocab_size		)

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

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

									SCREAMING_SNAKE_CASE__		  =				self.get_config()

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



					def 			lowerCAmelCase_					(						self			:				Tuple		)							->       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 			lowerCAmelCase_					(						self			:				str   , _snake_case			:				int   , _snake_case			:				Dict   , _snake_case			:				Union[str, Any]   , _snake_case			:				List[str]   , _snake_case			:				Any   , _snake_case			:				List[str]		)							->       Optional[int]:
									SCREAMING_SNAKE_CASE__		  =				EsmModel(config=_snake_case		)
									model.to(_snake_case		)
									model.eval()
									SCREAMING_SNAKE_CASE__		  =				model(_snake_case   , attention_mask=_snake_case		)
									SCREAMING_SNAKE_CASE__		  =				model(_snake_case		)
									SCREAMING_SNAKE_CASE__		  =				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 			lowerCAmelCase_					(						self			:				Tuple   , _snake_case			:				Union[str, Any]   , _snake_case			:				Optional[Any]   , _snake_case			:				Dict   , _snake_case			:				List[Any]   , _snake_case			:				Optional[int]   , _snake_case			:				int		)							->       int:
									SCREAMING_SNAKE_CASE__		  =				EsmForMaskedLM(config=_snake_case		)
									model.to(_snake_case		)
									model.eval()
									SCREAMING_SNAKE_CASE__		  =				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 			lowerCAmelCase_					(						self			:				int   , _snake_case			:				List[Any]   , _snake_case			:				Union[str, Any]   , _snake_case			:				Tuple   , _snake_case			:				List[str]   , _snake_case			:				Dict   , _snake_case			:				Tuple		)							->       Dict:
									SCREAMING_SNAKE_CASE__		  =				self.num_labels
									SCREAMING_SNAKE_CASE__		  =				EsmForTokenClassification(config=_snake_case		)
									model.to(_snake_case		)
									model.eval()
									SCREAMING_SNAKE_CASE__		  =				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 			lowerCAmelCase_					(						self			:				Dict		)							->       List[Any]:
									SCREAMING_SNAKE_CASE__		  =				self.prepare_config_and_inputs()
									(
									    (
									    SCREAMING_SNAKE_CASE__
									)  ,       (
									    SCREAMING_SNAKE_CASE__
									)  ,       (
									    SCREAMING_SNAKE_CASE__
									)  ,       (
									    SCREAMING_SNAKE_CASE__
									)  ,       (
									    SCREAMING_SNAKE_CASE__
									)  ,       (
									    SCREAMING_SNAKE_CASE__
									)  ,       
									)		  =				config_and_inputs
									SCREAMING_SNAKE_CASE__		  =				{"input_ids": input_ids, "attention_mask": input_mask}
									return config, inputs_dict



@require_torch
class lowerCamelCase		(_SCREAMING_SNAKE_CASE    ,    _SCREAMING_SNAKE_CASE    ,    unittest.TestCase      ):






					'''simple docstring'''


					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 			lowerCAmelCase_					(						self			:				List[str]		)							->       Tuple:
									SCREAMING_SNAKE_CASE__		  =				EsmModelTester(self		)
									SCREAMING_SNAKE_CASE__		  =				ConfigTester(self   , config_class=_snake_case   , hidden_size=37		)



					def 			lowerCAmelCase_					(						self			:				str		)							->       List[str]:
									self.config_tester.run_common_tests()



					def 			lowerCAmelCase_					(						self			:				str		)							->       Dict:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_snake_case		)



					def 			lowerCAmelCase_					(						self			:				Any		)							->       List[str]:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
													SCREAMING_SNAKE_CASE__		  =				type
													self.model_tester.create_and_check_model(*_snake_case		)



					def 			lowerCAmelCase_					(						self			:				str		)							->       Optional[Any]:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_snake_case		)



					def 			lowerCAmelCase_					(						self			:				Tuple		)							->       str:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_snake_case		)



					@slow
					def 			lowerCAmelCase_					(						self			:				Tuple		)							->       List[Any]:
									for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													SCREAMING_SNAKE_CASE__		  =				EsmModel.from_pretrained(_snake_case		)
													self.assertIsNotNone(_snake_case		)



					def 			lowerCAmelCase_					(						self			:				Union[str, Any]		)							->       str:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()[0]
									SCREAMING_SNAKE_CASE__		  =				EsmEmbeddings(config=_snake_case		)

									SCREAMING_SNAKE_CASE__		  =				torch.as_tensor([[12, 31, 13, model.padding_idx]]		)
									SCREAMING_SNAKE_CASE__		  =				torch.as_tensor(
									    [
									        [
									            0 + model.padding_idx + 1,
									            1 + model.padding_idx + 1,
									            2 + model.padding_idx + 1,
									            model.padding_idx,
									        ]
									    ]		)
									SCREAMING_SNAKE_CASE__		  =				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 			lowerCAmelCase_					(						self			:				Dict		)							->       List[str]:
									SCREAMING_SNAKE_CASE__		  =				self.model_tester.prepare_config_and_inputs()[0]
									SCREAMING_SNAKE_CASE__		  =				EsmEmbeddings(config=_snake_case		)

									SCREAMING_SNAKE_CASE__		  =				torch.empty(2   , 4   , 30		)
									SCREAMING_SNAKE_CASE__		  =				[
									    0 + embeddings.padding_idx + 1,
									    1 + embeddings.padding_idx + 1,
									    2 + embeddings.padding_idx + 1,
									    3 + embeddings.padding_idx + 1,
									]
									SCREAMING_SNAKE_CASE__		  =				torch.as_tensor([expected_single_positions, expected_single_positions]		)
									SCREAMING_SNAKE_CASE__		  =				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 			lowerCAmelCase_					(						self			:				List[str]		)							->       int:
									pass



					@unittest.skip("Esm does not support embedding resizing"		)
					def 			lowerCAmelCase_					(						self			:				Any		)							->       Tuple:
									pass



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



@require_torch
class lowerCamelCase		(_SCREAMING_SNAKE_CASE      ):






					'''simple docstring'''


					@slow
					def 			lowerCAmelCase_					(						self			:				Dict		)							->       str:
									with torch.no_grad():
													SCREAMING_SNAKE_CASE__		  =				EsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"		)
													model.eval()
													SCREAMING_SNAKE_CASE__		  =				torch.tensor([[0, 1, 2, 3, 4, 5]]		)
													SCREAMING_SNAKE_CASE__		  =				model(_snake_case		)[0]

													SCREAMING_SNAKE_CASE__		  =				33

													SCREAMING_SNAKE_CASE__		  =				torch.Size((1, 6, vocab_size)		)
													self.assertEqual(output.shape   , _snake_case		)

													SCREAMING_SNAKE_CASE__		  =				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 			lowerCAmelCase_					(						self			:				Union[str, Any]		)							->       Any:
									with torch.no_grad():
													SCREAMING_SNAKE_CASE__		  =				EsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"		)
													model.eval()

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




