[FEA] IVF-PQ transform: skip the rotation GEMM when the rotation matrix is the identity

オープン
#3,008 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
40/100
issue の種類
機能追加
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
cpp

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

feature request

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_impl holds a view onto
    caller-owned memory whose contents can change. A conservative start is to cache only for owning_impl and re-check
    views on each call.
  • The lazy fill happens in a const method, so concurrent first calls on one index would race. The existing *_int8_
    caches share this; a std::once_flag would 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).

主要言語
Cuda
スター
902
フォーク
267
平均マージ
2日 21時間
マージ済み PR(30日)
57

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

NVIDIA/cuvs のほかの issue

NVIDIA/cuvs の issue をすべて見る

似ている issue

Backend & API Design の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。