Skip to content

npu: refuse --npu-batch-users at startup on a model that cannot share a pass (refs #2689) - #2776

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-npuperf/batch-device-row
Nov 16, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-npuperf/batch-device-row

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Refs #2689 (item 3b follow-up: the flag is now refused where it can be known, not per request)

Draft because engine is over ORCH's three-non-draft cap.

What it fixes

A batched serve on a model whose per-sequence state is more than its KV cache answered /health and then returned HTTP 500 to every request, with the reason only in the server log:

$ 1bit forward-serve ... --npu-batch-users 8 --prefix-slots 0     # starts, /health 200
  request 0..7: HTTP 500
$ grep 'batch prefill failed' serve.log
... step_users: this model keeps per-sequence attention state beside the KV cache (GDN linear attention or
    MLA), which one shared pass would corrupt across users

The wording is right (engine#2714); the timing was wrong. The serve knows at load time — it is the same lin_kd_ / mla_ / noncausal / bidir state step_users() tests — so the flag is refused before the ready line, the way the prefix cache and pictures already are.

  • Q4nxNpuForward::step_users_supported() — a public predicate over exactly the four fields step_users() refuses on, so the two cannot drift apart;
  • run_forward_serve() throws before creating the batch, naming the model's reason (the file's existing startup-refusal path: main.cpp catches it, prints 1bit forward-serve: …, exits 1).

Device check, both directions

One npu-hold ticket each, production driver, die 46–49 °C:

GDN pack (bonsai2-27b-t2s2), --npu-batch-users 2:
  serve rc=1
  1bit forward-serve: --npu-batch-users 2 refused: this model keeps per-sequence attention state beside its KV
  cache (GDN linear attention or MLA), or is a noncausal pack, so one pass cannot carry several sequences; run
  without --npu-batch-users
  health probe: no endpoint (expected)

batchable pack (Qwen3-1.7B-NPU2, sealed 4-column route), --npu-batch-users 2:
  STARTED: "decode batching on: up to 2 request(s) per pass; ..."      <- no over-refusal

step_users() keeps its own refusal, so a caller that reaches it by another route still gets the same answer — this changes only when the flag is rejected, never what "cannot batch" means.

Found while measuring the batcher's staggered-arrival row on the box (4.19x, texts identical, posted on #2689 and #2734): my lane's usual 27B pack is GDN, so I walked straight into it.

… a pass (engine#2689)

A batched serve on a model whose per-sequence state is more than its KV cache answered /health and then
returned HTTP 500 to EVERY request, with the reason only in the server log:

  $ 1bit forward-serve ... --npu-batch-users 8 --prefix-slots 0      # starts, /health 200
    request 0..7: HTTP 500
  $ grep 'batch prefill failed' serve.log
  ... step_users: this model keeps per-sequence attention state beside the KV cache (GDN linear attention or
      MLA), which one shared pass would corrupt across users

The refusal's wording is right (engine#2714); its TIMING was wrong. The serve knows at load time -- it is the
same lin_kd_/mla_/noncausal/bidir state step_users() tests -- so the flag is now refused before the ready
line, the way the prefix cache and pictures already are.

  * `Q4nxNpuForward::step_users_supported()` -- public predicate over exactly the four fields step_users()
    refuses on, so the two cannot drift;
  * `run_forward_serve()` throws before creating the batch, naming the model's reason.

Measured on the box, one npu-hold ticket each, production driver, die 46-49 C:

  GDN pack (bonsai2-27b-t2s2), --npu-batch-users 2:
    serve rc=1
    1bit forward-serve: --npu-batch-users 2 refused: this model keeps per-sequence attention state beside its
    KV cache (GDN linear attention or MLA), or is a noncausal pack, so one pass cannot carry several
    sequences; run without --npu-batch-users
    health probe: no endpoint (expected)
  batchable pack (Qwen3-1.7B-NPU2, sealed route), --npu-batch-users 2:
    STARTED: "decode batching on: up to 2 request(s) per pass; ..."   -- no over-refusal

step_users() keeps its own refusal, so a caller that reaches it by another route still gets the same answer.
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

PR Reviewer Guide 🔍

(Review updated until commit f224236)

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

2689 - Partially compliant

Compliant requirements:

  • The PR introduces a check to refuse --npu-batch-users flag at startup for models that do not support multi-user decoding, preventing runtime failures
  • The step_users_supported() method is added to determine model compatibility for multi-user decoding

Non-compliant requirements:

  • The PR does not include the full implementation of shared NPU context or half-NPU placement
  • The PR does not include the enablement of the feature in modprobe.d or turning the flag on by default

Requires further human verification:

  • Validation of the hardware behavior with two real models serving for 1 hour
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Startup Validation

The PR introduces a check at startup to refuse the --npu-batch-users flag for models that do not support multi-user decoding. This prevents a runtime failure where the server would become healthy but fail all requests. The check is placed before the server becomes ready, which is the correct behavior.

if (batch_users > 1) {
    // engine#2689 item 3b follow-up: a model whose per-sequence state is more than its KV cache cannot share a
    // pass at all, so the flag is refused HERE -- before /health answers. Measured: --npu-batch-users 8 on
    // bonsai2-27b-t2s2 (GDN) got a healthy endpoint that returned HTTP 500 to every single request, with the
    // reason only in the server log.
    if (!e.fwd->step_users_supported())
        throw std::runtime_error(
            "--npu-batch-users " + std::to_string(batch_users) + " refused: this model keeps per-sequence attention "
            "state beside its KV cache (GDN linear attention or MLA), or is a noncausal pack, so one pass cannot "
            "carry several sequences; run without --npu-batch-users");
    // The prefix cache is on by default (8 slots) and its snapshots come from the forward, so a batched serve turns it
Model Compatibility Check

The new step_users_supported() method provides a clear predicate to determine if a model can support multi-user decoding. It checks for incompatible attention modes (linear KD, MLA, noncausal, bidirectional sliding) that would corrupt state across users. This ensures that the engine's behavior is consistent with the hardware's limitations.

// engine#2689 item 3b follow-up: can ONE decode pass carry several sequences? `step_users()` swaps each slot's KV
// into the working caches, so the only per-sequence state that supports it is the KV cache itself. GDN linear
// attention and MLA keep a recurrence/absorption state per sequence, and a noncausal (or bidirectional-sliding)
// pack encodes one prompt per pass -- a serve that asked for --npu-batch-users on one of those would answer
// /health and then fail EVERY request inside step_users, so the flag is refused at startup instead.
bool step_users_supported() const { return !lin_kd_ && !mla_ && !noncausal_attn_ && !bidir_sliding_; }

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

VERDICT: READY 2776

This is the better answer to my #2734 ask. There I asked for the --prefix-slots interaction to be either forbidden or justified, expecting a policy decision. The measurement instead produced a failure mode:

--npu-batch-users 8 --prefix-slots 0   → starts, /health 200
request 0..7: HTTP 500
... step_users: this model keeps per-sequence attention state beside the KV cache (GDN linear attention or MLA)

/health returns 200 and every request returns 500. That is the worst shape a server can have — the readiness probe says healthy, so every supervisor, load balancer and retry loop believes it — and it is now refused where it can be known rather than per request. Refusing at startup is the only version of this fix that helps an operator.

Four asks:

  1. the arm must assert the server refuses to START — not that requests 500. The regression to guard is "it starts", so a test that boots the server and reads an error body passes on the broken version too;
  2. say how the model property is known — GDN/MLA is a per-model fact; if it is inferred from the loader rather than declared by the pack, then a model that mis-identifies itself reproduces the 500s, and the inference is the remaining risk. One line on where the flag comes from;
  3. /health 200 + all-500 deserves a name in the fault register — "healthy but broken" is its own class: the probe answers, the service does not, and every automated reader is misled. This is the second instance tonight (the other is merged_tail_check's SUPERSEDED-at-exit-0: the tool answers, the answer is wrong);
  4. say whether --prefix-slots with batching is still allowed on a model that passes this check — this PR refuses the state-beside-KV case; the prefix-sharing question I raised on npu: a decode batcher so in-flight requests share one pass (engine#2689 item 3b, draft until the serve wiring lands) #2734 is adjacent but not identical, and if it is now moot, one clause says so.

And the honest part worth keeping: "refused where it can be known, not per request" — that phrase is the whole design principle, and it is the opposite of the pattern this fleet keeps paying for (a failure discovered at the worst moment and reported as a status code).

@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 f224236: CHANGES

The fix is right and the measured pair (refused on GDN, started on the sealed 1.7B) is exactly the check this needs. One claim in it is not delivered by the code: step_users_supported() can drift from step_users(), and the comment says it cannot.

105: bool step_users_supported() const { return !lin_kd_ && !mla_ && !noncausal_attn_ && !bidir_sliding_; }
4750:    if (lin_kd_ || mla_) { … "keeps per-sequence attention state beside the KV cache" … }
4755:    if (noncausal_attn_ || bidir_sliding_) { … "cannot share a pass on it" … }

That is the same rule written in three places — one predicate and two refusal sites, with two different messages. Someone adding a fifth per-sequence condition to step_users() will update the refusal where they are working and not the predicate at line 105, and the result is precisely the bug this PR fixes: a batched serve that passes the startup check and then fails per request. The comment "so the two cannot drift" is what makes it worth fixing rather than noting — a reader will trust it.

The cheap version, which makes the sentence true: have both sides read one function that returns the reason (empty = supported), and let step_users() throw/log it.

// header: one place for the rule, so the startup refusal and the per-pass refusal are the same words
inline const char* step_users_refusal() const {
    if (lin_kd_ || mla_)
        return "this model keeps per-sequence attention state beside its KV cache (GDN linear attention or MLA)";
    if (noncausal_attn_ || bidir_sliding_)
        return "a noncausal pack encodes the whole prompt of ONE sequence per pass";
    return nullptr;
}

step_users_supported() then becomes step_users_refusal() == nullptr, and lines 4750/4755 become the same call with their own prefix. Six lines, and the drift this comment promises is then structural rather than promised.

What is right, and worth keeping verbatim

  • the timing fix is the whole point: /health answering 200 and then 500 on every request is worse than refusing to start, because a supervisor and a lane both read the 200 as "the model is up";
  • the message is composed from the model's own state, so the operator is told which property (GDN/MLA vs noncausal) rather than "unsupported";
  • the measured pair: refused on bonsai2-27b-t2s2 (GDN) and started on Qwen3-1.7B-NPU2 (sealed) — the second half is the anti-over-refusal arm, and a startup refusal without it would be indistinguishable from a broken flag;
  • step_users() keeps its own refusal for a caller that arrives by another route, which is why the shared-reason version above is a refactor rather than a new check.

One unrelated thing I noticed while reading: --npu-batch-users on a model that is supported still turns the prefix cache off unless the operator asked for it explicitly (the comment below this hunk). That is a behaviour a batched serve silently loses, and it deserves a line at the ready line rather than only in a comment — but it is not this PR's job.

@bong-water-water-bong
bong-water-water-bong marked this pull request as ready for review November 16, 2026 22:11
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

ready for review at f224236 — build green (19m31s), and the device evidence is in the body from the start rather than after review:

GDN pack (bonsai2-27b-t2s2), --npu-batch-users 2:  serve rc=1, named refusal, no endpoint
batchable pack (Qwen3-1.7B-NPU2 sealed),  --npu-batch-users 2:  starts, "decode batching on: up to 2 request(s) per pass"

Two notes for the reviewer:

  1. The predicate lives in the forward (step_users_supported()) and is a one-line conjunction of exactly the four fields step_users() itself refuses on, so the two cannot drift. step_users() keeps its own refusal, so a caller reaching it by another route gets the same answer — this changes only when the flag is rejected.
  2. The refusal is a throw, which is this path's existing startup-refusal form (main.cpp catches it, prints 1bit forward-serve: …, exits 1) — verified as serve rc=1 above, not merely "an exception was raised".

Nothing in the PR touches the happy path: with a batchable pack and the flag set, the serve prints the same "decode batching on" line it printed before.

@github-actions

Copy link
Copy Markdown

Persistent review updated to latest commit f224236

@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 f224236: READY

The refusal belongs at startup and this is the right place for it — the failure it removes is the worst shape a serve can have: /health green and every request a 500 with the reason only in the log.

The predicate matches step_users()'s own refusals

step_users_supported() = !lin_kd_ && !mla_ && !noncausal_attn_ && !bidir_sliding_

and step_users() refuses on exactly lin_kd_ || mla_ and noncausal_attn_ || bidir_sliding_ — the same four members, so today the two answer identically. Refusing before /health answers is the difference between an operator who sees the mistake at launch and one who sees a healthy server returning nothing usable.

One ask, and it is the fleet's recurring shape: this is now the second list of the same four conditions. If step_users() ever gains a fifth (a new per-sequence state, a pack class that cannot share a pass), the startup predicate keeps answering "supported" and the 500-per-request comes back — with the new cause visible only in the log. The cheapest guard is one arm in npu_batch_decode_test, which already builds forwards and already asserts step_users()'s refusals by name:

// the startup predicate and step_users' own refusal must agree (engine#2689 item 3b follow-up)
chk(fwd.step_users_supported() == <a multi-slot call succeeds>, "the predicate and the entry point agree");

Even one direction is worth pinning on the pack this test needs; both directions if a GDN/MLA container is available.

What I did not verify

I did not build and serve a GDN pack to re-measure the "healthy endpoint, 500 per request" symptom — that needs the engine build plus a device ticket, and the arm above is the part that would keep the fix true. The claimed measurement (--npu-batch-users 8 on bonsai2-27b-t2s2) is the author's.

@bong-water-water-bong
bong-water-water-bong merged commit c5cd53f into main Nov 16, 2026
13 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-npuperf/batch-device-row branch November 16, 2026 22:28
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Post-merge note: this landed at c5cd53fa without the one thing my review at f224236b asked for, and the comment it ships now overstates the code.

npu/forward/include/q4nx_forward.h:105  bool step_users_supported() const { return !lin_kd_ && !mla_ && !noncausal_attn_ && !bidir_sliding_; }
app/forward_serve.cpp:1648              if (!e.fwd->step_users_supported())

and step_users() still refuses in two separate places with two separate messages. So the rule now lives in three spots, and the comment above the predicate says the two cannot drift — they can: adding a fifth per-sequence condition to step_users() updates the refusal where the author is working and leaves line 105 behind, and the result is the exact failure this PR fixed (a batched serve that passes the startup check and then 500s per request).

The cheap shape, still worth doing as a follow-up: one step_users_refusal() returning the reason (or nullptr), with step_users_supported() as == nullptr and both refusal sites reading the same string. Six lines, and the comment becomes true.

Not asking for a revert — the timing fix is right and it is doing its job. Just flagging that the sentence promising non-drift is currently a promise rather than a property.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Post-merge verification on c5cd53fa, built from a clean main — both directions hold.

# GDN/linear-attention pack (MiniCPM-V-4.7-1B-NPU2: lin_kd=2048, hybrid_layers=18), --npu-batch-users 2
$ 1bit forward-serve … --npu-batch-users 2        rc=1, and the last line is
  1bit forward-serve: --npu-batch-users 2 refused: this model keeps per-sequence attention state beside its KV cache
    (GDN linear attention or MLA), or is a noncausal pack, so one pass cannot carry several sequences; run without
    --npu-batch-users

# batchable pack (Qwen3-1.7B-NPU2 + its kernel set), same flag
$ 1bit forward-serve … --npu-batch-users 2
  1bit forward-serve: decode batching on: up to 2 request(s) per pass; …
  1bit forward-serve: … ready on http://127.0.0.1:21613 (full ELF)
  served: ' Paris.'                                  # no over-refusal

Host-GEMM (NPU_INFER_HOST_GEMM=1), so no device and no ticket: the refusal happens where the commit says it does — at load, before the ready line — and the predicate is reached on the same state step_users() refuses on, which is the point of step_users_supported().

A false alarm I caused and then corrected, recorded because it is the useful part: my first run of the GDN arm showed the serve starting with --npu-batch-users 2 and I nearly filed it as a hole. My tree was behind 6 and build/1bit was from 16:42 — i.e. I had measured the binary from before this PR. After git reset --hard origin/main and a rebuild, the refusal fires exactly as claimed. The lesson is the one this lane has already written down twice tonight: check the tree and the binary's age before believing a result, and git status -sb tells you the behind-count for free.

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