Skip to content

perf(qwen35): faster long-prompt prefill, same bits - #982

Merged
davide221 merged 7 commits into
mainfrom
perf/qwen35-prefill-main
Nov 10, 2026
Merged

davide221 merged 7 commits into
mainfrom
perf/qwen35-prefill-main

Conversation

@davide221

@davide221 davide221 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

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.

item seconds share
GPU 138.24 97.6%
paged attention (16 full-attention layers) 58.75 41.5%
quantized matmuls (MMQ), all 56.37 39.8%
of which IQ4_XS 27.93
of which IQ3_S 8.57
of which Q4_K 6.13
of which Q5_K 5.28
of which IQ3_XXS 3.25
of which IQ2_S 1.91
of which Q3_K 1.84
of which other types 1.46
gated delta net (48 linear-attention layers) 7.51 5.3%
concat (DeltaNet conv input) 6.41 4.5%
elementwise, norms, RoPE, SwiGLU, conv, copies 9.20 6.5%
host 3.34 2.4%
prefix checkpoint copy 1.45 1.0%
token embedding on the CPU 0.72 0.5%
embedding upload (pageable) 0.70 0.5%
tokenize and admit 0.26 0.2%
graph build, syncs, gaps, last partial chunk 0.21 0.1%
total 141.58 (measured 141.7 s)

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

  1. Concat fast path for strided rows (ggml-cuda/concat.cu). Each DeltaNet layer builds its conv input as concat(conv_state, transpose(qkv)). When a layer's gate and qkv projections are stacked, qkv is 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.
  2. Prompt embeddings go straight into pinned rows (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.
    • The buffer is the shared PinnedStage helper, reserved once when the engine is built: the most prompt rows step_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.
    • 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.

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_hugepage enabled and defrag settings, and with defrag=madvise a fault
in 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:

prompt tokens main TTFT this PR TTFT change generated text
1,035 1.18 s 1.16 s -1.7% identical
4,176 3.79 s 3.62 s -4.5% identical
18,355 18.06 s 17.25 s -4.5% identical
53,030 66.02 s 63.47 s -3.9% identical
102,766 166.29 s 161.04 s -3.2% identical

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:

  • 1K, 2K, 4K and 9K tokens without the drafter: two runs of main gave the same four texts;
    two runs of this PR differed from each other in all four, and the second matched main in
    two of the four.
  • 0.6K, 0.8K, 1K and 1.6K tokens without the drafter: two runs of main differed from each
    other in two of the four texts; this PR matched the first main run in all four.
  • 0.6K, 1K, 2K and 3.6K tokens without the drafter, which needs steps that carry prompt
    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.
  • 1K, 2K, 4K and 9K tokens with the drafter at fixed width (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.
  • End to end: the greedy no-drafter text is byte-identical to main at all five prompt sizes above.

Not in this PR

  • MMQ is the second-largest item. At 2,048-row chunks every 128-column block dequantizes the same IQ4_XS weight tile again, 16 times per chunk, about 22% of that kernel's VALU work. A larger chunk does not amortise it (4,096-row chunks: same matmul time).

🤖 Generated with Claude Code

davide221 and others added 7 commits November 10, 2026 20:26
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
davide221 marked this pull request as ready for review November 10, 2026 22:43

@davide221 davide221 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
davide221 merged commit d972beb into main Nov 10, 2026
18 checks passed
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]>
@davide221
davide221 deleted the perf/qwen35-prefill-main branch November 12, 2026 14:57
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