Skip to content

Keep sigmoid(beta) in fp32 in GDN/KDA decode kernels - #41352

Open
flyingpetals520 wants to merge 1 commit into
sgl-project:mainfrom
flyingpetals520:fix/decode-keep-beta-fp32
Open

flyingpetals520 wants to merge 1 commit into
sgl-project:mainfrom
flyingpetals520:fix/decode-keep-beta-fp32

Conversation

@flyingpetals520

@flyingpetals520 flyingpetals520 commented Sep 26, 2026 •

Copy link
Copy Markdown

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: 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:

  • 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 (rel-L2 < 1e-4; measured 1.05e-7 after the fix, 1.4e-3 before)

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 kernel
  • fused_recurrent_linear_replayssm.py: same fix in the GDN/KDA linear-replay decode kernel
  • test/registered/kernels/ops/attention/test_gdn_kda_decode_beta_fp32.py: new regression tests

Accuracy Tests

Verified locally on CUDA in bf16 and fp16 (pytest test/registered/kernels/ops/attention/test_gdn_kda_decode_beta_fp32.py):

  • degenerate single-step checks for both kernels: with zero initial state and unit inputs the stored state equals sigmoid(b) itself, asserted against the fp32 value and away from the activation-dtype-rounded value
  • 1000-step packed-decode drift vs a beta-in-fp32 torch recurrence: rel-L2 measured 1.05e-7 after the fix, 1.65e-3 before (assert < 1e-4)

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

  1. Ping Merge Oncalls to start the process. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
  4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

CI States

Latest PR Test (Base): ❌ Run #36247264110
Latest PR Test (Extra): ❌ Run #36247263942
Latest PR Test (AMD ROCm 10): ❌ Run #36247264099

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
flyingpetals520 force-pushed the fix/decode-keep-beta-fp32 branch from fc91a17 to 13e5dcf Compare September 26, 2026 14:04
@flyingpetals520

flyingpetals520 commented Sep 26, 2026 •

Copy link
Copy Markdown
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

No deployments
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.

[Bug] GDN packed decode and KDA replay decode round sigmoid(beta) through bf16; recurrent-state drift accumulates with context length

1 participant