cuda-device: a safe `DisjointSlice::empty()`
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 74/100
Research direction
Start with the constructors in crates/cuda-device/src/disjoint.rs and check how DisjointSlice represents its pointer, length, and index-space witnesses. Add the safe empty value and the proposed Default implementation, keeping the pointer non-null and aligned; verify that witnesses yield None for an empty slice. Done means callers can use an empty DisjointSlice without an unsafe block.
Written by the indexing model from the issue text.
Description
What problem does this solve?
A device helper with an optional output takes a DisjointSlice<T> and skips writes when it's empty. Callers that don't want the output still have to build one, and every DisjointSlice constructor is unsafe (from_raw_parts_with_space, from_raw_parts, from_mut_slice in crates/cuda-device/src/disjoint.rs). So the call site carries an unsafe block with a SAFETY comment for a value that can't be misused:
// SAFETY: an empty slice covers no memory
let no_part = unsafe { DisjointSlice::from_mut_slice(&mut []) };
flash_attn::<...>(tiles, o, /* ... */, no_part);
A zero-length slice discharges every obligation in from_raw_parts's contract (valid for len elements, lifetime, exclusive access over ptr..ptr + 0), so the constructor can be safe.
Proposed API / behavior
impl<'a, T> DisjointSlice<'a, T> {
/// An empty slice; every index-space witness on it yields `None`
pub const fn empty() -> Self { /* NonNull::dangling(), len 0 */ }
}
impl<T> Default for DisjointSlice<'_, T> { fn default() -> Self { Self::empty() } }
let no_part = DisjointSlice::<f32>::empty();
A dangling, aligned pointer (rather than null) keeps as_mut_ptr() well-formed for zero-sized accesses. For the index-space-typed form, empty() would be defined for each space whose witnesses all check len.
Alternatives considered
unsafe { DisjointSlice::from_mut_slice(&mut []) }: works, but it adds anunsafeblock per call site, and audits have to re-check each one.Option<DisjointSlice<T>>parameters on the helper: also works, but it changes every helper signature and moves the test into the helper.
Additional context
Found porting sam3rs's flash-attention kernels to cuda-oxide (main 2d47e43, rechecked at 88e52d117). A shared flash_attn body has an optional partial-output buffer, and the kernels that don't use it pass an empty DisjointSlice.
- Dominant language
- Rust
- Stars
- 3.9k
- Forks
- 349
- Avg merge
- 3d 20h
- Merged PRs (30d)
- 81
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from NVIDIA/cuda-rust
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-2 days Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
cutile-rs supply-chain
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
NVIDIA/cuda-rust#1568 · 1 comment ·
Maintainers usually reply within 1 day
-
[BUG]: The doc of trait `LoadTileLike` is incorrectPossibly taken @mangeshraut712 claimed this 5 days ago. Openbug
Difficulty 1/5 Under an hour Newbie friendliness 72/100
NVIDIA/cuda-rust#1523 · 2 comments ·
Maintainers usually reply within 1 day
All issues in NVIDIA/cuda-rust
Similar issues
-
bug duplicate enhancement priority: low ui
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
storytold/pdfcraft#5594 · 1 comment ·
Maintainers usually reply within 1 day
-
documentation good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
open-jarvis/OpenJarvis#1212 ·
Maintainers usually reply within 4 days
-
bug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
CakeRepository/agentshangout#38 ·
Maintainers usually reply within 1 day