leleka-gpu-npu-worker (crates/gpu-npu-worker) splits a long prompt: the NPU path takes the first share of the tokens and the GPU path the rest, on two threads. They meet only in the KV cache, through the new leleka_gptoss::LayerCache, which both prefill paths now use instead of &mut State (same behaviour). The GPU part's cache waits for each layer the NPU part stores, so the GPU runs layer l while the NPU runs l+1. The last-token distribution matches the GPU alone (KL <= 1e-4). Measured: 2048 tokens 0.89-1.13x against the GPU alone, depending on share; agent episode (4 turns of ~2,060 new tokens, 6 alternating pairs) 0.81-1.16x per pair, a net loss. Both paths stream all weights whatever their token share and share the 15 W budget, and the NPU part's CPU attention over the growing context is the slow side. Not adopted; stays experimental. Also: NpuPrefill::with_cpu_threads, and -ArmA/-ArmB/-LogDir in Compare-PrefillWorkers.ps1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
6.9 KiB
Markdown
121 lines
6.9 KiB
Markdown
# Intel NPU prefill through a separate OpenVINO service (STRA-207, experimental)
|
||
|
||
Status: experimental. Opt-in through `leleka-npu-worker`; the CPU worker (`leleka-cpu-worker`) is unchanged and
|
||
never loads OpenVINO.
|
||
|
||
## Decision
|
||
|
||
The NPU is reached through OpenVINO. The owner chose a **separate service process**: `leleka-npu-service` loads
|
||
the OpenVINO C API (`openvino_c.dll`, dynamically, via `libloading`), memory-maps the same GGUF, and answers matrix
|
||
product requests. The worker (`leleka-npu-worker`) does everything else on the CPU (RMSNorm, RoPE, attention,
|
||
router, SwiGLU, combine) and only offloads the large matrix products of **prefill**. Decoding stays on the CPU.
|
||
No Python is involved at any point.
|
||
|
||
If the service cannot start, the worker serves from the CPU only. If an NPU prefill fails it is redone on the
|
||
CPU and the NPU is not used again in that session. Before each prefill the worker checks free memory
|
||
(`NpuPrefill::estimate_bytes`) and falls back to the CPU when it is short.
|
||
|
||
## Crates
|
||
|
||
| crate | role |
|
||
|---|---|
|
||
| `leleka-shm` | shared file mapping between worker and service (Windows + unix) |
|
||
| `leleka-openvino` | minimal dynamic binding of the OpenVINO C API (core, compile, infer request, tensors, `CACHE_DIR`) |
|
||
| `leleka-npu-service` | the service: weight expansion, hand-written IR graphs, protocol, prefetch |
|
||
| `leleka-gptoss-npu` | client of the service and the NPU-assisted prefill (`PrefillAccelerator`) |
|
||
| `leleka-npu-worker` | the worker binary |
|
||
|
||
Unsafe exceptions (raw OS mapping, FFI into OpenVINO, pointer-backed tensors) are recorded in the code next to
|
||
each block and are due for review on 2027-01-14.
|
||
|
||
## How a product runs
|
||
|
||
* Graph: `x[M,K] f16`, int8 weights `[N,K/32,32]` and f16 scales `[N,K/32,1]` as **runtime inputs** →
|
||
Convert → Multiply → Reshape → MatMul. Group scales are exact for Q8_0 and MXFP4 block scales; this is about
|
||
2x slower than per-channel scales and was kept for exactness.
|
||
* The NPU rejects `f4e2m1`, so MXFP4 is expanded to int8 (doubled E2M1 table, scale `2^(e-128)` as f16; exact).
|
||
* The NPU **flushes f16 subnormals to zero**. Real gpt-oss scales include subnormals, which first produced wrong
|
||
results (KL about 0.1). Fix: per-row scale boost by `2^shift` (`normalize_scales`); the service returns per-column
|
||
shifts after the result and the client divides them out. Exact.
|
||
* Static shape buckets 32…4096 rows; compiled graphs are cached with OpenVINO `CACHE_DIR`
|
||
(cold compile of the buckets about 26 s, cached start-up is a few seconds).
|
||
* Protocol: lines on stdin/stdout plus a shared region for tensors. `BATCH n` followed by `n` `MM` lines tells the
|
||
service each product's successor, so it expands the next weights on a thread while the device runs the current
|
||
product (two slots per shape). Reply: `OK <micros> <bucket> <shifted>`.
|
||
* Result layout in the region: result, then per-column shifts; `out_stride(rows, cols) = (rows*cols*2 + cols)`
|
||
rounded up to 64.
|
||
|
||
## Quality
|
||
|
||
Teacher-forced against llama.cpp on the same model (criteria: agreement ≥ 95 %, mean KL ≤ 0.012, NLL within
|
||
0.02). The noise floor between this project's own scalar and SIMD builds is about 0.005 in KL.
|
||
|
||
* Per-product accuracy test (`crates/npu-service/tests/service.rs`, real weights, Q8_0 attention and MXFP4
|
||
experts, shifts applied): passes.
|
||
* End to end: KL 0.0124–0.0128, i.e. **marginally above the 0.012 gate**; NLL passes only with exact (unquantized)
|
||
activations, which is the default. The NPU path is therefore **not eligible to be made a default**; it stays an
|
||
explicitly experimental mode.
|
||
* The metric is chaotic in positions 10–60 of the Harmony text, and the reference set is small; a larger one is
|
||
still open.
|
||
|
||
## Speed
|
||
|
||
Hardware: this development laptop (14 threads, AVX-VNNI kernels, Intel AI Boost NPU, iGPU, 31.5 GB RAM).
|
||
Numbers below are from `benchmarks/results/npu-prefill-ab-2026-10-15.txt` and the optimization profile runs of
|
||
2026-10-15. Hardware, driver and power state are not controlled, and the machine ran at about 90 % memory use
|
||
during part of the work, which distorts timings; treat them as indicative.
|
||
|
||
Optimization profile for one 4096-token prefill, batch 4096, graphs cached:
|
||
|
||
| step | before | after |
|
||
|---|---|---|
|
||
| total | CPU 270 s | 74.6 s (54.9 tok/s) |
|
||
| NPU wait | | 36.8 s |
|
||
| CPU attention | | 20.0 s |
|
||
| other CPU phases | | 15.0 s |
|
||
|
||
The 1024-token prefill spent 33 s waiting for the device before prefetching and 21 s after.
|
||
|
||
The NPU is busy only about a third of the time (about 34 %, hence the roughly 40 % peak seen in Task Manager)
|
||
because the CPU phases of a layer and the NPU products are serial. Pipelining attention across batches was
|
||
estimated at ≤ 1.2x and not implemented.
|
||
|
||
Comparison with the iGPU prefill (`docs/igpu-prefill.md`): the NPU path is about equal to it (4096 tokens: 74.6 s
|
||
against 67.5 s); it is not clearly faster. Its value is that it leaves the GPU free and lowers power.
|
||
|
||
Paired A/B (`ab_prefill_npu`, CPU and NPU prefill of the same prompt in one process, alternating order, batch 2048,
|
||
graphs cached, 3 pairs per size; model digest and date in the results file). Greedy next token is identical (27)
|
||
in all 12 runs.
|
||
|
||
| prompt tokens | CPU median | NPU median | paired speed-up (mean, 95 % CI) |
|
||
|---|---|---|---|
|
||
| 1024 | 63.3 s (16.2 tok/s) | 20.9 s (48.9 tok/s) | 2.88x [2.07, 3.70] |
|
||
| 4096 | 295.3 s (13.9 tok/s) | 84.6 s (48.4 tok/s) | 3.33x [2.49, 4.16] |
|
||
|
||
The intervals are wide (three pairs) and memory use was near 95 % during the run. The earlier profile run with
|
||
batch 4096 gave 74.6 s, so batch 2048 and memory pressure cost roughly 10 s at 4096 tokens. The CPU worker uses 14
|
||
unpinned threads; Windows leaves the two low-power E-cores idle.
|
||
|
||
## Limits
|
||
|
||
* Prefill only; below roughly 1024 tokens the CPU is as fast or faster (`--npu-min-tokens` default 1024).
|
||
* Running at the same time as the GPU prefill on parts of one prompt (`leleka-gpu-npu-worker`, `docs/gpu-npu-prefill.md`)
|
||
gave no reliable gain over the GPU alone; the NPU part, whose attention runs on the CPU, was the slow side.
|
||
* Not usable for verifying speculative drafts: one 9-token pass takes 6.3 s against 0.93 s on the CPU (`docs/cpu-runtime.md`,
|
||
`--speculate` section); `--speculate` verifies on the CPU in this worker too.
|
||
* The service and the worker both map the 12 GB model; they share pages, but total memory use is high on a 32 GB
|
||
machine and the CPU, iGPU and NPU together slow each other (shared memory and power).
|
||
* Linux x64 build is untested (no toolchain available in WSL here, Docker not running).
|
||
* Not yet measured end to end with a real agent workload.
|
||
|
||
## Running it
|
||
|
||
```powershell
|
||
cargo build --release -p leleka-npu-service -p leleka-npu-worker
|
||
.\target\release\leleka-npu-worker.exe --serve --model <gpt-oss-20b-MXFP4.gguf> `
|
||
--openvino-root <openvino_toolkit_windows_*> [--npu-min-tokens 1024] [--npu-batch 2048]
|
||
```
|
||
|
||
`LELEKA_NPU_PROFILE=1` makes the service print a time profile on exit. Tests that need the model and the runtime
|
||
use `LELEKA_GPTOSS_GGUF`, `LELEKA_OPENVINO_ROOT` and `LELEKA_OPENVINO_DEVICE`.
|