[Main][feat] Support CUDA Graph capture offloading modules - #3697
Conversation
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
|
/ok to test 8a889c0 |
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
|
/ok to test ecaa319 |
|
/ok to test 5591584 |
|
We are changing our review process and marking all open, unlabeled PRs as draft. This change will go in effect starting once #3659 is merged. Moving forward, all PRs will be required to start as draft PRs. If you wish to get your PR merged, mark your PR as “Ready for review”. Read more about the new process at submit.md. |
2. remove flush_delayed_groups() when the training is not in replay mode Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: hongbinl <hongbinl@nvidia.com>
|
/ok to test 46fe4cf |
…t-fix Signed-off-by: hongbinl <hongbinl@nvidia.com> # Conflicts: # docs/user-guide/features/fine_grained_activation_offloading.md # megatron/core/pipeline_parallel/fine_grained_activation_offload.py # megatron/core/transformer/transformer_config.py
|
/ok to test c017623 |
Signed-off-by: hongbinl <hongbinl@nvidia.com>
|
/ok to test 56deb1d |
Signed-off-by: hongbinl <hongbinl@nvidia.com>
|
/ok to test bb69dee |
Signed-off-by: hongbinl <hongbinl@nvidia.com>
|
/ok to test 5b837ac |
Signed-off-by: hongbinl <hongbinl@nvidia.com>
…t-fix # Conflicts: # megatron/core/transformer/multi_latent_attention.py
|
/ok to test 45d2e90 |
Signed-off-by: hongbinl <hongbinl@nvidia.com>
Signed-off-by: hongbinl <hongbinl@nvidia.com>
Signed-off-by: hongbinl <hongbinl@nvidia.com>
Signed-off-by: hongbinl <hongbinl@nvidia.com>
Signed-off-by: hongbinl <hongbinl@nvidia.com>
|
/ok to test 6f0b3d1 |
Phlip79
left a comment
There was a problem hiding this comment.
Thank you for the great documentation!
|
/ok to test e50a23b |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28567889379 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28569830410 |
What does this PR do ?
PR to dev branch
This PR enables Fine-Grained Activation Offloading to work seamlessly with Transformer Engine CUDA Graph capture and replay. Previously, these two features were mutually exclusive — CUDA Graph captures a fixed sequence of GPU operations, while activation offloading involves dynamic D2H/H2D memory copies that conflict with graph semantics. This PR resolves the conflict by introducing dedicated CUDA stream/event synchronization and an optional deferred-commit strategy.
Scope: 15 files changed, +1033 / -212 lines
Key Changes
1. In-Graph Offload Synchronization (
transformer_layer.py)_te_cuda_graph_capture(): Whenoffload_module_in_cuda_graph=True, insertsbackward_record()at the sub-graph entry (synchronizes the compute stream with the H2D reload stream during backward) and callsforward_record()at the sub-graph exit (synchronizes the compute stream with the D2H offload stream during forward)._te_cuda_graph_replay(): Supports thedelay_offload_until_cuda_graphmode — during replay,enter_replay()/exit_replay()cause offload groups to be enqueued without immediate execution; after replay,flush_delayed_groups()issues the D2H copies during the CPU-idle window between graph launch and subsequent communication.2.
PipelineOffloadManagerExtensions (fine_grained_activation_offload.py)cuda_graph_stream/cuda_graph_event(external event) dedicated to synchronizing in-graph captured modules with the D2H/H2D offload streams.FineGrainedOffloadingGroupCommitFunctionpushes offload groups into a queue whendelay_offload=Trueand the manager is in replay state;flush_delayed_groups()drains the queue in batch during CPU-idle gaps.pre_warmup_hook/post_warmup_hooktemporarily disable/enable offloading around TE's warmup phase to avoid state-machine conflicts.3.
GraphableMegatronModuleIntegration (module.py)When
fine_grained_activation_offloadingandoffload_module_in_cuda_graphare both active,_get_te_cuda_graph_replay_args()injectscuda_graph_streamandcuda_graph_eventinto TE's replay kwargs, bridging the TE-side synchronization.4. Automatic Offload-in-Graph Detection (
_set_offload_modules)Added the
offload_module_in_cuda_graphflag, automatically determined by:CudaGraphModule.attnsub-graph containing offloadedqkv_linear/core_attn/attn_projCudaGraphModule.mlpsub-graph (dense layers) containing offloadedmlp_normattn_normoffload, or unsupportedmlp_normgraph boundaries) are auto-disabled with warningsexpert_fc1/moe_act)5. New Configuration Options (
TransformerConfig)delay_offload_until_cuda_graphactivation_offload_fractiondelta_offload_bytes_across_pp_rankscuda_graph_impl="transformer_engine","local", or"full_iteration". The local implementation is limited toexpert_fc1/moe_actoffload scopes and does not support whole-layer capture;full_iterationrequiresfine_grained_offloading_max_inflight_offloads >= 0.CudaGraphModule.moeis temporarily unsupported;cuda_graph_warmup_steps > 0is required. Fine-grained activation offloading remains mutually exclusive withcpu_offloadingandmhcrecompute.6. Code Cleanup and Compatibility
transformer_layer.pywith_get_offloading_interface()(@lru_cache) andself.off_interface.group_commit(...)call to per-group manager objects (group_offload(...)) so callers can carry group identity through delayed commits.expert_fc1input saving, avoiding extraset_save_original_input(self.linear_fc1)setup for those quantized recipes.Execution Flow
backward_record+forward_recordlink the compute stream with D2H/H2D streams through a sharedcuda_graph_eventgroup_commitonly enqueues;flush_delayed_groups()issues D2H during the CPU-idle window between graph launch and subsequent communicationoff_interface.reset()inschedules.pyandcuda_graphs._finish_capturingensures clean stateTests
test_fine_grained_activation_offloading_with_cuda_graph: covers multiple combinations ofcuda_graph_scope,offload_modules,activation_offload_fraction, anddelay_offload_until_cuda_graph(True/False); validates numerical correctness against baseline logits/gradients and performs peak memory sanity checks. Requires TE >= 2.14.TestCudaGraphConfigAndArgumentslocal-scope validation: covers allowed local CUDA graph activation-offload scopes, rejects unsupported offload modules, and rejects local whole-layer capture with fine-grained activation offloading.Contribution process
flowchart LR A[Pre-checks] --> B[PR Tests] subgraph Code Review/Approval C1[Expert Review] --> C2[Final Review] end B --> C1 C2 --> D[Merge]Pre-checks
Core 0.8)Code review
The following process is enforced via the CODEOWNERS file for changes into
megatron/core. For changes outside ofmegatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.For MRs into `main` branch
Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
(Step 1): Add PR label
Expert Review(Step 2): Collect the expert reviewers reviews
Expert Reviewlabel when your PR is ready for review.Final Review might get declined if these requirements are not fulfilled.
(Step 3): Final Review
Final Reviewlabel(Optional Step 4): Cherry-pick into release branch
If this PR also needs to be merged into
core_r*release branches, after this PR has been merged, selectCherry-pickto open a new PR into the release branch.For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either
eharper@nvidia.comorzijiey@nvidia.com.Merging your PR
Any member of core-adlr and
core-nemowill be able to merge your PR.