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
Merge remote-tracking branch 'rapidsai/main' into cuvs_ivf_rabitq_sta…
…ndalone_quantizer
  • Loading branch information
achirkin committed Nov 4, 2026
commit 8e68cded6cdecded95043400e52505ca4cfff0a9
123 changes: 47 additions & 76 deletions cpp/src/neighbors/ivf_rabitq/gpu_index/quantizer_gpu.cu
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,13 @@ __global__ void pack_and_compute_factors_kernel(
xu_sq += xu * xu;
}

// Fused single-pass reduction of the four factor components: one call over its own
// shared scratch, so no back-to-back scratch reuse and no inter-call __syncthreads.
float reduce_buf[4] = {l2_sqr, ip_resi_xucb, ip_cent_xucb, xu_sq};
blockReduceSumN<4>(reduce_buf);
l2_sqr = reduce_buf[0];
ip_resi_xucb = reduce_buf[1];
ip_cent_xucb = reduce_buf[2];
xu_sq = reduce_buf[3];
// All four factor components in one block reduction (the sums are valid in thread 0 only)
const FloatSumN<4> sums =
FactorReduce(factor_temp).Sum(FloatSumN<4>{{l2_sqr, ip_resi_xucb, ip_cent_xucb, xu_sq}});
l2_sqr = sums.v[0];
ip_resi_xucb = sums.v[1];
ip_cent_xucb = sums.v[2];
xu_sq = sums.v[3];

// Thread 0 performs the final calculations and writes the factors
if (threadIdx.x == 0) {
Expand Down Expand Up @@ -415,14 +414,13 @@ __global__ void exrabitq_fused_kernel_batch(
xu_sq += xu * xu;
}

// Fused single-pass reduction of the four factor components: one call over its own
// shared scratch, so no back-to-back scratch reuse and no inter-call __syncthreads.
float reduce_buf[4] = {l2_sqr, ip_resi_xucb, ip_cent_xucb, xu_sq};
blockReduceSumN<4>(reduce_buf);
l2_sqr = reduce_buf[0];
ip_resi_xucb = reduce_buf[1];
ip_cent_xucb = reduce_buf[2];
xu_sq = reduce_buf[3];
// All four factor components in one block reduction (the sums are valid in thread 0 only)
const FloatSumN<4> sums =
FactorReduce(factor_temp).Sum(FloatSumN<4>{{l2_sqr, ip_resi_xucb, ip_cent_xucb, xu_sq}});
l2_sqr = sums.v[0];
ip_resi_xucb = sums.v[1];
ip_cent_xucb = sums.v[2];
xu_sq = sums.v[3];

// Thread 0 computes and writes the final factors
if (tid == 0) {
Expand Down Expand Up @@ -951,11 +949,26 @@ extern __constant__ float d_kTightStart_opt[9] = {
0.81f,
};

// A (score, scale) candidate of the rescale search. The combiner keeps the candidate with the
// larger score; ties keep the left operand.
struct RescaleCandidate {
float ip;
float t;
};

struct RescaleCandidateMax {
__device__ __forceinline__ RescaleCandidate operator()(const RescaleCandidate& a,
const RescaleCandidate& b) const
{
return (b.ip > a.ip) ? b : a;
}
};

// PRECONDITION: s_xp_norm must be block-visible before the call — callers
// fill it with block-strided writes and must issue a __syncthreads() between
// the last write and this call (CUB-style caller-owned input visibility).
__device__ float compute_best_rescale_parallel(
float* s_xp_norm, int D, int EX_BITS, float* reuse_space, int BlockSize)
template <unsigned int BlockSize>
__device__ float compute_best_rescale_parallel(float* s_xp_norm, int D, int EX_BITS)
{
using MaxReduce = cub::BlockReduce<float, BlockSize>;
using ArgMaxReduce = cub::BlockReduce<RescaleCandidate, BlockSize>;
Expand All @@ -969,22 +982,17 @@ __device__ float compute_best_rescale_parallel(
constexpr float kEps = 1e-5f;
constexpr int kNEnum = 10;

// Tournament winners are broadcast through a dedicated shared slot (never
// aliased by the workspace and never reused as tournament scratch), so the
// final combine of each argmax tree doubles as the broadcast write and the
// publish/overwrite races need no guard barriers.
__shared__ float s_best_t;

//=========================================================================
// Step 1: Block maximum via warp-shuffle all-reduce. fmaxf is exactly
// associative, so this is bit-identical to the former tree reduction; the
// result lands in every thread, which also keeps the early return uniform.
// Step 1: Block maximum (broadcast keeps the early return uniform)
//=========================================================================
float local_max = 0.0f;
for (int i = tid; i < D; i += BlockSize) {
local_max = fmaxf(local_max, fabsf(s_xp_norm[i]));
}
float max_o = blockAllReduceMax(local_max);
const float block_max = MaxReduce(max_temp).Reduce(local_max, cuda::maximum<>{});
if (tid == 0) { s_max_o = block_max; }
__syncthreads();
float max_o = s_max_o;
if (max_o < kEps) return 1.0f;

//=========================================================================
Expand Down Expand Up @@ -1030,25 +1038,10 @@ __device__ float compute_best_rescale_parallel(
// Publishes s_coarse_t, and lets the fine search reuse argmax_temp.
__syncthreads();

for (int stride = BlockSize / 2; stride > 1; stride >>= 1) {
if (tid < stride) {
if (s_coarse_ip[tid + stride] > s_coarse_ip[tid]) {
s_coarse_ip[tid] = s_coarse_ip[tid + stride];
s_coarse_t[tid] = s_coarse_t[tid + stride];
}
}
__syncthreads();
}
// Final combine writes the winner straight into the broadcast slot; the
// barrier below both completes the tree and publishes s_best_t, and the
// fine phase may then overwrite the tournament scratch freely.
if (tid == 0) { s_best_t = (s_coarse_ip[1] > s_coarse_ip[0]) ? s_coarse_t[1] : s_coarse_t[0]; }
__syncthreads();

//=========================================================================
// Phase 2: Fine search around best coarse point
//=========================================================================
float center_t = s_best_t;
float center_t = s_coarse_t;
float range = (t_end - t_start) / COARSE_SAMPLES;
float fine_start = fmaxf(t_start, center_t - range);
float fine_end = fminf(t_end, center_t + range);
Expand Down Expand Up @@ -1084,23 +1077,7 @@ __device__ float compute_best_rescale_parallel(
.Reduce(RescaleCandidate{best_fine_ip, best_fine_t}, RescaleCandidateMax{});
if (tid == 0) { s_fine_t = fine_best.t; }
__syncthreads();

for (int stride = BlockSize / 2; stride > 1; stride >>= 1) {
if (tid < stride) {
if (s_fine_ip[tid + stride] > s_fine_ip[tid]) {
s_fine_ip[tid] = s_fine_ip[tid + stride];
s_fine_t[tid] = s_fine_t[tid + stride];
}
}
__syncthreads();
}
// Overwriting s_best_t is safe: the fine-publish barrier above ordered
// every thread's read of the coarse center before it. Returning the static
// slot keeps the result immune to workspace reuse in callers (e.g.
// s_tmp_code aliases the workspace in exrabitq_fused_kernel_batch_ori).
if (tid == 0) { s_best_t = (s_fine_ip[1] > s_fine_ip[0]) ? s_fine_t[1] : s_fine_t[0]; }
__syncthreads();
return s_best_t;
return s_fine_t;
}

template <unsigned int BlockSize>
Expand Down Expand Up @@ -1146,13 +1123,8 @@ __global__ void exrabitq_fused_kernel_batch_ori(
// compute_best_rescale_parallel cross-reads every element of s_xp_norm:
// its precondition requires the writes above to be block-visible.
__syncthreads();
// Compute scaling factor (reusing unused buffers)
float const_scaling_factor =
compute_best_rescale_parallel(s_xp_norm,
D,
EX_BITS,
(s_xp_norm + D), // Reused as workspace
BlockSize);
// Compute scaling factor
float const_scaling_factor = compute_best_rescale_parallel<BlockSize>(s_xp_norm, D, EX_BITS);

//=========================================================================
// Part A: ExRaBitQ Code Generation
Expand Down Expand Up @@ -1201,14 +1173,13 @@ __global__ void exrabitq_fused_kernel_batch_ori(
xu_sq += xu * xu;
}

// Fused single-pass reduction of the four factor components: one call over its own
// shared scratch, so no back-to-back scratch reuse and no inter-call __syncthreads.
float reduce_buf[4] = {l2_sqr, ip_resi_xucb, ip_cent_xucb, xu_sq};
blockReduceSumN<4>(reduce_buf);
l2_sqr = reduce_buf[0];
ip_resi_xucb = reduce_buf[1];
ip_cent_xucb = reduce_buf[2];
xu_sq = reduce_buf[3];
// All four factor components in one block reduction (the sums are valid in thread 0 only)
const FloatSumN<4> sums =
FactorReduce(factor_temp).Sum(FloatSumN<4>{{l2_sqr, ip_resi_xucb, ip_cent_xucb, xu_sq}});
l2_sqr = sums.v[0];
ip_resi_xucb = sums.v[1];
ip_cent_xucb = sums.v[2];
xu_sq = sums.v[3];

// Thread 0 computes and writes the final factors
if (tid == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,10 @@ __global__ void exrabitq_quantize_query(
out_ptr[j] = static_cast<int8_t>(code_val);
}

// only thread 0 needs the results; fuse both reductions into one race-free pass
float reduce_buf[2] = {ip_resi_xucb, xu_sq};
blockReduceSumN<2>(reduce_buf);
ip_resi_xucb = reduce_buf[0];
xu_sq = reduce_buf[1];
// only thread 0 needs the results, which is where cub's block reduction leaves them
const FloatSumN<2> sums = FactorReduce(factor_temp).Sum(FloatSumN<2>{{ip_resi_xucb, xu_sq}});
ip_resi_xucb = sums.v[0];
xu_sq = sums.v[1];

// Thread 0 computes and writes the final factors
if (tid == 0) {
Expand Down
88 changes: 13 additions & 75 deletions cpp/src/neighbors/ivf_rabitq/utils/reductions.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -22,84 +22,22 @@ __inline__ __device__ T warpReduceSum(T val)
return val;
}

// Block-level sum reduction of N independent values in a SINGLE pass over a
// private (per-instantiation) shared scratch. Each value gets its own column of
// shared[N][32], so all N are reduced within one write -> __syncthreads() -> read
// window. Because a group of sums is reduced in one call, the scratch is never
// reused by a back-to-back call, so no inter-call barrier is required: the
// shared-memory reuse race is removed by construction rather than patched with a
// guard. On return vals[k] holds the full block sum of the input vals[k] in lane 0
// of warp 0 (other lanes/warps hold partial values), matching blockReduceSum.
//
// Assumes blockDim.x is a multiple of the warp size and uses at most 32 warps.
template <int N, typename T>
__inline__ __device__ void blockReduceSumN(T (&vals)[N])
{
__shared__ T shared[N][32]; // one column per value; up to 1024 threads -> 32 warps
int lane = threadIdx.x & 31;
int wid = threadIdx.x >> 5;

#pragma unroll
for (int k = 0; k < N; ++k) {
vals[k] = warpReduceSum(vals[k]);
}
if (lane == 0) {
// N floats summed component-wise. Passing this to cub::BlockReduce (with its Sum()) performs N
// independent block-wide sums in one collective call: one warp-shuffle tree and one set of barriers
// instead of N of each. Used with cub::BlockReduce<FloatSumN<N>, BlockSize>.
template <int N>
struct FloatSumN {
float v[N];

__device__ __forceinline__ FloatSumN operator+(const FloatSumN& o) const
{
FloatSumN r;
#pragma unroll
for (int k = 0; k < N; ++k) {
shared[k][wid] = vals[k];
r.v[k] = v[k] + o.v[k];
}
return r;
}
__syncthreads();

bool active = threadIdx.x < blockDim.x / 32;
#pragma unroll
for (int k = 0; k < N; ++k) {
T v = active ? shared[k][lane] : T(0);
if (wid == 0) v = warpReduceSum(v);
vals[k] = v;
}
}

// Single-value convenience that routes through the fused family (N = 1). Standalone
// reductions (never called back-to-back over the same scratch) use this and stay
// race-free by construction; N = 1 gives each such site its own shared column,
// distinct from any blockReduceSumN<N != 1> used elsewhere in the same kernel.
template <typename T>
__inline__ __device__ T blockReduceSum(T val)
{
T v[1] = {val};
blockReduceSumN<1>(v);
return v[0];
}

// Block-level max all-reduce: butterfly shuffles + one shared-memory exchange,
// a single __syncthreads(), and EVERY thread receives the full block maximum
// (no separate broadcast barrier needed). Because fmaxf is exactly associative
// and commutative, the result is bit-identical to a tree reduction over the
// same inputs. Uses its own private scratch; the same one-call-per-kernel
// contract as blockReduceSumN applies. Assumes blockDim.x is a multiple of the
// warp size and uses at most 32 warps.
__inline__ __device__ float blockAllReduceMax(float val)
{
__shared__ float shared[32];
int lane = threadIdx.x & 31;
int wid = threadIdx.x >> 5;
int nwarps = blockDim.x >> 5;
const float kNegInf = __int_as_float(0xff800000); // -inf

#pragma unroll
for (int offset = raft::WarpSize / 2; offset > 0; offset >>= 1) {
val = fmaxf(val, __shfl_xor_sync(0xffffffff, val, offset));
}
if (lane == 0) { shared[wid] = val; }
__syncthreads();

float v = (lane < nwarps) ? shared[lane] : kNegInf;
#pragma unroll
for (int offset = raft::WarpSize / 2; offset > 0; offset >>= 1) {
v = fmaxf(v, __shfl_xor_sync(0xffffffff, v, offset));
}
return v;
}
};

} // namespace cuvs::neighbors::ivf_rabitq::detail
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.