Skip to content

fix: lifetime-brand CudaGraph so captured buffers cannot dangle or race - #270

Open
elibol wants to merge 2 commits into
mainfrom
fix/cuda-graph-lifetimes
Open

elibol wants to merge 2 commits into
mainfrom
fix/cuda-graph-lifetimes

Conversation

@elibol

@elibol elibol commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Breaking API changes (cuda-async):

  • CudaGraph<T>CudaGraph<'a, T>: the graph mutably borrows every captured buffer for its lifetime, so dropping, moving, or writing one while the graph can still replay is now a compile error (previously a use-after-free / data race reachable in safe code).
  • take_output removed: outputs are reachable only after a completed replay, via outputs(&mut self), into_inner(self), or the &mut T returned by replay() / GraphLaunch::sync.
  • launch takes &mut self and returns GraphLaunch<'_, 'a, T>; the old graph.launch().sync_on(graph.stream()) idiom becomes graph.replay().
  • DeviceOp::graph/graph_on gained a lifetime parameter (where Self: 'a).

New: replay(), outputs(), into_inner(), and update_with() (refresh an input buffer the graph owns). Self-referential graph structs migrate by moving buffers into the scope closure and returning them as T (see the updated cuda_graphs example). Compile-fail UI tests pin the rejected programs; GPU tests cover sequential/async replays and into_inner. cutile's public API is unchanged.

@elibol
elibol force-pushed the fix/cuda-graph-lifetimes branch from 2c38ce5 to 2bc0987 Compare September 7, 2026 05:02
CudaGraph<'a, T> now mutably borrows every buffer its captured ops touch
for as long as it lives: scope/capture bound the op or closure by 'a, the
executable's baked-in device pointers can no longer outlive or alias their
buffers, and outputs are reachable only after a completed replay (launch
borrows the graph mutably; sync/sync_on/outputs synchronize first;
into_inner consumes). take_output is removed; replay() and update_with()
cover the sync-replay and graph-owned-input idioms. Compile-fail UI tests
pin the rejected programs; GPU tests cover replays, async replay, and
into_inner.

Signed-off-by: Melih Elibol <elibol@users.noreply.github.com>
@elibol
elibol force-pushed the fix/cuda-graph-lifetimes branch from 2bc0987 to 676002f Compare September 7, 2026 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant