Skip to content

fix(norm): convert float2 to e4m3 directly in packed cast - #4167

Merged
bkryu merged 1 commit into
flashinfer-ai:mainfrom
elwhyjay:fix/layernorm-quant-e4m3-packed-cast
Jul 27, 2026
Merged

bkryu merged 1 commit into
flashinfer-ai:mainfrom
elwhyjay:fix/layernorm-quant-e4m3-packed-cast

Conversation

@elwhyjay

@elwhyjay elwhyjay commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

📌 Description

layernorm_quant can miss the FP8 e4m3 correctness bound when the quantization scale is small. Reported for A10/SM86 in the nightly pipeline.

The packed float2 to __nv_fp8x2_e4m3 cast in cudaTypeUtils.cuh rounded through bf16 before quantizing:

return __nv_fp8x2_e4m3(bf1622float2(float22bf162(val)));

generalLayerNorm applies the per-tensor scale before this cast, so the mantissa bits dropped by the bf16 step are scaled up by 1/scale. At scale=0.01 that is a 100x amplification, enough to push values into a neighbouring e4m3 bucket. The test reference models the bf16 rounding that the kernel does before scaling, but not this second rounding after it, so the two diverge.

This PR converts directly with __nv_cvt_float2_to_fp8x2, matching the style already used in vec_dtypes.cuh. The e5m2 packed cast already converts without a bf16 round trip, so it is unchanged, which matches the failures being e4m3 only. The cast is only reachable through generalLayerNorm, whose sole caller is LayerNormQuant, so layernorm_quant is the only affected API.

This PR also seeds test_layernorm_quant. The bf16 round trip is architecture independent, so the error was always present on the packed path; the check bounds the fraction of drifting elements, and unseeded inputs left that fraction varying per run, which is why it surfaced as an intermittent SM86 failure.

🔍 Related Issues

#4160

🚀 Pull Request Checklist

Thank you for contributing to FlashInfer! Before we review your pull request, please make sure the following items are complete.

✅ Pre-commit Checks

  • I have installed pre-commit by running pip install pre-commit (or used your preferred method).
  • I have installed the hooks with pre-commit install.
  • I have run the hooks manually with pre-commit run --all-files and fixed any reported issues.

If you are unsure about how to set up pre-commit, see the pre-commit documentation.

🧪 Tests

Verified on an RTX A6000 (SM86, same architecture as the A10 in the nightly pipeline).

Before and after, over 30 seeds per shape at scale=0.01, e4m3, batch 1:

hidden seeds over 1% bound (before) after worst mismatch (before) after
500 4/30 0/30 1.20% 0.00%
1024 3/30 0/30 1.27% 0.00%

The two cases named in the report, hidden=500 and hidden=1024 at scale=0.01, pass after the fix.

  • Full tests/utils/test_norm.py: 1543 passed, 1344 skipped. Skips are PDL cases that need Hopper or newer.
  • pytest tests/trace/ -k "norm": 123 passed, 21 skipped.
  • pre-commit run on the changed files: all hooks pass.

Unrelated pre-existing failures: pytest tests/trace/ -k "rope" reports 24 failures on this machine both with and without this change, so they are not caused by it.

  • Tests have been added or updated as needed.
  • All tests are passing (unittest, etc.).

Reviewer Notes

Summary by CodeRabbit

  • Bug Fixes

    • Improved FP8 conversion accuracy and handling by converting values directly with saturation.
  • Tests

    • Stabilized quantized layer normalization test results by making input generation deterministic.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 36745bb8-63ee-490a-b90e-81b12aaa3ef1

📥 Commits

Reviewing files that changed from the base of the PR and between 4b969c9 and b8406cf.

📒 Files selected for processing (2)
  • include/flashinfer/trtllm/common/cudaTypeUtils.cuh
  • tests/utils/test_norm.py

📝 Walkthrough

Walkthrough

The FP8x2 E4M3 conversion now uses direct finite-saturated conversion instead of a BF16 intermediate. The layernorm quantization test initializes a fixed Torch RNG seed.

Changes

FP8 conversion stabilization

Layer / File(s) Summary
Direct FP8 conversion and deterministic validation
include/flashinfer/trtllm/common/cudaTypeUtils.cuh, tests/utils/test_norm.py
float2 values convert directly to __nv_fp8x2_e4m3 with finite saturation and E4M3 formatting; test_layernorm_quant uses a fixed Torch RNG seed.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related issues

Possibly related PRs

Suggested labels: op: misc

Suggested reviewers: saltyminty, yzh119, bkryu

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states the main change: direct float2-to-e4m3 conversion in the packed cast.
Description check ✅ Passed The description includes the required sections, related issue, checklist, and test results, with only optional reviewer notes left blank.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kahyunnam kahyunnam added run-ci op: misc norm, activation, sampling, RoPE, quantization, etc. labels Jul 27, 2026
@kahyunnam

Copy link
Copy Markdown
Member

/bot run tests/utils

@flashinfer-bot

Copy link
Copy Markdown
Collaborator

GitLab MR !1051 has been created, and the CI pipeline #59823527 is currently running. I'll report back once the pipeline job completes.

@kahyunnam kahyunnam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the follow up fix @elwhyjay ! Approving, I can help merge after internal CI run clears.

@bkryu bkryu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @elwhyjay, the internal CI looks good to me

@bkryu
bkryu enabled auto-merge (squash) July 27, 2026 22:49
@bkryu
bkryu merged commit e683e30 into flashinfer-ai:main Jul 27, 2026
43 of 57 checks passed
aleozlx added a commit that referenced this pull request Jul 28, 2026
….6.16rc2 (#4197)

Cherry-picks for the `release-v0.6.16` branch, plus the version bump to
`0.6.16rc2`.

## Commits

| Commit | Source | Status |
| --- | --- | --- |
| `fix(norm): convert float2 to e4m3 directly in packed cast` | #4167
(`e683e307` on `main`) | merged upstream |
| `feat(xqa): ragged Q and per-row sliding-window masking for
speculative decode` | #4137 (`60783fb9` on `main`) | merged upstream |
| `test(jit): assert BMM export symlink under GEN_SRC_DIR` | #4187
(`417bbd29` on `main`) | merged upstream |
| `feat(mla): support packed low-head and variable-Q decode` | #4178 (PR
head `0774943c`) | **not yet merged** to `main` |
| `bump version to 0.6.16rc2` | — | — |

Applied in `main` merge order; #4187 sits directly on top of #4137
upstream, so that ordering is preserved.

## Notes

- **#4189 was not cherry-picked** — it was merged directly into
`release-v0.6.16` (`34368112`) and is already the base of this branch.
- **#4178 is still open against `main`.** It was cherry-picked at the
request of the release owner. Its two head commits (`b6cc59491`,
`0774943c2`) are squashed into one commit here, mirroring how it will
land upstream. If the PR changes before merge, this commit should be
dropped and re-picked.
- The three merged cherry-picks apply cleanly with patch-ids identical
to their sources. The #4178 squash is byte-identical to the PR's
combined diff.
- The #4178 commit was committed with `--no-verify`: the `mypy`
pre-commit hook reports two pre-existing `[no-redef]` errors in
`flashinfer/mla/_core.py` (conditional `if/else` imports of
`_check_can_implement` / `_get_split_kv_and_workspace_size`). These
reproduce identically on PR #4178's own branch and on the
`release-v0.6.16` base — they are not introduced by this cherry-pick,
and surface only when those modules are checked together. Left unfixed
here to keep the cherry-pick faithful.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Lee Yongjun <jqueen.astro@gmail.com>
Co-authored-by: yichengj <yichengj@nvidia.com>
Co-authored-by: bryu <bryu@nvidia.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Ka-Hyun Nam <knam@nvidia.com>
Co-authored-by: mingyangw <mingyangw@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

op: misc norm, activation, sampling, RoPE, quantization, etc. run-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants