





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					,			)				->  str:
      """simple docstring"""





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





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





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

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

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

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





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

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





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

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

      a__	:						Tuple      		=  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					,			)				->  List[Any]:
      """simple docstring"""





      a__	:						Optional[int]      		=  True

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

      a__	:						Union[str, Any]      		=  [input_ids, input_mask]
      a__	:						Dict      		=  model(snake_case					,			encoder_hidden_states=snake_case      )

      # Also check the case where encoder outputs are not passed
      a__	:						Union[str, Any]      		=  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      )				->  str:
      """simple docstring"""





      a__	:						Tuple      		=  TFEsmForMaskedLM(config=snake_case      )
      a__	:						int      		=  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      )				->  str:
      """simple docstring"""





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





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





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







@require_tf
class   __lowerCAmelCase			(					_UpperCamelCase		,_UpperCamelCase		,unittest.TestCase  ):
   _UpperCamelCase :						Any     		=				(
       (
           TFEsmModel,
           TFEsmForMaskedLM,
           TFEsmForSequenceClassification,
           TFEsmForTokenClassification,
       )
       if is_tf_available()
       else ()
   )
   _UpperCamelCase :						Tuple     		=				(
       {
           """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 :						Optional[int]     		=				False





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





      a__	:						Optional[int]      		=  TFEsmModelTester(self      )
      a__	:						Dict      		=  ConfigTester(self					,			config_class=snake_case					,			hidden_size=37      )





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





      self.config_tester.run_common_tests()





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





      a__	:						Any      		=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*snake_case      )





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





      a__	:						Optional[Any]      		=  self.model_tester.prepare_config_and_inputs_for_decoder()
      self.model_tester.create_and_check_model_as_decoder(*snake_case      )





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





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





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





      a__	:						List[Any]      		=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*snake_case      )





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





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





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





      pass





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





      pass





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





      a__		,     a__	:						Union[str, Any]      		=  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
         a__	:						Optional[int]      		=  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__	:						Any      		=  model.get_bias()
            assert isinstance(snake_case					,			snake_case      )
            for k, v in name.items():
               assert isinstance(snake_case					,			tf.Variable      )
         else:
            a__	:						Optional[int]      		=  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      )				->  int:
      """simple docstring"""





      a__	:						Dict      		=  TFEsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"      )

      a__	:						Optional[int]      		=  tf.constant([[0, 1, 2, 3, 4, 5]]      )
      a__	:						List[str]      		=  model(snake_case      )[0]
      a__	:						Dict      		=  [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      )				->  str:
      """simple docstring"""





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

      a__	:						List[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      )      )








SCREAMING_SNAKE_CASE__       :			Union[str, Any]			      =						9.80665






def       _A  (      lowerCamelCase							, lowerCamelCase							, lowerCamelCase = g   ):

   if fluid_density <= 0:
      raise ValueError("Impossible fluid density"   )
   if volume < 0:
      raise ValueError("Impossible Object volume"   )
   if gravity <= 0:
      raise ValueError("Impossible Gravity"   )

   return fluid_density * gravity * volume


if __name__ == "__main__":
  import doctest

  # run doctest
  doctest.testmod()

