


from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import numpy
     import tensorflow as tf

     from transformers import (
         TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
         TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
         TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
         BertConfig,
         DPRConfig,
         TFDPRContextEncoder,
         TFDPRQuestionEncoder,
         TFDPRReader,
     )







class 						_snake_case    :




 def __init__(						self   ,    _lowerCamelCase   ,    _lowerCamelCase=13   ,    _lowerCamelCase=7   ,    _lowerCamelCase=True   ,    _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   ,    _lowerCamelCase=0   ,    ):
       a		:List[Any]									=			parent
       a		:Optional[int]									=			batch_size
       a		:int									=			seq_length
       a		:List[Any]									=			is_training
       a		:str									=			use_input_mask
       a		:str									=			use_token_type_ids
       a		:Any									=			use_labels
       a		:int									=			vocab_size
       a		:int									=			hidden_size
       a		:List[str]									=			num_hidden_layers
       a		:Optional[Any]									=			num_attention_heads
       a		:int									=			intermediate_size
       a		:Any									=			hidden_act
       a		:Optional[int]									=			hidden_dropout_prob
       a		:List[str]									=			attention_probs_dropout_prob
       a		:Optional[int]									=			max_position_embeddings
       a		:Optional[int]									=			type_vocab_size
       a		:List[str]									=			type_sequence_label_size
       a		:int									=			initializer_range
       a		:Dict									=			num_labels
       a		:Optional[Any]									=			num_choices
       a		:Optional[Any]									=			scope
       a		:List[Any]									=			projection_dim




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[Any]									=			ids_tensor([self.batch_size, self.seq_length]   ,    self.vocab_size       )

       a		:int									=			None
       if self.use_input_mask:
             # follow test_modeling_tf_ctrl.py
             a		:List[str]									=			random_attention_mask([self.batch_size, self.seq_length]       )

       a		:Optional[Any]									=			None
       if self.use_token_type_ids:
             a		:Optional[int]									=			ids_tensor([self.batch_size, self.seq_length]   ,    self.type_vocab_size       )

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

       a		:Optional[int]									=			BertConfig(
           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   ,    is_decoder=_lowerCamelCase   ,    initializer_range=self.initializer_range   ,    )
       a		:str									=			DPRConfig(projection_dim=self.projection_dim   ,    **config.to_dict()       )

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




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       a		:Tuple									=			TFDPRContextEncoder(config=_lowerCamelCase       )
       a		:Dict									=			model(_lowerCamelCase   ,    attention_mask=_lowerCamelCase   ,    token_type_ids=_lowerCamelCase       )
       a		:Dict									=			model(_lowerCamelCase   ,    token_type_ids=_lowerCamelCase       )
       a		:int									=			model(_lowerCamelCase       )
       self.parent.assertEqual(result.pooler_output.shape   ,    (self.batch_size, self.projection_dim or self.hidden_size)       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       a		:List[str]									=			TFDPRQuestionEncoder(config=_lowerCamelCase       )
       a		:List[Any]									=			model(_lowerCamelCase   ,    attention_mask=_lowerCamelCase   ,    token_type_ids=_lowerCamelCase       )
       a		:Optional[Any]									=			model(_lowerCamelCase   ,    token_type_ids=_lowerCamelCase       )
       a		:Optional[Any]									=			model(_lowerCamelCase       )
       self.parent.assertEqual(result.pooler_output.shape   ,    (self.batch_size, self.projection_dim or self.hidden_size)       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       a		:List[Any]									=			TFDPRReader(config=_lowerCamelCase       )
       a		:Tuple									=			model(_lowerCamelCase   ,    attention_mask=_lowerCamelCase       )

       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)       )
       self.parent.assertEqual(result.relevance_logits.shape   ,    (self.batch_size,)       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Any									=			self.prepare_config_and_inputs()
       (
           (
           a
       )				,							(
           a
       )				,							(
           a
       )				,							(
           a
       )				,							(
           a
       )				,							(
           a
       )				,							(
           a
       )				,							
       )		:Dict									=			config_and_inputs
       a		:Optional[int]									=			{'''input_ids''': input_ids}
       return config, inputs_dict







@require_tf
class 						_snake_case    ( _snake_case		,       _snake_case		,       unittest.TestCase				):
 SCREAMING_SNAKE_CASE__ =			(
     (
         TFDPRContextEncoder,
         TFDPRQuestionEncoder,
         TFDPRReader,
     )
     if is_tf_available()
     else ()
 )
 SCREAMING_SNAKE_CASE__ =			{'feature-extraction': TFDPRQuestionEncoder} if is_tf_available() else {}

 SCREAMING_SNAKE_CASE__ =			False
 SCREAMING_SNAKE_CASE__ =			False
 SCREAMING_SNAKE_CASE__ =			False
 SCREAMING_SNAKE_CASE__ =			False
 SCREAMING_SNAKE_CASE__ =			False




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:int									=			TFDPRModelTester(self       )
       a		:Dict									=			ConfigTester(self   ,    config_class=_lowerCamelCase   ,    hidden_size=37       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       self.config_tester.run_common_tests()




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Any									=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_context_encoder(*_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:str									=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_question_encoder(*_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Optional[Any]									=			self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_dpr_reader(*_lowerCamelCase       )




 @slow
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             a		:Any									=			TFDPRContextEncoder.from_pretrained(_lowerCamelCase       )
             self.assertIsNotNone(_lowerCamelCase       )

       for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             a		:str									=			TFDPRContextEncoder.from_pretrained(_lowerCamelCase       )
             self.assertIsNotNone(_lowerCamelCase       )

       for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             a		:Optional[Any]									=			TFDPRQuestionEncoder.from_pretrained(_lowerCamelCase       )
             self.assertIsNotNone(_lowerCamelCase       )

       for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             a		:Tuple									=			TFDPRReader.from_pretrained(_lowerCamelCase       )
             self.assertIsNotNone(_lowerCamelCase       )







@require_tf
class 						_snake_case    ( unittest.TestCase				):




 @slow
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Optional[Any]									=			TFDPRQuestionEncoder.from_pretrained('''facebook/dpr-question_encoder-single-nq-base'''       )

       a		:Optional[Any]									=			tf.constant(
           [[101, 7592, 1010, 2003, 2026, 3899, 1_0140, 1029, 102]]       )  # [CLS] hello, is my dog cute? [SEP]
       a		:List[str]									=			model(_lowerCamelCase       )[0]  # embedding shape = (1, 768)
       # compare the actual values for a slice.
       a		:List[str]									=			tf.constant(
           [
               [
                   0.0323_6253,
                   0.1275_3335,
                   0.1681_8509,
                   0.0027_9786,
                   0.389_6933,
                   0.2426_4945,
                   0.217_8971,
                   -0.0233_5227,
                   -0.0848_1959,
                   -0.1432_4117,
               ]
           ]       )
       self.assertTrue(numpy.allclose(output[:, :10].numpy()   ,    expected_slice.numpy()   ,    atol=1e-4       )       )









import functools
import operator

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


snake_case							:       Optional[int]										=	logging.get_logger(__name__)

snake_case							:       Optional[Any]										=	{
    '''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    ( _snake_case				):
 SCREAMING_SNAKE_CASE__ =			'wavlm'




 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="group"   ,    _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=128   ,    _lowerCamelCase=16   ,    _lowerCamelCase=320   ,    _lowerCamelCase=800   ,    _lowerCamelCase=False   ,    _lowerCamelCase=True   ,    _lowerCamelCase=0.05   ,    _lowerCamelCase=10   ,    _lowerCamelCase=2   ,    _lowerCamelCase=0.0   ,    _lowerCamelCase=10   ,    _lowerCamelCase=320   ,    _lowerCamelCase=2   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=100   ,    _lowerCamelCase=256   ,    _lowerCamelCase=256   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase="mean"   ,    _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=80   ,    _lowerCamelCase=0   ,    _lowerCamelCase=1   ,    _lowerCamelCase=2   ,    _lowerCamelCase=False   ,    _lowerCamelCase=3   ,    _lowerCamelCase=2   ,    _lowerCamelCase=3   ,    _lowerCamelCase=None   ,    **_lowerCamelCase   ,    ):
       super().__init__(**_lowerCamelCase   ,    pad_token_id=_lowerCamelCase   ,    bos_token_id=_lowerCamelCase   ,    eos_token_id=_lowerCamelCase       )
       a		:Union[str, Any]									=			hidden_size
       a		:List[Any]									=			feat_extract_norm
       a		:Tuple									=			feat_extract_activation
       a		:Tuple									=			list(_lowerCamelCase       )
       a		:Optional[Any]									=			list(_lowerCamelCase       )
       a		:Optional[Any]									=			list(_lowerCamelCase       )
       a		:int									=			conv_bias
       a		:Tuple									=			num_buckets
       a		:Tuple									=			max_bucket_distance
       a		:int									=			num_conv_pos_embeddings
       a		:List[str]									=			num_conv_pos_embedding_groups
       a		:Tuple									=			len(self.conv_dim       )
       a		:Any									=			num_hidden_layers
       a		:Tuple									=			intermediate_size
       a		:Any									=			hidden_act
       a		:List[str]									=			num_attention_heads
       a		:int									=			hidden_dropout
       a		:Optional[Any]									=			attention_dropout
       a		:Dict									=			activation_dropout
       a		:Any									=			feat_proj_dropout
       a		:Dict									=			final_dropout
       a		:int									=			layerdrop
       a		:Tuple									=			layer_norm_eps
       a		:Union[str, Any]									=			initializer_range
       a		:Union[str, Any]									=			num_ctc_classes
       a		:Optional[Any]									=			vocab_size
       a		:str									=			do_stable_layer_norm
       a		:Dict									=			use_weighted_layer_sum
       a		:Any									=			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
       a		:List[Any]									=			apply_spec_augment
       a		:Optional[int]									=			mask_time_prob
       a		:Optional[Any]									=			mask_time_length
       a		:Tuple									=			mask_time_min_masks
       a		:Dict									=			mask_feature_prob
       a		:List[Any]									=			mask_feature_length

       # parameters for pretraining with codevector quantized representations
       a		:Tuple									=			num_codevectors_per_group
       a		:List[str]									=			num_codevector_groups
       a		:List[Any]									=			contrastive_logits_temperature
       a		:Union[str, Any]									=			num_negatives
       a		:Union[str, Any]									=			codevector_dim
       a		:Dict									=			proj_codevector_dim
       a		:Union[str, Any]									=			diversity_loss_weight

       # ctc loss
       a		:Dict									=			ctc_loss_reduction
       a		:Optional[int]									=			ctc_zero_infinity

       # adapter
       a		:Any									=			add_adapter
       a		:str									=			adapter_kernel_size
       a		:Tuple									=			adapter_stride
       a		:Dict									=			num_adapter_layers
       a		:Any									=			output_hidden_size or hidden_size

       # SequenceClassification-specific parameter. Feel free to ignore for other classes.
       a		:Tuple									=			classifier_proj_size

       # XVector-specific parameters. Feel free to ignore for other classes.
       a		:List[Any]									=			list(_lowerCamelCase       )
       a		:Optional[int]									=			list(_lowerCamelCase       )
       a		:List[str]									=			list(_lowerCamelCase       )
       a		:Optional[int]									=			xvector_output_dim




 @property
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       return functools.reduce(operator.mul   ,    self.conv_stride   ,    1       )





