Manuscript Offprint

arXiv:2609.07276

Matryoshka Hash Representations for Model-Aware Compact Semantic Retrieval

Peichun Hua, Yunming Xiao

Abstract

Retrieval-augmented generation (RAG) depends on dense retrieval: each document is stored as a learned vector, and a query is answered by finding its nearest neighbors in that vector space. Keeping one full-precision vector per document is the dominant index cost at corpus scale, so retrieval systems replace each vector with a short code of a few bytes—a step called quantization. Standard quantizers such as product quantization (PQ) pick the code that reconstructs the original vector most closely. Reconstruction is only a proxy for what retrieval needs: at a few bytes per document, a code trained directly on the retriever's ranking signal—a model-aware code—uses the budget better.

A single code is even more useful if it serves several byte budgets at once: when its short prefixes are each directly searchable, a deployment can set its efficiency-quality operating point without re-encoding the corpus. But training all prefixes under one objective makes the early bits a compromise across budgets—short codes improve while the full-width code degrades. Quantization to low-bit representation, such as binary codes, further sharpens the conflict, making optimization hard to reach an optimum. We introduce Matryoshka Hash Representations (MHR), a two-stage procedure that separates full-width training from prefix organization. MHR first learns a longer binary code, then freezes the model and trains additional zero-initialized residual code adaptors for directly searchable prefixes. Documents are stored at one bit per coordinate, while queries keep continuous logits like PQ to attain sufficient expressivity. We implement the search process with FAISS FastScan. Trained on MS MARCO and zero-shot transferred to seven BEIR datasets, MHR reaches .5561 NDCG@10 and .6535 Recall@100 at 32 bytes, against .5239 / .6426 for the best baseline of the same budget. The advantage is more pronounced in lower budgets. The same code also strengthens two common pipelines: shortlisting candidates for full-precision reranking, and pruning a low-storage graph index such as LEANN.

1 Introduction

Dense retrieval is the first stage of many text systems. It grounds retrieval-augmented generation (RAG), which answers a query by retrieving relevant documents and conditioning a language model on them rather than on its parameters alone (Lewis et al. 2020), and it also drives open-domain question answering (Karpukhin et al. 2020), large-scale semantic search (Douze et al. 2026), candidate genera-

tion in recommender systems (Covington, Adams, and Sargin 2016; Yi et al. 2019), and evidence retrieval for fact verification (Wadden et al. 2020). Each of these maps queries and documents into a shared vector space and reduces retrieval to nearest-neighbor search. Storing one full-precision vector per document then becomes the dominant index cost as the corpus grows. Vector compression techniques such as product quantization (PQ) and optimized product quantization (OPQ) reduce this cost by compressing vectors after encoder training (Jégou, Douze, and Schmid 2011; Ge et al. 2013). These quantizers are model-agnostic: they fit a code by minimizing reconstruction error and preserving the geometry of the original embeddings. The capacity is therefore mainly spent on variation that may not change the top retrieved documents. A model-aware code is instead trained on the retrieval model's own ranking signal through relevance ordering and anchoring on signals from the full-precision embeddings. Model-awareness is a property of the objective, not of which parameters move: a code learned on top of a frozen encoder still qualifies when its target is ranking rather than reconstruction. The distinction matters most at very small byte budgets, where every bit must be placed well, and it separates PQ/OPQ, ITQ (Gong and Lazebnik 2011; Gao and Long 2024), and LSH-style codes (Charikar 2002; Ji et al. 2012) from ranking-trained discrete retrievers.

Nested coding, introduced as Matryoshka representation learning (Kusupati et al. 2022), lets one stored representation serve every prefix, so a deployment can select its byte budget without maintaining an index per width. Nested codes also enable adaptive retrieval: a short prefix filters candidates before a longer one refines the shortlist (Rege et al. 2023). This is especially useful for binary hashing, where re-encoding requires model access and storage already approaches tens of bytes per document.

Yet nesting comes with a cost. Because each prefix must rank well on its own, the training objective places a ranking loss at every width, and the early bits fall under all of these losses at once. The short prefixes then improve at the full-width code's expense: a full-width-prefix trade-off. In real-valued MRL, this is softened by fitting the prefixes after training or one width at a time (Yoon et al. 2024; Zhang et al. 2025). Binary codes sharpen this trade-off. A continuous coordinate moves a little under each gradient step, but a stored bit is that coordinate's sign: a step either flips the bit or leaves it untouched, so the competing widths flip early bits back and forth instead of settling.

In this paper, we propose Matryoshka Hash Representations (MHR), which separates the two goals that direct nesting entangles. We specifically target the low-storage regime of 64–256 bits per document, as sought by recent indexing systems such as LEANN (Wang et al. 2025). MHR first adapts a pretrained dual encoder and learns a 256-bit hash code on MS MARCO with LoRA (Hu et al. 2022), compressing 768-dimensional floats by 96×96\times. It then freezes the encoder and trains two additional residual adaptors over the fixed logits. We adopt a full-width rank distillation that anchors the 256-bit code, while prefix ranking losses organize the adapted logits into nested 64-, 128-, and 256-bit codes that do not entangle with one another. For practical deployment, documents are binarized while queries keep the continuous logits to enhance expressivity.

The indexing of our MHR does not require a complex ANN topology like graphs (Subramanya et al. 2019; Malkov and Yashunin 2020). A flat index scores every code; IVF selects coarse lists and then ranks their MHR-coded postings. We further show that the same code supports graph indexes that prune neighbor expansions (Wang et al. 2025) and coarse-to-fine pipelines that shortlist candidates before reranking them with full-precision embeddings. This scoping lets us ask one core research question and test it across index types: can a binary code be trained to produce short, searchable prefixes without sacrificing full-width quality?

Our contributions are:

  • We identify a full-width-prefix trade-off in direct nested binary training and introduce a two-stage procedure that decouples the two goals. Our MHR framework first learns a strong full-width source code, then trains a residual adaptor cascade over the frozen code with prefix supervision, raw-query scoring matched to deployment, and a full-width rank anchor to obtain a series of code widths.

  • We align the compact score with deployment requirements, using hard document codes and continuous queries. We implement the search process with optimized FAISS-native kernels that outperform previous index types in efficiency.

  • In our extensive evaluation of seven BEIR datasets, MHR leads every compact baseline in macro NDCG@10 and Recall@100 at all budgets. We conduct detailed ablation studies that justify our design choices, and we show that MHR is a stronger drop-in replacement for PQ across common deployment settings.

References (54)