feat(megatron): expose optimizer_cuda_graph for the optimizer#9680
Merged
Jintao-Huang merged 1 commit intoJul 5, 2026
Merged
Conversation
Add `optimizer_cuda_graph: bool = False` to `MegatronArguments`, alongside the other `optimizer_*` toggles, and document it in the zh/en command-line-parameters docs. `swift/megatron/trainers/base.py` builds the Megatron `OptimizerConfig` from `MegatronArguments` via a field-name-keyed comprehension over `dataclasses.fields(config_cls)`, so exposing a field whose name matches Megatron-Core's `OptimizerConfig.optimizer_cuda_graph` is sufficient for it to be forwarded, with no additional plumbing.
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a new configuration parameter, optimizer_cuda_graph, which allows users to enable CUDA graphs for optimizer steps. The parameter is added to the MegatronArguments class with a default value of False, and its documentation has been updated in both the English and Chinese command-line parameter guides. There are no review comments to address.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Jintao-Huang
approved these changes
Jul 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Megatron-SWIFT's
MegatronArgumentsexposes several optimizer-level toggles (optimizer_cpu_offload,optimizer_offload_fraction,use_precision_aware_optimizer, …) but is missingoptimizer_cuda_graph, which Megatron-Core supports:--optimizer-cuda-graph(megatron/training/arguments.py,action='store_true')OptimizerConfig.optimizer_cuda_graph: bool = False(megatron/core/optimizer/optimizer_config.py) — "If true, enables CUDA graph for optimizer step."optimizer_cuda_graphCUDA-graphs the optimizer step, reducing per-step CPU launch overhead. Without it exposed, recipes that want the CUDA-graphed optimizer step cannot enable it through Megatron-SWIFT.Change
optimizer_cuda_graph: bool = FalsetoMegatronArguments, alongside the otheroptimizer_*toggles (matching Megatron'sOptimizerConfigfield name and default).No forwarding code is needed:
swift/megatron/trainers/base.pybuilds the MegatronOptimizerConfigfromMegatronArgumentsvia a field-name-keyed comprehension overdataclasses.fields(config_cls)({f.name: getattr(args, f.name) for f in dataclasses.fields(config_cls) if hasattr(args, f.name) ...}), so exposing a field whose name matchesOptimizerConfig.optimizer_cuda_graphis sufficient for it to be forwarded — exactly like the existing optimizer toggles.Notes
Typed as
bool(defaultFalse) to mirror Megatron's own CLI (store_true) andOptimizerConfigfield default. Verified against currentmain: the field is absent in Megatron-SWIFT (grep -r optimizer_cuda_graph→ 0 refs) while present in Megatron-Core, and the ms-swift attribute name matches Megatron'sOptimizerConfigfield name exactly (so the forwarding comprehension picks it up with no additional code).