
"""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     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__=13     , SCREAMING_SNAKE_CASE__=7     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=False     , SCREAMING_SNAKE_CASE__=True     , SCREAMING_SNAKE_CASE__=33     , SCREAMING_SNAKE_CASE__=32     , SCREAMING_SNAKE_CASE__=5     , SCREAMING_SNAKE_CASE__=4     , SCREAMING_SNAKE_CASE__=37     , SCREAMING_SNAKE_CASE__="gelu"     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=0.1     , SCREAMING_SNAKE_CASE__=5_12     , SCREAMING_SNAKE_CASE__=16     , SCREAMING_SNAKE_CASE__=2     , SCREAMING_SNAKE_CASE__=0.02     , SCREAMING_SNAKE_CASE__=3     , SCREAMING_SNAKE_CASE__=4     , SCREAMING_SNAKE_CASE__=None     , )      ->	str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Tuple     =     parent
											SCREAMING_SNAKE_CASE__		: str     =     batch_size
											SCREAMING_SNAKE_CASE__		: Any     =     seq_length
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     is_training
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     use_input_mask
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     use_token_type_ids
											SCREAMING_SNAKE_CASE__		: str     =     use_labels
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     vocab_size
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     hidden_size
											SCREAMING_SNAKE_CASE__		: List[str]     =     num_hidden_layers
											SCREAMING_SNAKE_CASE__		: Dict     =     num_attention_heads
											SCREAMING_SNAKE_CASE__		: int     =     intermediate_size
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     hidden_act
											SCREAMING_SNAKE_CASE__		: int     =     hidden_dropout_prob
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE__		: Any     =     max_position_embeddings
											SCREAMING_SNAKE_CASE__		: List[Any]     =     type_vocab_size
											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     type_sequence_label_size
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     initializer_range
											SCREAMING_SNAKE_CASE__		: Tuple     =     num_labels
											SCREAMING_SNAKE_CASE__		: int     =     num_choices
											SCREAMING_SNAKE_CASE__		: Dict     =     scope



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     ids_tensor([self.batch_size, self.seq_length]     , self.vocab_size					)

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

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

											SCREAMING_SNAKE_CASE__		: List[str]     =     self.get_config()

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



				def 			__magic_name__					(self					)      ->	Dict:



											"""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 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	Dict:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     EsmModel(config=SCREAMING_SNAKE_CASE__					)
											model.to(SCREAMING_SNAKE_CASE__					)
											model.eval()
											SCREAMING_SNAKE_CASE__		: str     =     model(SCREAMING_SNAKE_CASE__     , attention_mask=SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: Any     =     model(SCREAMING_SNAKE_CASE__					)
											SCREAMING_SNAKE_CASE__		: str     =     model(SCREAMING_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 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Tuple     =     EsmForMaskedLM(config=SCREAMING_SNAKE_CASE__					)
											model.to(SCREAMING_SNAKE_CASE__					)
											model.eval()
											SCREAMING_SNAKE_CASE__		: int     =     model(SCREAMING_SNAKE_CASE__     , attention_mask=SCREAMING_SNAKE_CASE__     , labels=SCREAMING_SNAKE_CASE__					)
											self.parent.assertEqual(result.logits.shape     , (self.batch_size, self.seq_length, self.vocab_size)					)



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)      ->	Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[str]     =     self.num_labels
											SCREAMING_SNAKE_CASE__		: List[Any]     =     EsmForTokenClassification(config=SCREAMING_SNAKE_CASE__					)
											model.to(SCREAMING_SNAKE_CASE__					)
											model.eval()
											SCREAMING_SNAKE_CASE__		: str     =     model(SCREAMING_SNAKE_CASE__     , attention_mask=SCREAMING_SNAKE_CASE__     , labels=SCREAMING_SNAKE_CASE__					)
											self.parent.assertEqual(result.logits.shape     , (self.batch_size, self.seq_length, self.num_labels)					)



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



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Dict     =     self.prepare_config_and_inputs()
											(
											    (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   (
											    SCREAMING_SNAKE_CASE__
											)   ,   
											)		: Optional[int]     =     config_and_inputs
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     {"""input_ids""": input_ids, """attention_mask""": input_mask}
											return config, inputs_dict






@require_torch
class 			lowerCAmelCase_		(a__				,      a__				,      unittest.TestCase						):


				"""simple docstring"""



				__UpperCamelCase   :		Dict       =     False

				__UpperCamelCase   :		int       =     (
				    (
				        EsmForMaskedLM,
				        EsmModel,
				        EsmForSequenceClassification,
				        EsmForTokenClassification,
				    )
				    if is_torch_available()
				    else ()
				)
				__UpperCamelCase   :		Tuple       =     ()
				__UpperCamelCase   :		Dict       =     (
				    {
				        '''feature-extraction''': EsmModel,
				        '''fill-mask''': EsmForMaskedLM,
				        '''text-classification''': EsmForSequenceClassification,
				        '''token-classification''': EsmForTokenClassification,
				        '''zero-shot''': EsmForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				__UpperCamelCase   :		str       =     True



				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[Any]     =     EsmModelTester(self					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     ConfigTester(self     , config_class=SCREAMING_SNAKE_CASE__     , hidden_size=37					)



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



											"""simple docstring"""




											self.config_tester.run_common_tests()



				def 			__magic_name__					(self					)      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: int     =     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	List[str]:



											"""simple docstring"""




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



				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Any     =     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	Tuple:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: int     =     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*SCREAMING_SNAKE_CASE__					)



				@slow
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     EsmModel.from_pretrained(SCREAMING_SNAKE_CASE__					)
																		self.assertIsNotNone(SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Tuple     =     self.model_tester.prepare_config_and_inputs()[0]
											SCREAMING_SNAKE_CASE__		: List[str]     =     EsmEmbeddings(config=SCREAMING_SNAKE_CASE__					)

											SCREAMING_SNAKE_CASE__		: Dict     =     torch.as_tensor([[12, 31, 13, model.padding_idx]]					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     torch.as_tensor(
											    [
											        [
											            0 + model.padding_idx + 1,
											            1 + model.padding_idx + 1,
											            2 + model.padding_idx + 1,
											            model.padding_idx,
											        ]
											    ]					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     create_position_ids_from_input_ids(SCREAMING_SNAKE_CASE__     , model.padding_idx					)
											self.assertEqual(position_ids.shape     , expected_positions.shape					)
											self.assertTrue(torch.all(torch.eq(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)					)					)



				def 			__magic_name__					(self					)      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     self.model_tester.prepare_config_and_inputs()[0]
											SCREAMING_SNAKE_CASE__		: Any     =     EsmEmbeddings(config=SCREAMING_SNAKE_CASE__					)

											SCREAMING_SNAKE_CASE__		: Tuple     =     torch.empty(2     , 4     , 30					)
											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     [
											    0 + embeddings.padding_idx + 1,
											    1 + embeddings.padding_idx + 1,
											    2 + embeddings.padding_idx + 1,
											    3 + embeddings.padding_idx + 1,
											]
											SCREAMING_SNAKE_CASE__		: List[str]     =     torch.as_tensor([expected_single_positions, expected_single_positions]					)
											SCREAMING_SNAKE_CASE__		: Dict     =     embeddings.create_position_ids_from_inputs_embeds(SCREAMING_SNAKE_CASE__					)
											self.assertEqual(position_ids.shape     , expected_positions.shape					)
											self.assertTrue(torch.all(torch.eq(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)					)					)



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



											"""simple docstring"""




											pass



				@unittest.skip("""Esm does not support embedding resizing"""					)
				def 			__magic_name__					(self					)      ->	List[Any]:



											"""simple docstring"""




											pass



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



											"""simple docstring"""




											pass






@require_torch
class 			lowerCAmelCase_		(a__						):


				"""simple docstring"""



				@slow
				def 			__magic_name__					(self					)      ->	int:



											"""simple docstring"""




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

																		SCREAMING_SNAKE_CASE__		: Any     =     33

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

																		SCREAMING_SNAKE_CASE__		: int     =     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]     , SCREAMING_SNAKE_CASE__     , atol=1E-4					)					)



				@slow
				def 			__magic_name__					(self					)      ->	Dict:



											"""simple docstring"""




											with torch.no_grad():
																		SCREAMING_SNAKE_CASE__		: Optional[Any]     =     EsmModel.from_pretrained("""facebook/esm2_t6_8M_UR50D"""					)
																		model.eval()

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






"""simple docstring"""




import doctest
import logging
import os
import unittest
from pathlib import Path
from typing import List, Union

import transformers
from transformers.testing_utils import require_tf, require_torch, slow


UpperCAmelCase__   :					Any												=				logging.getLogger()



@unittest.skip('''Temporarily disable the doc tests.'''						)
@require_torch
@require_tf
@slow
class 			lowerCAmelCase_		(unittest.TestCase						):


				"""simple docstring"""



				def 			__magic_name__					(self     , SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = None     , SCREAMING_SNAKE_CASE__ = True     , )      ->	int:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Tuple     =     [file for file in os.listdir(SCREAMING_SNAKE_CASE__					) if os.path.isfile(os.path.join(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)					)]

											if identifier is not None:
																		SCREAMING_SNAKE_CASE__		: int     =     [file for file in files if identifier in file]

											if n_identifier is not None:
																		if isinstance(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					):
																									for n_ in n_identifier:
																																SCREAMING_SNAKE_CASE__		: Optional[Any]     =     [file for file in files if n_ not in file]
																		else:
																									SCREAMING_SNAKE_CASE__		: List[Any]     =     [file for file in files if n_identifier not in file]

											SCREAMING_SNAKE_CASE__		: int     =     ignore_files or []
											ignore_files.append("""__init__.py"""					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     [file for file in files if file not in ignore_files]

											for file in files:
																		# Open all files
																		print("""Testing"""     , SCREAMING_SNAKE_CASE__					)

																		if only_modules:
																									SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     file.split("""."""					)[0]
																									try:
																																SCREAMING_SNAKE_CASE__		: Optional[Any]     =     getattr(SCREAMING_SNAKE_CASE__     , SCREAMING_SNAKE_CASE__					)
																																SCREAMING_SNAKE_CASE__		: Optional[int]     =     doctest.DocTestSuite(SCREAMING_SNAKE_CASE__					)
																																SCREAMING_SNAKE_CASE__		: int     =     unittest.TextTestRunner().run(SCREAMING_SNAKE_CASE__					)
																																self.assertIs(len(result.failures					)     , 0					)
																									except AttributeError:
																																logger.info(F'''{module_identifier} is not a module.'''					)
																		else:
																									SCREAMING_SNAKE_CASE__		: Optional[int]     =     doctest.testfile(str("""..""" / directory / file					)     , optionflags=doctest.ELLIPSIS					)
																									self.assertIs(result.failed     , 0					)



				def 			__magic_name__					(self					)      ->	Any:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[int]     =     Path("""src/transformers"""					)
											SCREAMING_SNAKE_CASE__		: Optional[int]     =     """modeling"""
											SCREAMING_SNAKE_CASE__		: int     =     [
											    """modeling_ctrl.py""",
											    """modeling_tf_ctrl.py""",
											]
											self.analyze_directory(SCREAMING_SNAKE_CASE__     , identifier=SCREAMING_SNAKE_CASE__     , ignore_files=SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	str:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: str     =     Path("""src/transformers"""					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     """tokenization"""
											self.analyze_directory(SCREAMING_SNAKE_CASE__     , identifier=SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	List[Any]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: List[Any]     =     Path("""src/transformers"""					)
											SCREAMING_SNAKE_CASE__		: List[str]     =     """configuration"""
											self.analyze_directory(SCREAMING_SNAKE_CASE__     , identifier=SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Union[str, Any]     =     Path("""src/transformers"""					)
											SCREAMING_SNAKE_CASE__		: Dict     =     ["""configuration""", """modeling""", """tokenization"""]
											self.analyze_directory(SCREAMING_SNAKE_CASE__     , n_identifier=SCREAMING_SNAKE_CASE__					)



				def 			__magic_name__					(self					)      ->	Optional[int]:



											"""simple docstring"""




											SCREAMING_SNAKE_CASE__		: Optional[int]     =     Path("""docs/source"""					)
											SCREAMING_SNAKE_CASE__		: Any     =     ["""favicon.ico"""]
											self.analyze_directory(SCREAMING_SNAKE_CASE__     , ignore_files=SCREAMING_SNAKE_CASE__     , only_modules=SCREAMING_SNAKE_CASE__					)




