Skip to content

kv-share: a backend that cannot export its KV degrades, instead of killing the decoder - #212

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/kv-share-degrade
Nov 6, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/kv-share-degrade

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

What

ONEBIT_KV_SHARE=1 makes 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:

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

Two causes, both here:

  1. 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.cpp marks 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 box libggml-hrx.so is the only shared object with a ggml_backend_buffer_export_dmabuf (libggml-cpu.so has none) — and that proc was then called on a foreign buffer. The fallback 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. 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 ordinary ggml_backend_alloc_ctx_tensors_from_buft buffer. The context is created normally, llama_kv_share_regions() reports 0, and ?action=share refuses 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:

501 {"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)"}}

Measured, before and after, same model and argv

before (shipped binary) after (this branch)
ONEBIT_KV_SHARE=1, -ngl 0, 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)
POST /slots/0?action=share unreachable (server dead) 501 with the message above
decode unreachable /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:

W share_degrade: CPU cannot export its memory; its KV cache is allocated normally and a shared handoff will be refused...
1bit serve: hybrid: share answered 501: {...}; this decoder cannot share its KV, so the state-file route is used
1bit serve: hybrid: 15 prompt tokens: prefill 20 ms, restore 0 ms, share 0.000 ms, first byte 160 ms

and the reply reports cached_tokens 14 of 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.sh against this decoder also passes: greedy reply equal to plain --device cpu, prompt_n 1, plain 22.

Not claimed

  • A decoder whose backend can export is untouched: its export proc still resolves through its own registry; the degrade path is only reached when the lookup fails or an export fails.
  • HRX/Vulkan export was not run here — no GPU ticket was taken; the export path's own device proof is unchanged (engine#753's 4 runs / 12 arms).
  • No behaviour change to import: the CPU importer keeps the fallback that made it work.
  • Build scope: -DLLAMA_BUILD_SERVER=ON -DGGML_HRX=OFF -DLLAMA_BUILD_TESTS=OFF Release on this box, cmake --build --target llama-server clean.

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.

…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.
@bong-water-water-bong

Copy link
Copy Markdown
Author

#212 ready for review at 9dd6f8a — base 1bit/hrx-vulkan-patched (a402b13), rebased onto that tip, device-free gate in the body: before the shipped binary exits at load with shared KV: CPU allocation or dma-buf export failed, after it serves with one warning and refuses ?action=share honestly (501), and ONEBIT_KV_SHARE=1 through the engine's hybrid route takes the state-file handoff (share 0.000 ms, cached_tokens 14 of 15). No test cases in this tree cover ONEBIT_KV_SHARE; the gate is engine#1203's route tests plus the script quoted in the body.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant