Add Kimi Delta Attention (KDA) linear attention variant - #5769
Closed
CodersAcademy006 wants to merge 5 commits into
Closed
CodersAcademy006 wants to merge 5 commits into
CodersAcademy006 wants to merge 5 commits into
Conversation
Implement Kimi Delta Attention (KDA) from "Kimi Linear: An Expressive, Efficient Attention Architecture" (https://arxiv.org/abs/2510.26692) as a new linear-attention module alongside GatedDeltaNet. KDA refines the gated delta rule with a fine-grained, per-key-channel forget gate Diag(alpha_t) in place of GDN's scalar per-head decay: S_t = (I - beta_t k_t k_t^T) Diag(alpha_t) S_{t-1} + beta_t k_t v_t^T o_t = S_t^T q_t The recurrence, chunk-wise parallel form, q/k L2-normalization, beta sigmoid and the -exp(A_log) * softplus(f_proj(x) + dt_bias) decay are evaluated inside the flash-linear-attention chunk_kda kernel, used as the numerical reference. The module owns the separate q/k/v/beta projections, the two low-rank bottlenecks (f_proj for the fine-grained gate, g_proj for the output gate), the depthwise short convolution, the decay parameters (A_log, dt_bias) and the gated output RMSNorm. It is tensor-parallel sharded over heads with a matching sharded_state_dict for distributed checkpointing. Signed-off-by: Srijan Upadhyay <srjnupadhyay@gmail.com>
Add "kimi_delta_attention" as a selectable experimental_attention_variant. TransformerConfig gains the new Literal value and shares the linear-attention validation (conv kernel, head dims, head counts, GVA and TP divisibility) with gated_delta_net, since KDA has identical requirements. The spec registry adds get_kimi_delta_attention_module_spec, which builds the separate q/k/v/beta column-parallel projections, the low-rank f_proj/g_proj bottlenecks (replicated down, column-parallel up) and the gated output RMSNorm. Unlike GDN, the input layernorm is not fused into the projection so that every projection consumes the same normalized hidden states. KDA is also registered as a linear-attention variant so it follows the LA/SDPA layer pattern from linear_attention_freq. Signed-off-by: Srijan Upadhyay <srjnupadhyay@gmail.com>
Cover the forward pass (output shape and dtype) and the sharded_state_dict (TP-sharded A_log, dt_bias and conv weight, plus the child projection weights) across tensor-parallel and sequence-parallel configurations. The test is gated on flash-linear-attention being installed, matching test_gated_delta_net. Signed-off-by: Srijan Upadhyay <srjnupadhyay@gmail.com>
Signed-off-by: Srijan Upadhyay <srjnupadhyay@gmail.com>
CodersAcademy006
marked this pull request as ready for review
July 13, 2026 03:55
50 tasks
This was referenced Aug 17, 2026
6 tasks
Contributor
|
@CodersAcademy006 thanks for the efforts! we're adding KDA here moving forward: #6877 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds Kimi Delta Attention (KDA) as a new
experimental_attention_variant, implementing the algorithm from Kimi Linear: An Expressive, Efficient Attention Architecture (https://arxiv.org/abs/2510.26692). This is the clean, single-purpose replacement requested in #2446, superseding the earlier #2573 and #2674 (which did not implement the paper's KDA algorithm).The algorithm
KDA extends the gated delta rule with a fine-grained, per-key-channel forget gate
Diag(alpha_t)(a channel-wise vector in[0,1]^{d_k}) in place of GatedDeltaNet's scalar per-head decay (Eq. 1 of the paper):Following the maintainer guidance in #2446, the official Moonshot design and the flash-linear-attention
chunk_kdaoperator are used as the numerical reference. The recurrence, chunk-wise parallel form, q/k L2-normalization,betasigmoid and the-exp(A_log) * softplus(f_proj(x) + dt_bias)decay are all evaluated insidechunk_kda.What this PR adds
megatron/core/ssm/kimi_delta_attention.py—KimiDeltaAttention+KimiDeltaAttentionSubmodules, mirroring the structure ofgated_delta_net.py. The module owns the q/k/v/beta projections, the two low-rank bottlenecks (f_projfor the fine-grained gate,g_projfor the output gate), the depthwise short convolution, the decay parameters (A_log,dt_bias) and the gated output RMSNorm.TransformerConfig— newkimi_delta_attentionvalue forexperimental_attention_variant, sharing the linear-attention validation withgated_delta_net.experimental_attention_variant_module_specs.py—get_kimi_delta_attention_module_spec, dispatch, and registration as a linear-attention variant (so it follows thelinear_attention_freqLA/SDPA layer pattern, matching the paper's 3:1 KDA-to-global ratio).tests/unit_tests/ssm/test_kimi_delta_attention.py(forward +sharded_state_dict, TP/SP parametrized, FLA-gated).Design notes
f_proj/g_projbuilt with the established MLA/DSA pattern in this file: replicated (duplicated) down-projection, column-parallel up-projection.fuse_input_layernorm=Falseso every projection consumes the same normalized hidden states (KDA has multiple projection groups, unlike GDN's single fused input projection).num_key_headsand v/gate/beta atnum_value_heads; grouping is handled insidechunk_kda(norepeat_interleave).sharded_state_dictfor distributed checkpointing.Scope (this draft)
Focused on a correct, reviewable core. Context parallelism, packed (THD) sequences and inference/KV-cache are explicitly guarded with
NotImplementedErrorfor now and can be layered on following the GDN CP/THD machinery in a follow-up.Testing
chunk_kdacontract and the output is[s, b, h].black/isortclean.chunk_kdakernel requires CUDA + flash-linear-attention, so numerical correctness and TP/SP execution are left to CI / GPU (I don't have the hardware to run the FLA kernel or the TE-backed unit test locally). Happy to iterate on anything CI surfaces.Requires a flash-linear-attention build that exposes
fla.ops.kda.chunk_kda.Refs #2446. cc @sbhavani @BoxiangW — this is the clean replacement you asked for; happy to iterate on scope (CP/THD/inference) and on any fusion you'd prefer for the projections.