






'''simple docstring'''







import argparse

import torch

# Step 1. clone https://github.com/microsoft/unilm
# Step 2. git checkout to https://github.com/microsoft/unilm/commit/b94ec76c36f02fb2b0bf0dcb0b8554a2185173cd
# Step 3. cd unilm
# Step 4. ln -s $(realpath wavlm/modules.py) ./  # create simlink
# import classes
from unilm.wavlm.WavLM import WavLM as WavLMOrig
from unilm.wavlm.WavLM import WavLMConfig as WavLMConfigOrig

from transformers import WavLMConfig, WavLMModel, logging


logging.set_verbosity_info()
__lowerCAmelCase       		=      logging.get_logger(__name__)

__lowerCAmelCase       		=      {
    'post_extract_proj': 'feature_projection.projection',
    'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv',
    'self_attn.k_proj': 'encoder.layers.*.attention.k_proj',
    'self_attn.v_proj': 'encoder.layers.*.attention.v_proj',
    'self_attn.q_proj': 'encoder.layers.*.attention.q_proj',
    'self_attn.out_proj': 'encoder.layers.*.attention.out_proj',
    'self_attn.grep_linear': 'encoder.layers.*.attention.gru_rel_pos_linear',
    'self_attn.relative_attention_bias': 'encoder.layers.*.attention.rel_attn_embed',
    'self_attn.grep_a': 'encoder.layers.*.attention.gru_rel_pos_const',
    'self_attn_layer_norm': 'encoder.layers.*.layer_norm',
    'fc1': 'encoder.layers.*.feed_forward.intermediate_dense',
    'fc2': 'encoder.layers.*.feed_forward.output_dense',
    'final_layer_norm': 'encoder.layers.*.final_layer_norm',
    'encoder.layer_norm': 'encoder.layer_norm',
    'w2v_model.layer_norm': 'feature_projection.layer_norm',
    'quantizer.weight_proj': 'quantizer.weight_proj',
    'quantizer.vars': 'quantizer.codevectors',
    'project_q': 'project_q',
    'final_proj': 'project_hid',
    'w2v_encoder.proj': 'ctc_proj',
    'mask_emb': 'masked_spec_embed',
}
__lowerCAmelCase       		=      [
    'ctc_proj',
    'quantizer.weight_proj',
    'quantizer.codevectors',
    'project_q',
    'project_hid',
]





def 		_UpperCAmelCase   (							__A							:   List[Any]						,		__A							:   int						,		__A							:   List[Any]						,		__A							:   Dict						,		__A							:   List[str]      ):
				for attribute in key.split('''.'''      ):
								a_		:  Optional[int]											=      getattr(__A						,		__A      )

				if weight_type is not None:
								a_		:  str											=      getattr(__A						,		__A      ).shape
				else:
								a_		:  Tuple											=      hf_pointer.shape

				assert hf_shape == value.shape, (
				    f'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be'
				    f' {value.shape} for {full_name}'
				)

				if weight_type == "weight":
								a_		:  str											=      value
				elif weight_type == "weight_g":
								a_		:  Tuple											=      value
				elif weight_type == "weight_v":
								a_		:  str											=      value
				elif weight_type == "bias":
								a_		:  List[Any]											=      value
				else:
								a_		:  str											=      value

				logger.info(f'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.'      )





def 		_UpperCAmelCase   (							__A							:   List[str]						,		__A							:   Optional[int]      ):
				a_		:  Any											=      []
				a_		:  Dict											=      fairseq_model.state_dict()

				a_		:  Dict											=      hf_model.feature_extractor

				for name, value in fairseq_dict.items():
								a_		:  List[Any]											=      False
								if "conv_layers" in name:
												load_conv_layer(
												    __A						,		__A						,		__A						,		__A						,		hf_model.config.feat_extract_norm == '''group'''						,		)
												a_		:  List[Any]											=      True
								else:
												for key, mapped_key in MAPPING.items():
																if key in name or key.split('''w2v_model.'''      )[-1] == name.split('''.'''      )[0]:
																				a_		:  Optional[int]											=      True
																				if "*" in mapped_key:
																								a_		:  Optional[int]											=      name.split(__A      )[0].split('''.'''      )[-2]
																								a_		:  Tuple											=      mapped_key.replace('''*'''						,		__A      )
																				if "weight_g" in name:
																								a_		:  List[str]											=      '''weight_g'''
																				elif "weight_v" in name:
																								a_		:  List[Any]											=      '''weight_v'''
																				elif "bias" in name and "relative_attention_bias" not in name:
																								a_		:  Optional[Any]											=      '''bias'''
																				elif "weight" in name:
																								# TODO: don't match quantizer.weight_proj
																								a_		:  Any											=      '''weight'''
																				else:
																								a_		:  List[str]											=      None

																				set_recursively(__A						,		__A						,		__A						,		__A						,		__A      )
																continue
								if not is_used:
												unused_weights.append(__A      )

				logger.warning(f'Unused weights: {unused_weights}'      )





