Repository navigation
npu: convert a real d3 pack (qwen3_5) — names, the three transforms, two load fixes (#796 host rung) - #819
Conversation
…and two load fixes (engine #796) `tools/safetensors_to_q4nx.py pack --arch qwen3_5` now converts a Decision-3.0 pack's backbone: `language_model.*` -> the names Q4nxNpuForward loads (configs/q4nx/qwen3.5_0.8b.json's q4nx_name column is the authority), `visual.*` skipped as the text path, and the three GDN transforms that load and compute garbage when they are wrong: A_log -> ssm_a -exp(A_log), F32 (the forward does expf(ssm_a * softplus(dt))) conv1d [CD,1,CK] transposed to tap-major [CK,CD]: the forward indexes conv[j*CD + c] in_proj_a/b rows zero-padded to 32 so they tile (q4_1 where the FLM config asks Q8_0 - a deviation verify() measures, not one it hides) Two converter bugs the engine found, both load failures rather than opinions: * the embedding was written 1-D, so model_load derived "vocab=254279680 hidden=0" from shape[0]/shape[1] (model.c:890). bf16 tensors now keep the source shape. * a d3 pack nests the text model under text_config and the engine reads config.json at depth 1 only (q4nx_semantics.c's value_for_top), so hidden_size stayed 0 and the layer count fell back to a default; text_config's keys are now lifted to the top level. Also fixes tools/npu_plan.py: the attn QKV N ignored `attn_output_gate`, so the plan asked for a 1024x3072 QKV kernel on a container whose rows are 5120 (npu/forward/src/q4nx_pack.c:150 is the doubling the C side already does). Found by running the generator on the real pack. Measured on this box, host-side only (no device opened, `journalctl -k | grep -c amdxdna.*ERROR` = 0 after every run): * the real vllm-sr/d3-lite pack (1.7 GB, 473 tensors) converts in 8.6 s: 320 tensors, 821 MB, 153 visual skipped, 36 F32 (ssm_a + ssm_dt.bias for 18 GDN layers) * `verify`: 320/320 tensors ok, worst q4_1 rel rms 0.0905 (model.layers.19.self_attn.v_proj.weight [512,1024]) * known answers on the real weights: 18/18 GDN layers have ssm_a == -exp(A_log) exactly and every value negative; 18/18 conv1d are tap-major with 0 of 24576 entries out of place * the engine accepts the container: H=1024 NL=24 NH=8 NKV=2 HD=256 IM=3584 NV=248320, 18 GDN layers, attn_output_gate=1, "derived 6 designs", then refuses by name at the one artifact the box does not have: forward init failed: plan .../plan.json: this model's plan needs the QKV kernel "full_i8_QKV_K1024_N5120.elf" in ... (missing) So the host rung stops one step short of "loads and answers", and not for a code reason: the forward asserts ELF existence even under NPU_INFER_HOST_GEMM=1, so the remaining item is the six-design set for this geometry (QKV 1024x5120, O 2048x1024, GU 1024x7168, D 3584x1024, GDN QKV 1024x6144, Z 1024x2048 - O is shared with GDN's ssm_out, so six and not seven). Tests: ctest safetensors_to_q4nx gains a d3-shaped fixture (renames, all three transforms with known answers, the 2-D embedding, the lifted config, the refusal for an unmapped tensor); ctest npu_plan_generate gains the attn_output_gate row. 26 new assertions, 7/7 green, tools/copyright.py --check rc 0.
PR Reviewer Guide 🔍(Review updated until commit 141176f)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 d540d8d (tprs-ready applied)
Converted the real pack myself (~/models/npu/d3-lite-src, 1.7 GB, 473 tensors) into a fresh output dir and
re-derived each row of your table; the three transforms are exact on the box, not just in the known-answer tests.
$ python3 tools/safetensors_to_q4nx.py pack ~/models/npu/d3-lite-src /var/tmp/d3-tprs --arch qwen3_5
wrote …/model.q4nx (820971656 bytes, 320 tensors) skipped 153 tensor(s) (visual.*, the text path)
$ python3 tools/safetensors_to_q4nx.py verify … --arch qwen3_5
320/320 tensors ok 36 f32 rows max err 0 98 bf16 rows 186 q4_1 rows
worst q4_1 rel rms 0.0905 model.layers.19.self_attn.v_proj.weight <- your number exactly
Independent of the tool's own transform (my own numpy against the source safetensors, reading the container's
raw entries, so verify's max err 0 is not circular):
ssm_a 18/18 GDN layers exactly -exp(A_log), bit for bit, every value < 0
ssm_conv1d 18/18 tap-major: [tap][channel] matches to 0.0 over 24,576 entries per layer
(the untransposed [channel][tap] ordering differs by up to 1.78 — the wrong order is detectable)
tools/npu_plan.py /var/tmp/d3-tprs→24 layers, gate/up fused, layout rm; the six full-attention layers
(3, 7, 11, 15, 19, 23) carryqkv K=1024 N=5120and the GDN layersN=6144, i.e. theattn_output_gate
doubling is in the plan, which is the bug you found in your own generator.python3 tests/safetensors_to_q4nx_test.py→ok;python3 tests/npu_plan_generate_test.py tools/npu_plan.py→
npu_plan_generate: ok;python3 tools/copyright.py --check→ rc 0.- 820,971,656 B for 320 tensors and 36
F32rows (ssm_a + ssm_dt.bias × 18 layers) — both as the body states.
Not verified by me, as the body says: the engine's load and its refusal naming
full_i8_QKV_K1024_N5120.elf, and the six-design table. That needs the NPU lane, the owner's :18097 serve holds the
device and I did not touch it. Worth one line when the kernel set lands: the refusal asserts ELF existence even under
NPU_INFER_HOST_GEMM=1, so #796's rung 1 is gated on the same artifact as rung 2 — that is a finding, and it reads
correctly as one in the body rather than as a missing feature.
|
Persistent review updated to latest commit 2ff8154 |
|
Persistent review updated to latest commit 4ab97b0 |
|
Persistent review updated to latest commit 141176f |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 141176f2f (head after #817 merged; tprs-ready already on)
141176f merges main 8273eb3 (#817). Third head, same content, re-checked at it:
- own diff is still the five files (
tools/safetensors_to_q4nx.py+175/−14,tools/npu_plan.py+7/−1,
tests/safetensors_to_q4nx_test.py+155,tests/npu_plan_generate_test.py+13/−3,docs/npu.md+24), and all five
are byte-identical to the head I reviewed (d540d8d); - re-ran the real conversion at this head:
pack→820971656 bytes, 320 tensors(153 visual skipped),verify→
320/320 tensors ok, worst q4_1rel rms 0.0905onmodel.layers.19.self_attn.v_proj.weight, plan → 24 layers
with the six full-attention layers atqkv N=5120; - CI on this head:
build,CodeQL, all fourAnalyzejobs andpr_agentsuccess (283 s),hw_gateskipped;
mergeable_state=clean.
(CodeQL reads neutral for a couple of minutes while its four Analyze jobs are still running, then flips to
success — worth knowing before anyone reads a neutral as a skip. It is success on this head.)
Unchanged from my first verdict: the engine-side load and the missing-ELF refusal are not verified by me (that is the
NPU lane, and the six-design table is the detail the kernel set will confirm).
…824) #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. Co-authored-by: bong-water-water-bong <[email protected]>
…s still answers (#796) (#833) The forward asserted the geometry->artifact mapping in host-GEMM mode too, so NPU_INFER_HOST_GEMM=1 refused to load any model whose kernel set had not been built yet: forward init failed: plan .../plan.json: this model's plan needs the QKV kernel "full_i8_QKV_K1024_N5120.elf" in ... (missing) That made engine #796's rung 1 ("loads and answers with no device") gated on the same six AIE designs as the device rung. Host GEMMs are plain float matmuls over the decoded weights: no ELF is opened on this path, so none is required. The artifact is still named when it is on disk; when it is not, the role logs the geometry and the missing name instead of failing. The device path keeps every check it had (file_exists + init_elf), and the ELF set itself is what tests/verify_design_elfs + tests/verify_full_elfs.sh check. Measured on the real vllm-sr/d3-lite pack converted by #819 (320 tensors, 782 MB, host 1024, 24 layers, 18 GDN layers), NPU_INFER_HOST_GEMM=1, one npu-hold ticket, no device opened: [forward] QKV M=128 K=1024 N=5120 (host-GEMM, no ELF: full_i8_QKV_K1024_N5120.elf) ... six designs, all "no ELF" ready in 2 s; /v1/systemone answers choice, noul and score; probabilities sum to 1.000000; a malformed sibling still 422s the whole request; 0 amdxdna errors This does not claim accuracy: the numbers are the engine's own host-GEMM answer over the converted weights, and parity against the pack's d3_runtime.py is not measured here. Co-authored-by: bong-water-water-bong <[email protected]>
Host rung of #796, engine-side half. The route (#653), the contract (#765) and the readout head (#763) are
already on
main; this is the pack those run on. No device row, and none claimed — see the last sectionfor the one artifact that is still missing.
What it does
tools/safetensors_to_q4nx.py pack <pack> <model dir> --arch qwen3_5(auto-detected from the pack'slanguage_model.*names, so plainpackworks) converts a Decision-3.0 pack's backbone. Three transformsare the whole point, because each one loads and computes garbage rather than failing when it is wrong —
each is named in the docstring and has a known-answer test:
linear_attn.A_loglinear_attn.ssm_a-exp(A_log), F32 — the forward evaluatesexpf(ssm_a * softplus(dt)), so the raw A_log decays backwardslinear_attn.conv1d.weight[CD,1,CK]linear_attn.ssm_conv1d.weight[CK,CD]— the forward indexesconv[j*CD + c](q4nx_forward.cpp:1802)linear_attn.in_proj_a/bssm_alpha_proj/ssm_beta_projdeq_pad,:1640). q4_1 where the FLM config asks Q8_0 — a deviationverifymeasures (rel rms 0.078–0.086) instead of hidingEverything else is a rename taken from
configs/q4nx/qwen3.5_0.8b.json'sq4nx_namecolumn (in_proj_qkv→
qkv_proj,in_proj_z→self_attn.gate_proj,out_proj→ssm_out_proj,dt_bias→ssm_dt.bias,language_model.*→model.*), andvisual.*is skipped because the forward runs the text path. Anunmapped
language_model.*tensor is a refusal naming the tensor, never a silent drop.Two bugs the engine found (both load failures, not opinions)
model_loadderivedvocab=254279680 hidden=0fromshape[0]/shape[1](model.c:890). bf16 tensors now keep the shape the source gave them.text_config, and the engine readsconfig.jsonat depth 1only (
q4nx_semantics.c'svalue_for_top), sohidden_sizestayed 0 and the layer count fell back toa default (
num_hidden_layers (28) does not match the 24 layers).text_config's keys are now lifted tothe top level;
text_configitself is left in place.A bug in my own plan generator, found by using it
tools/npu_plan.py's attn QKV N ignoredattn_output_gate, so P1's plan asked for a 1024×3072 QKVkernel on a container whose rows are 5120. The C side already doubles q under the gate
(
npu/forward/src/q4nx_pack.c:150); the generator now does too, with a row inctest npu_plan_generate.Measured (this box, host-side only: no device opened;
journalctl -k | grep -c "amdxdna.*ERROR"= 0 after every run)Real pack,
vllm-sr/d3-lite(1.7 GB, 473 tensors), at~/models/npu/d3-lite-src:packverifylayers.19.self_attn.v_proj [512,1024])ssm_a-exp(A_log), every value negativessm_conv1dnpu_plan.pyon the converted dirH=1024 NL=24 NH=8 NKV=2 HD=256 IM=3584 NV=248320, 18 GDN layers,attn_output_gate=1,derived 6 designsctest safetensors_to_q4nx npu_plan npu_plan_generate npu_sampling systemone_contract systemone_route decision_headtools/copyright.py --checkWhat this does not do, and why
The engine accepts the container and then refuses by name at the one thing the box has not got:
The forward asserts ELF existence even under
NPU_INFER_HOST_GEMM=1(q4nx_forward.cpp:874), so#796's rung 1 ("loads and answers, no device needed") is gated on the same artifact as rung 2. Measured,
not inferred: the refusal above came from a host-GEMM run of the converted pack.
The set is six designs for this geometry, not seven — GDN's
ssm_outreusesO:ssm_out)1bit-MONSTER-iso-build/.../xclbins)GU_K1024_N6144: same GEMM, different role name)#796's table has
QKV 1024×1536andO 1024×1024, which do not match the pack: its full attention hashead_dim 256, 8/2 heads andattn_output_gate: true, soq_projis[4096,1024](+ the gate) ando_projis[1024,2048]. The issue is being corrected in a comment, not left for the kernel build todiscover.
No accuracy row, no parity-vs-
d3_runtime.pyrow: both need the backbone to run, i.e. that set.Refs #796.