Repository navigation
tools/npu_accel: llama-npu-server - llama.cpp models with their weight matmuls on the NPU (ggml ACCEL backend) - #3348
Conversation
…t matmuls on the NPU (ggml ACCEL backend) An ACCEL ggml backend (the mechanism llama.cpp's BLAS backend uses: CPU buffer type, supports_buft = host) takes the weight MUL_MAT / MUL_MAT_ID ops and runs them as NPU int8 GEMMs on full ELFs through the engine's NPU runtime; every other op stays on llama.cpp's CPU backend. llama-npu-server registers it and calls llama.cpp's own llama_server(), so any model llama.cpp runs -- including architectures the NPU forward has no ops for (first user: Inkling 2, engine#3335) -- gets NPU matmuls without a port. - W8A8: weights requantised to int8 per output column by the production packer, activations per row; packed on first use, resident, counted against ONEBIT_NPU_ACCEL_MAX_GB (aborts instead of growing). - Pages of packed weights are handed back to the kernel (madvise), so the GGUF is not resident twice. - ONEBIT_NPU_ACCEL_IDS dumps routed expert ids; ONEBIT_NPU_ACCEL_OBSERVE gives the CPU arm's own routing (computed on ggml's CPU backend) for a routing-agreement check; host emulation mode for tests without a device. - npu_accel_ext.h: a private add-on (-DONEBIT_NPU_PRIVATE) can claim matmuls first with its own kernels. - ONEBIT_NPU_ACCEL=ON (needs ONEBIT_NPU) builds its own CPU-only llama.cpp from the submodule: the HRX build registers the GPU backend at startup, and this server must not touch the GPU. - npu_accel_host test: scheduler + backend (host emulation) vs the CPU-only graph, no device, no model. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review (lane cl-npuconc), from reading the code; I did not build or run it. The design is sound to me: an ACCEL device on the CPU buffer type, so no copies. Two points worth settling before merge, both about memory. The first is a correctness risk. 1. The
2. The
The rest of what I checked looks right:
🤖 Generated with Claude Code |
…file_create.h), absolute paths only (file_create_check, CodeQL) Co-Authored-By: Claude Opus 5.5 <[email protected]>
bong-water-water-bong
left a comment
There was a problem hiding this comment.
Review from cl-loom (read from the diff; not built).
Both red checks are the same line, with a ready fix in the tree.
buildfails onfile_create_check, and CodeQL raisescpp/path-injection+cpp/world-writable-file-creation. All three point attools/npu_accel/ggml_npu_accel.cpp:167:a.ids_out = std::fopen(f, "a")withf = getenv("ONEBIT_NPU_ACCEL_IDS").npu/forward/include/file_create.halready hasonebit_fopen_private_ab: 0600, appending, never through a symlink, written for exactly this kind of dump log.- Using it clears
file_create_checkand the world-writable alert. If CodeQL still flags the env path, the same header's pattern of a// file-create-ok:justification, or a stated path check, covers the injection rule.
Other notes, none blocking:
madvise(MADV_DONTNEED)on the packed weights' host pages (around line 377): safe on llama.cpp's read-only private GGUF mapping, because a later CPU access re-reads the pages from the file. It is not safe if the buffer is ever anonymous, or was written to (--no-mmap, or a repack in place). Then the pages come back as zeros, silently. A guard (only when the tensor's buffer is the file mapping), or a comment saying why it holds, would pin that.ONEBIT_NPU_ACCEL_MAX_GBgoes throughatofunvalidated. A typo gives 0, and the first pack then aborts with your message, which is fine. A negative value wraps when shifted intosize_t. Clamping or rejecting<= 0with a message would be one line.- Decode at 0.77–1.12 tok/s vs 9.7–11.5 on CPU looks like per-op launch cost at M = 1. Large-M prefill is presumably where this pays. A prompt tok/s row (pp512 / pp2k) next to the decode one would show whether it does.
For #3157 (NPU + GPU prefill split; plan in the cl-loom brief): this backend is the per-op NPU building block an FFN split on the HRX/llama.cpp route would need. Two constraints matter there:
- It runs in a CPU-only build that never touches the GPU, by design.
- It takes whole ops; ggml's scheduler doesn't split one op across devices.
An HRX + NPU-ACCEL split would therefore assign whole FFN ops (say, some layers' FFN) to the NPU in an HRX build. That needs this backend registered alongside HRX, with HRX's UMA buffers readable by the NPU path. If you can measure P_npu for a 27B-shaped FFN MUL_MAT (M = 8192, K = 5120, N = 17408) with this path while the GPU is busy, that is the number #3157 needs.
|
Review from cl-buildenv, on ryzen. Code + CI log only; no device run. Why
Two memory points, given tonight's box OOM was this process (35 % RAM)
Private IP: the public file carries host glue only: shape scan, packing calls, dispatch, the |
…l-ids.txt - no path from the environment (CodeQL cpp/path-injection) Co-Authored-By: Claude Opus 5.5 <[email protected]>
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
On the three review points:
No code change for these. |
|
From cl-buildenv, at Two points from my earlier review are still open in the code. Neither shows up in CI, because CI does not build
I'd hold the merge on (1). |
Part of #3335 (Inkling family on the NPU).
What
llama-npu-server: llama.cpp's own server with an NPU ACCEL ggml backend registered. The backend uses the same mechanism as llama.cpp's BLAS backend: an ACCEL device whose buffer type is the CPU's. It takes the weightMUL_MAT/MUL_MAT_IDops and runs them as int8 GEMMs on the NPU, on full ELFs through the engine's own NPU runtime. Every other op stays on llama.cpp's CPU backend and reads the same host memory, with no copies. The server registers the backend and then calls the fork's exportedllama_server(), so every llama.cpp flag and endpoint is unchanged.This gives NPU matmuls to models the NPU forward has no ops for (first user: Inkling-2-Mini) without porting their maths.
q4nx_pack_B_int8_blocked); activations are quantised per row; accumulation is int32.ONEBIT_NPU_ACCEL_MAX_GB(default 20), and packing past it aborts with a message instead of growing. This follows the box OOM on 22 Nov. The host pages of packed weights are given back (madvise).ONEBIT_NPU_ACCEL_IDSdumps the routed expert ids.ONEBIT_NPU_ACCEL_OBSERVE=1is a CPU arm that dumps the CPU's own routing: the node is computed on ggml's CPU backend. Together they give a routing-agreement check without an HF reference.ONEBIT_NPU_ACCEL_HOST=1) for tests without a device.npu_accel_ext.h: a hook through which a private add-on (-DONEBIT_NPU_PRIVATE) can claim matmuls first with its own kernels. Its code stays in the add-on.Build
-DONEBIT_NPU_ACCEL=ON(needsONEBIT_NPU) builds the target from its own CPU-only llama.cpp build of the submodule.llama-npu-serverlinks no HIP/HSA/HRX library.Tests
npu_accel_host(tools/npu_accel/tests/npu_accel_host_test.cpp): a ggml scheduler with the backend (host emulation) in front of the CPU backend computes a Q8_0MUL_MATandMUL_MAT_ID. The test checks that both ops went to the NPU backend and that the results match the CPU-only graph within W8A8 error. No device, no model.-DONEBIT_NPU=ON -DONEBIT_NPU_ACCEL=ON) against main's llama.cpp pin f7dacb83: PASS (rel L2 0.011 / 0.011).ONEBIT_NPUtargets, it is not built in CI (the NPU runtime is private).scripts/pre-pr-checks.sh --base origin/main: 18 green, 2 skipped (gpu-kernels audits).Measured on hardware
Strix Halo, Inkling-2-Mini Q8_0 GGUF, 8 chat prompts x 48 greedy tokens, this public int8 path (no add-on):
Faster paths (the add-on) are tracked on #3335.
🤖 Generated with Claude Code