Skip to content

Matryoshka Representation Learning

Skip →Late Chunking: contextual chunk embeddings with long-context encoders
main track #2corefundamentalpaperrubric 11/122022 · ~35 min

Aditya Kusupati, Gantavya Bhatt, Aniket Rege, … Ali Farhadi

Status

TL;DR. Matryoshka Representation Learning (MRL) trains one embedding so that its first 8, 16, 32, … coordinates are each a good embedding on their own. It does this by adding one loss per nested prefix, with no other change to the model. A single model then serves every size, and retrieval can shortlist with a small prefix and rerank with the full vector. On ImageNet this is about 128× cheaper in FLOPs at the same accuracy.

Why it matters now. Almost every 2024–26 text embedder ships “Matryoshka dimensions”: Nomic v2, Qwen3 Embedding, Gemini Embedding, EmbeddingGemma 2, Cohere Embed 5 and the static Bekko models. For an IR system, the dimension sets index memory and first-stage latency, which often cost more than the encoder. For search agents that call retrieval many times per episode over large corpora, MRL plus quantisation is the main way to keep a dense index affordable. The original paper, however, tests retrieval only on images, so it is worth knowing exactly what it shows.

Matryoshka representation learning and adaptive retrieval Left: one 2048-dimensional vector z. Its first 8, 16, 32 and so on up to 2048 coordinates are each fed to their own linear classifier and loss, and the losses are summed with weights set to one; in the efficient variant the classifiers share one weight matrix. Because every prefix is trained, any prefix is a usable embedding; at 8 dimensions 1-nearest-neighbour accuracy on ImageNet-1K is 62.2 percent versus 58.9 for an independently trained 8-dimensional model. Right: adaptive retrieval on ImageNet-1K scores all of about 1.3 million images with the first 16 coordinates, keeps 200, and rescores them with all 2048 coordinates. This matches single-shot 2048-dimensional retrieval at about 128 times fewer FLOPs and about 14 times lower wall-clock time with HNSW. A funnel variant halves the shortlist and doubles the dimension at each step. Matryoshka: every prefix of the vector is trained as an embedding Training: one loss per nested prefix z[1:8] → L(8) z[1:64] → L(64) z[1:512] → L(512) z[1:2048] → L(2048) M = {8, 16, 32, …, 2048}: 9 nested sizes loss = Σ over m of c_m · CE(W⁽ᵐ⁾ z[1:m], y) c_m = 1 for all m; MRL-E shares one W 8-d 1-NN on ImageNet-1K: 62.2 (MRL), 58.9 (own 8-d model), 19.1 (SVD) Adaptive retrieval on ImageNet-1K ~1.3M images: score with z[1:16] 16 × 1.3M ≈ 21 MFLOPs → keep 200 rescore 200 with z[1:2048] 200 × 2048 ≈ 0.4 MFLOPs → top 10 top 10 ≈ 2048-d single-shot brute force at 2048 dims: 2.6 GFLOPs → ~128× fewer FLOPs, ~14× faster (HNSW) Funnel: 200→100→50→25→10 candidates with 16→32→64→128→256→2048 dims
Original sketch. Left: nested prefixes of one vector each get their own loss. Right: adaptive retrieval shortlists with the 16-d prefix and reranks 200 candidates with all 2,048 dims.

Let F(x;θF)∈RdF(x;\theta_F) \in \mathbb{R}^d be the representation and M\mathcal{M} a set of nested sizes, for example 8,16,…,20488, 16, \ldots, 2048 for ResNet50 (∣M∣≤⌊log⁡d⌋|\mathcal{M}| \le \lfloor \log d \rfloor). Each size mm gets its own linear head W(m)W^{(m)} applied to the first mm coordinates, and the losses are summed:

min⁡{W(m)}, θF  1N∑i=1N∑m∈Mcm L ⁣(W(m)⋅F(xi;θF)1:m; yi)\min_{\{W^{(m)}\},\,\theta_F}\; \frac{1}{N}\sum_{i=1}^{N}\sum_{m \in \mathcal{M}} c_m \,\mathcal{L}\!\left(W^{(m)} \cdot F(x_i;\theta_F)_{1:m};\, y_i\right)

All cm=1c_m = 1 in the paper. Because coordinate jj is used by every prefix of length at least jj, the first coordinates carry the coarse information and later ones add detail. Sizes between the trained ones (say 100 dims) also work, by interpolation.

MRL-E ties the heads: W(m)W^{(m)} is the first mm columns of one matrix. This halves classifier memory, and it is what MRL reduces to for masked language modelling, where the output layer is already tied to the input embeddings. For contrastive learning (ALIGN), both towers are truncated to the same mm and normalised separately at each size.

Adaptive retrieval. Shortlist KK candidates with a cheap DsD_s-dim prefix, then rerank them with DrD_r dims. The funnel variant halves the shortlist and doubles the dimension at each step until the full vector.

Slide from 2 to 32 dims and watch the relevant document climb as finer coordinates are added, while storage per vector grows linearly.

Truncate a Matryoshka embedding

Toy 32-d vectors (not from a real model). Earlier coordinates have larger magnitude, as in an MRL embedding. The relevant document agrees with the query in every coordinate, but noisily. Each distractor matches the query closely on a prefix (2, 4, 8 or 16 dims), then diverges. Cosine is computed on the first m dims only.

Query: “cheap train tickets to Paris”

Documents ranked by cosine similarity on the first m dimensions
rankdocumentcosinerank at 32-d

  • per vector: fp32
  • int8
  • binary
  • 1M docs: fp32 , int8 , binary

Setups: ResNet50 on ImageNet-1K (2,048 dims), ViT-B/16 on JFT-300M, ALIGN (ViT-B/16 + BERT) and BERT-Base MLM (768 dims, sizes 12…768). Baselines are fixed-feature models trained separately for each size (FF), SVD, slimmable networks, random features and JL projections.

ImageNet-1K, 1-NN accuracy (%)8 dims16 dims2,048 dims
MRL62.1967.9170.97
FF (separate model per size)58.9366.7771.19
SVD19.14——
  • Linear classification (Table 1): MRL at 8 dims gets 66.63% top-1 versus 65.29% for FF and 56.66% for MRL-E; at 2,048 dims, 76.80% versus 76.87%.
  • Retrieval (§4.3): mAP@10 is up to 3% better than FF at the same size. Exact 2,048-d search costs 2.6 GFLOPs per query on ImageNet-1K and 8.6 on ImageNet-4K. The funnel (200→100→50→25→10 candidates at 16→32→64→128→256→2,048 dims) is as accurate as single-shot 2,048-d search at ~128× lower cost.
  • BERT MLM (Table 7): within about 0.6 percentage points of FF at every tested size (0.12–0.54); at 768 dimensions, 65.00 versus 65.54.
  • Robustness and analysis (§5): +0.6 points on ImageNet-A (20% relative). Perfect routing of each image to its best size would add up to 4.6 points. Low dimensions encode the coarse class hierarchy: the “garment” superclass gains up to 11% going from 8 to 16 dims.
  • No text retrieval evaluation. Retrieval is class-level image retrieval; the only language result is MLM accuracy. Text embedders adopted MRL on the strength of their own, mostly unpublished, truncation curves.
  • Fixed, untuned design. Equal loss weights, one loss type for all sizes, and hand-chosen DsD_s and KK. The authors list optimal weights, different losses per fidelity and learned search structures as future work.
  • FLOPs overstate the speedup. About 128× fewer FLOPs gave about 14× in wall-clock time with HNSW.
  • Fine detail goes first. Truncation keeps coarse structure. For IR, the dropped detail is what separates a relevant passage from a near-duplicate hard negative.
  • No venue is given on the arXiv record, so none is listed here.

Questions to think through

These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.

  1. Derive / compute

    MRL adaptive retrieval on ImageNet-1K shortlists K = 200 candidates from a database of about 1.28M images with the first Ds = 16 dimensions, then reranks them with all Dr = 2,048. The paper counts exact search as N·d multiply-adds and reports 2.6 GFLOPs per query at 2,048 dims. (a) Compute the FLOPs of the two-stage search and the real speedup, and explain why the paper quotes "~128×". (b) The funnel reranks 200→100→50→25→10 candidates at 32, 64, 128, 256 and then 2,048 dims after the same 16-d shortlist. Compute its cost. (c) For 10M text passages in float32, compare index memory for a full 768-d vector, a 64-d MRL prefix, and the 64-d prefix plus 768-d vectors fetched only for reranking.

    What a strong answer covers
    • (a) Shortlist: 1.28M × 16 ≈ 20.5 MFLOPs. Rerank: 200 × 2,048 ≈ 0.41 MFLOPs (the paper's 400 KFLOPs). Total ≈ 20.9 MFLOPs versus 1.28M × 2,048 ≈ 2.62 GFLOPs, a ≈ 125× saving. The quoted ~128× is the ratio 2,048 / 16, which ignores the rerank.
    • (b) Rerank steps: 200 × 32, 100 × 64, 50 × 128 and 25 × 256 are 6.4K FLOPs each (25.6K total), plus 10 × 2,048 ≈ 20.5K. About 46K FLOPs in total, under 0.25% of the 20.5 MFLOP shortlist. The cascade is essentially free; the first pass dominates.
    • Brute-force cost scales linearly with the shortlist dimension, so the speedup is bounded by d / Ds. Going from Ds = 16 to 64, as ImageNet-4K needs, cuts the theoretical speedup to ~32×.
    • (c) 768 × 4 B × 10M ≈ 30.7 GB; 64 × 4 B × 10M ≈ 2.6 GB (12× smaller). The full vectors can stay on disk or in cheap storage and be read for the K candidates only, so RAM holds 2.6 GB.
    • Wall-clock gains are smaller than FLOP gains (the paper measures ~14× with HNSW, against ~128× theoretical), because memory access, graph traversal and fixed overheads do not shrink with d.
  2. Mechanism

    Explain why an ordinary embedding truncated to its first 16 coordinates is nearly useless (SVD 19.1%, random features 2.4% at 8 dims on 1-NN), while an MRL prefix of the same size works (62.2%). Cover what the summed loss does to the gradient of each coordinate, why only about log d nesting sizes are enough for intermediate sizes to work, and what MRL-E changes and why it is the default for masked language modelling.

    What a strong answer covers
    • A normal loss is invariant to rotating the representation, so information is spread over all coordinates in no particular order. Nothing makes the first 16 special, and truncating keeps an arbitrary 16-d projection.
    • In MRL the loss is the sum over m in M of c_m · L(W⁽ᵐ⁾ z[1:m]). Coordinate j receives gradient from every term with m ≥ j, so early coordinates get the most supervision and must carry the most useful information on their own. This breaks the rotation symmetry and produces a coarse-to-fine ordering.
    • With M = 8, 16, …, 2,048 (|M| ≤ log d), each added block refines the previous one. The paper reports that sizes in between, which are never trained directly, interpolate accurately, so the log spacing covers all sizes without a loss per dimension.
    • MRL-E shares one classifier, W⁽ᵐ⁾ = W[:, 1:m]. Separate heads would need 8 + 16 + … + 2,048 ≈ 2 × 2,048 columns per class, so tying halves classifier memory. In MLM the output matrix is already tied to the input embedding table, so MRL naturally becomes MRL-E.
    • For contrastive training (ALIGN, and later text embedders), both towers are truncated to the same m and normalised per m, so every prefix pair is a valid similarity.
  3. Critique

    Text-embedding releases (Nomic v2, Qwen3, Gemini, EmbeddingGemma, Cohere Embed 5) cite MRL as the reason their vectors can be truncated. Using what the paper actually evaluates (ResNet50/ViT/ALIGN image tasks, class-level retrieval, a BERT MLM run), critique the claim that MRL "gives free truncation" for text retrieval, and propose one experiment that could falsify it.

    What a strong answer covers
    • All retrieval experiments are image retrieval on ImageNet-1K/4K, where a hit means the same class, measured by top-1 and mAP@10. That is far coarser than passage relevance, where two passages on the same topic can differ in whether they answer the query.
    • The only text result is BERT MLM accuracy, where MRL is 0.12–0.54 points below independently trained models at every size (65.00 vs 65.54 at 768). There is no text retrieval evaluation.
    • MRL is not quite free even in vision: at full size its 1-NN accuracy is 70.97 vs 71.19 for FF, and linear top-1 is 76.80 vs 76.87. The gains are at low dimensions.
    • Class-level structure lives in the first coordinates (the superclass analysis); fine-grained distinctions, which matter for hard negatives and long-tail queries, are what truncation drops first.
    • Falsifying test: train the same text embedder with and without MRL and report nDCG@10 on BEIR, BRIGHT and long-tail or multilingual sets at 768, 256, 128 and 64 dims, including the full-size cost. If losses at 64–128 dims are large on hard or reasoning-heavy sets but not on MS MARCO-like sets, "free truncation" depends on the task.
  4. What if…

    The paper keeps c_m = 1 for every nesting size, starts at 8 dims with log-spaced sizes, and shortlists K = 200. Its §5.1 ablations test boosting the low-dimension losses (Table 27), the smallest size and the spacing (Table 29), shortlist dimension and length (Tables 30–31), and adding MRL to a pretrained model by partial fine-tuning (Table 26). What does each ablation tell a practitioner adding MRL to an existing text embedder?

    What a strong answer covers
    • Loss weights: boosting the 8-d (and 16-d) terms improves the low-dimension accuracy, and the paper reports this without accuracy loss at larger sizes. Equal weights are a safe default; boost the size you will actually serve.
    • Granularity: log spacing beats uniform spacing, because accuracy saturates as dimension grows, so most of the change happens at small sizes. Extremely small starting sizes are avoided because they are poor on their own.
    • Shortlist: retrieval saturates after a certain shortlist dimension and length, and where depends on dataset difficulty. Beyond that point a larger K or Ds only adds cost, so both should be tuned on the target corpus.
    • Fine-tuning: partial fine-tuning of a pretrained FF-2048 model (with added non-linear layers) induces nesting cheaply. It stays within 1.5 points of end-to-end MRL above 64 dims, with larger gaps below. This is how text models adopt MRL, as a loss wrapper during contrastive fine-tuning.
    • None of these ablations are on text retrieval, so each should be re-checked on the target corpus.
  5. Design

    You are building the dense index for an RL-trained deep-research agent over 100M web passages, on one server with 256 GB of RAM. Your embedder is MRL-trained at 1,024 dims. The agent issues about 20 search calls per episode, and you care about recall@100 for the reranker. Design the index using MRL prefixes, quantisation and adaptive retrieval, and say how you would choose the shortlist dimension and K.

    What a strong answer covers
    • Budget: 1,024-d float32 is 4 KB per passage, 410 GB for 100M, which does not fit. A 128-d int8 prefix is 128 B per passage, 12.8 GB, which fits with room for an HNSW graph. A 256-d binary first pass would be 3.2 GB.
    • Plan: ANN over a small quantised prefix to get K candidates, then rescore them with the full 1,024-d float vectors read from SSD (100 × 4 KB = 400 KB per query), then the cross-encoder reranker.
    • Choosing Ds and K: sweep Ds (64, 128, 256) and K (100–1,000) on agent queries with relevance labels, and choose the cheapest point where recall@100 after rescoring is within a set tolerance of exact 1,024-d search. Harder corpora need larger Ds (ImageNet-4K needed 64 rather than 16).
    • Agent-specific check: agent queries change during training. Re-measure recall at the chosen Ds on queries from the current policy, because a cheap first stage can turn into the bottleneck as queries get more specific.
  6. Research idea

    The paper reports that if each image could be routed to the best dimension, accuracy would rise by up to 4.6%, and that a simple confidence cascade already reaches 76.30% top-1 with an expected ~37 dims. Propose a study of query-adaptive dimensionality for text retrieval: which queries need more dimensions, and can a router learn it?

    What a strong answer covers
    • Analogy: an easy navigational query may be solved at 64 dims; a multi-hop or reasoning query may need all of them. The vision oracle suggests the distribution of needed dims is wide.
    • Measure: for each query in BEIR, BRIGHT and agent logs, find the smallest prefix whose top-k matches full-size retrieval (or the oracle best size), and relate it to query length, specificity and domain.
    • Router: predict the shortlist dimension from query features or the 64-d embedding itself (score margin between top candidates), as in the paper's cascade; train it to minimise expected FLOPs at fixed recall.
    • Training side: the paper's future work suggests different losses per fidelity. For text, one could use in-batch negatives for small prefixes and hard negatives only for the full vector, and test whether that improves the cascade.
    • Report cost-recall curves, not single points, and include the wall-clock effect, since FLOP savings did not fully carry over in the paper.

Done with this page?

Status