Skip to content

npu: let a private route serve several requests at once (engine#3318) - #3327

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-npuconc/unified-slots
Nov 22, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-npuconc/unified-slots

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Part of #3318 (item 1: several users of one model on 1bit serve --device npu, the route Lemonade uses).

What

  • PrivateSession::max_concurrent() (default 1): how many requests a private route can take at once.
  • app/npu_queue.h: DeviceSlots, a counting timed lock (set_capacity, try_lock_for, unlock). wait_for_device is now a template over any timed lockable.
  • app/unified.cpp: the engine's device lock is a DeviceSlots, sized from the private session's max_concurrent() once it opens. With capacity 1 (every route today) behaviour is unchanged: one request at a time, the rest wait, and a timeout gives the same busy reply. The busy message names the count when it is above 1.
  • tests/npu_queue_test.cpp: four DeviceSlots arms (capacity 1 = a mutex, capacity N lets N in and blocks the next, try_lock_for times out then succeeds after an unlock, raising the capacity wakes a waiter).

No behaviour change until the private route reports more than 1. That route change is in the private add-on and reaches main with a later pin bump.

Checks

  • bash scripts/pre-pr-checks.sh --base origin/main: 18 green, 2 skipped (no gpu-kernels tree).
  • npu_queue_test: ok (ryzen, g++-16).

Measured with the private half (strixhalo)

An engine package built from this branch plus the private route change, under box-hold npu, amdxdna F87D0BDF248067862F83BA3, 0 amdxdna faults before and after. Qwen3-0.6B, 2 rounds per row, temperature 0:

1bit serve -m <Qwen3-0.6B NPU dir> --device npu --port P --npu-opt users=U
<checker> http://127.0.0.1:P/v1 x 1,2,4 2 200 long     # N streamed chats at once
<checker> http://127.0.0.1:P/v1 x 1 2 128 mixed        # 3 short questions 1 s into a 234-token reply
  • Correctness. Every concurrent reply was byte-identical to the same question asked alone, N up to 8, at U=1 and U=4. The same held through Lemonade (lemond with this build as its onebit backend, Qwen3-0.6B-NPU-1bit, N=1/2/4/8).
test U=1 (today: one at a time) U=4
4 long replies, total tok/s 63.9-64.8 75.2-75.3
2 long replies, total tok/s 57.9-61.8 71.7-72.2
4 long replies, slowest first token 4.76-5.09 s 1.36-1.38 s
3 short questions during a long reply: slowest short reply 3.62-3.92 s 1.58-1.60 s
the same: the long reply 3.28-3.43 s 4.44-4.46 s

Not run: models other than Qwen3-0.6B with U>1.

🤖 Generated with Claude Code

`1bit unified` (the route Lemonade starts with `1bit serve --device npu`) admitted one request at a
time through a timed mutex; every other chat waited behind a whole generation and a non-streamed
one got 503 after 5 s. This adds the hook a route needs to do better, with no change in behaviour:

- PrivateSession::max_concurrent() (default 1): how many generate() calls may run at once.
- app/npu_queue.h DeviceSlots: a counting lock with the timed mutex's member names, so
  wait_for_device() takes either. Capacity 1 is the old mutex exactly (same refusals, same ticks).
- unified sets the capacity from the session at load, before `ready`, and names the count in the
  busy refusal when it is above one.

The fast lane and every route that does not override max_concurrent() keep capacity 1. The private
dx route's slots are a separate change in the add-on. npu_queue_test: four new arms (capacity 1 =
the mutex; capacity 3 admits three without a tick and queues the fourth; 8 threads never exceed 2
inside; raising the capacity wakes a waiter).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

3318 - Partially compliant

Compliant requirements:

  • PrivateSession::max_concurrent() added with default 1, keeping behavior unchanged
  • DeviceSlots implemented as counting lock replacing timed_mutex
  • Engine uses DeviceSlots with capacity set from max_concurrent()
  • wait_for_device template function supports both timed_mutex and DeviceSlots
  • Tests added for DeviceSlots functionality

Non-compliant requirements:

  • Release-check row for concurrent NPU chats not implemented in this PR
  • Two models at once support not implemented in this PR

Requires further human verification:

  • Performance measurements for N=1/2/4/8 concurrent chats
  • Correctness verification of byte-identical replies
  • Private route change implementation (not in this PR)
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

DeviceSlots capacity validation

The DeviceSlots::set_capacity method throws std::invalid_argument for capacity < 1, but the code in unified.cpp calls std::max(1, e.priv->max_concurrent()) which should prevent this. However, if a private route returns a negative value for max_concurrent(), this could lead to an exception being thrown. This is a potential issue if the private route implementation is not properly validated.

void set_capacity(int capacity) {
    if (capacity < 1) throw std::invalid_argument("DeviceSlots: capacity must be at least 1");
    std::lock_guard<std::mutex> g(mu_);
    capacity_ = capacity;
    cv_.notify_all();
}
DeviceBusy message formatting

The DeviceBusy exception message construction uses std::to_string(e.mu.capacity()) which could be problematic if the capacity is not properly initialized or if there's a race condition. The message should be robust to handle any capacity value correctly.

throw DeviceBusy(e.mu.capacity() == 1 ? "the NPU is busy with another request"
                                      : "the NPU is busy with " + std::to_string(e.mu.capacity()) +
                                            " other requests");

@bong-water-water-bong
bong-water-water-bong enabled auto-merge (squash) November 22, 2026 20:12
@bong-water-water-bong
bong-water-water-bong merged commit df07ca5 into main Nov 22, 2026
11 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-npuconc/unified-slots branch November 22, 2026 20:24
bong-water-water-bong added a commit that referenced this pull request Nov 23, 2026
…te) + release-check row chat-npu-concurrent (engine#3318) (#3381)

* release-check: chat-npu-concurrent row, and the plain-queue arm accepts a concurrent answer (engine#3318)

- chat-npu-concurrent: four streamed known-answer chats sent together to the NPU entry; every one must end
  in [DONE], carry no error and contain its own answer (a slot mix-up hands one user another's text). A
  one-at-a-time route passes too (the others queue); the row prints the wall time. Pure verdict
  concurrent_verdict, driven by --self-test (7 cases).
- chat-npu-queue-plain: a 200 for the non-streamed chat is a PASS when it says 1BIT OK and came back while
  the long chat was still running (the route served both at once); a 200 after the long chat ended is still
  the old FAIL (a silently parked request). 6 new self-test cases.

--self-test PASS under bash and busybox sh (194 ok).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* pins: npu-kernels 10e73744 -> e61baa3b, several requests at once on the dx route (engine#3318)

npu-dx tip e61baa3b6 carries npu-kernels#121 (several users at once on one dx session: decoder slots, the route's
users= option, default 4) and #122 (fg_gemv_c34 sets its rounding mode at kernel start). #3327 already merged the
engine half (PrivateSession::max_concurrent).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* release-check: chat-npu-concurrent asks questions Qwen3-0.6B answers reliably (engine#3318)

Measured on strixhalo 23 Nov (installed v2026.48): the row FAILed on chat 4 of 4, "largest planet" -> "Earth": the
0.6B model's answer, not the route (the same question fails serially). It is now "3 times 3" -> 9, which the model
answers in every run of npu-kernels' concurrent-check; the four answers stay distinct, so a slot mix-up still shows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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