Repository navigation
npu: cache the dense FFN tail for GDN models by default (#587) - #637
Conversation
`step()`'s `dense_weights_cached` guard can never fire for a hybrid GDN model: it requires `lin_kd_ == 0`, and `lin_kd_` is model-level, so a qwen3_5 model re-dequantised each layer's 17408-wide dense FFN tail on every token even after those weights were packed on the device. A measured 5-round interleaved A/B on Qwen3.8-27B-NPU2 shows the guard line forced to `false` and left alone are the same code path (117.403 vs 117.443 s for an 8-token request, paired delta -0.040 +/- 0.102 s), while skipping the reload through `ffn_tail_cached()` cuts the same request to 104.524 s (-11.0%) and CPU per token from 63.30 to 22.85 s (-64%), with identical text (~/lb/587-cache-20261030). So the cache becomes the default where it is safe and worth it rather than opt-in: a GDN model on the NPU route, the packed-weight cache on, not a host-GEMM run, and a dense tail of at least 256 MB per layer. `ffn_cache_default()` is that rule, `ffn_cache_` is tri-state (-1 auto, 0/1 forced by NPU_FFN_CACHE as before), the resolved state is logged once as `[ffn] dense-tail cache on|off (default|NPU_FFN_CACHE; ...)`, and docs/npu.md gains a subsection with the numbers and the two env knobs.
|
Proof status, with the failure stated rather than hidden. Attempt 1 deadlocked, my bug. The driver held an exclusive Attempt 2 is running with the lock discipline inverted: the driver only probes the lock Arms and the self-check: I will post the numbers here and mark this ready when a window lands. |
|
Proof is in — the default does what the PR says, measured on the device. Run Each arm logged the state its name claims — this is asserted by the driver, not eyeballed:
Caveat, stated rather than smoothed: absolute seconds are not comparable with the 03:05 UTC A/B Known limit, unchanged: the below-threshold branch of |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY (posted as a comment: GitHub refuses --request-changes/--approve on a PR owned by the same account).
Built it, and re-derived the proof from the artifacts rather than from the table.
Build: -DONEBIT_NPU=ON -DONEBIT_NPU_RT_DIR=<the private runtime>, gcc-16, Release, under ~/lb/thermal-run.sh at e972701 — 37/37, 1bit links (3.7 MB, forward-serve present in --help), and the only five warnings in the whole build are the pre-existing nlohmann is_trivial deprecation; none from q4nx_forward.cpp. The new dense-tail cache log string is in the binary.
The proof numbers are exactly ~/lb/587-default-20261030/run-20261030T110721Z/results.tsv:
round arm wall_s ncpu_s cpu_s_per_tok ffn
1 default 153.795 579.42 72.428 on (default; lin_kd=2048 IM=17408 H=5120)
1 forced-off 150.547 892.70 111.588 off (NPU_FFN_CACHE; ...)
1 forced-on 138.752 561.71 70.214 on (NPU_FFN_CACHE; ...)
2 forced-off 152.675 838.72 104.840 off (...)
2 forced-on 135.623 548.20 68.525 on (...)
2 default 137.662 548.98 68.623 on (default; ...)
so 579.42/548.98, 561.71/548.20, 892.70/838.72 s CPU and 153.80/137.66, 138.75/135.62, 150.55/152.68 s wall are the measured values, void=0 in all six rows, text is 4, 5, 6 in all six, and no arm's logged state contradicts its name. Re-derived: default vs forced-on is +3.15 % (r1) and +0.14 % (r2); forced-off is +59.0 % / +53.0 % CPU against the forced-on arms (+331 s / +290 s), and 8.5 % / 12.6 % wall. All as stated.
The binary is a build of this change: driver.log records sha256=0c1cbf9425f2… for /home/bcloud/wt/w-587-def-build/1bit, that file still hashes to the same value, and it carries the new dense-tail cache string that only exists with this PR. The 03:05Z A/B behind docs/npu.md also checks out as stated, as means not bests: ~/lb/587-cache-20261030/run-20261030T030506Z 8-token rows, 5 rounds each — tail-cached 104.52 s / 22.85 s per token against 117.40 and 117.44 s / 63.30 and 63.26 s, i.e. −11 % and −64 %, which is where "104.5 / 117.4 s, 22.9 / 63.3 s" comes from.
The correctness argument holds by reading, and this is the part that matters for a default-on skip. wg_, wu_, wd_ are read only through q4nx_assemble_B() (the p.gate = wg_.data() assignments are pointer setup; mlp() and moe_mlp() never touch them). step() gates those assembles per key (2, then 3 for a split GU, then 4) — exactly the set ffn_tail_cached() requires, including the fused-GU case where key 3 does not exist, so the guard cannot be silently dead. And the load-time guard can never leave a stale tail where an assemble still runs: at a layer's first forward nothing is packed yet, so ffn_tail_cached() is false and the dequant happens; afterwards keys 0 (attention) is packed before 2/3/4 in the same layer, so any later state where 2/3/4 are cached also has step_batch()'s key-0 gate closed. Host-GEMM (NPU_INFER_HOST_GEMM) and NPU_NO_WCACHE both resolve the cache off, so the paths that really need the f32 weights keep them.
I could not run the device myself: the owner's Bonsai serve holds /tmp/1bit-npu-device.lock, and the lane rules put that job first, so the device numbers above are yours (recorded binary hash, per-request pid/port-owner checks, and the [ffn] state line in every arm are what make them checkable — thank you for that).
Two nits, neither blocking:
NPU_FFN_CACHEis parsed asatoi(v) != 0, soNPU_FFN_CACHE=on(ortrue,yes) resolves to off — the opposite of what the operator just wrote, and the opposite of every other presence-based switch in this file (NPU_NO_WCACHE,NPU_INFER_HOST_GEMM). Either document "=0 or =1 only, anything else is off" indocs/npu.md, or treat a non-numeric value as on.ffn_tail_cached()is weaker thanstep_batch()'s gate: that function uses onecached = weight_cached(0)for the whole layer (attention and MLP assembles), whilestep()gates per key as this guard does. It is consistent today for the reason above, but the invariant "key 0 cached implies 2/3/4 cached" is now load-bearing across two functions and lives only in this PR's reasoning — one line inffn_tail_cached()saying so would stop the next reader from "tightening" either guard.
Also recording the author's stated limit as mine: the below-256 MB branch of ffn_cache_default() is unexercised (no small GDN model on this box), and it is the branch that would decide the default on a different GDN model.
…the device already holds Decode on the 27B container was 13.3 s/token on the shipping build and 11.5 s/token with the M=16 twins passed in by hand; it is 8.87 s/token out of the box with this, and a 1-token request drops from 26.5 s to 9.2 s (the serve half is the next commit). The dense-FFN-tail cache that used to be part of this commit landed upstream as #637, so this is what is left on top of it: 1. The M=16 decode twins are found without an extra flag. The loader looks for full_i8_<ROLE>_K<K>_N<N>_m16.elf in <model dir>/kernels-m16, then <kernels>/m16, then <kernels>, so a model directory can ship its own decode family and a plain `--kernels <model>/kernels` serve runs M=16. A role with no twin still falls back to M=128, so a kernel dir without twins behaves exactly as before. Until now the twins were opt-in through NPU_DECODE_M16 plus a hand-built --kernels directory, which is why the shipped serve ran m16=0 and paid the 1.16x. (Twins are bit-exact against the M=128 kernels: NPU_M16_CHECK=1, mismatched=0, worst int32 maxdiff=0.) 2. ctx_for() gives the twin to any pass with <=16 rows, not just rows==1, so a short prompt's prefill chunk also stops streaming 128 MAC rows for <=16 useful ones. Chunks wider than 16 keep M=128, so long prefill is unchanged. 3. lm_head is packed once into resident weight BOs (same packer, same padded KD x ND BO, same per-column scales, so the packed bytes are bit-identical) instead of re-gathering 5.09 GB of f32 and re-running packB on all 15 tiles every token; lm_head_f_ / lm_head_tiles_ are released afterwards, 10.4 GB of RSS returned. Measured 1251 ms -> 419 ms for the head on the H2 container (-0.83 s/token), RSS after load 60 GB -> 18 GB. Also carries the env-gated NPU_PHASE_PROF attribution used to find (3) and the earlier host work: one [ph] line per decode step whose named phases sum to its wall time, plus the ranked layer-loop host call sites. Nothing in the compute path reads it and it is free when the variable is unset. It also carries m16_check_gemm_rows(), the multi-row half of the NPU_M16_CHECK gate, without which change (2) would be outside the bit-exact gate. Measured, decode step, 368 launches, 7 roles all at M=16: wall 8.87 s (layers 8.44 = wait 7.60 + host 0.61 + quantise/launch 0.22, head 0.42). Greedy text identical on 9 requests.
…en re-dequant) (#661) * npu: decode at M=16 from the model dir, and stop per-token host work the device already holds Decode on the 27B container was 13.3 s/token on the shipping build and 11.5 s/token with the M=16 twins passed in by hand; it is 8.87 s/token out of the box with this, and a 1-token request drops from 26.5 s to 9.2 s (the serve half is the next commit). The dense-FFN-tail cache that used to be part of this commit landed upstream as #637, so this is what is left on top of it: 1. The M=16 decode twins are found without an extra flag. The loader looks for full_i8_<ROLE>_K<K>_N<N>_m16.elf in <model dir>/kernels-m16, then <kernels>/m16, then <kernels>, so a model directory can ship its own decode family and a plain `--kernels <model>/kernels` serve runs M=16. A role with no twin still falls back to M=128, so a kernel dir without twins behaves exactly as before. Until now the twins were opt-in through NPU_DECODE_M16 plus a hand-built --kernels directory, which is why the shipped serve ran m16=0 and paid the 1.16x. (Twins are bit-exact against the M=128 kernels: NPU_M16_CHECK=1, mismatched=0, worst int32 maxdiff=0.) 2. ctx_for() gives the twin to any pass with <=16 rows, not just rows==1, so a short prompt's prefill chunk also stops streaming 128 MAC rows for <=16 useful ones. Chunks wider than 16 keep M=128, so long prefill is unchanged. 3. lm_head is packed once into resident weight BOs (same packer, same padded KD x ND BO, same per-column scales, so the packed bytes are bit-identical) instead of re-gathering 5.09 GB of f32 and re-running packB on all 15 tiles every token; lm_head_f_ / lm_head_tiles_ are released afterwards, 10.4 GB of RSS returned. Measured 1251 ms -> 419 ms for the head on the H2 container (-0.83 s/token), RSS after load 60 GB -> 18 GB. Also carries the env-gated NPU_PHASE_PROF attribution used to find (3) and the earlier host work: one [ph] line per decode step whose named phases sum to its wall time, plus the ranked layer-loop host call sites. Nothing in the compute path reads it and it is free when the variable is unset. It also carries m16_check_gemm_rows(), the multi-row half of the NPU_M16_CHECK gate, without which change (2) would be outside the bit-exact gate. Measured, decode step, 368 launches, 7 roles all at M=16: wall 8.87 s (layers 8.44 = wait 7.60 + host 0.61 + quantise/launch 0.22, head 0.42). Greedy text identical on 9 requests. * serve: do not spend a decode step on logits nobody reads The generate loop sampled the token from the current logits, emitted it, and then always ran one more forward to produce the NEXT position's logits -- even when that token was the last one the request could produce. On the 27B that step is a whole weight stream (8.9 s), so max_tokens=1 cost two full passes for one token: 26.5 s -> 9.2 s wall, and n=4 66.5 s -> 36.1 s. Skipping it is behaviour-preserving: the loop can only end after this token if it was the limit-th one, and the KV/recurrent state is reset per request. * npu: drop the NPU_KERNELS_M16 override (cpp/path-injection), one name for twins CodeQL's cpp/path-injection on the new kernel-dir discovery is right: taking a directory out of the environment and fopen()ing <it>/<elf> is user input reaching a file access, the same shape as #640's ONEBIT_NPU_DEVICE_LOCK alert. The override buys nothing the two derived candidates do not already give: <model dir>/kernels-m16 and <kernels>/m16 both come from the -m and --kernels paths the serve was already started with, so a model directory still ships its own decode family and a plain `--kernels <model>/kernels` serve still runs M=16. Search order is now: <model dir>/kernels-m16, <kernels>/m16, <kernels>. Also removes the second way of spelling a decode twin's artifact name: set_elf_name() keeps its original un-suffixed form (every caller is a prefill design) and q4nx_derive_decode_designs() remains the only place that inserts "_m16". Verified after the change: 7/7 roles load at M=16 from <model dir>/kernels-m16 with no environment variable, n=1 9.23 s / n=4 35.42 s on the H2 container, greedy text unchanged (' Paris', ' Paris.\nThe'). --------- Co-authored-by: bong-water-water-bong <[email protected]>
What this does
step()'sdense_weights_cachedguard cannot fire for a hybrid GDN model — it requireslin_kd_ == 0, andlin_kd_ = lin_nkh_ * lin_khd_is model-level — so aqwen3_5modelre-dequantised each layer's 17408-wide dense FFN tail on every token, even after those weights
were packed on the device.
ffn_tail_cached()closes that hole (the three f32wg_/wu_/wd_buffers are read only by
q4nx_assemble_B(), which the MLP sections skip once the layer's packedweights are resident).
This PR lands that mechanism and makes it the default for GDN models on the NPU route, with a
size/footprint guard, instead of leaving it as an env-only knob.
Measured, not guessed
~/lb/587-cache-20261030/run-20261030T030506Z(driver +results.tsv+ logs), Qwen3.8-27B-NPU2 onstrixhalo, 5 rounds x 3 arms interleaved, 0 void rows, arm order rotated, 8-token request:
falseffn_tail_cached()onfalsevs shipped: paired Δ −0.040 ± 0.102 s — identical because they are thesame branch (
lin_kd_ == 0short-circuits tofalseon this model). The guard line is dead codefor GDN; it stays because it is still right for dense models.
generated text (
4, 5, 6) in every arm, and no change in residency (MemAvailable 76.3–76.8 Gafter, every arm).
The guard
ffn_cache_is tri-state:-1auto (new default),0/1forced byNPU_FFN_CACHE=0|1as before.ffn_cache_default()enables the cache when all of:lin_kd_ > 0— a GDN/hybrid model, the only case thestep()guard cannot cover;weight_cache_— the packed-weight cache is on, so the tail can actually become resident;host_gemm_— a host-GEMM run never packs, and keeps its legacy behaviour;3 * IM * H * 4) — 1.07 GB/layer on the 27B, so a small GDN modelkeeps the old behaviour rather than taking an unasked behaviour change for no gain.
ffn_tail_cached()still requires each layer's packed BOs to be present (weight_cached(2),weight_cached(4), plus3on a split gate/up design), so the first use of every layer stayscorrect. The resolved state is logged once at init:
so a measurement can always tell "off" from "never asked".
Proof status
Draft PR: the confirmation run (
~/lb/587-default-20261030/drv.sh, three interleaved arms —default/NPU_FFN_CACHE=0/NPU_FFN_CACHE=1, same driver shape as the A/B, plus a self-checkthat each arm logs the state its name claims) is queued on
/tmp/1bit-npu-device.lock, which theowner's Bonsai job holds. It gates with
flock -nand waits; it never evicts the owner. Expected:default≡NPU_FFN_CACHE=1≈ 104.5 s,NPU_FFN_CACHE=0≈ 117.4 s. I will post the numbers here andmark the PR ready before the orchestrator merges.
Known limit, stated rather than hidden: the below-threshold branch of
ffn_cache_default()isunexercised — there is no small GDN model on the box to run it against.
Files
npu/forward/src/q4nx_forward.cpp— the tail cache, its guard, the resolved-state lognpu/forward/include/q4nx_forward.h—ffn_tail_cached()/ffn_cache_default(), tri-state flagdocs/npu.md— "The dense FFN tail cache (hybrid GDN models)"