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
Add cuvsRabitqRotatorMakeFromState to restore a saved rotator
C callers could save a rotator (GetKind + GetState) but not restore it, while
C++ callers restore one by filling the rotator's public fields. Re-running
cuvsRabitqRotatorMake with the seed used to cover this, but the matmul
rotation now depends on the GPU model, so C needs an explicit restore path to
stay on par with C++.

cuvsRabitqRotatorMakeFromState(res, kind, state, rotator) fills an empty
handle, like cuvsRabitqRotatorMake. It copies `state`, which may live in device
or host memory, into the rotator's own buffers, infers padded_dim from its
shape and rejects state that does not match `kind` (float32 [D, D] for matmul,
uint8 [4 * D / 8] for fht_kac). It synchronizes before returning, so `state`
may be freed right away. The handle, Create and GetKind docs now name it as the
restore half of GetState.

The C API grows from 20 to 21 functions; nothing existing changes.

Tested on an RTX PRO 6000 (sm_120a): RABITQ_C_TEST 2/2 with a new round trip
for both rotator kinds. Restoring from the device state and from a host copy
both rotate bit-identically to the original, and state of the other kind is
rejected. A deliberately broken copy (one element short) fails that check on
both paths. PREPROCESSING_TEST still passes 368/368.
  • Loading branch information
Stardust-SJF committed Oct 29, 2026
commit 3af40d4ecbf2fd320f818304dd7cc9cc7be75bdc
32 changes: 26 additions & 6 deletions c/include/cuvs/preprocessing/quantize/rabitq.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ CUVS_EXPORT cuvsError_t cuvsRabitqQuantizerParamsDestroy(cuvsRabitqQuantizerPara
* queries transformed by the very same rotator. There is deliberately no
* serialization API, so the caller owns this handle and is responsible for
* keeping it alongside the codes, by copying the buffers returned by
* cuvsRabitqRotatorGetState into its own index format. Re-running
* cuvsRabitqRotatorMake with the same params reproduces them only on the same
* GPU model and library version.
* cuvsRabitqRotatorGetState into its own index format and restoring them with
* cuvsRabitqRotatorMakeFromState. Re-running cuvsRabitqRotatorMake with the
* same params reproduces them only on the same GPU model and library version.
*
* A rotator is an independent peer of the quantizer: neither contains the
* other, and a single rotator may be shared by several quantizers.
Expand All @@ -132,8 +132,8 @@ typedef cuvsRabitqRotator* cuvsRabitqRotator_t;
/**
* @brief Allocate a RaBitQ rotator handle
*
* The handle starts out empty; populate it with cuvsRabitqRotatorMake or
* cuvsRabitqPipelineMake before use.
* The handle starts out empty; populate it with cuvsRabitqRotatorMake,
* cuvsRabitqRotatorMakeFromState or cuvsRabitqPipelineMake before use.
*
* @param[in] rotator cuvsRabitqRotator_t to allocate
* @return cuvsError_t
Expand Down Expand Up @@ -202,6 +202,26 @@ CUVS_EXPORT cuvsError_t cuvsRabitqRotatorMake(cuvsResources_t res,
int64_t dim,
cuvsRabitqRotator_t rotator);

/**
* @brief Initializes a RaBitQ rotator from saved state.
*
* The inverse of cuvsRabitqRotatorGetKind + cuvsRabitqRotatorGetState: restores
* a rotator persisted next to the codes it produced. `state` must have the
* shape and dtype cuvsRabitqRotatorGetState documents for `kind`, and the
* padded dimension is taken from it. It is copied, so it may live in device or
* host memory and may be freed once this returns.
*
* @param[in] res raft resource
* @param[in] kind the rotation implementation `state` belongs to
* @param[in] state the saved rotator state
* @param[out] rotator an allocated rotator handle to initialize
* @return cuvsError_t
*/
CUVS_EXPORT cuvsError_t cuvsRabitqRotatorMakeFromState(cuvsResources_t res,
enum cuvsRabitqRotatorKind kind,
DLManagedTensor* state,
cuvsRabitqRotator_t rotator);

/**
* @brief Initializes a RaBitQ quantizer.
*
Expand Down Expand Up @@ -392,7 +412,7 @@ CUVS_EXPORT cuvsError_t cuvsRabitqTransformResidualsFull(cuvsResources_t res,
* Together with cuvsRabitqRotatorGetPaddedDim and cuvsRabitqRotatorGetState
* this is the rotator's full identity. Because RaBitQ has no serialization API,
* reading these three back out is how a C caller persists the rotator next to
* the codes it produced.
* the codes it produced; cuvsRabitqRotatorMakeFromState restores it.
*
* @param[in] rotator a RaBitQ rotator
* @param[out] kind the rotation implementation
Expand Down
67 changes: 67 additions & 0 deletions c/src/preprocessing/quantize/rabitq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <cstdint>
#include <dlpack/dlpack.h>
#include <memory>
#include <optional>
#include <utility>

Expand All @@ -15,6 +16,10 @@
#include "../../core/exceptions.hpp"
#include "../../core/interop.hpp"

#include <raft/core/host_mdspan.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/util/cudart_utils.hpp>

namespace {

namespace rabitq = cuvs::preprocessing::quantize::rabitq;
Expand Down Expand Up @@ -88,6 +93,22 @@ void _expect_float32(DLManagedTensor* tensor, char const* name)
}
}

/**
* Saved rotator state, which may live in device or host memory: it is only read
* to be copied. from_dlpack checks its dtype, rank and row-major layout.
*/
template <typename T, typename Extents>
auto _saved_state(DLManagedTensor* state) -> std::pair<T const*, Extents>
{
if (state == nullptr) { RAFT_FAIL("state must not be NULL"); }
if (cuvs::core::is_dlpack_device_compatible(state->dl_tensor)) {
auto v = cuvs::core::from_dlpack<raft::device_mdspan<T const, Extents, raft::row_major>>(state);
return {v.data_handle(), v.extents()};
}
auto v = cuvs::core::from_dlpack<raft::host_mdspan<T const, Extents, raft::row_major>>(state);
return {v.data_handle(), v.extents()};
}

/** NULL maps to std::nullopt, which both transform() families treat as "no centroid". */
std::optional<centroid_mdspan_type> _optional_centroid(DLManagedTensor* centroid_tensor,
char const* name)
Expand Down Expand Up @@ -263,6 +284,52 @@ extern "C" cuvsError_t cuvsRabitqRotatorMake(cuvsResources_t res,
});
}

extern "C" cuvsError_t cuvsRabitqRotatorMakeFromState(cuvsResources_t res,
enum cuvsRabitqRotatorKind kind,
DLManagedTensor* state,
cuvsRabitqRotator_t rotator)
{
return cuvs::core::translate_exceptions([=] {
if (rotator == nullptr) { RAFT_FAIL("rotator is not allocated"); }
auto res_ptr = reinterpret_cast<raft::resources*>(res);
auto stream = raft::resource::get_cuda_stream(*res_ptr);
auto ret = std::make_unique<rabitq::rotator>(*res_ptr);
switch (kind) {
case CUVS_RABITQ_ROTATOR_KIND_MATMUL: {
auto [src, ext] = _saved_state<float, raft::matrix_extent<int64_t>>(state);
const int64_t D = ext.extent(0);
RAFT_EXPECTS(D > 0 && D % 32 == 0 && ext.extent(1) == D,
"matmul state must be a padded_dim x padded_dim matrix, padded_dim a "
"positive multiple of 32");
ret->kind = rabitq::rotator_kind::matmul;
ret->padded_dim = D;
ret->rotation_matrix =
raft::make_device_matrix<float, int64_t, raft::row_major>(*res_ptr, D, D);
raft::copy(ret->rotation_matrix.data_handle(), src, D * D, stream);
break;
}
case CUVS_RABITQ_ROTATOR_KIND_FHT_KAC: {
auto [src, ext] = _saved_state<uint8_t, raft::vector_extent<int64_t>>(state);
const int64_t bytes = ext.extent(0);
// flip_bits holds 4 * padded_dim / 8 bytes, so padded_dim = 2 * bytes.
RAFT_EXPECTS(bytes > 0 && (2 * bytes) % 32 == 0,
"fht_kac state must hold 4 * padded_dim / 8 bytes, padded_dim a positive "
"multiple of 32");
ret->kind = rabitq::rotator_kind::fht_kac;
ret->padded_dim = 2 * bytes;
ret->flip_bits = raft::make_device_vector<uint8_t, int64_t>(*res_ptr, bytes);
raft::copy(ret->flip_bits.data_handle(), src, bytes, stream);
break;
}
default: RAFT_FAIL("Unsupported rotator kind: %d", static_cast<int>(kind));
}
// `state` may be freed as soon as this returns.
raft::resource::sync_stream(*res_ptr);
delete reinterpret_cast<rabitq::rotator*>(rotator->addr);
rotator->addr = reinterpret_cast<uintptr_t>(ret.release());
});
}

