

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





			def __init__(						self   ,    __snake_case   ,    __snake_case=13   ,    __snake_case=7   ,    __snake_case=False   ,    __snake_case=True   ,    __snake_case=False   ,    __snake_case=True   ,    __snake_case=33   ,    __snake_case=32   ,    __snake_case=5   ,    __snake_case=4   ,    __snake_case=37   ,    __snake_case="gelu"   ,    __snake_case=0.1   ,    __snake_case=0.1   ,    __snake_case=512   ,    __snake_case=16   ,    __snake_case=2   ,    __snake_case=0.02   ,    __snake_case=3   ,    __snake_case=4   ,    __snake_case=None   ,    )  ->					str:




						'''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 						__magic_name__       (						self   )  ->					List[Any]:




						'''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 						__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   ,    __snake_case   ,    __snake_case   ,    __snake_case   ,    __snake_case   ,    __snake_case   ,    __snake_case   )  ->					Union[str, Any]:




						'''simple docstring'''
						__a     =EsmModel(config=__snake_case   )
						model.to(__snake_case   )
						model.eval()
						__a     =model(__snake_case   ,    attention_mask=__snake_case   )
						__a     =model(__snake_case   )
						__a     =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 						__magic_name__       (						self   ,    __snake_case   ,    __snake_case   ,    __snake_case   ,    __snake_case   ,    __snake_case   ,    __snake_case   )  ->					List[Any]:




						'''simple docstring'''
						__a     =EsmForMaskedLM(config=__snake_case   )
						model.to(__snake_case   )
						model.eval()
						__a     =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 						__magic_name__       (						self   ,    __snake_case   ,    __snake_case   ,    __snake_case   ,    __snake_case   ,    __snake_case   ,    __snake_case   )  ->					Optional[Any]:




						'''simple docstring'''
						__a     =self.num_labels
						__a     =EsmForTokenClassification(config=__snake_case   )
						model.to(__snake_case   )
						model.eval()
						__a     =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 						__magic_name__       (						self   )  ->					List[Any]:




						'''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       __magic_name__       (     lowerCAmelCase_    ,       lowerCAmelCase_    ,       unittest.TestCase ):
			SCREAMING_SNAKE_CASE				  =		False

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





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




						'''simple docstring'''
						__a     =EsmModelTester(self   )
						__a     =ConfigTester(self   ,    config_class=__snake_case   ,    hidden_size=37   )





			def 						__magic_name__       (						self   )  ->					Any:




						'''simple docstring'''
						self.config_tester.run_common_tests()





			def 						__magic_name__       (						self   )  ->					int:




						'''simple docstring'''
						__a     =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*__snake_case   )





			def 						__magic_name__       (						self   )  ->					int:




						'''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(*__snake_case   )





			def 						__magic_name__       (						self   )  ->					Tuple:




						'''simple docstring'''
						__a     =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*__snake_case   )





			def 						__magic_name__       (						self   )  ->					Tuple:




						'''simple docstring'''
						__a     =self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*__snake_case   )





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




						'''simple docstring'''


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





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




						'''simple docstring'''
						__a     =self.model_tester.prepare_config_and_inputs()[0]
						__a     =EsmEmbeddings(config=__snake_case   )

						__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(__snake_case   ,    model.padding_idx   )
						self.assertEqual(position_ids.shape   ,    expected_positions.shape   )
						self.assertTrue(torch.all(torch.eq(__snake_case   ,    __snake_case   )   )   )





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




						'''simple docstring'''
						__a     =self.model_tester.prepare_config_and_inputs()[0]
						__a     =EsmEmbeddings(config=__snake_case   )

						__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(__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 						__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   )  ->					List[Any]:




						'''simple docstring'''
						pass






@require_torch
class       __magic_name__       (     lowerCAmelCase_ ):





			@slow
			def 						__magic_name__       (						self   )  ->					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(__snake_case   )[0]

									__a     =33

									__a     =torch.Size((1, 6, vocab_size)   )
									self.assertEqual(output.shape   ,    __snake_case   )

									__a     =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 						__magic_name__       (						self   )  ->					str:




						'''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(__snake_case   )[0]
									# compare the actual values for a slice.
									__a     =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   )   )










from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__UpperCamelCase     		=							logging.get_logger(__name__)

__UpperCamelCase     		=							{
    'facebook/xmod-base': 'https://huggingface.co/facebook/xmod-base/resolve/main/config.json',
    'facebook/xmod-large-prenorm': 'https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json',
    'facebook/xmod-base-13-125k': 'https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json',
    'facebook/xmod-base-30-125k': 'https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json',
    'facebook/xmod-base-30-195k': 'https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json',
    'facebook/xmod-base-60-125k': 'https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json',
    'facebook/xmod-base-60-265k': 'https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json',
    'facebook/xmod-base-75-125k': 'https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json',
    'facebook/xmod-base-75-269k': 'https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json',
}







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			_UpperCamelCase		:       List[str] 			=		'xmod'




			def __init__(				self   ,    snake_case=30522   ,    snake_case=768   ,    snake_case=12   ,    snake_case=12   ,    snake_case=3072   ,    snake_case="gelu"   ,    snake_case=0.1   ,    snake_case=0.1   ,    snake_case=512   ,    snake_case=2   ,    snake_case=0.02   ,    snake_case=1E-1_2   ,    snake_case=1   ,    snake_case=0   ,    snake_case=2   ,    snake_case="absolute"   ,    snake_case=True   ,    snake_case=None   ,    snake_case=False   ,    snake_case=2   ,    snake_case=False   ,    snake_case=True   ,    snake_case=True   ,    snake_case=("en_XX",)   ,    snake_case=None   ,    **snake_case   ,    ):

						'''simple docstring'''


						super().__init__(pad_token_id=snake_case   ,    bos_token_id=snake_case   ,    eos_token_id=snake_case   ,    **snake_case  )

						UpperCamelCase__       =							vocab_size
						UpperCamelCase__       =							hidden_size
						UpperCamelCase__       =							num_hidden_layers
						UpperCamelCase__       =							num_attention_heads
						UpperCamelCase__       =							hidden_act
						UpperCamelCase__       =							intermediate_size
						UpperCamelCase__       =							hidden_dropout_prob
						UpperCamelCase__       =							attention_probs_dropout_prob
						UpperCamelCase__       =							max_position_embeddings
						UpperCamelCase__       =							type_vocab_size
						UpperCamelCase__       =							initializer_range
						UpperCamelCase__       =							layer_norm_eps
						UpperCamelCase__       =							position_embedding_type
						UpperCamelCase__       =							use_cache
						UpperCamelCase__       =							classifier_dropout
						UpperCamelCase__       =							pre_norm
						UpperCamelCase__       =							adapter_reduction_factor
						UpperCamelCase__       =							adapter_layer_norm
						UpperCamelCase__       =							adapter_reuse_layer_norm
						UpperCamelCase__       =							ln_before_adapter
						UpperCamelCase__       =							list(snake_case  )
						UpperCamelCase__       =							default_language







class 							lowerCamelCase__						(				UpperCAmelCase   ):






			"""simple docstring"""






			@property
			def 					snake_case__					(				self  ):

						'''simple docstring'''


						if self.task == "multiple-choice":
									UpperCamelCase__       =							{0: "batch", 1: "choice", 2: "sequence"}
						else:
									UpperCamelCase__       =							{0: "batch", 1: "sequence"}
						return OrderedDict(
						    [
						        ("input_ids", dynamic_axis),
						        ("attention_mask", dynamic_axis),
						    ]  )


