perf(gated_delta_net): fold q/k L2-norm into the gated_delta_rule kernel#5396
Draft
yuchenwang3 wants to merge 1 commit into
Draft
perf(gated_delta_net): fold q/k L2-norm into the gated_delta_rule kernel#5396yuchenwang3 wants to merge 1 commit into
yuchenwang3 wants to merge 1 commit into
Conversation
GatedDeltaNet L2-normalizes q/k with an explicit l2norm(query_key) before the FLA kernel while passing use_qk_l2norm_in_kernel=False, materializing a normalized query_key [B,T,2*Hk,128] activation kept for backward. Folding it into the kernel (use_qk_l2norm_in_kernel=self.use_qk_l2norm) lets FLA keep only rstd [B,T,H] and recompute normalized q/k in backward, removing that activation. GatedDeltaNet is ~3/4 of layers in Qwen3.5-class hybrids -> meaningful backward activation saving at long context. Lossless: eps=1e-6 on all paths (FLA l2norm / in-kernel l2norm_fwd / torch deterministic), matching the previous explicit l2norm default. GQA: per-head l2norm commutes with repeat_interleave, so in-kernel (post-repeat Hv) == previous explicit (pre-repeat Hk) per head. use_qk_l2norm=False unchanged. Signed-off-by: yuchenwang3 <eang333cms@gmail.com>
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.
Background. Found while running ms-swift + Megatron-Core SFT of Qwen3.5-35B-A3B (GatedDeltaNet hybrid) at 128K context on 16× B200 across 2 nodes (2×8); this change ran in that real training.
What
GatedDeltaNetL2-normalizes q/k with an explicitl2norm(query_key)before the FLAgated_delta_rulekernel, while passinguse_qk_l2norm_in_kernel=False. This materializes a normalizedquery_key[B, T, 2*Hk, 128]activation that must be kept for backward.Folding the L2-norm into the kernel (
use_qk_l2norm_in_kernel=self.use_qk_l2norm) lets FLA keep only the smallrstd[B, T, H]vectors and recompute normalized q/k in backward vial2norm_bwd, removing the materialized normalized-q/k activation. GatedDeltaNet is ~3/4 of the layers in Qwen3.5-class hybrids, so this is a meaningful backward-activation saving at long context.Numerically lossless
l2normdefaulteps=1e-6; FLA in-kernell2norm_fwddefaulteps=1e-6; the torch deterministic path uses expliciteps=1e-6— same as the previous explicitl2norm()default.dim=-1;repeat_interleaveonly duplicates heads, sol2norm(repeat_interleave(x)) == repeat_interleave(l2norm(x)). The in-kernel path normalizes post-repeat (Hv) heads; the previous explicit path normalized pre-repeat (Hk) heads — numerically identical per head.use_qk_l2norm=False, behavior is unchanged (kernel receivesFalse, no norm).Testing
Ran in real Qwen3.5-35B-A3B 128K SFT on 16× B200 (2 nodes). I could not run Megatron's GPU/FLA test suite locally (no GPU / no triton+FLA on my machine), so relying on CI.
Question for maintainers
The flag was hardcoded
Falsewith an explicit pre-kernell2norm. If that was intentional for a specific path (e.g.cu_seqlens/packed-sequence or CP correctness of the in-kernel l2norm), please advise — happy to gate the fold behind a condition instead.