[FEA] IVF-PQ transform: skip the rotation GEMM when the rotation matrix is the identity
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 40/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- cpp
- Ambito
- backend, performance
Direzione di ricerca
Start by reading ivf_pq::transform and extend, then inspect index_impl and the existing lazy caches such as centers_int8_ and rotation_matrix_int8_. Trace where the rotation matrix is written and how owning and view implementations differ before adding the identity check and bypassing the GEMM. Done means identity matrices skip the GEMM with bit-identical output, while non-identity matrices retain current behavior and the cache remains correct across writes and views.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Is it related to a problem?
ivf_pq::transform (and extend) always applies rotation_matrix via GEMM, even when force_random_rotation=false
and dim == rot_dim, i.e. when the rotation is exactly the identity. In that case the GEMM computes x * I = x: it changes
nothing and is pure overhead. Skipping it whenever the identity is known is a free win: bit-identical output, no accuracy
or memory trade-off, and strictly less work. The only cost is a small cached value on the index (see Solution); no
serialization or public API change is needed.
How large the win is
The skip is worthwhile at any size; the analysis below only quantifies it and shows why it grows with dimension.
Per row, with D = dim = rot_dim, K = n_lists, pq_dim sub-vectors of pq_len = D / pq_dim, and 2^b codewords:
| Step | FLOPs/row | Scaling in D |
|---|---|---|
| Rotation GEMM | 2 D^2 | quadratic |
| Coarse assignment, exact | 2 K D | linear |
| Coarse assignment, approximate (grouped, M ~ sqrt(K) groups, p probes) | ~2 D sqrt(K) (1 + p) | linear |
| PQ encode | 2 pq_dim 2^b pq_len = 2^(b+1) D | linear |
Every other step is linear in D, so the rotation's share rises with D. It exceeds the PQ-encode cost once D > 2^b,
exceeds exact assignment once D > K, and exceeds approximate assignment once D > ~sqrt(K) (1 + p). Modern embedding
models (1-4K dims) are already past the first and, with approximate assignment, the last of these thresholds, and
dimensions keep growing. The rotation matrix is also D^2 floats, so it is a growing memory-traffic cost as well.
Note that the GEMM may run at a lower effective throughput than the assignment kernels (e.g. plain FP32 vs TF32 tensor cores),
so its share of time can exceed its share of FLOPs.
Solution
Have the index lazily determine, once, whether its rotation matrix is exactly the identity, and cache the result in a
non-serialized member of index_impl (next to the existing lazily initialized, never-serialized members such as
centers_int8_ / rotation_matrix_int8_; e.g. a mutable std::optional<bool> rotation_is_identity_). The first
transform/extend call checks the D x D matrix on the device once; later calls read the cached value. When it is set,
transform/extend bypass the GEMM and use the input directly as the rotated data. Output is bit-identical.
Why check the matrix rather than record what build was asked to do: the identity is a property of the matrix, and
the build-from-components overloads accept a caller-supplied rotation_matrix, so force_random_rotation and
dim == rot_dim alone do not tell whether it is the identity. A check of the actual matrix is correct for a freshly
built index, a component-built index and a deserialized index alike, so there is no serialization format change and
older index files benefit without versioning.
Points to settle in review:
- Invalidation: the cache must be reset wherever the matrix can be written, and
view_implholds a view onto
caller-owned memory whose contents can change. A conservative start is to cache only forowning_impland re-check
views on each call. - The lazy fill happens in a
constmethod, so concurrent first calls on one index would race. The existing*_int8_
caches share this; astd::once_flagwould make it safe if preferred.
Example measurements (anecdotal, one setup: H100, CUDA 13.4, 1M rows, D = 2048, K = 32768, 256 sub-vectors, 8-bit codes):
- Exact (unfused) assignment: rotation is ~6% of FLOPs; skipping it cut transform_s from 0.986 to 0.85 (-14%).
- Grouped approximate assignment (see #3007): rotation is ~52% of FLOPs; skipping it cut transform_s from
~0.63 to ~0.42 (-32%; two runs each, run-to-run spread ~10%).
The saving per row is the same cost in both cases (~0.14-0.20 s per 1M rows here); it is a larger fraction when the
rest of the transform is cheaper or D is larger.
Alternatives
Callers could avoid rotation themselves, but that needs a different API path. The change is also beneficial to stock
cuvsIvfPqTransform with no API change.
Additional context
We have a working patch and will open a PR. The prototype checks the matrix on every call and gates the skip on an env
var for A/B; the upstream version would be always-on with the cached member (no knob, no per-call check).
- Lingua principale
- Cuda
- Stelle
- 902
- Fork
- 267
- Merge medio
- 2g 21h
- PR unite (30g)
- 57
Preparare l'ambiente
- Include un Dockerfile o un file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di NVIDIA/cuvs
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 1 giorno
-
[BUG] CAGRA multi-CTA and multi-kernel search over-allocate the hashmap they useForse già presa @mc8max l’ha presa 12 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
NVIDIA/cuvs#2856 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
[BUG] cuVS Bench Faiss CAGRA b_ratio reads the wrong JSON keyForse già presa @arcusbuilds l’ha presa 55 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
I maintainer di solito rispondono entro 1 giorno
-
doc
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Upgrade faiss to v1.16.0Forse già presa @aamijar l’ha presa 8 giorni fa. Apertafaiss improvement
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 86/100
I maintainer di solito rispondono entro 1 giorno
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
I maintainer di solito rispondono entro 1 giorno
-
aws-documentation-mcp-server: empty spanning headers shift table column labelsForse già presa @pradeep-ramola l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 2 giorni
-
bug untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
opensearch-project/sql#5990 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
GustavoBaranda/chatflow-agent#238 ·
I maintainer di solito rispondono entro 1 giorno