-
Notifications
You must be signed in to change notification settings - Fork 417
[megatron] 3/n towards Kimi K2.6: skip MLA THD value pad on sm100+ to keep fused attention trainable #2025
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
[megatron] 3/n towards Kimi K2.6: skip MLA THD value pad on sm100+ to keep fused attention trainable #2025
Changes from all commits
348c324
2f72c7b
28e3c69
3a0da81
7decddd
f751223
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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """Skip Megatron's MLA THD value pad on Blackwell (sm100+). | ||
|
|
||
| For packed (``qkv_format="thd"``) execution, Megatron-core pads the MLA value | ||
| tensor from ``v_head_dim`` (e.g. 128) up to the QK head dim (e.g. 192) in | ||
| ``_prepare_mla_core_attention_value`` and trims the attention output back | ||
| afterwards. | ||
|
|
||
| On Blackwell that pad is fatal for training: cuDNN fused attention has no | ||
| backward support for ``head_dim > 128`` on sm100+, so with the padded | ||
| ``head_dim_v == head_dim_qk == 192`` TransformerEngine disables FusedAttention | ||
| for training-mode forwards. FlashAttention 2 does not support MLA at all, | ||
| FlashAttention 3 is sm90-only, and UnfusedDotProductAttention does not support | ||
| context parallelism - so MLA + CP training raises | ||
| ``ValueError: No dot product attention backend is available``. Inference-mode | ||
| forwards (logprob computation) are unaffected, which makes the failure appear | ||
| only at the first ``forward_backward``. | ||
|
|
||
| cuDNN fused attention natively supports MLA's unequal QK/V head dims | ||
| (192/128), including THD + context parallelism with the ``p2p`` exchange, for | ||
| both forward and backward. Skipping the pad simply selects that native path | ||
| (and saves the pad/trim memory traffic). Behavior on pre-Blackwell devices is | ||
| left unchanged. | ||
| """ | ||
|
|
||
| from loguru import logger | ||
|
|
||
| _APPLIED = False | ||
|
|
||
|
|
||
| def patch_mla_thd_v_pad() -> None: | ||
| """Patch ``_prepare_mla_core_attention_value`` to skip the V pad on sm100+.""" | ||
| global _APPLIED | ||
| if _APPLIED: | ||
| return | ||
|
|
||
| import torch | ||
| from megatron.core.transformer import multi_latent_attention as mla | ||
|
|
||
| orig_prepare = mla._prepare_mla_core_attention_value | ||
|
|
||
| def patched_prepare(parallel_attention, query, value, packed_seq_params): | ||
| if ( | ||
| value is not None | ||
| and packed_seq_params is not None | ||
| and getattr(packed_seq_params, "qkv_format", None) == "thd" | ||
| and query.shape[-1] != value.shape[-1] | ||
| and torch.cuda.is_available() | ||
| and torch.cuda.get_device_capability() >= (10, 0) | ||
|
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. Calling and torch.cuda.is_available()
and torch.cuda.get_device_capability() >= (10, 0)
Contributor
Author
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. Done in 2f72c7b — CPU-only environments now fall through to the original pad path (the skip only matters where cuDNN fused attention runs). |
||
| ): | ||
| orig_v_dim = value.shape[-1] | ||
| return value, False, orig_v_dim, orig_v_dim | ||
| return orig_prepare(parallel_attention, query, value, packed_seq_params) | ||
|
|
||
| mla._prepare_mla_core_attention_value = patched_prepare | ||
| _APPLIED = True | ||
| logger.info("Applied Megatron MLA THD V-pad skip for sm100+ (native unequal-head-dim attention)") | ||
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.
Monkey-patching private methods of external libraries (like Megatron-core's
_prepare_mla_core_attention_value) can be fragile across library updates. If the method is renamed or removed in a future version, importing this module will raise anAttributeErrorand crash the application. It is safer to check for the existence of the attribute before patching it.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.
Deliberately not guarding this one: megatron-core is pinned to an exact revision (uv.lock / the deploy manifest), so the symbol can only disappear on an intentional pin bump — and in that case we want a loud AttributeError at import. Skipping the patch with a warning would instead resurface the failure this patch exists to fix ("No dot product attention backend is available" at the first forward_backward on sm100+), which is far harder to trace back to a missing patch.