





'''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  lowerCAmelCase_		:




 def __init__(						self      ,  _lowerCAmelCase      ,  )     ->				Optional[Any]:
     _lowerCAmelCase						      =   parent
     _lowerCAmelCase						      =   13
     _lowerCAmelCase						      =   7
     _lowerCAmelCase						      =   True
     _lowerCAmelCase						      =   True
     _lowerCAmelCase						      =   True
     _lowerCAmelCase						      =   99
     _lowerCAmelCase						      =   32
     _lowerCAmelCase						      =   2
     _lowerCAmelCase						      =   4
     _lowerCAmelCase						      =   37
     _lowerCAmelCase						      =   "gelu"
     _lowerCAmelCase						      =   0.1
     _lowerCAmelCase						      =   0.1
     _lowerCAmelCase						      =   512
     _lowerCAmelCase						      =   16
     _lowerCAmelCase						      =   2
     _lowerCAmelCase						      =   0.02
     _lowerCAmelCase						      =   3
     _lowerCAmelCase						      =   4
     _lowerCAmelCase						      =   None




 def 				_snake_case (						self		)     ->				Dict:
     _lowerCAmelCase						      =   ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size		)

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

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

     _lowerCAmelCase						      =   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		)     ->				str:
     (
         (
         _lowerCAmelCase
     )      ,      (
         _lowerCAmelCase
     )      ,      (
         _lowerCAmelCase
     )      ,      (
         _lowerCAmelCase
     )      ,      (
         _lowerCAmelCase
     )      ,      (
         _lowerCAmelCase
     )      ,      
     )						      =   self.prepare_config_and_inputs()

     _lowerCAmelCase						      =   True
     _lowerCAmelCase						      =   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
     _lowerCAmelCase						      =   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      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase		)     ->				Any:
     _lowerCAmelCase						      =   TFEsmModel(config=_lowerCAmelCase		)
     _lowerCAmelCase						      =   {"input_ids": input_ids, "attention_mask": input_mask}
     _lowerCAmelCase						      =   model(_lowerCAmelCase		)

     _lowerCAmelCase						      =   [input_ids, input_mask]
     _lowerCAmelCase						      =   model(_lowerCAmelCase		)

     _lowerCAmelCase						      =   model(_lowerCAmelCase		)

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




 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  )     ->				Union[str, Any]:
     _lowerCAmelCase						      =   True

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

     _lowerCAmelCase						      =   [input_ids, input_mask]
     _lowerCAmelCase						      =   model(_lowerCAmelCase      ,  encoder_hidden_states=_lowerCAmelCase		)

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

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




 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase		)     ->				List[str]:
     _lowerCAmelCase						      =   TFEsmForMaskedLM(config=_lowerCAmelCase		)
     _lowerCAmelCase						      =   model([input_ids, input_mask]		)
     self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length, self.vocab_size)		)




 def 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase		)     ->				str:
     _lowerCAmelCase						      =   self.num_labels
     _lowerCAmelCase						      =   TFEsmForTokenClassification(config=_lowerCAmelCase		)
     _lowerCAmelCase						      =   {"input_ids": input_ids, "attention_mask": input_mask}
     _lowerCAmelCase						      =   model(_lowerCAmelCase		)
     self.parent.assertEqual(result.logits.shape      ,  (self.batch_size, self.seq_length, self.num_labels)		)





 def 				_snake_case (						self		)     ->				List[str]:
     _lowerCAmelCase						      =   self.prepare_config_and_inputs()
     (
         (
         _lowerCAmelCase
     )      ,      (
         _lowerCAmelCase
     )      ,      (
         _lowerCAmelCase
     )      ,      (
         _lowerCAmelCase
     )      ,      (
         _lowerCAmelCase
     )      ,      (
         _lowerCAmelCase
     )      ,      
     )						      =   config_and_inputs
     _lowerCAmelCase						      =   {"input_ids": input_ids, "attention_mask": input_mask}
     return config, inputs_dict







@require_tf
class  lowerCAmelCase_		(		__magic_name__    ,__magic_name__    ,unittest.TestCase		):
 __lowerCamelCase    :				Tuple								=       (
     (
         TFEsmModel,
         TFEsmForMaskedLM,
         TFEsmForSequenceClassification,
         TFEsmForTokenClassification,
     )
     if is_tf_available()
     else ()
 )
 __lowerCamelCase    :				Any								=       (
     {
         "feature-extraction": TFEsmModel,
         "fill-mask": TFEsmForMaskedLM,
         "text-classification": TFEsmForSequenceClassification,
         "token-classification": TFEsmForTokenClassification,
         "zero-shot": TFEsmForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 __lowerCamelCase    :				List[Any]								=       False
 __lowerCamelCase    :				int								=       False




 def 				_snake_case (						self		)     ->				List[Any]:
     _lowerCAmelCase						      =   TFEsmModelTester(self		)
     _lowerCAmelCase						      =   ConfigTester(self      ,  config_class=_lowerCAmelCase      ,  hidden_size=37		)




 def 				_snake_case (						self		)     ->				List[str]:
     self.config_tester.run_common_tests()




 def 				_snake_case (						self		)     ->				int:
     _lowerCAmelCase						      =   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowerCAmelCase		)




 def 				_snake_case (						self		)     ->				int:
     _lowerCAmelCase						      =   self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(*_lowerCAmelCase		)




 def 				_snake_case (						self		)     ->				int:
     _lowerCAmelCase						      =   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase		)




 def 				_snake_case (						self		)     ->				Union[str, Any]:
     _lowerCAmelCase						      =   self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase		)




 @slow
 def 				_snake_case (						self		)     ->				Optional[int]:
     for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _lowerCAmelCase						      =   TFEsmModel.from_pretrained(_lowerCAmelCase		)
         self.assertIsNotNone(_lowerCAmelCase		)




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




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





 def 				_snake_case (						self		)     ->				Tuple:
     _lowerCAmelCase      ,      _lowerCAmelCase						      =   self.model_tester.prepare_config_and_inputs_for_common()

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







@require_tf
class  lowerCAmelCase_		(		unittest.TestCase		):




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

     _lowerCAmelCase						      =   tf.constant([[0, 1, 2, 3, 4, 5]]		)
     _lowerCAmelCase						      =   model(_lowerCAmelCase		)[0]
     _lowerCAmelCase						      =   [1, 6, 33]
     self.assertEqual(list(output.numpy().shape		)      ,  _lowerCAmelCase		)
     # compare the actual values for a slice.
     _lowerCAmelCase						      =   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 				_snake_case (						self		)     ->				List[Any]:
     _lowerCAmelCase						      =   TFEsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"		)

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


import math

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE									=				logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE									=				{
    "facebook/data2vec-base-960h": "https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json",
    # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio
}

class  lowerCAmelCase_		(		__magic_name__		):
 __lowerCamelCase    :				List[Any]								=       "data2vec-audio"




 def __init__(						self      ,  _lowerCAmelCase=32      ,  _lowerCAmelCase=768      ,  _lowerCAmelCase=12      ,  _lowerCAmelCase=12      ,  _lowerCAmelCase=3072      ,  _lowerCAmelCase="gelu"      ,  _lowerCAmelCase=0.1      ,  _lowerCAmelCase=0.1      ,  _lowerCAmelCase=0.1      ,  _lowerCAmelCase=0.0      ,  _lowerCAmelCase=0.1      ,  _lowerCAmelCase=0.1      ,  _lowerCAmelCase=0.02      ,  _lowerCAmelCase=1E-5      ,  _lowerCAmelCase="gelu"      ,  _lowerCAmelCase=(512, 512, 512, 512, 512, 512, 512)      ,  _lowerCAmelCase=(5, 2, 2, 2, 2, 2, 2)      ,  _lowerCAmelCase=(10, 3, 3, 3, 3, 2, 2)      ,  _lowerCAmelCase=False      ,  _lowerCAmelCase=16      ,  _lowerCAmelCase=19      ,  _lowerCAmelCase=5      ,  _lowerCAmelCase=0.05      ,  _lowerCAmelCase=10      ,  _lowerCAmelCase=2      ,  _lowerCAmelCase=0.0      ,  _lowerCAmelCase=10      ,  _lowerCAmelCase=0      ,  _lowerCAmelCase="sum"      ,  _lowerCAmelCase=False      ,  _lowerCAmelCase=False      ,  _lowerCAmelCase=256      ,  _lowerCAmelCase=(512, 512, 512, 512, 1500)      ,  _lowerCAmelCase=(5, 3, 3, 1, 1)      ,  _lowerCAmelCase=(1, 2, 3, 1, 1)      ,  _lowerCAmelCase=512      ,  _lowerCAmelCase=0      ,  _lowerCAmelCase=1      ,  _lowerCAmelCase=2      ,  _lowerCAmelCase=False      ,  _lowerCAmelCase=3      ,  _lowerCAmelCase=2      ,  _lowerCAmelCase=3      ,  _lowerCAmelCase=None      ,  **_lowerCAmelCase      ,  )     ->				Dict:
     super().__init__(**_lowerCAmelCase      ,  pad_token_id=_lowerCAmelCase      ,  bos_token_id=_lowerCAmelCase      ,  eos_token_id=_lowerCAmelCase		)
     _lowerCAmelCase						      =   hidden_size
     _lowerCAmelCase						      =   feat_extract_activation
     _lowerCAmelCase						      =   list(_lowerCAmelCase		)
     _lowerCAmelCase						      =   list(_lowerCAmelCase		)
     _lowerCAmelCase						      =   list(_lowerCAmelCase		)
     _lowerCAmelCase						      =   conv_bias
     _lowerCAmelCase						      =   num_conv_pos_embeddings
     _lowerCAmelCase						      =   num_conv_pos_embedding_groups
     _lowerCAmelCase						      =   conv_pos_kernel_size
     _lowerCAmelCase						      =   len(self.conv_dim		)
     _lowerCAmelCase						      =   num_hidden_layers
     _lowerCAmelCase						      =   intermediate_size
     _lowerCAmelCase						      =   hidden_act
     _lowerCAmelCase						      =   num_attention_heads
     _lowerCAmelCase						      =   hidden_dropout
     _lowerCAmelCase						      =   attention_dropout
     _lowerCAmelCase						      =   activation_dropout
     _lowerCAmelCase						      =   feat_proj_dropout
     _lowerCAmelCase						      =   final_dropout
     _lowerCAmelCase						      =   layerdrop
     _lowerCAmelCase						      =   layer_norm_eps
     _lowerCAmelCase						      =   initializer_range
     _lowerCAmelCase						      =   vocab_size
     _lowerCAmelCase						      =   use_weighted_layer_sum

     if (
         (len(self.conv_stride		) != self.num_feat_extract_layers)
         or (len(self.conv_kernel		) != self.num_feat_extract_layers)
         or (len(self.conv_dim		) != self.num_feat_extract_layers)
     ):
         raise ValueError(
             "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
             " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
             f''' {len(self.conv_dim		)}`, `len(config.conv_stride) = {len(self.conv_stride		)}`,'''
             f''' `len(config.conv_kernel) = {len(self.conv_kernel		)}`.'''		)

     # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
     _lowerCAmelCase						      =   mask_time_prob
     _lowerCAmelCase						      =   mask_time_length
     _lowerCAmelCase						      =   mask_time_min_masks
     _lowerCAmelCase						      =   mask_feature_prob
     _lowerCAmelCase						      =   mask_feature_length
     _lowerCAmelCase						      =   mask_feature_min_masks

     # ctc loss
     _lowerCAmelCase						      =   ctc_loss_reduction
     _lowerCAmelCase						      =   ctc_zero_infinity

     # adapter
     _lowerCAmelCase						      =   add_adapter
     _lowerCAmelCase						      =   adapter_kernel_size
     _lowerCAmelCase						      =   adapter_stride
     _lowerCAmelCase						      =   num_adapter_layers
     _lowerCAmelCase						      =   output_hidden_size or hidden_size

     # SequenceClassification-specific parameter. Feel free to ignore for other classes.
     _lowerCAmelCase						      =   classifier_proj_size

     # XVector-specific parameters. Feel free to ignore for other classes.
     _lowerCAmelCase						      =   list(_lowerCAmelCase		)
     _lowerCAmelCase						      =   list(_lowerCAmelCase		)
     _lowerCAmelCase						      =   list(_lowerCAmelCase		)
     _lowerCAmelCase						      =   xvector_output_dim





 @property
 def 				_snake_case (						self		)     ->				str:
     return math.prod(self.conv_stride		)


