GptOssEngine holds up to N conversation caches (--cache-slots N on the four workers, default 1 = unchanged). crates/cpu-worker/src/slots.rs chooses per prompt: continue in place a slot that is wholly a prefix of the prompt (the same agent's next turn); otherwise take the slot sharing the longest prefix as a donor and either continue it (losing its tail) or copy the shared part into a new/empty/least recently used slot (State::copy_prefix_from), whichever discards fewer cached tokens, so a new agent gets the shared system prompt without destroying another agent's cache. Results are bit-identical to a fresh prefill (synthetic test: copied prefix continues bit for bit, donor untouched). Measured: CPU only, 3 agents, 36 requests: 1.37x/1.46x prompt time, outputs identical. Through the control plane, 4 agents on one iGPU worker, 3 clean alternating pairs: 986/993/995 s with 1 slot against 569/575/575 s with 4 slots (1.73x), answers identical in all six runs. The first run of pair 1 was disturbed by the UTC date in the system prompt changing mid-run and was repeated. Compare-ConcurrentEpisodes.ps1 takes -Agents, -SharedInstructionTokens and the 'slots' arm and records output hashes. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
131 lines
9.3 KiB
Markdown
131 lines
9.3 KiB
Markdown
# Two agent requests at once: an iGPU worker and an NPU worker in one pool (STRA-220, experimental)
|
|
|
|
The control plane can supervise several workers behind its one request channel (`engine.extra_workers`,
|
|
`control-plane/src/pool.rs`, contract in `docs/api-contract.md`). With `leleka-igpu-worker` and `leleka-npu-worker` in a
|
|
pool, two agent requests run at the same time, one prefilling on the integrated GPU and one on the NPU (both decode on
|
|
the CPU). The dispatcher sends a request to an idle worker whose last prompt shares the longest prefix with it, so each
|
|
conversation keeps finding its cached prefix in the same worker.
|
|
|
|
```json
|
|
{ "engine": {
|
|
"executable": "target/release/leleka-igpu-worker.exe",
|
|
"args": ["--model", "<gpt-oss-20b-MXFP4.gguf>", "--ctx", "16384", "--threads", "7"],
|
|
"gptoss_gguf": "<gpt-oss-20b-MXFP4.gguf>",
|
|
"extra_workers": [
|
|
{ "executable": "target/release/leleka-npu-worker.exe",
|
|
"args": ["--model", "<gpt-oss-20b-MXFP4.gguf>", "--ctx", "16384", "--threads", "7",
|
|
"--openvino-root", "<openvino_toolkit_windows_*>"] } ] } }
|
|
```
|
|
|
|
The two workers map the same model file (the pages are shared) and each holds its own KV cache (about 0.8 GB at 16k
|
|
context), so memory use rises by one cache, the NPU service and the GPU scratch.
|
|
|
|
## Paired A/B: two concurrent agent episodes (2026-10-16)
|
|
|
|
`benchmarks/tools/Compare-ConcurrentEpisodes.ps1`, raw lines in `benchmarks/results/concurrent-episodes-pool-ab-2026-10-16.jsonl`.
|
|
Two different scripted episodes (four turns each; turn k carries k tool outputs of about 1,500 tokens of pseudo-source,
|
|
so about 2,060 new tokens per turn and 32 output tokens) are sent at the same time; each turn waits for the previous
|
|
one of its episode. Core Ultra 7 265U, gpt-oss-20b-MXFP4 (SHA-256 65d06d31...cde53ab), base commit 3ebcb3f plus the
|
|
pool, otherwise idle machine, power and clocks not controlled. Single worker: 10 CPU threads; pool: 7 each. One
|
|
untimed warm-up request per worker. Time until both episodes are done, three alternating pairs per comparison:
|
|
|
|
| arms | pair 1 | pair 2 | pair 3 | speed-up (mean) |
|
|
| --- | --- | --- | --- | --- |
|
|
| one iGPU worker, both episodes at once | 691.7 s | 712.8 s | 686.0 s | |
|
|
| iGPU + NPU pool, both at once | 253.8 s | 306.4 s | 271.2 s | 2.73, 2.33, 2.53 (2.53x) |
|
|
| one iGPU worker, the episodes one after the other | 325.2 s | 317.2 s | 341.7 s | |
|
|
| iGPU + NPU pool, both at once | 292.0 s | 302.5 s | 299.2 s | 1.11, 1.05, 1.14 (1.10x) |
|
|
|
|
What the two comparisons separate:
|
|
|
|
* Most of the 2.5x is the second cache. A single worker serving two interleaved conversations keeps only the last
|
|
request's prefix, so every turn recomputed its whole context (116 tokens cached of 2,191-8,425); in the pool each
|
|
turn reused its conversation's previous prompt (2,191-6,379 tokens cached).
|
|
* The second device itself adds about 1.1x: against the best schedule one worker can have (the two episodes in
|
|
sequence, full prefix reuse), the pool finished both 5-14% sooner.
|
|
* Each turn is slower in the pool (38-93 s against 31-53 s for the same turns on the single worker in sequence),
|
|
because the two devices, the two CPU decoders and the NPU path's CPU work share the memory channel and the 15 W
|
|
budget. The pool trades per-request latency for total throughput.
|
|
|
|
Limits: scripted episodes rather than an agent trace, three pairs per comparison, uncontrolled power state; the answers
|
|
were not compared between arms (the decoders are the same; the NPU path's numerics are the experimental ones of
|
|
`docs/npu-service.md`). A single worker that keeps several conversations' caches should get most of the 2.5x without a
|
|
second device; that is the next section.
|
|
|
|
## Cache slots: one worker, several agents' caches (`--cache-slots`, 2026-10-17)
|
|
|
|
The measurement above showed that most of the pool's gain was the second KV cache, not the second device. A worker now
|
|
keeps the caches of up to N conversations: `--cache-slots N` on `leleka-cpu-worker`, `leleka-igpu-worker`,
|
|
`leleka-npu-worker` and `leleka-gpu-npu-worker` (default 1: only the last request's, as before). Slots are created when
|
|
first needed; each costs `ctx x 49 KB` once filled (0.8 GB at 16,384 tokens, 6.4 GB at 131,072). The line protocol and
|
|
the control plane are unchanged; `usage.input_tokens_details.cached_tokens` still reports what the chosen slot supplied.
|
|
`LELEKA_SLOT_LOG=1` prints the choice for every prompt.
|
|
|
|
### Choosing the slot (`crates/cpu-worker/src/slots.rs`, unit-tested)
|
|
|
|
Taking the slot with the longest common prefix is not enough: `Model::prefill_reuse` truncates the slot to the common
|
|
prefix, which destroys the rest of a cache that may be another agent's whole history. The planner therefore:
|
|
|
|
1. continues in place a slot that is entirely a prefix of the prompt (the same agent's next turn, or a retry); the
|
|
longest wins, ties go to the most recently used;
|
|
2. otherwise takes the slot sharing the longest prefix as a donor and compares two losses, in cached tokens: continuing
|
|
the donor drops its tail (everything past the shared part), copying the shared part into another slot
|
|
(`State::copy_prefix_from`, a memory copy of about 49 KB per token) drops that slot's whole content, and a new or empty
|
|
slot loses nothing. The smaller loss wins. A new agent that shares only the system prompt and tools with others gets a
|
|
copy of that part in a free (else the least recently used) slot and leaves the donor intact; an agent whose own history
|
|
was rewritten near its end (Harmony drops the reasoning of earlier turns) continues its slot and loses only the tail;
|
|
3. a prompt sharing nothing takes a new slot, then an empty one, then the least recently used.
|
|
|
|
The last token of every prompt is always computed (its logits are needed), as before. Results are bit-identical to a
|
|
fresh prefill (`gptoss/tests/synthetic_model.rs`: a copied prefix continues bit for bit and the donor is untouched).
|
|
|
|
### Measured on the CPU path (`crates/cpu-worker/examples/ab_slots.rs`, real model, 2026-10-17)
|
|
|
|
Three agents sharing a 603-token system and developer prefix take turns round-robin for three turns (prompts of 619,
|
|
about 1,390 and about 2,150 tokens); each turn's prompt is the previous one plus the assistant message re-rendered as
|
|
Harmony history is (reasoning dropped), plus a new tool output, tokenized whole; 24 output tokens, greedy; CPU prefill
|
|
only, 14 threads, two alternating pairs:
|
|
|
|
| | 1 slot | 3 slots |
|
|
| --- | --- | --- |
|
|
| prompt time, pair 1 / pair 2 | 256 s / 277 s | 188 s / 190 s |
|
|
| tokens computed / reused | 7,680 / 4,824 | 5,256 / 7,248 |
|
|
| paired speed-up | | 1.37x, 1.46x |
|
|
|
|
Agents 2 and 3 copied the shared 603 tokens from agent 1's slot on their first turn (the slot log shows
|
|
`Copy { from, into, len: 603 }`), and from then on each continued its own slot (1,399 tokens reused on the third turn
|
|
instead of 603). The greedy outputs of all 36 requests were identical between the arms. The gain grows with the share
|
|
of each agent's own history in the prompt and with the number of agents.
|
|
|
|
### Measured through the control plane: four agents, one iGPU worker (2026-10-17)
|
|
|
|
`benchmarks/tools/Compare-ConcurrentEpisodes.ps1 -Agents 4 -Turns 3 -SharedInstructionTokens 1500 -Arms single,slots`,
|
|
raw lines in `benchmarks/results/agent-pool-cache-slots-ab-2026-10-17*.jsonl`. Four scripted agents sent their requests
|
|
at the same time, so the worker served them one after the other in arrival order; each agent has its own task and
|
|
tool outputs and all share 1,500 tokens of instructions (2,050 shared prompt tokens with the system and tool parts);
|
|
turns of about 2,060 new tokens, 32 output tokens, three turns each (prompts of about 2,100-8,300 tokens, context 16,384).
|
|
`leleka-igpu-worker` alone, once with one cache slot and once with `--cache-slots 4`, three alternating pairs, otherwise
|
|
idle machine, power and clocks not controlled, model gpt-oss-20b-MXFP4 (SHA-256 65d06d31...cde53ab), base commit 4821fc4
|
|
plus this change. Time until all four episodes are done:
|
|
|
|
| pair | 1 slot | 4 slots | speed-up |
|
|
| --- | --- | --- | --- |
|
|
| 1 (rerun, see below) | 986.2 s | 569.2 s | 1.73x |
|
|
| 2 | 993.1 s | 575.2 s | 1.73x |
|
|
| 3 | 995.2 s | 574.9 s | 1.73x |
|
|
|
|
Per run, 74,495 prompt tokens were sent; the one-slot worker computed 51,885 of them (22,610 cached: only the shared
|
|
2,050-token prefix) and the four-slot worker 26,889 (47,606 cached: each turn continued its own agent's slot). The mean
|
|
time of a turn, which includes waiting behind the other agents, fell from 269 s to 165 s. The answers of all 12 agent
|
|
turns were identical, character for character, in all six runs.
|
|
|
|
The first run of pair 1 is not used. The control plane puts the current UTC date into the system prompt, and the date
|
|
changed (02:00 local time) while that run was going; one request therefore found only 35 tokens of its slot's cache
|
|
still valid (the date is the 35th token on), its answers and about 3% of the time differ, and the pair was rerun
|
|
(`...-pair1-rerun.jsonl`). The same effect means that once a day, at midnight UTC, the first request of every agent
|
|
misses its cache in the whole prefix after the date.
|
|
|
|
Memory: a slot costs `ctx x 49 KB` once filled, 768 MiB at 16,384 tokens, so four slots hold up to 3 GiB of cache; the
|
|
worker logs this at start. The working set was not measured. Limits: scripted episodes, not an agent trace; three pairs;
|
|
the shared prefix here is 2,050 tokens, so the gain depends on how much of a real agent's prompt is its own history.
|