Skip to content

Add SM89 Qwen3-VL FP8 path (8B + 2B) - #116

Merged
LiangSu8899 merged 9 commits into
flashrt-project:mainfrom
heiheiha798:feat/qwen3-vl-sm89-fp8
Jun 26, 2026
Merged

LiangSu8899 merged 9 commits into
flashrt-project:mainfrom
heiheiha798:feat/qwen3-vl-sm89-fp8

Conversation

@heiheiha798

Copy link
Copy Markdown
Contributor

Summary

Brings up Qwen3-VL on Ada RTX 4090 (sm_89) with the checkpoint's native
block-scaled FP8 language weights, for both the 8B and 2B variants.
This is the SM89 counterpart to the SM120/NVFP4 Qwen3-VL path: the vision tower,
geometry, image scatter and MRoPE are reused from that work; this branch adds
the Ada FP8 language stack, a hand-written sm_89 block-128 GEMM/GEMV, and the
offline quantizer + config-driven generalization that bring up the 2B.

This is a clean re-cut of the earlier prototype branch onto current main,
reorganized into reviewable commits with the file scope aligned to the SM120
Qwen3-VL pattern (dedicated flash_rt_qwen3_vl_kernels module; no central
bindings.cpp churn; iteration/benchmark harnesses kept out of the PR).

Why hand-written (no TMA on Ada)

The SM120 Qwen3-VL GEMM runs through the CUTLASS
KernelTmaWarpSpecializedBlockwise collective — TMA bulk-copy + a
warp-specialized producer/consumer pipeline with an auto-selected deep
(3-4 stage) software pipeline that hides load latency without spending
registers or occupancy on staging.

Ada (sm_89) has no TMA and no warp-specialized CUTLASS collective (those
builders are Sm90+/Sm120 only), so that design does not port:

  • GEMM is a hand-written cp.async + ldmatrix.x4 + block-128-scaling MMA
    kernel (mma.sync...m16n8k32...e4m3.e4m3.f32, no .kind::f8f6f4). With
    cp.async instead of TMA, each extra pipeline stage costs a full smem buffer;
    at the tuned 64x64 tile the kernel is already register-limited to 4 CTA/SM, so
    a 2-stage pipeline halves occupancy and regresses. It runs single-stage and is
    latency-bound. The obvious memory-pattern wins (load/store coalescing,
    ldmatrix) are applied; CUTLASS's own Ada blockwise example is slower than
    this kernel at these shapes, so it is not used.
  • Attention uses vendored FlashAttention-2 (Tri Dao). The only addition is
    an hdim64 instantiation for the 2B ViT (avoids a 64->96 pad); the FA2 kernel
    body is upstream and unmodified.

So the SM89 path matches the SM120 design intent (block-128 FP8, fused
act/norm-quant, ldmatrix MMA) but cannot reuse the TMA/warp-specialized
machinery — the main reason its prefill is slower than the Blackwell NVFP4 path.

Commits

  1. csrc: add SM89 native FP8 block-128 GEMM kernel
  2. csrc: add SM89 block-128 M=1 GEMV for Qwen3-VL decode
  3. csrc: add SM89 Qwen3-VL helper kernels (qk-norm-rope, fp8 block act/norm)
  4. build: bind SM89 Qwen3-VL kernels into flash_rt_qwen3_vl_kernels
  5. fa2: add hdim64 instantiation for Qwen3-VL 2B vision attention
  6. frontend: SM89 Qwen3-VL official-FP8 path (8B + 2B)
  7. tools: SM89 FP8 block-128 quantizer + smoke/CPU tests
  8. docs: SM89 Qwen3-VL FP8 (8B + 2B); note no-TMA optimization limits

Module layout

SM89-specific kernels (block-128 GEMM/GEMV, fused act/norm-quant, QK
norm-rope-kvwrite, the ViT bf16 cuBLASLt matmul) build into the dedicated
flash_rt_qwen3_vl_kernels module (FLASHRT_BUILD_QWEN3_VL=ON, now GPU_ARCH
89 or 120), keeping flash_rt_kernels.so small. The frontend resolves names
from the dedicated module first, then the generic one — the same dual-module
split the SM120 ViT path already uses.

Build

cmake -S . -B build -DGPU_ARCH=89 -DFLASHRT_BUILD_QWEN3_VL=ON
cmake --build build --target flash_rt_kernels flash_rt_fa2 flash_rt_qwen3_vl_kernels -j

Verification

Built clean for sm_89 (both modules). End-to-end 2B multimodal smoke
(scripts/smoke_qwen3_vl_fp8_sm89.py --multimodal --fp8-lm-head, FlashRT.png):
cos_vs_eager=1.000000, top token matches eager, prefill ~80 ms, decode
cos_vs_eager=1.000000, coherent generation. CPU/CI tests cover the config
guard, the block-128 quant round-trip, and the expected quant-linear count
without the multi-GB checkpoint or CUDA.

Hand-written Ada (sm_89) FP8 e4m3 -> BF16 block-128 scaled GEMM for official
Qwen3-VL FP8 prefill. Reads FP8 weight directly (no dequant scratch), keeps
per-token act scale [M,K/128] and DeepSeek-style weight block scale
[N/128,K/128]. cp.async pipeline + m16n8k32 e4m3 MMA + ldmatrix.x4 over a
128B-swizzled smem layout + coalesced scale staging + bfloat162 paired stores.

The device kernel body lives in fp8_bs_gemm_device.cuh so the production
launcher and the micro-bench share one source (zero drift). Gated by
ENABLE_SM89_BLOCK_FP8_GEMM for GPU_ARCH=89.
Decode-shape (M=1) sibling of the SM89 block-128 GEMM: per-token activation
block scale [K/128] x per-weight block scale [N/128,K/128], warp-per-row with
A staged in smem and 16-byte coalesced B loads. Lives in its own file/namespace
(gemv_m1_sm89) rather than the SM120 per-tensor GEMV, so the two architectures'
GEMVs do not share a source. Wired into the build with the dedicated Qwen3-VL
module in a later commit.
…orm)

Union-merge of the existing main kernels with the SM89-needed additions, so no
existing variant is lost:
  - qwen3_qkv_post_proc: add qwen3_qk_norm_rope_kvwrite_bf16 and _batched_bf16
    (fused Q+K norm+rope+kv-write used by the SM89 prefill path), keeping main's
    _prefill_bf16 / _devpos_bf16 variants.
  - fp8_per_token_block_quant: add rms_norm / residual_add_rms_norm / silu_mul /
    silu_mul_merged to-fp8-block128 fused activation-quant kernels.
Both TUs compile clean for sm_89.
Extend the dedicated Qwen3-VL pybind module (previously SM120-only) to also
build for GPU_ARCH=89, compiling the SM89 block-128 GEMM/GEMV + fused
act/norm-quant + QK norm-rope sources and binding their 8 new symbols
(fp8_block128_gemm_blockscaled_sm89_bf16out, ht_gemv_fp8_block128_m1_*,
rms_norm/residual_add_rms_norm/silu_mul[_merged]_to_fp8_block128_bf16,
qwen3_qk_norm_rope_kvwrite[_batched]_bf16). The 7 generic symbols the SM89
frontend also needs stay in central flash_rt_kernels. Keeping the SM89 GEMM out
of flash_rt_kernels keeps that .so small (mirrors the SM120 split). The SM89
frontend imports both modules, like the SM120 ViT path.
Qwen3-VL 2B ViT runs full (non-causal) attention at head_dim=64; the previous
FA2 build only shipped hdim 96/128/256, forcing a 64->96 pad (33% waste). Add
the auto-generated hdim64 sm80 fp16/bf16 instantiations, the launch-template
hdim64 tile selection (no-split, 128x64 for the non-causal Ada path), and the
fa2_wrapper <=64 dispatch bucket. CMake FA2_HDIMS gains 64 (default) and skips
the unused split-KV hdim64 object to keep ptxas time down. Vendored FA2 kernel
body unchanged (Tri Dao); this is instantiation only.
Add the SM89 text + multimodal frontends and the FP8 block-128 weight loader:
  - qwen3_vl_fp8_sm89.py: batch-1 text decode + FP8 block-scaled prefill GEMM.
  - qwen3_vl_fp8_sm89_multimodal.py: ViT tower + image scatter + FP8 language.
  - _qwen3_vl_fp8_weights.py: official .weight/.weight_scale_inv loader.
Kernel symbols resolve via _MergedKernels: the SM89-specific kernels come from
flash_rt_qwen3_vl_kernels, generic ones from flash_rt_kernels (same dual-module
split the SM120 ViT path already uses). Shared SM120 infra is union-merged with
main: _qwen3_vl_geometry (dim contract for 2B/8B), _qwen3_vl_vision_rtx (SM89
FP8 linear dispatch via the dedicated module), qwen3_vl_rtx + attn_backend_qwen3
(generalized head/layer config for both archs).
  - quantize_qwen3_vl_to_fp8_block128.py: offline BF16 -> FP8 block-128
    quantizer for the language linears (single-file or sharded output).
  - smoke_qwen3_vl_fp8_sm89.py: end-to-end text/multimodal smoke + logit-cosine
    vs eager + tps harness.
  - test_qwen3_vl_fp8_sm89{,_2b}.py: CPU/CI-friendly checks (config guard,
    block-128 quant round-trip, expected quant-linear count) without the
    multi-GB checkpoint or CUDA.
Bench/NCU iteration harnesses are intentionally not shipped.
Document the SM89 official-FP8 path for the 8B and 2B variants (build flags,
quantizer usage, smoke command, measured prefill/decode). Add a 'Why this path
is hand-written' section explaining that Ada has no TMA and no warp-specialized
CUTLASS collective, so the SM120 TMA warp-specialized blockwise GEMM and its
deep multi-stage cp.async pipeline do not port -- hence the single-stage
hand-written cp.async + ldmatrix kernel and the use of vendored FA2 for
attention.
@heiheiha798
heiheiha798 requested a review from LiangSu8899 as a code owner June 26, 2026 19:55
Copilot AI review requested due to automatic review settings June 26, 2026 19:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@LiangSu8899

Copy link
Copy Markdown
Member

Very cool!

@LiangSu8899
LiangSu8899 merged commit ce268ab into flashrt-project:main Jun 26, 2026
@heiheiha798
heiheiha798 deleted the feat/qwen3-vl-sm89-fp8 branch June 27, 2026 00:22
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