Repository navigation
Support Standalone RaBitQ in cuVS #2377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
742dba9
22172a1
f3a31aa
1a5e2a3
2c64068
36028d1
a2c5747
2a1c334
93ea433
3af40d4
33020da
69d8b96
e8cd13c
1618145
140fab3
128268a
e837738
b5ccd94
42fcdfb
a6ad49e
89d518e
8f1b9ce
4a9e8f3
f02247a
7db6e2f
28ccd92
afb5f91
82b1325
8e68cde
c6632a7
d42a40a
a7f80d5
12ed643
2e5d05a
b3db6d9
2cc3b97
6d27955
0a1302e
3a5fa14
d3c0cab
d916649
b741b07
c11432f
94ed09e
1540b4c
049f6d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Imports the standalone RaBitQ GPU quantizer as a first-class preprocessing
component, alongside the existing scalar, binary and pq quantizers. It is
independent of cuvs::neighbors::ivf_rabitq and shares no code with it.
Public API (cuvs::preprocessing::quantize::rabitq, header-installed with the
rest of include/cuvs):
rotator_kind { matmul, fht_kac }, delta_kind, params
rotator - kind + padded_dim + the rotation state (raft containers)
quantizer - params + const_scaling_factor + dim/padded_dim
pipeline - composes the two; neither owns the other
init_rotator / init_quantizer / init_pipeline
rotate, transform, transform_residuals
The rotator and the quantizer are independent peer types, mirroring how
IVFGPU already holds RotatorGPU and DataQuantizerGPU as siblings; `pipeline`
is the pq-like convenience that composes them. The factories take a `dim`
rather than a dataset because RaBitQ needs no training: they generate the
random rotation and derive the scaling constant from (dim, ex_bits, seed).
State is plain caller-owned data in raft containers, following
binary::quantizer, so the rotator travels with the codes and no serialize API
is required - matching every other preprocessing quantizer, none of which has
I/O (persistence lives at the index layer, cf. detail/dataset_serialize.hpp).
Import notes:
- The imported sources land in detail/rabitq/ essentially verbatim, wrapped in
...::rabitq::detail. Include paths, the enclosing namespace and the error
macros are the only edits; kernels and numerics are untouched.
- CUDA_CHECK/CUBLAS_CHECK, which printed and called exit(), are now
RAFT_CUDA_TRY/RAFT_CUBLAS_TRY so failures throw instead of killing the
process.
- class RotatorGPU is dissolved: it owned raw cudaMalloc buffers and its own
cublasHandle_t. Its state now lives in the public rotator, and detail exposes
free launchers over caller-owned buffers, taking the cuBLAS handle and the
stream from raft::resources.
- The pipeline's scratch buffer comes from the workspace resource instead of
cudaMalloc, and its glue kernels run on the resource stream.
- The fht_kac segment size is derived from the padded dimension, matching the
standalone.
Known follow-up: quantize.cu and rescale_search.cu still launch on the default
stream. Since cuVS builds with CUDA_API_PER_THREAD_DEFAULT_STREAM that does not
implicitly sync with the resource stream, so pipeline.cu bridges them with two
TODO(rabitq)-marked host syncs. Threading a stream through those two files
removes both.
Tested on an RTX PRO 6000 (sm_120a): PREPROCESSING_TEST passes 368/368,
including 141 new RaBitQ cases covering both rotator kinds, uint8/uint16 codes,
padded/pow2/non-pow2 dims, rotation norm preservation, non-degenerate codes,
finite factors and run-to-run determinism.- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,209 @@ | ||||||
| /* | ||||||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||||||
| * SPDX-License-Identifier: Apache-2.0 | ||||||
| * | ||||||
| * Imported from the standalone RaBitQ GPU quantizer. Behaviour unchanged; | ||||||
| * only include paths, error macros and the enclosing namespace differ. | ||||||
| */ | ||||||
|
|
||||||
| // | ||||||
| // RaBitQ pipeline — orchestration only. Composes the rotator launchers and the | ||||||
| // fused quantizer entry points; the only device code here is trivial glue | ||||||
| // (row padding, centroid subtraction). | ||||||
| // | ||||||
|
|
||||||
| #include "pipeline.cuh" | ||||||
| #include "quantize.cuh" | ||||||
|
|
||||||
| #include <raft/util/cuda_rt_essentials.hpp> | ||||||
| #include <raft/core/error.hpp> | ||||||
| #include <raft/core/resource/cuda_stream.hpp> | ||||||
| #include <raft/core/resource/device_memory_resource.hpp> | ||||||
|
|
||||||
| #include <rmm/device_uvector.hpp> | ||||||
|
|
||||||
| #include <cuda_runtime.h> | ||||||
|
|
||||||
| namespace cuvs::preprocessing::quantize::rabitq::detail { | ||||||
|
|
||||||
| namespace { | ||||||
|
|
||||||
| __global__ void pad_rows_kernel(const float* __restrict__ d_src, | ||||||
| float* __restrict__ d_dst, | ||||||
| int N, int dim, int D) { | ||||||
| int idx = blockIdx.x * blockDim.x + threadIdx.x; | ||||||
| if (idx >= N * D) return; | ||||||
| int row = idx / D; | ||||||
| int col = idx % D; | ||||||
| d_dst[idx] = (col < dim) ? d_src[row * dim + col] : 0.0f; | ||||||
| } | ||||||
|
|
||||||
| __global__ void subtract_row_kernel(float* __restrict__ d_rows, | ||||||
| const float* __restrict__ d_row, | ||||||
| int N, int D) { | ||||||
| int idx = blockIdx.x * blockDim.x + threadIdx.x; | ||||||
| if (idx >= N * D) return; | ||||||
| d_rows[idx] -= d_row[idx % D]; | ||||||
| } | ||||||
|
|
||||||
| /// Pad (if dim < rotator.padded_dim) and rotate d_src into d_dst. When padding | ||||||
| /// is needed, pads directly into d_dst and rotates in place if the rotator | ||||||
| /// supports it (fht_kac); otherwise stages through a temporary (matmul). | ||||||
| void pad_and_rotate(raft::resources const& res, | ||||||
| const float* d_src, size_t N, size_t dim, | ||||||
| rotator_ref const& rotator, float* d_dst) { | ||||||
| const size_t D = static_cast<size_t>(rotator.padded_dim); | ||||||
| if (dim == D) { | ||||||
| rotate(res, rotator, d_src, d_dst, static_cast<int64_t>(N)); | ||||||
| return; | ||||||
| } | ||||||
| auto stream = raft::resource::get_cuda_stream(res); | ||||||
| constexpr int kBlock = 256; | ||||||
| const size_t total = N * D; | ||||||
| const int grid = static_cast<int>((total + kBlock - 1) / kBlock); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now this handwritten grid disappeared. |
||||||
| if (supports_inplace_rotate(rotator)) { | ||||||
| pad_rows_kernel<<<grid, kBlock, 0, stream>>>(d_src, d_dst, | ||||||
| static_cast<int>(N), static_cast<int>(dim), | ||||||
| static_cast<int>(D)); | ||||||
| RAFT_CUDA_TRY(cudaGetLastError()); | ||||||
| rotate(res, rotator, d_dst, d_dst, static_cast<int64_t>(N)); | ||||||
| } else { | ||||||
| rmm::device_uvector<float> d_tmp( | ||||||
| total, stream, raft::resource::get_workspace_resource_ref(res)); | ||||||
| pad_rows_kernel<<<grid, kBlock, 0, stream>>>(d_src, d_tmp.data(), | ||||||
| static_cast<int>(N), static_cast<int>(dim), | ||||||
| static_cast<int>(D)); | ||||||
| RAFT_CUDA_TRY(cudaGetLastError()); | ||||||
| rotate(res, rotator, d_tmp.data(), d_dst, static_cast<int64_t>(N)); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// d_residuals = rotate(pad(data)) [- rotate(pad(centroid))]. | ||||||
| /// require_rotated_c: full-factor mode always needs the rotated-centroid | ||||||
| /// buffer (zero-filled when there is no centroid). | ||||||
| void prepare_residuals(raft::resources const& res, | ||||||
| const float* d_data, size_t N, size_t dim, | ||||||
| rotator_ref const& rotator, | ||||||
| const float* d_centroid, float* d_rotated_centroid, | ||||||
| float* d_residuals, bool require_rotated_c) { | ||||||
| const size_t D = static_cast<size_t>(rotator.padded_dim); | ||||||
| auto stream = raft::resource::get_cuda_stream(res); | ||||||
| pad_and_rotate(res, d_data, N, dim, rotator, d_residuals); | ||||||
| if (d_centroid != nullptr) { | ||||||
| pad_and_rotate(res, d_centroid, 1, dim, rotator, d_rotated_centroid); | ||||||
| constexpr int kBlock = 256; | ||||||
| const size_t total = N * D; | ||||||
| subtract_row_kernel<<<static_cast<int>((total + kBlock - 1) / kBlock), kBlock, 0, stream>>>( | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be expressed with raft::linewise_op: auto rows = raft::make_device_matrix_view<float, int64_t>(d_residuals, N, D);
raft::matrix::linewise_op<raft::Apply::ALONG_ROWS>(
res, raft::make_const_mdspan(rows), rows, raft::sub_op{},
raft::make_device_vector_view<const float, int64_t>(d_rotated_centroid, dim));
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now expressed with |
||||||
| d_residuals, d_rotated_centroid, static_cast<int>(N), static_cast<int>(D)); | ||||||
| RAFT_CUDA_TRY(cudaGetLastError()); | ||||||
| } else if (require_rotated_c) { | ||||||
| RAFT_CUDA_TRY(cudaMemsetAsync(d_rotated_centroid, 0, D * sizeof(float), stream)); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// The residual stage above runs on the resource stream, while the imported | ||||||
| /// quantizer entry points still launch on the (per-thread) default stream. | ||||||
| /// Bridge the two with a host-side sync until quantize.cuh takes a stream. | ||||||
| /// TODO(rabitq): drop once quantize_*_on_residuals is stream-aware. | ||||||
| void sync_before_quantize(raft::resources const& res) { raft::resource::sync_stream(res); } | ||||||
|
|
||||||
| /// Mirror of sync_before_quantize: make the codes written by the quantizer | ||||||
| /// visible to work the caller subsequently issues on the resource stream. | ||||||
| /// TODO(rabitq): drop together with sync_before_quantize. | ||||||
| void sync_after_quantize() { RAFT_CUDA_TRY(cudaStreamSynchronize(0)); } | ||||||
|
|
||||||
| template <typename CodeT> | ||||||
| void quantize_data_impl(raft::resources const& res, | ||||||
| const float* d_data, size_t N, size_t dim, | ||||||
| rotator_ref const& rotator, | ||||||
| const float* d_centroid, float* d_rotated_centroid, | ||||||
| size_t ex_bits, float const_scaling_factor, bool use_fast, | ||||||
| CodeT* d_total_code, float* d_delta, float* d_vl, | ||||||
| float* d_residuals, | ||||||
| int delta_mode, int coarse_samples, int fine_samples) { | ||||||
| prepare_residuals(res, d_data, N, dim, rotator, d_centroid, d_rotated_centroid, | ||||||
| d_residuals, /*require_rotated_c=*/false); | ||||||
| sync_before_quantize(res); | ||||||
| quantize_fused_on_residuals( | ||||||
| d_residuals, N, static_cast<size_t>(rotator.padded_dim), ex_bits, | ||||||
| const_scaling_factor, use_fast, | ||||||
| d_total_code, d_delta, d_vl, delta_mode, coarse_samples, fine_samples); | ||||||
| sync_after_quantize(); | ||||||
| } | ||||||
|
|
||||||
| template <typename CodeT> | ||||||
| void quantize_data_full_impl(raft::resources const& res, | ||||||
| const float* d_data, size_t N, size_t dim, | ||||||
| rotator_ref const& rotator, | ||||||
| const float* d_centroid, float* d_rotated_centroid, | ||||||
| size_t ex_bits, float const_scaling_factor, bool use_fast, | ||||||
| CodeT* d_total_code, float* d_factors, | ||||||
| float* d_residuals) { | ||||||
| prepare_residuals(res, d_data, N, dim, rotator, d_centroid, d_rotated_centroid, | ||||||
| d_residuals, /*require_rotated_c=*/true); | ||||||
| sync_before_quantize(res); | ||||||
| quantize_full_on_residuals( | ||||||
| d_residuals, d_rotated_centroid, N, static_cast<size_t>(rotator.padded_dim), ex_bits, | ||||||
| const_scaling_factor, use_fast, d_total_code, d_factors); | ||||||
| sync_after_quantize(); | ||||||
| } | ||||||
|
|
||||||
| } // namespace | ||||||
|
|
||||||
| void quantize_data( | ||||||
| raft::resources const& res, | ||||||
| const float* d_data, size_t N, size_t dim, | ||||||
| rotator_ref const& rotator, | ||||||
| const float* d_centroid, float* d_rotated_centroid, | ||||||
| size_t ex_bits, float const_scaling_factor, bool use_fast, | ||||||
| uint16_t* d_total_code, float* d_delta, float* d_vl, | ||||||
| float* d_residuals, | ||||||
| int delta_mode, int coarse_samples, int fine_samples) { | ||||||
| quantize_data_impl(res, d_data, N, dim, rotator, d_centroid, d_rotated_centroid, | ||||||
| ex_bits, const_scaling_factor, use_fast, | ||||||
| d_total_code, d_delta, d_vl, d_residuals, | ||||||
| delta_mode, coarse_samples, fine_samples); | ||||||
| } | ||||||
|
|
||||||
| void quantize_data( | ||||||
| raft::resources const& res, | ||||||
| const float* d_data, size_t N, size_t dim, | ||||||
| rotator_ref const& rotator, | ||||||
| const float* d_centroid, float* d_rotated_centroid, | ||||||
| size_t ex_bits, float const_scaling_factor, bool use_fast, | ||||||
| uint8_t* d_total_code, float* d_delta, float* d_vl, | ||||||
| float* d_residuals, | ||||||
| int delta_mode, int coarse_samples, int fine_samples) { | ||||||
| quantize_data_impl(res, d_data, N, dim, rotator, d_centroid, d_rotated_centroid, | ||||||
| ex_bits, const_scaling_factor, use_fast, | ||||||
| d_total_code, d_delta, d_vl, d_residuals, | ||||||
| delta_mode, coarse_samples, fine_samples); | ||||||
| } | ||||||
|
|
||||||
| void quantize_data_full( | ||||||
| raft::resources const& res, | ||||||
| const float* d_data, size_t N, size_t dim, | ||||||
| rotator_ref const& rotator, | ||||||
| const float* d_centroid, float* d_rotated_centroid, | ||||||
| size_t ex_bits, float const_scaling_factor, bool use_fast, | ||||||
| uint16_t* d_total_code, float* d_factors, | ||||||
| float* d_residuals) { | ||||||
| quantize_data_full_impl(res, d_data, N, dim, rotator, d_centroid, d_rotated_centroid, | ||||||
| ex_bits, const_scaling_factor, use_fast, | ||||||
| d_total_code, d_factors, d_residuals); | ||||||
| } | ||||||
|
|
||||||
| void quantize_data_full( | ||||||
| raft::resources const& res, | ||||||
| const float* d_data, size_t N, size_t dim, | ||||||
| rotator_ref const& rotator, | ||||||
| const float* d_centroid, float* d_rotated_centroid, | ||||||
| size_t ex_bits, float const_scaling_factor, bool use_fast, | ||||||
| uint8_t* d_total_code, float* d_factors, | ||||||
| float* d_residuals) { | ||||||
| quantize_data_full_impl(res, d_data, N, dim, rotator, d_centroid, d_rotated_centroid, | ||||||
| ex_bits, const_scaling_factor, use_fast, | ||||||
| d_total_code, d_factors, d_residuals); | ||||||
| } | ||||||
|
|
||||||
| } // namespace cuvs::preprocessing::quantize::rabitq::detail | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Imported from the standalone RaBitQ GPU quantizer. Behaviour unchanged; | ||
| * only include paths, error macros and the enclosing namespace differ. | ||
| */ | ||
|
|
||
| // | ||
| // One-call RaBitQ quantization pipeline. | ||
| // | ||
| // This layer only orchestrates the two underlying components — the rotator | ||
| // (rotator.cuh) and the quantizer (quantize.cuh) — plus two trivial glue | ||
| // kernels (row padding, centroid subtraction). It adds no quantization logic | ||
| // of its own; use the components directly for custom pipelines (e.g. | ||
| // externally computed residuals). | ||
| // | ||
| // Pipeline: pad(dim -> rotator.padded_dim) -> rotate -> [subtract rotated | ||
| // centroid] -> fused quantization on the rotated residuals. | ||
| // | ||
| // Design note: the centroid is rotated with a separate 1-row rotate() call | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this make kernel launch overhead a problem?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will not be a problem as launches are lightweight (μs) and only matter for small batches, whereas in most cases, the bottleneck should be the bandwidth for the following subtraction. By the way, it can be possible to optimize this pipeline, such as subtracting before the rotation, or using a fused kernel. |
||
| // rather than appended to the data batch (as cuVS does). Appending is only | ||
| // free when a gather/copy pass over the data exists anyway; for contiguous | ||
| // input it would cost a full extra N x D copy, while a 1-row rotate is a | ||
| // single tiny launch. | ||
| // | ||
| // The rotator state is NOT owned here: callers pass a non-owning rotator_ref | ||
| // that borrows the buffers held by the public `rabitq::rotator` struct. | ||
| // | ||
|
|
||
| #ifndef RABITQ_GPU_PIPELINE_CUH | ||
| #define RABITQ_GPU_PIPELINE_CUH | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| #include <raft/core/resources.hpp> | ||
|
|
||
| #include "rotator.cuh" | ||
|
|
||
| namespace cuvs::preprocessing::quantize::rabitq::detail { | ||
|
|
||
| /// Quantize raw (unrotated, unpadded) vectors end to end, emitting per-vector | ||
| /// scalar factors (delta, vl). | ||
| /// | ||
| /// d_data N x dim on device. | ||
| /// d_centroid optional (nullable) dim floats on device, UNrotated. | ||
| /// When non-null, residuals = rotate(pad(data)) - | ||
| /// rotate(pad(centroid)) and d_rotated_centroid must be | ||
| /// non-null (receives the rotated centroid, D floats). | ||
| /// When null, residuals = rotate(pad(data)) and | ||
| /// d_rotated_centroid may be null. | ||
| /// d_residuals N x D output (D = rotator.padded_dim): the rotated | ||
| /// residuals that the emitted codes refer to. | ||
| /// Remaining parameters mirror quantize_fused_on_residuals. | ||
| void quantize_data(raft::resources const& res, | ||
| const float* d_data, | ||
| size_t N, | ||
| size_t dim, | ||
| rotator_ref const& rotator, | ||
| const float* d_centroid, | ||
| float* d_rotated_centroid, | ||
| size_t ex_bits, | ||
| float const_scaling_factor, | ||
| bool use_fast, | ||
| uint16_t* d_total_code, | ||
| float* d_delta, | ||
| float* d_vl, | ||
| float* d_residuals, | ||
| int delta_mode = 0, | ||
| int coarse_samples = 64, | ||
| int fine_samples = 64); | ||
|
|
||
| void quantize_data(raft::resources const& res, | ||
| const float* d_data, | ||
| size_t N, | ||
| size_t dim, | ||
| rotator_ref const& rotator, | ||
| const float* d_centroid, | ||
| float* d_rotated_centroid, | ||
| size_t ex_bits, | ||
| float const_scaling_factor, | ||
| bool use_fast, | ||
| uint8_t* d_total_code, | ||
| float* d_delta, | ||
| float* d_vl, | ||
| float* d_residuals, | ||
| int delta_mode = 0, | ||
| int coarse_samples = 64, | ||
| int fine_samples = 64); | ||
|
|
||
| /// Full-factor variant: emits the (f_add, f_rescale, f_error) triplet used | ||
| /// for approximate-distance estimation. d_rotated_centroid (D floats) is | ||
| /// always required here — it participates in the factor computation — and is | ||
| /// zero-filled when d_centroid is null. | ||
| void quantize_data_full(raft::resources const& res, | ||
| const float* d_data, | ||
| size_t N, | ||
| size_t dim, | ||
| rotator_ref const& rotator, | ||
| const float* d_centroid, | ||
| float* d_rotated_centroid, | ||
| size_t ex_bits, | ||
| float const_scaling_factor, | ||
| bool use_fast, | ||
| uint16_t* d_total_code, | ||
| float* d_factors, | ||
| float* d_residuals); | ||
|
|
||
| void quantize_data_full(raft::resources const& res, | ||
| const float* d_data, | ||
| size_t N, | ||
| size_t dim, | ||
| rotator_ref const& rotator, | ||
| const float* d_centroid, | ||
| float* d_rotated_centroid, | ||
| size_t ex_bits, | ||
| float const_scaling_factor, | ||
| bool use_fast, | ||
| uint8_t* d_total_code, | ||
| float* d_factors, | ||
| float* d_residuals); | ||
|
|
||
| } // namespace cuvs::preprocessing::quantize::rabitq::detail | ||
|
|
||
| #endif // RABITQ_GPU_PIPELINE_CUH | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be expressed with
raft::linalg::map:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now replaced with
raft::linalg::map