Skip to content

[None][fix] Fix amax to avoid NaN issue in fp8_blockscale_gemm_kernel. - #11256

Merged
yuxianq merged 1 commit into
NVIDIA:mainfrom
yuxianq:fix-amax
Feb 5, 2026
Merged

yuxianq merged 1 commit into
NVIDIA:mainfrom
yuxianq:fix-amax

Conversation

@yuxianq

@yuxianq yuxianq commented Feb 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Refactor
    • Optimized FP8 block-scale GEMM kernels by simplifying scaling computation logic and removing conditional branches for more stable and efficient GPU computations.

Description

Test Coverage

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

/bot [-h] ['run', 'kill', 'skip', 'reuse-pipeline'] ...

Provide a user friendly way for developers to interact with a Jenkins server.

Run /bot [-h|--help] to print this help message.

See details below for each supported subcommand.

Details

run [--reuse-test (optional)pipeline-id --disable-fail-fast --skip-test --stage-list "A10-PyTorch-1, xxx" --gpu-type "A30, H100_PCIe" --test-backend "pytorch, cpp" --add-multi-gpu-test --only-multi-gpu-test --disable-multi-gpu-test --post-merge --extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx" --detailed-log --debug(experimental)]

Launch build/test pipelines. All previously running jobs will be killed.

--reuse-test (optional)pipeline-id (OPTIONAL) : Allow the new pipeline to reuse build artifacts and skip successful test stages from a specified pipeline or the last pipeline if no pipeline-id is indicated. If the Git commit ID has changed, this option will be always ignored. The DEFAULT behavior of the bot is to reuse build artifacts and successful test results from the last pipeline.

--disable-reuse-test (OPTIONAL) : Explicitly prevent the pipeline from reusing build artifacts and skipping successful test stages from a previous pipeline. Ensure that all builds and tests are run regardless of previous successes.

--disable-fail-fast (OPTIONAL) : Disable fail fast on build/tests/infra failures.

--skip-test (OPTIONAL) : Skip all test stages, but still run build stages, package stages and sanity check stages. Note: Does NOT update GitHub check status.

--stage-list "A10-PyTorch-1, xxx" (OPTIONAL) : Only run the specified test stages. Examples: "A10-PyTorch-1, xxx". Note: Does NOT update GitHub check status.

--gpu-type "A30, H100_PCIe" (OPTIONAL) : Only run the test stages on the specified GPU types. Examples: "A30, H100_PCIe". Note: Does NOT update GitHub check status.

--test-backend "pytorch, cpp" (OPTIONAL) : Skip test stages which don't match the specified backends. Only support [pytorch, cpp, tensorrt, triton]. Examples: "pytorch, cpp" (does not run test stages with tensorrt or triton backend). Note: Does NOT update GitHub pipeline status.

--only-multi-gpu-test (OPTIONAL) : Only run the multi-GPU tests. Note: Does NOT update GitHub check status.

--disable-multi-gpu-test (OPTIONAL) : Disable the multi-GPU tests. Note: Does NOT update GitHub check status.

--add-multi-gpu-test (OPTIONAL) : Force run the multi-GPU tests in addition to running L0 pre-merge pipeline.

--post-merge (OPTIONAL) : Run the L0 post-merge pipeline instead of the ordinary L0 pre-merge pipeline.

--extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx" (OPTIONAL) : Run the ordinary L0 pre-merge pipeline and specified test stages. Examples: --extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx".

--detailed-log (OPTIONAL) : Enable flushing out all logs to the Jenkins console. This will significantly increase the log volume and may slow down the job.

--debug (OPTIONAL) : Experimental feature. Enable access to the CI container for debugging purpose. Note: Specify exactly one stage in the stage-list parameter to access the appropriate container environment. Note: Does NOT update GitHub check status.

For guidance on mapping tests to stage names, see docs/source/reference/ci-overview.md
and the scripts/test_to_stage_mapping.py helper.

kill

kill

Kill all running builds associated with pull request.

skip

skip --comment COMMENT

Skip testing for latest commit on pull request. --comment "Reason for skipping build/test" is required. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break.

reuse-pipeline

reuse-pipeline

Reuse a previous pipeline to validate current commit. This action will also kill all currently running builds associated with the pull request. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break.

@yuxianq
yuxianq requested a review from chang-l February 4, 2026 03:54
@yuxianq

yuxianq commented Feb 4, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@yuxianq
yuxianq requested a review from wm2012011492 February 4, 2026 03:56
@coderabbitai

coderabbitai Bot commented Feb 4, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Modified FP8 blockscale GEMM kernel to replace conditional scaling logic with uniform clamping. All scale computations now clamp amax to at least 1e-10 before division by 448, eliminating zero-branch handling and a new header inclusion.

Changes

Cohort / File(s) Summary
FP8 Blockscale GEMM Kernel
cpp/tensorrt_llm/kernels/cutlass_kernels/fp8_blockscale_gemm/fp8_blockscale_gemm_kernel.cuh
Added cudaTypeUtils.cuh inclusion. Replaced conditional zero-handling (amax == 0 ? 1.f : 448.f / amax) with uniform max(amax, 1e-10) clamping followed by 448/amax division across scale_1x128_kernel, scale_1x128_reshape_kernel, and scale_128x128_kernel functions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is incomplete. It contains only the template with no actual content in the Description, Test Coverage, or critical PR Checklist fields (except a checked box at the end). Add a clear Description explaining the issue and solution, list Test Coverage details, and fill out relevant PR Checklist items.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: fixing amax to avoid NaN issues in fp8_blockscale_gemm_kernel, which aligns with the actual code changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Important

Action Needed: IP Allowlist Update

If your organization protects your Git platform with IP whitelisting, please add the new CodeRabbit IP address to your allowlist:

  • ✨ 136.113.208.247/32 (new)
  • 34.170.211.100/32
  • 35.222.179.152/32

Reviews will stop working after February 8, 2026 if the new IP is not added to your allowlist.


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 and usage tips.

@yuxianq
yuxianq requested review from lucifer1004 and removed request for chang-l February 4, 2026 03:57
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #34731 [ run ] triggered by Bot. Commit: 5bdd2c5

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #34828 [ run ] triggered by Bot. Commit: 5bdd2c5

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #34828 [ run ] completed with state SUCCESS. Commit: 5bdd2c5
/LLM/main/L0_MergeRequest_PR pipeline #26866 completed with status: 'SUCCESS'

@yuxianq
yuxianq merged commit d3d951d into NVIDIA:main Feb 5, 2026
7 of 8 checks passed
SchumiDing pushed a commit to SchumiDing/TensorRT-LLM that referenced this pull request Feb 6, 2026
inciaf pushed a commit to inciaf/trtllm-energy-monitoring that referenced this pull request Feb 18, 2026
aleozlx pushed a commit to flashinfer-ai/flashinfer that referenced this pull request Nov 16, 2026
<!-- .github/pull_request_template.md -->

## 📌 Description

Four fixes to the TRT-LLM CUTLASS MoE / GEMM sources under
`csrc/nv_internal`, from upstream TensorRT-LLM commits FlashInfer's
copies predate, plus one FlashInfer-side fix in the same quantization
kernel.

- ~~PDL wait without a memory clobber (NVIDIA/TensorRT-LLM#9695, header
part)~~ dropped at review (`2419e49d6`): the header it changed,
`cutlass_extensions/arch/grid_dependency_control.h`, is included by
nothing in this tree. The MoE kernels (and TRT-LLM's own pingpong GEMM)
take `cutlass/arch/grid_dependency_control.h` from the CUTLASS
submodule, which is bare at pin `b46b16d0` and already carries the
clobber on CUTLASS main; the fix arrives with the submodule bump, not
here.
- **NaN from `448 / amax`**
([NVIDIA/TensorRT-LLM#11256](NVIDIA/TensorRT-LLM#11256)).
The 1x128 and 128x128 block-scale quantization kernels in
`fp8_blockscale_gemm_kernel.cuh` guarded only `amax == 0`; a tiny
nonzero amax gave an inf scale and NaN output. `amax` is clamped to
`1e-10` first.
- **`util_convert_type` returned a dangling fragment**
([NVIDIA/TensorRT-LLM#20017](NVIDIA/TensorRT-LLM#20017)).
`moe_cute_util.cuh` built the result tensor over a stack
`cutlass::Array` that goes out of scope on return; it now copies into a
tensor that owns its storage, with a static assert that the input is
contiguous.
- **SM120 / SM121 launch check**
([NVIDIA/TensorRT-LLM#12704](NVIDIA/TensorRT-LLM#12704),
launcher part). Consumer Blackwell has 99 KiB of shared memory per
block; both TMA warp-specialized launchers now check the kernel's
`SharedStorage` against `cudaDevAttrMaxSharedMemoryPerBlockOptin` before
launching, so an oversized tile fails with a message instead of a launch
error. Upstream's heuristic-level filter (keep only
`CtaShape128x128x64B`) is not taken: FlashInfer's SM120 list already
drops the `...x64B` tiles for FP8 activations, so the filter left W4A8
with no tactic on RTX PRO 6000 / Spark (first push of this PR), and the
`128x128x128B` tile it relies on fits there.
- **`scale_128x128_kernel` reduced its amax over the wrong rows**
(FlashInfer-side, found in review). The reduction loop bounded the
column check with the row index `i` instead of the inner index `j` (the
write loop already used `j`): `scales_idx_x * 128 + i * 32 + lane_id >=
dim_x` breaks out of the reduction at row `i >= (dim_x - scales_idx_x *
128) / 32`, so for any `dim_x` below about 4200 every 128x128 block's
amax came from its first rows only and the remaining rows saturate at
448. Upstream still has `i`; FlashInfer's 2025-10 import carried it over
and this fix is FlashInfer-first. Only the internal BF16-weight
quantization path (`weight_scale=None`) is affected, and that kernel now
reads all 128 rows of a block for the amax where it read the first few;
its write loop already read the whole block, so the kernel is at most
about 2x its old (wrong) cost on that path only. The amax floor is
applied in fp32 so a half instantiation would not flush `1e-10` to 0.

No kernel arithmetic changes except the amax clamp and the reduction
bound above.

## 🔍 Related Issues


[NVIDIA/TensorRT-LLM#9695](NVIDIA/TensorRT-LLM#9695),
[NVIDIA/TensorRT-LLM#11256](NVIDIA/TensorRT-LLM#11256),
[NVIDIA/TensorRT-LLM#20017](NVIDIA/TensorRT-LLM#20017),
[NVIDIA/TensorRT-LLM#12704](NVIDIA/TensorRT-LLM#12704)
(launcher part).
[NVIDIA/TensorRT-LLM#11561](NVIDIA/TensorRT-LLM#11561)
is not needed: FlashInfer gates the SM120 TMA warp-specialized path on
`isValidSM120MOESpecialisation`, so pure-int weight-only configs never
reach the SM120 list.

## 🚀 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

- [x] I have installed `pre-commit` by running `pip install pre-commit`
(or used your preferred method).
- [x] I have installed the hooks with `pre-commit install`.
- [x] 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](https://pre-commit.com/).

## 🧪 Tests

- [x] Tests have been added or updated as needed.
- [x] All tests are passing (`unittest`, etc.).

New: `test_fp8_blockscale_gemm_weight_amax_covers_whole_block` (SM90a):
weight blocks whose rows 0-3 are tiny and whose other rows sit near the
FP8 maximum, against the BF16 reference; a reduction that stops after
the first rows saturates the block. It runs in the GitLab H100 job (the
file is SM90-only and skips on B200). For the upstream fixes no new
tests: the SM120 / SM121 launch check needs consumer Blackwell hardware,
the dangling fragment is undefined behavior that happens to work today,
and the NaN needs an amax below `1e-10`. Run on B200 (CUDA 13.0 CI
container): `tests/gemm/test_fp8_blockscale_gemm.py` and
`tests/moe/test_trtllm_cutlass_fused_moe.py -k "test_moe[ or
test_moe_fp8_block_scaling or test_moe_w4a8[ or test_moe_fp8[ or
test_moe_nvfp4["`: 56 passed, 86 skipped (SM90-only and Hopper-only
variants), at FlashInfer `e3ebb9e7dd` + this commit.

## 🔬 Experimental Track

<!-- Only for PRs submitted under the experimental policy
(CONTRIBUTING.md → "Experimental APIs and Backends").
     Leave this section untouched for normal PRs. -->

- [ ] This PR is **experimental**: it adds or changes code under
`flashinfer/experimental/` and/or an `@flashinfer_experimental_api`.
Tracking issue: #
- [ ] The tracking issue names an owner, the reason for the experimental
path, and a graduation plan with a target release.
- [ ] Core changes are limited to a thin entry point (signature, shared
validation, feature-gate check, backend selection, handoff).
- [ ] Tests live in `tests/experimental/` and were validated on the
intended hardware; a runnable example is included.
- [ ] Nothing is registered in `flashinfer/aot.py`, and no experimental
backend is reachable from `backend="auto"` without
`FLASHINFER_ALLOW_EXPERIMENTAL_AUTO_BACKENDS=1`. (Calling an
`@flashinfer_experimental_api` or naming a backend explicitly is itself
the opt-in and needs no environment variable.)
- [ ] **Test scope declared below.** The experimental CI lane runs
exactly these targets, so keep them as narrow as the change allows.

<!-- Required for experimental PRs. Replace the commented lines below
with your targets.
Do not delete the fence or change its `experimental-tests` tag — the
experimental-track
watcher reads it verbatim to decide which targets to ask CI for. -->

```experimental-tests
# One target per line: a directory or a file. (A pytest ::selector is not
# supported -- the sharding runner cannot consume one.) Must be under
# tests/experimental/ and must exist. Delete these comment lines and add yours, e.g.
#
#   tests/experimental/test_my_backend.py
#   tests/experimental/my_backend/
#
# Declaring the whole tree (tests/experimental/) is allowed but means every
# experimental PR pays for every other feature's tests, in every matrix cell.
```

## Reviewer Notes

Reviewers with SM120 / SM121 hardware:
`tests/moe/test_trtllm_cutlass_fused_moe.py` covers the launch check;
the GitLab lane runs it on RTX PRO 6000 and Spark.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants