Repository navigation
mtp: block the head's matvec over n so a code byte shares its cache line (2.7x on the host head, bit-identical) - #968
Conversation
PR Reviewer Guide 🔍(Review updated until commit 07d9a80)Here are some key observations to aid the review process:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 822b24b4c. Bit-identity confirmed end to end, and the speedup is larger than claimed here (the box is loaded, so treat the absolute numbers as soft and the direction as solid).
Built and ran both halves, at this head with gcc-16 / Release:
$ ./build/mtp_draft_test
PASS matvec: the blocked form is BIT-identical to the unblocked form on every column
INFO matvec: 692 columns over 5 shapes, 0 differ (K x N incl. a 200 = 3x64+8 block tail)
rc 0
The block tail is covered (N = 200 = 3*64 + 8), which is the case a NB=64 blocking gets wrong if the last block is mishandled.
The end-to-end A/B, same bench binary, only npu/mtp/mtp_head.cpp swapped for origin/main's:
| median ms/step (8 steps) | min | max | output hash | |
|---|---|---|---|---|
main's unblocked matvec |
2656.27 | 1180.14 | 3445.04 | 91688767b0eba370 |
| this head, blocked | 432.09 | 292.14 | 455.90 | 91688767b0eba370 |
6.15x median here against the 2.7x in the body — the same bytes out (FNV-1a over 8 x 5120 floats, identical across every run I did, including a third run of each that landed at 40.45 and 3490.77 ms) and the spread between my runs of the same build was large, so I would not quote any of these as a headline; the claim is conservative, and what matters is that the hash never moved.
Why the arithmetic is safe, read not just measured: for each column the accumulation order is unchanged — P over k ascending, acc over g ascending, then the additive-min pass over g — and ex[i] = ldexp(1.0, eref[...]) is hoisted but pure, so nothing is reassociated. The bounds are safe too: ck[i] with i < w = min(N - n0, NB) cannot pass N - 1, and nblocks rounds up so the tail is handled. ag_sum was dead in the old form and is gone.
tools/mtp_head_bench.cpp is a new file with the notice, and the --matvec / --steps flags make the A/B reproducible, which is what let me run this at all.
|
Persistent review updated to latest commit b540bae |
|
Second commit at One array lm_head call, measured ( So the drafter's four logits calls per iteration cost ~117 ms, not the ~1.8 s the 2026-11-02 12:44 entry fitted by subtracting base passes out of a draft-phase wall clock. That fit was the basis for "the per-draft logits call is the cost, P3 is the only lever" — it was wrong, and this PR is the correction: the cost was the host head's matvec (previous commit, 2.6x, bit-identical), which is where the drafter's time actually went. Same ticket, end-to-end proof of the previous commit on the real model:
Both are the counts this lane measured before the matvec change (2026-11-02 12:11/12:38) — bit-identical is not a claim about the bench hash only. Wall clock on those tickets: prompt 0 plain Also added, and deliberately carries no number: |
…ine (2.7x on the host head, bit-identical) The MTP head runs on the host (npu/mtp/mtp_head.cpp, the pb16i-exact form) and one head step is its eight matvecs: 424 M MACs. The code image is laid out [k][n], so the per-column loop read its 5120 codes with a stride of N bytes -- one 64-byte cache line per 1-byte code, ~27 GB of traffic per head step -- and measured 1-7 GF/s per matrix. Walk a block of NB=64 columns at once instead. For a fixed k the block's codes are 64 contiguous bytes, so each line fetched is fully used. Each column keeps its exact accumulation order (g ascending, k ascending, then the additive-min pass over g), so the result does not move: the bench's output hash is identical between the two builds, and tests/mtp_draft_test.cpp [10] keeps the unblocked form as the reference and compares every column bit for bit (692 columns over 5 shapes, including a 200 = 3*64+8 block tail). Measured on this box, interleaved A/B (three alternating reps, same window, same weights, --steps 5 --matvec 15): matvec sum, the eight matrices 135-155 ms -> 50-68 ms head step, no lm head (median) 152-162 ms -> 57-71 ms output hash e817b44a834a7eef == e817b44a834a7eef Per matrix (one rep): fc 14.4->11.5, q 27.2->8.6, k 9.2->5.6, v 8.1->9.5, o 11.2->6.2, gate 24.8->9.0, up 24.1->8.7, down 36.4->9.2 ms (absolute milliseconds move with box load; the ratio and the hash are what the claim rests on). tools/mtp_head_bench.cpp is new and is how the above is stated: it loads the real head, prints per-matrix and per-step milliseconds, and hashes every output hidden, so the next change to the head's maths has a before/after and a bit-exactness check instead of an argument. The live loop runs 6 + accepted head steps per iteration (both commit_rows calls and every chained draft), so this is ~0.7-0.9 s off a ~4 s iteration; the device re-measurement of that lands separately. No acceptance number can move, because the head's output is bit-identical. Refs #805 (P6's gate: acceptance, hit rate and passes/token for this drafter).
… call, and a verify batch's row scaling SERVE-RECIPE and TMTPLOOP-STATE carried two numbers that were fits, not measurements: "~0.4 s per full-vocab logits call" (from subtracting base passes out of a draft-phase wall clock, 2026-11-02 12:44) and "two passes per iteration" (from counting the loop's calls). Neither survives a direct measurement, and the draft route's break-even is computed from both. --probe-lm N times N real lm_head calls on the probe step's own hidden and prints median/min/max. Measured on this box, prompt 0, one npu-hold ticket (151 s, amdxdna errors 0): lm head probe: 10 calls over vocab 248320, median 29.3 ms, min 28.2, max 30.4 i.e. the drafter's four logits calls per iteration are ~117 ms, not ~1.8 s: the array's vocabulary projection is NOT what makes the draft route slower, and the host head (tools/mtp_head_bench.cpp, 2.6x in the previous commit) was the dominant term. --batchcheck now also prints the verify batch's cost at M = 1, 2, k+1 and max_batch_rows on the same stream. The loop's "two passes per iteration" is only true if an M-row batch costs one row; the kernels are sealed at M=16, so either the pad rows are idle (batch harder, #805's leaf fill) or the work is per row (fix the batch). Instrumented and not yet run -- the array went into the owner's amdxdna maintenance window before it could be, so it carries no number here. The same run also re-proves the previous commit end to end: prompt 0 gave TEXT IDENTICAL, 44/80, chain 3.20, stream 72 rows, 0 mismatch, per-depth 90/65/35/30 and prompt 3 gave 50/64, chain 4.12 -- the same counts as before the matvec change, which is what bit-identical means on the real model at speed: plain 0.588 s/tok vs draft 0.904 s/tok (0.65x) on prompt 0, 0.484 vs 0.771 (0.63x) on prompt 3.
721cf90 to
e229e0f
Compare
|
The unmeasured item in this PR is now measured, and it closes the cost model. One So a verify batch is near-linear in rows, and the loop's Filed as #972 with the arithmetic and the break-even conclusion (no Also rebased onto main |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at e229e0fc4 (re-verdict; my READY was at 822b24b4c).
The certified code is untouched. The Matrix::matvec I verified bit-identical is the same bytes at both heads:
822b24b4c matvec: a71c9a5cf61a646f
e229e0fc4 matvec: a71c9a5cf61a646f
so the A/B I ran (2656.27 → 432.09 ms/step, output hash 91688767b0eba370 identical both ways) and the mtp_draft_test arm (692 columns, 5 shapes, 0 differ, block tail N=200) carry to this head unchanged. The other changes that appear in the diff since my verdict — depth_verdict() in mtp_head.cpp, the reached_at/LiveDepths additions, the +98 in tests/mtp_draft_test.cpp — are #916 arriving through a main merge, not part of this PR.
The one new own commit is measurement tooling, and it is honest about itself. e229e0f adds --probe-lm N (+55 lines in tools/mtp_acceptance.cpp) to replace two numbers that were fits:
lm head probe: 10 calls over vocab 248320, median 29.3 ms, min 28.2, max 30.4
i.e. the drafter's four logits calls are ~117 ms an iteration, not the ~1.8 s the recipe carried, which is a useful correction to the draft route's break-even arithmetic — and it points at the same conclusion my A/B reached from the other side: the host head was the dominant term, not the vocabulary projection.
It also re-proves the previous commit end to end on the real model at speed — TEXT IDENTICAL, per-depth 90/65/35/30 and 50/64, chain 3.20/4.12, 0 mismatch, "the same counts as before the matvec change, which is what bit-identical means" — and it says plainly that the --batchcheck M-row row is instrumented and not yet run, so it carries no number. That is exactly the right way to land a probe: no row claimed that was not measured.
Not run by me: the new --probe-lm and the --batchcheck extension (both need an npu-hold ticket, a real base model and the sealed head ELFs; the lane's 151 s run had 0 amdxdma errors). Nothing here changes what I verified with the host bench, and tools/mtp_acceptance.cpp is not in the shipped binary.
|
Persistent review updated to latest commit c13882b |
|
Persistent review updated to latest commit 1b90867 |
|
Persistent review updated to latest commit 7bdb7d8 |
|
Persistent review updated to latest commit f0e3018 |
|
Persistent review updated to latest commit 5b741c4 |
|
Persistent review updated to latest commit 07d9a80 |
Root cause
Matrix::matvec(npu/mtp/mtp_head.cpp) walked the code image one column at a time. The image is laid out[k][n], so one column's 5120 codes are read with a stride ofNbytes — one 64-byte cache line per 1-bytecode. One MTP head step is those eight matvecs (424 M MACs,
fc q k v o gate up down), i.e. ~27 GB of trafficper step, and it measured 1-7 GF/s per matrix. The live draft loop runs
6 + acceptedhead steps periteration (both
commit_rowscalls plus every chained draft), so this was the loop's largest single cost on thehost — bigger than the drafter's logits calls, which is not what
SERVE-RECIPE.mdassumed.Fix
Block
n(NB=64) instead of walking single columns: for a fixedka block's codes are 64 contiguous bytes, soevery line fetched is fully used. Each column keeps its exact accumulation order (g ascending, k ascending,
then the additive-min pass over g), so not one float moves.
npu/mtp/mtp_head.cpp: the blocked loop (with the reason written next to it).tests/mtp_draft_test.cpp[10]: the unblocked form is kept in the test as the reference and every columnis compared with
memcmp— 692 columns over 5 shapes, includingN = 200 = 3*64 + 8so a wrong block tail boundfails it. Deleting the defect's statement is the only way to pass it.
tools/mtp_head_bench.cpp(new, +1 CMake target, host only, no device): loads the real head, prints per-matrixand per-step milliseconds, and hashes every output hidden. That is how the numbers below are stated, and how the
next change to the head's maths gets a before/after instead of an argument.
Measured (this box, interleaved A/B, three alternating reps, same window, real weights)
Per matrix, one rep (
--steps 5 --matvec 15):fc14.4→11.5,q27.2→8.6,k9.2→5.6,v8.1→9.5,o11.2→6.2,gate24.8→9.0,up24.1→8.7,down36.4→9.2 ms. Absolute milliseconds move with box load(other lanes' builds); the ratio and the hash are what the claim rests on.
ctest -R mtp_draft1/1,tools/copyright.py --checkrc 0,ninja mtp_head_bench mtp_draft_test mtp_acceptancerc 0.
What this does and does not change
(
44/80, chain3.20, per-depth90/65/35/30on prompt 0) stands. The device re-measurement of the liveloop's wall clock lands separately (one
npu-holdticket, prompt 0, the same numbers expected).from a ~4 s iteration is the first lever that moves that ratio in the route's favour; it is not claimed until the
ticket reports it.
Refs #805 (P6: acceptance, hit rate, passes/token for this drafter). No device number is claimed in this PR.