-
Notifications
You must be signed in to change notification settings - Fork 713
add support for enabling cuda graph under thd format in megatron. #2898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2044,6 +2044,17 @@ def forward( | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| nvtx_range_pop(f"{nvtx_label}") | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # THD CUDA Graph: zero-fill output at padded positions after CP assembly. | ||||||||||||||||||||||||||||||||||||
| # cu_seqlens_q_padded is GLOBAL; divide by cp_size to get local actual_T. | ||||||||||||||||||||||||||||||||||||
| if qkv_format == "thd" and out_ret is not None and hasattr(out_ret, "shape"): | ||||||||||||||||||||||||||||||||||||
| import torch as _torch | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| _local_aT = cu_seqlens_q_padded[-1] // cp_size | ||||||||||||||||||||||||||||||||||||
| if out_ret.shape[0] > 0: | ||||||||||||||||||||||||||||||||||||
| _m = _torch.arange(out_ret.shape[0], device=out_ret.device) >= _local_aT | ||||||||||||||||||||||||||||||||||||
| out_ret.data[_m] = 0 | ||||||||||||||||||||||||||||||||||||
| out.data[_m.view(-1, *([1] * (out.dim() - 1))).expand_as(out)] = 0 | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+2052
to
+2056
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if return_max_logit: | ||||||||||||||||||||||||||||||||||||
| return out_ret, max_logit | ||||||||||||||||||||||||||||||||||||
| return out_ret | ||||||||||||||||||||||||||||||||||||
|
|
@@ -2680,10 +2691,17 @@ def backward(ctx, dout, *_args): | |||||||||||||||||||||||||||||||||||
| dim = ctx.qkv_format.index("s") | ||||||||||||||||||||||||||||||||||||
| dq, dk, dv = [x.view(*x.shape[:dim], -1, *x.shape[dim + 2 :]) for x in [dq, dk, dv]] | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # THD CUDA Graph fix: reading cu_seqlens[-1] as a Python index triggers | ||||||||||||||||||||||||||||||||||||
| # GPU->CPU sync during graph capture. Use .shape[0] instead when capturing. | ||||||||||||||||||||||||||||||||||||
| if ctx.qkv_format == "thd" and not ctx.use_fused_attention: | ||||||||||||||||||||||||||||||||||||
| dq[cu_seqlens_q_padded[-1] :].fill_(0) | ||||||||||||||||||||||||||||||||||||
| dk[cu_seqlens_kv_padded[-1] :].fill_(0) | ||||||||||||||||||||||||||||||||||||
| dv[cu_seqlens_kv_padded[-1] :].fill_(0) | ||||||||||||||||||||||||||||||||||||
| if torch.cuda.is_current_stream_capturing(): | ||||||||||||||||||||||||||||||||||||
| _q_end, _kv_end = dq.shape[0], dk.shape[0] | ||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||
| _q_end = cu_seqlens_q_padded[-1] | ||||||||||||||||||||||||||||||||||||
| _kv_end = cu_seqlens_kv_padded[-1] | ||||||||||||||||||||||||||||||||||||
| dq[_q_end:].fill_(0) | ||||||||||||||||||||||||||||||||||||
| dk[_kv_end:].fill_(0) | ||||||||||||||||||||||||||||||||||||
| dv[_kv_end:].fill_(0) | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if ctx.fp8 and ctx.is_input_fp8: | ||||||||||||||||||||||||||||||||||||
| dq, dk, dv = combine_and_quantize(qkv_layout, dq, dk, dv, ctx.dQKV_quantizer) | ||||||||||||||||||||||||||||||||||||
|
|
@@ -2731,6 +2749,16 @@ def backward(ctx, dout, *_args): | |||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| nvtx_range_pop(f"{nvtx_label}") | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| # THD CUDA Graph: zero-fill dQ/dK/dV at padded positions after CP backward. | ||||||||||||||||||||||||||||||||||||
| if ctx.qkv_format == "thd": | ||||||||||||||||||||||||||||||||||||
| import torch as _torch | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| _local_aT_bwd = cu_seqlens_q_padded[-1] // get_distributed_world_size(ctx.cp_group) | ||||||||||||||||||||||||||||||||||||
| for _dg in [dq, dk, dv]: | ||||||||||||||||||||||||||||||||||||
| if _dg is not None and hasattr(_dg, "shape") and _dg.shape[0] > 0: | ||||||||||||||||||||||||||||||||||||
| _mb = _torch.arange(_dg.shape[0], device=_dg.device) >= _local_aT_bwd | ||||||||||||||||||||||||||||||||||||
| _dg[_mb] = 0 | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+2752
to
+2760
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Same as the forward:
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||
| None, | ||||||||||||||||||||||||||||||||||||
| dq, | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1330,13 +1330,10 @@ def forward( | |||||||||||||||||||||||||||||||||
| # check if there is padding between sequences when qkv_format='thd' | ||||||||||||||||||||||||||||||||||
| if pad_between_seqs is None: | ||||||||||||||||||||||||||||||||||
| if qkv_format == "thd": | ||||||||||||||||||||||||||||||||||
| pad_between_seqs = ( | ||||||||||||||||||||||||||||||||||
| cu_seqlens_q_padded is not None | ||||||||||||||||||||||||||||||||||
| and not torch.equal(cu_seqlens_q_padded[:-1], cu_seqlens_q[:-1]) | ||||||||||||||||||||||||||||||||||
| ) or ( | ||||||||||||||||||||||||||||||||||
| cu_seqlens_kv_padded is not None | ||||||||||||||||||||||||||||||||||
| and not torch.equal(cu_seqlens_kv_padded[:-1], cu_seqlens_kv[:-1]) | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| # THD + CUDA Graph fix: torch.equal() triggers GPU->CPU sync, | ||||||||||||||||||||||||||||||||||
| # which is forbidden during CUDA graph capture. | ||||||||||||||||||||||||||||||||||
| # pad_between_seqs=True is always safe for THD with padded cu_seqlens. | ||||||||||||||||||||||||||||||||||
| pad_between_seqs = True | ||||||||||||||||||||||||||||||||||
|
Comment on lines
1332
to
+1336
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The original logic returned
Suggested change
|
||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| pad_between_seqs = False | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import torch as _torchtorchis already imported at the top of this module. The local alias_torchadds no value and makes the code harder to grep. The same pattern appears in the backward block at line ~2754.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!