Skip to content

Preserve base offsets when slicing already-sliced patches on CUDA - #12313

Open
lwwmanning wants to merge 6 commits into
developfrom
wm/fix-cuda-sliced-device-patches
Open

lwwmanning wants to merge 6 commits into
developfrom
wm/fix-cuda-sliced-device-patches

Conversation

@lwwmanning

@lwwmanning lwwmanning commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

This fixes the GPU slice of BitPacked patches in vortex-cuda (load_device_patches and slice_device_patches) when the CPU has already sliced the patches. CPU patch slicing and the CUDA kernels are unchanged.

A CPU slice of Patches trims indices and values, but its chunk_offsets entries still count from the start of the original indices. To find positions in the trimmed buffers, the kernel subtracts indices_base, which is the first chunk offset plus offset_within_chunk. On develop, slice_device_patches sets indices_base to 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_patches captures indices_base for free when chunk_offsets is still on the host before upload, and leaves it as None when chunk_offsets is already on the device so unsliced device loads keep deriving the base on the GPU with no host readback. slice_device_patches is now async and, for non-empty slices where indices_base is still None, performs the 1-element D2H read before slicing chunk_offsets. slice_device_patches no longer takes the host Patches, 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.

`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>
@lwwmanning lwwmanning added the changelog/fix A bug fix label Nov 29, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T13:56:28.972853Z 5ab75ce PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@codspeed

codspeed Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

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

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

⚠️ 2 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

❌ 1 regressed benchmark
✅ 2239 untouched benchmarks
⏩ 101 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Walltime cuda/bitpacked_u8/unpack/3bw[100M] 301.7 µs 356.7 µs -15.43%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing wm/fix-cuda-sliced-device-patches (12cefbb) with develop (4525dfa)

Open in CodSpeed

Footnotes

  1. 101 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@lwwmanning lwwmanning left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Addressed both requests in 020a25a71:

  • Removed the offset < PATCH_CHUNK_SIZE shortcut in read_first_chunk_offset and added a CUDA test for device-resident chunk_offsets with offset < PATCH_CHUNK_SIZE and a nonzero base.
  • Simplified load_device_patches to compute indices_base from chunk_offsets[0] + offset_within_chunk directly, removing DevicePatches::chunk_offsets_base and the need_chunk_offsets_base bool parameter from all callers.
  • Removed simulate_gpu_patches_in_range, test_slice_device_patches_back_to_back, and out_of_range_slice_of_slice, consolidating unit tests to 10 new cases total and adding test_cuda_bitunpack_gpu_slice_of_cpu_sliced_patches to test sliced BitPacked decoding with CPU-sliced patches end to end against CPU.

@devin-ai-integration devin-ai-integration Bot added the quick-review Small, self-contained change that is quick to review label Nov 29, 2026
devin-ai-integration[bot]

This comment was marked as resolved.

…comments

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
devin-ai-integration[bot]

This comment was marked as resolved.

…hunks

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>

@lwwmanning lwwmanning left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. vortex-cuda/src/kernel/patches/types.rs (load_device_patches): Leave load_device_patches unchanged from develop (indices_base: None, no read_first_chunk_offset call) so unsliced BitPacked, ALP, ALP-RD, and PlanPatches loads keep deriving the base on the GPU via PATCH_DERIVE_INDICES_BASE with zero host readbacks.
  2. vortex-cuda/src/kernel/patches/types.rs (slice_device_patches and load_patches_to_gpu) & vortex-cuda/src/kernel/encodings/bitpacked.rs (decode_bitpacked): Make slice_device_patches pub(crate) async fn slice_device_patches(patch_range: Range<usize>, device_patches: &mut DevicePatches) -> VortexResult<()> (both callers—decode_bitpacked and load_patches_to_gpu—are already async fns). Inside slice_device_patches, for non-empty slices (start < end), compute indices_base via read_first_chunk_offset(&device_patches.chunk_offsets, device_patches.chunk_offset_ptype, device_patches.n_chunks).await? + device_patches.offset_within_chunk before slicing device_patches.chunk_offsets (when device_patches.indices_base.is_none()), and set indices_base: Some(0) on the empty/start >= end branches.
  3. Tests & PR description: Update test_load_device_patches_nonzero_base_below_chunk_size (and .await? on slice_device_patches call sites in patches/types.rs tests) to assert indices_base is None after load_device_patches and Some(6) after slice_device_patches, and update the PR description to state that the 1-element D2H read only runs when GPU-slicing device-resident chunk_offsets.

Signed-off-by: Will Manning <will@spiraldb.com>
Co-Authored-By: Will Manning <will@spiraldb.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Addressed in a5baa7e3e:

  • Restored load_device_patches to indices_base: None with no read_first_chunk_offset call, keeping zero host readbacks on unsliced loads.
  • Made slice_device_patches async and moved the read_first_chunk_offset + offset_within_chunk computation into its non-empty (start < end) branch when indices_base is None, setting indices_base = Some(0) on empty/out-of-range slices.
  • Updated test_load_device_patches_nonzero_base_below_chunk_size, the slice_device_patches test call sites, and the PR description accordingly.

devin-ai-integration[bot]

This comment was marked as resolved.

@lwwmanning lwwmanning left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_patches uploads chunk_offsets before slice_device_patches runs. So read_first_chunk_offset always 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-resident chunk_offsets, which is no longer true.
  • A small fix: in load_device_patches, before the upload, set indices_base when chunk_offsets is still on the host. Use the first chunk offset plus offset_within_chunk, and leave it None otherwise. Unsliced device loads keep the GPU derivation. slice_device_patches then reads from the device only when the offsets started there. The cross_chunk_host and cross_chunk_device_chunk_offsets cases (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>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Addressed in 6ff449277: load_device_patches now captures indices_base before upload when chunk_offsets is host-resident and leaves it None when already on the device, so unsliced device loads keep GPU derivation, host-originated slices do zero D2H readback, and only GPU slices of device-resident chunk_offsets perform the 1-element D2H read in slice_device_patches. Also asserted indices_base.is_none() == device_resident_chunk_offsets after load in test_slice_device_patches_cpu_sliced and updated the PR description.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

Devin Review

Comment thread vortex-cuda/src/kernel/patches/types.rs Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/fix A bug fix quick-review Small, self-contained change that is quick to review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant