


"""simple docstring"""

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    lowerCamelCase__						(		_a    ):
      a :     List[str]											=		"""wavlm"""
      def __init__(		self	:	Dict      ,       A_	:	Any=3_2      ,       A_	:	Dict=7_6_8      ,       A_	:	List[Any]=1_2      ,       A_	:	Any=1_2      ,       A_	:	Optional[Any]=3_0_7_2      ,       A_	:	Dict="gelu"      ,       A_	:	List[Any]=0.1      ,       A_	:	Optional[int]=0.1      ,       A_	:	Optional[Any]=0.1      ,       A_	:	Dict=0.0      ,       A_	:	List[Any]=0.1      ,       A_	:	Optional[int]=0.1      ,       A_	:	Union[str, Any]=0.02      ,       A_	:	Dict=1e-5      ,       A_	:	List[Any]="group"      ,       A_	:	List[Any]="gelu"      ,       A_	:	Union[str, Any]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)      ,       A_	:	str=(5, 2, 2, 2, 2, 2, 2)      ,       A_	:	List[str]=(1_0, 3, 3, 3, 3, 2, 2)      ,       A_	:	Tuple=False      ,       A_	:	Union[str, Any]=1_2_8      ,       A_	:	int=1_6      ,       A_	:	Optional[int]=3_2_0      ,       A_	:	Any=8_0_0      ,       A_	:	Any=False      ,       A_	:	Optional[int]=True      ,       A_	:	int=0.05      ,       A_	:	str=1_0      ,       A_	:	Optional[int]=2      ,       A_	:	List[Any]=0.0      ,       A_	:	Tuple=1_0      ,       A_	:	List[str]=3_2_0      ,       A_	:	Optional[int]=2      ,       A_	:	Dict=0.1      ,       A_	:	Union[str, Any]=1_0_0      ,       A_	:	Any=2_5_6      ,       A_	:	List[str]=2_5_6      ,       A_	:	Any=0.1      ,       A_	:	int="mean"      ,       A_	:	int=False      ,       A_	:	List[Any]=False      ,       A_	:	Dict=2_5_6      ,       A_	:	str=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0)      ,       A_	:	List[str]=(5, 3, 3, 1, 1)      ,       A_	:	str=(1, 2, 3, 1, 1)      ,       A_	:	Any=5_1_2      ,       A_	:	Tuple=8_0      ,       A_	:	Optional[int]=0      ,       A_	:	Union[str, Any]=1      ,       A_	:	int=2      ,       A_	:	int=False      ,       A_	:	str=3      ,       A_	:	Tuple=2      ,       A_	:	Optional[Any]=3      ,       A_	:	Optional[Any]=None      ,       **A_	:	Any      ,       ):






       '''simple docstring'''


       super().__init__(**A_      ,       pad_token_id=A_      ,       bos_token_id=A_      ,       eos_token_id=A_							)
       __lowercase									=	hidden_size
       __lowercase									=	feat_extract_norm
       __lowercase									=	feat_extract_activation
       __lowercase									=	list(A_							)
       __lowercase									=	list(A_							)
       __lowercase									=	list(A_							)
       __lowercase									=	conv_bias
       __lowercase									=	num_buckets
       __lowercase									=	max_bucket_distance
       __lowercase									=	num_conv_pos_embeddings
       __lowercase									=	num_conv_pos_embedding_groups
       __lowercase									=	len(self.conv_dim							)
       __lowercase									=	num_hidden_layers
       __lowercase									=	intermediate_size
       __lowercase									=	hidden_act
       __lowercase									=	num_attention_heads
       __lowercase									=	hidden_dropout
       __lowercase									=	attention_dropout
       __lowercase									=	activation_dropout
       __lowercase									=	feat_proj_dropout
       __lowercase									=	final_dropout
       __lowercase									=	layerdrop
       __lowercase									=	layer_norm_eps
       __lowercase									=	initializer_range
       __lowercase									=	num_ctc_classes
       __lowercase									=	vocab_size
       __lowercase									=	do_stable_layer_norm
       __lowercase									=	use_weighted_layer_sum
       __lowercase									=	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
       __lowercase									=	apply_spec_augment
       __lowercase									=	mask_time_prob
       __lowercase									=	mask_time_length
       __lowercase									=	mask_time_min_masks
       __lowercase									=	mask_feature_prob
       __lowercase									=	mask_feature_length

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

       # ctc loss
       __lowercase									=	ctc_loss_reduction
       __lowercase									=	ctc_zero_infinity

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

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

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






      @property
      def SCREAMING_SNAKE_CASE_	(		self	:	Tuple							):






       '''simple docstring'''


       return functools.reduce(operator.mul      ,       self.conv_stride      ,       1							)




"""simple docstring"""

import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers.models.realm.configuration_realm import RealmConfig
from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever
from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer





