[FEA] IVF-PQ transform: skip the rotation GEMM when the rotation matrix is the identity
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 40/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- cpp
- Área
- backend, performance
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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).
- Lenguaje dominante
- Cuda
- Estrellas
- 902
- Forks
- 267
- Merge medio
- 2 d 21 h
- PR fusionados (30 d)
- 57
Preparar el entorno
- Incluye un Dockerfile o un archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de NVIDIA/cuvs
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
Los mantenedores suelen responder en 1 día
-
[BUG] CAGRA multi-CTA and multi-kernel search over-allocate the hashmap they usePosiblemente ocupada @mc8max la tomó hace 12 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
NVIDIA/cuvs#2856 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
-
[BUG] cuVS Bench Faiss CAGRA b_ratio reads the wrong JSON keyPosiblemente ocupada @arcusbuilds la tomó hace 55 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
Los mantenedores suelen responder en 1 día
-
doc
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
Upgrade faiss to v1.16.0Posiblemente ocupada @aamijar la tomó hace 8 días. Abiertofaiss improvement
Dificultad 1/5 Menos de una hora Aptitud para principiantes 86/100
Los mantenedores suelen responder en 1 día
Todos los issues de NVIDIA/cuvs
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
vllm-project/vllm#70030 ·
Los mantenedores suelen responder en 1 día
-
help wanted small extra
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 1 día
-
t: bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 3 días
-
Retry an empty final answer when Gemini reports MALFORMED_RESPONSE (gemini-4-hydrogen)Posiblemente ocupada @waleedkadous la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
iaser-ai/ansari#444 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
Los mantenedores suelen responder en 1 día