





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





   def __init__(			self       ,      snake_case       ,      )					->	List[Any]:
      """simple docstring"""





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





   def 						_snake_case			(			self   )					->	str:
      """simple docstring"""





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

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

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

      a__						:    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 						_snake_case			(			self   )					->	List[Any]:
      """simple docstring"""





      (
          (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  
      )						:    Tuple   		=	self.prepare_config_and_inputs()

      a__						:    Optional[Any]   		=	True
      a__						:    List[Any]   		=	floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
      a__						:    Dict   		=	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 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case   )					->	List[Any]:
      """simple docstring"""





      a__						:    Tuple   		=	TFEsmModel(config=snake_case   )
      a__						:    List[Any]   		=	{"input_ids": input_ids, "attention_mask": input_mask}
      a__						:    str   		=	model(snake_case   )

      a__						:    Any   		=	[input_ids, input_mask]
      a__						:    Any   		=	model(snake_case   )

      a__						:    List[Any]   		=	model(snake_case   )

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





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      )					->	int:
      """simple docstring"""





      a__						:    List[Any]   		=	True

      a__						:    List[str]   		=	TFEsmModel(config=snake_case   )
      a__						:    Optional[int]   		=	{
          "input_ids": input_ids,
          "attention_mask": input_mask,
          "encoder_hidden_states": encoder_hidden_states,
          "encoder_attention_mask": encoder_attention_mask,
      }
      a__						:    int   		=	model(snake_case   )

      a__						:    List[Any]   		=	[input_ids, input_mask]
      a__						:    Tuple   		=	model(snake_case       ,      encoder_hidden_states=snake_case   )

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

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





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case   )					->	Tuple:
      """simple docstring"""





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





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case   )					->	Any:
      """simple docstring"""





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





   def 						_snake_case			(			self   )					->	Optional[Any]:
      """simple docstring"""





      a__						:    Optional[int]   		=	self.prepare_config_and_inputs()
      (
          (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  (
          a__
      )		,  
      )						:    int   		=	config_and_inputs
      a__						:    int   		=	{"input_ids": input_ids, "attention_mask": input_mask}
      return config, inputs_dict







@require_tf
class 	__lowerCAmelCase      (       _UpperCamelCase		,_UpperCamelCase		,unittest.TestCase	):
   _UpperCamelCase			:    Union[str, Any]  		=					(
       (
           TFEsmModel,
           TFEsmForMaskedLM,
           TFEsmForSequenceClassification,
           TFEsmForTokenClassification,
       )
       if is_tf_available()
       else ()
   )
   _UpperCamelCase			:    int  		=					(
       {
           """feature-extraction""": TFEsmModel,
           """fill-mask""": TFEsmForMaskedLM,
           """text-classification""": TFEsmForSequenceClassification,
           """token-classification""": TFEsmForTokenClassification,
           """zero-shot""": TFEsmForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   _UpperCamelCase			:    Optional[int]  		=					False
   _UpperCamelCase			:    Dict  		=					False





   def 						_snake_case			(			self   )					->	Union[str, Any]:
      """simple docstring"""





      a__						:    int   		=	TFEsmModelTester(self   )
      a__						:    str   		=	ConfigTester(self       ,      config_class=snake_case       ,      hidden_size=37   )





   def 						_snake_case			(			self   )					->	List[str]:
      """simple docstring"""





      self.config_tester.run_common_tests()





   def 						_snake_case			(			self   )					->	List[Any]:
      """simple docstring"""





      a__						:    Union[str, Any]   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*snake_case   )





   def 						_snake_case			(			self   )					->	Optional[Any]:
      """simple docstring"""





      a__						:    Tuple   		=	self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_model_as_decoder(*snake_case   )





   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      a__						:    Dict   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*snake_case   )





   def 						_snake_case			(			self   )					->	Union[str, Any]:
      """simple docstring"""





      a__						:    Union[str, Any]   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*snake_case   )





   @slow
   def 						_snake_case			(			self   )					->	List[str]:
      """simple docstring"""





      for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a__						:    Dict   		=	TFEsmModel.from_pretrained(snake_case   )
         self.assertIsNotNone(snake_case   )





   @unittest.skip("Protein models do not support embedding resizing."   )
   def 						_snake_case			(			self   )					->	str:
      """simple docstring"""





      pass





   @unittest.skip("Protein models do not support embedding resizing."   )
   def 						_snake_case			(			self   )					->	str:
      """simple docstring"""





      pass





   def 						_snake_case			(			self   )					->	Optional[int]:
      """simple docstring"""





      a__		,  a__						:    Optional[int]   		=	self.model_tester.prepare_config_and_inputs_for_common()

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







@require_tf
class 	__lowerCAmelCase      (       unittest.TestCase	):





   @slow
   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      a__						:    List[Any]   		=	TFEsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"   )

      a__						:    Any   		=	tf.constant([[0, 1, 2, 3, 4, 5]]   )
      a__						:    Dict   		=	model(snake_case   )[0]
      a__						:    Optional[Any]   		=	[1, 6, 33]
      self.assertEqual(list(output.numpy().shape   )       ,      snake_case   )
      # compare the actual values for a slice.
      a__						:    Tuple   		=	tf.constant(
          [
              [
                  [8.921_518, -10.589_814, -6.4_671_307],
                  [-6.3_967_156, -13.911_377, -1.1_211_915],
                  [-7.781_247, -13.951_557, -3.740_592],
              ]
          ]   )
      self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()       ,      expected_slice.numpy()       ,      atol=1E-2   )   )





   @slow
   def 						_snake_case			(			self   )					->	Any:
      """simple docstring"""





      a__						:    str   		=	TFEsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"   )

      a__						:    str   		=	tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]   )
      a__						:    Optional[Any]   		=	model(snake_case   )[0]
      # compare the actual values for a slice.
      a__						:    Any   		=	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   )   )








def    _A	(   lowerCamelCase = 200 ):
   a__						:    List[str]   		=	[1, 2, 5, 10, 20, 50, 100, 200]
   a__						:    Dict   		=	[0] * (pence + 1)
   a__						:    int   		=	1  # base case: 1 way to make 0 pence

   for coin in coins:
      for i in range(lowerCamelCase				,			pence + 1				,			1 ):
         number_of_ways[i] += number_of_ways[i - coin]
   return number_of_ways[pence]


if __name__ == "__main__":
  assert solution(2_0_0) == 7_3_6_8_2

