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
Batch RaBitQ transform() internally, sized to the L2 cache
Following review feedback, transform() no longer quantizes the whole dataset
in one pass with an n_rows x padded_dim residual scratch: both overloads now
loop over row batches, handing detail::quantize_data / quantize_data_full
offset pointers into the caller's input and outputs. The detail pipeline is
untouched, and rows are quantized independently, so batching only bounds the
scratch.

The batch size (transform_batch_rows) is chosen for speed, not only memory.
Every stage after the input read (pad, rotate, centroid subtraction, quantize)
sweeps the batch's residual scratch, so keeping it L2-resident is faster than
one unbatched pass:
- fht_kac, and matmul with padded_dim <= 256: the scratch is sized to ~3/8 of
  the L2 cache, clamped to [2048, 65536] rows ([2048, 32768] for matmul) and
  rounded down to a power of two (64K rows up to dim 128, 32K at 384, 8K
  around 1000, 4K at 3072 on a 128 MB L2).
- wider matmul is GEMM-bound and runs best on large batches: 131072 rows.
- as in IVF-PQ's extend, the batch is then halved (down to 128 rows) until
  its scratch fits half of the free workspace; the other half covers the
  rotated centroid, the matmul padding temporary and fragmentation.
The constants come from a sweep with one process per measurement; comparing
batch sizes inside a single process made the unbatched call look 10-15%
slower than it is.

transform() no longer needs the n_rows <= INT_MAX check, since every kernel
launch now sees at most one batch. The docs drop the advice to batch by hand.

Tested on an RTX PRO 6000 (sm_120a, 128 MB L2):
- PREPROCESSING_TEST 369/369 (a new test checks that batched fht_kac output is
  bit-identical to rotate() + transform_residuals() over all rows, across
  several batches plus a short tail that runs the other FHT kernel, both
  output modes, with and without a centroid), RABITQ_C_TEST 2/2.
- Output byte-identical to the previous commit for 40 configurations at
  200000 rows (fht_kac and matmul, dims 100/128/960/1000/3000).
- One transform() over a ~2 GiB dataset, 56 configurations, fresh processes
  x3: fht_kac 1.33x faster (geomean; 1.08-1.68x), matmul 1.05x (0.97-1.29x;
  dim 384 without a centroid is the one 3% regression).
- With a 256 MiB workspace and a ~2 GiB dataset the previous commit runs out
  of memory; this one completes, for both rotators with and without centroid.
  • Loading branch information
Stardust-SJF committed Oct 29, 2026
commit 33020da93a3033b91a048d2cbceee4c623a916dc
12 changes: 6 additions & 6 deletions c/include/cuvs/preprocessing/quantize/rabitq.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ CUVS_EXPORT cuvsError_t cuvsRabitqRotate(cuvsResources_t res,
* `cuvsRabitqQuantizerParams::ex_bits + 1` bit value, so `codes` must be
* `n_rows x padded_dim`.
*
* A temporary `n_rows x padded_dim` float buffer holding the rotated residuals
* is allocated from the workspace resource, so large datasets should be
* transformed in batches (a pool workspace resource is recommended).
* Rows are processed in internal batches, so the temporary float buffer holding
* the rotated residuals, allocated from the workspace resource, covers one batch
* rather than the whole dataset (a pool workspace resource is recommended).
*
* @param[in] res raft resource
* @param[in] quantizer a RaBitQ quantizer
Expand Down Expand Up @@ -328,9 +328,9 @@ CUVS_EXPORT cuvsError_t cuvsRabitqTransform(cuvsResources_t res,
* used for approximate-distance estimation during search:
* `factors[i] = { f_add, f_rescale, f_error }`.
*
* A temporary `n_rows x padded_dim` float buffer holding the rotated residuals
* is allocated from the workspace resource, so large datasets should be
* transformed in batches (a pool workspace resource is recommended).
* Rows are processed in internal batches, so the temporary float buffer holding
* the rotated residuals, allocated from the workspace resource, covers one batch
* rather than the whole dataset (a pool workspace resource is recommended).
*
* The rotated centroid is an internal temporary as well. A search
* implementation that needs it can reproduce it exactly by zero-padding
Expand Down
12 changes: 6 additions & 6 deletions cpp/include/cuvs/preprocessing/quantize/rabitq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ void rotate(raft::resources const& res,
* is written per padded dimension; each holds an unsigned
* `params::ex_bits + 1` bit value, so `codes` must be `n_rows x padded_dim`.
*
* A temporary `n_rows x padded_dim` float buffer holding the rotated residuals
* is allocated from the workspace resource, so large datasets should be
* transformed in batches (a pool workspace resource is recommended).
* Rows are processed in internal batches, so the temporary float buffer holding
* the rotated residuals, allocated from the workspace resource, covers one batch
* rather than the whole dataset (a pool workspace resource is recommended).
Comment thread
Stardust-SJF marked this conversation as resolved.
Resolved
*
* Usage example:
* @code{.cpp}
Expand Down Expand Up @@ -335,9 +335,9 @@ void transform(raft::resources const& res,
* ones used for approximate-distance estimation during search:
* `factors[i] = { f_add, f_rescale, f_error }`.
*
* A temporary `n_rows x padded_dim` float buffer holding the rotated residuals
* is allocated from the workspace resource, so large datasets should be
* transformed in batches (a pool workspace resource is recommended).
* Rows are processed in internal batches, so the temporary float buffer holding
* the rotated residuals, allocated from the workspace resource, covers one batch
* rather than the whole dataset (a pool workspace resource is recommended).
*
* The rotated centroid is an internal temporary as well. A search implementation
* that needs it can reproduce it exactly by zero-padding `centroid` to
Expand Down
134 changes: 95 additions & 39 deletions cpp/src/preprocessing/quantize/rabitq.cu
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@

#include <cuvs/preprocessing/quantize/rabitq.hpp>

#include <raft/util/cuda_rt_essentials.hpp>
#include <raft/core/device_mdarray.hpp>
#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/util/cuda_rt_essentials.hpp>

#include <rmm/device_uvector.hpp>

#include <cuda_runtime.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
Expand Down Expand Up @@ -121,6 +123,52 @@ void check_factors(int64_t n_rows,
"rabitq: factors must be a n_rows x 3 matrix");
}

/**
* Rows per internal transform() batch.
Comment thread
achirkin marked this conversation as resolved.
Resolved
*
* Every stage after the input read (pad, rotate, centroid subtraction, quantize)
* sweeps the batch's `batch x padded_dim` residual scratch, so a batch whose
* scratch fits in ~3/8 of the L2 cache keeps those passes in L2 instead of DRAM:
* 1.1-1.7x faster than one unbatched call for fht_kac on an RTX PRO 6000 (128 MB
* L2). Below 2K rows the fixed per-batch cost outweighs that, and beyond 64K rows
* (32K for a narrow matmul) nothing more is gained. A matmul rotator wider than
* 256 is GEMM-bound instead and runs best on large batches. As in
* IVF-PQ's extend, the batch is then halved until its scratch fits the free
* workspace, here half of it: the rest covers the smaller temporaries (rotated
* centroid, padding) and fragmentation.
*/
[[nodiscard]] auto transform_batch_rows(raft::resources const& res,
Comment thread
tfeher marked this conversation as resolved.
Resolved
rabitq::quantizer const& q,
rabitq::rotator const& rot,
int64_t n_rows) -> int64_t
{
constexpr int64_t kMinL2Rows = 2048;
constexpr int64_t kMaxL2Rows = 65536;
constexpr int64_t kMaxL2RowsMatmul = 32768;
constexpr int64_t kGemmRows = 131072;
constexpr int64_t kMinWorkspaceRows = 128;

const bool matmul = rot.kind == rotator_kind::matmul;
const size_t row_floats = static_cast<size_t>(q.padded_dim);
int64_t batch = kGemmRows;
if (!matmul || q.padded_dim <= 256) {
const size_t l2 = raft::resource::get_device_properties(res).l2CacheSize;
const auto fit = static_cast<int64_t>(l2 * 3 / 8 / (row_floats * sizeof(float)));
const auto cap = matmul ? kMaxL2RowsMatmul : kMaxL2Rows;
batch = kMinL2Rows;
while (batch * 2 <= std::min(fit, cap)) {
batch *= 2;
}
}
// matmul pads into a second batch x padded_dim temporary (pad_and_rotate).
const size_t row_bytes = row_floats * sizeof(float) * (matmul && q.dim != q.padded_dim ? 2 : 1);
const size_t budget = raft::resource::get_workspace_free_bytes(res) / 2;
while (batch > kMinWorkspaceRows && static_cast<size_t>(batch) * row_bytes > budget) {
batch /= 2;
}
return std::min(batch, n_rows);
}

template <typename CodeT>
void transform_impl(raft::resources const& res,
rabitq::quantizer const& q,
Expand All @@ -133,7 +181,6 @@ void transform_impl(raft::resources const& res,
{
const int64_t n_rows = dataset.extent(0);
check_peers(q, rot);
check_rows(n_rows);
RAFT_EXPECTS(dataset.extent(1) == q.dim,
"rabitq::transform: dataset.extent(1) must equal quantizer.dim");
check_codes(q, n_rows, codes);
Expand All @@ -144,30 +191,35 @@ void transform_impl(raft::resources const& res,
}
if (n_rows == 0) { return; }

auto stream = raft::resource::get_cuda_stream(res);
auto workspace = raft::resource::get_workspace_resource_ref(res);
auto stream = raft::resource::get_cuda_stream(res);
auto workspace = raft::resource::get_workspace_resource_ref(res);
const int64_t batch = transform_batch_rows(res, q, rot, n_rows);
rmm::device_uvector<float> residuals(
static_cast<size_t>(n_rows) * static_cast<size_t>(q.padded_dim), stream, workspace);
static_cast<size_t>(batch) * static_cast<size_t>(q.padded_dim), stream, workspace);
rmm::device_uvector<float> rotated_centroid(
centroid.has_value() ? static_cast<size_t>(q.padded_dim) : 0, stream, workspace);

detail::quantize_data(res,
dataset.data_handle(),
static_cast<size_t>(n_rows),
static_cast<size_t>(q.dim),
as_ref(rot),
centroid.has_value() ? centroid->data_handle() : nullptr,
centroid.has_value() ? rotated_centroid.data() : nullptr,
static_cast<size_t>(q.params_quantizer.ex_bits),
q.const_scaling_factor,
q.params_quantizer.use_fast,
codes.data_handle(),
delta.data_handle(),
vl.data_handle(),
residuals.data(),
static_cast<int>(q.params_quantizer.delta_mode),
static_cast<int>(q.params_quantizer.coarse_samples),
static_cast<int>(q.params_quantizer.fine_samples));
// Rows are quantized independently, so batching only bounds the scratch.
for (int64_t r0 = 0; r0 < n_rows; r0 += batch) {
const int64_t n = std::min(batch, n_rows - r0);
detail::quantize_data(res,
dataset.data_handle() + r0 * q.dim,
static_cast<size_t>(n),
static_cast<size_t>(q.dim),
as_ref(rot),
centroid.has_value() ? centroid->data_handle() : nullptr,
centroid.has_value() ? rotated_centroid.data() : nullptr,
static_cast<size_t>(q.params_quantizer.ex_bits),
q.const_scaling_factor,
q.params_quantizer.use_fast,
codes.data_handle() + r0 * q.padded_dim,
delta.data_handle() + r0,
vl.data_handle() + r0,
residuals.data(),
static_cast<int>(q.params_quantizer.delta_mode),
static_cast<int>(q.params_quantizer.coarse_samples),
static_cast<int>(q.params_quantizer.fine_samples));
}
}

template <typename CodeT>
Expand All @@ -181,7 +233,6 @@ void transform_full_impl(raft::resources const& res,
{
const int64_t n_rows = dataset.extent(0);
check_peers(q, rot);
check_rows(n_rows);
RAFT_EXPECTS(dataset.extent(1) == q.dim,
"rabitq::transform: dataset.extent(1) must equal quantizer.dim");
check_codes(q, n_rows, codes);
Expand All @@ -192,28 +243,33 @@ void transform_full_impl(raft::resources const& res,
}
if (n_rows == 0) { return; }

auto stream = raft::resource::get_cuda_stream(res);
auto workspace = raft::resource::get_workspace_resource_ref(res);
auto stream = raft::resource::get_cuda_stream(res);
auto workspace = raft::resource::get_workspace_resource_ref(res);
const int64_t batch = transform_batch_rows(res, q, rot, n_rows);
rmm::device_uvector<float> residuals(
static_cast<size_t>(n_rows) * static_cast<size_t>(q.padded_dim), stream, workspace);
static_cast<size_t>(batch) * static_cast<size_t>(q.padded_dim), stream, workspace);
// The full-factor formula always reads the rotated centroid; the detail layer
// zero-fills this buffer when no centroid is given.
rmm::device_uvector<float> rotated_centroid(
static_cast<size_t>(q.padded_dim), stream, workspace);

detail::quantize_data_full(res,
dataset.data_handle(),
static_cast<size_t>(n_rows),
static_cast<size_t>(q.dim),
as_ref(rot),
centroid.has_value() ? centroid->data_handle() : nullptr,
rotated_centroid.data(),
static_cast<size_t>(q.params_quantizer.ex_bits),
q.const_scaling_factor,
q.params_quantizer.use_fast,
codes.data_handle(),
factors.data_handle(),
residuals.data());
// Rows are quantized independently, so batching only bounds the scratch.
for (int64_t r0 = 0; r0 < n_rows; r0 += batch) {
const int64_t n = std::min(batch, n_rows - r0);
detail::quantize_data_full(res,
dataset.data_handle() + r0 * q.dim,
static_cast<size_t>(n),
static_cast<size_t>(q.dim),
as_ref(rot),
centroid.has_value() ? centroid->data_handle() : nullptr,
rotated_centroid.data(),
static_cast<size_t>(q.params_quantizer.ex_bits),
q.const_scaling_factor,
q.params_quantizer.use_fast,
codes.data_handle() + r0 * q.padded_dim,
factors.data_handle() + r0 * kNumFactors,
residuals.data());
}
}

template <typename CodeT>
Expand Down
102 changes: 101 additions & 1 deletion cpp/tests/preprocessing/rabitq_quantization.cu
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
#include <raft/core/device_mdarray.hpp>
#include <raft/core/error.hpp>
#include <raft/core/mdspan.hpp>
#include <raft/core/operators.hpp>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/matrix/linewise_op.cuh>
#include <raft/util/itertools.hpp>

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <optional>
#include <ostream>
#include <vector>
Expand All @@ -31,7 +34,8 @@
* - that transform() emits in-range, not-all-zero codes and finite factors;
* - that transform() is deterministic;
* - that transform() agrees with rotate() + transform_residuals() whenever no
* zero padding is involved (dim % 32 == 0).
* zero padding is involved (dim % 32 == 0), and for fht_kac also across its
* internal row batches (a matmul GEMM's last bits depend on its batch size).
*/

namespace cuvs::preprocessing::quantize::rabitq {
Expand Down Expand Up @@ -705,4 +709,100 @@ TEST(RabitqQuantizationBasicTest, EmptyInput)
raft::resource::sync_stream(handle);
}

/**
* transform() quantizes in internal row batches. Rows are processed independently
* and the two FHT kernels agree, so for fht_kac the result must be bit-identical
* to one unbatched pass: rotate() over all rows, subtract the rotated centroid,
* then transform_residuals(). kRows spans several batches plus a short tail, which
* runs the other FHT kernel than the reference does.
*/
TEST(RabitqQuantizationBasicTest, BatchedTransformMatchesUnbatched)
{
raft::resources handle;
auto stream = raft::resource::get_cuda_stream(handle);
constexpr int64_t kRows = 2 * 32768 + 123;

auto to_host = [&](auto const* d_ptr, size_t len) {
std::vector<std::remove_const_t<std::remove_pointer_t<decltype(d_ptr)>>> h(len);
raft::update_host(h.data(), d_ptr, len, stream);
raft::resource::sync_stream(handle);
return h;
};
auto expect_same = [&](auto const& a, auto const& b, char const* what, int64_t dim, bool c) {
ASSERT_EQ(a.size(), b.size());
EXPECT_EQ(std::memcmp(a.data(), b.data(), a.size() * sizeof(a[0])), 0)
<< what << " of batched transform() differ from the unbatched reference (dim " << dim
<< (c ? ", with centroid)" : ", no centroid)");
};

for (int64_t dim : {96, 128, 960}) {
params p;
p.rotator = rotator_kind::fht_kac;
auto pipe = make_pipeline(handle, p, dim);
const auto pd = pipe.quantizer.padded_dim;
ASSERT_EQ(pd, dim) << "the reference path needs dim % 32 == 0";

auto dataset = raft::make_device_matrix<float, int64_t>(handle, kRows, dim);
auto centroid = raft::make_device_vector<float, int64_t>(handle, dim);
raft::random::RngState r(99ULL);
raft::random::uniform(handle, r, dataset.data_handle(), dataset.size(), -1.0f, 1.0f);
raft::random::uniform(handle, r, centroid.data_handle(), centroid.size(), -0.5f, 0.5f);
auto data = raft::make_const_mdspan(dataset.view());

// Unbatched reference residuals: rotate(x) and rotate(x) - rotate(c).
auto rotated = raft::make_device_matrix<float, int64_t>(handle, kRows, pd);
rotate(handle, pipe.rotator, data, rotated.view());
auto rotated_c = raft::make_device_matrix<float, int64_t>(handle, 1, pd);
rotate(handle,
pipe.rotator,
raft::make_device_matrix_view<const float, int64_t>(centroid.data_handle(), 1, pd),
rotated_c.view());
auto rotated_c_view =
raft::make_device_vector_view<const float, int64_t>(rotated_c.data_handle(), pd);
auto centered = raft::make_device_matrix<float, int64_t>(handle, kRows, pd);
raft::matrix::linewise_op<raft::Apply::ALONG_ROWS>(handle,
raft::make_const_mdspan(rotated.view()),
centered.view(),
raft::sub_op{},
rotated_c_view);

for (bool with_c : {false, true}) {
auto ref_in = raft::make_const_mdspan(with_c ? centered.view() : rotated.view());
std::optional<raft::device_vector_view<const float, int64_t>> c, ref_c;
if (with_c) {
c = raft::make_const_mdspan(centroid.view());
ref_c = rotated_c_view;
}
const size_t n_codes = static_cast<size_t>(kRows) * static_cast<size_t>(pd);

// (delta, vl) mode
auto codes = raft::make_device_matrix<uint8_t, int64_t>(handle, kRows, pd);
auto delta = raft::make_device_vector<float, int64_t>(handle, kRows);
auto vl = raft::make_device_vector<float, int64_t>(handle, kRows);
transform(
handle, pipe.quantizer, pipe.rotator, data, c, codes.view(), delta.view(), vl.view());
auto h_codes = to_host(codes.data_handle(), n_codes);
auto h_delta = to_host(delta.data_handle(), kRows);
auto h_vl = to_host(vl.data_handle(), kRows);
transform_residuals(handle, pipe.quantizer, ref_in, codes.view(), delta.view(), vl.view());
expect_same(h_codes, to_host(codes.data_handle(), n_codes), "codes", dim, with_c);
expect_same(h_delta, to_host(delta.data_handle(), kRows), "delta", dim, with_c);
expect_same(h_vl, to_host(vl.data_handle(), kRows), "vl", dim, with_c);

// full-factor mode
auto factors = raft::make_device_matrix<float, int64_t>(handle, kRows, 3);
transform(handle, pipe.quantizer, pipe.rotator, data, c, codes.view(), factors.view());
h_codes = to_host(codes.data_handle(), n_codes);
auto h_factors = to_host(factors.data_handle(), static_cast<size_t>(kRows) * 3);
transform_residuals(handle, pipe.quantizer, ref_in, ref_c, codes.view(), factors.view());
expect_same(h_codes, to_host(codes.data_handle(), n_codes), "full-factor codes", dim, with_c);
expect_same(h_factors,
to_host(factors.data_handle(), static_cast<size_t>(kRows) * 3),
"factors",
dim,
with_c);
}
}
}

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