






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    ,				SCREAMING_SNAKE_CASE_    ,				)				->      List[str]:
          UpperCamelCase       :List[Any]								=   parent
          UpperCamelCase       :int								=   13
          UpperCamelCase       :Union[str, Any]								=   7
          UpperCamelCase       :int								=   True
          UpperCamelCase       :List[Any]								=   True
          UpperCamelCase       :Optional[int]								=   True
          UpperCamelCase       :Tuple								=   99
          UpperCamelCase       :str								=   32
          UpperCamelCase       :Dict								=   2
          UpperCamelCase       :Optional[Any]								=   4
          UpperCamelCase       :int								=   37
          UpperCamelCase       :List[str]								=   '''gelu'''
          UpperCamelCase       :List[Any]								=   0.1
          UpperCamelCase       :int								=   0.1
          UpperCamelCase       :List[str]								=   512
          UpperCamelCase       :int								=   16
          UpperCamelCase       :List[Any]								=   2
          UpperCamelCase       :Any								=   0.02
          UpperCamelCase       :int								=   3
          UpperCamelCase       :Optional[int]								=   4
          UpperCamelCase       :List[Any]								=   None
      def     UpperCAmelCase						(				self )				->      Tuple:
          UpperCamelCase       :int								=   ids_tensor([self.batch_size, self.seq_length]    ,				self.vocab_size )

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

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

          UpperCamelCase       :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 )				->      int:
          (
              (
              UpperCamelCase							
          )							,   (
              UpperCamelCase							
          )							,   (
              UpperCamelCase							
          )							,   (
              UpperCamelCase							
          )							,   (
              UpperCamelCase							
          )							,   (
              UpperCamelCase							
          )							,   							
          )       :Any								=   self.prepare_config_and_inputs()

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

          UpperCamelCase       :Any								=   [input_ids, input_mask]
          UpperCamelCase       :Optional[int]								=   model(__lowerCAmelCase )

          UpperCamelCase       :str								=   model(__lowerCAmelCase )

          self.parent.assertEqual(result.last_hidden_state.shape    ,				(self.batch_size, self.seq_length, self.hidden_size) )
      def     UpperCAmelCase						(				self    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				)				->      str:
          UpperCamelCase       :List[str]								=   True

          UpperCamelCase       :Optional[Any]								=   TFEsmModel(config=__lowerCAmelCase )
          UpperCamelCase       :Dict								=   {
              '''input_ids''': input_ids,
              '''attention_mask''': input_mask,
              '''encoder_hidden_states''': encoder_hidden_states,
              '''encoder_attention_mask''': encoder_attention_mask,
          }
          UpperCamelCase       :Union[str, Any]								=   model(__lowerCAmelCase )

          UpperCamelCase       :Any								=   [input_ids, input_mask]
          UpperCamelCase       :Any								=   model(__lowerCAmelCase    ,				encoder_hidden_states=__lowerCAmelCase )

          # Also check the case where encoder outputs are not passed
          UpperCamelCase       :int								=   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    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_ )				->      List[str]:
          UpperCamelCase       :str								=   TFEsmForMaskedLM(config=__lowerCAmelCase )
          UpperCamelCase       :Optional[Any]								=   model([input_ids, input_mask] )
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.seq_length, self.vocab_size) )
      def     UpperCAmelCase						(				self    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_ )				->      Union[str, Any]:
          UpperCamelCase       :List[str]								=   self.num_labels
          UpperCamelCase       :Dict								=   TFEsmForTokenClassification(config=__lowerCAmelCase )
          UpperCamelCase       :str								=   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
          UpperCamelCase       :Union[str, Any]								=   model(__lowerCAmelCase )
          self.parent.assertEqual(result.logits.shape    ,				(self.batch_size, self.seq_length, self.num_labels) )




      def     UpperCAmelCase						(				self )				->      int:
          UpperCamelCase       :List[Any]								=   self.prepare_config_and_inputs()
          (
              (
              UpperCamelCase							
          )							,   (
              UpperCamelCase							
          )							,   (
              UpperCamelCase							
          )							,   (
              UpperCamelCase							
          )							,   (
              UpperCamelCase							
          )							,   (
              UpperCamelCase							
          )							,   							
          )       :str								=   config_and_inputs
          UpperCamelCase       :Tuple								=   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
          return config, inputs_dict







@require_tf
class 			UpperCAmelCase_						(					lowercase,	lowercase,	unittest.TestCase ):



      """simple docstring"""
      UpperCamelCase_				:					Optional[Any]           =(
          (
              TFEsmModel,
              TFEsmForMaskedLM,
              TFEsmForSequenceClassification,
              TFEsmForTokenClassification,
          )
          if is_tf_available()
          else ()
      )
      UpperCamelCase_				:					Dict           =(
          {
              'feature-extraction': TFEsmModel,
              'fill-mask': TFEsmForMaskedLM,
              'text-classification': TFEsmForSequenceClassification,
              'token-classification': TFEsmForTokenClassification,
              'zero-shot': TFEsmForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      UpperCamelCase_				:					List[Any]           =False
      UpperCamelCase_				:					Optional[int]           =False
      def     UpperCAmelCase						(				self )				->      Any:
          UpperCamelCase       :Any								=   TFEsmModelTester(self )
          UpperCamelCase       :List[Any]								=   ConfigTester(self    ,				config_class=__lowerCAmelCase    ,				hidden_size=37 )
      def     UpperCAmelCase						(				self )				->      Optional[Any]:
          self.config_tester.run_common_tests()
      def     UpperCAmelCase						(				self )				->      Optional[Any]:
          UpperCamelCase       :Any								=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__lowerCAmelCase )
      def     UpperCAmelCase						(				self )				->      int:
          UpperCamelCase       :Optional[int]								=   self.model_tester.prepare_config_and_inputs_for_decoder()
          self.model_tester.create_and_check_model_as_decoder(*__lowerCAmelCase )
      def     UpperCAmelCase						(				self )				->      Union[str, Any]:
          UpperCamelCase       :Dict								=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase )
      def     UpperCAmelCase						(				self )				->      int:
          UpperCamelCase       :Union[str, Any]								=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase )
      @slow
      def     UpperCAmelCase						(				self )				->      int:



          for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCamelCase       :int								=   TFEsmModel.from_pretrained(__lowerCAmelCase )
              self.assertIsNotNone(__lowerCAmelCase )
      @unittest.skip('''Protein models do not support embedding resizing.''' )
      def     UpperCAmelCase						(				self )				->      int:
          pass
      @unittest.skip('''Protein models do not support embedding resizing.''' )
      def     UpperCAmelCase						(				self )				->      Optional[Any]:
          pass




      def     UpperCAmelCase						(				self )				->      int:
          UpperCamelCase							,   UpperCamelCase       :Any								=   self.model_tester.prepare_config_and_inputs_for_common()

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







@require_tf
class 			UpperCAmelCase_						(					unittest.TestCase ):



      """simple docstring"""
      @slow
      def     UpperCAmelCase						(				self )				->      Tuple:
          UpperCamelCase       :Union[str, Any]								=   TFEsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D''' )

          UpperCamelCase       :Any								=   tf.constant([[0, 1, 2, 3, 4, 5]] )
          UpperCamelCase       :List[str]								=   model(__lowerCAmelCase )[0]
          UpperCamelCase       :List[str]								=   [1, 6, 33]
          self.assertEqual(list(output.numpy().shape )    ,				__lowerCAmelCase )
          # compare the actual values for a slice.
          UpperCamelCase       :int								=   tf.constant(
              [
                  [
                      [8.92_1518, -10.58_9814, -6.467_1307],
                      [-6.396_7156, -13.91_1377, -1.121_1915],
                      [-7.78_1247, -13.95_1557, -3.74_0592],
                  ]
              ] )
          self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()    ,				expected_slice.numpy()    ,				atol=1e-2 ) )




      @slow
      def     UpperCAmelCase						(				self )				->      List[str]:
          UpperCamelCase       :List[Any]								=   TFEsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D''' )

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






"""simple docstring"""






from __future__ import annotations

from collections.abc import Callable

__lowerCAmelCase    :       str					     =list[list[float | int]]


