





from __future__ import annotations

import unittest

from transformers import EsmConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import numpy
						import tensorflow as tf

						from transformers.models.esm.modeling_tf_esm import (
						    TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFEsmForMaskedLM,
						    TFEsmForSequenceClassification,
						    TFEsmForTokenClassification,
						    TFEsmModel,
						)
class SCREAMING_SNAKE_CASE_						:






						def __init__(      self      :      Optional[Any]		,   _A      :      Tuple		,   )							->    Dict:







							"""simple docstring"""


							snake_case_		:			Any											=	parent
							snake_case_		:			str											=	13
							snake_case_		:			int											=	7
							snake_case_		:			str											=	True
							snake_case_		:			Optional[Any]											=	True
							snake_case_		:			List[str]											=	True
							snake_case_		:			List[str]											=	99
							snake_case_		:			List[str]											=	32
							snake_case_		:			Tuple											=	2
							snake_case_		:			Optional[int]											=	4
							snake_case_		:			Union[str, Any]											=	37
							snake_case_		:			Optional[Any]											=	'gelu'
							snake_case_		:			Tuple											=	0.1
							snake_case_		:			str											=	0.1
							snake_case_		:			Optional[Any]											=	512
							snake_case_		:			Union[str, Any]											=	16
							snake_case_		:			Union[str, Any]											=	2
							snake_case_		:			Tuple											=	0.0_2
							snake_case_		:			int											=	3
							snake_case_		:			int											=	4
							snake_case_		:			Dict											=	None






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







							"""simple docstring"""


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

							snake_case_		:			Tuple											=	None
							if self.use_input_mask:
								snake_case_		:			List[Any]											=	random_attention_mask([self.batch_size, self.seq_length]	)

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

							snake_case_		:			Optional[Any]											=	EsmConfig(
							    vocab_size=self.vocab_size		,   hidden_size=self.hidden_size		,   num_hidden_layers=self.num_hidden_layers		,   pad_token_id=1		,   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		,   )

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






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







							"""simple docstring"""


							(
							    (
							    snake_case_
							)						,(
							    snake_case_
							)						,(
							    snake_case_
							)						,(
							    snake_case_
							)						,(
							    snake_case_
							)						,(
							    snake_case_
							)						,
							)		:			Union[str, Any]											=	self.prepare_config_and_inputs()

							snake_case_		:			int											=	True
							snake_case_		:			Tuple											=	floats_tensor([self.batch_size, self.seq_length, self.hidden_size]	)
							snake_case_		:			List[str]											=	ids_tensor([self.batch_size, self.seq_length]		,   vocab_size=2	)

							return (
							    config,
							    input_ids,
							    input_mask,
							    sequence_labels,
							    token_labels,
							    choice_labels,
							    encoder_hidden_states,
							    encoder_attention_mask,
							)






						def UpperCAmelCase_				(      self      :      List[Any]		,   _A      :      int		,   _A      :      List[str]		,   _A      :      Any		,   _A      :      List[Any]		,   _A      :      Dict		,   _A      :      List[str]	)							->    List[str]:







							"""simple docstring"""


							snake_case_		:			Union[str, Any]											=	TFEsmModel(config=_A	)
							snake_case_		:			Union[str, Any]											=	{'input_ids': input_ids, 'attention_mask': input_mask}
							snake_case_		:			List[Any]											=	model(_A	)

							snake_case_		:			int											=	[input_ids, input_mask]
							snake_case_		:			Optional[int]											=	model(_A	)

							snake_case_		:			Optional[int]											=	model(_A	)

							self.parent.assertEqual(result.last_hidden_state.shape		,   (self.batch_size, self.seq_length, self.hidden_size)	)






						def UpperCAmelCase_				(      self      :      Optional[Any]		,   _A      :      int		,   _A      :      List[str]		,   _A      :      Dict		,   _A      :      List[Any]		,   _A      :      Union[str, Any]		,   _A      :      Union[str, Any]		,   _A      :      Any		,   _A      :      Optional[Any]		,   )							->    str:







							"""simple docstring"""


							snake_case_		:			Optional[int]											=	True

							snake_case_		:			List[str]											=	TFEsmModel(config=_A	)
							snake_case_		:			Optional[Any]											=	{
							    'input_ids': input_ids,
							    'attention_mask': input_mask,
							    'encoder_hidden_states': encoder_hidden_states,
							    'encoder_attention_mask': encoder_attention_mask,
							}
							snake_case_		:			Dict											=	model(_A	)

							snake_case_		:			Any											=	[input_ids, input_mask]
							snake_case_		:			Optional[Any]											=	model(_A		,   encoder_hidden_states=_A	)

							# Also check the case where encoder outputs are not passed
							snake_case_		:			List[str]											=	model(_A		,   attention_mask=_A	)

							self.parent.assertEqual(result.last_hidden_state.shape		,   (self.batch_size, self.seq_length, self.hidden_size)	)






						def UpperCAmelCase_				(      self      :      List[Any]		,   _A      :      Dict		,   _A      :      Tuple		,   _A      :      List[str]		,   _A      :      Tuple		,   _A      :      Any		,   _A      :      List[Any]	)							->    int:







							"""simple docstring"""


							snake_case_		:			List[Any]											=	TFEsmForMaskedLM(config=_A	)
							snake_case_		:			Any											=	model([input_ids, input_mask]	)
							self.parent.assertEqual(result.logits.shape		,   (self.batch_size, self.seq_length, self.vocab_size)	)






						def UpperCAmelCase_				(      self      :      Optional[int]		,   _A      :      str		,   _A      :      Any		,   _A      :      List[Any]		,   _A      :      int		,   _A      :      Optional[Any]		,   _A      :      Union[str, Any]	)							->    Union[str, Any]:







							"""simple docstring"""


							snake_case_		:			Dict											=	self.num_labels
							snake_case_		:			str											=	TFEsmForTokenClassification(config=_A	)
							snake_case_		:			int											=	{'input_ids': input_ids, 'attention_mask': input_mask}
							snake_case_		:			List[Any]											=	model(_A	)
							self.parent.assertEqual(result.logits.shape		,   (self.batch_size, self.seq_length, self.num_labels)	)






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







							"""simple docstring"""


							snake_case_		:			Dict											=	self.prepare_config_and_inputs()
							(
							    (
							    snake_case_
							)						,(
							    snake_case_
							)						,(
							    snake_case_
							)						,(
							    snake_case_
							)						,(
							    snake_case_
							)						,(
							    snake_case_
							)						,
							)		:			Optional[Any]											=	config_and_inputs
							snake_case_		:			int											=	{'input_ids': input_ids, 'attention_mask': input_mask}
							return config, inputs_dict
@require_tf
class SCREAMING_SNAKE_CASE_						(					snake_case_       ,				snake_case_       ,				unittest.TestCase					):
						__magic_name__:			Union[str, Any]       =       (
						    (
						        TFEsmModel,
						        TFEsmForMaskedLM,
						        TFEsmForSequenceClassification,
						        TFEsmForTokenClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						__magic_name__:			List[Any]       =       (
						    {
						        "feature-extraction": TFEsmModel,
						        "fill-mask": TFEsmForMaskedLM,
						        "text-classification": TFEsmForSequenceClassification,
						        "token-classification": TFEsmForTokenClassification,
						        "zero-shot": TFEsmForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						__magic_name__:			Any       =       False
						__magic_name__:			List[str]       =       False






						def UpperCAmelCase_				(      self      :      Optional[Any]	)							->    Optional[int]:







							"""simple docstring"""


							snake_case_		:			Dict											=	TFEsmModelTester(self	)
							snake_case_		:			Optional[int]											=	ConfigTester(self		,   config_class=_A		,   hidden_size=37	)






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







							"""simple docstring"""


							self.config_tester.run_common_tests()






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







							"""simple docstring"""


							snake_case_		:			Dict											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_A	)






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







							"""simple docstring"""


							snake_case_		:			str											=	self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(*_A	)






						def UpperCAmelCase_				(      self      :      Tuple	)							->    Any:







							"""simple docstring"""


							snake_case_		:			Any											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*_A	)






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







							"""simple docstring"""


							snake_case_		:			Tuple											=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*_A	)






						@slow
						def UpperCAmelCase_				(      self      :      int	)							->    List[Any]:







							"""simple docstring"""


							for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								snake_case_		:			Union[str, Any]											=	TFEsmModel.from_pretrained(_A	)
								self.assertIsNotNone(_A	)






						@unittest.skip('Protein models do not support embedding resizing.'	)
						def UpperCAmelCase_				(      self      :      Union[str, Any]	)							->    Optional[int]:







							"""simple docstring"""


							pass






						@unittest.skip('Protein models do not support embedding resizing.'	)
						def UpperCAmelCase_				(      self      :      Optional[int]	)							->    Tuple:







							"""simple docstring"""


							pass






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







							"""simple docstring"""


							snake_case_						,snake_case_		:			List[Any]											=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_		:			List[Any]											=	model_class(_A	)
								assert isinstance(model.get_input_embeddings()		,   tf.keras.layers.Layer	)
								if model_class is TFEsmForMaskedLM:
									# Output embedding test differs from the main test because they're a matrix, not a layer
									snake_case_		:			Any											=	model.get_bias()
									assert isinstance(_A		,   _A	)
									for k, v in name.items():
										assert isinstance(_A		,   tf.Variable	)
								else:
									snake_case_		:			List[Any]											=	model.get_output_embeddings()
									assert x is None
									snake_case_		:			Any											=	model.get_bias()
									assert name is None
@require_tf
class SCREAMING_SNAKE_CASE_						(					unittest.TestCase					):






						@slow
						def UpperCAmelCase_				(      self      :      int	)							->    List[Any]:







							"""simple docstring"""


							snake_case_		:			int											=	TFEsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D'	)

							snake_case_		:			List[Any]											=	tf.constant([[0, 1, 2, 3, 4, 5]]	)
							snake_case_		:			Optional[Any]											=	model(_A	)[0]
							snake_case_		:			List[str]											=	[1, 6, 33]
							self.assertEqual(list(output.numpy().shape	)		,   _A	)
							# compare the actual values for a slice.
							snake_case_		:			Optional[Any]											=	tf.constant(
							    [
							        [
							            [8.9_2_1_5_1_8, -1_0.5_8_9_8_1_4, -6.4_6_7_1_3_0_7],
							            [-6.3_9_6_7_1_5_6, -1_3.9_1_1_3_7_7, -1.1_2_1_1_9_1_5],
							            [-7.7_8_1_2_4_7, -1_3.9_5_1_5_5_7, -3.7_4_0_5_9_2],
							        ]
							    ]	)
							self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()		,   expected_slice.numpy()		,   atol=1E-2	)	)






						@slow
						def UpperCAmelCase_				(      self      :      Union[str, Any]	)							->    int:







							"""simple docstring"""


							snake_case_		:			str											=	TFEsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D'	)

							snake_case_		:			str											=	tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]	)
							snake_case_		:			List[str]											=	model(_A	)[0]
							# compare the actual values for a slice.
							snake_case_		:			Optional[Any]											=	tf.constant(
							    [
							        [
							            [0.1_4_4_4_3_0_9_2, 0.5_4_1_2_5_3_2_7, 0.3_2_4_7_7_3_9],
							            [0.3_0_3_4_0_4_8_4, 0.0_0_5_2_6_6_7_6, 0.3_1_0_7_7_7_2_2],
							            [0.3_2_2_7_8_0_4_3, -0.2_4_9_8_7_0_9_6, 0.3_4_1_4_6_2_8],
							        ]
							    ]	)
							self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()		,   expected_slice.numpy()		,   atol=1E-4	)	)








