Skip to content

Qwen3 vl prefill - #109

Merged
LiangSu8899 merged 6 commits into
mainfrom
qwen3-vl-prefill
Jun 23, 2026
Merged

LiangSu8899 merged 6 commits into
mainfrom
qwen3-vl-prefill

Conversation

@LiangSu8899

Copy link
Copy Markdown
Member

Summary

Makes the Qwen3-VL single-image prefill run as a pure-kernel CUDA-graph
replay (zero per-request torch dispatch, like decode), moves image
preprocessing onto the GPU, and folds the ViT bias adds into adjacent
kernels. Full-request TTFT (5090, full-res 1581-token prompt) ~100 ms;
prefill 93 ms; decode ~150 tok/s.

Additive: the shared flash_rt_kernels.so is untouched; new kernels live
in the opt-in flash_rt_qwen3_vl_kernels module.

What's in it

  • Whole-prefill CUDA graph (embed → ViT tower → scatter → NVFP4 layers +
    DeepStack → final norm); lm_head eager on the last row. Multi-image /
    video keep the eager prefill.
  • GPU image preprocessing via apply_chat_template(device=...) (set_prompt
    24 → 8 ms).
  • Merger FP8 (−2.6 ms).
  • Bias fusion — the per-column bias rides the op that consumes the GEMM
    output (3 new general kernels: gelu_tanh_bias_to_fp8_block128_bf16,
    residual_add_bias_bf16, qkv_split_bias_bf16); no bias round-trips HBM.

Correctness (vs HF bf16)

Next-token argmax matches HF: image 785, two-image 8420, video 785; prefill
graph eager-vs-replay logits cos 0.99999; new-kernel standalone cos ≥ 0.9997.

Build

cmake -B build -S . -DGPU_ARCH=120 -DFLASHRT_BUILD_QWEN3_VL=ON, build
flash_rt_qwen3_vl_kernels. Smoke: pytest tests/test_qwen3_vl_smoke.py.

Capture the whole single-image prefill as one CUDA graph: embed gather ->
ViT tower -> image-feature scatter -> NVFP4 decoder layers with DeepStack
inject -> final RMSNorm. All torch glue (gather/scatter/clone/copy) traces
into the graph, so the replay path is pure-kernel with zero torch dispatch
(same model as the decode graph and qwen3_rtx prefill graph). lm_head runs
eager post-replay on the real last row. Multi-image / video keep the eager
prefill (variable segment structure is not capturable). Captured under
no_grad (not inference_mode) to compose with the ViT forward_graph.

Also FP8 the four mergers (128-aligned): image_embeds cosine 0.9703 ->
0.9698 (argmax-safe), ~-2.6 ms. generate() now routes through prefill_graph.

TTFT (single image, full res) 98.8 -> 94.9 ms; next-token argmax still 785
(matches HF and the eager path; logits cos eager-vs-graph 0.99999).
Pass device=self.device to apply_chat_template so the fast image
processor's resize / normalize / patchify runs on the GPU instead of the
CPU (~10x: the image_processor alone drops 24 -> 2.7 ms). set_prompt 24-41
-> ~8 ms; full TTFT (set_prompt + graph prefill, single image full res)
~103 ms. Next-token argmax unchanged across image (785) / multi-image
(8420) / video (785); the GPU resize differs from the CPU path only at
~2e-3 cosine, well inside the FP8 vision tolerance.
Move the per-request static-buffer staging out of prefill_graph and into
set_prompt (one-time set-up glue): set_prompt now copies the prompt tensors
into persistent (P,S,a,b)-keyed buffers, and the captured graph reads only
from those. prefill_graph's hot path is now graph.replay() + the eager
lm_head kernel + sync -- no torch alloc/copy/compute. The logits and
last-hidden buffers are pre-allocated in __init__, and reset_state is
dropped (the captured layers write K/V[0:S], all the causal prefill reads;
decode overwrites K/V[>=S] before reading -- matches qwen3_rtx).

argmax 785 (== eager, cos 0.99998); decode after graph prefill 142.7 tok/s;
multi-image (8420) / video keep the eager fallback.
…> 92.7)

The shared block-scaled FP8/FP4 GEMM has a fixed epilogue, so the per-
column bias was a standalone add_bias kernel re-reading/writing the whole
GEMM output (~117 launches, ~3.5 ms even in the graph). Fold each bias
into the op that consumes the GEMM output instead — three new general
kernels in the isolated module:

  gelu_tanh_bias_to_fp8_block128_bf16  (fc1 bias -> GELU->FP8)
  residual_add_bias_bf16               (proj / fc2 bias -> residual)
  qkv_split_bias_bf16                  (qkv bias -> split)

plus the bf16 blocks use the existing bias_gelu_inplace, and the mergers
fold fc1 bias the same way. No bias now round-trips HBM as its own kernel.

Standalone cos vs ref: gelu+bias 0.9997, residual+bias / qkv-split+bias
1.0. image_embeds cos 0.9679 (unchanged), argmax 785 (image) / 8420
(two-image) / 785 (video) all match HF; prefill graph eager-vs-replay cos
0.99999.
Prefill is now a pure-kernel CUDA-graph replay; image preprocessing runs
on the GPU (set_prompt 24 -> 8 ms); ViT bias adds fold into adjacent
kernels. Headline TTFT ~100 ms full request, decode ~150 tok/s; refresh the
max_pixels resolution table (100 / 59 / 32 / 25 ms).
The graph prefill now calls gelu_tanh_bias_to_fp8_block128_bf16,
residual_add_bias_bf16 and qkv_split_bias_bf16, but the construction-time
kernel-module check did not list them, so a stale flash_rt_qwen3_vl_kernels
.so would pass __init__ and only AttributeError mid-ViT. Add the three
symbols to _QWEN3_VL_KERNEL_FNS, and a smoke test asserting that dropping
any single required symbol raises at the check (naming it).
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.

1 participant