Why Unnormalized Prefix Slices Corrupt Vector Search
Matryoshka Representation Learning (MRL) trains nested prefix slices $v_{1:d}$ (d in {256, 512, 768, 1536, 3072}) simultaneously using multi-scale contrastive loss. At full width (D = 3072), gemini-embedding-001 projects vectors onto the unit hypersphere:
$$|v_{1:3072}|2 = \sqrt{\sum{i=1}^{3072} v_i^2} = 1.0$$
When you pass output_dimensionality=768 or output_dimensionality=1536 to the Gemini / Vertex AI embedContent endpoint, the API truncates indices $d+1 \dots 3072$ without dividing by the truncated prefix norm. Because different documents pack different fractions of variance into dimensions 1..768 versus 769..3072, the truncated norm $|v_{1:768}|_2$ fluctuates across chunks (empirically 0.68 to 0.94):
- Inner-Product (
<#>,dotproduct) failure: $q \cdot v = |q|_2 |v|_2 \cos(\theta)$. A passage with $|v|_2 = 0.93$ and $\cos(\theta) = 0.72$ (0.6696) outranks a far better passage with $|v|_2 = 0.74$ and $\cos(\theta) = 0.88$ (0.6512). - Euclidean (
<->,L2) failure: $|q - v|_2^2 = |q|_2^2 + |v|_2^2 - 2|q|_2|v|_2\cos(\theta)$ only preserves cosine order when $|v|_2 \equiv 1$. - Quantization clip drift: Scalar
INT8calibration ranges assume bounded unit-sphere coordinates.
Runnable Fix: Python, Go, and PostgreSQL L2 Enforcement
1. Python (google-genai SDK) + NumPy Batch Renormalizer
import numpy as np
from google import genai
from google.genai import types
client = genai.Client()
def embed_gemini_mrl_safe(
texts: list[str],
dim: int = 1536,
task_type: str = "RETRIEVAL_DOCUMENT",
) -> np.ndarray:
resp = client.models.embed_content(
model="gemini-embedding-001",
contents=texts,
config=types.EmbedContentConfig(
task_type=task_type,
output_dimensionality=dim,
),
)
mat = np.asarray([e.values for e in resp.embeddings], dtype=np.float32)
# Mandatory L2 renormalization for any dim < 3072
norms = np.linalg.norm(mat, axis=1, keepdims=True)
mat /= np.maximum(norms, 1e-12)
return mat
2. Go Zero-Allocation SIMD-Friendly L2 Normalizer
package vecnorm
import "math"
// NormalizeL2InPlace scales v to unit length (||v||_2 == 1.0).
func NormalizeL2InPlace(v []float32) {
var sumSq float64
for _, x := range v {
sumSq += float64(x) * float64(x)
}
if sumSq <= 1e-24 {
return
}
invNorm := float32(1.0 / math.Sqrt(sumSq))
for i := range v {
v[i] *= invNorm
}
}
3. PostgreSQL In-Place Repair for Already-Ingested pgvector Tables
If you already wrote unnormalized 768d or 1536d Gemini vectors into Postgres, pgvector 0.7.0+ ships l2_normalize(vector) and l2_normalize(halfvec):
-- Audit how badly your existing table drifted off the unit sphere
SELECT
MIN(vector_norm(embedding)) AS min_l2_norm,
AVG(vector_norm(embedding)) AS avg_l2_norm,
MAX(vector_norm(embedding)) AS max_l2_norm
FROM gemini_chunks;
-- Repair in-place (batch by PK window on tables > 1M rows to avoid WAL bloat)
UPDATE gemini_chunks
SET embedding = l2_normalize(embedding)
WHERE ABS(vector_norm(embedding) - 1.0) > 1e-4;
MRL Dimensionality vs Recall & RAM Tradeoff (10M Chunks)
| Model & MRL Slice | Server-Side Unit Norm? | Dot-Product nDCG@10 (Unpatched vs Normalized) |
halfvec HNSW RAM (10M Rows) |
Price / 1M Tokens |
|---|---|---|---|---|
gemini-embedding-001 (3072d) |
Yes (||v||_2 = 1.0) |
0.678 / 0.678 |
~72.2 GB |
$0.15 |
gemini-embedding-001 (1536d) |
NO (0.76..0.95 norm) |
0.591 -> 0.674 (+8.3 pts fixed) |
~37.9 GB |
$0.15 |
gemini-embedding-001 (768d) |
NO (0.68..0.91 norm) |
0.554 -> 0.665 (+11.1 pts fixed) |
~20.8 GB |
$0.15 |
OpenAI text-embedding-3-large (1536d) |
Yes (||v||_2 = 1.0) |
0.640 / 0.640 |
~37.9 GB |
$0.13 |
Voxell Forge Turbo (1024d) |
Yes (||v||_2 = 1.0) |
Always unit-normalized | ~26.5 GB |
$0 forever |
Voxell Forge Pro (256..2560d + C_corpus) |
Yes (||v||_2 = 1.0) |
0.6652 Stage-1 (0.8849 w/ Max) |
~60.8 GB (2560d) / ~26.5 GB (1024d) |
$0.25 |
Upgrade Path: Guaranteed Unit-Norm MRL + Corpus Conditioning
Renormalizing fixes dot-product geometry, yet gemini-embedding-001 still silently truncates inputs at 2,048 tokens (Guide 03) and penalizes mismatched task prefixes (Guide 04). Voxell Forge /v1/embeddings guarantees unit-norm vectors at every MRL width (256..2560d) via a one-line base_url = "https://api.voxell.ai/v1" swap, and pairs dense recall (99.04% Recall@20) with Stage-2 Max cross-encoder reranking (0.8849 nDCG@10). Test it live with zero signup:
curl -sS -X POST https://api.voxell.ai/api/v1/agent/bakeoff \
-H "Content-Type: application/json" \
-d '{
"documents": [
{"name": "gemini-mrl.md", "text": "gemini-embedding-001 slices at 768 and 1536 dimensions require client-side L2 normalization before dot-product indexing."},
{"name": "forge-mrl.md", "text": "Voxell Forge Pro normalizes every Matryoshka slice from 256d to 2560d server-side on the unit hypersphere."}
],
"queries": ["Which embedding API requires client-side L2 normalization below 3072 dimensions?"]
}'
Model your exact memory savings in the Vector RAM & TCO Calculator, compare full pipelines on /compare/, or start free in the Forge Console (v4.0 Pricing).