Skip to content

npu: a noncausal pack refuses step_batch_all() too, so no draft path can verify it causally (Fixes #847) - #1282

Merged
bong-water-water-bong merged 14 commits into
mainfrom
1bit/npu-noncausal-batchall
Nov 6, 2026
Merged

bong-water-water-bong merged 14 commits into
mainfrom
1bit/npu-noncausal-batchall

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

What this changes

Q4nxNpuForward::step_batch_all() is the batched pass that returns every row's logits — the one a
speculative decoder compares drafted tokens against. step_batch() already refuses a noncausal pack when a
batch starts above position 0, and step() refuses it outright, but step_batch_all() forwarded straight into
step_batch() with start_pos == 0, so on a pack with attention.mode: noncausal_full_attention it would
happily return M rows of logits from a pass in which every row attended to the rows after it. Those are not
prefix-conditioned logits, so no draft may be checked against them.

Three lines of refusal, worded like the other two (the mode's name, what is wrong, and what to use instead),
plus the contract in the header comment and in docs/npu.md. docs/npu.md also still listed the noncausal mode
itself as not implemented — stale since #861 — so that paragraph now says what is true: the mode is
implemented and measured, and what is missing is an accuracy row against upstream (#796's third rung).

Nothing else moves: the refusal returns before any state is touched, it is unreachable while a d3 pack ships
no draft head (none does), and the prompt-encoding path is untouched — the regression row below is what says so.

Measured

row result
build ninja 1bit with -DONEBIT_NPU=ON links (binary sha256 c6a38f4e…); tools/copyright.py --check rc 0
regression, the row that says the change is inert both mask arms on d3-lite-bf16 + its causal hardlink, host-GEMM, 20/20 answers byte-identical to the pre-change arms (worst |Δ| 0), one npu-hold ticket (108 s wait, 305 s held — inside the c31 10-minute cap), 0 amdxdna errors, no device opened
arm vs arm (unchanged by this PR) engine causal vs ref causal 0.0061; engine noncausal vs ref noncausal 0.0044; engine mask effect 0.2837, reference 0.2869
generation against the noncausal pack HTTP 500, attention.mode noncausal_full_attention is a prompt-encoding mode and step() generates: … — the designed refusal, re-measured on this build
the refusal itself not exercisable today: a d3 pack ships no draft head, so step_batch_all() is unreachable on a noncausal pack. The proof is the compile plus the rows above showing the reachable path is untouched; there is no path to hang a device test on until a draft head exists.

Closes #847

#847's acceptance, row by row, all measured on current main with the engine and the reference on the same
weights (host-GEMM, no device in the loop):

#847 acceptance item result
mask is a plan field, derived from the pack, unknown modes refused in main since #861 (attention.mode, optional, default causal; tools/npu_plan.py reads decision_config.json; npu_plan_test/npu_plan_generate_test pin parse + refusal)
bidirectional mask on full_attention layers, GDN recurrence untouched in main (step_batch() fills K/V for every row first; step()/step_batch(start_pos>0)/now step_batch_all() refuse by name)
noncausal vs causal differs by at least the reference's 0.2869 engine 0.2837, reference 0.2869, same case (score_three_levels); per-case table below
a causal model byte-identical to before causal arm vs the 2 Nov artifact: worst |Δp| 8.3e-7, choices 4/4
generation refused by name HTTP 500 with the mode's name (re-measured above)

The threshold row, per case rather than on the two worst-case scalars (~/lb/847-main/mask_effect_percase.py; the band is the two arms' parity added, because an effect is a difference of two measured quantities):

case ref mask engine mask Δ(mask) parity causal parity noncausal band
choice_two_one_null 0.0458 0.0446 −0.0013 0.0031 0.0019 0.0050
choice_three_json_desc 0.0377 0.0423 +0.0047 0.0034 0.0043 0.0077
choice_unicode 0.0175 0.0152 −0.0023 0.0038 0.0015 0.0053
choice_instructions 0.2587 0.2626 +0.0039 0.0061 0.0022 0.0083
noul_plain 0.0574 0.0610 +0.0036 0.0032 0.0004 0.0036
noul_criteria 0.0004 0.0041 +0.0037 0.0025 0.0021 0.0046
noul_instructions 0.0685 0.0638 −0.0047 0.0003 0.0044 0.0047
score_three_levels 0.2869 0.2837 −0.0032 0.0056 0.0014 0.0070
score_json_levels 0.0692 0.0726 +0.0034 0.0050 0.0020 0.0070

Every case's mask-effect delta is inside its own propagated band, and the case that sets the acceptance number is the one where the engine is 0.0032 short while its own causal-arm disagreement with the reference is 0.0056 — the shortfall is the forward's parity, not a smaller mask.

refs #847 (the hardening) — the acceptance rows above are what close the issue, and step_batch_all() was the
one path in it that could still answer a noncausal pack silently. If you read the 0.0032 against the literal
"at least 0.2869" as short rather than as inside the parity band, say so on the issue and I will produce a
same-weights reference comparison instead of fitting the mask to a number.

…can verify it causally (#847)

step_batch_all() is the batched pass that returns every row's logits for a speculative decoder to check
drafted tokens against. step() refuses a noncausal pack and step_batch() refuses a batch that starts above
position 0, but step_batch_all() forwarded into step_batch() at start_pos 0, where a noncausal pass has every
row attending to the rows after it -- so its per-row logits are not prefix-conditioned and no draft may be
checked against them. Refused by name, same wording family as the other two; header and docs/npu.md carry the
contract. docs/npu.md also still listed the mode itself as not implemented (stale since #861).

Unreachable while a d3 pack ships no draft head; measured inert on the path it cannot reach: both mask arms,
host-GEMM, 20/20 answers byte-identical to the pre-change arms, arm-vs-arm numbers unchanged.
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

#1282 ready for review at 7caaafc5 — measured rows in the body: the change is inert on the reachable path (20/20 answers byte-identical to the pre-change arms, host-GEMM, 0 amdxdna errors, one ticket 305 s), and #847's acceptance table is there per case, with the 0.2837-vs-0.2869 reading stated as inside the propagated parity band rather than as a fitted number.

@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 at 34de5656 — I compiled the changed translation unit at this head with this box's g++-16 against the real headers (rc 0), and the refusal is placed where a refusal has to be; the two doc claims re-read. Runtime behaviour is unexercised and I say why below.

Compile check of the change itself (npu/forward/src/q4nx_forward.cpp, the only C++ touched):

g++-16 -std=c++20 -fsyntax-only -DONEBIT_NPU_RUNTIME_OWN -Inpu -Inpu/forward/include -Inpu/forward \
       -Inpu/target -Inpu/engine-src -I<nlohmann>  npu/forward/src/q4nx_forward.cpp      rc 0

That is the same TU that carries noncausal_attn_ (:534, set from plan_.noncausal()), so the member, the err_ convention and the includes are all real, not assumed.

The refusal is correctly shaped, checked against its siblings on the same file:

  • it is the first statement in step_batch_all(), before batch_all_out_ is swapped or any KV work happens, so nothing is mutated on the refused path;
  • it fails the way step() and step_batch(start_pos > 0) do — err_ = "<named reason>", return false — and the message names the mode, says why (a noncausal batch's rows attend forward, so the per-row logits are not prefix-conditioned) and names the escape (step_batch() with the whole prompt at position 0). A caller that prints err_ learns what to do, which is the house rule in this file.

Docs: the new bullet says what the code does; the second hunk retires an already-stale statement — "Also not implemented: … the pack's noncausal_full_attention attention mode" — which engine #861 made false on 2026-11-03 (gh issue view 861: MERGED 2026-11-03T01:59:34Z, "npu: attention.mode, so a noncausal_full_attention pack (d3-lite) is n…"). Correcting it to "is implemented; what is not measured is accuracy against upstream (engine#796's third rung)" is right, and it is the more useful sentence for the next reader.

What I did not do, and it is the interesting half: run it. There is no draft head on any d3 pack, so the branch is unreachable end to end by the PR's own account, and demonstrating it would need a d3 pack plus an NPU window — tonight's c31 hold excludes that for a six-line refusal. I also note there is no test arm for this refusal: tests/npu_plan_generate_test.py covers the plan-level ones (an unknown attention_mode is refused, not defaulted, and that a noncausal pack's decision_config.json sets the mode), but the runtime refusals of step()/step_batch() have no arm either, so this PR is consistent with the neighbours rather than weaker than them. If a draft head ever lands on a d3 pack, that is the moment the arm is worth writing — and the code comment says exactly that ("unreachable today … refused now so a pack that grows one cannot be silently verified through a mask it was not trained with").

@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 at 7de315b9 — main merges only; the reviewed content is byte-identical to the head I verdicted.

Checked rather than assumed: the PR's own added lines (own diff vs merge-base, which excludes everything main brought in) hash to d5111afe4941 at the head I reviewed and to the same value here, so no line of this PR's change moved — only its base did (the branch now sits on newer main).

Everything in my review at the previous head therefore still applies unchanged, including the numbers I re-derived there and the nits I named.

@bong-water-water-bong
bong-water-water-bong merged commit 598b837 into main Nov 6, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/npu-noncausal-batchall branch November 6, 2026 21:27
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

796 - Partially compliant

Compliant requirements:

  • The PR addresses the noncausal attention mode issue, which is part of the host rung requirements
  • The PR adds refusal logic for step_batch_all() when attention.mode is noncausal_full_attention
  • The PR updates documentation to reflect that noncausal mode is implemented

Non-compliant requirements:

  • The converter handling of 235 non-2-D tensors is not addressed in this PR
  • The name map implementation using configs/q4nx/qwen3.5_0.8b.json is not addressed in this PR
  • The three transforms (A_log → ssm_a, conv1d transpose, alpha/beta Q8_0) are not addressed in this PR
  • The ELF set for 0.8B-GDN geometry with 7 designs is not addressed in this PR

Requires further human verification:

  • Verification that the pack converts correctly with the new refusal logic
  • Verification that the pack loads and answers correctly under NPU_INFER_HOST_GEMM=1
  • Verification that the numeric parity against d3_runtime.py is maintained

847 - Partially compliant

Compliant requirements:

  • The PR adds refusal logic for step_batch_all() when attention.mode is noncausal_full_attention
  • The PR updates documentation to reflect that noncausal mode is implemented

Non-compliant requirements:

  • The full-attention path honouring the noncausal mode for prompt-only forward is not addressed in this PR
  • The test for mask verification is not addressed in this PR

Requires further human verification:

  • Verification that the full-attention path correctly honours the noncausal mode for prompt-only forward
  • Verification of the test that ensures mask differences are correctly handled
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Noncausal Attention Mode Refusal

The PR adds refusal logic for step_batch_all() when attention.mode is noncausal_full_attention. This prevents the function from returning logits that are not prefix-conditioned, which are needed for draft verification. The refusal is properly worded to match the style of other refusal messages in the codebase.

if (noncausal_attn_) {
    err_ = "attention.mode noncausal_full_attention is a prompt-encoding mode and step_batch_all() "
           "verifies drafted tokens: every row of that batch attends to the rows after it, so its "
           "per-row logits are not the prefix-conditioned ones a draft is checked against (use "
           "step_batch() with the whole prompt at position 0)";
    return false;
}
Documentation Update

The PR updates the documentation to reflect that the noncausal attention mode is implemented and explains why step_batch_all() is refused for such packs. This helps clarify the behavior and prevents confusion.

- **nor can it verify a draft.** `step_batch_all()` returns the logits of *every* row of a batch, which a
  speculative decoder compares against the tokens it drafted at those positions. In a noncausal batch each
  row attends to the rows after it, so those are not prefix-conditioned logits and the call is refused with
  the same mode name. Unreachable today (the d3 packs ship no draft head), refused now so a pack that grows
  one cannot be silently verified through a mask it was not trained with.

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.

npu: the forward is causal-only, so a noncausal_full_attention pack (d3-lite) cannot be reproduced — measured, not inferred

1 participant