Skip to content

[profiler] draw CUDA graph replays on logical lanes - #5526

Draft
pianpwk wants to merge 6 commits into
gh/pianpwk/116/basefrom
gh/pianpwk/116/head
Draft

pianpwk wants to merge 6 commits into
gh/pianpwk/116/basefrom
gh/pianpwk/116/head

Conversation

@pianpwk

@pianpwk pianpwk commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

A replayed CUDA graph runs on whatever streams the driver's graph executor
picks, so a profile draws one training step across hundreds of single-kernel
stream lanes. Rank 0 of a Llama3 8B FSDP2 x TP2 GraphTrainer run had 408 GPU
tracks, 370 of them a single NCCL kernel.

Add Profiler.Config.cuda_graph_lanes (on by default), which asks torch to
move graphed work onto logical lanes: one per process group for collectives,
one per mark_stream stream, and one for everything else. It sets
CuspyConfig(graph_lanes="all") for Cuspy and passes graph_lanes="all" to
Kineto's export_chrome_trace. Moved events keep the stream they ran on as
original_stream.

Both need a torch with CuspyConfig.graph_lanes, which landed together with
process group lanes for collectives; on older torch the trace keeps its
replay-stream layout, with a warning when it contains CUDA graph work.

With a prototype of the same lane rule, the same run has 8 GPU tracks on
Cuspy (CUDA graph, mesh_tp, mesh_fsdp, and the eager streams) and 7 on
Kineto. A DeepSeek V3 Dist-MoE FSDP2 x EP2 run has 4.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Oct 20, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #5527
* #5526
* #5136
* __->__ #5525

Profiler.Config.cuspy switches CUDA activity collection from Kineto to
Cuspy
(PM sampling, environment counters, sync events, graph dependencies,
event-node
ids, and optional Perfetto protobuf output). Profiler startup raises a
ValueError if torch or libcupti lacks Cuspy support.

With CUDA graphs on, kernels replayed from graphs do not carry
annotations in
the Cuspy trace yet; the next PR in the stack captures graphs with a
CUPTI
annotation config to fix that.
[ghstack-poisoned]
[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Oct 20, 2026
)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* #5527
* #5526
* __->__ #5136

When Cuspy profiling is enabled, capture the forward-backward and
optimizer
CUDA graphs with an annotation config (backend="cupti") so kernels
replayed
from graphs keep their mark_kernels metadata in the Cuspy trace. The
CUDA graph
wrappers take an annotation_config that is passed through to
torch.cuda.graph;
it stays None, preserving current behavior, unless profiler.cuspy is
set.

key_by="auto" keys annotations to the capture graph, which Cuspy
resolves
through CUPTI's sourceGraphNodeId. Cuspy resolves graph event-record
nodes only
by exec-graph id, so enable_event_node_ids keys by the exec graph
instead.

This starts Cuspy's CUPTI subscription at the first capture and holds it
for
the rest of the run, so other CUPTI tools (Nsight Systems, dynolog)
can't
attach to that process.
shuhuayu added a commit that referenced this pull request Nov 11, 2026
Independent PR on `main`.

## Summary (human-written)

On main, an OOM during training closes the profiler as if shutdown were
clean, so the memory profiler never writes its exit snapshot; passing
the exception to the profiler fixes that, which is how I debugged
several full-scale Kimi K3 OOMs.

## Why

#4580 added an OOM branch to `Profiler.__exit__` that writes an exit
memory
snapshot when the exception (or anything in its cause chain) is an
`OutOfMemoryError`. #4773 then replaced the profiler `with` block in
`Trainer.train()` with `engine.start_profiler()` plus
`engine.close_profiler()`
in a `finally`, and `close_profiler()` always calls
`Profiler.__exit__(None, None, None)`. Since then the OOM branch is
unreachable for the main trainer. The exception itself still propagates;
only
the snapshot is lost.

## What changed

- `TrainingEngine.close_profiler(exc: BaseException | None = None)`
passes
  `exc` to `Profiler.__exit__`.
- `Trainer.train()` replaces its `finally` with `except BaseException as
e:
  close_profiler(e); raise` and `else: close_profiler()`.
- `engine.close()` still calls `close_profiler()` with no exception. It
only
reaches an open profiler when `train()` never started it, so nothing is
lost.

## Example

```text
train_step raises an OOM (possibly wrapped in a RuntimeError by PP)
before: close_profiler() -> Profiler.__exit__(None, None, None); no snapshot
after:  close_profiler(e) -> Profiler.__exit__(type(e), e, tb); snapshot written once
the same exception object still propagates to the caller
```

The exception is passed explicitly rather than read from
`sys.exc_info()`,
which would also return an exception an outer frame is still handling
(e.g. a
caller that catches an OOM and retries `train()` would get a bogus
snapshot).
This matches the `close(exc)` shape in #5871.

## Scope

- RL is not covered. `rl/trainer/trainer.py` starts the profiler through
the
shared engine but closes it from its async `close()` endpoint, which has
no
  training exception to pass, so RL OOMs still get no exit snapshot.
- #5871 rewrites `observability/profiler.py` and touches
`tests/unit_tests/cpu/test_trainer.py`; whichever lands second needs a
rebase.
- CUDA-graph stream-lane rendering is not part of this PR; #5526 covers
it.

## User-facing changes

| Before | After |
| --- | --- |
| An OOM in `Trainer.train()` writes no exit memory snapshot. | The
existing OOM branch writes it once. |

## Numerics

None. Only profiler shutdown on a training failure changes.

## Testing

`test_train_passes_failure_to_profiler_close` runs the real
`Trainer.train`,
`TrainingEngine.close_profiler`, and `Profiler.__exit__` with the
snapshot
writer mocked, for three cases: normal exit (no snapshot), a plain OOM,
and an
OOM wrapped in a RuntimeError as PP does (snapshot once, and the same
exception
object is re-raised). All 57 tests in
`tests/unit_tests/cpu/test_trainer.py`
pass; `pre-commit` passes.

Each of these breakages fails the test: `train()` not passing the
exception,
`close_profiler()` ignoring it, a snapshot on every close, and
swallowing the
exception after closing.

Not verified: writing a real GPU OOM snapshot.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant