Skip to content

Enable CudaGraph Annotations - #4359

Open
SherlockNoMad wants to merge 4 commits into
mainfrom
port-d117008356
Open

Enable CudaGraph Annotations#4359
SherlockNoMad wants to merge 4 commits into
mainfrom
port-d117008356

Conversation

@SherlockNoMad

@SherlockNoMad SherlockNoMad commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Human Note: this is for enabling the mark_kernels, so that we see annotations in profiling trace when cudagraph is used.
Also removed the duplicated implementation in GraphTrainer.

Summary

Graph Trainer maintained its own CUDA graph manager and wrapper even though core TorchTitan already provided the same capture, warmup, input-copy, replay, and teardown behavior. The two implementations had begun to diverge, which made CUDA graph fixes and profiler annotation support harder to maintain.

This PR:

  • adds explicit tensor_input_indices support to the core CUDAGraphWrapper
  • moves CUDA graph annotation collection and trace post-processing into the core implementation
  • always enables CUDA graph annotations and removes the profiler trace_post_processor configuration field
  • updates Graph Trainer to use the core CUDA graph wrapper and teardown path
  • removes the duplicated Graph Trainer CUDA graph manager and wrapper
  • temporarily disables test_precompile_vs_trace, because loaded precompile artifacts currently provide no example inputs for CUDA graph setup

The precompile test will be fixed and re-enabled in a follow-up by constructing the wrapper with live inputs from the training loop instead of serializing inputs in the artifact.

Test plan

  • pytest -q tests/unit_tests/cpu/test_cudagraph.py torchtitan/experiments/graph_trainer/tests/test_precompile.py torchtitan/experiments/graph_trainer/tests/test_profiler.py
  • pytest -q torchtitan/experiments/graph_trainer/tests/test_passes.py -k cudagraph
  • pytest -q torchtitan/experiments/graph_trainer/tests/test_trace_module.py -k cudagraph
  • pytest -q torchtitan/experiments/graph_trainer/tests/test_bitwise_deterministic.py::TestLlama3BitwiseDeterministic::test_aot_fx_trace_vs_eager
  • targeted ufmt and flake8 checks

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 27, 2026
@SherlockNoMad SherlockNoMad changed the title [torchtitan][pp] Annotate CUDA graph pipeline actions [torchtitan][pp] Enable CudaGraph Annotations Aug 27, 2026
@SherlockNoMad SherlockNoMad changed the title [torchtitan][pp] Enable CudaGraph Annotations Enable CudaGraph Annotations Aug 27, 2026
@SherlockNoMad
SherlockNoMad marked this pull request as ready for review August 27, 2026 23:50
Comment thread torchtitan/trainer.py Outdated
)
self.fwd_bwd_fn = self._forward_backward_body
if not config.training.disable_cuda_graphs:
enable_cudagraph_annotations()

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.

if it's always enabled, don't need this function -- just always set with torch.cuda.graph(..., enable_annotations=True)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Comment thread torchtitan/distributed/cudagraph.py
Comment thread torchtitan/trainer.py Outdated
self.fwd_bwd_fn = self._forward_backward_body
if not config.training.disable_cuda_graphs:
enable_cudagraph_annotations()
config.profiler.trace_post_processor = (

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.

This probably should go to config, rather than being ad hoc-ly changed in trainer build. E.g. if there had been other processors, they'll be overwritten silently.

@SherlockNoMad SherlockNoMad Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have removed trace_post_processor as a config. it was originally added to support annotation_post_processing. since we are have enable_annotations=True, we can just always run the post_processing.

it's a no-op when there is no annotation collected.

Comment on lines +403 to +405
@unittest.skip(
"Precompiled artifacts do not provide example inputs for CUDA graph setup"
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

will be fixed in the following PR.

@tianyu-l tianyu-l left a comment

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.

one comment


if post_processor is not None:
post_processor(output_file)
cudagraph_annotate_trace_post_processor(output_file)

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.

need to make sure this doesn't fail on non-nvidia chips

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.

2 participants