Skip to content

npu plan: the reference plans carry the output gate's q rows (#819) - #824

Merged
bong-water-water-bong merged 6 commits into
mainfrom
npu/plan-regen
Nov 2, 2026
Merged

bong-water-water-bong merged 6 commits into
mainfrom
npu/plan-regen

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Why

#819 taught tools/npu_plan.py that a qwen3_5 model with attn_output_gate emits q|gate (the C side
already doubles q — npu/forward/src/q4nx_pack.c:150), and fixed the generator. It left the four committed
reference plans alone, so three of them still ask for K5120 N8192 on their 16 full-attention layers:
q + k + v with no gate. tools/npu_plan.py <dir> --check has been failing on those model dirs ever since:

@@ attn layers, qkv
-    "N": 8192,
+    "N": 14336,

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 packing
factor 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):

tensor container shape un-packed rows
self_attn.q_proj [7680,5120] 12288 = q 6144 + gate 6144
self_attn.k_proj / v_proj [640,5120] 1024 each
fused QKV — 14336

So 14336 is the model's own geometry and 8192 was the attn_output_gate omission 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:

python3 tools/npu_plan.py ~/models/npu/<model>            # writes <model>/plan.json
cp ~/models/npu/<model>/plan.json configs/plans/<model>.plan.json
python3 tools/npu_plan.py ~/models/npu/<model> --check    # "matches the model dir"

Qwen3-0.6B.plan.json is 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 no
launch ever read the stale numbers (q4nx_forward.cpp:703-1000). It stops being inert the moment something
trusts per-layer geometry — P4's NpuTarget (npu/target/xdna) is being built on exactly that field, and any
future per-layer kernel selection would name full_i8_QKV_K5120_N8192.elf, which no kernel directory on the box
holds. 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.json fixture and the model's kernel file names (the generator's fusion/decode.dirs
depend on which kernels exist), i.e. a small fixture farm for four models. The rule itself is covered — #819 added
the attn_output_gate case to ctest npu_plan_generate — and the drift is caught by the checklist's
tools/npu_plan.py <dir> --check.

Verified (strixhalo, host-side, no device, no npu-hold ticket)

row result
--check on all four model dirs matches the model dir (three failed before)
ctest npu_plan, npu_plan_generate, safetensors_to_q4nx 3/3 PASS
tools/copyright.py --check rc 0
diff shape 3 files, 48+/48−, only attn.qkv.N
owner serve :18097 untouched; it reads plan.json at startup only, and the fields it reads are unchanged

#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.
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Preparing review...

@bong-water-water-bong bong-water-water-bong added the tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied label Nov 2, 2026

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit eccad14)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

819 - Partially compliant

Compliant requirements:

  • Reference plans updated to reflect correct N values (14336) for attn layers with attn_output_gate true
  • Plans regenerated using updated tool to ensure consistency
  • Verification that plans match model geometry

Non-compliant requirements:

  • None

Requires further human verification:

  • None
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect N value in reference plans

The reference plan files contain outdated N values (8192) for attention layers that should be 14336 when attn_output_gate is true. This discrepancy could cause incorrect kernel selection or runtime behavior on hardware.

"N": 14336,
Incorrect N value in reference plans

The reference plan files contain outdated N values (8192) for attention layers that should be 14336 when attn_output_gate is true. This discrepancy could cause incorrect kernel selection or runtime behavior on hardware.

"N": 14336,
Incorrect N value in reference plans

The reference plan files contain outdated N values (8192) for attention layers that should be 14336 when attn_output_gate is true. This discrepancy could cause incorrect kernel selection or runtime behavior on hardware.

"N": 14336,

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 50d1ba0

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit c9eb696

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit 465afb7

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Persistent review updated to latest commit eccad14

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@bong-water-water-bong
bong-water-water-bong merged commit 39b39ab into main Nov 2, 2026
8 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the npu/plan-regen branch November 2, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review effort 2/5 tprs-ready TPRS: review verdict READY posted by the standing reviewer; merge gate satisfied

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant