


"""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 			UpperCamelCase_     :

	"""simple docstring"""





	def __init__(			self      :    Dict      ,      UpperCAmelCase__      :    Union[str, Any]      ,      )	->     int:
						__SCREAMING_SNAKE_CASE						       =    parent
						__SCREAMING_SNAKE_CASE						       =    1_3
						__SCREAMING_SNAKE_CASE						       =    7
						__SCREAMING_SNAKE_CASE						       =    True
						__SCREAMING_SNAKE_CASE						       =    True
						__SCREAMING_SNAKE_CASE						       =    True
						__SCREAMING_SNAKE_CASE						       =    9_9
						__SCREAMING_SNAKE_CASE						       =    3_2
						__SCREAMING_SNAKE_CASE						       =    2
						__SCREAMING_SNAKE_CASE						       =    4
						__SCREAMING_SNAKE_CASE						       =    3_7
						__SCREAMING_SNAKE_CASE						       =    '''gelu'''
						__SCREAMING_SNAKE_CASE						       =    0.1
						__SCREAMING_SNAKE_CASE						       =    0.1
						__SCREAMING_SNAKE_CASE						       =    5_1_2
						__SCREAMING_SNAKE_CASE						       =    1_6
						__SCREAMING_SNAKE_CASE						       =    2
						__SCREAMING_SNAKE_CASE						       =    0.02
						__SCREAMING_SNAKE_CASE						       =    3
						__SCREAMING_SNAKE_CASE						       =    4
						__SCREAMING_SNAKE_CASE						       =    None




	def   UpperCAmelCase_				(			self      :    List[str]       )	->     int:
						__SCREAMING_SNAKE_CASE						       =    ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size       )

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

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

						__SCREAMING_SNAKE_CASE						       =    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      :    Optional[Any]       )	->     List[str]:
						(
						    __SCREAMING_SNAKE_CASE    
						)						       =    self.prepare_config_and_inputs()

						__SCREAMING_SNAKE_CASE						       =    True
						__SCREAMING_SNAKE_CASE						       =    floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
						__SCREAMING_SNAKE_CASE						       =    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      :    int      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    Optional[Any]       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    TFEsmModel(config=_lowerCamelCase       )
						__SCREAMING_SNAKE_CASE						       =    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
						__SCREAMING_SNAKE_CASE						       =    model(_lowerCamelCase       )

						__SCREAMING_SNAKE_CASE						       =    [input_ids, input_mask]
						__SCREAMING_SNAKE_CASE						       =    model(_lowerCamelCase       )

						__SCREAMING_SNAKE_CASE						       =    model(_lowerCamelCase       )

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




	def   UpperCAmelCase_				(			self      :    str      ,      UpperCAmelCase__      :    Tuple      ,      UpperCAmelCase__      :    Union[str, Any]      ,      UpperCAmelCase__      :    Tuple      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    Tuple      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    str      ,      )	->     Optional[int]:
						__SCREAMING_SNAKE_CASE						       =    True

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

						__SCREAMING_SNAKE_CASE						       =    [input_ids, input_mask]
						__SCREAMING_SNAKE_CASE						       =    model(_lowerCamelCase      ,      encoder_hidden_states=_lowerCamelCase       )

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

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




	def   UpperCAmelCase_				(			self      :    Dict      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    Union[str, Any]      ,      UpperCAmelCase__      :    Optional[Any]      ,      UpperCAmelCase__      :    Optional[int]       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    TFEsmForMaskedLM(config=_lowerCamelCase       )
						__SCREAMING_SNAKE_CASE						       =    model([input_ids, input_mask]       )
						self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.vocab_size)       )




	def   UpperCAmelCase_				(			self      :    str      ,      UpperCAmelCase__      :    Optional[int]      ,      UpperCAmelCase__      :    List[str]      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    int      ,      UpperCAmelCase__      :    List[Any]      ,      UpperCAmelCase__      :    Dict       )	->     Optional[int]:
						__SCREAMING_SNAKE_CASE						       =    self.num_labels
						__SCREAMING_SNAKE_CASE						       =    TFEsmForTokenClassification(config=_lowerCamelCase       )
						__SCREAMING_SNAKE_CASE						       =    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
						__SCREAMING_SNAKE_CASE						       =    model(_lowerCamelCase       )
						self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.num_labels)       )




	def   UpperCAmelCase_				(			self      :    Optional[int]       )	->     Any:
						__SCREAMING_SNAKE_CASE						       =    self.prepare_config_and_inputs()
						(
						    __SCREAMING_SNAKE_CASE    
						)						       =    config_and_inputs
						__SCREAMING_SNAKE_CASE						       =    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
						return config, inputs_dict




@require_tf
class 			UpperCamelCase_     (			a__      ,  a__      ,  unittest.TestCase):

	"""simple docstring"""





	snake_case__    :  str    =      (
	    (
	        TFEsmModel,
	        TFEsmForMaskedLM,
	        TFEsmForSequenceClassification,
	        TFEsmForTokenClassification,
	    )
	    if is_tf_available()
	    else ()
	)
	snake_case__    :  Any    =      (
	    {
	        'feature-extraction': TFEsmModel,
	        'fill-mask': TFEsmForMaskedLM,
	        'text-classification': TFEsmForSequenceClassification,
	        'token-classification': TFEsmForTokenClassification,
	        'zero-shot': TFEsmForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	snake_case__    :  Tuple    =      False
	snake_case__    :  Dict    =      False




	def   UpperCAmelCase_				(			self      :    Dict       )	->     Tuple:
						__SCREAMING_SNAKE_CASE						       =    TFEsmModelTester(self       )
						__SCREAMING_SNAKE_CASE						       =    ConfigTester(self      ,      config_class=_lowerCamelCase      ,      hidden_size=3_7       )




	def   UpperCAmelCase_				(			self      :    List[Any]       )	->     Optional[int]:
						self.config_tester.run_common_tests()




	def   UpperCAmelCase_				(			self      :    Optional[int]       )	->     Union[str, Any]:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*_lowerCamelCase       )




	def   UpperCAmelCase_				(			self      :    Optional[Any]       )	->     int:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_model_as_decoder(*_lowerCamelCase       )




	def   UpperCAmelCase_				(			self      :    int       )	->     str:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*_lowerCamelCase       )




	def   UpperCAmelCase_				(			self      :    Optional[int]       )	->     Optional[Any]:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*_lowerCamelCase       )




	@slow
	def   UpperCAmelCase_				(			self      :    Optional[int]       )	->     str:
						for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__SCREAMING_SNAKE_CASE						       =    TFEsmModel.from_pretrained(_lowerCamelCase       )
											self.assertIsNotNone(_lowerCamelCase       )




	@unittest.skip("Protein models do not support embedding resizing."       )
	def   UpperCAmelCase_				(			self      :    str       )	->     Tuple:
						pass




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




	def   UpperCAmelCase_				(			self      :    Dict       )	->     int:
						__SCREAMING_SNAKE_CASE						       =    self.model_tester.prepare_config_and_inputs_for_common()

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




@require_tf
class 			UpperCamelCase_     (			unittest.TestCase):

	"""simple docstring"""





	@slow
	def   UpperCAmelCase_				(			self      :    int       )	->     Optional[int]:
						__SCREAMING_SNAKE_CASE						       =    TFEsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"       )

						__SCREAMING_SNAKE_CASE						       =    tf.constant([[0, 1, 2, 3, 4, 5]]       )
						__SCREAMING_SNAKE_CASE						       =    model(_lowerCamelCase       )[0]
						__SCREAMING_SNAKE_CASE						       =    [1, 6, 3_3]
						self.assertEqual(list(output.numpy().shape       )      ,      _lowerCamelCase       )
						# compare the actual values for a slice.
						__SCREAMING_SNAKE_CASE						       =    tf.constant(
						    [
						        [
						            [8.921_518, -1_0.5_8_9_8_1_4, -6.4_671_307],
						            [-6.3_967_156, -1_3.9_1_1_3_7_7, -1.1_211_915],
						            [-7.781_247, -1_3.9_5_1_5_5_7, -3.740_592],
						        ]
						    ]       )
						self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()      ,      expected_slice.numpy()      ,      atol=1E-2       )       )




	@slow
	def   UpperCAmelCase_				(			self      :    List[str]       )	->     Dict:
						__SCREAMING_SNAKE_CASE						       =    TFEsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"       )

						__SCREAMING_SNAKE_CASE						       =    tf.constant([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]]       )
						__SCREAMING_SNAKE_CASE						       =    model(_lowerCamelCase       )[0]
						# compare the actual values for a slice.
						__SCREAMING_SNAKE_CASE						       =    tf.constant(
						    [
						        [
						            [0.14_443_092, 0.54_125_327, 0.3_247_739],
						            [0.30_340_484, 0.00_526_676, 0.31_077_722],
						            [0.32_278_043, -0.24_987_096, 0.3_414_628],
						        ]
						    ]       )
						self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()      ,      expected_slice.numpy()      ,      atol=1E-4       )       )




"""simple docstring"""







def        UpperCAmelCase__	(lowerCAmelCase_  ,						lowerCAmelCase_       ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						       =    [0 for i in range(r + 1       )]
					# nc0 = 1
					__SCREAMING_SNAKE_CASE						       =    1
					for i in range(1  ,						n + 1       ):
										# to compute current row from previous row.
										__SCREAMING_SNAKE_CASE						       =    min(lowerCAmelCase_  ,						lowerCAmelCase_       )
										while j > 0:
															c[j] += c[j - 1]
															j -= 1
					return c[r]



print(binomial_coefficient(n=1_0, r=5))
