Skip to content

[profiler] capture CUDA graphs with CUPTI annotations under Cuspy - #5136

Merged
pianpwk merged 12 commits into
mainfrom
gh/pianpwk/110/head
Oct 20, 2026
Merged

pianpwk merged 12 commits into
mainfrom
gh/pianpwk/110/head

Conversation

@pianpwk

@pianpwk pianpwk commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

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.

[ghstack-poisoned]
[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added the ciflow/rl label Oct 8, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 8, 2026
pianpwk added a commit that referenced this pull request Oct 8, 2026
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.

ghstack-source-id: 454e540
Pull Request resolved: #5136
@pianpwk
pianpwk marked this pull request as ready for review October 8, 2026 00:20
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 8, 2026
Comment thread torchtitan/training_engine.py Outdated
]


def _get_cuda_graph_annotation_config(

@tianyu-l tianyu-l Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be a method of profiler.cuspy config or a "maybe" method from profiler config

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@tianyu-l

Copy link
Copy Markdown
Contributor

@claude review

@claude

claude Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 1m 7s —— View job


Review: [profiler] capture CUDA graphs with CUPTI annotations under Cuspy

  • Read the PR diff against gh/pianpwk/110/base
  • Check every callsite of wrap_with_cuda_graph / wrap_fwd_bwd_with_cuda_graph / Optim
  • Post review

The change is small and the default behavior stays the same: when Cuspy isn't enabled, annotation_config is None everywhere. Moving the logic into Profiler.Config.maybe_cuda_graph_annotation_config() addresses the earlier review comment. Putting the torch-specific helper in distributed/cuda_graph.py keeps profiler -> cuda_graph as the only import direction, so there's no import cycle. The issues below are mostly non-blocking.

Issues worth addressing

  1. Some graph captures don't get annotation_config (CLAUDE.md asks to audit all callsites).

    • torchtitan/components/validator.py:211: the "validation" graph is still captured without it. If a profiling window overlaps validation, kernels replayed from the eval graph won't carry mark_kernels metadata, while train kernels in the same trace will. Either pass it through, or say in the docs and PR description that only fwd-bwd and optimizer graphs are covered.
    • torchtitan/experiments/graph_trainer/trainer.py:197: GraphTrainingEngine overrides _initialize_forward_backward and calls wrap_fwd_bwd_with_cuda_graph without the new kwarg. Its optimizer graph does get the config through the shared _initialize_optim, but its fwd-bwd graph doesn't. Passing annotation_config=self.config.profiler.maybe_cuda_graph_annotation_config() there is a one-line fix inside the experiment.
  2. The helper is evaluated eagerly and can warn spuriously or more than once. training_engine.py:455 calls maybe_cuda_graph_annotation_config() as a kwarg to optim.build(...). That runs even when Optim decides not to use a CUDA graph (enable_cuda_graph=False, unsupported runtime, non-CUDA params). On a setup where graph_annotations.is_available() is False, you get the "annotations are unavailable" warning even though nothing is captured. You also get it a second time from the fwd-bwd call. Options: compute it once in the engine and reuse it, or pass the profiler config / a callable into Optim and resolve it inside if enable_cuda_graph:.

  3. record_py_stacks has no effect without CUDA graphs, and nothing says so.

    • The rules say to warn when a config option silently has no effect. With disable_cuda_graphs=True (or no optimizer or fwd-bwd graph), record_py_stacks=True writes nothing, because the dump at profiler.py:464 checks get_kernel_py_stacks() and skips. A warning at config validation or profiler setup would help.
    • The record_py_stacks field isn't in the PR description or in docs/debugging.md. Please add a sentence about the new rank{rank}_cuda_graph_py_stacks.json.gz artifact.
  4. The warning text is narrower than the feature (cuda_graph.py:331). It says replayed kernels "will not carry process-group metadata", but what's lost is all mark_kernels metadata (module FQNs and so on), not just PG info. Suggest: "...kernels replayed from CUDA graphs will not carry mark_kernels metadata in the Cuspy trace."

Nits

  • optim.py:64: cuda_graph_annotation_config defaults to None, but the only core caller always passes it. That default is fine for backward compatibility with torchft/trainer.py and the tests. Just confirming the default is intended, not an oversight.
  • profiler.py:466: get_kernel_py_stacks() is called only to check truthiness, then dump_kernel_py_stacks presumably fetches the same data again. If the stacks dict is large, calling the getter once and writing it out yourself, or relying on dump handling the empty case, avoids building it twice per export.
  • The py-stacks file is re-dumped at every profiling step and includes everything captured so far (capture happens once). That's harmless, but each iteration_* folder gets an identical copy. Consider dumping only on the first export, or note this in the docstring.
  • Tests: the parametrized test covers the fwd-bwd path well. Nothing checks that the optimizer receives a non-None config under Cuspy (test_initialize_optim_builds_component only covers None), and there's no CPU test for the py-stacks dump branch in trace_handler. Both would be cheap to add with mocks.

Correctness checks that passed

  • key_by="exec" when enable_event_node_ids is set matches the stated reason: Cuspy resolves event-record nodes only by exec-graph id. record_py_stacks keys by (graph id << 32) | node id from the trace, which is consistent under either key_by mode.
  • annotation_config is threaded through every internal wrapper (_wrap_with_cuda_graph -> CUDAGraphWrapper / _ForwardBackwardCUDAGraphWrapper -> torch.cuda.graph), and every mock signature in the tests is updated.
  • When Cuspy isn't enabled, capture behavior is unchanged.

I didn't run tests or lint; this was a read-only review.

submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Oct 19, 2026
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.

CuspyProfilerConfig.record_py_stacks also records each graph node's Python
launch stack (annotation_config["record_py_stacks"], which needs the CUPTI
backend) and writes them beside each trace as
rank{rank}_cuda_graph_py_stacks.json.gz, keyed by (graph id << 32) | graph
node id of the trace's kernel events. On a 1-GPU llama3 debugmodel run with
CUDA graphs, all 545 graphed kernel events in the Cuspy trace have a stack,
e.g. the embedding gather resolves to torchtitan/models/common/embedding.py:41.

ghstack-source-id: 54dbd0a
Pull Request resolved: pytorch/torchtitan#5136
[ghstack-poisoned]
[ghstack-poisoned]
@pianpwk
pianpwk requested a review from IvanKobzarev as a code owner October 19, 2026 23:13
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Oct 20, 2026
When Cuspy profiling is enabled, capture the forward-backward, optimizer, and
validation 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.

CuspyProfilerConfig.record_py_stacks also records each graph node's Python
launch stack (annotation_config["record_py_stacks"], which needs the CUPTI
backend) and writes them beside each trace as
rank{rank}_cuda_graph_py_stacks.json.gz, keyed by (graph id << 32) | graph
node id of the trace's kernel events. On a 1-GPU llama3 debugmodel run with
CUDA graphs, all 545 graphed kernel events in the Cuspy trace have a stack,
e.g. the embedding gather resolves to torchtitan/models/common/embedding.py:41.

ghstack-source-id: 21cdfca
Pull Request resolved: pytorch/torchtitan#5136
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]
[ghstack-poisoned]
submartingales pushed a commit to botcanlearn/torchtitan-upstream that referenced this pull request Oct 20, 2026
When Cuspy profiling is enabled, capture the forward-backward, optimizer, and
validation 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.

CuspyProfilerConfig.record_py_stacks also records each graph node's Python
launch stack (annotation_config["record_py_stacks"], which needs the CUPTI
backend) and writes them beside each trace as
rank{rank}_cuda_graph_py_stacks.json.gz, keyed by (graph id << 32) | graph
node id of the trace's kernel events. On a 1-GPU llama3 debugmodel run with
CUDA graphs, all 545 graphed kernel events in the Cuspy trace have a stack,
e.g. the embedding gather resolves to torchtitan/models/common/embedding.py:41.

ghstack-source-id: 0f33ac5
Pull Request resolved: pytorch/torchtitan#5136
@pianpwk
pianpwk changed the base branch from gh/pianpwk/110/base to main October 20, 2026 16:16
@pianpwk
pianpwk merged commit b93d5c0 into main Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl 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.

2 participants