Repository navigation
perf(gated_delta_net): fold q/k L2-norm into the gated_delta_rule kernel - #5396
yuchenwang3 wants to merge 3 commits into
Conversation
|
@xuantengh pinging you as the most recent committer on |
|
Thanks for the contribution! We had a refactor to the GDN module recently in #5843, would you please rebase that and apply this to the new file? |
|
Done, @xuantengh — merged latest
Net diff vs main is the same two-line semantic change as before (+7/−4 with the comment block). Ready for another look. |
|
@xuantengh one more follow-up before I refresh this again: current main added GDN2 sharing |
b752055 to
39fd9a0
Compare
|
Rebased onto current main and adjusted the shared input helper added since the original PR: GDN now defers q/k normalization to its kernel, while GDN2 keeps the existing caller-side normalization. Local formatting, syntax, and routing checks pass. @xuantengh, could you re-review this head when you get a chance? |
|
Sorry for the delayed reply. Would you please add a test to |
8206dc3 to
398a42b
Compare
|
Done, @xuantengh. I added a small kernel-level parity check plus a BF16 end-to-end test that runs the real The new tests pass on all 8 ranks on 8×B200. While running the existing deterministic matrix I also caught and fixed a current FLA API mismatch in the newly activated fallback ( |
|
Hi @yuchenwang3 thanks for your contribution. Generally LGTM, but currently there is no way to specify |
398a42b to
edf2ff5
Compare
|
@xuantengh Thanks again for the reviews and enabling auto-merge. The CODEOWNERS check on |
Head branch was pushed to by a user without write access
|
@xuantengh The conflicts are resolved at |
|
/ok to test fb1eece |
|
Hi @yuchenwang3, please rebase the branch and check the conflicts with |
|
Rebased on main. The streamed fusion still handles q/k normalization, so the GDR kernel won’t normalize them again. Could you approve CI for the new head? |
|
/ok to test 18ae9ce |
Signed-off-by: Yuchen Wang <[email protected]>
Signed-off-by: Yuchen Wang <[email protected]>
|
Merged current main in |
|
@guihong-nv Could you run |
|
/ok to test 1c61820 |
|
/review |
svcnvidia-nemo-ci
left a comment
There was a problem hiding this comment.
Add a bit-exact replay for GDN2’s changed normalization branch
The changed GDN2 branch, reached with use_qk_l2norm_in_kernel=True, has no bit-exact replay test. The updated kernel replay exercises only GDN. Parameterizing the module deterministic test doesn't reach this branch because GDN2’s module call always passes False. The new explicit-versus-in-kernel test checks numerical parity, not byte-identical replay.
Add one focused replay beside the existing GDN replay. The manifest already registers GDN2 against that test file, so no registry or production-routing change is needed. This restores the required coverage for the changed branch; it isn't a reproduced numerical failure.
Proposed test, not executed; verify it before adopting:
def test_torch_chunk_gdn2_in_kernel_l2norm_replays_fwd_bwd():
from megatron.core.ssm.gated_delta_net.gdn2 import torch_chunk_gdn2
seeded()
shape = (2, 65, 4, 32)
q = torch.randn(shape, device="cuda", dtype=torch.bfloat16, requires_grad=True)
k = torch.randn_like(q).requires_grad_(True)
v = torch.randn(2, 65, 4, 48, device="cuda", dtype=torch.bfloat16, requires_grad=True)
g = (-torch.rand(shape, device="cuda") * 0.1).requires_grad_(True)
b = torch.rand(shape, device="cuda").requires_grad_(True)
w = torch.rand(2, 65, 4, 48, device="cuda").requires_grad_(True)
def fn(q, k, v, g, b, w):
return torch_chunk_gdn2(
q, k, v, g, b, w,
output_final_state=True,
use_qk_l2norm_in_kernel=True,
)
assert_replays_bit_exact(
fn, (q, k, v, g, b, w), replays=3,
contention=True, what="torch_chunk_gdn2 in-kernel L2 norm",
)Acceptance: the proposed test must produce byte-identical outputs, final states, and gradients for all six inputs across three runs. Retain the existing FP32/BF16 explicit-versus-in-kernel parity assertions and both GDN replay cases. GDN2’s module must continue normalizing in the caller and passing False to its kernel; enabling this direct-kernel test must not change that routing.
I completed the source-only light review across all eight changed files and their affected paths. No tests were executed.
Review generated by codex. This was a full review. Request a full review with /review reset=true.
Cover the use_qk_l2norm_in_kernel=True branch of torch_chunk_gdn2 with a fwd/bwd bit-exact replay across a chunk boundary, as suggested in review. GDN2's module routing is unchanged. Assisted-by: Claude Signed-off-by: Yuchen Wang <[email protected]>
|
@jiemingz Added the bit-exact replay for GDN2's in-kernel q/k normalization suggested in the review, in |
What
Move GatedDeltaNet q/k L2 normalization into the FLA
gated_delta_rulekernel instead of materializing normalized q/k before the call.The current GDN and GDN2 implementations share
_prepare_input_for_gated_delta_rule, but GDN2's kernel path does not enable in-kernel normalization. The rebased change therefore makes deferral an explicit helper option:use_qk_l2norm_in_kernelis enabled; fused preprocessing retains its existing normalization.Falseto its kernel.This preserves GDN2 behavior added after the original PR rather than silently removing its normalization during the rebase.
Why
Caller-side normalization leaves an additional pre-split normalized
query_keyactivation live for backward alongside the q/k tensors the gated-delta kernel already needs. Letting FLA own normalization removes that extra caller-side activation and keeps the normalization state with the kernel-owned q/k, which saves activation memory in long-context GDN-heavy hybrids.The numerical contract is unchanged:
l2normand the in-kernel path useeps=1e-6;use_qk_l2norm=Falseremains a no-op.Validation
Current head
Merged current
main(5df6168232) into head1c61820349on November 5, preserving the CP size/group routing added upstream. Eight CPU source-method checks pass for GDN/GDN2 output and gradient parity between explicit and in-kernel normalization, covering FP32/BF16 and two sequence lengths. Black, isort, Ruff, syntax parsing, whitespace and kernel determinism coverage checks pass. The CPU harness isolates the actual Torch methods; it does not execute FLA, CUDA, or distributed communication. GPU results below apply to earlier revisions.Earlier validation
The original change ran in Qwen3.5-35B-A3B 128K SFT on 16×B200. The earlier main replay passed Black, isort, Ruff, Pylint,
py_compile, andgit diff --check.The final
398a42b4head was also exercised with an 8-rank torchrun on 8×B200:GatedDeltaNet.forwardcomparison passed on every rank, covering outputs, input gradients, all parameter gradients, and both routing flags;Assisted-by: OpenAI Codex