

'''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 		__SCREAMING_SNAKE_CASE							:







				def __init__(       self     :		Optional[Any]		,					UpperCAmelCase__     :		List[Any]		,					):
										'''simple docstring'''
										lowercase :							List[Any]    		=parent
										lowercase :							Tuple    		=13
										lowercase :							Any    		=7
										lowercase :							Any    		=True
										lowercase :							Any    		=True
										lowercase :							Dict    		=True
										lowercase :							int    		=99
										lowercase :							List[str]    		=32
										lowercase :							Optional[Any]    		=2
										lowercase :							Dict    		=4
										lowercase :							Dict    		=37
										lowercase :							List[str]    		="""gelu"""
										lowercase :							str    		=0.1
										lowercase :							List[Any]    		=0.1
										lowercase :							Dict    		=512
										lowercase :							str    		=16
										lowercase :							Union[str, Any]    		=2
										lowercase :							Optional[int]    		=0.02
										lowercase :							Optional[int]    		=3
										lowercase :							int    		=4
										lowercase :							Optional[int]    		=None







				def  lowerCamelCase_	(       self     :		Optional[int]   ):
										'''simple docstring'''
										lowercase :							str    		=ids_tensor([self.batch_size, self.seq_length]		,					self.vocab_size   )

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

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

										lowercase :							str    		=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  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										(
										    lowercase				
										) :							int    		=self.prepare_config_and_inputs()

										lowercase :							str    		=True
										lowercase :							Tuple    		=floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
										lowercase :							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  lowerCamelCase_	(       self     :		int		,					UpperCAmelCase__     :		List[Any]		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		Any		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		List[Any]   ):
										'''simple docstring'''
										lowercase :							Union[str, Any]    		=TFEsmModel(config=__A   )
										lowercase :							List[Any]    		={"""input_ids""": input_ids, """attention_mask""": input_mask}
										lowercase :							Dict    		=model(__A   )

										lowercase :							List[str]    		=[input_ids, input_mask]
										lowercase :							List[str]    		=model(__A   )

										lowercase :							Tuple    		=model(__A   )

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







				def  lowerCamelCase_	(       self     :		int		,					UpperCAmelCase__     :		str		,					UpperCAmelCase__     :		Any		,					UpperCAmelCase__     :		Optional[Any]		,					UpperCAmelCase__     :		str		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		str		,					):
										'''simple docstring'''
										lowercase :							List[str]    		=True

										lowercase :							Optional[int]    		=TFEsmModel(config=__A   )
										lowercase :							Dict    		={
										    """input_ids""": input_ids,
										    """attention_mask""": input_mask,
										    """encoder_hidden_states""": encoder_hidden_states,
										    """encoder_attention_mask""": encoder_attention_mask,
										}
										lowercase :							Optional[int]    		=model(__A   )

										lowercase :							List[Any]    		=[input_ids, input_mask]
										lowercase :							int    		=model(__A		,					encoder_hidden_states=__A   )

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

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







				def  lowerCamelCase_	(       self     :		Dict		,					UpperCAmelCase__     :		Union[str, Any]		,					UpperCAmelCase__     :		Dict		,					UpperCAmelCase__     :		List[str]		,					UpperCAmelCase__     :		Tuple		,					UpperCAmelCase__     :		int		,					UpperCAmelCase__     :		Tuple   ):
										'''simple docstring'''
										lowercase :							Optional[Any]    		=TFEsmForMaskedLM(config=__A   )
										lowercase :							Optional[int]    		=model([input_ids, input_mask]   )
										self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.vocab_size)   )







				def  lowerCamelCase_	(       self     :		Optional[int]		,					UpperCAmelCase__     :		Dict		,					UpperCAmelCase__     :		Any		,					UpperCAmelCase__     :		Any		,					UpperCAmelCase__     :		str		,					UpperCAmelCase__     :		Dict		,					UpperCAmelCase__     :		Any   ):
										'''simple docstring'''
										lowercase :							Tuple    		=self.num_labels
										lowercase :							Any    		=TFEsmForTokenClassification(config=__A   )
										lowercase :							Tuple    		={"""input_ids""": input_ids, """attention_mask""": input_mask}
										lowercase :							List[Any]    		=model(__A   )
										self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.num_labels)   )







				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										lowercase :							Tuple    		=self.prepare_config_and_inputs()
										(
										    lowercase				
										) :							str    		=config_and_inputs
										lowercase :							List[str]    		={"""input_ids""": input_ids, """attention_mask""": input_mask}
										return config, inputs_dict





@require_tf
class 		__SCREAMING_SNAKE_CASE							(			_lowercase						,							_lowercase						,							unittest.TestCase      ):
				lowerCamelCase_		       =    (
				    (
				        TFEsmModel,
				        TFEsmForMaskedLM,
				        TFEsmForSequenceClassification,
				        TFEsmForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				lowerCamelCase_		       =    (
				    {
				        '''feature-extraction''': TFEsmModel,
				        '''fill-mask''': TFEsmForMaskedLM,
				        '''text-classification''': TFEsmForSequenceClassification,
				        '''token-classification''': TFEsmForTokenClassification,
				        '''zero-shot''': TFEsmForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False







				def  lowerCamelCase_	(       self     :		int   ):
										'''simple docstring'''
										lowercase :							Union[str, Any]    		=TFEsmModelTester(self   )
										lowercase :							List[Any]    		=ConfigTester(self		,					config_class=__A		,					hidden_size=37   )







				def  lowerCamelCase_	(       self     :		Dict   ):
										'''simple docstring'''
										self.config_tester.run_common_tests()







				def  lowerCamelCase_	(       self     :		int   ):
										'''simple docstring'''
										lowercase :							Optional[int]    		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__A   )







				def  lowerCamelCase_	(       self     :		Dict   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_model_as_decoder(*__A   )







				def  lowerCamelCase_	(       self     :		Tuple   ):
										'''simple docstring'''
										lowercase :							int    		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*__A   )







				def  lowerCamelCase_	(       self     :		Optional[int]   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*__A   )







				@slow
				def  lowerCamelCase_	(       self     :		Dict   ):
										'''simple docstring'''
										for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowercase :							Tuple    		=TFEsmModel.from_pretrained(__A   )
																self.assertIsNotNone(__A   )







				@unittest.skip('''Protein models do not support embedding resizing.'''   )
				def  lowerCamelCase_	(       self     :		int   ):
										'''simple docstring'''
										pass







				@unittest.skip('''Protein models do not support embedding resizing.'''   )
				def  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										pass







				def  lowerCamelCase_	(       self     :		Optional[Any]   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=self.model_tester.prepare_config_and_inputs_for_common()

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





@require_tf
class 		__SCREAMING_SNAKE_CASE							(			unittest.TestCase      ):







				@slow
				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										lowercase :							Optional[int]    		=TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D'''   )

										lowercase :							Union[str, Any]    		=tf.constant([[0, 1, 2, 3, 4, 5]]   )
										lowercase :							Optional[int]    		=model(__A   )[0]
										lowercase :							Dict    		=[1, 6, 33]
										self.assertEqual(list(output.numpy().shape   )		,					__A   )
										# compare the actual values for a slice.
										lowercase :							Union[str, Any]    		=tf.constant(
										    [
										        [
										            [8.92_15_18, -10.589814, -6.4_67_13_07],
										            [-6.3_96_71_56, -13.911377, -1.1_21_19_15],
										            [-7.78_12_47, -13.951557, -3.74_05_92],
										        ]
										    ]   )
										self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()		,					expected_slice.numpy()		,					atol=1E-2   )   )







				@slow
				def  lowerCamelCase_	(       self     :		Optional[int]   ):
										'''simple docstring'''
										lowercase :							str    		=TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D'''   )

										lowercase :							Optional[Any]    		=tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]   )
										lowercase :							Tuple    		=model(__A   )[0]
										# compare the actual values for a slice.
										lowercase :							Union[str, Any]    		=tf.constant(
										    [
										        [
										            [0.14_44_30_92, 0.54_12_53_27, 0.3_24_77_39],
										            [0.30_34_04_84, 0.00_52_66_76, 0.31_07_77_22],
										            [0.32_27_80_43, -0.24_98_70_96, 0.3_41_46_28],
										        ]
										    ]   )
										self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()		,					expected_slice.numpy()		,					atol=1E-4   )   )








from __future__ import annotations
def 							__SCREAMING_SNAKE_CASE     (   a__    :   List[str]			,a__    :   Dict			,a__    :   Union[str, Any]			,a__    :   Any       )					->				Optional[int]:  # noqa: E741
	while r - l > 1:
		__A :			Any		    =			(l + r) // 2
		if v[m] >= key:
			__A :			Optional[int]		    =			m
		else:
			__A :			List[Any]		    =			m  # noqa: E741
	return r
def 							__SCREAMING_SNAKE_CASE     (   a__    :   list[int]       )					->				int:
	if len(a__       ) == 0:
		return 0

	__A :			str		    =			[0] * len(a__       )
	__A :			List[str]		    =			1

	__A :			List[Any]		    =			v[0]

	for i in range(1			,len(a__       )       ):
		if v[i] < tail[0]:
			__A :			int		    =			v[i]
		elif v[i] > tail[length - 1]:
			__A :			Union[str, Any]		    =			v[i]
			length += 1
		else:
			__A :			Any		    =			v[i]

	return length


if __name__ == "__main__":
			import doctest

			doctest.testmod()





