Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
742dba9
Fuse RaBitQ block reductions into one templated blockReduceSumN<N>
Stardust-SJF Jul 24, 2026
22172a1
Fix rescale-search shared-memory races by restructuring
Stardust-SJF Jul 25, 2026
f3a31aa
Add a standalone RaBitQ quantizer under preprocessing/quantize
Stardust-SJF Jul 26, 2026
1a5e2a3
Make the RaBitQ quantizer detail layer stream- and resource-aware
Stardust-SJF Jul 26, 2026
2c64068
Rename the RaBitQ factories init_* -> make_*
Stardust-SJF Jul 27, 2026
36028d1
Add the C API for the RaBitQ preprocessing quantizer
Stardust-SJF Jul 28, 2026
a2c5747
Merge branch 'main' into cuvs_ivf_rabitq_syn_fix_fused
tfeher Aug 3, 2026
2a1c334
Replace the RaBitQ glue kernels with raft primitives
Stardust-SJF Oct 29, 2026
93ea433
Generate the RaBitQ matmul rotation with the shared IVF-PQ helper
Stardust-SJF Oct 29, 2026
3af40d4
Add cuvsRabitqRotatorMakeFromState to restore a saved rotator
Stardust-SJF Oct 29, 2026
33020da
Batch RaBitQ transform() internally, sized to the L2 cache
Stardust-SJF Oct 29, 2026
69d8b96
Fix 32-bit factor index overflow in the RaBitQ full-factor kernel
Stardust-SJF Oct 29, 2026
e8cd13c
Accept host input in RaBitQ transform()
Stardust-SJF Oct 29, 2026
1618145
Rename pipeline to quantizer and the old quantizer to encoder in RaBitQ
Stardust-SJF Oct 30, 2026
140fab3
Merge branch 'pull-request/2362' into cuvs_ivf_rabitq_standalone_quan…
achirkin Nov 2, 2026
128268a
Merge branch 'main' into cuvs_ivf_rabitq_syn_fix_fused
achirkin Nov 2, 2026
e837738
Merge remote-tracking branch 'sjf/cuvs_ivf_rabitq_syn_fix_fused' into…
achirkin Nov 2, 2026
b5ccd94
Merge remote-tracking branch 'rapidsai/main' into pull-request/2362
achirkin Nov 2, 2026
42fcdfb
Merge branch 'pull-request/2362' into cuvs_ivf_rabitq_standalone_quan…
achirkin Nov 2, 2026
a6ad49e
Merge branch 'main' into cuvs_ivf_rabitq_standalone_quantizer
achirkin Nov 3, 2026
89d518e
Spec-based dataset api
achirkin Nov 3, 2026
8f1b9ce
Fix style
achirkin Nov 3, 2026
4a9e8f3
Fix style
achirkin Nov 3, 2026
f02247a
Merge branch 'cuvs_ivf_rabitq_standalone_quantizer' into fea-rabitq-spec
achirkin Nov 3, 2026
7db6e2f
Fix build errors
achirkin Nov 3, 2026
28ccd92
Merge branch 'cuvs_ivf_rabitq_standalone_quantizer' into fea-rabitq-spec
achirkin Nov 3, 2026
afb5f91
Merge pull request #1 from achirkin/fea-rabitq-spec
Stardust-SJF Nov 4, 2026
82b1325
Merge branch 'main' into cuvs_ivf_rabitq_standalone_quantizer
achirkin Nov 4, 2026
8e68cde
Merge remote-tracking branch 'rapidsai/main' into cuvs_ivf_rabitq_sta…
achirkin Nov 4, 2026
c6632a7
Fix the symbol visibility
achirkin Nov 4, 2026
d42a40a
Merge remote-tracking branch 'public/main' into cuvs_ivf_rabitq_stand…
Stardust-SJF Nov 5, 2026
a7f80d5
Throw instead of exiting on unsupported RaBitQ FHT dimensions
Stardust-SJF Nov 5, 2026
12ed643
Opt in to more shared memory for the RaBitQ scaling-factor kernel
Stardust-SJF Nov 5, 2026
2e5d05a
Request shared memory in the RaBitQ quantize kernels only when needed
Stardust-SJF Nov 5, 2026
b3db6d9
Forward the rescale sample counts to the RaBitQ full-factor path
Stardust-SJF Nov 5, 2026
2cc3b97
Document that RaBitQ delta is computed from signed code levels
Stardust-SJF Nov 5, 2026
6d27955
Document the 16-bit code requirement for RaBitQ ex_bits = 8
Stardust-SJF Nov 5, 2026
0a1302e
Show the RaBitQ delta formulas in the API page tables
Stardust-SJF Nov 5, 2026
3a5fa14
Update cpp/include/cuvs/preprocessing/quantize/rabitq.hpp
Stardust-SJF Nov 11, 2026
d3c0cab
Update cpp/tests/preprocessing/rabitq_quantization.cu
Stardust-SJF Nov 11, 2026
d916649
Apply the review cleanups to the RaBitQ quantizer
Stardust-SJF Nov 11, 2026
b741b07
Use cub::BlockReduce for the RaBitQ quantizer block reductions
Stardust-SJF Nov 11, 2026
c11432f
Take mdspan views in the RaBitQ quantizer detail layer
Stardust-SJF Nov 12, 2026
94ed09e
Use raft::warpReduce for the RaBitQ quantizer warp sums
Stardust-SJF Nov 22, 2026
1540b4c
Add missing cub includes
tfeher Nov 23, 2026
049f6d6
Merge branch 'release/26.12' into cuvs_ivf_rabitq_standalone_quantizer
tfeher Nov 23, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make the RaBitQ quantizer detail layer stream- and resource-aware
The imported quantizer entry points launched on the per-thread default stream,
which under CUDA_API_PER_THREAD_DEFAULT_STREAM does not implicitly synchronize
with the resource stream. That was bridged by four host-side syncs, so every
transform() paid two full host<->device round trips and serialized the caller's
stream. Thread the stream through instead and delete the bridge.

- quantize_{fused,full}_on_residuals and the launch_quantize_* helpers now take
  cudaStream_t as their first parameter (first, so the existing trailing default
  arguments still work, and without a default of its own so no caller can fall
  back to the default stream by accident). Both kernel launches use it.
- get_const_scaling_factor takes raft::resources const& first: it needs the
  stream, the workspace memory resource and the device properties.
- Its three cudaMalloc/cudaFree pairs become rmm::device_uvector on the
  workspace resource, so the scratch comes from the caller's memory resource and
  is freed on the stream rather than at a device-wide synchronization point.
- cudaGetDeviceProperties(&prop, 0) becomes
  raft::resource::get_device_properties(res). Reading device 0's
  sharedMemPerBlock to pick block_size was wrong whenever the current device was
  not device 0, and it was the one CUDA call in the file with no RAFT_CUDA_TRY;
  the resource accessor is also cached, so this drops a driver query.
- Both cub::DeviceReduce::Sum calls take the stream and are wrapped in
  RAFT_CUDA_TRY.
- sync_before_quantize / sync_after_quantize are removed from both pipeline.cu
  and rabitq.cu along with all four call sites.

The transform() path now issues no host synchronization at all. The two
remaining syncs are init-only and necessary: the async read-back of the scaling
factor in get_const_scaling_factor (it returns a host float) and the state
uploads in init_state_matmul / init_state_fht_kac (they copy from host vectors
that die at scope exit).

No kernel body, no numerics and no rotator behaviour changed. PREPROCESSING_TEST
still passes 368/368 on an RTX PRO 6000 (sm_120a), unchanged from before.
  • Loading branch information
Stardust-SJF committed Jul 26, 2026
commit 1a5e2a35a68577ab3dfc7e13d3c41bde2bbc0fdb
17 changes: 2 additions & 15 deletions cpp/src/preprocessing/quantize/detail/rabitq/pipeline.cu
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,6 @@ void prepare_residuals(raft::resources const& res,
}
}

