MIPS -> L2 Reduction
Background
Maximum Inner-Product Search (MIPS) isn't directly compatible with L2-nearest-neighbour structures because MIPS isn't a metric — there's no triangle inequality on dot products. MIPS-to-L2 reduction (Bachrach et al. 2014) augments each vector with one extra dimension chosen so that on the unit sphere MIPS-argmax equals L2-argmin. After the augmentation, any off-the-shelf L2 ANN structure (HNSW, IVF, KD-tree) handles MIPS correctly.
Problem statement
Implement two functions:
mips_augment(db_vecs)— return(augmented_db, M)where and each augmented row is (unit norm).mips_augment_query(q)— return (unit norm too).
In the augmented space, (MIPS) equals (L2-NN).
Input
db_vecs—np.ndarrayshape .q— 1-Dnp.ndarrayof length .
Output
mips_augmentreturns(augmented_db: (n, d+1), M: float).mips_augment_queryreturnsnp.ndarrayof length .
Examples
Example 1 — augmented shape
Input: db_vecs shape=(5, 4)
Output: augmented shape=(5, 5)
Example 2 — augmented db rows have unit norm
Input: any non-zero db
Output: every row's L2 norm == 1.0 ± 1e-6
Example 3 — MIPS = L2 in augmented space
Input: random db, query
Output: argmax(db @ q) == argmin(||aug_db - aug_q||^2)
Constraints
- Handle
M = 0(all-zero db) by settingM = 1. - Use
np.maximum(0, ...)before the square root to avoid floating-point issues that produce tiny negatives. - Augmented vectors have exactly one extra dimension.
Notes
- Why query has zero in the extra slot. The reduction relies on ; the extra dimension contributes nothing because .
- Asymmetric variant. Other reductions augment query and db differently (asymmetric LSH) — same effect, slightly different geometry.
▶ 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.
- •Example: augmented db rows have unit norm
- •Reference: query is normalized with a zero appended
- •Sample: M equals the max row norm