Repository navigation
kv-share: a backend that cannot export its KV degrades, instead of killing the decoder - #212
Conversation
…lling the decoder
`ONEBIT_KV_SHARE=1` makes a context allocate its KV as exportable dma-buf chunks. Two problems turned a decoder
that simply cannot export into a decoder that does not start, which is fatal for a hybrid route that decodes on
the CPU (measured 2026-11-05, shipped binary, Qwen3-0.6B, `-ngl 0`):
I srv load_model: KV cache exports its region: a shared prompt handoff moves no bytes
E llama_init_from_model: failed to initialize the context: shared KV: CPU allocation or dma-buf export failed
E srv llama_server: exiting due to model loading error
1. `share_proc()` fell back to "ask every registered backend" whenever the buffer type had no device -- added so
the CPU could be the *importing* side. That fallback must never resolve the *export* proc: no CPU backend
exports, so a CPU buffer was handed whichever backend does implement it (ggml-hrx: `libggml-hrx.so` is the only
shared object with one, `libggml-cpu.so` has none) and it was called on a foreign buffer. It is now restricted
to `ggml_backend_dev_buffer_from_dmabuf`, whose caller passes the buffer type's own device (null for the CPU,
which ggml-cpu's importer accepts).
2. `llama_share_alloc()` threw when the export proc was missing or an allocation/export failed. It now warns once
with the backend's own name, closes any fds it had already exported, clears the region list and returns an
ordinary `ggml_backend_alloc_ctx_tensors_from_buft` buffer. The context is then created normally,
`llama_kv_share_regions()` reports 0 regions, and `?action=share` refuses that context per request -- which is
a state a caller can act on (the engine's hybrid route falls back to its state-file handoff for that request)
instead of a backend that never came up.
The server's refusal says both causes now, instead of claiming the flag was not set:
501 {"error":{"message":"This server's memory has no shareable region: either it was not created with
ONEBIT_KV_SHARE=1, or its KV backend cannot export memory (the load-time warning names the backend)"}}
Measured on the same binary, before and after, no device (release build of this branch, `-ngl 0`, `--parallel 1`):
| | before | after |
|---|---|---|
| flag on, load | process exits: `shared KV: CPU allocation or dma-buf export failed` | `/health` 200, one warning: `share_degrade: CPU cannot export its memory; its KV cache is allocated normally and a shared handoff will be refused for this context (the state-file handoff still works)` |
| `?action=share` | unreachable (server dead) | 501 with the message above |
| decode | unreachable | `/completion` → ` Paris. The capital of France is also` (8 tokens) |
And through the engine's hybrid route with the flag on, one request against this decoder and a real model: the
route logs `share answered 501: ...; this decoder cannot share its KV, so the state-file route is used` and then
`15 prompt tokens: prefill 20 ms, restore 0 ms, share 0.000 ms, first byte 160 ms`; the reply reports
`cached_tokens 14` of 15, i.e. the decoder evaluated the last token only (engine ggml-org#941's contract) on the fallback
path. A decoder whose backend CAN export is untouched: the export proc still resolves through its own registry.
Refs engine#1203 (the per-request fallback this composes with) and engine#753.
|
#212 ready for review at 9dd6f8a — base |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at 9dd6f8a8 — reproduced the kill on the base, then the degrade on this head, with the same command line.
Built the fork at the PR's base (a402b136c, -DGGML_HRX=OFF so no device is involved, Qwen3-0.6B-Q4_K_M, -ngl 0, the CPU is the whole story), then rebuilt with only this PR's two files changed:
base a402b136c, ONEBIT_KV_SHARE=1 -ngl 0
I load_model: KV cache exports its region: a shared prompt handoff moves no bytes
E llama_init_from_model: failed to initialize the context: shared memory: CPU cannot export its memory
E llama_server: exiting due to model loading error rc 1
head 9dd6f8a8, same command
I load_model: KV cache exports its region: a shared prompt handoff moves no bytes
W share_degrade: CPU cannot export its memory; its KV cache is allocated normally and a shared
handoff will be refused for this context (the state-file handoff still works)
I llama_server: listening on http://127.0.0.1:18125 /health {"status":"ok"}
chat completion served (8 tokens, finish_reason "length")
Same defect, same load, opposite outcome — the backend that used to refuse to start now starts, says which backend cannot export, and serves.
The per-request half also holds. With -np 1 --slot-save-path on that server:
POST /slots/0?action=share {"socket":"/tmp/kvshare-test.sock"}
-> {"error":{"code":501,"message":"This server's memory has no shareable region: either it was not
created with ONEBIT_KV_SHARE=1, or its KV backend cannot export memory (the load-time warning
names the backend)","type":"not_supported_error"}}
/health afterwards: {"status":"ok"} (the refused share leaves the server usable)
The exporter half is code, and the code is the right shape: share_proc() still resolves the proc from the buffer type's own device first (llama-kv-share.cpp:113-118), so an HRX buffer keeps HRX's own exporter, and only the device-less fallback is narrowed to ggml_backend_dev_buffer_from_dmabuf — the import proc whose caller passes the buft's device (null for CPU, which ggml-cpu's importer accepts). The export proc can no longer be answered by a foreign backend for a CPU buffer, which is the line that was red in the body's log. The failure path frees the fds it had exported before degrading (both the out entries and the buffers), so nothing leaks on the way out.
Control arm: with ONEBIT_KV_SHARE unset, the same build logs neither exports its region nor share_degrade and serves normally — so the change is confined to the shared-KV path and does not alter a plain CPU decode.
Not verified by me: the HRX-device arm of the same scenario (a CPU decoder beside a registered libggml-hrx exporter, i.e. the exact two-backend log in the body). That needs a GPU window and a build with GGML_HRX=ON; the code path it exercises is the share_proc narrowing above, which is where the foreign-proc call was.
What
ONEBIT_KV_SHARE=1makes a context allocate its KV as exportable dma-buf chunks. On a decoder whose KV backend cannot export — ggml-cpu is the one that matters, because a hybrid route can legitimately decode on the CPU — that turned into a decoder that does not start. Measured on the shipped binary, no device, Qwen3-0.6B,-ngl 0:Two causes, both here:
share_proc()resolved the wrong backend's exporter. The "ask every registered backend" fallback was added so the CPU could be the importing side (a device-less buffer type — ggml-cpu's static buft is one,ggml-backend.cppmarks its device NULL). It also answered the export lookup: no CPU backend exports, so a CPU buffer was handed whoever does implement it — on this boxlibggml-hrx.sois the only shared object with aggml_backend_buffer_export_dmabuf(libggml-cpu.sohas none) — and that proc was then called on a foreign buffer. The fallback is now restricted toggml_backend_dev_buffer_from_dmabuf, whose caller passes the buffer type's own device (null for the CPU, which ggml-cpu's importer accepts).llama_share_alloc()threw. A backend that cannot export is not a misconfiguration, it is a backend that cannot. It now warns once, naming the backend; closes any fds it had already exported; clears the region list; and returns an ordinaryggml_backend_alloc_ctx_tensors_from_buftbuffer. The context is created normally,llama_kv_share_regions()reports 0, and?action=sharerefuses that context per request — a state a caller can act on, instead of a backend that never came up.The server's refusal names both causes instead of claiming the flag was never set:
Measured, before and after, same model and argv
ONEBIT_KV_SHARE=1,-ngl 0, loadshared KV: CPU allocation or dma-buf export failed/health200, one warning:share_degrade: CPU cannot export its memory; its KV cache is allocated normally and a shared handoff will be refused for this context (the state-file handoff still works)POST /slots/0?action=share/completion→" Paris. The capital of France is also"(8 tokens)Script (kept, not
/tmp):~/wt/zc-probes/kvshare-cpu-degrade.sh <llama-server> <model> <label>, logs in~/wt/zc-probes/kvshare-cpu-degrade-{before,after}/serve.log.Composition with the engine's fallback (engine#1203), one real request,
--device hybrid, flag on, this decoder:and the reply reports
cached_tokens 14of 15 prompt tokens — the decoder evaluated the last token only (engine#941's contract) on the fallback path, with a real state file.ONEBIT_KV_SHARE=1 tests/hybrid_e2e.shagainst this decoder also passes: greedy reply equal to plain--device cpu,prompt_n 1, plain 22.Not claimed
-DLLAMA_BUILD_SERVER=ON -DGGML_HRX=OFF -DLLAMA_BUILD_TESTS=OFFRelease on this box,cmake --build --target llama-serverclean.Engine side: the per-request fallback that makes the degraded decoder usable is engine#1203 (which links here). The engine consumes this through its usual pin bump.