/// 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,
Expand All @@ -123,12 +112,11 @@ void quantize_data_impl(raft::resources const& res,
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(
raft::resource::get_cuda_stream(res),
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>
Expand All @@ -141,11 +129,10 @@ void quantize_data_full_impl(raft::resources const& res,
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(
raft::resource::get_cuda_stream(res),
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
Expand Down
20 changes: 14 additions & 6 deletions cpp/src/preprocessing/quantize/detail/rabitq/quantize.cu
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ __global__ void sa_quantize_fused_kernel(
// ---------------------------------------------------------------------------
template<typename CodeT>
static void launch_quantize_fused(
cudaStream_t stream,
const float* d_residual, size_t N, uint32_t padded_dim, size_t ex_bits,
float const_scaling_factor, bool use_fast, int delta_mode,
int coarse_samples, int fine_samples,
Expand All @@ -279,39 +280,44 @@ static void launch_quantize_fused(
constexpr int nwarps = block / 32;

size_t q_smem = (padded_dim + 2 * nwarps) * sizeof(float);
sa_quantize_fused_kernel<CodeT, block, false><<<iN, block, q_smem>>>(
sa_quantize_fused_kernel<CodeT, block, false><<<iN, block, q_smem, stream>>>(
d_residual, d_total_code, iN, iD, iB, const_scaling_factor, use_fast,
coarse_samples, fine_samples,
d_delta, d_vl, delta_mode, nullptr, nullptr);
RAFT_CUDA_TRY(cudaGetLastError());
}

void quantize_fused_on_residuals(
cudaStream_t stream,
const float* d_residuals, size_t N, size_t padded_dim,
size_t ex_bits, float const_scaling_factor, bool use_fast,
uint16_t* d_total_code, float* d_delta, float* d_vl, int delta_mode,
int coarse_samples, int fine_samples)
{
launch_quantize_fused(d_residuals, N, static_cast<uint32_t>(padded_dim), ex_bits,
launch_quantize_fused(stream,
d_residuals, N, static_cast<uint32_t>(padded_dim), ex_bits,
const_scaling_factor, use_fast, delta_mode,
coarse_samples, fine_samples,
d_total_code, d_delta, d_vl);
}

void quantize_fused_on_residuals(
cudaStream_t stream,
const float* d_residuals, size_t N, size_t padded_dim,
size_t ex_bits, float const_scaling_factor, bool use_fast,
uint8_t* d_total_code, float* d_delta, float* d_vl, int delta_mode,
int coarse_samples, int fine_samples)
{
launch_quantize_fused(d_residuals, N, static_cast<uint32_t>(padded_dim), ex_bits,
launch_quantize_fused(stream,
d_residuals, N, static_cast<uint32_t>(padded_dim), ex_bits,
const_scaling_factor, use_fast, delta_mode,
coarse_samples, fine_samples,
d_total_code, d_delta, d_vl);
}

template<typename CodeT>
static void launch_quantize_full(
cudaStream_t stream,
const float* d_residual, const float* d_centroid,
size_t N, uint32_t padded_dim, size_t ex_bits,
float const_scaling_factor, bool use_fast,
Expand All @@ -324,32 +330,34 @@ static void launch_quantize_full(
constexpr int nwarps = block / 32;

size_t q_smem = (padded_dim + 2 * nwarps) * sizeof(float);
sa_quantize_fused_kernel<CodeT, block, true><<<iN, block, q_smem>>>(
sa_quantize_fused_kernel<CodeT, block, true><<<iN, block, q_smem, stream>>>(
d_residual, d_total_code, iN, iD, iB, const_scaling_factor, use_fast,
64, 64,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
nullptr, nullptr, 0, d_centroid, d_factors);
RAFT_CUDA_TRY(cudaGetLastError());
}

void quantize_full_on_residuals(
cudaStream_t stream,
const float* d_residuals, const float* d_centroid,
size_t N, size_t padded_dim,
size_t ex_bits, float const_scaling_factor, bool use_fast,
uint16_t* d_total_code, float* d_factors)
{
launch_quantize_full(d_residuals, d_centroid, N,
launch_quantize_full(stream, d_residuals, d_centroid, N,
static_cast<uint32_t>(padded_dim), ex_bits,
const_scaling_factor, use_fast,
d_total_code, d_factors);
}

void quantize_full_on_residuals(
cudaStream_t stream,
const float* d_residuals, const float* d_centroid,
size_t N, size_t padded_dim,
size_t ex_bits, float const_scaling_factor, bool use_fast,
uint8_t* d_total_code, float* d_factors)
{
launch_quantize_full(d_residuals, d_centroid, N,
launch_quantize_full(stream, d_residuals, d_centroid, N,
static_cast<uint32_t>(padded_dim), ex_bits,
const_scaling_factor, use_fast,
d_total_code, d_factors);
Expand Down
17 changes: 16 additions & 1 deletion cpp/src/preprocessing/quantize/detail/rabitq/quantize.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,35 @@
#ifndef RABITQ_GPU_QUANTIZER_STANDALONE_CUH
#define RABITQ_GPU_QUANTIZER_STANDALONE_CUH

#include <cuda_runtime.h>

#include <cstdint>
#include <cstddef>

namespace cuvs::preprocessing::quantize::rabitq::detail {

/// Quantize pre-computed, rotated residuals into RaBitQ scalar codes.
///
/// d_residuals must be N × padded_dim on device (rotated, zero-centroid).
/// stream : the caller owns the stream. Every one of these entry points
/// only enqueues kernel launches on it and is therefore
/// *asynchronous*: the outputs are not readable until the caller
/// synchronizes the stream (or orders subsequent work on it).
/// All input and output buffers must stay alive and unmodified
/// until that work completes.
/// d_residuals : must be N × padded_dim on device (rotated, zero-centroid).
///
/// Writes N × padded_dim codes, plus a per-vector (delta, vl) pair.
///
/// delta_mode: 0 = RECONSTRUCTION, 1 = UNBIASED, 2 = PLAIN.
void quantize_fused_on_residuals(
cudaStream_t stream,
const float* d_residuals, size_t N, size_t padded_dim,
size_t ex_bits, float const_scaling_factor, bool use_fast,
uint16_t* d_total_code, float* d_delta, float* d_vl, int delta_mode = 0,
int coarse_samples = 64, int fine_samples = 64);

void quantize_fused_on_residuals(
cudaStream_t stream,
const float* d_residuals, size_t N, size_t padded_dim,
size_t ex_bits, float const_scaling_factor, bool use_fast,
uint8_t* d_total_code, float* d_delta, float* d_vl, int delta_mode = 0,
Expand All @@ -41,18 +52,22 @@ void quantize_fused_on_residuals(
/// (f_add, f_rescale, f_error) factor triplet used for approximate-distance
/// estimation during search.
///
/// stream : the caller owns the stream; the call is asynchronous on it
/// (see quantize_fused_on_residuals above).
/// d_residuals : N × padded_dim on device (rotated, centroid already subtracted).
/// d_centroid : padded_dim floats on device (the rotated centroid) — pass a
/// zero-filled buffer when there is no centroid.
/// d_factors : N × 3 floats on device — stride 3, layout
/// [f_add, f_rescale, f_error] per vector.
void quantize_full_on_residuals(
cudaStream_t stream,
const float* d_residuals, const float* d_centroid,
size_t N, size_t padded_dim,
size_t ex_bits, float const_scaling_factor, bool use_fast,
uint16_t* d_total_code, float* d_factors);

void quantize_full_on_residuals(
cudaStream_t stream,
const float* d_residuals, const float* d_centroid,
size_t N, size_t padded_dim,
size_t ex_bits, float const_scaling_factor, bool use_fast,
Expand Down
51 changes: 29 additions & 22 deletions cpp/src/preprocessing/quantize/detail/rabitq/rescale_search.cu
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@

#include <cstdint>
#include <raft/core/error.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/core/resource/device_memory_resource.hpp>
#include <raft/core/resource/device_properties.hpp>
#include <raft/core/resources.hpp>
#include <raft/util/cuda_rt_essentials.hpp>

#include <rmm/device_uvector.hpp>

namespace cuvs::preprocessing::quantize::rabitq::detail {

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -244,49 +250,50 @@ __global__ void rabitq_rescale_sample_kernel(
// ---------------------------------------------------------------------------
// Host entry point — average the per-row factors on device.
// ---------------------------------------------------------------------------
float get_const_scaling_factor(size_t dim, size_t ex_bits,
uint64_t seed,
int coarse_samples,
int fine_samples) {
float get_const_scaling_factor(raft::resources const& res,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thought: Here we generate random unit vectors, search scaling factors for them and average this over a 100 vectors. Ideally results should depend on dim and ex_bits. In theory one could construct a formula or a look up table and formula to get the scaling factor without estimating it. Although this estimation is cheap, we could remove some indeterminism related to the numerical search of these scaling factors. What do you think?

@Stardust-SJF Stardust-SJF Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can build a lookup table for some regular cases (e.g., dim {32, ..., 3072} x ex _bits {1, ..., 8}) and still keep this function to support arbitrary dimensions that are outside the lookup table. Does it make sense?

size_t dim, size_t ex_bits,
uint64_t seed,
int coarse_samples,
int fine_samples) {
constexpr long kConstNum = 100;

float* d_factors;
float* d_sum;
RAFT_CUDA_TRY(cudaMalloc(&d_factors, kConstNum * sizeof(float)));
RAFT_CUDA_TRY(cudaMalloc(&d_sum, sizeof(float)));
auto stream = raft::resource::get_cuda_stream(res);
auto workspace = raft::resource::get_workspace_resource_ref(res);

rmm::device_uvector<float> d_factors(static_cast<size_t>(kConstNum), stream, workspace);
rmm::device_uvector<float> d_sum(1, stream, workspace);

int block_size = 256;
if (dim <= 512) block_size = 128;
if (dim >= 1536) block_size = 512;

size_t shared_mem_size = (dim + 3 * block_size) * sizeof(float);

cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 0);
// Device properties of the *resource's* device (cached on `res`), not device 0.
cudaDeviceProp const& prop = raft::resource::get_device_properties(res);
if (shared_mem_size > prop.sharedMemPerBlock) {
block_size = 128;
shared_mem_size = (dim + 3 * block_size) * sizeof(float);
}

rabitq_rescale_sample_kernel<<<kConstNum, block_size, shared_mem_size>>>(
d_factors, kConstNum, static_cast<int>(dim), static_cast<int>(ex_bits), seed,
rabitq_rescale_sample_kernel<<<kConstNum, block_size, shared_mem_size, stream>>>(
d_factors.data(), kConstNum, static_cast<int>(dim), static_cast<int>(ex_bits), seed,
coarse_samples, fine_samples);
RAFT_CUDA_TRY(cudaGetLastError());

size_t temp_storage_bytes = 0;
cub::DeviceReduce::Sum(nullptr, temp_storage_bytes, d_factors, d_sum, kConstNum);
RAFT_CUDA_TRY(cub::DeviceReduce::Sum(
nullptr, temp_storage_bytes, d_factors.data(), d_sum.data(), kConstNum, stream));

void* d_temp_storage = nullptr;
RAFT_CUDA_TRY(cudaMalloc(&d_temp_storage, temp_storage_bytes));
cub::DeviceReduce::Sum(d_temp_storage, temp_storage_bytes, d_factors, d_sum, kConstNum);
RAFT_CUDA_TRY(cudaGetLastError());
rmm::device_uvector<char> d_temp_storage(temp_storage_bytes, stream, workspace);
RAFT_CUDA_TRY(cub::DeviceReduce::Sum(
d_temp_storage.data(), temp_storage_bytes, d_factors.data(), d_sum.data(), kConstNum,
stream));

float sum;
RAFT_CUDA_TRY(cudaMemcpy(&sum, d_sum, sizeof(float), cudaMemcpyDeviceToHost));

RAFT_CUDA_TRY(cudaFree(d_factors));
RAFT_CUDA_TRY(cudaFree(d_sum));
RAFT_CUDA_TRY(cudaFree(d_temp_storage));
RAFT_CUDA_TRY(
cudaMemcpyAsync(&sum, d_sum.data(), sizeof(float), cudaMemcpyDeviceToHost, stream));
raft::resource::sync_stream(res);

return sum / kConstNum;
}
Expand Down
18 changes: 14 additions & 4 deletions cpp/src/preprocessing/quantize/detail/rabitq/rescale_search.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef RABITQ_GPU_RESCALE_SEARCH_CUH
#define RABITQ_GPU_RESCALE_SEARCH_CUH

#include <raft/core/resources.hpp>

#include <cstddef>
#include <cstdint>

Expand Down Expand Up @@ -46,10 +48,18 @@ __device__ float compute_best_rescale_parallel(

/// Host: estimate the constant scaling factor used by the fast-quantize path.
/// Averages `kConstNum` rescale factors computed on random Gaussian vectors.
float get_const_scaling_factor(size_t dim, size_t ex_bits,
uint64_t seed = 12345ULL,
int coarse_samples = 64,
int fine_samples = 64);
///
/// All work is issued on `raft::resource::get_cuda_stream(res)` and all scratch
/// memory comes from `raft::resource::get_workspace_resource_ref(res)`; the
/// device properties used to pick the block size are read from `res` as well,
/// so the sizing follows the resource's device rather than device 0.
/// Because the estimate is returned by value, the function synchronizes that
/// stream once before returning.
float get_const_scaling_factor(raft::resources const& res,
size_t dim, size_t ex_bits,
uint64_t seed = 12345ULL,
int coarse_samples = 64,
int fine_samples = 64);

} // namespace cuvs::preprocessing::quantize::rabitq::detail

Expand Down
Loading