Repository navigation
Preserve base offsets when slicing already-sliced patches on CUDA - #12313
lwwmanning wants to merge 6 commits into
Conversation
`slice_device_patches` previously reset `offset_within_chunk` to 0 and `indices_base` to `Some(0)`, discarding the initial `chunk_offsets[0]` and `offset_within_chunk` of patches that were already sliced before loading to the GPU. It also derived chunk offsets from `patches.offset` rather than `device_patches.offset`, breaking repeated slicing, and left `chunk_offsets` and `n_chunks` intact for empty or out-of-range slices. Capture the initial `chunk_offsets[0]` base in `load_device_patches` only when slicing is requested (reading directly from host memory or fetching only element 0 via `try_to_host` when device-resident past chunk 0), preserve `chunk_offsets[0] + offset_within_chunk` as `indices_base` across `slice_device_patches` calls relative to `device_patches.offset`, and clear `chunk_offsets`, `n_chunks`, and `num_patches` for empty or out-of-range slices. Signed-off-by: Will Manning <will@willmanning.io>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Merging this PR will degrade performance by 15.43%Warning vortex-data has exceeded the Hobby plan limit of 5 active contributors. Upgrade your plan or apply to the open source program by December 8, 2026 to keep using CodSpeed. 2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
lwwmanning
left a comment
There was a problem hiding this comment.
Posted by Claude Opus 5.5 on Will's behalf.
The base fix looks right to me. The new offset and chunk window match Patches::slice, and the CUDA cursor accepts the new base. I have two requests.
First, please remove the offset < PATCH_CHUNK_SIZE shortcut in read_first_chunk_offset, as Codex suggests. Patches::new does not check that chunk_offsets[0] is 0. So the shortcut depends on a rule that nothing enforces, and it saves a read of one element.
Second, the change could be smaller. Could the loader set indices_base from the first chunk offset when it loads sliced patches? That would replace the chunk_offsets_base field and the bool parameter on the three callers.
In the tests, test_slice_device_patches_back_to_back covers a GPU slice of a GPU slice, which no caller does. The out_of_range_slice_of_slice case uses a range past the array length. simulate_gpu_patches_in_range copies the kernel logic into Rust and can drift from it. One CUDA test that decodes a sliced BitPacked array with CPU-sliced patches, compared with the CPU result, would cover more.
Signed-off-by: Will Manning <will@spiraldb.com> Co-Authored-By: Will Manning <will@spiraldb.com>
|
Addressed both requests in
|
…comments Signed-off-by: Will Manning <will@spiraldb.com> Co-Authored-By: Will Manning <will@spiraldb.com>
…hunks Signed-off-by: Will Manning <will@spiraldb.com> Co-Authored-By: Will Manning <will@spiraldb.com>
lwwmanning
left a comment
There was a problem hiding this comment.
Review by Gemini 4 Carbon on behalf of @lwwmanning.
The chunk-window math in slice_device_patches and the end-to-end BitPacked regression test look right. One performance/correctness issue remains in load_device_patches:
Unconditional GPU-to-host readback on unsliced patch loads (vortex-cuda/src/kernel/patches/types.rs)
load_device_patches now awaits read_first_chunk_offset and sets indices_base: Some(indices_base) on every load—even when no GPU slice follows (ALPExecutor and ALPRDExecutor never call slice_device_patches, and unsliced BitPackedExecutor and PlanPatches have patch_range == None).
Whenever chunk_offsets is device-resident—both in GPU file scans (PooledFileReadAt / CopyDeviceReadAt) and whenever chunk_offsets is compressed (e.g. BitPacked, decoded onto the GPU by co.clone().execute_cuda(ctx).await? right above)—read_first_chunk_offset executes chunk_offsets.slice(0..width).try_to_host()?.await?, stalling the host task on a D2H copy + stream callback before the decode kernel can be enqueued. On unsliced paths, PatchesCursor in patches.cuh already computes load_chunk_offset(patches, 0) + patches.offset_within_chunk on the GPU via PATCH_DERIVE_INDICES_BASE; unconditionally setting Some(indices_base) in load_device_patches also turns DevicePatches::indices_base = None and PATCH_DERIVE_INDICES_BASE into unreachable dead code.
Remediation Plan
vortex-cuda/src/kernel/patches/types.rs(load_device_patches): Leaveload_device_patchesunchanged fromdevelop(indices_base: None, noread_first_chunk_offsetcall) so unslicedBitPacked,ALP,ALP-RD, andPlanPatchesloads keep deriving the base on the GPU viaPATCH_DERIVE_INDICES_BASEwith zero host readbacks.vortex-cuda/src/kernel/patches/types.rs(slice_device_patchesandload_patches_to_gpu) &vortex-cuda/src/kernel/encodings/bitpacked.rs(decode_bitpacked): Makeslice_device_patchespub(crate) async fn slice_device_patches(patch_range: Range<usize>, device_patches: &mut DevicePatches) -> VortexResult<()>(both callers—decode_bitpackedandload_patches_to_gpu—are alreadyasync fns). Insideslice_device_patches, for non-empty slices (start < end), computeindices_baseviaread_first_chunk_offset(&device_patches.chunk_offsets, device_patches.chunk_offset_ptype, device_patches.n_chunks).await?+device_patches.offset_within_chunkbefore slicingdevice_patches.chunk_offsets(whendevice_patches.indices_base.is_none()), and setindices_base: Some(0)on the empty/start >= endbranches.- Tests & PR description: Update
test_load_device_patches_nonzero_base_below_chunk_size(and.await?onslice_device_patchescall sites inpatches/types.rstests) to assertindices_baseisNoneafterload_device_patchesandSome(6)afterslice_device_patches, and update the PR description to state that the 1-element D2H read only runs when GPU-slicing device-residentchunk_offsets.
Signed-off-by: Will Manning <will@spiraldb.com> Co-Authored-By: Will Manning <will@spiraldb.com>
|
Addressed in
|
lwwmanning
left a comment
There was a problem hiding this comment.
Posted by Claude Opus 5.5 on Will's behalf.
I reviewed a5baa7e. load_device_patches is back to develop, so unsliced loads do no readback. The base math in slice_device_patches is right.
- vortex-cuda/src/kernel/patches/types.rs line 154:
load_device_patchesuploadschunk_offsetsbeforeslice_device_patchesruns. Soread_first_chunk_offsetalways sees a device buffer. Its host branch (line 80) never runs outside the tests. Every GPU slice that keeps a patch chunk waits on a device copy (line 88). At 12cefbb, offsets that came from the host were read on the host at no cost. The description says the read happens only for device-residentchunk_offsets, which is no longer true. - A small fix: in
load_device_patches, before the upload, setindices_basewhenchunk_offsetsis still on the host. Use the first chunk offset plusoffset_within_chunk, and leave itNoneotherwise. Unsliced device loads keep the GPU derivation.slice_device_patchesthen reads from the device only when the offsets started there. Thecross_chunk_hostandcross_chunk_device_chunk_offsetscases (lines 402 and 403) would then test different paths again. Today both go through the device read.
…ice_patches Signed-off-by: Will Manning <will@spiraldb.com> Co-Authored-By: Will Manning <will@spiraldb.com>
|
Addressed in |
This fixes the GPU slice of BitPacked patches in
vortex-cuda(load_device_patchesandslice_device_patches) when the CPU has already sliced the patches. CPU patch slicing and the CUDA kernels are unchanged.A CPU slice of
Patchestrimsindicesandvalues, but itschunk_offsetsentries still count from the start of the original indices. To find positions in the trimmed buffers, the kernel subtractsindices_base, which is the first chunk offset plusoffset_within_chunk. On develop,slice_device_patchessetsindices_baseto zero. So when the GPU slices patches that the CPU already sliced, the kernel reads the wrong positions and can apply wrong values or skip patches.With this PR,
load_device_patchescapturesindices_basefor free whenchunk_offsetsis still on the host before upload, and leaves it asNonewhenchunk_offsetsis already on the device so unsliced device loads keep deriving the base on the GPU with no host readback.slice_device_patchesis nowasyncand, for non-empty slices whereindices_baseis stillNone, performs the 1-element D2H read before slicingchunk_offsets.slice_device_patchesno longer takes the hostPatches, and an empty slice range gives no patches. The PR adds unit tests for the patch slice and a CUDA test that decodes a GPU slice of a CPU-sliced BitPacked array.Assisted by Antigravity and Devin.