



"""simple docstring"""

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     :







						"""simple docstring"""







						def __init__(self   ,		lowercase__   ,		):
											snake_case_					:		Tuple 					=					parent
											snake_case_					:		Tuple 					=					13
											snake_case_					:		List[str] 					=					7
											snake_case_					:		Optional[Any] 					=					True
											snake_case_					:		Tuple 					=					True
											snake_case_					:		Optional[Any] 					=					True
											snake_case_					:		Dict 					=					99
											snake_case_					:		str 					=					32
											snake_case_					:		List[str] 					=					2
											snake_case_					:		int 					=					4
											snake_case_					:		List[str] 					=					37
											snake_case_					:		Dict 					=					"""gelu"""
											snake_case_					:		Any 					=					0.1
											snake_case_					:		Dict 					=					0.1
											snake_case_					:		Union[str, Any] 					=					5_12
											snake_case_					:		List[str] 					=					16
											snake_case_					:		List[str] 					=					2
											snake_case_					:		Dict 					=					0.02
											snake_case_					:		Dict 					=					3
											snake_case_					:		Any 					=					4
											snake_case_					:		Union[str, Any] 					=					None







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Union[str, Any] 					=					ids_tensor([self.batch_size, self.seq_length]   ,		self.vocab_size       )

											snake_case_					:		Union[str, Any] 					=					None
											if self.use_input_mask:
																snake_case_					:		Optional[Any] 					=					random_attention_mask([self.batch_size, self.seq_length]       )

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

											snake_case_					:		Union[str, 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       ):
											(
											    (
											    snake_case_						
											)					,			(
											    snake_case_						
											)					,			(
											    snake_case_						
											)					,			(
											    snake_case_						
											)					,			(
											    snake_case_						
											)					,			(
											    snake_case_						
											)					,									
											)					:		Any 					=					self.prepare_config_and_inputs()

											snake_case_					:		List[str] 					=					True
											snake_case_					:		str 					=					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
											snake_case_					:		Tuple 					=					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   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__       ):
											snake_case_					:		int 					=					TFEsmModel(config=__UpperCamelCase       )
											snake_case_					:		Dict 					=					{"""input_ids""": input_ids, """attention_mask""": input_mask}
											snake_case_					:		Any 					=					model(__UpperCamelCase       )

											snake_case_					:		Optional[Any] 					=					[input_ids, input_mask]
											snake_case_					:		Dict 					=					model(__UpperCamelCase       )

											snake_case_					:		Tuple 					=					model(__UpperCamelCase       )

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







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		):
											snake_case_					:		List[Any] 					=					True

											snake_case_					:		Tuple 					=					TFEsmModel(config=__UpperCamelCase       )
											snake_case_					:		List[Any] 					=					{
											    """input_ids""": input_ids,
											    """attention_mask""": input_mask,
											    """encoder_hidden_states""": encoder_hidden_states,
											    """encoder_attention_mask""": encoder_attention_mask,
											}
											snake_case_					:		Union[str, Any] 					=					model(__UpperCamelCase       )

											snake_case_					:		str 					=					[input_ids, input_mask]
											snake_case_					:		Tuple 					=					model(__UpperCamelCase   ,		encoder_hidden_states=__UpperCamelCase       )

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

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







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







						def 						__UpperCamelCase							(self   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__   ,		lowercase__       ):
											snake_case_					:		List[Any] 					=					self.num_labels
											snake_case_					:		List[Any] 					=					TFEsmForTokenClassification(config=__UpperCamelCase       )
											snake_case_					:		Union[str, Any] 					=					{"""input_ids""": input_ids, """attention_mask""": input_mask}
											snake_case_					:		Union[str, Any] 					=					model(__UpperCamelCase       )
											self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.seq_length, self.num_labels)       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		str 					=					self.prepare_config_and_inputs()
											(
											    (
											    snake_case_						
											)					,			(
											    snake_case_						
											)					,			(
											    snake_case_						
											)					,			(
											    snake_case_						
											)					,			(
											    snake_case_						
											)					,			(
											    snake_case_						
											)					,									
											)					:		Optional[Any] 					=					config_and_inputs
											snake_case_					:		Optional[Any] 					=					{"""input_ids""": input_ids, """attention_mask""": input_mask}
											return config, inputs_dict







@require_tf
class    __lowercase     (					_UpperCAmelCase					,    _UpperCAmelCase					,    unittest.TestCase):







						"""simple docstring"""







						_A		:							Union[str, Any]							       =		(
						    (
						        TFEsmModel,
						        TFEsmForMaskedLM,
						        TFEsmForSequenceClassification,
						        TFEsmForTokenClassification,
						    )
						    if is_tf_available()
						    else ()
						)
						_A		:							Union[str, Any]							       =		(
						    {
						        """feature-extraction""": TFEsmModel,
						        """fill-mask""": TFEsmForMaskedLM,
						        """text-classification""": TFEsmForSequenceClassification,
						        """token-classification""": TFEsmForTokenClassification,
						        """zero-shot""": TFEsmForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						_A		:							List[str]							       =		False
						_A		:							List[Any]							       =		False







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Dict 					=					TFEsmModelTester(self       )
											snake_case_					:		Dict 					=					ConfigTester(self   ,		config_class=__UpperCamelCase   ,		hidden_size=37       )







						def 						__UpperCamelCase							(self       ):
											self.config_tester.run_common_tests()







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







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Any 					=					self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_model_as_decoder(*__UpperCamelCase       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		List[str] 					=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*__UpperCamelCase       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Optional[Any] 					=					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase       )







						@slow
						def 						__UpperCamelCase							(self       ):
											for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																snake_case_					:		List[Any] 					=					TFEsmModel.from_pretrained(__UpperCamelCase       )
																self.assertIsNotNone(__UpperCamelCase       )







						@unittest.skip("""Protein models do not support embedding resizing."""       )
						def 						__UpperCamelCase							(self       ):
											pass







						@unittest.skip("""Protein models do not support embedding resizing."""       )
						def 						__UpperCamelCase							(self       ):
											pass







						def 						__UpperCamelCase							(self       ):
											snake_case_					,			snake_case_					:		List[str] 					=					self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																snake_case_					:		List[str] 					=					model_class(__UpperCamelCase       )
																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(__UpperCamelCase   ,		__UpperCamelCase       )
																					for k, v in name.items():
																										assert isinstance(__UpperCamelCase   ,		tf.Variable       )
																else:
																					snake_case_					:		Union[str, Any] 					=					model.get_output_embeddings()
																					assert x is None
																					snake_case_					:		Union[str, Any] 					=					model.get_bias()
																					assert name is None







@require_tf
class    __lowercase     (					unittest.TestCase):







						"""simple docstring"""







						@slow
						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Optional[Any] 					=					TFEsmForMaskedLM.from_pretrained("""facebook/esm2_t6_8M_UR50D"""       )

											snake_case_					:		Optional[int] 					=					tf.constant([[0, 1, 2, 3, 4, 5]]       )
											snake_case_					:		Union[str, Any] 					=					model(__UpperCamelCase       )[0]
											snake_case_					:		Tuple 					=					[1, 6, 33]
											self.assertEqual(list(output.numpy().shape       )   ,		__UpperCamelCase       )
											# compare the actual values for a slice.
											snake_case_					:		Tuple 					=					tf.constant(
											    [
											        [
											            [8.921518, -10.58_98_14, -6.4671307],
											            [-6.3967156, -13.91_13_77, -1.1211915],
											            [-7.781247, -13.95_15_57, -3.740592],
											        ]
											    ]       )
											self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()   ,		expected_slice.numpy()   ,		atol=1e-2       )       )







						@slow
						def 						__UpperCamelCase							(self       ):
											snake_case_					:		List[str] 					=					TFEsmModel.from_pretrained("""facebook/esm2_t6_8M_UR50D"""       )

											snake_case_					:		Dict 					=					tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]       )
											snake_case_					:		Tuple 					=					model(__UpperCamelCase       )[0]
											# compare the actual values for a slice.
											snake_case_					:		Union[str, Any] 					=					tf.constant(
											    [
											        [
											            [0.14443092, 0.54125327, 0.3247739],
											            [0.30340484, 0.00526676, 0.31077722],
											            [0.32278043, -0.24987096, 0.3414628],
											        ]
											    ]       )
											self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()   ,		expected_slice.numpy()   ,		atol=1e-4       )       )





"""simple docstring"""

import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class    __lowercase     (					_UpperCAmelCase):







						"""simple docstring"""







						_A		:							Union[str, Any]							       =		["""image_processor""", """tokenizer"""]
						_A		:							str							       =		"""ChineseCLIPImageProcessor"""
						_A		:							Tuple							       =		("""BertTokenizer""", """BertTokenizerFast""")







						def __init__(self   ,		lowercase__=None   ,		lowercase__=None   ,		**lowercase__       ):
											snake_case_					:		Any 					=					None
											if "feature_extractor" in kwargs:
																warnings.warn(
																    """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
																    """ instead."""   ,		lowercase__   ,		)
																snake_case_					:		Optional[Any] 					=					kwargs.pop("""feature_extractor"""       )

											snake_case_					:		str 					=					image_processor if image_processor is not None else feature_extractor
											if image_processor is None:
																raise ValueError("""You need to specify an `image_processor`."""       )
											if tokenizer is None:
																raise ValueError("""You need to specify a `tokenizer`."""       )

											super().__init__(lowercase__   ,		lowercase__       )
											snake_case_					:		Union[str, Any] 					=					self.image_processor







						def __call__(self   ,		lowercase__=None   ,		lowercase__=None   ,		lowercase__=None   ,		**lowercase__       ):

											if text is None and images is None:
																raise ValueError("""You have to specify either text or images. Both cannot be none."""       )

											if text is not None:
																snake_case_					:		Any 					=					self.tokenizer(lowercase__   ,		return_tensors=lowercase__   ,		**lowercase__       )

											if images is not None:
																snake_case_					:		Tuple 					=					self.image_processor(lowercase__   ,		return_tensors=lowercase__   ,		**lowercase__       )

											if text is not None and images is not None:
																snake_case_					:		List[Any] 					=					image_features.pixel_values
																return encoding
											elif text is not None:
																return encoding
											else:
																return BatchEncoding(data=dict(**lowercase__       )   ,		tensor_type=lowercase__       )







						def 						__UpperCamelCase							(self   ,		*lowercase__   ,		**lowercase__       ):
											return self.tokenizer.batch_decode(*lowercase__   ,		**lowercase__       )







						def 						__UpperCamelCase							(self   ,		*lowercase__   ,		**lowercase__       ):
											return self.tokenizer.decode(*lowercase__   ,		**lowercase__       )







						@property
						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Optional[int] 					=					self.tokenizer.model_input_names
											snake_case_					:		Union[str, Any] 					=					self.image_processor.model_input_names
											return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names       )       )







						@property
						def 						__UpperCamelCase							(self       ):
											warnings.warn(
											    """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""   ,		lowercase__   ,		)
											return self.image_processor_class




