intermediate embedding-geometry 32 min read

Matryoshka Representations: Jointly Trained Nested Subspaces

One embedding whose every prefix is itself a usable representation — and the exact sense in which the linear version is just PCA

Overview & motivation

The PCA topic reduced an embedding cloud to kk dimensions by reading its covariance, and random projection reduced it with a fixed-width random map. Both produce a single target width: choose kk up front, and that is the representation you store. But a retriever often wants two widths at once — a short embedding to scan a billion candidates quickly, and a long one to score the survivors accurately — and re-encoding the whole corpus at a second width is exactly the cost we are trying to avoid.

Matryoshka Representation Learning answers this by training one embedding so that every prefix is itself a usable representation. Store the full 15361536-dimensional vector; retrieve with its first 9696 coordinates when speed matters and its full width when accuracy does, with no re-encoding, because the short representation is literally a slice of the long one. The name is the nesting dolls: z1:24z1:48z1:96z_{1:24} \subset z_{1:48} \subset z_{1:96} \subset \dots, each a complete representation at its own resolution.

Before the algebra, drag the prefix-width slider and watch what nesting buys:

050%100%24963841536nested (Matryoshka)rotated (no nesting)
Recall@10 using only the first m coordinates: the nested (Matryoshka / PCA-ordered) embedding (accent) degrades gracefully, while a random rotation of the same embedding (faint) — identical full-width distances — retrieves near chance until almost every dimension is kept. Nesting is trained, not free.
nested recall@10 at m = 96
93.7%
rotated (no nesting)
12.0%
kept / full
96 / 1536

The first panel is the point of the whole method: the nested embedding’s recall holds up as you shrink the prefix, while a random rotation of the same embedding — identical full-width geometry — collapses to near chance at small widths. The second shows the geometric reason in the linear case: the nested basis sits exactly on the Eckart–Young optimum at every width. The third is the payoff — shortlist on a cheap prefix, rerank at full width, and recover almost all the recall for a fraction of the cost.

What we cover

  1. The Matryoshka objective: a weighted sum of per-granularity losses.
  2. The linear theorem — the jointly optimal nested basis is exactly PCA — and why.
  3. Weight invariance: PCA is the optimum for any granularity weighting.
  4. Why nesting must be trained, not assumed.
  5. Adaptive funnel retrieval, and the nonlinear contrastive case.

The Matryoshka objective

Fix an embedding dimension dd and a set of granularities M={m1<m2<<d}\mathcal{M} = \{m_1 < m_2 < \dots < d\} — in practice the halving sequence {d,d/2,d/4,}\{d, d/2, d/4, \dots\}. A Matryoshka model produces, for each input, one vector zRdz \in \mathbb{R}^d, and is trained so that every prefix z1:mz_{1:m} is a good representation under a task loss \ell. The objective is a weighted sum over the granularities,

L=mMcm(z1:m),cm>0.\mathcal{L} = \sum_{m \in \mathcal{M}} c_m\, \ell\bigl(z_{1:m}\bigr), \qquad c_m > 0 .

The weights cmc_m trade off the granularities; the single shared trunk producing zz is what forces the prefixes to nest, because the same early coordinates must serve every width at once. To see the geometry cleanly we specialize \ell to linear reconstruction, where the answer is exact and familiar.


The linear theorem: Matryoshka is PCA

Let X~Rn×D\tilde{X} \in \mathbb{R}^{n \times D} be the centered data and let the representation be a linear projection onto an orthonormal basis VRD×dV \in \mathbb{R}^{D \times d}, so the prefix z1:mz_{1:m} uses the first mm columns V:mV_{:m} and reconstructs X~\tilde{X} through the projector Pm=V:mV:mP_m = V_{:m} V_{:m}^{\top}. Take \ell to be the squared reconstruction error (z1:m)=X~X~PmF2\ell(z_{1:m}) = \lVert \tilde{X} - \tilde{X} P_m \rVert_F^2.

Theorem 1 (The linear Matryoshka optimum is PCA).

For the linear squared-reconstruction loss, the basis minimizing the joint objective mMcmX~X~V:mV:mF2\sum_{m \in \mathcal{M}} c_m \lVert \tilde{X} - \tilde{X} V_{:m} V_{:m}^{\top}\rVert_F^2, for any positive weights cmc_m, is the PCA basis: the eigenvalue-ordered eigenvectors of the covariance X~X~\tilde{X}^{\top}\tilde{X}, equivalently the right singular vectors of X~\tilde{X}.

Proof.

Fix a single granularity mm. Minimizing X~X~V:mV:mF2\lVert \tilde{X} - \tilde{X} V_{:m}V_{:m}^{\top}\rVert_F^2 over rank-mm orthonormal projectors is the Eckart–Young–Mirsky problem, whose minimizer is the top-mm PCA subspace and whose minimum value is the tail i>mσi2\sum_{i>m}\sigma_i^2. Call that per-granularity minimum m\ell_m^\star.

The decisive structural fact is nestedness: the top-mm principal subspace is contained in the top-(m+1)(m+1) principal subspace, because both are spanned by a prefix of the same eigenvalue-ordered list v1,v2,v_1, v_2, \dots. So a single ordered basis Vpca=[v1  vd]V^{\mathrm{pca}} = [v_1\ \cdots\ v_d] has the property that its first mm columns span exactly the top-mm subspace, for every mm at once. Therefore VpcaV^{\mathrm{pca}} attains m\ell_m^\star at every granularity simultaneously, and

mcmm(Vpca)=mcmm    mcmm(W)\sum_{m} c_m\, \ell_m\bigl(V^{\mathrm{pca}}\bigr) = \sum_m c_m\, \ell_m^\star \;\le\; \sum_m c_m\, \ell_m(W)

for any orthonormal WW, since m(W)m\ell_m(W) \ge \ell_m^\star termwise and cm>0c_m > 0. This is exactly what a non-nested basis cannot do: a WW tuned to be optimal at one width has, in general, suboptimal prefixes at the others, because its top-mm subspaces are not nested. Only the eigenvalue-ordered basis is optimal at all widths at once, so it is the joint minimizer. \blacksquare

The reading to keep is that PCA already is a nested representation — its top-kk subspaces are the original matryoshka dolls — and the linear Matryoshka objective simply rediscovers it. What Matryoshka adds, in the nonlinear case below, is the same nested structure for a representation optimized against a retrieval loss rather than reconstruction. The companion harness confirms the theorem to the decimal: the PCA prefix reconstruction error equals the Eckart–Young rank-mm optimum at every granularity, while a random orthonormal basis is far worse.


The optimum does not depend on the weighting

A natural worry is that the granularity weights cmc_m should change the answer — favor short prefixes and surely the basis should tilt toward them. In the linear case they do not.

Proposition 1 (Weight invariance of the nested optimum).

Because the PCA basis attains the per-granularity optimum m\ell_m^\star at every mm, the joint loss it achieves equals mcmm\sum_m c_m \ell_m^\star, which is the smallest value the weighted sum can take for those weights. Hence the PCA basis is the joint minimizer for every choice of positive weights cmc_m — the optimal nested basis is weight-invariant.

Proof.

The quantity mcmm\sum_m c_m \ell_m^\star is a lower bound on mcmm(W)\sum_m c_m \ell_m(W) for any WW, because each term is bounded below by cmmc_m \ell_m^\star. The PCA basis meets this lower bound exactly (Theorem 1), so it is optimal, and the bound — and the minimizer that meets it — do not depend on the cmc_m beyond their positivity. The harness verifies this over randomly drawn weight vectors: the PCA joint loss equals the weighted sum of the per-mm optima every time. \blacksquare

This is a comforting sanity check and a warning at once. Comforting, because in the linear world you never have to tune the weights. A warning, because it is a symptom of the linear case being too easy: the weighting only starts to matter when the per-granularity optima cannot all be achieved by one basis, which is precisely the nonlinear regime where Matryoshka earns its keep.


Nesting must be trained

Here is the fact that keeps Matryoshka from being trivial, and it is best seen by trying to fake it. Take the nested embedding and apply a random rotation RO(d)R \in O(d) to produce z=Rzz' = Rz. Rotation is an isometry, so it preserves every full-width distance exactly — at m=dm = d the two embeddings retrieve identically. But zz' smears the information that lived in the early coordinates of zz across all dd of them, so its prefixes are arbitrary mixtures.

The retrieval consequence is stark. On the finance cloud, the nested embedding’s first 9696 coordinates retrieve at about 94%94\% recall@10; the rotated embedding’s first 9696 coordinates, with identical full-width geometry, retrieve at about 12%12\% — barely above chance. The “Prefix recall” panel shows the two curves: the nested one rises fast and plateaus near the intrinsic dimension of the cloud, while the rotated one crawls along the bottom until almost every coordinate is restored. Nesting is not a property an embedding has for free; it is a property the training must put there, by forcing the shared trunk to load the most useful directions into the earliest coordinates. In the linear case that loading is the eigenvalue ordering; in the nonlinear case it is what the multi-granularity loss trains.


Adaptive retrieval, and the nonlinear case

The practical reward is adaptive, or funnel, retrieval. Shortlist candidates cheaply using a short prefix, then rerank only the shortlist at full width. The first stage scores every document on mshortm_{\text{short}} dimensions and the second scores a handful on all dd, so the cost is a small fraction of an exhaustive full-width scan while the recall is nearly unchanged — the short prefix is good enough to retrieve the right candidates even when it is not good enough to order them, and the full-width rerank fixes the order. On the finance cloud, shortlisting on a 9696-dimensional prefix and reranking at 15361536 recovers 100%100\% of the exhaustive recall@10 at under 9%9\% of the scoring cost; the “Adaptive funnel” panel traces the recall-versus-cost frontier as the shortlist grows.

The version that matters in practice is nonlinear and contrastive. Real Matryoshka encoders replace the reconstruction loss \ell with a contrastive objective — pull a query toward its relevant document and push it from negatives — applied at each prefix width and summed, mcmcontrastive(z1:m)\sum_m c_m\,\ell_{\text{contrastive}}(z_{1:m}). That contrastive loss is the subject of a forthcoming neural-retrieval topic; here we only note its role. With it, the headline empirical results hold — a nonlinear Matryoshka embedding matches a full-width model’s accuracy at a fraction of the dimensions, and the jointly trained prefixes beat heads trained independently at each width. We are careful to call these empirical: there is no general theorem that joint training dominates independent heads, and the clean optimality we proved is special to the linear case. What carries over is the geometry — coordinates ordered by usefulness, prefixes that nest — which the linear theorem shows is not magic but the natural generalization of PCA.


Finance case study


Honest caveats


Implementation

The companion module matryoshka_nested_representations.py owns every number this page and the laboratory cite. Its seven assertions encode the claims in order: the linear Matryoshka optimum is PCA (the prefix reconstruction equals the Eckart–Young rank-mm optimum at every granularity, to the decimal); weight invariance (the PCA joint loss equals the weighted sum of per-mm optima for random weightings); prefix-recall monotonicity; the trained-nesting gap (a random rotation preserves full-width recall but collapses the prefixes); funnel retrieval (near-full recall at a fraction of the cost); a scikit-learn cross-check of the nested basis; and the finance headline. A subtlety worth flagging, since it bit during development: the synthetic corpus must have more points than dimensions for PCA to return a full-width 15361536-dimensional basis, so the finance cloud uses n=2000n = 2000. Both the module and the narrative notebook run in about five seconds and must exit cleanly before the topic ships.

Connections

  • this topic generalizes PCA's central optimality from variance to an arbitrary task loss: PCA's nested top-k subspaces, each the Eckart-Young rank-k optimum proved there, are exactly the linear Matryoshka optimum, so the nested-subspace structure that PCA gives for reconstruction is the structure Matryoshka trains for retrieval, and in the linear case the two coincide pca-dimensionality-reduction

References & Further Reading