def 			UpperCAmelCase__       (				lowerCAmelCase__     :Matrix					,		lowerCAmelCase__     :Matrix		) ->    Matrix:
      '''simple docstring'''


      lowercase														=	len(lowerCAmelCase__		)
      lowercase														=	[[0 for _ in range(size + 1		)] for _ in range(lowerCAmelCase__		)]
      lowercase														=	42
      lowercase														=	42
      lowercase														=	42
      lowercase														=	42
      lowercase														=	42
      lowercase														=	42

      for row in range(lowerCAmelCase__		):
            for col in range(lowerCAmelCase__		):
                  lowercase														=	matrix[row][col]

            lowercase														=	vector[row][0]

      lowercase														=	0
      lowercase														=	0
      while row < size and col < size:
            # pivoting
            lowercase														=	max((abs(augmented[rowa][col]		), rowa) for rowa in range(lowerCAmelCase__					,		lowerCAmelCase__		)		)[
                1
            ]
            if augmented[pivot_row][col] == 0:
                  col += 1
                  continue
            else:
                  lowercase							,							lowercase														=	augmented[pivot_row], augmented[row]

            for rowa in range(row + 1					,		lowerCAmelCase__		):
                  lowercase														=	augmented[rowa][col] / augmented[row][col]
                  lowercase														=	0
                  for cola in range(col + 1					,		size + 1		):
                        augmented[rowa][cola] -= augmented[row][cola] * ratio

            row += 1
            col += 1

      # back substitution
      for col in range(1					,		lowerCAmelCase__		):
            for row in range(lowerCAmelCase__		):
                  lowercase														=	augmented[row][col] / augmented[col][col]
                  for cola in range(lowerCAmelCase__					,		size + 1		):
                        augmented[row][cola] -= augmented[col][cola] * ratio

    # round to get rid of numbers like 2.000000000000004
      return [
          [round(augmented[row][size] / augmented[row][row]					,		1_0		)] for row in range(lowerCAmelCase__		)
      ]


def 			UpperCAmelCase__       (				lowerCAmelCase__     :list[int]		) ->    Callable[[int], int]:
      '''simple docstring'''


      lowercase														=	len(lowerCAmelCase__		)
      lowercase														=	[[0 for _ in range(lowerCAmelCase__		)] for _ in range(lowerCAmelCase__		)]
      lowercase														=	[[0] for _ in range(lowerCAmelCase__		)]
      lowercase														=	42
      lowercase														=	42
      lowercase														=	42
      lowercase														=	42

      for x_val, y_val in enumerate(lowerCAmelCase__		):
            for col in range(lowerCAmelCase__		):
                  lowercase														=	(x_val + 1) ** (size - col - 1)
            lowercase														=	y_val

      lowercase														=	solve(lowerCAmelCase__					,		lowerCAmelCase__		)

      def interpolated_func(lowerCAmelCase__     :int		) -> int:
            return sum(
                round(coeffs[x_val][0]		) * (var ** (size - x_val - 1))
                for x_val in range(lowerCAmelCase__		)		)

      return interpolated_func


def 			UpperCAmelCase__       (				lowerCAmelCase__     :int		) ->    int:
      '''simple docstring'''


      return (
          1
          - variable
          + variable**2
          - variable**3
          + variable**4
          - variable**5
          + variable**6
          - variable**7
          + variable**8
          - variable**9
          + variable**1_0
      )


def 			UpperCAmelCase__       (				lowerCAmelCase__     :Callable[[int], int] = question_function					,		lowerCAmelCase__     :int = 1_0		) ->    int:
      '''simple docstring'''


      lowercase														=	[func(lowerCAmelCase__		) for x_val in range(1					,		order + 1		)]

      lowercase														=	[
          interpolate(data_points[:max_coeff]		) for max_coeff in range(1					,		order + 1		)
      ]

      lowercase														=	0
      lowercase														=	42
      lowercase														=	42

      for poly in polynomials:
            lowercase														=	1
            while func(lowerCAmelCase__		) == poly(lowerCAmelCase__		):
                  x_val += 1

            ret += poly(lowerCAmelCase__		)

      return ret


if __name__ == "__main__":
       print(F"""{solution() = }""")



