





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

						def __init__(	self				:      Optional[int]							,							__lowerCamelCase				:      Dict							,							)							->		int:


									'''simple docstring'''

									lowercase							=		parent
									lowercase							=		13
									lowercase							=		7
									lowercase							=		True
									lowercase							=		True
									lowercase							=		True
									lowercase							=		99
									lowercase							=		32
									lowercase							=		2
									lowercase							=		4
									lowercase							=		37
									lowercase							=		'''gelu'''
									lowercase							=		0.1
									lowercase							=		0.1
									lowercase							=		5_12
									lowercase							=		16
									lowercase							=		2
									lowercase							=		0.02
									lowercase							=		3
									lowercase							=		4
									lowercase							=		None

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


									'''simple docstring'''

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

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

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

									lowercase							=		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        __a		(	self				:      Optional[int]   )							->		List[str]:


									'''simple docstring'''

									(
									    (
									    lowercase      
									)		,(
									    lowercase      
									)		,(
									    lowercase      
									)		,(
									    lowercase      
									)		,(
									    lowercase      
									)		,(
									    lowercase      
									)		,      
									)							=		self.prepare_config_and_inputs()

									lowercase							=		True
									lowercase							=		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
									lowercase							=		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        __a		(	self				:      List[Any]							,							__lowerCamelCase				:      List[Any]							,							__lowerCamelCase				:      Any							,							__lowerCamelCase				:      Optional[int]							,							__lowerCamelCase				:      List[Any]							,							__lowerCamelCase				:      Union[str, Any]							,							__lowerCamelCase				:      int   )							->		int:


									'''simple docstring'''

									lowercase							=		TFEsmModel(config=_SCREAMING_SNAKE_CASE   )
									lowercase							=		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									lowercase							=		model(_SCREAMING_SNAKE_CASE   )

									lowercase							=		[input_ids, input_mask]
									lowercase							=		model(_SCREAMING_SNAKE_CASE   )

									lowercase							=		model(_SCREAMING_SNAKE_CASE   )

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

						def        __a		(	self				:      Tuple							,							__lowerCamelCase				:      List[str]							,							__lowerCamelCase				:      List[Any]							,							__lowerCamelCase				:      Tuple							,							__lowerCamelCase				:      Dict							,							__lowerCamelCase				:      Optional[int]							,							__lowerCamelCase				:      Any							,							__lowerCamelCase				:      Any							,							__lowerCamelCase				:      Optional[Any]							,							)							->		Tuple:


									'''simple docstring'''

									lowercase							=		True

									lowercase							=		TFEsmModel(config=_SCREAMING_SNAKE_CASE   )
									lowercase							=		{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''encoder_hidden_states''': encoder_hidden_states,
									    '''encoder_attention_mask''': encoder_attention_mask,
									}
									lowercase							=		model(_SCREAMING_SNAKE_CASE   )

									lowercase							=		[input_ids, input_mask]
									lowercase							=		model(_SCREAMING_SNAKE_CASE							,							encoder_hidden_states=_SCREAMING_SNAKE_CASE   )

									# Also check the case where encoder outputs are not passed
									lowercase							=		model(_SCREAMING_SNAKE_CASE							,							attention_mask=_SCREAMING_SNAKE_CASE   )

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

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


									'''simple docstring'''

									lowercase							=		TFEsmForMaskedLM(config=_SCREAMING_SNAKE_CASE   )
									lowercase							=		model([input_ids, input_mask]   )
									self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.vocab_size)   )

						def        __a		(	self				:      Any							,							__lowerCamelCase				:      int							,							__lowerCamelCase				:      Any							,							__lowerCamelCase				:      Union[str, Any]							,							__lowerCamelCase				:      Dict							,							__lowerCamelCase				:      Optional[Any]							,							__lowerCamelCase				:      Any   )							->		Optional[int]:


									'''simple docstring'''

									lowercase							=		self.num_labels
									lowercase							=		TFEsmForTokenClassification(config=_SCREAMING_SNAKE_CASE   )
									lowercase							=		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									lowercase							=		model(_SCREAMING_SNAKE_CASE   )
									self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.seq_length, self.num_labels)   )



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


									'''simple docstring'''

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






@require_tf
class      __lowercase					(    lowerCAmelCase__       ,     lowerCAmelCase__       ,     unittest.TestCase    ):
						lowercase					 =     (
						    (
						        TFEsmModel,
						        TFEsmForMaskedLM,
						        TFEsmForSequenceClassification,
						        TFEsmForTokenClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						lowercase					 =     (
						    {
						        "feature-extraction": TFEsmModel,
						        "fill-mask": TFEsmForMaskedLM,
						        "text-classification": TFEsmForSequenceClassification,
						        "token-classification": TFEsmForTokenClassification,
						        "zero-shot": TFEsmForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						lowercase					 =     False
						lowercase					 =     False

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


									'''simple docstring'''

									lowercase							=		TFEsmModelTester(self   )
									lowercase							=		ConfigTester(self							,							config_class=_SCREAMING_SNAKE_CASE							,							hidden_size=37   )

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


									'''simple docstring'''

									self.config_tester.run_common_tests()

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


									'''simple docstring'''

									lowercase							=		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE   )

						def        __a		(	self				:      Tuple   )							->		Dict:


									'''simple docstring'''

									lowercase							=		self.model_tester.prepare_config_and_inputs_for_decoder()
									self.model_tester.create_and_check_model_as_decoder(*_SCREAMING_SNAKE_CASE   )

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


									'''simple docstring'''

									lowercase							=		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE   )

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


									'''simple docstring'''

									lowercase							=		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE   )

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


									'''simple docstring'''

									for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowercase							=		TFEsmModel.from_pretrained(_SCREAMING_SNAKE_CASE   )
												self.assertIsNotNone(_SCREAMING_SNAKE_CASE   )

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


									'''simple docstring'''

									pass

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


									'''simple docstring'''

									pass



						def        __a		(	self				:      Any   )							->		int:


									'''simple docstring'''

									lowercase		,lowercase							=		self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												lowercase							=		model_class(_SCREAMING_SNAKE_CASE   )
												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
															lowercase							=		model.get_bias()
															assert isinstance(_SCREAMING_SNAKE_CASE							,							_SCREAMING_SNAKE_CASE   )
															for k, v in name.items():
																		assert isinstance(_SCREAMING_SNAKE_CASE							,							tf.Variable   )
												else:
															lowercase							=		model.get_output_embeddings()
															assert x is None
															lowercase							=		model.get_bias()
															assert name is None






@require_tf
class      __lowercase					(    unittest.TestCase    ):

						@slow
						def        __a		(	self				:      str   )							->		Tuple:


									'''simple docstring'''

									lowercase							=		TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D'''   )

									lowercase							=		tf.constant([[0, 1, 2, 3, 4, 5]]   )
									lowercase							=		model(_SCREAMING_SNAKE_CASE   )[0]
									lowercase							=		[1, 6, 33]
									self.assertEqual(list(output.numpy().shape   )							,							_SCREAMING_SNAKE_CASE   )
									# compare the actual values for a slice.
									lowercase							=		tf.constant(
									    [
									        [
									            [8.92_1518, -10.58_98_14, -6.467_1307],
									            [-6.396_7156, -13.91_13_77, -1.121_1915],
									            [-7.78_1247, -13.95_15_57, -3.74_0592],
									        ]
									    ]   )
									self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()							,							expected_slice.numpy()							,							atol=1E-2   )   )



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


									'''simple docstring'''

									lowercase							=		TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D'''   )

									lowercase							=		tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]   )
									lowercase							=		model(_SCREAMING_SNAKE_CASE   )[0]
									# compare the actual values for a slice.
									lowercase							=		tf.constant(
									    [
									        [
									            [0.1444_3092, 0.5412_5327, 0.324_7739],
									            [0.3034_0484, 0.0052_6676, 0.3107_7722],
									            [0.3227_8043, -0.2498_7096, 0.341_4628],
									        ]
									    ]   )
									self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()							,							expected_slice.numpy()							,							atol=1E-4   )   )






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


A_      =      {
    "configuration_gpt_bigcode": ["GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP", "GPTBigCodeConfig"],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A_      =      [
						    "GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST",
						    "GPTBigCodeForSequenceClassification",
						    "GPTBigCodeForTokenClassification",
						    "GPTBigCodeForCausalLM",
						    "GPTBigCodeModel",
						    "GPTBigCodePreTrainedModel",
						]

if TYPE_CHECKING:
						from .configuration_gpt_bigcode import GPT_BIGCODE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTBigCodeConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_gpt_bigcode import (
												    GPT_BIGCODE_PRETRAINED_MODEL_ARCHIVE_LIST,
												    GPTBigCodeForCausalLM,
												    GPTBigCodeForSequenceClassification,
												    GPTBigCodeForTokenClassification,
												    GPTBigCodeModel,
												    GPTBigCodePreTrainedModel,
												)


else:
						import sys

						A_      =      _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





