Skip to content

fix: alignment and padding of sliced CUDA Arrow bitmaps - #9926

Merged
0ax1 merged 16 commits into
developfrom
ad/cuda-arrow-bitmap-correctness
Sep 22, 2026
Merged

0ax1 merged 16 commits into
developfrom
ad/cuda-arrow-bitmap-correctness

Conversation

@0ax1

@0ax1 0ax1 commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix CUDA Arrow export of sliced BOOL values and validity bitmaps so their storage is safe for cuDF’s word-based reads.

Correct logical values and offsets are not sufficient:

  • Byte slicing can misalign device pointers.
  • Word-sized reads can extend beyond the logical bitmap bytes into
    cuDF’s padded mask extent.

Export now reuses suitably aligned, zero-padded storage or copies/repacks it when necessary. Repacking aligns the input view within its backing allocation and uses bounded byte loads only for the partial tail. Kernel-produced bitmaps are padded up front to avoid export copies.

Focused regression tests cover sliced values and validity, zeroed tails, nonzero-offset reuse, and capped-grid repacking and null counting.

@0ax1 0ax1 changed the title Ad/cuda arrow bitmap correctness fix: alignment and padding of sliced CUDA Arrow bitmaps Sep 17, 2026
@0ax1 0ax1 added the changelog/fix A bug fix label Sep 17, 2026
@0ax1
0ax1 marked this pull request as ready for review September 17, 2026 11:09
@0ax1
0ax1 requested review from joseph-isaacs and myrrc September 17, 2026 11:09
@codspeed

codspeed Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 1 benchmark

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 3 improved benchmarks
❌ 1 regressed benchmark
✅ 2246 untouched benchmarks
⏩ 251 skipped benchmarks1
🗄️ 1 archived benchmark run2

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation take_fsl_u32_random[256, 10] 124.4 µs 159.6 µs -22.05%
⚡ Simulation take_fsl_u32_random[16, 100] 165.7 µs 126 µs +31.53%
⚡ Simulation take_fsl_nullable_random[16, 100] 190.8 µs 165.4 µs +15.38%
⚡ Simulation take_fsl_f16_random[256, 100] 231.7 µs 204.8 µs +13.15%

Tip

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


Comparing ad/cuda-arrow-bitmap-correctness (50ec1e4) with develop (2c8f160)

Open in CodSpeed

Footnotes

  1. 251 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. ↩

  2. 1 benchmark was run, but is now archived. If it was deleted in another branch, consider rebasing to remove it from the report. Instead if it was added back, click here to restore it. ↩

@0ax1
0ax1 requested a review from robert3005 September 17, 2026 13:05
@vortex-data vortex-data deleted a comment from github-actions Bot Sep 21, 2026
@robert3005

Copy link
Copy Markdown
Contributor

@claude review

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @robert3005's task in 3m 43s —— View job


Claude Code Review

  • Read changed files (arrow_validity.cu, canonical.rs, stream.rs)
  • Analyze CUDA kernel correctness (alignment/bounds/masking)
  • Analyze Rust export path (repack/copy/padding)
  • Review tests & edge cases
  • Post review

Reviewed all three files end-to-end (kernel, export path, and stream padding helpers). This is a careful, well-tested fix. My overall assessment: the logic is correct; I have only minor/defensive suggestions and a note on the CodSpeed report.


Correctness — CUDA kernel (arrow_validity.cu)

The core repack logic holds up under close reading:

  • load_input_word (fast vs. byte path): the fast 8-byte load is correctly gated on both available_bytes >= sizeof(uint64_t) and 8-byte pointer alignment, so a byte-sliced (base + 1) pointer safely falls back to the bounded byte loop. Both paths only ever touch bytes < input_bytes, so no over-read of the logical buffer.
  • Floor division for negative shifts: source_bit_start >> 6 (vs. / 64) genuinely matters when arrow_offset > input_offset makes source_bit_start negative — I traced the arrow_ahead_of_input case and the floor + source_bit & 63 combination lands the bits correctly, with load_input_word(word_idx < 0) returning 0.
  • No shift-width UB: the source_bit == 0 early return avoids hi << (64 - 0), and both mask builders guard lead >= 64/lead >= 8 before shifting. remaining is always in [1, 64)/[1, 8) when the < 64/< 8 branch is taken because word_start/byte_start < validity_bits for every launched index, so (1 << remaining) - 1 is well-defined.
  • No valid bit ever reads as zero: the maximum needed source bit is input_offset + len - 1, which is always within expected_input_bytes (enforced Rust-side), so the bounded byte path never zeroes a live bit — only masked-out padding.

