Skip to content

NPU forward: fix cross-request KV leak, faster pack, batched MoE gate/up; GGUF to fused lane - #222

Merged
bong-water-water-bong merged 5 commits into
mainfrom
npu-forward-kv-leak-batch
Sep 29, 2026
Merged

bong-water-water-bong merged 5 commits into
mainfrom
npu-forward-kv-leak-batch

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Three NPU commits from an agent's local branch npu-glm-kb-fix on strixhalo, rebased onto main (a fourth was already in #209). The agent's A/B notes and duplicate diffs (evidence/holo4-1bit-capture/) are left out; their numbers are below.

Per-op forward (npu/forward)

  • Cross-request KV leak (correctness). The KV cache is append-only and attention indexes it by position, but nothing cleared it between requests. A second request read the first one's K/V. On MiniCPM5-1B, asking Japan then France gave " Tokyo" twice. step_batch now resets at start_pos == 0. After the fix: Tokyo / Paris / Berlin / Paris.
  • Pack: row-major blocked int8 pack, with bit-identical bytes and scales. OpenMP was never enabled for onebit_npu_forward; it is now (C and CXX). Pack time went 10.9 → 3.2 → 1.2 ms per call.
  • Batched MoE gate/up: one GEMM per projection over the top-k experts, for non-MLA MoE only. NPU_NO_EXPERT_BATCH=1 restores the per-expert path. npu_gemm calls per 6-token request drop from 6,650 to 3,300. Holo4-35B-A3B, 8 tokens: 221-225 s → 161-162 s. Anchors unchanged on Holo4, Qwen3-Coder-30B-A3B and Qwen3.6-35B-A3B.

GGUF → fused lane. With ONEBIT_NPU_LANE_DIR, serve --device npu packs a Qwen3 GGUF in the lane's own Q4NX layout and runs it on the fast lane. Anything else falls through to the per-op forward. gguf_to_q4nx.py also packs Qwen2.

These are the agent's measurements. I rebuilt the rebased branch on strixhalo (NPU host build, OpenMP found). I'll re-run the KV-leak anchor sequence myself once the NPU is free, before merging. Host code only: no kernels or designs.

🤖 Generated with Claude Code

1bit agent and others added 4 commits September 29, 2026 07:59
`1bit serve -m <gguf> --device npu` only ever repacked with repack_gguf.py
(the per-op forward's layout) and ran ~100+ synchronous dispatches per token.
ONEBIT_NPU_LANE_DIR now packs the GGUF with tools/gguf_to_q4nx.py (the lane's
q4_1 tile layout) into its own cache dir and serves it on the fast lane, one
launch per layer.

The two layouts are not interchangeable (evidence/gguf-npu-six/
task3-fusion-feasibility.md), so the lane pack never aliases the forward pack.
Anything the lane cannot serve -- an arch the lane-layout packer does not model,
a missing tokenizer, a missing kernel set -- falls through to the unchanged
per-op forward, so the route cannot regress a model the lane does not cover.

Captured Qwen3-0.6B: GGUF -> fused lane, anchor " Paris", 97-103 tok/s
(evidence/gguf-npu-six/fused-lane-gguf-2026-09-28.md); Qwen2.5-7B -> the per-op
forward (the lane/dx designs are Qwen3-shaped: 8 KV heads, per-head q/k RMSNorm,
no projection bias).

(cherry picked from commit 5fb4f4b47a597dcef69761d24e981e8ee4ad26d8)
The dx fused per-layer route reads this layout, and Qwen2.5-7B is the arch the
goal is proven on.  The tensor-name map already covered qwen2's blk.N.attn_q/
attn_k/attn_v (the same names qwen3 uses), and 1-D tensors are already written as
bf16, so the bias tensors land in the Q4NX as model.layers.N.self_attn.*_proj.bias
with no new code.  Only config() rejected the arch, and qwen2 additionally needs
`attention_bias` / `qk_norm` declared so a consumer does not have to sniff the
tensor table (dx's decoder checks them against its kernel build flags).

Verified: Qwen2.5-7B-Instruct Q4_K_M -> 5,509,628,488 B model.q4nx, 339 tensors,
config with attention_bias=true / qk_norm=false; and the run through the dx route
answers " Paris" (evidence/gguf-npu-six/dx-qwen2/).

(cherry picked from commit a6a134717bfcd7ae5caf0d3210d7b2a000309f66)
… batch MoE expert gate/up

(cherry picked from commit 54c31e82a1024bf36e1244f11155a814f490236e)
@context7

context7 Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Docs7 for 1bit-monster/engine

Result Status Action
Deployment ➖ Not used —
Content review ➖ Did not run. This site has no agent runs available this month. Wait for the monthly reset or check your Docs7 plan. —

Commit f9d7e64

@github-actions

Copy link
Copy Markdown

Preparing review...

@bong-water-water-bong
bong-water-water-bong marked this pull request as draft September 29, 2026 11:24
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Draft until verified. My first NPU check of this build gave wrong first tokens on MiniCPM5-1B (Japan/France/Germany/France -> ' a', ' the', ' the', ' the'), not the Tokyo/Paris/Berlin/Paris in the agent's notes. That run shared ~/.cache/1bit/q4nx with the agent's live runs, which rewrote the MiniCPM5 pack at the same time, so it may be a cache race and not the code. Re-running with a private ONEBIT_Q4NX_CACHE.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Private-cache re-runs: main and the agent's pre-#211 commit give the same wrong MiniCPM5 answers, so this PR did not cause them. Tracked in #224. Staying a draft until MiniCPM5 answers correctly again, since this PR's MiniCPM5 claims can't be checked until then.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Verified. #224 was a converter lookup problem, not code (fixed in #225). With the canonical converter (a7f29fe3f):

  • main: Japan -> ' Tokyo', then ' Tokyo' for France, Germany and France. That is the cross-request KV leak, reproduced.
  • this PR: Tokyo / Paris / Berlin / Paris.

Leaving the agent's later opt-in expert LRU commit (1be569bef) out: it measured as not paying, and it's off by default.

@bong-water-water-bong
bong-water-water-bong marked this pull request as ready for review September 29, 2026 13:03
@bong-water-water-bong
bong-water-water-bong merged commit 65d18fb into main Sep 29, 2026
10 of 11 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the npu-forward-kv-leak-batch branch September 29, 2026 13:08
@github-actions

Copy link
Copy Markdown

Preparing review...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant