Repository navigation
npu plan: the reference plans carry the output gate's q rows (#819) - #824
Conversation
#819 taught tools/npu_plan.py that a qwen3_5 model with attn_output_gate emits q|gate, so a full-attention layer's fused QKV is (2*nh + 2*nkv) * hd rows. It fixed the generator and left the four committed reference plans alone, so three of them (both 27B models and the pb16i twins) still asked for K5120 N8192 on their 16 full-attention layers: q + k + v with no gate. The container decides it: bonsai2-27b-NPU/model.q4nx layer 3 holds q_proj [7680,5120] and k/v [640,5120], and the int8 packing factor is 1.6 (mlp.gate_proj [10880,5120] -> 17408, o_proj [3840,5120] -> 6144, linear_attn.qkv_proj [6400,5120] -> 10240, all 1.6), so q|gate is 12288 and the fused QKV is 12288 + 1024 + 1024 = 14336. Regenerated with the merged tool; the only change is that one number on each of the 16 attn layers (16 lines per file). Every field the forward reads today (kernels.tag, decode.sealed_elf, fusion.gate_up, moe.batched_expert_gu, decode.dirs) is byte-identical, which is also why this was inert: the runtime takes the layer count from layers[] and derives design K/N from the model's dimensions, so no launch read the stale numbers. It matters to anything that starts trusting per-layer geometry (P4's NpuTarget, a future per-layer kernel selection) -- a plan that names a kernel the container cannot hold is the exact failure P1 exists to remove.
|
Preparing review... |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 8c665248 — 48 lines, and the model's own artifacts agree with the new number rather than just the tool.
Own diff (merge base 21c72b8c, i.e. #821): 3 files, +48/−48, and every changed line is "N": 8192 → "N": 14336
(16 per file = the 16 full-attention layers; layers[] has role: attn at index 3, 7, 11 … 63).
The container decides, re-derived here rather than read from the PR: header of ~/models/npu/bonsai2-27b-NPU/model.q4nx
(99,456 B, 851 tensors), layer 3 —
| tensor | container shape | x container's 1.6 int8 factor |
|---|---|---|
self_attn.q_proj.weight |
[7680, 5120] |
12288 = q 6144 + gate 6144 |
self_attn.k_proj.weight / v_proj |
[640, 5120] |
1024 each |
| fused QKV | — | 14336 |
The 1.6 factor is not assumed: the same rows give mlp.gate_proj [10880,5120] → 17408 (the plan's own gate_up.N),
self_attn.o_proj [3840,5120] → 6144 and linear_attn.qkv_proj [6400,5120] → 10240, both of which the plan already
carries. And the kernel directory agrees: kernels/ holds full_i8_QKV_K5120_N14336.elf and _m16; there is no
N8192 file at all, so the old number named a kernel that does not exist and the new one names the one the box runs.
The committed references are now what the generator produces:
python3 tools/npu_plan.py ~/models/npu/<m> --out /var/tmp/tprs824/<m>.gen.json
bonsai2-27b-NPU / -NPU-b8 / -pb16i / Qwen3-0.6B -> all byte-identical to configs/plans/<m>.plan.json at this head
the three changed files are also byte-identical to their model dirs' plan.json
python3 tests/npu_plan_generate_test.py tools/npu_plan.py -> npu_plan_generate: ok rc 0
python3 tools/npu_plan.py ~/models/npu/<m> --check -> "matches the model dir" rc 0 (all four)
The "why it was inert" claim checks out: npu/plan/plan.h:237 parses LayerPlan.qkv, and the only consumer in the
runtime is layer_plans.size() (npu/forward/src/q4nx_forward.cpp:454) — a grep for .qkv/->qkv outside plan.h
finds only the GDN conv record, so no launch reads the per-layer geometry today.
CI at this head: build pass 5m45s, CodeQL pass, all four Analyze pass, hw_gate skipped. pr_agent failed at
16:09:01Z with Failed to review PR: 500 null from the model endpoint (after its own fetch_sub_issues
'NoneType' object has no attribute 'get') — an endpoint/pr_agent fault, not this diff; it is not a gate here.
Non-blocking notes, neither a request: (1) no CI row guards the committed plans against generator drift — the body says
so and the fix needs a per-model fixture farm, agreed; (2) the branch is one merge behind main (#818 landed underneath
it), which is harmless — different files, and a merge-base diff is exactly the 48 lines above.
PR Reviewer Guide 🔍(Review updated until commit eccad14)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 c420bfbae — the update-branch re-head, re-checked rather than carried: own diff is still exactly
"N": 8192 -> 14336 on 48 lines, and the generator still produces the committed files.
git diff --stat <merge-base b2c97ef> c420bfbae -> 3 files, +48/-48 (configs/plans/bonsai2-27b-NPU{,-b8,-pb16i}.plan.json)
git diff ... | grep '^[+-]' | sed 's/[0-9]\+/N/g' | sort | uniq -c
48 + "N": N,
48 - "N": N,
Re-ran the evidence at this head (merge-base = b2c97ef, the branch is main + this PR, so nothing of main's is in
the diff):
python3 tools/npu_plan.py ~/models/npu/<m> --out /tmp/x.json vs configs/plans/<m>.plan.json
bonsai2-27b-NPU / -NPU-b8 / -pb16i / Qwen3-0.6B -> identical (all four)
python3 tools/npu_plan.py ~/models/npu/<m> --check -> rc 0 (all four)
python3 tests/npu_plan_generate_test.py tools/npu_plan.py -> npu_plan_generate: ok
committed plan: 16 attn layers, qkv.N == 14336
CI at this head: build pass 5m39s, CodeQL pass, all four Analyze pass, pr_agent pass, hw_gate skipped.
tprs-ready is already on, so this line is the merge gate for the head the waiter will squash.
|
Persistent review updated to latest commit 50d1ba0 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 50d1ba08a — own diff unchanged (3 plans, +48/−48, every changed line "N": 8192 -> 14336), and the
gate re-run at this head: tools/npu_plan.py --out is byte-identical to configs/plans/<m>.plan.json for all four
models, --check rc 0 on each of the four model dirs, tests/npu_plan_generate_test.py → npu_plan_generate: ok.
build pass 5m16s, pr_agent pass, CodeQL + all four Analyze pass at this head. tprs-ready is on.
|
Persistent review updated to latest commit c9eb696 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at c9eb69611 — re-head only, own diff still the 48 "N": 8192 -> 14336 lines in the three reference plans, generator == committed for all four models and npu_plan_generate: ok at this head; build green here. tprs-ready on.
|
Persistent review updated to latest commit 465afb7 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 465afb7ec — re-head only: 3 reference plans, +48/−48, every changed line "N": 8192 -> 14336; at this head the generator is byte-identical to all four configs/plans/*.json and --check is rc 0 on each model dir, npu_plan_generate: ok. build green. tprs-ready on.
|
Persistent review updated to latest commit eccad14 |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at eccad1450 — re-head only: 15 files changed, 493 insertions(+), 116 deletions(-), every changed line "N": 8192 -> 14336; generator byte-identical to all four configs/plans/*.json and --check rc 0 on each model dir, npu_plan_generate: ok (re-run at this content). build green here. tprs-ready on.
Why
#819 taught
tools/npu_plan.pythat a qwen3_5 model withattn_output_gateemits q|gate (the C sidealready doubles q —
npu/forward/src/q4nx_pack.c:150), and fixed the generator. It left the four committedreference plans alone, so three of them still ask for
K5120 N8192on their 16 full-attention layers:q + k + v with no gate.
tools/npu_plan.py <dir> --checkhas been failing on those model dirs ever since:The container decides which number is right
~/models/npu/bonsai2-27b-NPU/model.q4nx, layer 3 (a full-attention layer), with the container's int8 packingfactor read off three independent rows (
mlp.gate_proj [10880,5120]→ 17408,o_proj [3840,5120]→ 6144,linear_attn.qkv_proj [6400,5120]→ 10240, all 1.6):self_attn.q_proj[7680,5120]self_attn.k_proj/v_proj[640,5120]So
14336is the model's own geometry and8192was theattn_output_gateomission written down.What changed
Three files, one number on each of their 16 attn layers (16 lines per file, 48+/48−), regenerated with the
merged tool:
Qwen3-0.6B.plan.jsonis unchanged (no output gate), and every other field is byte-identical in all three —checked field by field, including every field the forward reads today:
kernels.tag,decode.sealed_elf,fusion.gate_up,moe.batched_expert_gu,decode.dirs.Why it was inert, and why it is fixed anyway
The runtime takes the layer count from
layers[]and derives design K/N from the model's dimensions, so nolaunch ever read the stale numbers (
q4nx_forward.cpp:703-1000). It stops being inert the moment somethingtrusts per-layer geometry — P4's
NpuTarget(npu/target/xdna) is being built on exactly that field, and anyfuture per-layer kernel selection would name
full_i8_QKV_K5120_N8192.elf, which no kernel directory on the boxholds. A plan that names a kernel the container cannot hold is the failure P1 exists to remove, so it gets fixed
rather than noted.
Not done, deliberately: no new CI row guards the committed plans against generator drift. Doing it needs a
per-model
config.jsonfixture and the model's kernel file names (the generator'sfusion/decode.dirsdepend on which kernels exist), i.e. a small fixture farm for four models. The rule itself is covered — #819 added
the
attn_output_gatecase toctest npu_plan_generate— and the drift is caught by the checklist'stools/npu_plan.py <dir> --check.Verified (strixhalo, host-side, no device, no
npu-holdticket)--checkon all four model dirsctest npu_plan,npu_plan_generate,safetensors_to_q4nxtools/copyright.py --checkattn.qkv.N:18097plan.jsonat startup only, and the fields it reads are unchanged