





import json
import os
import sys
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from huggingface_hub import HfFolder, Repository, create_repo, delete_repo
from requests.exceptions import HTTPError

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    PROCESSOR_MAPPING,
    TOKENIZER_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    AutoProcessor,
    AutoTokenizer,
    BertTokenizer,
    ProcessorMixin,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
)
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test
from transformers.tokenization_utils import TOKENIZER_CONFIG_FILE
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_tokenizers_available


sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils'''))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402
from test_module.custom_processing import CustomProcessor  # noqa E402
from test_module.custom_tokenization import CustomTokenizer  # noqa E402


A   : Union[str, Any]			=				get_tests_dir('''fixtures/dummy_feature_extractor_config.json''')
A   : Union[str, Any]			=				get_tests_dir('''fixtures/vocab.json''')
A   : Tuple			=				get_tests_dir('''fixtures''')


class   A      (unittest.TestCase   ):
			'''simple docstring'''




			__lowerCamelCase     :	Tuple          =   ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''bla''', '''blou''']






			def        a_	(      self       :   Optional[Any]       )     ->     List[str]:







					"""simple docstring"""






					A__  				=       0






			def        a_	(      self       :   List[str]       )     ->     Tuple:







					"""simple docstring"""






					A__  				=       AutoProcessor.from_pretrained("""facebook/wav2vec2-base-960h"""       )
					self.assertIsInstance(__lowerCAmelCase							,     __lowerCAmelCase       )






			def        a_	(      self       :   int       )     ->     int:







					"""simple docstring"""






					with tempfile.TemporaryDirectory() as tmpdirname:
							A__  				=       WavaVecaConfig()
							A__  				=       AutoProcessor.from_pretrained("""facebook/wav2vec2-base-960h"""       )

							# save in new folder
							model_config.save_pretrained(__lowerCAmelCase       )
							processor.save_pretrained(__lowerCAmelCase       )

							A__  				=       AutoProcessor.from_pretrained(__lowerCAmelCase       )

					self.assertIsInstance(__lowerCAmelCase							,     __lowerCAmelCase       )






			def        a_	(      self       :   Dict       )     ->     Dict:







					"""simple docstring"""






					with tempfile.TemporaryDirectory() as tmpdirname:
							# copy relevant files
							copyfile(__lowerCAmelCase							,     os.path.join(__lowerCAmelCase							,     __lowerCAmelCase       )       )
							copyfile(__lowerCAmelCase							,     os.path.join(__lowerCAmelCase							,     """vocab.json"""       )       )

							A__  				=       AutoProcessor.from_pretrained(__lowerCAmelCase       )

					self.assertIsInstance(__lowerCAmelCase							,     __lowerCAmelCase       )






			def        a_	(      self       :   List[str]       )     ->     Dict:







					"""simple docstring"""






					with tempfile.TemporaryDirectory() as tmpdirname:
							A__  				=       WavaVecaFeatureExtractor()
							A__  				=       AutoTokenizer.from_pretrained("""facebook/wav2vec2-base-960h"""       )

							A__  				=       WavaVecaProcessor(__lowerCAmelCase							,     __lowerCAmelCase       )

							# save in new folder
							processor.save_pretrained(__lowerCAmelCase       )

							# drop `processor_class` in tokenizer
							with open(os.path.join(__lowerCAmelCase							,     __lowerCAmelCase       )							,     """r"""       ) as f:
									A__  				=       json.load(__lowerCAmelCase       )
									config_dict.pop("""processor_class"""       )

							with open(os.path.join(__lowerCAmelCase							,     __lowerCAmelCase       )							,     """w"""       ) as f:
									f.write(json.dumps(__lowerCAmelCase       )       )

							A__  				=       AutoProcessor.from_pretrained(__lowerCAmelCase       )

					self.assertIsInstance(__lowerCAmelCase							,     __lowerCAmelCase       )






			def        a_	(      self       :   Optional[Any]       )     ->     Any:







					"""simple docstring"""






					with tempfile.TemporaryDirectory() as tmpdirname:
							A__  				=       WavaVecaFeatureExtractor()
							A__  				=       AutoTokenizer.from_pretrained("""facebook/wav2vec2-base-960h"""       )

							A__  				=       WavaVecaProcessor(__lowerCAmelCase							,     __lowerCAmelCase       )

							# save in new folder
							processor.save_pretrained(__lowerCAmelCase       )

							# drop `processor_class` in feature extractor
							with open(os.path.join(__lowerCAmelCase							,     __lowerCAmelCase       )							,     """r"""       ) as f:
									A__  				=       json.load(__lowerCAmelCase       )
									config_dict.pop("""processor_class"""       )

							with open(os.path.join(__lowerCAmelCase							,     __lowerCAmelCase       )							,     """w"""       ) as f:
									f.write(json.dumps(__lowerCAmelCase       )       )

							A__  				=       AutoProcessor.from_pretrained(__lowerCAmelCase       )

					self.assertIsInstance(__lowerCAmelCase							,     __lowerCAmelCase       )






			def        a_	(      self       :   str       )     ->     Any:







					"""simple docstring"""






					with tempfile.TemporaryDirectory() as tmpdirname:
							A__  				=       WavaVecaConfig(processor_class="""Wav2Vec2Processor"""       )
							model_config.save_pretrained(__lowerCAmelCase       )
							# copy relevant files
							copyfile(__lowerCAmelCase							,     os.path.join(__lowerCAmelCase							,     """vocab.json"""       )       )
							# create emtpy sample processor
							with open(os.path.join(__lowerCAmelCase							,     __lowerCAmelCase       )							,     """w"""       ) as f:
									f.write("""{}"""       )

							A__  				=       AutoProcessor.from_pretrained(__lowerCAmelCase       )

					self.assertIsInstance(__lowerCAmelCase							,     __lowerCAmelCase       )






			def        a_	(      self       :   List[str]       )     ->     Optional[Any]:







					"""simple docstring"""






					with self.assertRaises(__lowerCAmelCase       ):
							A__  				=       AutoProcessor.from_pretrained("""hf-internal-testing/test_dynamic_processor"""       )
					# If remote code is disabled, we can't load this config.
					with self.assertRaises(__lowerCAmelCase       ):
							A__  				=       AutoProcessor.from_pretrained(
							    """hf-internal-testing/test_dynamic_processor"""							,     trust_remote_code=__lowerCAmelCase       )

					A__  				=       AutoProcessor.from_pretrained("""hf-internal-testing/test_dynamic_processor"""							,     trust_remote_code=__lowerCAmelCase       )
					self.assertTrue(processor.special_attribute_present       )
					self.assertEqual(processor.__class__.__name__							,     """NewProcessor"""       )

					A__  				=       processor.feature_extractor
					self.assertTrue(feature_extractor.special_attribute_present       )
					self.assertEqual(feature_extractor.__class__.__name__							,     """NewFeatureExtractor"""       )

					A__  				=       processor.tokenizer
					self.assertTrue(tokenizer.special_attribute_present       )
					if is_tokenizers_available():
							self.assertEqual(tokenizer.__class__.__name__							,     """NewTokenizerFast"""       )

							# Test we can also load the slow version
							A__  				=       AutoProcessor.from_pretrained(
							    """hf-internal-testing/test_dynamic_processor"""							,     trust_remote_code=__lowerCAmelCase							,     use_fast=__lowerCAmelCase       )
							A__  				=       new_processor.tokenizer
							self.assertTrue(new_tokenizer.special_attribute_present       )
							self.assertEqual(new_tokenizer.__class__.__name__							,     """NewTokenizer"""       )
					else:
							self.assertEqual(tokenizer.__class__.__name__							,     """NewTokenizer"""       )






			def        a_	(      self       :   Any       )     ->     Tuple:







					"""simple docstring"""






					try:
							AutoConfig.register("""custom"""							,     __lowerCAmelCase       )
							AutoFeatureExtractor.register(__lowerCAmelCase							,     __lowerCAmelCase       )
							AutoTokenizer.register(__lowerCAmelCase							,     slow_tokenizer_class=__lowerCAmelCase       )
							AutoProcessor.register(__lowerCAmelCase							,     __lowerCAmelCase       )
							# Trying to register something existing in the Transformers library will raise an error
							with self.assertRaises(__lowerCAmelCase       ):
									AutoProcessor.register(__lowerCAmelCase							,     __lowerCAmelCase       )

							# Now that the config is registered, it can be used as any other config with the auto-API
							A__  				=       CustomFeatureExtractor.from_pretrained(__lowerCAmelCase       )

							with tempfile.TemporaryDirectory() as tmp_dir:
									A__  				=       os.path.join(__lowerCAmelCase							,     """vocab.txt"""       )
									with open(__lowerCAmelCase							,     """w"""							,     encoding="""utf-8"""       ) as vocab_writer:
											vocab_writer.write("""""".join([x + """\n""" for x in self.vocab_tokens]       )       )
									A__  				=       CustomTokenizer(__lowerCAmelCase       )

							A__  				=       CustomProcessor(__lowerCAmelCase							,     __lowerCAmelCase       )

							with tempfile.TemporaryDirectory() as tmp_dir:
									processor.save_pretrained(__lowerCAmelCase       )
									A__  				=       AutoProcessor.from_pretrained(__lowerCAmelCase       )
									self.assertIsInstance(__lowerCAmelCase							,     __lowerCAmelCase       )

					finally:
							if "custom" in CONFIG_MAPPING._extra_content:
									del CONFIG_MAPPING._extra_content["custom"]
							if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
									del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
							if CustomConfig in TOKENIZER_MAPPING._extra_content:
									del TOKENIZER_MAPPING._extra_content[CustomConfig]
							if CustomConfig in PROCESSOR_MAPPING._extra_content:
									del PROCESSOR_MAPPING._extra_content[CustomConfig]






			def        a_	(      self       :   Union[str, Any]       )     ->     List[str]:







					"""simple docstring"""






					class   A      (SCREAMING_SNAKE_CASE   ):
								'''simple docstring'''




								__lowerCamelCase     :	Union[str, Any]          =   False


					class   A      (SCREAMING_SNAKE_CASE   ):
								'''simple docstring'''




								__lowerCamelCase     :	List[str]          =   False


					class   A      (SCREAMING_SNAKE_CASE   ):
								'''simple docstring'''




								__lowerCamelCase     :	str          =   '''AutoFeatureExtractor'''
								__lowerCamelCase     :	Optional[int]          =   '''AutoTokenizer'''
								__lowerCamelCase     :	Dict          =   False

					try:
							AutoConfig.register("""custom"""							,     __lowerCAmelCase       )
							AutoFeatureExtractor.register(__lowerCAmelCase							,     __lowerCAmelCase       )
							AutoTokenizer.register(__lowerCAmelCase							,     slow_tokenizer_class=__lowerCAmelCase       )
							AutoProcessor.register(__lowerCAmelCase							,     __lowerCAmelCase       )
							# If remote code is not set, the default is to use local classes.
							A__  				=       AutoProcessor.from_pretrained("""hf-internal-testing/test_dynamic_processor"""       )
							self.assertEqual(processor.__class__.__name__							,     """NewProcessor"""       )
							self.assertFalse(processor.special_attribute_present       )
							self.assertFalse(processor.feature_extractor.special_attribute_present       )
							self.assertFalse(processor.tokenizer.special_attribute_present       )

							# If remote code is disabled, we load the local ones.
							A__  				=       AutoProcessor.from_pretrained(
							    """hf-internal-testing/test_dynamic_processor"""							,     trust_remote_code=__lowerCAmelCase       )
							self.assertEqual(processor.__class__.__name__							,     """NewProcessor"""       )
							self.assertFalse(processor.special_attribute_present       )
							self.assertFalse(processor.feature_extractor.special_attribute_present       )
							self.assertFalse(processor.tokenizer.special_attribute_present       )

							# If remote is enabled, we load from the Hub.
							A__  				=       AutoProcessor.from_pretrained(
							    """hf-internal-testing/test_dynamic_processor"""							,     trust_remote_code=__lowerCAmelCase       )
							self.assertEqual(processor.__class__.__name__							,     """NewProcessor"""       )
							self.assertTrue(processor.special_attribute_present       )
							self.assertTrue(processor.feature_extractor.special_attribute_present       )
							self.assertTrue(processor.tokenizer.special_attribute_present       )

					finally:
							if "custom" in CONFIG_MAPPING._extra_content:
									del CONFIG_MAPPING._extra_content["custom"]
							if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
									del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]
							if CustomConfig in TOKENIZER_MAPPING._extra_content:
									del TOKENIZER_MAPPING._extra_content[CustomConfig]
							if CustomConfig in PROCESSOR_MAPPING._extra_content:
									del PROCESSOR_MAPPING._extra_content[CustomConfig]






			def        a_	(      self       :   List[str]       )     ->     List[Any]:







					"""simple docstring"""






					A__  				=       AutoProcessor.from_pretrained("""hf-internal-testing/tiny-random-bert"""       )
					self.assertEqual(processor.__class__.__name__							,     """BertTokenizerFast"""       )







			def        a_	(      self       :   str       )     ->     Dict:







					"""simple docstring"""






					A__  				=       AutoProcessor.from_pretrained("""hf-internal-testing/tiny-random-convnext"""       )
					self.assertEqual(processor.__class__.__name__							,     """ConvNextImageProcessor"""       )


@is_staging_test
class   A      (unittest.TestCase   ):
			'''simple docstring'''




			__lowerCamelCase     :	int          =   ['''[UNK]''', '''[CLS]''', '''[SEP]''', '''[PAD]''', '''[MASK]''', '''bla''', '''blou''']






			@classmethod
			def        a_	(      cls       :   Dict       )     ->     Tuple:







					"""simple docstring"""






					A__  				=       TOKEN
					HfFolder.save_token(__lowerCAmelCase       )






			@classmethod
			def        a_	(      cls       :   int       )     ->     List[Any]:







					"""simple docstring"""






					try:
							delete_repo(token=cls._token							,     repo_id="""test-processor"""       )
					except HTTPError:
							pass

					try:
							delete_repo(token=cls._token							,     repo_id="""valid_org/test-processor-org"""       )
					except HTTPError:
							pass

					try:
							delete_repo(token=cls._token							,     repo_id="""test-dynamic-processor"""       )
					except HTTPError:
							pass






			def        a_	(      self       :   Dict       )     ->     Optional[int]:







					"""simple docstring"""






					A__  				=       WavaVecaProcessor.from_pretrained(__lowerCAmelCase       )
					with tempfile.TemporaryDirectory() as tmp_dir:
							processor.save_pretrained(
							    os.path.join(__lowerCAmelCase							,     """test-processor"""       )							,     push_to_hub=__lowerCAmelCase							,     use_auth_token=self._token       )

							A__  				=       WavaVecaProcessor.from_pretrained(f'{USER}/test-processor'       )
							for k, v in processor.feature_extractor.__dict__.items():
									self.assertEqual(__lowerCAmelCase							,     getattr(new_processor.feature_extractor							,     __lowerCAmelCase       )       )
							self.assertDictEqual(new_processor.tokenizer.get_vocab()							,     processor.tokenizer.get_vocab()       )






			def        a_	(      self       :   Tuple       )     ->     Tuple:







					"""simple docstring"""






					A__  				=       WavaVecaProcessor.from_pretrained(__lowerCAmelCase       )

					with tempfile.TemporaryDirectory() as tmp_dir:
							processor.save_pretrained(
							    os.path.join(__lowerCAmelCase							,     """test-processor-org"""       )							,     push_to_hub=__lowerCAmelCase							,     use_auth_token=self._token							,     organization="""valid_org"""							,     )

							A__  				=       WavaVecaProcessor.from_pretrained("""valid_org/test-processor-org"""       )
							for k, v in processor.feature_extractor.__dict__.items():
									self.assertEqual(__lowerCAmelCase							,     getattr(new_processor.feature_extractor							,     __lowerCAmelCase       )       )
							self.assertDictEqual(new_processor.tokenizer.get_vocab()							,     processor.tokenizer.get_vocab()       )







			def        a_	(      self       :   List[str]       )     ->     Any:







					"""simple docstring"""






					CustomFeatureExtractor.register_for_auto_class()
					CustomTokenizer.register_for_auto_class()
					CustomProcessor.register_for_auto_class()

					A__  				=       CustomFeatureExtractor.from_pretrained(__lowerCAmelCase       )

					with tempfile.TemporaryDirectory() as tmp_dir:
							A__  				=       os.path.join(__lowerCAmelCase							,     """vocab.txt"""       )
							with open(__lowerCAmelCase							,     """w"""							,     encoding="""utf-8"""       ) as vocab_writer:
									vocab_writer.write("""""".join([x + """\n""" for x in self.vocab_tokens]       )       )
							A__  				=       CustomTokenizer(__lowerCAmelCase       )

					A__  				=       CustomProcessor(__lowerCAmelCase							,     __lowerCAmelCase       )

					with tempfile.TemporaryDirectory() as tmp_dir:
							create_repo(f'{USER}/test-dynamic-processor'							,     token=self._token       )
							A__  				=       Repository(__lowerCAmelCase							,     clone_from=f'{USER}/test-dynamic-processor'							,     token=self._token       )
							processor.save_pretrained(__lowerCAmelCase       )

							# This has added the proper auto_map field to the feature extractor config
							self.assertDictEqual(
							    processor.feature_extractor.auto_map							,     {
							        """AutoFeatureExtractor""": """custom_feature_extraction.CustomFeatureExtractor""",
							        """AutoProcessor""": """custom_processing.CustomProcessor""",
							    }							,     )

							# This has added the proper auto_map field to the tokenizer config
							with open(os.path.join(__lowerCAmelCase							,     """tokenizer_config.json"""       )       ) as f:
									A__  				=       json.load(__lowerCAmelCase       )
							self.assertDictEqual(
							    tokenizer_config["""auto_map"""]							,     {
							        """AutoTokenizer""": ["""custom_tokenization.CustomTokenizer""", None],
							        """AutoProcessor""": """custom_processing.CustomProcessor""",
							    }							,     )

							# The code has been copied from fixtures
							self.assertTrue(os.path.isfile(os.path.join(__lowerCAmelCase							,     """custom_feature_extraction.py"""       )       )       )
							self.assertTrue(os.path.isfile(os.path.join(__lowerCAmelCase							,     """custom_tokenization.py"""       )       )       )
							self.assertTrue(os.path.isfile(os.path.join(__lowerCAmelCase							,     """custom_processing.py"""       )       )       )

							repo.push_to_hub()

					A__  				=       AutoProcessor.from_pretrained(f'{USER}/test-dynamic-processor'							,     trust_remote_code=__lowerCAmelCase       )
					# Can't make an isinstance check because the new_processor is from the CustomProcessor class of a dynamic module
					self.assertEqual(new_processor.__class__.__name__							,     """CustomProcessor"""       )










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      :
			'''simple docstring'''




			def __init__(      self       :   Union[str, Any]							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   Tuple=13							,     __lowerCAmelCase       :   Optional[Any]=7							,     __lowerCAmelCase       :   List[str]=False							,     __lowerCAmelCase       :   Optional[Any]=True							,     __lowerCAmelCase       :   Tuple=False							,     __lowerCAmelCase       :   Any=True							,     __lowerCAmelCase       :   Union[str, Any]=33							,     __lowerCAmelCase       :   List[str]=32							,     __lowerCAmelCase       :   Optional[Any]=5							,     __lowerCAmelCase       :   Dict=4							,     __lowerCAmelCase       :   List[Any]=37							,     __lowerCAmelCase       :   str="gelu"							,     __lowerCAmelCase       :   Any=0.1							,     __lowerCAmelCase       :   str=0.1							,     __lowerCAmelCase       :   List[Any]=5_12							,     __lowerCAmelCase       :   Dict=16							,     __lowerCAmelCase       :   Any=2							,     __lowerCAmelCase       :   List[str]=0.0_2							,     __lowerCAmelCase       :   Dict=3							,     __lowerCAmelCase       :   Optional[int]=4							,     __lowerCAmelCase       :   Tuple=None							,     )     ->     int:







					"""simple docstring"""






					A__  				=       parent
					A__  				=       batch_size
					A__  				=       seq_length
					A__  				=       is_training
					A__  				=       use_input_mask
					A__  				=       use_token_type_ids
					A__  				=       use_labels
					A__  				=       vocab_size
					A__  				=       hidden_size
					A__  				=       num_hidden_layers
					A__  				=       num_attention_heads
					A__  				=       intermediate_size
					A__  				=       hidden_act
					A__  				=       hidden_dropout_prob
					A__  				=       attention_probs_dropout_prob
					A__  				=       max_position_embeddings
					A__  				=       type_vocab_size
					A__  				=       type_sequence_label_size
					A__  				=       initializer_range
					A__  				=       num_labels
					A__  				=       num_choices
					A__  				=       scope






			def        a_	(      self       :   List[Any]       )     ->     Tuple:







					"""simple docstring"""






					A__  				=       ids_tensor([self.batch_size, self.seq_length]							,     self.vocab_size       )

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

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

					A__  				=       self.get_config()

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






			def        a_	(      self       :   Optional[int]       )     ->     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        a_	(      self       :   str							,     __lowerCAmelCase       :   List[Any]							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   Any							,     __lowerCAmelCase       :   Optional[int]       )     ->     str:







					"""simple docstring"""






					A__  				=       EsmModel(config=__lowerCAmelCase       )
					model.to(__lowerCAmelCase       )
					model.eval()
					A__  				=       model(__lowerCAmelCase							,     attention_mask=__lowerCAmelCase       )
					A__  				=       model(__lowerCAmelCase       )
					A__  				=       model(__lowerCAmelCase       )

					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       :   List[Any]							,     __lowerCAmelCase       :   List[str]							,     __lowerCAmelCase       :   List[str]							,     __lowerCAmelCase       :   Tuple							,     __lowerCAmelCase       :   Optional[int]							,     __lowerCAmelCase       :   Union[str, Any]							,     __lowerCAmelCase       :   Any       )     ->     str:







					"""simple docstring"""






					A__  				=       EsmForMaskedLM(config=__lowerCAmelCase       )
					model.to(__lowerCAmelCase       )
					model.eval()
					A__  				=       model(__lowerCAmelCase							,     attention_mask=__lowerCAmelCase							,     labels=__lowerCAmelCase       )
					self.parent.assertEqual(result.logits.shape							,     (self.batch_size, self.seq_length, self.vocab_size)       )






			def        a_	(      self       :   Optional[int]							,     __lowerCAmelCase       :   List[Any]							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   Optional[Any]							,     __lowerCAmelCase       :   str							,     __lowerCAmelCase       :   str							,     __lowerCAmelCase       :   List[str]       )     ->     Any:







					"""simple docstring"""






					A__  				=       self.num_labels
					A__  				=       EsmForTokenClassification(config=__lowerCAmelCase       )
					model.to(__lowerCAmelCase       )
					model.eval()
					A__  				=       model(__lowerCAmelCase							,     attention_mask=__lowerCAmelCase							,     labels=__lowerCAmelCase       )
					self.parent.assertEqual(result.logits.shape							,     (self.batch_size, self.seq_length, self.num_labels)       )







			def        a_	(      self       :   Any       )     ->     Dict:







					"""simple docstring"""






					A__  				=       self.prepare_config_and_inputs()
					(
					    (
					    A__
					)				,      (
					    A__
					)				,      (
					    A__
					)				,      (
					    A__
					)				,      (
					    A__
					)				,      (
					    A__
					)				,      
					)  				=       config_and_inputs
					A__  				=       {"""input_ids""": input_ids, """attention_mask""": input_mask}
					return config, inputs_dict


@require_torch
class   A      (SCREAMING_SNAKE_CASE       ,					SCREAMING_SNAKE_CASE       ,					unittest.TestCase   ):
			'''simple docstring'''




			__lowerCamelCase     :	List[str]          =   False

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






			def        a_	(      self       :   Tuple       )     ->     Optional[int]:







					"""simple docstring"""






					A__  				=       EsmModelTester(self       )
					A__  				=       ConfigTester(self							,     config_class=__lowerCAmelCase							,     hidden_size=37       )






			def        a_	(      self       :   Any       )     ->     str:







					"""simple docstring"""






					self.config_tester.run_common_tests()






			def        a_	(      self       :   List[str]       )     ->     Optional[int]:







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__lowerCAmelCase       )






			def        a_	(      self       :   Optional[int]       )     ->     str:







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					for type in ["absolute", "relative_key", "relative_key_query"]:
							A__  				=       type
							self.model_tester.create_and_check_model(*__lowerCAmelCase       )






			def        a_	(      self       :   Optional[Any]       )     ->     Optional[Any]:







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase       )






			def        a_	(      self       :   Union[str, Any]       )     ->     Union[str, Any]:







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase       )






			@slow
			def        a_	(      self       :   Optional[int]       )     ->     int:







					"""simple docstring"""






					for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							A__  				=       EsmModel.from_pretrained(__lowerCAmelCase       )
							self.assertIsNotNone(__lowerCAmelCase       )






			def        a_	(      self       :   List[str]       )     ->     Union[str, Any]:







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()[0]
					A__  				=       EsmEmbeddings(config=__lowerCAmelCase       )

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






			def        a_	(      self       :   List[Any]       )     ->     str:







					"""simple docstring"""






					A__  				=       self.model_tester.prepare_config_and_inputs()[0]
					A__  				=       EsmEmbeddings(config=__lowerCAmelCase       )

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






			@unittest.skip("""Esm does not support embedding resizing"""       )
			def        a_	(      self       :   Dict       )     ->     Tuple:







					"""simple docstring"""






					pass






			@unittest.skip("""Esm does not support embedding resizing"""       )
			def        a_	(      self       :   List[str]       )     ->     Optional[int]:







					"""simple docstring"""






					pass







			@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""       )
			def        a_	(      self       :   List[Any]       )     ->     Dict:







					"""simple docstring"""






					pass


@require_torch
class   A      (SCREAMING_SNAKE_CASE   ):
			'''simple docstring'''




			@slow
			def        a_	(      self       :   int       )     ->     Optional[int]:







					"""simple docstring"""






					with torch.no_grad():
							A__  				=       EsmForMaskedLM.from_pretrained("""facebook/esm2_t6_8M_UR50D"""       )
							model.eval()
							A__  				=       torch.tensor([[0, 1, 2, 3, 4, 5]]       )
							A__  				=       model(__lowerCAmelCase       )[0]

							A__  				=       33

							A__  				=       torch.Size((1, 6, vocab_size)       )
							self.assertEqual(output.shape							,     __lowerCAmelCase       )

							A__  				=       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]							,     __lowerCAmelCase							,     atol=1e-4       )       )







			@slow
			def        a_	(      self       :   List[str]       )     ->     Tuple:







					"""simple docstring"""






					with torch.no_grad():
							A__  				=       EsmModel.from_pretrained("""facebook/esm2_t6_8M_UR50D"""       )
							model.eval()

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



