





'''simple docstring'''






import unittest

from transformers import DebertaVaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          DebertaVaForMaskedLM,
          DebertaVaForMultipleChoice,
          DebertaVaForQuestionAnswering,
          DebertaVaForSequenceClassification,
          DebertaVaForTokenClassification,
          DebertaVaModel,
      )
      from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST







class 		UpperCAmelCase (					lowercase_):


  """simple docstring"""







  def __init__(					self     :       Optional[Any]				,						UpperCamelCase__     :       Union[str, Any]				,						UpperCamelCase__     :       Union[str, Any]=13				,						UpperCamelCase__     :       Tuple=7				,						UpperCamelCase__     :       Any=True				,						UpperCamelCase__     :       Tuple=True				,						UpperCamelCase__     :       Any=True				,						UpperCamelCase__     :       Dict=True				,						UpperCamelCase__     :       int=99				,						UpperCamelCase__     :       Union[str, Any]=32				,						UpperCamelCase__     :       Dict=5				,						UpperCamelCase__     :       Optional[int]=4				,						UpperCamelCase__     :       Any=37				,						UpperCamelCase__     :       List[str]="gelu"				,						UpperCamelCase__     :       Dict=0.1				,						UpperCamelCase__     :       int=0.1				,						UpperCamelCase__     :       List[Any]=512				,						UpperCamelCase__     :       Union[str, Any]=16				,						UpperCamelCase__     :       Any=2				,						UpperCamelCase__     :       Tuple=0.02				,						UpperCamelCase__     :       Optional[Any]=False				,						UpperCamelCase__     :       List[Any]=True				,						UpperCamelCase__     :       int="None"				,						UpperCamelCase__     :       Tuple=3				,						UpperCamelCase__     :       List[str]=4				,						UpperCamelCase__     :       Optional[Any]=None				,						)  ->    Any:
    _UpperCamelCase							      =parent
    _UpperCamelCase							      =batch_size
    _UpperCamelCase							      =seq_length
    _UpperCamelCase							      =is_training
    _UpperCamelCase							      =use_input_mask
    _UpperCamelCase							      =use_token_type_ids
    _UpperCamelCase							      =use_labels
    _UpperCamelCase							      =vocab_size
    _UpperCamelCase							      =hidden_size
    _UpperCamelCase							      =num_hidden_layers
    _UpperCamelCase							      =num_attention_heads
    _UpperCamelCase							      =intermediate_size
    _UpperCamelCase							      =hidden_act
    _UpperCamelCase							      =hidden_dropout_prob
    _UpperCamelCase							      =attention_probs_dropout_prob
    _UpperCamelCase							      =max_position_embeddings
    _UpperCamelCase							      =type_vocab_size
    _UpperCamelCase							      =type_sequence_label_size
    _UpperCamelCase							      =initializer_range
    _UpperCamelCase							      =num_labels
    _UpperCamelCase							      =num_choices
    _UpperCamelCase							      =relative_attention
    _UpperCamelCase							      =position_biased_input
    _UpperCamelCase							      =pos_att_type
    _UpperCamelCase							      =scope


  def 	UpperCamelCase__   (					self     :       Optional[Any]	)  ->    int:
    _UpperCamelCase							      =ids_tensor([self.batch_size, self.seq_length]				,						self.vocab_size	)

    _UpperCamelCase							      =None
    if self.use_input_mask:
      _UpperCamelCase							      =ids_tensor([self.batch_size, self.seq_length]				,						vocab_size=2	)

    _UpperCamelCase							      =None
    if self.use_token_type_ids:
      _UpperCamelCase							      =ids_tensor([self.batch_size, self.seq_length]				,						self.type_vocab_size	)

    _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							      =self.get_config()

    return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels


  def 	UpperCamelCase__   (					self     :       Dict	)  ->    int:
    return DebertaVaConfig(
        vocab_size=self.vocab_size				,						hidden_size=self.hidden_size				,						num_hidden_layers=self.num_hidden_layers				,						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				,						relative_attention=self.relative_attention				,						position_biased_input=self.position_biased_input				,						pos_att_type=self.pos_att_type				,						)


  def 	UpperCamelCase__   (					self     :       Optional[Any]				,						UpperCamelCase__     :       List[str]	)  ->    Union[str, Any]:
    self.parent.assertListEqual(list(result.loss.size()	)				,						[]	)


  def 	UpperCamelCase__   (					self     :       List[Any]				,						UpperCamelCase__     :       Optional[int]				,						UpperCamelCase__     :       List[str]				,						UpperCamelCase__     :       int				,						UpperCamelCase__     :       Optional[Any]				,						UpperCamelCase__     :       List[Any]				,						UpperCamelCase__     :       Optional[int]				,						UpperCamelCase__     :       List[str]	)  ->    List[str]:
    _UpperCamelCase							      =DebertaVaModel(config=A_	)
    model.to(A_	)
    model.eval()
    _UpperCamelCase							      =model(A_				,						attention_mask=A_				,						token_type_ids=A_	)[0]
    _UpperCamelCase							      =model(A_				,						token_type_ids=A_	)[0]
    _UpperCamelCase							      =model(A_	)[0]

    self.parent.assertListEqual(list(sequence_output.size()	)				,						[self.batch_size, self.seq_length, self.hidden_size]	)


  def 	UpperCamelCase__   (					self     :       Union[str, Any]				,						UpperCamelCase__     :       List[Any]				,						UpperCamelCase__     :       str				,						UpperCamelCase__     :       Union[str, Any]				,						UpperCamelCase__     :       Optional[Any]				,						UpperCamelCase__     :       str				,						UpperCamelCase__     :       Tuple				,						UpperCamelCase__     :       List[str]	)  ->    Optional[int]:
    _UpperCamelCase							      =DebertaVaForMaskedLM(config=A_	)
    model.to(A_	)
    model.eval()
    _UpperCamelCase							      =model(A_				,						attention_mask=A_				,						token_type_ids=A_				,						labels=A_	)

    self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.vocab_size)	)


  def 	UpperCamelCase__   (					self     :       Union[str, Any]				,						UpperCamelCase__     :       List[str]				,						UpperCamelCase__     :       List[str]				,						UpperCamelCase__     :       Optional[Any]				,						UpperCamelCase__     :       Dict				,						UpperCamelCase__     :       Dict				,						UpperCamelCase__     :       int				,						UpperCamelCase__     :       Optional[int]	)  ->    Union[str, Any]:
    _UpperCamelCase							      =self.num_labels
    _UpperCamelCase							      =DebertaVaForSequenceClassification(A_	)
    model.to(A_	)
    model.eval()
    _UpperCamelCase							      =model(A_				,						attention_mask=A_				,						token_type_ids=A_				,						labels=A_	)
    self.parent.assertListEqual(list(result.logits.size()	)				,						[self.batch_size, self.num_labels]	)
    self.check_loss_output(A_	)


  def 	UpperCamelCase__   (					self     :       List[str]				,						UpperCamelCase__     :       Tuple				,						UpperCamelCase__     :       str				,						UpperCamelCase__     :       Optional[Any]				,						UpperCamelCase__     :       Tuple				,						UpperCamelCase__     :       Union[str, Any]				,						UpperCamelCase__     :       Dict				,						UpperCamelCase__     :       Optional[int]	)  ->    Tuple:
    _UpperCamelCase							      =self.num_labels
    _UpperCamelCase							      =DebertaVaForTokenClassification(config=A_	)
    model.to(A_	)
    model.eval()
    _UpperCamelCase							      =model(A_				,						attention_mask=A_				,						token_type_ids=A_				,						labels=A_	)
    self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.num_labels)	)


  def 	UpperCamelCase__   (					self     :       int				,						UpperCamelCase__     :       Optional[int]				,						UpperCamelCase__     :       Union[str, Any]				,						UpperCamelCase__     :       Tuple				,						UpperCamelCase__     :       Any				,						UpperCamelCase__     :       Union[str, Any]				,						UpperCamelCase__     :       Optional[int]				,						UpperCamelCase__     :       Optional[Any]	)  ->    Union[str, Any]:
    _UpperCamelCase							      =DebertaVaForQuestionAnswering(config=A_	)
    model.to(A_	)
    model.eval()
    _UpperCamelCase							      =model(
        A_				,						attention_mask=A_				,						token_type_ids=A_				,						start_positions=A_				,						end_positions=A_				,						)
    self.parent.assertEqual(result.start_logits.shape				,						(self.batch_size, self.seq_length)	)
    self.parent.assertEqual(result.end_logits.shape				,						(self.batch_size, self.seq_length)	)


  def 	UpperCamelCase__   (					self     :       Dict				,						UpperCamelCase__     :       Tuple				,						UpperCamelCase__     :       List[Any]				,						UpperCamelCase__     :       List[Any]				,						UpperCamelCase__     :       Optional[int]				,						UpperCamelCase__     :       Dict				,						UpperCamelCase__     :       Any				,						UpperCamelCase__     :       Any	)  ->    Optional[Any]:
    _UpperCamelCase							      =DebertaVaForMultipleChoice(config=A_	)
    model.to(A_	)
    model.eval()
    _UpperCamelCase							      =input_ids.unsqueeze(1	).expand(-1				,						self.num_choices				,						-1	).contiguous()
    _UpperCamelCase							      =token_type_ids.unsqueeze(1	).expand(-1				,						self.num_choices				,						-1	).contiguous()
    _UpperCamelCase							      =input_mask.unsqueeze(1	).expand(-1				,						self.num_choices				,						-1	).contiguous()
    _UpperCamelCase							      =model(
        A_				,						attention_mask=A_				,						token_type_ids=A_				,						labels=A_				,						)
    self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_choices)	)




  def 	UpperCamelCase__   (					self     :       List[str]	)  ->    Tuple:
    _UpperCamelCase							      =self.prepare_config_and_inputs()
    (
        (
        _UpperCamelCase							
    )      ,  (
        _UpperCamelCase							
    )      ,  (
        _UpperCamelCase							
    )      ,  (
        _UpperCamelCase							
    )      ,  (
        _UpperCamelCase							
    )      ,  (
        _UpperCamelCase							
    )      ,  (
        _UpperCamelCase							
    )      ,  							
    )							      =config_and_inputs
    _UpperCamelCase							      ={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
    return config, inputs_dict







@require_torch
class 		UpperCAmelCase (					lowercase_ ,  lowercase_ ,  unittest.TestCase):


  """simple docstring"""







  lowerCAmelCase_											=					(
      (
          DebertaVaModel,
          DebertaVaForMaskedLM,
          DebertaVaForSequenceClassification,
          DebertaVaForTokenClassification,
          DebertaVaForQuestionAnswering,
          DebertaVaForMultipleChoice,
      )
      if is_torch_available()
      else ()
  )
  lowerCAmelCase_											=					(
      {
          """feature-extraction""": DebertaVaModel,
          """fill-mask""": DebertaVaForMaskedLM,
          """question-answering""": DebertaVaForQuestionAnswering,
          """text-classification""": DebertaVaForSequenceClassification,
          """token-classification""": DebertaVaForTokenClassification,
          """zero-shot""": DebertaVaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )

  lowerCAmelCase_											=					True
  lowerCAmelCase_											=					False
  lowerCAmelCase_											=					False
  lowerCAmelCase_											=					False
  lowerCAmelCase_											=					False


  def 	UpperCamelCase__   (					self     :       Optional[int]	)  ->    Any:
    _UpperCamelCase							      =DebertaVaModelTester(self	)
    _UpperCamelCase							      =ConfigTester(self				,						config_class=A_				,						hidden_size=37	)


  def 	UpperCamelCase__   (					self     :       Any	)  ->    int:
    self.config_tester.run_common_tests()


  def 	UpperCamelCase__   (					self     :       List[Any]	)  ->    Tuple:
    _UpperCamelCase							      =self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_model(*A_	)


  def 	UpperCamelCase__   (					self     :       Optional[int]	)  ->    int:
    _UpperCamelCase							      =self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_sequence_classification(*A_	)


  def 	UpperCamelCase__   (					self     :       List[str]	)  ->    int:
    _UpperCamelCase							      =self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_masked_lm(*A_	)


  def 	UpperCamelCase__   (					self     :       Union[str, Any]	)  ->    Dict:
    _UpperCamelCase							      =self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_question_answering(*A_	)


  def 	UpperCamelCase__   (					self     :       str	)  ->    List[str]:
    _UpperCamelCase							      =self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_token_classification(*A_	)


  def 	UpperCamelCase__   (					self     :       Dict	)  ->    Optional[int]:
    _UpperCamelCase							      =self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_deberta_for_multiple_choice(*A_	)




  @slow
  def 	UpperCamelCase__   (					self     :       Dict	)  ->    Union[str, Any]:
    for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      _UpperCamelCase							      =DebertaVaModel.from_pretrained(A_	)
      self.assertIsNotNone(A_	)







@require_torch
@require_sentencepiece
@require_tokenizers
class 		UpperCAmelCase (					unittest.TestCase):


  """simple docstring"""







  @unittest.skip(reason='''Model not available yet'''	)
  def 	UpperCamelCase__   (					self     :       Union[str, Any]	)  ->    Optional[int]:
    pass




  @slow
  def 	UpperCamelCase__   (					self     :       Optional[Any]	)  ->    Union[str, Any]:
    _UpperCamelCase							      =DebertaVaModel.from_pretrained('''microsoft/deberta-v2-xlarge'''	)

    _UpperCamelCase							      =torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]	)
    _UpperCamelCase							      =torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
    with torch.no_grad():
      _UpperCamelCase							      =model(A_				,						attention_mask=A_	)[0]
    # compare the actual values for a slice.
    _UpperCamelCase							      =torch.tensor(
        [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]]	)
    self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,						A_				,						atol=1E-4	)				,						F'''{output[:, 1:4, 1:4]}'''	)













import functools
import operator

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


UpperCamelCase_												=	logging.get_logger(__name__)

UpperCamelCase_												=	{
    'microsoft/wavlm-base': 'https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json',
    # See all WavLM models at https://huggingface.co/models?filter=wavlm
}

class     snake_case_		(   a							):






 '''simple docstring'''





 __UpperCamelCase       =      'wavlm'

 def __init__(  self,		A_=32,		A_=768,		A_=12,		A_=12,		A_=3072,		A_="gelu",		A_=0.1,		A_=0.1,		A_=0.1,		A_=0.0,		A_=0.1,		A_=0.1,		A_=0.02,		A_=1E-5,		A_="group",		A_="gelu",		A_=(512, 512, 512, 512, 512, 512, 512),		A_=(5, 2, 2, 2, 2, 2, 2),		A_=(10, 3, 3, 3, 3, 2, 2),		A_=False,		A_=128,		A_=16,		A_=320,		A_=800,		A_=False,		A_=True,		A_=0.05,		A_=10,		A_=2,		A_=0.0,		A_=10,		A_=320,		A_=2,		A_=0.1,		A_=100,		A_=256,		A_=256,		A_=0.1,		A_="mean",		A_=False,		A_=False,		A_=256,		A_=(512, 512, 512, 512, 1500),		A_=(5, 3, 3, 1, 1),		A_=(1, 2, 3, 1, 1),		A_=512,		A_=80,		A_=0,		A_=1,		A_=2,		A_=False,		A_=3,		A_=2,		A_=3,		A_=None,		**A_,		)	->       Union[str, Any]:
    super().__init__(**A_,		pad_token_id=A_,		bos_token_id=A_,		eos_token_id=A_		)
    UpperCAmelCase__						=hidden_size
    UpperCAmelCase__						=feat_extract_norm
    UpperCAmelCase__						=feat_extract_activation
    UpperCAmelCase__						=list(A_		)
    UpperCAmelCase__						=list(A_		)
    UpperCAmelCase__						=list(A_		)
    UpperCAmelCase__						=conv_bias
    UpperCAmelCase__						=num_buckets
    UpperCAmelCase__						=max_bucket_distance
    UpperCAmelCase__						=num_conv_pos_embeddings
    UpperCAmelCase__						=num_conv_pos_embedding_groups
    UpperCAmelCase__						=len(self.conv_dim		)
    UpperCAmelCase__						=num_hidden_layers
    UpperCAmelCase__						=intermediate_size
    UpperCAmelCase__						=hidden_act
    UpperCAmelCase__						=num_attention_heads
    UpperCAmelCase__						=hidden_dropout
    UpperCAmelCase__						=attention_dropout
    UpperCAmelCase__						=activation_dropout
    UpperCAmelCase__						=feat_proj_dropout
    UpperCAmelCase__						=final_dropout
    UpperCAmelCase__						=layerdrop
    UpperCAmelCase__						=layer_norm_eps
    UpperCAmelCase__						=initializer_range
    UpperCAmelCase__						=num_ctc_classes
    UpperCAmelCase__						=vocab_size
    UpperCAmelCase__						=do_stable_layer_norm
    UpperCAmelCase__						=use_weighted_layer_sum
    UpperCAmelCase__						=classifier_proj_size

    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
    UpperCAmelCase__						=apply_spec_augment
    UpperCAmelCase__						=mask_time_prob
    UpperCAmelCase__						=mask_time_length
    UpperCAmelCase__						=mask_time_min_masks
    UpperCAmelCase__						=mask_feature_prob
    UpperCAmelCase__						=mask_feature_length

    # parameters for pretraining with codevector quantized representations
    UpperCAmelCase__						=num_codevectors_per_group
    UpperCAmelCase__						=num_codevector_groups
    UpperCAmelCase__						=contrastive_logits_temperature
    UpperCAmelCase__						=num_negatives
    UpperCAmelCase__						=codevector_dim
    UpperCAmelCase__						=proj_codevector_dim
    UpperCAmelCase__						=diversity_loss_weight

    # ctc loss
    UpperCAmelCase__						=ctc_loss_reduction
    UpperCAmelCase__						=ctc_zero_infinity

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

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

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






 @property
 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    return functools.reduce(operator.mul,		self.conv_stride,		1		)



