[dev] partial cuda graph support for thd format training. - #4359
Conversation
| When cuda_graph_impl is set to "local", "full_iteration" can be specified as cuda_graph_scope | ||
| to enable whole iteration CUDA graph. All other values enable layerwise CUDA graph.""" | ||
|
|
||
| thd_cuda_graph_max_num_seqs: int = 32 |
There was a problem hiding this comment.
What if the actual num seq is larger than this value?
There was a problem hiding this comment.
No, it's not handled — if actual > max, we silently return the longer cu_seqlens, which will break the graph at replay with a shape mismatch. I think it's better to add an explicit assertion in _pad_cu_seqlens to fail fast with a clear error message, plus update the docstring/help text with a sizing formula.
There was a problem hiding this comment.
I can think of two solutions.
- fail the
should_call_te_cudagraphcondition so that it can go to the eager path for this microbatch. - update the data generation logic to prevent generating samples with num seq >
thd_cuda_graph_max_num_seqs. If so,thd_cuda_graph_max_num_seqscan be renamed tothd_max_num_seqs(not only manages cudagraph).
There was a problem hiding this comment.
I would prefer the solution 2, do you have any comment? @xiaoyao0115
39e70af to
34176af
Compare
xiaoyao0115
left a comment
There was a problem hiding this comment.
Maybe it'd be better to refactor the newly added functions in megatron/core/packed_seq_params.py
59cfb32 to
3b36e44
Compare
Signed-off-by: HaochenYuan <haocheny@nvidia.com>
Signed-off-by: HaochenYuan <haocheny@nvidia.com>
Signed-off-by: HaochenYuan <haocheny@nvidia.com>
Signed-off-by: HaochenYuan <haocheny@nvidia.com>
…antics - Remove THD RoPE packed-frequency shape heuristic by requiring max_seqlen - Prefer explicit CP group when resolving THD padding lengths Signed-off-by: HaochenYuan <haocheny@nvidia.com>
Signed-off-by: HaochenYuan <haocheny@nvidia.com>
10d2e3c to
07a24f2
Compare
Signed-off-by: HaochenYuan <haocheny@nvidia.com>
Signed-off-by: HaochenYuan <haocheny@nvidia.com>
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/27631855121 |
What does this PR do ?
CUDA Graph requires static tensor shapes; THD packed-sequence inputs are variable-length.
The bridge: pad to upper bounds, decompose
PackedSeqParams(dataclass → tensor kwargs)across the graph boundary, vectorize RoPE (no GPU→CPU sync), and route a
padding_maskthrough the model so MoE aux loss ignores padding.
This feature collaborate with this TransformerEngine PR 2898.
Files
packed_seq_params.pypad_thd_for_cuda_graph(): pads tokens/labels/cu_seqlens, generatespadding_mask, handles CP partitioning.transformer/transformer_layer.py_decompose_/_reconstruct_packed_seq_params_*kwargs(dataclass ↔ tensor kwargs across graph boundary); THD-awareget_layer_static_inputs.transformer/module.py_is_thd_cuda_graph()gate; THD static-input shape[max_T // (TP if SP else 1), 1, H]. OptionalTHD_DEBUG_CG_IO=1shape logging.transformer/transformer_config.pythd_cuda_graph_max_num_seqs=32,cuda_graph_dynamic_microbatches=False,cuda_graph_num_microbatch_slots=None.transformer/cuda_graphs.pymodels/common/embeddings/rope_utils.py.tolist()+ Python loop withtorch.arange+torch.searchsortedper-token position lookup. Pure CUDA, graph-safe.models/gpt/gpt_model.pypre_processPP stages: scatterpadding_maskto SP-sharded shape.transformer/moe/moe_layer.pypadding_maskalignment for MoE aux-loss masking.tokenizers/text/libraries/null_tokenizer.pypad_idproperty (returnseod_id) for THD padding.pretrain_gpt.pyget_batchcallspad_thd_for_cuda_graphwhenmax_seqlen_per_dp_cp_rankis set;forward_stepforwardspadding_mask. Backward-compatible:None→ original path.Data flow
Validation
tests/unit_tests/transformer/test_thd_cuda_graph.py::TestE2EBitwise— bothparametrized cases pass with
--deterministic-mode:Contribution process
Pre-checks
Code review
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!
All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.
Step 1: Mark PR as "Ready for Review"
.github/CODEOWNERS.Final Review might get declined if these requirements are not fulfilled.
Step 2: Final Review
For PRs that change
megatron/core, once all expert reviewers have approved, theFinal Reviewlabel is applied automatically and final reviewers are assigned.For PRs outside
megatron/core, this step is skipped.Step 3: Approved
Once all required reviewers have approved, the
Approvedlabel is applied automatically.Merge
Any member of mcore-engineers will be able to merge your PR.
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.