Skip to content

feat: support packed TND context parallelism for KDA and GDN - #1263

Merged
liuchongming74 merged 1 commit into
mindspore-ai:masterfrom
xuxl1008:feat/linear-attention-tnd-cp
Oct 31, 2026
Merged

liuchongming74 merged 1 commit into
mindspore-ai:masterfrom
xuxl1008:feat/linear-attention-tnd-cp

Conversation

@xuxl1008

@xuxl1008 xuxl1008 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Paired: GitHub #1263 ↔ GitCode !1818

What type of PR is this?

/kind feature

What does this PR do / why do we need it:

Enable existing KDA and GDN context-parallel implementations to train on packed TND token streams. ShortConv and recurrent state reset at real document boundaries, while artificial CP cuts preserve state continuity. For example, with CP4, 4096 local tokens and a document boundary at global token 6145, rank 1 continues rank 0 for its first fragment and independently resets for its final fragment.

  • Share immutable packed metadata, local chunk indices and document-aware convolution across layer entry, local computation, checkpoint replay and backward.
  • Support KDA P2P, AllGather, grouped AllGather + P2P, and their two-/three-dimensional combinations with Ulysses; support GDN P2P and pure Ulysses. Reuse existing process groups and retain only the local transition matrix M per invocation.
  • Build the forward summary from the final local document fragment and the backward summary from the first fragment. Real resets cut state dependencies. Triton P2P skips messages at real document cuts and can send an independent summary early after an internal reset.
  • Include the GDN forward-state fixes from fix: correct GDN key-gate addressing and optional inputs #1224: key-gate layout, variable-length addressing, integer indices and optional gate/v_new branches. This does not claim support for gk-only backward.
  • Add Kimi/Qwen runtime metadata adapters and Qwen linear-decoder forwarding while preserving parameter FQNs. Qwen metadata handling and CP wrappers share adapter/distributed/context_parallel.py.
  • Use one public backend for attention and ShortConv in pure Ulysses. This PR exposes eager/Triton layer execution; Ascend C attention is the dependent increment in Add Ascend C backends for KDA state CP and KDA/GDN Ulysses #1331. The common convolution helper handles dense/packed layouts and per-invocation saved tensors.
  • Keep the fused kernel chunk width at 64 without requiring dense sequence lengths or packed document lengths to be divisible by 64. is_flat_input restores the caller's physical [T,D] or [B,T,D] layout; explicit offsets determine document boundaries.
  • Update the guides to the current configuration schema: match: "*.self_attn.forward", replace.kind: method, and replace.implementation. Decoder overrides must target linear-attention layers, not indiscriminately replace full-attention layers.

Configuration is documented in docs/guide/linear_attention_tnd_cp.md and docs/guide/kda_context_parallel.md. Dense/packed eager P2P consolidation and functional-module renaming remain separate work.

Validation:

  • Rebased onto master e0cdf62c9d66f1f9ec48c6f15df87dcf4ce80a5f. The rebased TND tree passes 6 existing packed-metadata tests. All 4 YAML blocks pass the actual Trainer resolver, including CP activation/deactivation and method-wrapper binding. AST, diff, catalog, changed documentation links and the repository's existing Pylint/Lizard rules pass. No new production test files were added in this follow-up.
  • Before this configuration-only follow-up, 15 existing CPU regressions passed. On this PR's implementation, CP4/local4097/H96/K=V128 P2P core comparisons used identical projected inputs and dO for dense versus single-document TND. KDA outputs and all input gradients were bitwise identical. GDN differed only in FP32 dg: maximum relative L2 difference 4.90e-7 and maximum absolute difference 1.29e-6.
  • Shared Ulysses code was tested in the dependent Add Ascend C backends for KDA state CP and KDA/GDN Ulysses #1331 worktree for KDA/GDN Triton layers with dense B2 and packed B1. Two outstanding calls, reverse-order backward and packed checkpoint replay matched ordinary execution bitwise for outputs and gradients. This explicitly records the execution location rather than claiming independent NPU reruns on both branches.
  • Earlier TND validation covered CP4/local4K, CP4/local8K and CP8/local8K protocol/lifecycle cases. Those historical results are not presented as a fresh full-training acceptance run on the rebased commit.

The supported scope is natural temporal ordering, contiguous equal-length nonempty CP shards, packed B1, TP=1 and training without inference caches. Full-attention layers in hybrid models need their own TND backend. Existing dense/packed convolution rounding differences are separate from the core comparison above. Numerical thresholds were not relaxed to obtain passing results.

Which issue(s) this PR fixes:

Fixes #1262

@xuxl1008

Copy link
Copy Markdown
Contributor Author

/check-pr

@xuxl1008

Copy link
Copy Markdown
Contributor Author

/retest

@mindspore-ai-bot

Copy link
Copy Markdown

🔵 The pipeline #1022 is running. Please wait a moment... (Link)

@mindspore-ai-bot

Copy link
Copy Markdown
Project Name Build_Stage Build Result Details
Hyper-parallel_Github_Gate - ❌ FAILURE 1022
Check_Pylint FAILURE 1022
Check_Lizard FAILURE 1022

@mindspore-ai-bot

Copy link
Copy Markdown

@xuxl1008, your gate pipeline failed at the code check stage. You can configure some code check tools locally following the (pre-push quick guide), to enable automatic checking when you push your code every time. Tools included are: Check_CMakelint, Check_Codespell, Check_Cpplint, Check_Lizard, Check_Pylint, Check_ClangFormat, Check_ShellCheck.

@xuxl1008

Copy link
Copy Markdown
Contributor Author

/retest

@mindspore-ai-bot

Copy link
Copy Markdown

🔵 The pipeline #1125 is running. Please wait a moment... (Link)

@mindspore-ai-bot

Copy link
Copy Markdown
Project Name Build_Stage Build Result Details
Hyper-parallel_Github_Gate - ❌ FAILURE 1125
Check_Lizard FAILURE 1125
Check_Pylint FAILURE 1125

@mindspore-ai-bot

Copy link
Copy Markdown

@xuxl1008, your gate pipeline failed at the code check stage. You can configure some code check tools locally following the (pre-push quick guide), to enable automatic checking when you push your code every time. Tools included are: Check_CMakelint, Check_Codespell, Check_Cpplint, Check_Lizard, Check_Pylint, Check_ClangFormat, Check_ShellCheck.

@xuxl1008

Copy link
Copy Markdown
Contributor Author

/check-pr

@xuxl1008

Copy link
Copy Markdown
Contributor Author

/retest

@mindspore-ai-bot

Copy link
Copy Markdown

🔵 The pipeline #1126 is running. Please wait a moment... (Link)

@mindspore-ai-bot

Copy link
Copy Markdown
Project Name Build_Stage Build Result Details
Hyper-parallel_Github_Gate - ✅ SUCCESS 1209

Comment thread docs/guide/linear_attention_tnd_cp.md Resolved
@xuxl1008
xuxl1008 force-pushed the feat/linear-attention-tnd-cp branch from 88757bc to ae728e2 Compare October 30, 2026 16:25
@xuxl1008

Copy link
Copy Markdown
Contributor Author

/retest

@mindspore-ai-bot

Copy link
Copy Markdown

🔵 The pipeline #1280 is running. Please wait a moment... (Link)

@mindspore-ai-bot

Copy link
Copy Markdown
Project Name Build_Stage Build Result Details(Full/Incremental) Detail
Coverage Line Coverage ✅ SUCCESS 72%/33% Link

@mindspore-ai-bot

Copy link
Copy Markdown
Platform Type Module Excution Time Time Limit
- pipeline_parallel 694.787s 600.0s
- shard 204.112s 300.0s
- auto_models 120.911s 150.0s
- activation_memory 132.4405s 120.0s
- optimizer 291.561s 300.0s
- init_weights 94.869s 118.75s
- swap_optimizer 332.47s 300.0s
- tensor_parallel 266.17s 300.0s
- accuracy 109.638s 120.0s
- fully_shard 373.573s 600.0s
- clip_grad 109.735s 120.0s
- context_parallel 82.26s 120.0s
- process_group 69.982s 90.0s
- llama3_examples 88.728s 120.0s
- distributed_checkpoint 98.727s 120.0s
- dtensor 98.985s 120.0s
- expert_parallel 150.26875s 120.0s
- multicore 123.61813s 120.0s

@mindspore-ai-bot

Copy link
Copy Markdown
Project Name Build_Stage Build Result Details
Hyper-parallel_Github_Gate - ✅ SUCCESS 1280

@liuchongming74 liuchongming74 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

Comment thread docs/guide/linear_attention_tnd_cp.md Resolved
@liuchongming74
liuchongming74 merged commit cb5d714 into mindspore-ai:master Oct 31, 2026
8 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] support packed TND context parallelism for KDA and GDN

4 participants