Skip to content

npu/forward: each slot keeps its own short-convolution windows, so LFM2 and LFM3 share a decode pass - #3002

Merged
bong-water-water-bong merged 2 commits into
mainfrom
cl-2978/shortconv-per-slot
Nov 20, 2026
Merged

bong-water-water-bong merged 2 commits into
mainfrom
cl-2978/shortconv-per-slot

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

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

  • Each slot has its own windows: (kernel length - 1) x hidden floats 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.
  • A conv layer keeps one launch per projection for all rows; between them each slot's rows go through that slot's windows.
  • A pass that fails after it started empties the slots it carried and says so; they accept position 0 next and nothing else. Before, a continuation was accepted and read rows of the failed pass (true for K/V too). Token ids are checked before anything is cleared.
  • step_users_test no 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_users and npu_batch_slot_reuse pass.

On the array

A serve with --npu-batch-users 2 on 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 through 1bit forward-serve; whether 1bit serve --device npu passes 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

bong-water-water-bong and others added 2 commits November 20, 2026 12:48
…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 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

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-users on LFM2/LFM3 is no longer refused — each slot gets its own conv windows, (kernel length - 1) × hidden floats 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:

  1. step_users_test losing 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 --device will wonder whether something was dropped;
  2. 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.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY 3002

"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:

  1. the arm must be two slots with DIFFERENT short-conv state — a pass on slot 0, then slot 1, then slot 0 again, where a shared buffer would give slot 0 slot 1's state. If both slots carry the same tokens the test passes on the shared buffer, which is the HRX: symmetric-i4 consumer route re-enabled by AMD aadd6025 after f2099e9b removed it #450 lesson (four KV streams with identical content prove nothing);
  2. and the third pass must be checked against the first's result — the property is not "slot 0 still runs", it is "slot 0's second pass equals its first pass given the same input", because that is what "its own state" means operationally;
  3. say what the batch path does — tests/batch_check_state_ci.py is in the diff, and the npu/forward: position 0 on a re-used slot starts a new sequence, as the comment below it already promised (Fixes #2978) #2979 follow-up (npu: slot re-use gets the tests it lacked, and batched users refuse short-convolution models (follow-up to #2978) #2989) touched the same area: if NPU_BATCH_CHECK replays every row as one sequence (npu: step_users refuses a slot's non-consecutive positions, and NPU_BATCH_CHECK under it (refs #2689) #2738), say whether it can judge a multi-slot pass at all, or whether this arm must run outside it;
  4. docs/npu.md + CMakeLists.txt are in the diff — name the row and the page change in the body, since a registered row whose page still describes the old behaviour is the docs: stop counting the pre-PR commands, because the count was wrong #2766 class.

On the shape: +239/−54 across 7 files, behind 0, and the deleted lines are in the file the feature changes — that is what a clean branch looks like, and after an afternoon of reviewing 1475-line reverts it is worth saying so.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

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 conv_layer_batch:

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 [0, 1, 0] this loads slot 0, runs row 0, loads slot 1, runs row 1, then loads slot 0 again — which works, because each group re-loads its slot, so the grouping only has to be correct, not sorted. The question is whether the attention block on the same pass assumes the same ordering: rows_pos_/rows_slot_ feed rows_context with a per-row window start, so a non-adjacent repeat is fine there too — but if any pass-level code assumes "slot changes once", the convolution windows and the KV would disagree about which row belongs to which sequence. If step_users already validates or sorts slot, this is a non-issue and worth one comment saying so; if it does not, the test is slot = [0,1,0] with three tokens and each row compared against its own single-sequence run.

2. The early return false inside the layer loop skips the pass's parking discipline. if (!load_user_slot(s)) return false; leaves own_kc_/own_vc_/own_conv_ swapped out and rows_slot_ set. That is only safe if every caller treats a false return as "the pass ended, run the cleanup" — which your new "a failed pass leaves its slots empty" block implies, but the own_conv_ swap happens before the pass and is undone after it, so a caller that returns straight out of conv_layer_batch (rather than out of the pass) would leave the model's single-sequence windows parked in own_conv_ and conv_state_ holding a slot's. Worth a look at the one call site's control flow; a scope guard around the swaps would make it unconditional.

Everything else reads right to me: the per-slot windows travel with the KV in load_user_slot, are cleared by the one empty_user_slot, rows_slot_ == nullptr keeps the single-sequence path byte-identical, and the refusal for the other per-sequence states (GDN/MLA/noncausal) is correctly narrowed rather than dropped — with the docs and the startup message updated in the same PR, which is where most of this class of change goes wrong.

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.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at ea4487d: READY. It is the third step of a progression that was done in the right order, and the test knows something most suites do not.

The order is the part to keep

  1. npu: slot re-use gets the tests it lacked, and batched users refuse short-convolution models (follow-up to #2978) #2989 refused short-convolution models under --npu-batch-users, with a measured 2.6× wrong-logit as the evidence.
  2. This implements the per-slot window, so the refusal can be lifted.
  3. The predicate drops shortconv_ and keeps the other four.

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

"A short-convolution layer (LFM2, LFM3) has no KV; its per-sequence state is the window of its last inputs. Each slot keeps its own windows beside its KV, loaded and cleared with it, so those models share a pass too."

"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

// This arm runs LAST on purpose: it starts a new sequence at position 0 on a slot that already holds one, so …
// A re-used slot starts a NEW sequence at position 0: it must answer from an empty cache, not from the previous …
// … so the comparison above would pass on a build that never clears. A separate instance carries …
  • Last on purpose, because the arm deliberately leaves a slot in a restarted state and would contaminate any arm after it. Most suites do not think about test ordering at all; this one states why the order is part of the assertion.
  • A fresh reference for the re-used slot (FAIL fresh reference…, …a fresh run says %.9g) — the cold-run comparison that npu/forward: position 0 on a re-used slot starts a new sequence, as the comment below it already promised (Fixes #2978) #2979 established.
  • The never-clears control: a separate instance carrying the previous conversation, so the comparison fails on a build that does not clear. That is the arm that makes the positive assertion discriminating rather than decorative.

LFM2 and LFM3 hybrids, the other four refusals kept, and docs/npu.md updated to match.

One ask

Say in the body that the #2989 arms were inverted, not removed: step_users_lfm2 used to expect the refusal and now expects the same logits as a cold run. A test whose expectation flips is the one most easily deleted while "lifting a refusal", and one sentence — "the row that asserted 77 now asserts equality against a fresh instance" — makes the inversion reviewable instead of invisible.

Nothing else

Refuse-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.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT at ea4487d: READY — the per-slot state is real, it travels with the slot's KV by the same mechanism, and the reset path that #2978 was about now clears it. This is the fix that #2989's refusal was a placeholder for, and the pair reads correctly together.

The mechanism, checked on the code rather than the description

q4nx_forward.h   std::vector<std::vector<std::vector<float>>> user_conv_;   // [slot][layer], (taps-1)*hidden per conv layer
                 std::vector<std::vector<float>> own_conv_;                 // the model's windows, parked for a pass
                 void empty_user_slot(size_t s);                            // a slot that holds no sequence

q4nx_forward.cpp 4755   if (shortconv_) conv_state_.swap(user_conv_[slot]);   // loaded with the slot's KV
                 4761   if (shortconv_) conv_state_.swap(user_conv_[slot]);   // …and put back after
                 4781   user_conv_[s] = fresh_conv_windows();                 // emptied with the slot's KV

Three things make this the right shape:

  1. It reuses the existing per-slot discipline instead of inventing a parallel one. user_conv_ has the same [slot][layer] shape conv_state_ has, and it is swapped in the same place as user_kc_/user_vc_ — so a slot's windows and its KV cannot get out of step with each other.

  2. The reset path clears it, which is the property npu: step_users refuses position 0 on a re-used slot, so the 'start a new sequence' reset it documents is unreachable #2978 was actually about:

    if (pos[m] != 0) continue;
    empty_user_slot((size_t)slot[m]);   // K/V, the convolution windows of a short-convolution model, the length

    A slot that restarts at 0 now cannot read the previous conversation's window — by the same line that already cleared its KV.

  3. The parking is symmetric on every exit, including failure: own_conv_.swap(conv_state_) in, the in-flight slot's swap out, own_conv_ back — and the if (!ok) path empties every slot that had a row, which is the right call, because a pass that stopped part-way has advanced the windows in the layers it reached and not in the rest.

And the refusal is lifted only for short-convolution: step_users_supported() still excludes lin_kd_ and mla_, whose state genuinely is not per slot — and the tests pin that asymmetry (npu_batch_slot_reuse_lfm2/lfm3 for bit-identical rows, the qwen3_5 hybrid refused by name with no rows).

Five new/updated rows: step_users_lfm2, step_users_lfm3, npu_batch_slot_reuse_lfm2, npu_batch_slot_reuse_lfm3, all on batch_check_state_ci.py's fabricated containers with SKIP_RETURN_CODE 77/TIMEOUT 120, so they run in CI (numpy is pinned there) rather than skipping — which is the coverage lesson #2989's comment recorded.

Not covered: I traced the two swap sites and the reset and failure paths, not every caller that sets user_slot_on_; and there is no device here, so "bit-identical to its own single-sequence run" is the fabricated-container arms' claim, not mine. The docs/npu.md and forward_serve.cpp changes I read only as far as their consistency with the header.

@bong-water-water-bong
bong-water-water-bong merged commit 2b7d9da into main Nov 20, 2026
11 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-2978/shortconv-per-slot branch November 20, 2026 17:03

@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 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.

@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

2978 - Partially compliant

Compliant requirements:

  • Exempt pos[m] == 0 from the check in step_users() to allow re-used slots to start new sequences
  • The test suite now passes with the re-used-slot arm

Non-compliant requirements:

  • None

Requires further human verification:

  • None

2997 - Partially compliant

Compliant requirements:

  • The change allows LFM2 and LFM3 models to share convolution windows per slot, which is a key part of enabling the NPU route for these models
  • The convolution window is now reset per slot, which addresses the issue of incorrect rows being returned
  • The test suite now includes tests for LFM2 and LFM3 models to verify correct behavior

Non-compliant requirements:

  • The specific measurements (prefill and decode tok/s, first-token time, peak host memory) are not verified in the PR code diff
  • The detailed ops identification (which run on array vs host) is not included in the diff

Requires further human verification:

  • Verification of the specific performance measurements (tok/s, first-token time, peak memory)
  • Confirmation of which ops run on the array vs host
  • Final confirmation that the model works correctly on 16 GB machines
⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incorrect Position Check

The check for position 0 in step_users() was too strict and did not exempt slots starting at position 0, which should be allowed to start new sequences. This caused a re-used slot at position 0 to be refused with a message that described the case it was refusing, making the reset path unreachable.

bool multi = false;
Convolution State Management

The convolution state management for short-convolution layers was not properly handled in multi-slot passes. Each slot now maintains its own convolution windows, which is necessary to prevent incorrect state sharing between sequences.

    if (shortconv_ && (size_t)s >= user_conv_.size()) {
        err_ = "step_users: slot " + std::to_string(s) + " has no convolution windows prepared";
        return false;
    }
    if (user_slot_on_ == s) return true;
    if (user_slot_on_ >= 0) {   // put the loaded slot back in its own bank entry
        kcache_.swap(user_kc_[(size_t)user_slot_on_]);
        vcache_.swap(user_vc_[(size_t)user_slot_on_]);
        if (shortconv_) conv_state_.swap(user_conv_[(size_t)user_slot_on_]);
    }
    kcache_.swap(user_kc_[(size_t)s]);
    vcache_.swap(user_vc_[(size_t)s]);
    // A short-convolution layer's window is this sequence's as much as its K/V are, so it is loaded with them: the
    // layer reads and advances conv_state_, which is now this slot's.
    if (shortconv_) conv_state_.swap(user_conv_[(size_t)s]);
    user_slot_on_ = s;
    return true;
}
Test Coverage for Failed Passes

The test suite now includes checks for failed passes that leave slots empty, ensuring that slots are properly reset when a pass fails partway through, which prevents incorrect continuation of sequences.

    // A PASS THAT FAILS AFTER IT STARTED leaves the slots it carried empty, and says so. The layers it reached hold
    // this pass's rows and the others do not, so continuing would read rows of a pass that never completed; the
    // slot's old length must not survive it. NPU_NO_BATCH makes the batched body refuse a step_users pass after the
    // entry checks, which is the cheapest failure there is that happens inside the pass. Slot 2 is in the pass;
    // slot 1 is not and must keep everything.
    {
        const int next2 = pos2 + 1;   // where slot 2's sequence ends now
        const int next1 = int(third.size());
        setenv("NPU_NO_BATCH", "1", 1);
        const bool ran = fwd.step_users({27}, {next2}, {2}, row);
        const std::string said = fwd.last_error();
        unsetenv("NPU_NO_BATCH");
        if (ran) {
            std::printf("FAIL a pass ran with NPU_NO_BATCH set, so this arm has no failing pass to look at\n");
            return 1;
        }
        if (said.find("emptied") == std::string::npos) {
            std::printf("FAIL a failed pass does not say what it did to its slots: %s\n", said.c_str());
            return 1;
        }
        if (fwd.step_users({27}, {next2}, {2}, row)) {
            std::printf("FAIL a slot continued from where it was before a pass that failed\n");
            return 1;
        }
        if (std::string(fwd.last_error()).find("holds 0 position(s)") == std::string::npos) {
            std::printf("FAIL the slot of a failed pass is not empty: %s\n", fwd.last_error());
            return 1;
        }
        // The slot starts again at 0 and answers as a fresh run does ...
        std::vector<float> all2;
        if (!fwd.step_users(third, {0, 1, 2}, {2, 2, 2}, all2) || all2.size() != third.size() * width) {
            std::printf("FAIL the slot of a failed pass did not start again at position 0: %s\n", fwd.last_error());
            return 1;
        }
        const std::vector<float> got2(all2.end() - (long)width, all2.end());
        int at2 = first_difference(got2, want1);   // want1 is a fresh run of `third`, from above
        if (at2 >= 0) {
            std::printf("FAIL the slot of a failed pass did not start clean: logit %d is %.9g, a fresh run says %.9g\n",
                        at2, got2[(size_t)at2], want1[(size_t)at2]);
            return 1;
        }
        // ... and the slot that was not in the failed pass continues its own sequence.
        std::vector<float> want_n;
        if (!fresh1.step(34, next1, want_n) || !fwd.step_users({34}, {next1}, {1}, row)) {
            std::printf("FAIL a slot outside the failed pass: %s / %s\n", fresh1.last_error(), fwd.last_error());
            return 1;
        }
        at2 = first_difference(row, want_n);
        if (at2 >= 0) {
            std::printf("FAIL a failed pass changed a slot it did not carry: logit %d is %.9g, its own run says %.9g\n",
                        at2, row[(size_t)at2], want_n[(size_t)at2]);
            return 1;
        }
    }
}

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant