Skip to content

[FEA] Faster coarse-list assignment in IVF-PQ transform for large n_lists (grouped-GEMM assigner) #3007

Open

Description

@jamxia155

Problem
Coarse assignment in ivf_pq::transform is a top-1 NN over all K centroids (GEMM + argmin). At K=32768 it dominates transform time.

Solution
Add an opt-in approximate assigner, selected inside ivf_pq::transform through a params/index option (no new public labels API). The default stays exact assignment. Approximate assignment changes list membership slightly.

The assigner is a standalone centroid-assignment component (queries + centroids -> nearest centroid id per query) with no dependence on PQ, so IVF-PQ, IVF-Flat and other IVF builders can share it. This issue proposes wiring it into IVF-PQ transform first; each algorithm would expose the same opt-in switch in its own params, defaulting to exact.

Relation to existing work
Approximate top-1 assignment over centroids is already being explored in cuVS, and this proposal is meant to sit alongside it as another variant of the same step, not as a separate mechanism:

The routing structure (coarse lists over the centroids, bucket queries by list, exact search within the candidate lists) is the same idea as #2963. What this issue adds:

  • a single persistent fused kernel for the per-list search (no per-list launches, no separate gather pass, no score matrix), and
  • spherical k-means grouping of the centroids, with a small, bounded workspace, and
  • use at transform/extend time on large datasets, in addition to training-time assignment.
    The measured gains below are for the transform path only; we have not measured the training-time path.

Algorithm:

  1. Group the K centroids into M ~ sqrt(K) lists (spherical k-means, ~10 iterations, built once per index; ~0.1 s at K=32768).
  2. Score each query against the M list centers and take its top n_probes lists (default 8).
  3. Bucket the (list, query) pairs by list (radix sort on list id).
  4. Per list, find the exact nearest centroid for each of its queries among that list's centroids.
  5. Merge each query's n_probes partial winners.

Implementation: a single persistent kernel for steps 3-4. Work items are (list, 128-query tile); CTAs pull items from an atomic counter. Each CTA gathers its query rows by index straight into shared memory (cp.async), runs TF32 tensor-core MMA against the list's centroid tiles, and keeps a running argmax per query. It writes one (score, centroid id) pair per (list, query) pair. There is no per-list kernel launch, no separate gather pass and no score matrix. The work table (pair offsets, tile prefix sums) is built on the device from the per-list counts, so the assign stage needs no host synchronization.

Properties:

  • The assigner runs inside the existing max_batch_size = 65536 loop: each chunk is assigned and then encoded, with the same chunk size as the rest of transform. It needs no restructuring of transform and no separate staging rule for host-resident datasets.
  • Scratch is small and bounded by the chunk: one (score, id) pair per (query, probe), plus O(M) tables.
  • Per-row cost is flat from about 8K rows upward, so the 64K chunk costs nothing relative to larger batches, and exact assignment is only faster below about 2K rows per call.
  • Requirements: sm80 or newer (mma.sync TF32); the prototype currently needs dim to be a multiple of 32. Other architectures and dimensions use the existing exact path.
  • Metrics: the prototype covers inner product / cosine; L2 adds a per-centroid bias term in the epilogue (not yet implemented).

Measured (H100, CUDA 13.4, K=32768, D=2048, M=181 lists, 8 probes; preliminary)
Assign stage only, standalone benchmark on contiguous queries, including list scoring and sort. Exact reference: TF32 GEMM + argmax over all K centroids, 0.42 us/row.

Rows per call Persistent kernel Speedup vs exact
128K 17.4 ms (0.133 us/row) 3.2x
64K 8.55 ms (0.131 us/row) 3.2x
16K 2.0 ms 3.5x
4K 0.72 ms 2.4x

Label agreement with the exact assignment is 98.2% (the same as a per-list cuBLAS implementation of the same algorithm). The kernel is not yet tuned: it reaches about 55 TFLOP/s.

End-to-end transform_s with a per-list cuBLAS implementation of the same algorithm (1M and 15M rows, cosine, 256 sub-vectors): 0.703 s vs 1.08-1.22 s for exact assignment at 1M rows, and 9.3 s vs 14.0-14.3 s at 15M rows. recall@10 is unchanged within build-to-build spread (~0.007). End-to-end numbers for the persistent kernel will follow once it is integrated.

Open questions

  • Is a hand-written tensor-core kernel (mma.sync, sm80+) acceptable here, or would maintainers prefer building on CUTLASS? CUTLASS is already a dependency. Its grouped GEMM reached ~125 TFLOP/s on these shapes, but it has no per-problem gather and no fused argmax epilogue, so it would still need a separate gather pass and a score matrix.
  • Fallback policy for other architectures and for dimensions not handled by the kernel: use the exact path silently, or raise?
  • API shape, as a follow-up to [WIP] ANNs for clustering at training time  #1938: exact assignment goes through the internal top_1_nn selector (Top1nnBackend: Auto/Cutile/Cutlass/Unfused, with probe_top_1_nn returning a reusable Top1nnPlan), which kmeans_balanced::predict, Lloyd's k-means and IVF-PQ transform all share. Approximate backends differ in two ways: they must stay opt-in (never chosen by Auto), and they need a structure built once over the centroids (CAGRA graph, IVF lists or our grouping). One option is for the plan object returned by probe_top_1_nn to own that structure, so the build is amortized across calls. Should approximate assigners be expressed as variants of that selector, or as a separate component that callers opt into? We would follow whatever [WIP] ANNs for clustering at training time  #1938 establishes.
  • Where should the shared component and the grouped centroid index live? Options: a shared location (e.g. cluster/ or neighbors/detail) with the index built from the centroid set and owned by whichever index holds those centroids (so it can be reused by IVF-PQ and IVF-Flat), or built lazily on first transform. Is a shared component preferred over an IVF-PQ-only one?

Alternatives tried

  • CAGRA k=1 and IVF-flat assignment: 2.3-3x slower than exact at K=32768.
  • A per-list cuBLAS GEMM loop (about 3 launches per list): faster than exact only above ~14K rows per call.
  • cublasGemmGroupedBatchedEx: its GEMM alone was slower than 181 separate GEMMs, and it needs all gathered operands materialized (~9.6 GB at 128K rows).
  • A public transform_with_labels taking caller-supplied labels works, but only helps callers that adopt it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions