Skip to content

[Qwen3.8] Cherry-pick Triton skinny GEMV for the bf16 lm_head to qwen4_v0.5.21 - #101

Merged
sammysun0711 merged 4 commits into
apinge:qwen4_v0.5.21from
yicyang:qwen4_v0.5.21-qwen38-lm-head-gemv
Nov 11, 2026
Merged

sammysun0711 merged 4 commits into
apinge:qwen4_v0.5.21from
yicyang:qwen4_v0.5.21-qwen38-lm-head-gemv

Conversation

@yicyang

@yicyang yicyang commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

Cherry-pick of #46 (c996fcb1ef) from qwen38-flash-next-amd-current-fix-install.

  • Qwen3.8: in the profiler, the decode lm_head ([124160, 2560] at TP2) goes from 251 us (hipBLASLt) to 181 us (_gemv_m1_kernel). The decode step drops from 10.67 to 10.57 ms (-1%).
  • Qwen3.5: the lm_head has K=2048, which is not in the tuned table, so it stays on hipBLASLt. Its differences are run-to-run noise.

…project#46)

* [AMD] Triton skinny GEMV for the bf16 lm_head on gfx942

The lm_head GEMM is a pure weight-streaming workload at decode
(M = batch size, N = vocab shard). On MI308X hipBLASLt picks a generic
Tensile tile that reaches ~2.2 TB/s; for the Qwen3.8-Flash-Next TP2
shard [124160, 2560] this costs ~250 us per step.

Add a Triton skinny GEMM (python/sglang/kernels/ops/gemm/rocm_bf16_gemv.py)
with three fixed, capture-safe configs:
  - M == 1: row-reduction GEMV on the vector ALUs
  - 2 <= M <= 16: 16-row MFMA tile, kpack=2, waves_per_eu=2
  - 17 <= M <= 32: 32-row MFMA tile with a narrower N tile
M > 32 and non-bf16 / unaligned-K inputs keep using torch.matmul.

LogitsProcessor routes the plain bf16 lm_head matmul through it on gfx942;
SGLANG_ENABLE_ROCM_LM_HEAD_GEMV=0 opts out.

MI308X, K=2560, us (triton / hipBLASLt):
  N=124160: M=1 199/269, M=8 230/286, M=32 278/320
  N=31040:  M=1 37/52,   M=8 49/53,   M=32 60/65
End-to-end TP2 (pyhip config, 12k in / 5 out, conc 1):
  lm_head kernel 250.7 -> 179.3 us, TPOT 8.49 -> 8.38 ms,
  GSM8K (chat, 200, greedy) 0.975 -> 0.980.

* [AMD] Tune the gfx942 lm_head GEMV and route only tuned shapes

Replace the hard-coded launch configs with a per-(N, K) table in
rocm_bf16_gemv.py (_GFX942_CONFIGS, M upper-bound buckets) and add
cache_modifier on the weight load as a tunable.

Only shapes in the table use the Triton kernels; everything else keeps
hipBLASLt. The previous generic configs were slower than hipBLASLt on
untuned shapes for some M (e.g. N <= 24576 at K=2560, or K=5120), and no
single N threshold separated wins from losses across K.

Tuned on MI308X for the Qwen3.8-Flash-Next lm_head shards
(N in {248320, 124160, 62080, 31040}, K=2560) with
benchmark/kernels/rocm_bf16_gemv/tune_rocm_bf16_gemv.py. Tuned vs previous
configs: 1.02-1.14x; vs hipBLASLt: 1.11-1.49x. ".cg" and narrower
1-warp MFMA tiles (BLOCK_N=64) account for most of the gain. Split-K was
evaluated and dropped: no shape improved beyond noise.

TP2 end to end: lm_head 249 -> 165 us (previous configs: 173.5 us).

* [AMD] Time the lm_head GEMV tuner with triton.testing.do_bench and re-tune

Replace the hand-rolled CUDA-event loop in tune_rocm_bf16_gemv.py with
triton.testing.do_bench(warmup=25, rep=100), the setting aiter's Triton
op_benchmarks use (e.g. bench_gemm_a8w8_per_token_scale.py). Candidates,
hipBLASLt and the shipped config are all timed the same way, with L2
flushed between reps.

Re-tuned all four Qwen3.8-Flash-Next lm_head shards on MI308X. The GPUs
were shared with other jobs, so every bucket where the tuner proposed a
different config was re-checked with an interleaved A/B (20 rounds of
do_bench, min and median); identical configs varied by up to ~4.6%, so a
config is replaced only when it wins by >5% on both:

- N=31040, M<=2..32: BLOCK_K 128 -> 256 (BLOCK_K 128 kept for M<=24) and
  ".cg"; 1.11-1.49x vs the previous entries. The previous M<=32 entry was
  no faster than hipBLASLt.
- N=124160, M<=2: 1-warp BLOCK_N=64 tile, 1.09x.
- N=62080, M<=4: ".cg" on the weight load, 1.12x.

Every shipped entry beats hipBLASLt by 1.21-1.83x (do_bench min).

---------

Co-authored-by: yicyang <[email protected]>
(cherry picked from commit c996fcb)
@yicyang
yicyang force-pushed the qwen4_v0.5.21-qwen38-lm-head-gemv branch from 31ee472 to ba406b4 Compare November 10, 2026 02:45
@sammysun0711

Copy link
Copy Markdown
Collaborator

This PR backports the Triton BF16 lm_head GEMV optimization from #46 to qwen4_v0.5.21 for Qwen3.8 case, Qwen3.5 case should not hit by this skinny gemm optimization.

  • Adds a gfx942 fast path, tuned on MI308X, for Qwen3.8-Flash-Next's BF16 vocabulary-projection shapes at TP1/TP2/TP4/TP8.
  • A single token uses _gemv_m1_kernel; small batches of up to 32 token rows use the skinny MFMA kernel. Both accumulate in FP32.
  • The logits processor selects the fast path only for supported shapes, dtypes, and layouts; other inputs retain the existing matrix-multiplication path. SGLANG_ENABLE_ROCM_LM_HEAD_GEMV=0 disables the optimization.
  • Add a tuning script, a standalone kernel benchmark, and regression tests for the new kernels.

MI308X CI: PR #94 baseline → PR #101

  • Before: PR #94, SGLang 598ebb63de3ddc8c51cab59bd43ea3ab56deae12, run 42624481704, attempt 1, November 9, 2026 UTC.
  • After: PR #101, SGLang 54f8bd857cf6c4c5455653deba351f353260c131, run 42772425542, attempt 4, November 10, 2026 UTC.
  • Shared sources: AITER 514c166e9abad59acde74309923c2d923a89eee0, pyhip 8027faad29415747707670db7776528c6b72db81, and benchmark client fc6ddbbf7aac80167ea04cc00bf200af8adf2b85.
  • Performance inputs: random dataset, configured input/output lengths of 12,000/350 tokens for Qwen3.8 and 8,000/500 for Qwen3.5.

Changes are (after / before - 1) × 100%, calculated from unrounded artifact values and rounded to one decimal place. Negative TTFT/TPOT changes mean lower latency; positive throughput/QPS changes mean higher throughput.

Case GSM8K (before → after) TTFT ms (before → after; Δ %) TPOT ms (before → after; Δ %)
Qwen3.8-Flash-TP2-Base 98.00% → 98.50% 730.911 → 723.772 (-1.0%) 7.270 → 7.239 (-0.4%)
Qwen3.8-Flash-TP2-MTP 98.50% → 99.00% 779.860 → 782.597 (+0.4%) 6.264 → 6.270 (+0.1%)
Qwen3.5-35B-TP4-Base 96.50% → 98.00% 166.622 → 169.688 (+1.8%) 4.071 → 4.153 (+2.0%)
Qwen3.5-35B-TP1-DFlash 97.00% → 98.00% 304.783 → 310.695 (+1.9%) 2.866 → 2.762 (-3.6%)
Qwen3.5-397B-TP8-Base 98.00% → 99.00% 339.071 → 344.805 (+1.7%) 6.165 → 6.235 (+1.1%)
Qwen3.5-397B-TP8-DFlash 99.50% → 99.00% 361.708 → 363.559 (+0.5%) 3.059 → 2.981 (-2.5%)

Additional CI summary metrics:

Case Output tokens/s (before → after; Δ %) QPS (before → after; Δ %) Accept length (before → after; Δ %)
Qwen3.8-Flash-TP2-Base 107.065 → 107.659 (+0.6%) 0.306 → 0.308 (+0.6%) —
Qwen3.8-Flash-TP2-MTP 117.972 → 117.792 (-0.2%) 0.337 → 0.337 (-0.2%) 3.521 → 3.514 (-0.2%)
Qwen3.5-35B-TP4-Base 227.406 → 222.948 (-2.0%) 0.455 → 0.446 (-2.0%) —
Qwen3.5-35B-TP1-DFlash 288.091 → 295.928 (+2.7%) 0.576 → 0.592 (+2.7%) 5.911 → 6.221 (+5.2%)
Qwen3.5-397B-TP8-Base 146.371 → 144.638 (-1.2%) 0.293 → 0.289 (-1.2%) —
Qwen3.5-397B-TP8-DFlash 264.697 → 269.988 (+2.0%) 0.529 → 0.540 (+2.0%) 7.769 → 7.976 (+2.7%)

For the targeted Qwen3.8 cases, Base records TTFT -1.0%, TPOT -0.4%, and output throughput +0.6%; MTP records TTFT +0.4%, TPOT +0.1%, and output throughput -0.2%. GSM8K increases by 0.50 percentage points in both cases, to 98.50% and 99.00%, respectively. These serving measurements do not measure lm_head kernel latency independently.

Qwen3.5 model test case output throughput changes range from -2.0% to +2.7% within run-to-run variance.

@sammysun0711
sammysun0711 merged commit 76a5f7f into apinge:qwen4_v0.5.21 Nov 11, 2026
7 of 8 checks passed
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.

2 participants