Repository navigation
Keep sigmoid(beta) in fp32 in GDN/KDA decode kernels - #41352
Open
flyingpetals520 wants to merge 1 commit into
Open
flyingpetals520 wants to merge 1 commit into
flyingpetals520 wants to merge 1 commit into
Conversation
flyingpetals520
requested review from
hebiao064,
yizhang2077 and
yuan-luo
as code owners
September 26, 2026 13:18
flyingpetals520
force-pushed
the
fix/decode-keep-beta-fp32
branch
from
September 26, 2026 13:40
8f9e890 to
fc91a17
Compare
The GDN packed decode kernel and the GDN/KDA linear-replay decode kernel
rounded sigmoid(beta) through the activation dtype:
beta_val = tl.sigmoid(b_val).to(b.dtype.element_ty).to(tl.float32)
The rounded beta feeds the delta-rule update of the persistent SSM state,
so the rounding error enters the state once per decode step and accumulates
with context length: after 1000 decode steps the state rel-L2 vs a
beta-in-fp32 recurrence is ~1.6e-3, vs ~1e-7 with the round-trip removed.
The failure is silent -- outputs stay well-formed and per-op tolerances
stay green.
The same line was fixed in vLLM in vllm-project/vllm#53877 (the packed
decode kernel originated there; sglang#20627 imported it pre-fix) but was
never ported. Both sites now keep beta in fp32, matching what the KDA
packed decode kernel in the same file already does.
fused_recurrent_linear_replayssm.py:138 sits before the IS_KDA branch, so
this fixes both the GDN and KDA replay paths.
Regression tests:
- degenerate single-step checks for both kernels: with zero initial state
and unit inputs the stored state equals sigmoid(b) itself, so the state
pool reads the (non-)round-trip directly; asserted against the fp32
value and away from the activation-dtype-rounded value
- 1000-step packed-decode drift test vs a beta-in-fp32 torch recurrence
(assert rel-L2 < 1e-4; measured 1.05e-7 after the fix, 1.65e-3 before)
Verified locally on CUDA in bf16 and fp16.
Fixes sgl-project#38975
flyingpetals520
force-pushed
the
fix/decode-keep-beta-fp32
branch
from
September 26, 2026 14:04
fc91a17 to
13e5dcf
Compare
Author
|
Hi maintainers, emm this is my first contribution to SGLang. Could someone help add the run-ci label? Happy to make any changes needed. (What a pity currently I don't have AMD RoCm platform to run and test) Thanks! |
This branch has not been deployed
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.
The GDN packed decode kernel and the GDN/KDA linear-replay decode kernel rounded sigmoid(beta) through the activation dtype:
The rounded beta feeds the delta-rule update of the persistent SSM state, so the rounding error enters the state once per decode step and accumulates with context length: state rel-L2 reaches 1.4e-3 after 1000 decode steps, vs 1e-7 with the round-trip removed. The failure is silent (outputs stay well-formed, per-op tolerances stay green).
The same line was fixed in vLLM in vllm-project/vllm#53877 (the packed decode kernel originated there; sglang#20627 imported it pre-fix) but was never ported. Both sites now keep beta in fp32, mirroring the form the KDA packed decode kernel in the same file already uses deliberately. fused_recurrent_linear_replayssm.py:138 sits before the IS_KDA branch, so this fixes both the GDN and KDA replay paths.
Adds regression tests:
Verified locally on CUDA in bf16 and fp16.
Fixes #38975
Motivation
Fixes #38975 — silent precision loss in the GDN/KDA decode kernels that grows with context length.
Modifications
fused_recurrent.py: drop the.to(b.dtype.element_ty)round-trip in the GDN packed decode kernelfused_recurrent_linear_replayssm.py: same fix in the GDN/KDA linear-replay decode kerneltest/registered/kernels/ops/attention/test_gdn_kda_decode_beta_fp32.py: new regression testsAccuracy Tests
Verified locally on CUDA in bf16 and fp16 (
pytest test/registered/kernels/ops/attention/test_gdn_kda_decode_beta_fp32.py):The oracles keep beta in fp32 on purpose — a reference that computes
torch.sigmoid(b.float()).to(dtype)carries the same round-trip and is blind to this bug.Speed Tests and Profiling
Checklist
Review and Merge Process
/tag-and-rerun-ci,/tag-run-ci-label,/rerun-failed-ciCI States
Latest PR Test (Base): ❌ Run #36247264110
Latest PR Test (Extra): ❌ Run #36247263942
Latest PR Test (AMD ROCm 10): ❌ Run #36247264099