extern "C" cuvsError_t cuvsRabitqQuantizerMake(cuvsResources_t res,
cuvsRabitqQuantizerParams_t params,
int64_t dim,
Expand Down
95 changes: 94 additions & 1 deletion c/tests/preprocessing/rabitq_c.cu
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <raft/core/device_mdspan.hpp>
#include <raft/core/device_resources.hpp>
#include <raft/core/host_mdspan.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/random/rng.cuh>
#include <raft/util/cudart_utils.hpp>
Expand All @@ -17,6 +18,7 @@

#include <cmath>
#include <cstdint>
#include <cstring>
#include <vector>

namespace {
Expand Down Expand Up @@ -58,9 +60,97 @@ constexpr int64_t expected_padded_dim(int64_t dim) { return ((dim + 31) / 32) *
/** Mirrors detail::flip_bytes(): four sign-flip passes of `padded_dim` bits each. */
constexpr int64_t expected_flip_bytes(int64_t padded_dim) { return 4 * padded_dim / 8; }

/**
* Restores `rotator` with cuvsRabitqRotatorMakeFromState, once from its device
* state view and once from a host copy of it, and checks that both restored
* rotators rotate bit-identically to the original. State of the other kind
* must be rejected.
*/
void check_restore(raft::device_resources const& handle,
cuvsResources_t res,
cuvsRabitqRotator_t rotator,
enum cuvsRabitqRotatorKind kind,
DLManagedTensor* state,
int64_t padded_dim)
{
auto stream = raft::resource::get_cuda_stream(handle);
constexpr int64_t n_rows = 16;
rmm::device_uvector<float> in(n_rows * padded_dim, stream);
rmm::device_uvector<float> ref(n_rows * padded_dim, stream);
rmm::device_uvector<float> got(n_rows * padded_dim, stream);
raft::random::RngState rng(77ULL);
raft::random::uniform(handle, rng, in.data(), in.size(), -1.0f, 1.0f);
handle.sync_stream();
auto in_t = make_row_major_tensor(in.data(), n_rows, padded_dim);
auto ref_t = make_row_major_tensor(ref.data(), n_rows, padded_dim);
auto got_t = make_row_major_tensor(got.data(), n_rows, padded_dim);
ASSERT_EQ(cuvsRabitqRotate(res, rotator, &in_t, &ref_t), CUVS_SUCCESS);
ASSERT_EQ(cuvsStreamSync(res), CUVS_SUCCESS);
auto h_ref = to_host(handle, ref.data(), ref.size());

// A host copy of the state, as a caller would load it from its own storage.
const bool matmul = kind == CUVS_RABITQ_ROTATOR_KIND_MATMUL;
std::vector<float> h_matrix;
std::vector<uint8_t> h_flip;
DLManagedTensor host_state{};
if (matmul) {
h_matrix = to_host(
handle, static_cast<float const*>(state->dl_tensor.data), padded_dim * padded_dim);
cuvs::core::to_dlpack(raft::make_host_matrix_view<float, int64_t, raft::row_major>(
h_matrix.data(), padded_dim, padded_dim),
&host_state);
} else {
h_flip = to_host(handle,
static_cast<uint8_t const*>(state->dl_tensor.data),
static_cast<size_t>(expected_flip_bytes(padded_dim)));
cuvs::core::to_dlpack(
raft::make_host_vector_view<uint8_t, int64_t>(h_flip.data(), expected_flip_bytes(padded_dim)),
&host_state);
}

// Both restored rotators stay alive until the end, so the host-path check
// cannot reuse the device-path rotator's freed buffer.
std::vector<cuvsRabitqRotator_t> restored_rotators;
for (DLManagedTensor* saved : {state, &host_state}) {
cuvsRabitqRotator_t restored;
ASSERT_EQ(cuvsRabitqRotatorCreate(&restored), CUVS_SUCCESS);
restored_rotators.push_back(restored);
ASSERT_EQ(cuvsRabitqRotatorMakeFromState(res, kind, saved, restored), CUVS_SUCCESS);
enum cuvsRabitqRotatorKind read_kind = matmul ? CUVS_RABITQ_ROTATOR_KIND_FHT_KAC
: CUVS_RABITQ_ROTATOR_KIND_MATMUL;
int64_t read_padded_dim = 0;
ASSERT_EQ(cuvsRabitqRotatorGetKind(restored, &read_kind), CUVS_SUCCESS);
ASSERT_EQ(cuvsRabitqRotatorGetPaddedDim(restored, &read_padded_dim), CUVS_SUCCESS);
EXPECT_EQ(read_kind, kind);
EXPECT_EQ(read_padded_dim, padded_dim);

ASSERT_EQ(cuvsRabitqRotate(res, restored, &in_t, &got_t), CUVS_SUCCESS);
ASSERT_EQ(cuvsStreamSync(res), CUVS_SUCCESS);
auto h_got = to_host(handle, got.data(), got.size());
EXPECT_EQ(std::memcmp(h_got.data(), h_ref.data(), h_ref.size() * sizeof(float)), 0)
<< "restored rotator (from " << (saved == state ? "device" : "host")
<< " state) does not rotate like the original";
}
for (auto restored : restored_rotators) {
EXPECT_EQ(cuvsRabitqRotatorDestroy(restored), CUVS_SUCCESS);
}

// State that does not match `kind` is rejected.
cuvsRabitqRotator_t mismatched;
ASSERT_EQ(cuvsRabitqRotatorCreate(&mismatched), CUVS_SUCCESS);
auto other_kind = matmul ? CUVS_RABITQ_ROTATOR_KIND_FHT_KAC : CUVS_RABITQ_ROTATOR_KIND_MATMUL;
EXPECT_NE(cuvsRabitqRotatorMakeFromState(res, other_kind, state, mismatched), CUVS_SUCCESS);
EXPECT_EQ(cuvsRabitqRotatorDestroy(mismatched), CUVS_SUCCESS);

free_tensor(host_state);
free_tensor(in_t);
free_tensor(ref_t);
free_tensor(got_t);
}

/**
* Exercises the whole C surface for one rotator kind: params -> pipeline ->
* getters -> transform -> state readback -> teardown.
* getters -> transform -> state readback -> restore -> teardown.
*/
void run_rabitq_c_smoke(enum cuvsRabitqRotatorKind kind)
{
Expand Down Expand Up @@ -239,6 +329,9 @@ void run_rabitq_c_smoke(enum cuvsRabitqRotatorKind kind)
EXPECT_GT(n_set_bits, 0u);
EXPECT_LT(n_set_bits, h_state.size() * 8);
}

// --- restore from the saved state ---
check_restore(handle, res, rotator, kind, &state, padded_dim);
free_tensor(state);

// --- teardown ---
Expand Down
Loading