Repository navigation
perf(qwen35): faster long-prompt prefill, same bits - #982
Merged
Merged
Conversation
The Qwen 3.5/3.6 DeltaNet conv input is concat(conv_history, transpose(qkv)) along tokens. Where a layer's gate and qkv projections are stacked, qkv is a column slice of 16,384-float rows, so the dense-only fast path rejected it and the generic concat_non_cont kernel ran instead, reading one channel per lane `row` floats apart (about 64 GB/s on gfx1201). The 32x32 shared-memory transpose now takes the source's token-row and sequence strides; channels must still be contiguous. It is a copy, so the output bits are unchanged. Measured on an R9700 (Qwen3.8-27B IQ4_XS, paged prefill, 2,048-row chunks): CONCAT 6.41 s -> fast path at a 102,766-token prompt; time to first token 1K / 4K / 18K / 53K / 103K 1.2 / 3.8 / 17.3 / 59.4 / 142.3 s -> 1.2 / 3.6 / 16.5 / 57.1 / 138.0 s; greedy text without the drafter identical at all five sizes. test_concat_transpose: three strided cases (including 3 + 2048 tokens x 10,240 channels from 16,384-float rows), bit-exact against the host reference, all on the fast path. Co-Authored-By: Claude <[email protected]>
Each 2,048-row prefill step used to dequantize its token embeddings into a fresh, zero-filled 42 MB vector, copy them into a second host buffer, and upload that pageable buffer synchronously. Prompt chunks and decode rows are now embedded once into one pinned host buffer (the device's host buffer type), grown on demand and uploaded asynchronously. Lifetime: the buffer is only rewritten by the next step. Every step synchronizes after its compute, nothing returns between the upload and the compute, and the compute-failure path now synchronizes before it returns. Measured on an R9700 (Qwen3.8-27B IQ4_XS, paged prefill): host time per 2,048-row chunk staging 15.9 -> 7.9 ms, upload 14.1 -> 0.03 ms. With the checkpoint change that follows, time to first token at 1K / 4K / 18K / 53K / 103K tokens 1.2 / 3.6 / 16.5 / 57.1 / 138.0 s -> 1.1 / 3.5 / 16.3 / 56.7 / 137.0 s; greedy text without the drafter identical to the base at all five. Co-Authored-By: Claude <[email protected]>
A checkpoint copies the paged KV and recurrent state into a fresh host buffer (3.7 GB at a 103K-token prompt), whose pages are first touched by that copy. On Linux, madvise(MADV_HUGEPAGE) on the buffer's 2 MiB-aligned interior lets the copy fault once per 2 MiB instead of once per 4 KiB. Advice only: contents and layout are unchanged, and it compiles out where MADV_HUGEPAGE does not exist. Measured on an R9700 box (THP in madvise mode): checkpoint time at 18K / 53K / 103K tokens 322 / 768 / 1,392 ms -> 282 / 690 / 1,271 ms. Co-Authored-By: Claude <[email protected]>
…path The fast path reads src1 with contiguous channels and any token and sequence stride that is a multiple of 4 bytes, so a token row may be a column slice of a stacked projection and sequences may be padded. Two comments still described the strict dense layout of the original specialization. With the fast path, the concat kernels at 102,766 prompt tokens on Qwen3.8-27B take 0.77 s of GPU time instead of 6.41 s (profiled on the branch of #980, whose concat is the same as main's). Co-Authored-By: Claude <[email protected]>
Adds a case whose source sequences are not tokens x row width apart (5 unused token rows between them), so the sequence stride is checked on its own and not only through the token stride. Co-Authored-By: Claude <[email protected]>
This reverts commit b2ca568. The test box runs transparent huge pages with enabled=madvise and defrag=madvise. Under defrag=madvise, a page fault in a MADV_HUGEPAGE region enters direct reclaim and compaction, so on a box whose free memory is fragmented the checkpoint copy can stall instead of getting faster. The measured gain was 120 to 135 ms per checkpoint at 53K to 103K tokens (0.1% of the time to first token), on a box with plenty of free 2 and 4 MiB blocks; that is too small to carry a platform setting dependent risk. Main (f5993e0) against this branch after the revert, Qwen3.8-27B, greedy without the drafter, time to first token: 1.18 to 1.16 s at 1,035 tokens, 3.79 to 3.62 s at 4,176, 18.06 to 17.25 s at 18,355, 66.02 to 63.47 s at 53,030 and 166.29 to 161.04 s at 102,766 tokens (-1.7 to -4.5%). The generated text is identical at all five sizes. Co-Authored-By: Claude <[email protected]>
…tage The pinned input-embedding rows grew during requests through a raw host buffer, freed by hand. They now use the shared PinnedStage helper and are reserved once when the engine is built, for the largest step step_plan_limits() allows: min(max prefills x 2048, the largest of the idle, mixed and long mixed token caps) prompt rows plus the decode bucket of every slot. step() already rejects plans above those limits, and checks the sum once more before staging. Without pinned memory the rows fall back to pageable memory, with one log line. Also drops a token offset that was initialized twice. Co-Authored-By: Claude <[email protected]>
davide221
marked this pull request as ready for review
November 10, 2026 22:43
davide221
commented
Nov 10, 2026
davide221
left a comment
Contributor
Author
There was a problem hiding this comment.
Code review
No issues found. Checked for bugs and logic errors in the changed code.
Reviewed 5334d4d8 · 0 candidate issues checked, kept at ≥70% confidence · add the code-review label to review again
davide221
added a commit
that referenced
this pull request
Nov 10, 2026
The lane-isolation fix this branch was stacked on is on main as #981, so the branch now carries only the overlap step on top of main. Co-Authored-By: Claude <[email protected]>
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.
This PR makes the time to first token on long prompts 2 to 5% shorter on Qwen3.8-27B
(measured; 5.2 s at 100K tokens). The concat gain applies wherever the loader stacks the
gate and qkv projections. Output is bit-identical: the changes copy data or move buffers
and never change a computation. There is no new option or environment variable.
Where the prefill time goes
One 102,766-token prompt, Qwen3.8-27B UD-IQ4_XS, paged prefill in 2,048-row chunks,
AMD Radeon AI PRO R9700. This profile was measured on the branch of #980 (faster
long-context paged attention), which this work started from, before this PR. GPU time is
per-node event timing over every prefill graph; host time is per-step wall-clock timers.
The rows add up to the wall time.
The elementwise ops already run at 480 to 610 GB/s. Attention and the matmuls are the
large items; this PR takes the exact items that were plainly wasted.
On current main (f5993e0) the same prompt takes 166.3 s, not 141.7 s, because main does
not have the faster paged attention of #980. That puts paged attention at about 50% and
MMQ at about 34% on main. These two shares are estimated from the time-to-first-token
difference, not re-profiled. The items this PR removes take the same absolute time on
both.
Changes
ggml-cuda/concat.cu). Each DeltaNet layer builds its conv input asconcat(conv_state, transpose(qkv)). When a layer's gate and qkv projections are stacked,qkvis a column slice of wider rows (10,240 of 16,384 floats). The transposed-concat fast path only took dense sources, so the generic kernel ran instead, reading one channel per lane a full row apart. The 32x32 shared-memory transpose now takes any token and sequence stride of the source that is a multiple of 4 bytes; channels must still be contiguous. It is a copy, so the bits do not change. Concat GPU time at 103K: 6.41 s to 0.77 s.qwen35_seq_engine). Each 2,048-row step used to dequantize its token embeddings into a fresh, zero-filled 42 MB vector, copy them into a second host buffer, and upload that pageable buffer. Prompt chunks and decode rows are now embedded once into one pinned host buffer and uploaded asynchronously. Host time per chunk: staging 15.9 to 7.9 ms, upload 14.1 to 0.03 ms.PinnedStagehelper, reserved once when the engine is built: the most prompt rowsstep_plan_limits()allows in one step, min(max prefills x 2,048, the largest of the idle, mixed and long mixed token caps), plus the decode bucket of every slot. With the default limits and 8 slots that is 4,104 rows, 84 MB.step()already rejects larger plans. Without pinned memory it falls back to pageable memory, with one log line.An earlier revision also advised huge pages for the prefix checkpoint buffer. It saved
about 125 ms per checkpoint at 53K to 103K tokens, but its effect depends on the
transparent_hugepageenabledanddefragsettings, and withdefrag=madvisea faultin an advised region enters direct compaction, so on a fragmented machine it could stall
instead. It is reverted in this PR.
Results
Same machine, one build directory: main (f5993e0), then main with this PR. Greedy
decoding without the drafter, 300 generated tokens:
Decode speed is unchanged (25.3 vs 25.4 tok/s at 100K). On the branch of #980, the first version
of these changes gave 142.26 to 136.88 s at 100K (-3.8%).
With several requests at once, the text depends on timing, on main as well. A decode
row's logits depend on how many rows share its step: a matmul of up to 8 rows runs the
vector kernel (MMVQ) and one with prompt rows beside it runs MMQ, and with the drafter a
verify of two or more lanes runs split-k MMQ (
ggml_backend_cuda_set_mmq_splitk_scope).Which rows share a step depends on when requests arrive and, once the prompts being
prefilled bring more than 8,192 new tokens, on wall time: after each prompt chunk the
scheduler lets the decoding requests step alone for as long as that chunk took. A faster
prefill therefore regroups the rows. Four requests at once, greedy, 200 tokens each:
two runs of this PR differed from each other in all four, and the second matched main in
two of the four.
other in two of the four texts; this PR matched the first main run in all four.
rows and decode rows together but stays under 8,192 tokens, so no time window applies:
two runs of main and this PR gave the same four texts, byte for byte.
LUCE_ADAPTIVE_SPEC_WIDTH=0):two runs of main gave the same texts, as did two runs of this PR; the two builds differ
in two of the four.
One request at a time, the texts are identical to main with and without the drafter
(fixed width, 1,035 / 4,176 / 18,355 tokens).
Tests
All on main with this PR, on each GPU of a two-GPU machine:
test_concat_transpose: four new strided cases, including 3 + 2,048 tokens x 10,240 channels taken from 16,384-float rows, and sequences that are not tokens x row width apart. All are bit-exact against the host reference and all take the fast path. 13/13 pass on each GPU (main: 9/9).test_recurrent_snapshot: covers checkpoint snapshot and restore. 5/5 pass on each GPU.test_generate --seq-engine-contract: runs the Qwen3.5 sequence engine through mixed prefill and decode, failures, and slot reuse, with the pinned embedding rows. It passes on each GPU (4 slots), as on main.Not in this PR
🤖 Generated with Claude Code