Add GPU-accelerated IVFPQ search to the Metal backend - #5449
Open
Evandabest wants to merge 20 commits into
Open
Conversation
Contributor
Author
|
Following up on #5288, here are the isolated PQ lookup-table construction results from the GPU-scan work (same as shown in first PR). All measurements were collected on an M3 Pro with d=128, 8-bit PQ, and FP32 lookup tables. PQ LUT computation
LUT speedup saturates at ~3.7-3.8x once the batch is large enough to hide kernel-launch overhead (nq ≥ 10k); ~1.2x for tiny batches. |
mnorris11
reviewed
Jul 22, 2026
mnorris11
reviewed
Jul 22, 2026
Contributor
|
@mnorris11 has imported this pull request. If you are a Meta employee, you can view this in D113864779. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This is a follow-up to #5288 which added
MetalIndexIVFPQwith GPU-resident storage while delegating search to the CPU.Adds GPU-accelerated IVFPQ search to the Metal backend.
M x 256query term once per query batch instead of once per(query, probe)nprobe * kcandidate limitChanges
MetalDistance.metal- IVFPQ LUT construction, precomputed-table, list scan, segmented top-k, and grouped merge kernelsMetalDistance.h/.mm- IVFPQ search orchestration and precomputed-table entry pointsMetalKernels.h/.mm- IVFPQ Metal kernel dispatch methodsMetalIndexIVFPQ.h/.mm- GPU search path, reusable buffers, centroid uploads, precomputed terms, and CPU fallbackMetalIndex.h- add theuseFloat16LUT configurationDifferences from CUDA IVFPQ
Training:
MetalIndexIVFPQ::traindelegates training to its CPU index, as introduced in #5288. CUDA can train the coarse and product quantizers on GPU. Training is a one-time cost.Add path: Coarse assignment, residual computation, and PQ encoding remain on the CPU. The encoded PQ codes are stored in GPU-resident Metal buffers. CUDA performs these operations on GPU.
Coarse quantization: The coarse quantizer search runs on the CPU. The selected lists and coarse distances are passed to the Metal scan.
Lookup tables: The preferred Metal path mirrors the CPU IVFPQ precomputed-table decomposition. The query independent centroid/PQ term is computed once per trained index, while the query term is computed once per batch. This avoids materializing a separate lookup table for every
(query, probe)pair.List scanning: Metal performs asymmetric distance computation directly over the GPU-resident 8-bit PQ codes. Segmented selection maintains an exact running top-k for inverted lists of any length.
Top-k merge: Per-list results are merged in groups over multiple rounds. This avoids the fixed candidate limit of the original single-pass merge.
Fallback: The preferred precomputed-table path supports
M <= 16,d / M <= 256, andk <= 512. Unsupported configurations fall back to the legacy Metal path or CPU search.Build and test