Skip to content

[split 4/4] Enable DSA CP and THD hooks - #5246

Merged
HollowMan6 merged 3 commits into
NVIDIA:mainfrom
HollowMan6:split-3674-dsa-cp-thd-core
Jun 25, 2026
Merged

[split 4/4] Enable DSA CP and THD hooks#5246
HollowMan6 merged 3 commits into
NVIDIA:mainfrom
HollowMan6:split-3674-dsa-cp-thd-core

Conversation

@HollowMan6

@HollowMan6 HollowMan6 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Split out from #3674 to reduce review scope.

Original changes by @HollowMan6 in #3674.

Scope

  • Refactor DSA around backend-neutral fused-kernel hooks.
  • Add DSA layout and masking helpers for allgather CP and packed THD.
  • Add optional DSA kernel backend config and DSA indexer compatibility knobs.
  • Remove the DSA-specific MLA call path from generic multi_latent_attention.py.
  • Wire GPT DSA specs to absorbed MLA and add DSA-focused unit coverage.

Dependencies

CODEOWNERS note

megatron/training/arguments.py has an exact CODEOWNERS entry with no owners after the general megatron/training/ rule, so this PR includes that one-line skip-field update here and calls out the ownership ambiguity explicitly.

Validation

  • git diff --check upstream/main..split-3674-dsa-cp-thd-core

@copy-pr-bot

copy-pr-bot Bot commented Jun 9, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4245d4d58b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

linear_kv_up_proj=backend.column_parallel_linear(),

P1 Badge Provide absorbed MLA submodule names

With experimental_attention_variant == "dsa", this now instantiates AbsorbedMLASelfAttentionSubmodules, but the spec still passes the old MLA field linear_kv_up_proj. That dataclass defines linear_k_up_proj and linear_v_up_proj instead, so constructing the DSA module spec raises an unexpected-keyword TypeError before any model can be built in this path. Please split the absorbed K/V up projections into the field names expected by AbsorbedMLASelfAttentionSubmodules.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@HollowMan6
HollowMan6 force-pushed the split-3674-dsa-cp-thd-core branch 4 times, most recently from 888e0ec to 2553904 Compare June 12, 2026 18:16

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

linear_kv_up_proj=backend.column_parallel_linear(),

P1 Badge Use the absorbed MLA submodule names

With this switch to AbsorbedMLASelfAttentionSubmodules, constructing the DSA module spec now passes linear_kv_up_proj, but that dataclass only accepts linear_k_up_proj and linear_v_up_proj. As a result, any caller of get_dsa_module_spec_for_backend() with experimental_attention_variant="dsa" fails immediately with an unexpected-keyword TypeError before the model can be built.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread megatron/core/transformer/experimental_attention_variant/dsa.py
Comment thread megatron/core/transformer/experimental_attention_variant/dsa.py
Comment thread megatron/core/transformer/experimental_attention_variant/dsa.py
Comment thread megatron/core/transformer/transformer_config.py

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 181710c61c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread megatron/core/transformer/multi_latent_attention.py
Comment thread megatron/core/transformer/multi_latent_attention.py
Comment thread megatron/core/transformer/multi_latent_attention.py Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7062b3878a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +20 to +23
_BACKEND_MODULE_NAME_BY_BACKEND = {
"tilelang": "megatron.core.transformer.experimental_attention_variant.dsa_tilelang_kernels",
"cudnn": "megatron.core.transformer.experimental_attention_variant.dsa_cudnn_kernels",
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Add the missing fused DSA backend modules

When users set dsa_kernel_backend to tilelang or cudnn, use_fused_dsa_kernels() returns true and the first fused hook calls _load_backend(), but these module names do not exist in this tree (I checked repo-wide with rg --files for dsa_tilelang_kernels, dsa_cudnn_kernels, and tilelang_dsa). In that configuration the forward path raises RuntimeError: Failed to import DSA kernel backend ... instead of using the advertised optional backend or falling back, so either include/register these backend modules here or keep those choices disabled until they are available.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those will be in #5049 and #5099

@HollowMan6

Copy link
Copy Markdown
Contributor Author

/claude review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@HollowMan6

Copy link
Copy Markdown
Contributor Author

/ok to test 8f75c5c

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why the changes in this file are necessary? In particular, why do we need to manually construct this checkpoint_inputs list and specifically overwrite the attn_mask_type value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added code comments for the explanation. Some more details here:

The change is needed for activation checkpointing (recompute) when the core attention needs extra tensor inputs, specifically the DSA path in this case. In the DSA/MLA flow, core_attention_extra_kwargs can contain tensors like x=hidden_states and qr=q_compressed. Those tensors are used inside core_attention and must be explicit inputs to tensor_parallel.checkpoint; otherwise they are captured from the Python closure, which can either defeat checkpointing by retaining the original forward tensors or give incorrect/backward-fragile behavior because checkpoint recompute only detaches and returns gradients for tensors passed as checkpoint inputs.

attn_mask_type is handled specially because it is metadata semantically, but Megatron’s checkpoint wrapper saves its *args via ctx.save_for_backward(*args), so the checkpoint argument list must contain tensors, not an AttnMaskType enum. We preserve the existing behavior from main by encoding the enum value as a small tensor before calling checkpoint, then converting it back inside custom_forward with AttnMaskType(attn_mask_type.item()).

The overwrite of checkpoint_inputs[5] is just because the list is initially constructed with a placeholder before defaulting attn_mask_type and converting it to the tensor form. It is not changing the mask type; it replaces None/enum metadata with the tensor representation required by checkpointing.

Comment thread megatron/core/transformer/attention.py Outdated
if seq_lens.numel() == 0:
return torch.empty(0, dtype=torch.int64, device=device)

if cu_seqlens_i64.device.type == "cpu":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this block do? IIUC the sequence lengths are only known at runtime and vary per batch, so divisibility can only really be guaranteed by the data pipeline's padding and this block is a no-op during training, is that right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this block is a host-side validation guard for CPU/test/debug paths. Packed zigzag CP assumes each packed sequence is padded to a multiple of 2 * cp_size, so the rank-local front/back chunks are integral. In training we avoid checking CUDA runtime lengths here to prevent synchronization; the data pipeline is responsible for that padding guarantee. My preference is to keep it with the comment because it documents and validates the packed zigzag CP invariant without affecting CUDA training, but we can also get this removed if you prefer.



def build_causal_mask_from_positions(
query_pos: torch.Tensor, key_pos: torch.Tensor

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: it looks like key_pos is always an arange, is there a use case where key_pos is shuffled?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current main path, after gathered KV is restored to global order, key_pos is usually arange. I kept it explicit because this helper is a public function that meant to build masks from logical token positions rather than assuming KV storage order. That also covers cases where we build/apply the mask before KV reorder, or if a caller passes subset/reordered KV.

Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
Signed-off-by: Hollow Man <hollowman@opensuse.org>
@HollowMan6

Copy link
Copy Markdown
Contributor Author

/ok to test f58de5b

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/28195685815

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants