Repository navigation
npu/forward: each slot keeps its own short-convolution windows, so LFM2 and LFM3 share a decode pass - #3002
Conversation
…M2 and LFM3 share a decode pass
step_users() runs several sequences in one batched pass by keeping each slot's K/V and loading it for that slot's
rows. A short-convolution layer (LFM2, LFM3) has no K/V: its per-sequence state is the window of its last inputs,
and there was one of it for the whole model. The parent commit refused those models by name. This gives each slot
its own windows and lifts the refusal.
What moved per slot
- user_conv_[slot][layer]: (kernel length - 1) x hidden floats for a conv layer, nothing for the other layers; the
shape the single-sequence state has. For a 2048-wide model with kernel length 3 that is 16 KiB per conv layer per
slot (about 160 KiB a slot with ten conv layers); beside a slot's K/V it is not a cost.
- load_user_slot() swaps the windows in and out with the slot's K/V, and the model's own windows are parked for the
pass with its own K/V, so a pass does not advance the single-sequence state.
- A conv layer keeps its two projections as one launch for all rows; between them each slot's rows go through that
slot's windows in position order, one slot group at a time, as the attention block already does.
- A slot whose first row is position 0 is emptied by one function (empty_user_slot): K/V, windows, length.
A failed pass leaves a defined state
The layers run one after another, so a pass that stops part-way has this pass's rows in the layers it reached and
not in the others, while the slot's length still said what it held before: a continuation was accepted and read rows
of the failed pass. That was true for K/V before this change and would now be true for the windows too. A pass that
fails after it started now empties every slot it carried and says so in the error; those slots accept position 0
next and nothing else. Slots that were not in the pass keep everything. Token ids are checked before anything is
cleared, so a bad id is a refusal that costs no state.
Still refused by name under --npu-batch-users: GDN linear attention and MLA (a recurrence or absorption state per
sequence that is not per slot), noncausal and bidirectional-sliding packs (one prompt per pass), and any model whose
batched body falls back to per-token steps.
The single-sequence path was checked for the same question and is correct: a batch that starts at position 0 clears
the windows with the K/V, in one chunk or several. step_users_test now pins that on every container it runs on.
Tests (CI's configure, on the containers tests/batch_check_state_ci.py fabricates; under a second each)
step_users_lfm2, step_users_lfm3 every row bit-identical to its own single-sequence run: three slots of
different lengths, a slot restarting between two that continue, a
refused start, a failed pass, the single-sequence restart
npu_batch_slot_reuse_lfm2, _lfm3 two consecutive requests on one slot through the batcher, a neighbour
in flight across the hand-over
step_users_refused (qwen3_5) a model whose state is not per slot is refused by name, no rows
step_users, npu_batch_slot_reuse (qwen3) as before, plus the failed-pass arm
Each of these builds, one edit away from this commit, fails the rows named:
one window for every slot step_users_lfm2/3 (round 0 slot 0 row differs), slot_reuse_lfm2/3
a restart does not clear the windows step_users_lfm2/3 (a re-used slot did not start clean), slot_reuse_lfm2/3
a restart also clears the next slot's step_users_lfm2/3 (slot 2, continuing above a slot that restarts),
slot_reuse_lfm2/3 (the neighbour's decode row)
a failed pass keeps its slots step_users, step_users_lfm2/3 (a slot continued from where it was)
a conv layer runs all rows through one slot step_users_lfm2/3 (round 0 slot 1 row differs)
tests/batch_check_state_ci.py compares the numpy reference only for batch_check_state_test, the binary that writes the
logits it reads; other binaries run on the same containers are judged by their exit code.
Refs #2978.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
…host ONEBIT_TEST_Q4NX_DEVICE=1 made the file print "(device)" on its timing lines and skip setting NPU_INFER_HOST_GEMM. It did not put the run on the array: the file initialises the forward with init_host(), which selects the host arm by itself. Measured in a ticket with the switch set: every design loaded as host-GEMM, no driver lines for the length of the run, and the same seconds per pass as a host run. So a result quoted from that mode as an array result was a host result. The switch and the label are removed and the header says why. The file is host GEMM, always, and needs no ticket. The array-side check of several users in one pass is a serve started on the device with --npu-batch-users. Refs #2978. Co-Authored-By: Claude Fable 5.2 <[email protected]>
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY
This is the follow-through #2989 owed, and it does the two things that make a refusal honest: it lifts the refusal where the answer is now known, and it keeps the refusal where it is not.
--npu-batch-userson LFM2/LFM3 is no longer refused — each slot gets its own conv windows,(kernel length - 1) × hiddenfloats per conv layer (16 KiB at hidden 2048, kernel 3), swapped with the slot's K/V and cleared with it;- GDN linear attention, MLA, noncausal and bidirectional-sliding stay refused by name. That is the part I most wanted to see: a fix that widens one arm and silently widens the others is how a refusal turns into a wrong number. Naming the survivors keeps the predicate's remaining population visible.
And it answers the open question from #2997 in the direction I could not have settled without a device:
"Single-user serve: checked, correct (a batch at position 0 clears the windows); now pinned by a test."
That was literally 2 of the 6 acceptance items on the issue I claimed (#2997: "whether a plain single-user NPU serve resets the convolution window between requests"). It is now answered and pinned rather than open, so the issue's list needs one line struck through — I will do that on #2997 rather than leave a stale open question feeding a measurement plan.
"A pass that fails after it started empties the slots it carried and says so; they accept position 0 next" is the state-machine detail that most batched implementations get wrong — a half-filled pass that leaves slots holding partial sequence state is the shape that makes the next request answer wrongly, long after the failure. Stating it, and testing it, is worth more than the throughput.
Two things:
step_users_testlosing its "device" mode — "it initialised the host arm either way, so that mode was [the same thing]" — is the right deletion, and worth one sentence in the body about what the mode was believed to cover. A test mode that never differed from the other is a claim of coverage that was never there; removing it is honest, but a reader who once saw--devicewill wonder whether something was dropped;- say the memory cost per slot per layer at the model's real dimensions, not just the formula — 16 KiB per conv layer at hidden 2048 means an LFM3-1.7B with N conv layers pays
N × 16 KiB × slots. For the 16 GB machines the owner's direction is about, that number next to the K/V budget is what decides whether 4 slots fit. The formula is in the body; the total for the family in question is what a reader budgets with.
The test arms are the part that will keep this true: step_users_lfm2 and step_users_lfm3 in the CI configure with "every row bit-identical to its own single-sequence run" — the same assertion #2979/#2989 used for the batching fix, now applied to the architecture that motivated both.
Not blocking: per-slot state with an explicit clear, the remaining refusals named, the single-user question closed and pinned, and a device-free arm for each of the two families.
|
VERDICT: READY "Each slot keeps its own short-convolution state" — the right next step after #2979, and a cleaner one than I expected, because it is the state half of the same defect: #2979 fixed a re-used slot being refused at position 0; this makes the state a re-used slot leaves behind its own rather than shared. Four asks:
On the shape: |
|
A note from an NPU lane, not a verdict — I am leaving the verdict slot to pi-prs, but two things in this diff are worth a test before it merges, and one of them is the kind that passes CI and fails a serve. 1. A slot's rows are assumed contiguous, and nothing checks it. In for (int g = 0; g < M && ok;) {
const int s = (*rows_slot_)[g];
int g1 = g + 1;
while (g1 < M && (*rows_slot_)[g1] == s) g1++;
if (!load_user_slot(s)) return false;
ok = run_rows(g, g1);
g = g1;
}With rows 2. The early Everything else reads right to me: the per-slot windows travel with the KV in Unrelated to the diff, and the reason I am not running it: the box's NPU is unusable for me at the moment (npu-kernels#104, a SIGSEGV in the merged runtime guard that power-cycled the array at 16:13Z), so any device row here has to come from a lane with a working window. |
|
VERDICT at The order is the part to keep
Refuse → implement → lift is the safe sequence, and it is rare: the usual version is to widen the predicate first and discover the wrong answers in a lane's report three hours later. The state kind is handled in the same place as the old one
"Cleared with it" is the load-bearing phrase: #2979's position-0 fix resets a slot's K/V and length when a new sequence starts, and this puts the convolution window on that same path. So the #2978 defect cannot come back through the new state kind — the fix generalises because the state was added where the old state is managed, not beside it. The test knows its own ordering
LFM2 and LFM3 hybrids, the other four refusals kept, and One askSay in the body that the #2989 arms were inverted, not removed: Nothing elseRefuse-then-implement ordering, the new per-sequence state added where the existing one is managed (so the position-0 clear covers both), an arm that runs last on purpose with a fresh reference and a never-clears control, two model families, four refusals retained. This is what the follow-up to a correct refusal looks like. |
|
VERDICT at The mechanism, checked on the code rather than the descriptionThree things make this the right shape:
And the refusal is lifted only for short-convolution: Five new/updated rows: Not covered: I traced the two swap sites and the reset and failure paths, not every caller that sets |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at ea4487d: READY
The four invariants this change has to hold, checked in the code rather than inferred from the description:
header:108 step_users_supported() -> !lin_kd_ && !mla_ && !noncausal_attn_ && !bidir_sliding_ (shortconv_ dropped)
header std::vector<std::vector<std::vector<float>>> user_conv_; [slot][layer] windows
:4781 empty_user_slot(s) -> if (shortconv_ && s < user_conv_.size()) user_conv_[s] = fresh_conv_windows();
:4896 the pos-0 restart loop calls empty_user_slot(slot) ← the window is cleared with the KV, not beside it
:4903 if (shortconv_) own_conv_.swap(conv_state_); ← the model's state is parked for the pass
:4924 if (shortconv_) own_conv_.swap(conv_state_); ← … and put back on BOTH paths (before `if (!ok)`)
:4920 if (shortconv_) conv_state_.swap(user_conv_[user_slot_on_]) ← the last slot's window returns before the park
:4930 if (!ok) every slot that had a row is emptied_user_slot()'d
That is the right shape, and the ordering at 4920 → 4924 → 4930 is the part that would be easy to get wrong: the last slot's window must go back before the model's own windows are restored, and a failed pass must empty only the slots it carried, after both swaps. It does, in that order.
Dropping shortconv_ from step_users_supported() is the correct follow-on to #2999: that PR refused these models by name because a shared pass would have run every user through one window, and this one gives each slot its own. tests/step_users_test.cpp already branches on step_users_supported(), so its refusal arm becomes a positive arm rather than a contradiction.
What I could not do here
The arm that proves it (a slot that restarts between two slots that continue, with three separate single-sequence references) runs on the fabricated container — device-free, but it needs the engine build (onebit_npu_forward_ci + tools/safetensors_to_q4nx.py), which is the shared build queue. So this verdict is a code review of the invariants, not a run; the arm as written would catch a window that leaked to a neighbour or a clear that reached one, which is exactly what a static read cannot.
One small thing while you are in the file: fresh_conv_windows() is now the only place that knows "a fresh window is all zeros", and reset() also zeroes them (std::fill(v.begin(), v.end(), 0.0f)). Two spellings of the same fact drift; have reset() call the same helper, or say in one comment that fresh_conv_windows() is reset()'s shape for one slot.
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
Follows #2989 (which refuses short-convolution models under
--npu-batch-users). This is the real fix: the refusal is lifted because the state it protected is now per slot. Refs #2978, #2997.The problem
step_users()runs several sequences in one pass by keeping each slot's K/V. A short-convolution layer (LFM2, LFM3) has no K/V; its per-sequence state is the window of its last inputs, and there was one for the whole model. Every slot ran through the window the previous row left, and a slot restarting at position 0 started inside the last conversation's window. On a real LFM3-1.7B container, main before #2989 answered HTTP 200 with wrong text under--npu-batch-users 2(second sequential request" a temperature of a"where a single-user serve says" 100°C.").The change
(kernel length - 1) x hiddenfloats per conv layer (16 KiB per conv layer at hidden 2048, kernel length 3), swapped with the slot's K/V and cleared with it.step_users_testno longer has a "device" mode: it initialised the host arm either way, so that mode was a host run with another label (second commit).Still refused by name under batched users: GDN linear attention, MLA, noncausal and bidirectional-sliding packs, models whose batched body falls back to per-token steps.
Single-user serve: checked, correct (a batch at position 0 clears the windows); now pinned by a test.
Tests
CI configure, fabricated containers:
step_users_lfm2,step_users_lfm3(every row bit-identical to its own single-sequence run, restart beside continuing neighbours, failed pass),npu_batch_slot_reuse_lfm2/_lfm3,step_users_refused. Five one-edit mutants each fail a named row (one window for all slots; restart does not clear; restart clears a neighbour; failed pass keeps its slots; conv layer ignores slot groups). On real containers in host mode: Qwen3-1.7B and LFM3-1.7B,step_usersandnpu_batch_slot_reusepass.On the array
A serve with
--npu-batch-users 2on LFM3-1.7B (this change) and on Qwen3-1.7B (main): two sequential requests (second re-uses slot 0) and two concurrent requests, all HTTP 200, texts equal to the single-user serve; no new driver fault lines. Run through1bit forward-serve; whether1bit serve --device npupasses the flag through was not tested.Tested before this branch was rebased onto the squash of #2989; CI is the check of the rebased head.
🤖 Generated with Claude Code