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


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



         UpperCAmelCase__								=  parent
         UpperCAmelCase__								=  1_3
         UpperCAmelCase__								=  7
         UpperCAmelCase__								=  True
         UpperCAmelCase__								=  True
         UpperCAmelCase__								=  True
         UpperCAmelCase__								=  9_9
         UpperCAmelCase__								=  3_2
         UpperCAmelCase__								=  2
         UpperCAmelCase__								=  4
         UpperCAmelCase__								=  3_7
         UpperCAmelCase__								=  "gelu"
         UpperCAmelCase__								=  0.1
         UpperCAmelCase__								=  0.1
         UpperCAmelCase__								=  5_1_2
         UpperCAmelCase__								=  1_6
         UpperCAmelCase__								=  2
         UpperCAmelCase__								=  0.02
         UpperCAmelCase__								=  3
         UpperCAmelCase__								=  4
         UpperCAmelCase__								=  None


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



         UpperCAmelCase__								=  ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size		)

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

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

         UpperCAmelCase__								=  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 			lowercase_      (self     :						Union[str, Any]		)						->				Tuple:
         """simple docstring"""



         (
             (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      
         )								=  self.prepare_config_and_inputs()

         UpperCAmelCase__								=  True
         UpperCAmelCase__								=  floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
         UpperCAmelCase__								=  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 			lowercase_      (self     :						Any       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						Dict       ,  __UpperCAmelCase     :						Union[str, Any]       ,  __UpperCAmelCase     :						int		)						->				str:
         """simple docstring"""



         UpperCAmelCase__								=  TFEsmModel(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  {"input_ids": input_ids, "attention_mask": input_mask}
         UpperCAmelCase__								=  model(__UpperCAmelCase		)

         UpperCAmelCase__								=  [input_ids, input_mask]
         UpperCAmelCase__								=  model(__UpperCAmelCase		)

         UpperCAmelCase__								=  model(__UpperCAmelCase		)

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


   def 			lowercase_      (self     :						Optional[Any]       ,  __UpperCAmelCase     :						Union[str, Any]       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						Dict       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						Any       ,  __UpperCAmelCase     :						Tuple       ,  )						->				List[str]:
         """simple docstring"""



         UpperCAmelCase__								=  True

         UpperCAmelCase__								=  TFEsmModel(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  {
             "input_ids": input_ids,
             "attention_mask": input_mask,
             "encoder_hidden_states": encoder_hidden_states,
             "encoder_attention_mask": encoder_attention_mask,
         }
         UpperCAmelCase__								=  model(__UpperCAmelCase		)

         UpperCAmelCase__								=  [input_ids, input_mask]
         UpperCAmelCase__								=  model(__UpperCAmelCase       ,  encoder_hidden_states=__UpperCAmelCase		)

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

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


   def 			lowercase_      (self     :						Optional[Any]       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						Tuple       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						Tuple       ,  __UpperCAmelCase     :						int		)						->				Optional[Any]:
         """simple docstring"""



         UpperCAmelCase__								=  TFEsmForMaskedLM(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  model([input_ids, input_mask]		)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)		)


   def 			lowercase_      (self     :						Any       ,  __UpperCAmelCase     :						Optional[int]       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						Dict       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						Optional[Any]       ,  __UpperCAmelCase     :						Optional[Any]		)						->				int:
         """simple docstring"""



         UpperCAmelCase__								=  self.num_labels
         UpperCAmelCase__								=  TFEsmForTokenClassification(config=__UpperCAmelCase		)
         UpperCAmelCase__								=  {"input_ids": input_ids, "attention_mask": input_mask}
         UpperCAmelCase__								=  model(__UpperCAmelCase		)
         self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.num_labels)		)





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



         UpperCAmelCase__								=  self.prepare_config_and_inputs()
         (
             (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      (
             UpperCAmelCase__
         )		,      
         )								=  config_and_inputs
         UpperCAmelCase__								=  {"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict





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


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



         UpperCAmelCase__								=  TFEsmModelTester(self		)
         UpperCAmelCase__								=  ConfigTester(self       ,  config_class=__UpperCAmelCase       ,  hidden_size=3_7		)


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



         self.config_tester.run_common_tests()


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



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__UpperCAmelCase		)


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



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_model_as_decoder(*__UpperCAmelCase		)


   def 			lowercase_      (self     :						Dict		)						->				Tuple:
         """simple docstring"""



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase		)


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



         UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase		)


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



         for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase__								=  TFEsmModel.from_pretrained(__UpperCAmelCase		)
               self.assertIsNotNone(__UpperCAmelCase		)


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



         pass


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



         pass





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



         UpperCAmelCase__		,      UpperCAmelCase__								=  self.model_tester.prepare_config_and_inputs_for_common()

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