from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE      	=		logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE      	=		{
    """SCUT-DLVCLab/lilt-roberta-en-base""": (
        """https://huggingface.co/SCUT-DLVCLab/lilt-roberta-en-base/resolve/main/config.json"""
    ),
}
class SCREAMING_SNAKE_CASE_						(					snake_case_					):
						__magic_name__:			str       =       "lilt"






						def __init__(      self      :      Any		,   _A      :      Dict=30522		,   _A      :      List[Any]=768		,   _A      :      str=12		,   _A      :      int=12		,   _A      :      Optional[int]=3072		,   _A      :      Optional[Any]="gelu"		,   _A      :      Dict=0.1		,   _A      :      Tuple=0.1		,   _A      :      str=512		,   _A      :      Union[str, Any]=2		,   _A      :      Union[str, Any]=0.0_2		,   _A      :      Optional[Any]=1E-12		,   _A      :      Optional[int]=0		,   _A      :      Dict="absolute"		,   _A      :      Any=None		,   _A      :      Optional[Any]=4		,   _A      :      Any=1024		,   **_A      :      Union[str, Any]		,   )							->    List[str]:







							"""simple docstring"""


							super().__init__(pad_token_id=_A		,   **_A	)

							snake_case_		:			List[str]											=	vocab_size
							snake_case_		:			Dict											=	hidden_size
							snake_case_		:			Union[str, Any]											=	num_hidden_layers
							snake_case_		:			List[str]											=	num_attention_heads
							snake_case_		:			Tuple											=	hidden_act
							snake_case_		:			Tuple											=	intermediate_size
							snake_case_		:			int											=	hidden_dropout_prob
							snake_case_		:			Tuple											=	attention_probs_dropout_prob
							snake_case_		:			Dict											=	max_position_embeddings
							snake_case_		:			Any											=	type_vocab_size
							snake_case_		:			Union[str, Any]											=	initializer_range
							snake_case_		:			Optional[int]											=	layer_norm_eps
							snake_case_		:			List[str]											=	position_embedding_type
							snake_case_		:			Optional[int]											=	classifier_dropout
							snake_case_		:			Dict											=	channel_shrink_ratio
							snake_case_		:			Tuple											=	max_ad_position_embeddings