def 		_UpperCAmelCase   (							__A							:   Optional[Any]						,		__A							:   Union[str, Any]						,		__A							:   Any						,		__A							:   Optional[int]						,		__A							:   Union[str, Any]      ):
				a_		:  Optional[Any]											=      full_name.split('''conv_layers.'''      )[-1]
				a_		:  Union[str, Any]											=      name.split('''.'''      )
				a_		:  Optional[int]											=      int(items[0]      )
				a_		:  Optional[Any]											=      int(items[1]      )

				if type_id == 0:
								if "bias" in name:
												assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
												    f'{full_name} has size {value.shape}, but'
												    f' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.'
												)
												a_		:  Tuple											=      value
												logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.'      )
								elif "weight" in name:
												assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
												    f'{full_name} has size {value.shape}, but'
												    f' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.'
												)
												a_		:  Dict											=      value
												logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.'      )
				elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
								if "bias" in name:
												assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
												    f'{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was'
												    " found."
												)
												a_		:  str											=      value
												logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'      )
								elif "weight" in name:
												assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
												    f'{full_name} has size {value.shape}, but'
												    f' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.'
												)
												a_		:  List[Any]											=      value
												logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'      )
				else:
								unused_weights.append(__A      )





@torch.no_grad()
def 		_UpperCAmelCase   (							__A							:   Optional[int]						,		__A							:   Union[str, Any]						,		__A							:   int=None      ):
				# load the pre-trained checkpoints
				a_		:  Dict											=      torch.load(__A      )
				a_		:  List[str]											=      WavLMConfigOrig(checkpoint['''cfg''']      )
				a_		:  Any											=      WavLMOrig(__A      )
				model.load_state_dict(checkpoint['''model''']      )
				model.eval()

				if config_path is not None:
								a_		:  Optional[int]											=      WavLMConfig.from_pretrained(__A      )
				else:
								a_		:  List[Any]											=      WavLMConfig()

				a_		:  str											=      WavLMModel(__A      )

				recursively_load_weights(__A						,		__A      )

				hf_wavlm.save_pretrained(__A      )


if __name__ == "__main__":
		__lowerCAmelCase       		=      argparse.ArgumentParser()
		parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
		parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint')
		parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert')
		__lowerCAmelCase       		=      parser.parse_args()
		convert_wavlm_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)












'''simple docstring'''







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

import numpy as np
from datasets import Dataset

from transformers import is_faiss_available
from transformers.models.bart.configuration_bart import BartConfig
from transformers.models.bart.tokenization_bart import BartTokenizer
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES as DPR_VOCAB_FILES_NAMES
from transformers.models.dpr.configuration_dpr import DPRConfig
from transformers.models.dpr.tokenization_dpr import DPRContextEncoderTokenizer, DPRQuestionEncoderTokenizer
from transformers.models.rag.configuration_rag import RagConfig
from transformers.models.rag.retrieval_rag import CustomHFIndex, RagRetriever
from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES as BART_VOCAB_FILES_NAMES
from transformers.testing_utils import require_faiss, require_sentencepiece, require_tokenizers, require_torch


if is_faiss_available():
		import faiss





@require_faiss
class 	SCREAMING_SNAKE_CASE					(					SCREAMING_SNAKE_CASE_       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Dict:
							a_		:  Union[str, Any]											=      tempfile.mkdtemp()
							a_		:  Union[str, Any]											=      8

							# DPR tok
							a_		:  Dict											=      [
							    '''[UNK]''',
							    '''[CLS]''',
							    '''[SEP]''',
							    '''[PAD]''',
							    '''[MASK]''',
							    '''want''',
							    '''##want''',
							    '''##ed''',
							    '''wa''',
							    '''un''',
							    '''runn''',
							    '''##ing''',
							    ''',''',
							    '''low''',
							    '''lowest''',
							]
							a_		:  str											=      os.path.join(self.tmpdirname  ,						'''dpr_tokenizer'''					)
							os.makedirs(__SCREAMING_SNAKE_CASE  ,						exist_ok=__SCREAMING_SNAKE_CASE					)
							a_		:  Optional[int]											=      os.path.join(__SCREAMING_SNAKE_CASE  ,						DPR_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]					)					)

							# BART tok
							a_		:  Union[str, Any]											=      [
							    '''l''',
							    '''o''',
							    '''w''',
							    '''e''',
							    '''r''',
							    '''s''',
							    '''t''',
							    '''i''',
							    '''d''',
							    '''n''',
							    '''\u0120''',
							    '''\u0120l''',
							    '''\u0120n''',
							    '''\u0120lo''',
							    '''\u0120low''',
							    '''er''',
							    '''\u0120lowest''',
							    '''\u0120newer''',
							    '''\u0120wider''',
							    '''<unk>''',
							]
							a_		:  int											=      dict(zip(__SCREAMING_SNAKE_CASE  ,						range(len(__SCREAMING_SNAKE_CASE					)					)					)					)
							a_		:  int											=      ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', '''''']
							a_		:  Optional[int]											=      {'''unk_token''': '''<unk>'''}

							a_		:  List[str]											=      os.path.join(self.tmpdirname  ,						'''bart_tokenizer'''					)
							os.makedirs(__SCREAMING_SNAKE_CASE  ,						exist_ok=__SCREAMING_SNAKE_CASE					)
							a_		:  Tuple											=      os.path.join(__SCREAMING_SNAKE_CASE  ,						BART_VOCAB_FILES_NAMES['''vocab_file''']					)
							a_		:  int											=      os.path.join(__SCREAMING_SNAKE_CASE  ,						BART_VOCAB_FILES_NAMES['''merges_file''']					)
							with open(self.vocab_file  ,						'''w'''  ,						encoding='''utf-8'''					) as fp:
											fp.write(json.dumps(__SCREAMING_SNAKE_CASE					) + '''\n'''					)
							with open(self.merges_file  ,						'''w'''  ,						encoding='''utf-8'''					) as fp:
											fp.write('''\n'''.join(__SCREAMING_SNAKE_CASE					)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			DPRQuestionEncoderTokenizer:
							return DPRQuestionEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname  ,						'''dpr_tokenizer'''					)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			DPRContextEncoderTokenizer:
							return DPRContextEncoderTokenizer.from_pretrained(os.path.join(self.tmpdirname  ,						'''dpr_tokenizer'''					)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			BartTokenizer:
							return BartTokenizer.from_pretrained(os.path.join(self.tmpdirname  ,						'''bart_tokenizer'''					)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[Any]					)			->			Union[str, Any]:
							shutil.rmtree(self.tmpdirname					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			Union[str, Any]:
							a_		:  str											=      Dataset.from_dict(
							    {
							        '''id''': ['''0''', '''1'''],
							        '''text''': ['''foo''', '''bar'''],
							        '''title''': ['''Foo''', '''Bar'''],
							        '''embeddings''': [np.ones(self.retrieval_vector_size					), 2 * np.ones(self.retrieval_vector_size					)],
							    }					)
							dataset.add_faiss_index('''embeddings'''  ,						string_factory='''Flat'''  ,						metric_type=faiss.METRIC_INNER_PRODUCT					)
							return dataset






			def 			SCREAMING_SNAKE_CASE      ( self      :			List[str]					)			->			Tuple:
							a_		:  List[str]											=      self.get_dummy_dataset()
							a_		:  Tuple											=      RagConfig(
							    retrieval_vector_size=self.retrieval_vector_size  ,						question_encoder=DPRConfig().to_dict()  ,						generator=BartConfig().to_dict()  ,						)
							with patch('''transformers.models.rag.retrieval_rag.load_dataset'''					) as mock_load_dataset:
											a_		:  Tuple											=      dataset
											a_		:  Any											=      RagRetriever(
											    __SCREAMING_SNAKE_CASE  ,						question_encoder_tokenizer=self.get_dpr_tokenizer()  ,						generator_tokenizer=self.get_bart_tokenizer()  ,						)
							return retriever






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict  ,						__SCREAMING_SNAKE_CASE      :			bool					)			->			Dict:
							a_		:  Dict											=      self.get_dummy_dataset()
							a_		:  Dict											=      RagConfig(
							    retrieval_vector_size=self.retrieval_vector_size  ,						question_encoder=DPRConfig().to_dict()  ,						generator=BartConfig().to_dict()  ,						index_name='''custom'''  ,						)
							if from_disk:
											a_		:  Optional[int]											=      os.path.join(self.tmpdirname  ,						'''dataset'''					)
											a_		:  str											=      os.path.join(self.tmpdirname  ,						'''index.faiss'''					)
											dataset.get_index('''embeddings'''					).save(os.path.join(self.tmpdirname  ,						'''index.faiss'''					)					)
											dataset.drop_index('''embeddings'''					)
											dataset.save_to_disk(os.path.join(self.tmpdirname  ,						'''dataset'''					)					)
											del dataset
											a_		:  int											=      RagRetriever(
											    __SCREAMING_SNAKE_CASE  ,						question_encoder_tokenizer=self.get_dpr_tokenizer()  ,						generator_tokenizer=self.get_bart_tokenizer()  ,						)
							else:
											a_		:  Optional[Any]											=      RagRetriever(
											    __SCREAMING_SNAKE_CASE  ,						question_encoder_tokenizer=self.get_dpr_tokenizer()  ,						generator_tokenizer=self.get_bart_tokenizer()  ,						index=CustomHFIndex(config.retrieval_vector_size  ,						__SCREAMING_SNAKE_CASE					)  ,						)
							return retriever






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			Optional[int]:
							a_		:  str											=      Dataset.from_dict(
							    {
							        '''id''': ['''0''', '''1'''],
							        '''text''': ['''foo''', '''bar'''],
							        '''title''': ['''Foo''', '''Bar'''],
							        '''embeddings''': [np.ones(self.retrieval_vector_size + 1					), 2 * np.ones(self.retrieval_vector_size + 1					)],
							    }					)
							dataset.add_faiss_index('''embeddings'''  ,						string_factory='''Flat'''  ,						metric_type=faiss.METRIC_INNER_PRODUCT					)

							a_		:  Optional[int]											=      os.path.join(self.tmpdirname  ,						'''hf_bert_base.hnswSQ8_correct_phi_128.c_index'''					)
							dataset.save_faiss_index('''embeddings'''  ,						index_file_name + '''.index.dpr'''					)
							pickle.dump(dataset['''id''']  ,						open(index_file_name + '''.index_meta.dpr'''  ,						'''wb'''					)					)

							a_		:  Union[str, Any]											=      os.path.join(self.tmpdirname  ,						'''psgs_w100.tsv.pkl'''					)
							a_		:  Dict											=      {sample['''id''']: [sample['''text'''], sample['''title''']] for sample in dataset}
							pickle.dump(__SCREAMING_SNAKE_CASE  ,						open(__SCREAMING_SNAKE_CASE  ,						'''wb'''					)					)

							a_		:  Optional[Any]											=      RagConfig(
							    retrieval_vector_size=self.retrieval_vector_size  ,						question_encoder=DPRConfig().to_dict()  ,						generator=BartConfig().to_dict()  ,						index_name='''legacy'''  ,						index_path=self.tmpdirname  ,						)
							a_		:  int											=      RagRetriever(
							    __SCREAMING_SNAKE_CASE  ,						question_encoder_tokenizer=self.get_dpr_tokenizer()  ,						generator_tokenizer=self.get_bart_tokenizer()					)
							return retriever






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Dict:
							a_		:  Optional[Any]											=      1
							a_		:  Dict											=      self.get_dummy_canonical_hf_index_retriever()
							a_		:  Tuple											=      np.array(
							    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]  ,						dtype=np.floataa					)
							a_				,			a_				,			a_		:  str											=      retriever.retrieve(__SCREAMING_SNAKE_CASE  ,						n_docs=__SCREAMING_SNAKE_CASE					)
							self.assertEqual(retrieved_doc_embeds.shape  ,						(2, n_docs, self.retrieval_vector_size)					)
							self.assertEqual(len(__SCREAMING_SNAKE_CASE					)  ,						2					)
							self.assertEqual(sorted(doc_dicts[0]					)  ,						['''embeddings''', '''id''', '''text''', '''title''']					)
							self.assertEqual(len(doc_dicts[0]['''id''']					)  ,						__SCREAMING_SNAKE_CASE					)
							self.assertEqual(doc_dicts[0]['''id'''][0]  ,						'''1'''					)  # max inner product is reached with second doc
							self.assertEqual(doc_dicts[1]['''id'''][0]  ,						'''0'''					)  # max inner product is reached with first doc
							self.assertListEqual(doc_ids.tolist()  ,						[[1], [0]]					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Dict:
							a_		:  str											=      self.get_dummy_canonical_hf_index_retriever()
							with tempfile.TemporaryDirectory() as tmp_dirname:
											with patch('''transformers.models.rag.retrieval_rag.load_dataset'''					) as mock_load_dataset:
															a_		:  List[str]											=      self.get_dummy_dataset()
															retriever.save_pretrained(__SCREAMING_SNAKE_CASE					)
															a_		:  Optional[Any]											=      RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE					)
															self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
															a_		:  Union[str, Any]											=      np.array(
															    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]  ,						dtype=np.floataa					)
															a_		:  List[str]											=      retriever.retrieve(__SCREAMING_SNAKE_CASE  ,						n_docs=1					)
															self.assertTrue(out is not None					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			int:
							a_		:  Union[str, Any]											=      1
							a_		:  Optional[Any]											=      self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE					)
							a_		:  List[Any]											=      np.array(
							    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]  ,						dtype=np.floataa					)
							a_				,			a_				,			a_		:  Any											=      retriever.retrieve(__SCREAMING_SNAKE_CASE  ,						n_docs=__SCREAMING_SNAKE_CASE					)
							self.assertEqual(retrieved_doc_embeds.shape  ,						(2, n_docs, self.retrieval_vector_size)					)
							self.assertEqual(len(__SCREAMING_SNAKE_CASE					)  ,						2					)
							self.assertEqual(sorted(doc_dicts[0]					)  ,						['''embeddings''', '''id''', '''text''', '''title''']					)
							self.assertEqual(len(doc_dicts[0]['''id''']					)  ,						__SCREAMING_SNAKE_CASE					)
							self.assertEqual(doc_dicts[0]['''id'''][0]  ,						'''1'''					)  # max inner product is reached with second doc
							self.assertEqual(doc_dicts[1]['''id'''][0]  ,						'''0'''					)  # max inner product is reached with first doc
							self.assertListEqual(doc_ids.tolist()  ,						[[1], [0]]					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			str					)			->			List[str]:
							a_		:  Dict											=      self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE					)
							with tempfile.TemporaryDirectory() as tmp_dirname:
											retriever.save_pretrained(__SCREAMING_SNAKE_CASE					)
											a_		:  List[str]											=      RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE					)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
											a_		:  Tuple											=      np.array(
											    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]  ,						dtype=np.floataa					)
											a_		:  Dict											=      retriever.retrieve(__SCREAMING_SNAKE_CASE  ,						n_docs=1					)
											self.assertTrue(out is not None					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Optional[int]:
							a_		:  Union[str, Any]											=      1
							a_		:  str											=      self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE					)
							a_		:  Optional[int]											=      np.array(
							    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]  ,						dtype=np.floataa					)
							a_				,			a_				,			a_		:  Tuple											=      retriever.retrieve(__SCREAMING_SNAKE_CASE  ,						n_docs=__SCREAMING_SNAKE_CASE					)
							self.assertEqual(retrieved_doc_embeds.shape  ,						(2, n_docs, self.retrieval_vector_size)					)
							self.assertEqual(len(__SCREAMING_SNAKE_CASE					)  ,						2					)
							self.assertEqual(sorted(doc_dicts[0]					)  ,						['''embeddings''', '''id''', '''text''', '''title''']					)
							self.assertEqual(len(doc_dicts[0]['''id''']					)  ,						__SCREAMING_SNAKE_CASE					)
							self.assertEqual(doc_dicts[0]['''id'''][0]  ,						'''1'''					)  # max inner product is reached with second doc
							self.assertEqual(doc_dicts[1]['''id'''][0]  ,						'''0'''					)  # max inner product is reached with first doc
							self.assertListEqual(doc_ids.tolist()  ,						[[1], [0]]					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			Dict:
							a_		:  List[str]											=      self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE					)
							with tempfile.TemporaryDirectory() as tmp_dirname:
											retriever.save_pretrained(__SCREAMING_SNAKE_CASE					)
											a_		:  Any											=      RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE					)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
											a_		:  Tuple											=      np.array(
											    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]  ,						dtype=np.floataa					)
											a_		:  Dict											=      retriever.retrieve(__SCREAMING_SNAKE_CASE  ,						n_docs=1					)
											self.assertTrue(out is not None					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			Any:
							a_		:  str											=      1
							a_		:  Tuple											=      self.get_dummy_legacy_index_retriever()
							a_		:  Union[str, Any]											=      np.array(
							    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]  ,						dtype=np.floataa					)
							a_				,			a_				,			a_		:  Any											=      retriever.retrieve(__SCREAMING_SNAKE_CASE  ,						n_docs=__SCREAMING_SNAKE_CASE					)
							self.assertEqual(retrieved_doc_embeds.shape  ,						(2, n_docs, self.retrieval_vector_size)					)
							self.assertEqual(len(__SCREAMING_SNAKE_CASE					)  ,						2					)
							self.assertEqual(sorted(doc_dicts[0]					)  ,						['''text''', '''title''']					)
							self.assertEqual(len(doc_dicts[0]['''text''']					)  ,						__SCREAMING_SNAKE_CASE					)
							self.assertEqual(doc_dicts[0]['''text'''][0]  ,						'''bar'''					)  # max inner product is reached with second doc
							self.assertEqual(doc_dicts[1]['''text'''][0]  ,						'''foo'''					)  # max inner product is reached with first doc
							self.assertListEqual(doc_ids.tolist()  ,						[[1], [0]]					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			Optional[int]:
							a_		:  List[str]											=      self.get_dummy_legacy_index_retriever()
							with tempfile.TemporaryDirectory() as tmp_dirname:
											retriever.save_pretrained(__SCREAMING_SNAKE_CASE					)
											a_		:  Any											=      RagRetriever.from_pretrained(__SCREAMING_SNAKE_CASE					)
											self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
											a_		:  List[str]											=      np.array(
											    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]  ,						dtype=np.floataa					)
											a_		:  Optional[Any]											=      retriever.retrieve(__SCREAMING_SNAKE_CASE  ,						n_docs=1					)
											self.assertTrue(out is not None					)






			@require_torch
			@require_tokenizers
			@require_sentencepiece
			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			List[str]:
							import torch

							a_		:  Any											=      1
							a_		:  List[Any]											=      self.get_dummy_canonical_hf_index_retriever()
							a_		:  Union[str, Any]											=      [[5, 7], [10, 11]]
							a_		:  Optional[int]											=      np.array(
							    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]  ,						dtype=np.floataa					)
							a_		:  str											=      retriever(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						prefix=retriever.config.generator.prefix  ,						n_docs=__SCREAMING_SNAKE_CASE					)
							a_				,			a_				,			a_		:  List[str]											=      (
							    out['''context_input_ids'''],
							    out['''context_attention_mask'''],
							    out['''retrieved_doc_embeds'''],
							)
							self.assertEqual(retrieved_doc_embeds.shape  ,						(2, n_docs, self.retrieval_vector_size)					)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						np.ndarray					)

							a_		:  Any											=      retriever(
							    __SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						prefix=retriever.config.generator.prefix  ,						n_docs=__SCREAMING_SNAKE_CASE  ,						return_tensors='''pt'''  ,						)
							a_				,			a_				,			a_				,			a_		:  str											=      (  # noqa: F841
							    out['''context_input_ids'''],
							    out['''context_attention_mask'''],
							    out['''retrieved_doc_embeds'''],
							    out['''doc_ids'''],
							)
							self.assertEqual(retrieved_doc_embeds.shape  ,						(2, n_docs, self.retrieval_vector_size)					)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						torch.Tensor					)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						torch.Tensor					)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,						torch.Tensor					)







			@require_torch
			@require_tokenizers
			@require_sentencepiece
			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Any:
							a_		:  str											=      self.get_dpr_ctx_encoder_tokenizer()
							a_		:  Tuple											=      1
							a_		:  Any											=      self.get_dummy_custom_hf_index_retriever(from_disk=__SCREAMING_SNAKE_CASE					)
							retriever.set_ctx_encoder_tokenizer(__SCREAMING_SNAKE_CASE					)

							a_		:  Dict											=      [[5, 7], [10, 11]]
							a_		:  List[str]											=      np.array(
							    [np.ones(self.retrieval_vector_size					), -np.ones(self.retrieval_vector_size					)]  ,						dtype=np.floataa					)
							a_		:  List[Any]											=      retriever(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						prefix=retriever.config.generator.prefix  ,						n_docs=__SCREAMING_SNAKE_CASE					)

							self.assertEqual(
							    len(__SCREAMING_SNAKE_CASE					)  ,						6					)  # check whether the retriever output consist of 6 attributes including tokenized docs
							self.assertEqual(
							    all(k in out for k in ('''tokenized_doc_ids''', '''tokenized_doc_attention_mask''')					)  ,						__SCREAMING_SNAKE_CASE					)  # check for doc token related keys in dictionary.




