[Dev][feat] Support CUDA Graph capture offloading modules#3219
Merged
lhb8125 merged 125 commits intoMar 30, 2026
Merged
Conversation
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
…ithub.com/lhb8125/Megatron-LM into hongbinl/activation_offloading_cuda_graph
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Contributor
Author
|
/ok to test bb4ac50 |
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
Contributor
Author
|
/ok to test 51ba05a |
buptzyb
approved these changes
Mar 23, 2026
Contributor
Author
|
/ok to test 837dba4 |
Contributor
Author
|
@Victarry @NVIDIA/core-nemo Could you give a final review of this PR? |
Contributor
Author
|
/ok to test d78b776 |
hxbai
reviewed
Mar 25, 2026
hxbai
reviewed
Mar 25, 2026
hxbai
reviewed
Mar 25, 2026
Signed-off-by: Hongbin Liu <hongbinl@nvidia.com>
…ttps://github.com/lhb8125/Megatron-LM into hongbinl/activation_offloading_refactor_cuda_graph
Contributor
Author
|
/ok to test 0cef457 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/23732168075 |
48 tasks
This was referenced May 8, 2026
71 tasks
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 does this PR do ?
PR to main 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: 18 files changed, +944 / -259 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:CudaGraphScope.attnsub-graph containing offloadedqkv_linear/core_attn/attn_projCudaGraphScope.mlpsub-graph (dense layers) containing offloadedmlp_normattn_normoffload) are auto-disabled with warnings5. New Configuration Options (
TransformerConfig)delay_offload_until_cuda_graphactivation_offload_fractiondelta_offload_bytes_across_pp_rankscuda_graph_impl="transformer_engine"andcuda_graph_warmup_steps > 0;CudaGraphScope.moeis temporarily unsupported; mutually exclusive withcpu_offloadingandmhcrecompute.6. Code Cleanup
Replaced redundant
from ... import FineGrainedActivationOffloadingInterfacescattered across methods with a single_get_offloading_interface()helper (@lru_cache), accessed uniformly viaself.off_interface.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.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.