Skip to content

npu: 64x64 tiled transpose for projection assembly (first-token latency) - #622

Merged
bong-water-water-bong merged 1 commit into
mainfrom
fix/587-pack-transpose
Oct 27, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
fix/587-pack-transpose

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

q4nx_assemble_B() writes a projection as B[i*N + n0+n] while walking n — one
float per cache line, 68 KB apart at N=17408, 0.56–1.6 GB/s effective on a machine
that does ~100. That call, not the quantisation that follows it, is where the first
token of a 64-layer GDN model goes.

Measured offline (a replay of one projection using the engine's own
scatter_transposed and pack loop, no device): ~/lb/587-bench/packbench on
strixhalo, 32 threads.

projection before after effective BW
5120x17408 (gate) 610.6 ms 27.0 ms 1.6 → 13 GB/s
17408x5120 (down) 1862.2 ms 24.0 ms 0.56 → 14 GB/s
5120x10240 (gdn qkv) 777.6 ms 18.0 ms 0.79 → 13 GB/s
6144x5120 (o) 296.8 ms 11.4 ms 1.19 → 13 GB/s
5120x6144 (z) 471.3 ms 11.3 ms 0.77 → 13 GB/s

Per layer, all six roles: 4799 ms → 313 ms.

End to end, Qwen3.8-27B-NPU2 Q4NX on the NPU (1bit forward-serve, gated on
~/npu.lock + 5× fuser /dev/accel/accel0, taskset -c 0-3, OMP_NUM_THREADS=4,
8 greedy tokens, prompt "The capital of France is"):

  • before: 505 s, " Paris.\nThe capital of Germany is"
  • after: 351 s, " Paris.\nThe capital of Germany is"

Scope: the output layout is deliberately unchanged, so packB_blocked in the
private add-on needs no change, and per-column scales are bit-identical (the
benchmark asserts that and exits non-zero otherwise). No change to quantisation,
to the device side, or to any cache path.

Why tiles and not the bigger rewrite: I benchmarked a blocked output layout
(14.6 GB/s, ~20 % better again) and dropped it — it would need a coordinated change
in the private add-on for a fifth of the win.

…tride

q4nx_assemble_B() lays a projection out as B[i*N + n0+n] while walking n, so
every element it writes is its own cache line, 68 KB apart at N=17408. Measured
on strixhalo with an offline replay of one 5120x17408 projection (the same
scatter_transposed and pack loop as the engine, no device):

    K=5120  N=17408 : 610.6 ms  ->  27.0 ms     (1.6 -> 13 GB/s effective)
    K=17408 N=5120  : 1862.2 ms ->  24.0 ms     (0.56 -> 14 GB/s)
    per layer, all six roles: 4799 ms -> 313 ms

64x64 tiles make every inner run 64 contiguous floats on both sides and the
16 KB source tile stays in L1/L2. The output layout is unchanged, so the pack
that consumes it does not change, and per-column scales stay bit-identical (the
benchmark exits non-zero if they differ; they match on all six geometries).

A driver trace had put 220.9 s of a 436 s request in the gap before each weight
BO was created, which is this call; a first token on Qwen3.8-27B-NPU2 measured
505 s for 8 tokens before and 351 s after with the same text.
@bong-water-water-bong
bong-water-water-bong merged commit beaf683 into main Oct 27, 2026
7 of 8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the fix/587-pack-transpose branch October 27, 2026 19:47
bong-water-water-bong added a commit that referenced this pull request Oct 27, 2026
* tools: rank measured NPU engine runs in a generated leaderboard

A row is one measured run, and the generator refuses any record that cannot say
how it was measured: instrument, repeat count, timestamp and source are required,
and a record with repeats=0 or a missing instrument fails --check instead of
being ranked. The unit decides the sort direction (s/token low-to-high, tok/s
high-to-low), rows are grouped by host so nobody compares a laptop NPU with a
server, and docs/npu-leaderboard.md carries a do-not-edit-by-hand banner.

Seeded with the six runs we have on disk: the 27B Q4NX pair on the NPU route,
three Flash-Next prefill points, and the Qwen3-4B four-slot arm from the #528
repro (noted as single-slot, since that arm never used four slots).

* leaderboard: emit the licence header from the generator

The build job runs tools/copyright.py --check, which wants a notice on every
tracked file. The generator now writes the same notice into the top of
docs/npu-leaderboard.md instead of fighting --fix on every regeneration.

* leaderboard: the 27B first-token pair from the #622 trace

435.9 s before the tiled transpose, 145.7 s after, same instrument and same model.
Both records carry what the gap analysis showed: which share is device wait and
which is host time.

---------

Co-authored-by: 1bit <[email protected]>
Co-authored-by: agent <agent@local>
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