class    lowerCamelCase__						(		_a    ):
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[Any]							):






       '''simple docstring'''


       __lowercase									=	tempfile.mkdtemp()
       __lowercase									=	5

       # Realm tok
       __lowercase									=	[
           """[UNK]""",
           """[CLS]""",
           """[SEP]""",
           """[PAD]""",
           """[MASK]""",
           """test""",
           """question""",
           """this""",
           """is""",
           """the""",
           """first""",
           """second""",
           """third""",
           """fourth""",
           """fifth""",
           """record""",
           """want""",
           """##want""",
           """##ed""",
           """wa""",
           """un""",
           """runn""",
           """##ing""",
           """,""",
           """low""",
           """lowest""",
       ]
       __lowercase									=	os.path.join(self.tmpdirname      ,       """realm_tokenizer"""							)
       os.makedirs(A_      ,       exist_ok=A_							)
       __lowercase									=	os.path.join(A_      ,       VOCAB_FILES_NAMES["""vocab_file"""]							)
       with open(self.vocab_file      ,       """w"""      ,       encoding="""utf-8"""							) as vocab_writer:
        vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]							)							)

       __lowercase									=	os.path.join(self.tmpdirname      ,       """realm_block_records"""							)
       os.makedirs(A_      ,       exist_ok=A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]							):






       '''simple docstring'''


       return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname      ,       """realm_tokenizer"""							)							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]							):






       '''simple docstring'''


       shutil.rmtree(self.tmpdirname							)
      def SCREAMING_SNAKE_CASE_	(		self	:	int							):






       '''simple docstring'''


       __lowercase									=	RealmConfig(num_block_records=self.num_block_records							)
       return config
      def SCREAMING_SNAKE_CASE_	(		self	:	Any							):






       '''simple docstring'''


       __lowercase									=	Dataset.from_dict(
           {
               """id""": ["""0""", """1"""],
               """question""": ["""foo""", """bar"""],
               """answers""": [["""Foo""", """Bar"""], ["""Bar"""]],
           }							)
       return dataset
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[Any]							):






       '''simple docstring'''


       __lowercase									=	np.array(
           [
               B"""This is the first record""",
               B"""This is the second record""",
               B"""This is the third record""",
               B"""This is the fourth record""",
               B"""This is the fifth record""",
               B"""This is a longer longer longer record""",
           ]      ,       dtype=A_      ,       )
       return block_records
      def SCREAMING_SNAKE_CASE_	(		self	:	Union[str, Any]							):






       '''simple docstring'''


       __lowercase									=	RealmRetriever(
           block_records=self.get_dummy_block_records()      ,       tokenizer=self.get_tokenizer()      ,       )
       return retriever
      def SCREAMING_SNAKE_CASE_	(		self	:	Dict							):






       '''simple docstring'''


       __lowercase									=	self.get_config()
       __lowercase									=	self.get_dummy_retriever()
       __lowercase									=	retriever.tokenizer

       __lowercase									=	np.array([0, 3]      ,       dtype="""long"""							)
       __lowercase									=	tokenizer(["""Test question"""]							).input_ids
       __lowercase									=	tokenizer(
           ["""the fourth"""]      ,       add_special_tokens=A_      ,       return_token_type_ids=A_      ,       return_attention_mask=A_      ,       ).input_ids
       __lowercase									=	config.reader_seq_len

       __lowercase							,		__lowercase							,		__lowercase							,		__lowercase									=	retriever(
           A_      ,       A_      ,       answer_ids=A_      ,       max_length=A_      ,       return_tensors="""np"""							)

       self.assertEqual(len(A_							)      ,       2							)
       self.assertEqual(len(A_							)      ,       2							)
       self.assertEqual(len(A_							)      ,       2							)
       self.assertEqual(concat_inputs.input_ids.shape      ,       (2, 1_0)							)
       self.assertEqual(concat_inputs.attention_mask.shape      ,       (2, 1_0)							)
       self.assertEqual(concat_inputs.token_type_ids.shape      ,       (2, 1_0)							)
       self.assertEqual(concat_inputs.special_tokens_mask.shape      ,       (2, 1_0)							)
       self.assertEqual(
           tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0]							)      ,       ["""[CLS]""", """test""", """question""", """[SEP]""", """this""", """is""", """the""", """first""", """record""", """[SEP]"""]      ,       )
       self.assertEqual(
           tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1]							)      ,       ["""[CLS]""", """test""", """question""", """[SEP]""", """this""", """is""", """the""", """fourth""", """record""", """[SEP]"""]      ,       )
      def SCREAMING_SNAKE_CASE_	(		self	:	str							):






       '''simple docstring'''


       __lowercase									=	self.get_config()
       __lowercase									=	self.get_dummy_retriever()
       __lowercase									=	retriever.tokenizer

       __lowercase									=	np.array([0, 3, 5]      ,       dtype="""long"""							)
       __lowercase									=	tokenizer(["""Test question"""]							).input_ids
       __lowercase									=	tokenizer(
           ["""the fourth""", """longer longer"""]      ,       add_special_tokens=A_      ,       return_token_type_ids=A_      ,       return_attention_mask=A_      ,       ).input_ids
       __lowercase									=	config.reader_seq_len

       __lowercase							,		__lowercase							,		__lowercase							,		__lowercase									=	retriever(
           A_      ,       A_      ,       answer_ids=A_      ,       max_length=A_      ,       return_tensors="""np"""							)

       self.assertEqual([False, True, True]      ,       A_							)
       self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]]      ,       A_							)
       self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]]      ,       A_							)






      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[Any]							):






       '''simple docstring'''


       __lowercase									=	self.get_dummy_retriever()
       retriever.save_pretrained(os.path.join(self.tmpdirname      ,       """realm_block_records"""							)							)

       # Test local path
       __lowercase									=	retriever.from_pretrained(os.path.join(self.tmpdirname      ,       """realm_block_records"""							)							)
       self.assertEqual(retriever.block_records[0]      ,       B"""This is the first record"""							)

       # Test mocked remote path
       with patch("""transformers.models.realm.retrieval_realm.hf_hub_download"""							) as mock_hf_hub_download:
        __lowercase									=	os.path.join(
            os.path.join(self.tmpdirname      ,       """realm_block_records"""							)      ,       _REALM_BLOCK_RECORDS_FILENAME							)
        __lowercase									=	RealmRetriever.from_pretrained("""google/realm-cc-news-pretrained-openqa"""							)

       self.assertEqual(retriever.block_records[0]      ,       B"""This is the first record"""							)
