Skip to content

feat(examples): capture SIMT kernel launches into a CudaGraph - #1731

Merged
nihalpasham merged 1 commit into
NVIDIA:mainfrom
vyncint:feat/cuda-graph-example
Nov 9, 2026
Merged

nihalpasham merged 1 commit into
NVIDIA:mainfrom
vyncint:feat/cuda-graph-example

Conversation

@vyncint

@vyncint vyncint commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

What This PR Does

Adds examples/cuda_graph demonstrating how to capture and replay SIMT kernel launches generated by #[cuda_module] using cuda_async::cuda_graph::CudaGraph:

  1. cuda_graph Example (crates/rustc-codegen-cuda/examples/cuda_graph)
  2. Example Catalog Entry (cuda-oxide/README.md)

1. Capturing #[cuda_module] Async Launchers into CudaGraph

Each step executes 16 small kernel launches (8 layers of axpy followed by scale) and compares eager stream submission against a captured CudaGraph replay and a host CPU reference:

EAGER (per step):
  for layer in 0..8 {
      module.axpy(stream.as_ref(), cfg, a, &x, &mut y_eager)?;
      module.scale(stream.as_ref(), cfg, s, &mut y_eager)?;
  }
  stream.synchronize()?;

CUDA GRAPH (recorded once, replayed per step):
  let graph = CudaGraph::scope(&stream, |s| {
      for layer in 0..8 {
          s.record(unsafe { module.axpy_async(cfg, a, &x, &mut y_graph) }?)?;
          s.record(unsafe { module.scale_async(cfg, sc, &mut y_graph) }?)?;
      }
      Ok(())
  })?;
  graph.launch().sync_on(&stream)?;
  • Verifies bit-identical outputs between eager execution and graph replay (0 mismatched elements) and checks max relative error (< 1e-4) against the CPU reference.

Verification

  • GPU Execution (RTX 5090, sm_120a): cargo oxide run cuda_graph passes (23.4 us/step eager vs 16.4 us/step graph replay; compute-sanitizer --tool memcheck: 0 errors).
  • Tests & CI Gates:
    • cargo clippy --manifest-path crates/rustc-codegen-cuda/examples/cuda_graph/Cargo.toml --all-targets -- -D warnings: PASS
    • sync-example-locks.sh --check, check-spdx-headers.sh, check-example-smoketest-contract.sh: all PASS

Refs #107

cuda-oxide's async launches have been cuda_async AsyncKernelLaunches since
NVIDIA#1461, and those implement GraphNode, so SIMT kernels can be recorded into
the shared CudaGraph. No cuda-oxide example showed it; the only graph
examples were cutile-rs's.

`cuda_graph` builds a step of 16 small launches (eight layers of `axpy`
then `scale`), runs it eagerly through the ordinary module API, and records
the same launches once with `CudaGraph::scope` and replays them. Both paths
run the same kernels in the same order, so the example requires their
results to match bit for bit and checks both against a CPU reference. It
prints the time per step of each path.

On an A10G the step takes 47-55 us eagerly and 24.3 us as a graph replay,
with no element differing and a relative error of 2.1e-7 against the CPU.
compute-sanitizer reports no errors.

Signed-off-by: Vyncint Ng <[email protected]>
@vyncint
vyncint requested a review from nihalpasham as a code owner November 4, 2026 05:05
@copy-pr-bot

copy-pr-bot Bot commented Nov 4, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@vyncint vyncint mentioned this pull request Nov 4, 2026
@nihalpasham nihalpasham added cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book enhancement New feature or request examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples labels Nov 9, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator

/ok to test 207f3ac

@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks, this is a clean, self-contained example of capturing #[cuda_module] async launches into CudaGraph::scope.

At 207f3ac5, cargo oxide run cuda_graph (23.4 us/step eager vs 16.4 us/step replay, 0 elements differ) and compute-sanitizer --tool memcheck pass on RTX 5090 alongside all example guards. Merging once CI is green.

@nihalpasham
nihalpasham merged commit fcc6169 into NVIDIA:main Nov 9, 2026
77 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda-oxide SIMT programming model: rustc backend, cargo oxide, cuda-device, cuda-host, book enhancement New feature or request examples Example kernels and host harnesses under crates/rustc-codegen-cuda/examples

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants