Skip to content

[kernel] add fused silu mul quant fp8 - #37376

Merged
BBuf merged 26 commits into
sgl-project:mainfrom
xieminghe1:main
Sep 7, 2026
Merged

BBuf merged 26 commits into
sgl-project:mainfrom
xieminghe1:main

Conversation

@xieminghe1

@xieminghe1 xieminghe1 commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Reduce MoE launch and memory-traffic overhead by fusing silu_and_mul and per_token_group_quant_fp8. The fused Triton kernel computes the gated activation, derives per-token-group FP8 scales, and writes the FP8 down-projection input in one pass.

The no-flag auto-dispatch update is in xieminghe1/sglang#1. The original PR head has Allow edits from maintainers disabled, so it must be merged by the fork owner before this PR updates.

Changes

  • Add fused_silu_mul_quant_fp8 and its H200 benchmark.
  • Feed the fused FP8 tensor and generated activation scales directly into the down MoE GEMM.
  • Preserve the DeepSeek-V4 swiglu_limit clamp inside the fused kernel.
  • Automatically dispatch to the fusion from the Triton MoE path. No environment variable or new server argument is introduced.
  • Add standalone kernel coverage, dispatch-condition coverage, and an end-to-end fused-vs-fallback MoE comparison.

Automatic dispatch

The fast path is selected only when all of the following are true:

  • NVIDIA CUDA; BF16 or FP16 activation.
  • Blockwise FP8 W8A8 MoE with a valid positive K-group size that evenly divides the post-SwiGLU width.
  • Standard gated SiLU, with no gemm1_alpha or gemm1_limit modifier.
  • No expert filtering, LoRA hooks, or interleaved SwiGLU GEMM epilogue.

swiglu_limit is supported for DeepSeek-V4. Every incompatible case keeps the existing activation + quantization fallback.

flowchart LR
    A[Gate-up MoE GEMM] --> B{CUDA blockwise FP8\nand all contracts match?}
    B -->|yes| C[Fused SiLU × up + group FP8 quant]
    B -->|no| D[Existing activation path]
    D --> E[Existing per-token-group quant]
    C --> F[Down MoE GEMM with fused scales]
    E --> F
Loading

Validation

H200 (CUDA_VISIBLE_DEVICES=4):

PYTHONPATH=python python test/registered/unit/test_legacy_global_ratchet.py
CUDA_VISIBLE_DEVICES=4 PYTHONPATH=python \
  pytest -q test/registered/kernels/ops/quantization/test_fused_silu_mul_quant_fp8.py
pre-commit run --files \
  python/sglang/srt/layers/moe/moe_runner/triton_utils/fused_moe.py \
  python/sglang/srt/server_args.py \
  test/registered/kernels/ops/quantization/test_fused_silu_mul_quant_fp8.py

Results:

  • CPU legacy-global ratchet: passed (1 test).
  • H200 kernel and dispatch suite: passed (32 tests).
  • End-to-end synthetic blockwise-FP8 MoE: the automatic path invoked the fused kernel and matched the forced fallback within FP8 tolerance.
  • Pre-commit on changed files: passed.

Accuracy results

kernel accuracy kernel test results

DeepSeek-V4:

DeepSeek-V4 accuracy

Performance results

H200 activation-boundary benchmark:

H200 fused kernel benchmark

End-to-end baseline:

end-to-end baseline

End-to-end fused path (captured on the earlier opt-in revision; after the follow-up merges, the same path is selected automatically with no extra flag):

end-to-end fused path

Comparison:

end-to-end comparison

Checklist

  • Format code with pre-commit.
  • Add registered unit/kernel tests.
  • Provide accuracy and speed evidence.
  • Preserve unsupported-path fallbacks and DeepSeek-V4 clamp semantics.

CI States

Latest PR Test (Base): 🚫 Run #34007782658
Latest PR Test (Extra): 🚫 Run #34007782493
Latest PR Test (AMD ROCm 7.2): ❌ Run #34007782660

@BBuf BBuf 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.

Change summary

This PR adds an opt-in Triton fusion for the MoE activation boundary: it computes SiLU(gate) * up, derives per-token-group FP8 scales, writes FP8 activations directly, and passes those scales into the down-projection MoE GEMM, avoiding the separate activation buffer and quantization launch.

flowchart LR
    A["Gate-up MoE GEMM"] --> B{"Fusion predicate"}:::changed
    B -->|fused| C["SiLU × up + group absmax + FP8"]:::changed
    C --> D["Down MoE GEMM with fused scales"]:::changed
    B -->|fallback| E["Existing activation / modifier path"]
    E --> F["Existing group FP8 quantization"]
    F --> D
    classDef changed stroke-dasharray: 5 5,stroke-width: 2px;
Loading

The new server flag enables the fast path only for CUDA blockwise-FP8 gated SiLU MoE. The fused kernel produces row-major activation scales consumed by the existing down GEMM; all unsupported cases should remain on the established activation/modifier path. Dashed nodes are changed by this PR.

Findings

  • [P1] The fusion predicate bypasses two existing correctness contracts (python/sglang/srt/layers/moe/moe_runner/triton_utils/fused_moe.py:588-597):

    • It does not require gemm1_limit is None, so a limit-only model that normally executes _swiglu_silu_clamp_mul() instead gets plain SiLU×up when the flag is enabled.
    • It can run with an after_down LoRA hook. The base down GEMM receives the new FP8 scales, but the hook receives intermediate_cache2 as raw FP8 without those scales and uses it as the LoRA down input, changing or breaking adapter output.

    Please preserve these paths in the predicate (or implement their exact semantics in the fusion) and add integration tests that exercise gemm1_limit and active MoE LoRA hooks through _fused_moe_kernel_sequence().

  • [P2] swiglu_limit is an advertised no-op in the new public wrapper (python/sglang/kernels/ops/moe/fused_moe_triton_kernels.py:1228). The docstring says 10.0 applies the DeepSeek-V4 clamp, but the argument is never passed into the Triton kernel. Please implement the clamp and test it, or remove the argument/documentation so callers cannot silently get unclamped results.

Review evidence

  • Reviewed all five changed files and traced the generated FP8 tensor/scale pair into the down MoE GEMM and LoRA hook call sites.
  • Exhaustively swept 32,639 historical human-review threads for every touched path plus SiLU/SwiGLU, FP8, quantization, clamp/modifier, correctness, kernel, performance, and test terms. Prior DeepSeek-V4 reviews specifically require preserving SwiGLU clamp and real end-to-end accuracy, not only isolated kernel tolerance.
  • Compared the design with the current upstream vLLM fused SiLU/group-FP8 kernels, which explicitly carry clamp/alpha/beta controls and preserve the intended intermediate precision before quantization.
  • git diff --check and Python byte-compilation pass. No CUDA device is available locally, and CI only ran lint/change detection; GPU lanes are gated because the PR lacks run-ci.

Residual validation risk after the fixes: the test covers the standalone plain-SiLU kernel with tolerance, but the opt-in serving path still needs at least one real FP8 MoE model accuracy/e2e comparison and a benchmark using SGLang's standard kernel timing utilities.

and activation == "silu"
and is_gated
and gemm1_alpha is None
and swiglu_limit is None

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.

[P1] Two existing semantics need guards here. First, gemm1_limit is not excluded, so a limit-only model that normally runs _swiglu_silu_clamp_mul() silently takes plain SiLU×up under this flag. Second, this can run with an after_down LoRA hook: the base down GEMM gets _a2_scale_fused, but the hook receives raw FP8 intermediate_cache2 without its scales and uses it as the LoRA down input. Please preserve both paths in the predicate (or implement their exact semantics) and add _fused_moe_kernel_sequence() integration tests for gemm1_limit and active MoE LoRA.

tl.store(scale_ptrs, scale, mask=scale_mask)


def fused_silu_mul_quant_fp8(x, group_size, swiglu_limit=0.0):

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.

[P2] swiglu_limit is advertised as supporting the DeepSeek-V4 clamp, but the value is never passed to or read by _fused_silu_mul_quant_fp8_kernel; 10.0 produces exactly the same unclamped result as 0.0. Please implement and test the clamp, or remove this argument/documentation so future callers cannot silently get the wrong semantics.

@BBuf BBuf added run-ci CI: run the baseline test suite on this PR bypass-fastfail run-ci-extra CI: also run the extra suite (requires run-ci) labels Sep 3, 2026
@xieminghe1
xieminghe1 force-pushed the main branch 2 times, most recently from 47217ba to 0c28495 Compare September 3, 2026 10:10
@BBuf

BBuf commented Sep 5, 2026 •

Copy link
Copy Markdown
Collaborator

I prepared the CPU CI + auto-dispatch fix in xieminghe1#1. The original PR has Allow edits from maintainers disabled, so GitHub rejected a direct push to the head branch.

The follow-up removes the new ServerArgs flag and get_global_server_args() call, automatically dispatches only for compatible CUDA blockwise-FP8 MoE inputs (with fallback for unsupported shapes/modifiers/filtering/LoRA), preserves DeepSeek-V4 swiglu_limit, and adds an end-to-end fused-vs-fallback MoE test.

Validation: CPU legacy ratchet passed; H200 kernel/dispatch suite passed (32 passed); pre-commit passed. Please merge the follow-up PR so this PR updates.

@BBuf

BBuf commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/kernels/ops/quantization/test_fused_silu_mul_quant_fp8.py

@github-actions

github-actions Bot commented Sep 6, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/kernels/ops/quantization/test_fused_silu_mul_quant_fp8.py:

🚀 1-gpu-h100 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/quantization/test_fused_silu_mul_quant_fp8.py

@BBuf
BBuf merged commit a8b2f36 into sgl-project:main Sep 7, 2026
151 of 206 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fastfail jit-kernel quant LLM Quantization run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) sgl-kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants