#322Matryoshka Embedding TruncationMediumML System DesignEmbeddingsAsked atOpenAI · Cohere · Google
Matryoshka Embedding Truncation
Background
Matryoshka Representation Learning (MRL) trains an embedding model such that any prefix of the output vector is a valid embedding in its own right. At serving time, truncating from to gives a × smaller index with only a small recall hit — and the embeddings still need to be L2-renormalised after slicing so cosine similarity is meaningful.
Problem statement
Implement truncate_and_renorm(embs, dim) returning (n, dim) L2-normalised embeddings:
- Slice the first
dimcolumns. - Divide each row by its (post-slice) L2 norm.
Input
embs—np.ndarrayshape , the original embeddings (need not be normalised).dim—intin , the target dimension.
Output
Returns np.ndarray shape with each row's L2 norm equal to .
Examples
Example 1 — shape
Input: embs shape=(5, 1024), dim=128
Output: shape=(5, 128)
Example 2 — re-normalised
After truncation, ||row||_2 should be 1.0 ± 1e-9 for every row.
Example 3 — dim == d is the full-vector L2-normalised case
Input: embs=[[3.0, 4.0]], dim=2
Output: [[0.6, 0.8]]
Constraints
dimmust be in[1, embs.shape[1]]. ValueError otherwise.- Add
1e-12epsilon to the denominator to avoid division-by-zero on zero rows. - Original
embsis not mutated.
Notes
- Recall hit. Truncating typically loses ~2pp recall@10 on MRL-trained embeddings; truncating an embedding NOT trained for MRL can lose 10pp+.
- Combine with quantisation. MRL truncation + binary or scalar quantisation gives 30–100× total index reduction — that's the production stack at most vector DB providers.
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Reference 3-4-5 row truncated to dim=2
- •Worked example with a three-dim row
- •Sample two-row batch truncated to dim=2