@require_tf
class      A							(				unittest.TestCase     ):


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



         UpperCAmelCase__								=  TFEsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"		)

         UpperCAmelCase__								=  tf.constant([[0, 1, 2, 3, 4, 5]]		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)[0]
         UpperCAmelCase__								=  [1, 6, 3_3]
         self.assertEqual(list(output.numpy().shape		)       ,  __UpperCAmelCase		)
         # compare the actual values for a slice.
         UpperCAmelCase__								=  tf.constant(
             [
                 [
                     [8.921518, -10.589814, -6.4671307],
                     [-6.3967156, -13.911377, -1.1211915],
                     [-7.781247, -13.951557, -3.740592],
                 ]
             ]		)
         self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()       ,  expected_slice.numpy()       ,  atol=1E-2		)		)





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



         UpperCAmelCase__								=  TFEsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"		)

         UpperCAmelCase__								=  tf.constant([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]]		)
         UpperCAmelCase__								=  model(__UpperCAmelCase		)[0]
         # compare the actual values for a slice.
         UpperCAmelCase__								=  tf.constant(
             [
                 [
                     [0.14443092, 0.54125327, 0.3247739],
                     [0.30340484, 0.00526676, 0.31077722],
                     [0.32278043, -0.24987096, 0.3414628],
                 ]
             ]		)
         self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()       ,  expected_slice.numpy()       ,  atol=1E-4		)		)









'''simple docstring'''

# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import doctest
import sys
import warnings
from os.path import abspath, dirname, join

import _pytest

from transformers.testing_utils import HfDoctestModule, HfDocTestParser


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
a  :				int									=  abspath(join(dirname(__file__), 'src'))
sys.path.insert(1, git_repo_path)

# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action='ignore', category=FutureWarning)


def 		__magic_name__    (	__UpperCAmelCase	)		->					Optional[int]:

					'''simple docstring'''

					config.addinivalue_line(
					    '''markers''',      '''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested'''	)
					config.addinivalue_line(
					    '''markers''',      '''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested'''	)
					config.addinivalue_line('''markers''',      '''is_pipeline_test: mark test to run only when pipelines are tested'''	)
					config.addinivalue_line('''markers''',      '''is_staging_test: mark test to run only in the staging environment'''	)
					config.addinivalue_line('''markers''',      '''accelerate_tests: mark test that require accelerate'''	)
					config.addinivalue_line('''markers''',      '''tool_tests: mark the tool tests that are run on their specific schedule'''	)


def 		__magic_name__    (	__UpperCAmelCase	)		->					int:

					'''simple docstring'''

					from transformers.testing_utils import pytest_addoption_shared

					pytest_addoption_shared(__UpperCAmelCase	)


def 		__magic_name__    (	__UpperCAmelCase	)		->					Union[str, Any]:

					'''simple docstring'''

					from transformers.testing_utils import pytest_terminal_summary_main

					snake_case_									=				terminalreporter.config.getoption('''--make-reports'''	)
					if make_reports:
										pytest_terminal_summary_main(__UpperCAmelCase,      id=__UpperCAmelCase	)


def 		__magic_name__    (	__UpperCAmelCase,      __UpperCAmelCase	)		->					Optional[int]:

					'''simple docstring'''

					if exitstatus == 5:
										snake_case_									=				0


# Doctest custom flag to ignore output.
a  :				Union[str, Any]									=  doctest.register_optionflag('IGNORE_RESULT')

a  :				Optional[int]									=  doctest.OutputChecker






class 						a				(   _lowerCamelCase     ):


				def   A_ (		self    :					List[Any]       ,				lowercase_    :					int       ,				lowercase_    :					Tuple       ,				lowercase_    :					Optional[int]					):
									if IGNORE_RESULT & optionflags:
														return True
									return OutputChecker.check_output(self       ,				lowercase_       ,				lowercase_       ,				lowercase_					)







a  :				List[Any]									=  CustomOutputChecker
a  :				Optional[int]									=  HfDoctestModule

a  :				Tuple									=  HfDocTestParser