Correctness — Rust export path (canonical.rs)

  • The input_offset == arrow_offset reuse fast path correctly requires 4-byte alignment (matching cuDF's 32-bit mask-word reads) and zeroed tail padding across the full 64-byte-padded extent, otherwise copies. has_zeroed_tail_padding reasons purely from tracked zeroed_tail_start metadata (no device readback), which is sound.
  • Bool export threads a single meta.offset() through both values and validity, and the test_export_byte_sliced_device_validity[boolean_values=true] case exercises the values-vs-validity offset mismatch that forces a validity repack — good coverage.
  • Padding math in repack_arrow_validity_buffer is consistent: kernel masks the partial tail word to zero, zero_padding zeroes the remaining words, and new_with_zeroed_tail(output, output_bytes) records the byte boundary before slicing.

Test coverage

The rstest matrices are strong — upload_unpadded deliberately defeats the 64-byte upload padding that would otherwise mask OOB/alignment bugs, and the offset cases span byte-aligned, bit-offset, word-boundary, and below/full/above padding-block sizes, plus a multi-block (20_001) case. The overflow/truncation rejection tests are a nice touch.


Minor suggestions (non-blocking)

  1. arrow_validity_repack launch is missing the block clamp that count_valid has. count_arrow_validity_nulls clamps to MAX_COUNT_BLOCKS = 4096 and relies on the grid-stride loop, but repack_arrow_validity_buffer (canonical.rs:1080) computes num_blocks = output_words.div_ceil(256) with no upper bound. The repack kernel already has a grid-stride loop, so clamping would be a safe consistency/defensiveness improvement (the unclamped grid_dim.x is only theoretically reachable at absurd buffer sizes, so this is not a real-world bug). Fix this →

  2. Duplicated masking logic. repack_word and arrow_validity_byte_mask implement the same lead/tail padding-mask pattern at two widths (64-bit vs 8-bit). Not worth a shared helper given the different widths, but worth a mental note if a third consumer appears.

CodSpeed regressions

The two flagged regressions — arrow_checked_add_u32_neon (vortex-compute) and random_i16 (vortex-array filter) — are in CPU crates untouched by this CUDA-only change, and CodSpeed itself warns about "Unknown Walltime execution environment" and "Different runtime environments". These look like measurement noise rather than a real regression from this PR.
· branch ad/cuda-arrow-bitmap-correctness

Comment thread vortex-cuda/kernels/src/arrow_validity.cu Outdated
Comment thread vortex-cuda/src/arrow/canonical.rs
Comment thread vortex-cuda/src/arrow/canonical.rs
Comment thread vortex-cuda/src/arrow/canonical.rs
Comment thread vortex-cuda/src/stream.rs Outdated
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
…ation

Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
@0ax1
0ax1 force-pushed the ad/cuda-arrow-bitmap-correctness branch from 2b76063 to ef0c517 Compare September 22, 2026 12:10
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Align repack inputs within their backing allocations so full words use aligned loads. Share capped launch constants and exercise grid-stride wrapping with a smaller test cap.

Use bitmap-neutral helpers, extract bool export, and compare device pointers for reuse. Cover both bool buffer tails and nonzero-offset reuse while reducing redundant repack cases.

Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Allocate cuDF-padded bitmap storage and zero only its tail before dictionary bool gathers and run-end validity expansion. Track that tail while preserving logical buffer lengths so Arrow export avoids a copy.

Extend existing producer tests to check zero padding and exported device pointer reuse.

Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Exercise multi-block reduction and grid-stride iteration with a nonzero offset and dirty prefix and tail bits. Assert that the fixture exceeds the test grid cap and matches the exact CPU-derived null count.

Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
@0ax1
0ax1 enabled auto-merge (squash) September 22, 2026 13:13
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
@0ax1
0ax1 merged commit c3ade49 into develop Sep 22, 2026
93 checks passed
@0ax1
0ax1 deleted the ad/cuda-arrow-bitmap-correctness branch September 22, 2026 13:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants