Repository navigation
hrx: chunk a batched prefill matmul above the tile's 2048-token range - #244
Conversation
A vision graph contracts a whole image in one node: Qwen3-VL's CLIP attention matmuls are 8464 image tokens over 16 planes, with every operand layout the f32 tile already accepts. The only thing refusing them was `common_is_supported_prefill_token_count()` — `token_count <= 2048`, a *tile* range rather than a shape or layout limit. Those nodes therefore ran on the CPU while the rest of the graph was on the device (engine ggml-org#1467: the projector falls back to the CPU at ~17 s per image, and with the projector "offloaded" the shipped payload dies on the first image request). The fork already splits a dispatch that exceeds its kernel range: `append_token_chunked_dispatches` does exactly that for ROPE and for flash attention's query rows. Use it here: - `common_match_mul_mat_any_format` gains `allow_token_chunks`, which accepts up to `kCommonMaxChunkedTokens` (65536) instead of the tile's 2048. Only callers that chunk may pass it, so no accepted shape changes elsewhere. - the batched prefill matmul matcher passes it and splits its token dimension into equal chunks, rebinding the activation and output rows per chunk and carrying the token capacity of each chunk's own row count. One chunked set per plane, so batching is unchanged. - two `test-backend-ops` rows at the vision shapes: `m=256,n=4096,k=256,bs=[1,2]` and `m=128,n=8464,k=64,bs=[1,16]`. Measured on HRX0 against the CPU reference: both new rows went from `not supported [HRX0]` to **OK**, and the whole `-o MUL_MAT` suite is **566/566** with no failures. This is coverage, not the whole issue: the CLIP graph's real blockers are still `IM2COL` and `UPSCALE` (no HRX kernel exists), flash attention at head_dim 64, and matmuls whose activation arrives as a non-contiguous `PERMUTE` (declined for contiguity before the token range is even consulted). Those are tracked on ggml-org#1467. Refs ggml-org#1467
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at fedd5b0ee940d9f5e3bb86f23c7965a8a71e18ed
The diagnosis is right and it is the general form of a bug that keeps reappearing in this backend: the 2048-token cap is a tile bound, not a shape or layout limit. A vision graph contracts a whole image at once (CLIP attention: 8464 tokens × 16 planes), so every operand layout was fine and the node still fell to the CPU. Chunking the token dimension is what ROPE and FLASH_ATTN already do above their kernel ranges, and this reuses the same helper (append_token_chunked_dispatches, dispatch-token-chunks.h) instead of growing a second chunker — which is the right call: one place that knows how a row range is split.
Built and ran the added cases:
$ cmake --build build226 --target test-backend-ops -> [25/25] Linking
$ ./build226/bin/test-backend-ops -b CPU -o MUL_MAT
MUL_MAT(type_a=f32,type_b=f32,m=256,n=4096,k=256,bs=[1,2], …): OK (4096 > 2048, the plain over-bound case)
MUL_MAT(type_a=f32,type_b=f32,m=128,n=8464,k=64, bs=[1,16], …): OK (the CLIP attention shape: 8464 × 16 planes)
1/1 backends passed
Both are ordinary test_mul_mat cases rather than an HRX-only case, which is the better choice: they must also hold on the reference backend, so the shape itself is checked everywhere and only the chunked dispatch is backend-specific. Note the test harness refused to report a pass when I filtered in a way that selected no case ("error: no test case ran … refusing to report a pass") — that is the behaviour I want from a test binary, and it is why the two OK lines above are worth quoting.
kCommonMaxChunkedTokens = 1 << 16 as a ceiling rather than "unbounded" is right: an 8464-token case is the motivation, a 65,536-token one would be a different problem (and probably a different bug), and the boundary is asserted by common_is_supported_chunked_token_count() rather than assumed.
Not run, and it is the part that matters most: the chunked dispatch itself. build226 is GGML_HRX=OFF, so these cases ran through the CPU reference and the HRX path (allow_token_chunks, the per-plane chunked set at dispatch-mul-mat.cpp:1397-1403) was never entered. The command that would cover it: an HRX build + test-backend-ops -b HRX0 -o MUL_MAT under gpu-hold. What I can take is that the shapes are well-formed on the reference, the helper is the shared one, and the bound is explicit.
… too (#248) #244 took the *batched* matmul path above the 2048-token tile range. The plain 2-D matmuls at the same token counts are the larger class in Qwen3-VL's CLIP graph (`[1024,8464]`, `[3072,8464]`, `[4096,8464]`, `[4096,2116]`, `[2048,2116]`, `[8464,1024]`) and were refused by the same range, so chunk them as well — but only for the form where that is provably safe: the plain contraction, with no activation packing, no split-k and no fused unary, and binding the input itself. Everything else keeps today's behaviour and stays on the CPU rather than being handed a dispatch whose meaning chunking would change. Measured on HRX0 against the CPU reference: `m=1024,n=8464,k=1024,bs=[1,1]` (the shape family of the graph's refused 2-D matmuls, float weights) went from `not supported [HRX0]` to **OK**, and `-o MUL_MAT` is 567/567. Scope, measured rather than implied: the CLIP graph's own projections carry **quantized** weights, where the packed-q8 and split-k paths are exactly what this guard declines, so the graph-level refusal count barely moves yet. The test row proves the path, not the graph; chunk-aware activation packing is the larger change that would. Refs ggml-org#1467 Co-authored-by: pi-hrx-compat <[email protected]>
What refuses, and what this fixes
Qwen3-VL-2B's CLIP (mmproj) graph reports 118 nodes as unsupported on HRX0 at load. Measured per op, and then per
layout with a temporary probe in
device_supports_op(saved outside the tree, not in this PR):MUL_MAT f32with 8464 tokens (CLIP attention: 8464 image tokens x 16 planes)common_is_supported_prefill_token_count()—token_count <= 2048, a tile range, not a shape or layout limitSOFT_MAXwith 8464-wide rows (288 declines)FLASH_ATTN_EXT ne=[64,16,8464,1](288)IM2COL f16 [768,92,92,1]UPSCALE f32CONT/PERMUTE/RESHAPEThe change
A >2048-token matmul is not unrepresentable — the fork already splits a dispatch that exceeds its kernel range
(
append_token_chunked_dispatches, used by ROPE and by flash attention's query rows). Do the same here:common_match_mul_mat_any_formatgainsallow_token_chunks, accepting up tokCommonMaxChunkedTokens(65536) instead ofthe tile's 2048. Only callers that chunk may pass it, so no shape accepted today changes.
output rows per chunk and carrying each chunk's token capacity. One chunked set per plane, so batching is unchanged.
test-backend-opsrows at the vision shapes.Measured (HRX0, reference-checked)
m=256,n=4096,k=256,bs=[1,2]not supported [HRX0]m=128,n=8464,k=64,bs=[1,16](the CLIP attention shape)not supported [HRX0]-o MUL_MATsuiteSecond commit: the plain 2-D matmuls (the larger class)
The first commit covers the batched attention contractions. The plain 2-D matmuls at the same token counts
(
[1024,8464],[3072,8464],[4096,8464],[4096,2116],[2048,2116],[8464,1024]) are the larger class in the CLIPgraph and were refused by the same
token_count <= 2048range, so they are chunked too — only for the form where that isprovably safe: the plain contraction, no activation packing, no split-k, no fused unary, binding the input itself. Anything
else keeps today's behaviour and stays on the CPU rather than receiving a dispatch whose meaning chunking would change.
Measured:
m=1024,n=8464,k=1024,bs=[1,1]went fromnot supported [HRX0]to OK,-o MUL_MATis 567/567.Honest scope, measured rather than implied: the CLIP graph's own projections carry quantized weights, where the packed-q8
and split-k paths are exactly the ones the guard declines — so the graph-level refusal count barely moves yet (the decline
probe still shows the same
w=[1024,1024] in=[1024,8464]layouts). The test row proves the path; it does not prove the graph.Closing that needs chunk-aware activation packing, which is a larger change and is not in this PR.
A diagnostic run also settled why the remaining nodes decline, since a matcher's decline reason is otherwise only printed when
a graph fails: 134902 probes of the shared matcher hit the token-count check, 13284 a non-contiguous weight/input, 9280 a
non-contiguous activation (the CLIP attention matmul reads a
PERMUTE). That is the follow-up list on ggml-org#1467.Why this says
Refs, notFixesThe issue's acceptance is "image TTFT <= 17 s with the projector on HRX0". That cannot be met by fork-side matcher work
alone:
IM2COL,UPSCALEand flash attention at head_dim 64 need kernels that do not exist in the HRX kernel set (privaterepo), and the CLIP matmuls whose activation arrives as a non-contiguous
PERMUTEare declined for contiguity before the tokenrange is consulted. This PR removes one real class of the refusal and is provable on its own; the issue stays open with the
list above.
Refs 1bit-MONSTER/engine#1467