fix(cuda): push element count, not byte length, for slice kernel args#6
Open
haixuanTao wants to merge 2 commits into
Open
fix(cuda): push element count, not byte length, for slice kernel args#6haixuanTao wants to merge 2 commits into
haixuanTao wants to merge 2 commits into
Conversation
cuda-oxide lowers a `&[T]` kernel parameter to a `(ptr, len)` pair where `len` is an ELEMENT count, but the khal CUDA backend was pushing the buffer's BYTE length. So a kernel calling `slice.len()` got `byte_len` (4x too large for `&[u32]`) and read out of bounds — fatal in the batched broad-phase radix sort (`gpu_init_sort_dispatch` derives the workgroup grid from `num_keys_arr.len()`, producing a garbage indirect dispatch and an illegal memory access) and in the LBVH traversal (`*_len.len()`). Fix: in the three CUDA `write_arg` arms (`GpuBuffer`/`GpuBufferSlice`/ `GpuBufferSliceMut`, all generic over `T`), push `byte_len / size_of::<T>()`. Sized arrays (`&[T; N]`), scalars (`&T`) and uniforms reconstruct via the pointer and ignore this value, so they are unaffected; only true `&[T]` slices change — to correct. Latent in vortx (its kernels bound loops by `Shape` uniforms, never `slice.len()`) and in single-env physics; surfaced once N>1 batched physics ran on native CUDA. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
Author
|
Fixed formatting |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The CUDA backend's
write_argpushes a buffer's byte length as the second arg for slice bindings, but a kernel&[T]parameter expects an element count (ptr,len). So a kernel callingslice.len()seesbyte_len— e.g. 4× too large for&[u32]— and reads out of bounds.This is latent for kernels that bound their loops by other means (shape uniforms, explicit counts), but corrupts any kernel that uses
slice.len()directly — e.g. an indirect-dispatch setup deriving a workgroup grid fromnum_keys.len()produces a garbage grid and an illegal memory access.Fix
In the three CUDA
write_argarms (GpuBuffer/GpuBufferSlice/GpuBufferSliceMut, all generic overT), pushbyte_len / size_of::<T>()(element count).Sized arrays (
&[T; N]), scalars (&T), and uniforms reconstruct via the pointer and ignore this value, so they are unaffected — only true&[T]slices change, to correct.Verification
Tested on an RTX 5090 with cuda-oxide-compiled kernels: batched physics + tensor workloads that previously hit
CUDA_ERROR_ILLEGAL_ADDRESSviaslice.len()now run correctly and bit-exact vs the WebGPU backend.🤖 Generated with Claude Code