Repository navigation
Qwen3 vl prefill - #109
Merged
Merged
Qwen3 vl prefill#109
Conversation
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).
This was referenced Jun 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.sois untouched; new kernels livein the opt-in
flash_rt_qwen3_vl_kernelsmodule.What's in it
DeepStack → final norm); lm_head eager on the last row. Multi-image /
video keep the eager prefill.
apply_chat_template(device=...)(set_prompt24 → 8 ms).
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, buildflash_rt_qwen3_vl_kernels. Smoke:pytest tests/test_qwen3_vl_smoke.py.