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
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
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
|
|
@@ -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, | ||
|
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. 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
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 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; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.