Repository navigation
llama: read a state's tensors asynchronously and synchronize once - #16
Conversation
Saving a sequence's state read every tensor with its own synchronous ggml_backend_tensor_get. On a tensor split over RPC a hybrid model's recurrent state is dozens of tensors read from every rank, so a checkpoint after each prompt cost a round trip per tensor. The host writer now queues each read from a buffer a non-host backend uses on that backend and synchronizes each backend once. The meta backend's async read now reads exactly what its synchronous read does, with each device's part queued on that device: tensors split in several segments, and reads at an offset (one sequence's cells of a recurrent state).
There was a problem hiding this comment.
🟡 Changes recommended
Segmented recurrent-state saves can abort on empty slices, and multi-row RPC state reads still block by default instead of batching unless the async opt-in is enabled.
2 open issues
What changed in this PR
This PR aims to reduce state-save latency in llama.cpp by batching backend tensor reads, particularly for tensor-parallel models over RPC.
Changes:
- Queue host state-writer reads on compatible non-host backends and synchronize each used backend once.
- Extend meta-backend async reads to handle segmented tensors and nonzero row offsets.
- Correct per-device offsets for single-segment split reads.
| File | Description |
|---|---|
src/llama-context.cpp |
Pass context backends to host state writers and batch tensor reads. |
ggml/src/ggml-backend-meta.cpp |
Support segmented async reads and correct split-read offsets. |
Review effort: Balanced
A device whose slices of every tensor in a buffer are empty holds them in a zero-sized dummy buffer, and those tensors have no data. The segmented async read passed such a slice to ggml_backend_tensor_get_2d_async, which asserts the data pointer before it checks the size, so saving a state from that split aborted. The synchronous read already passes through empty transfers; the async read now skips them. test-meta-async-read splits tensors in two segments across two slices of the CPU device, along axis 0 and axis 1, with and without an empty slice, and checks that the synchronous and async reads of the whole tensor and of each row return the bytes written.
|
Addressed #16 (review):
|
There was a problem hiding this comment.
🔵 Needs a closer look
The async getter changes still need hardware round-trip validation for caller-owned host and state buffers on non-host backends, especially CANN, beyond the supplied CPU/meta and RPC coverage.
No open issues 🎉
2 resolved since last review
Review effort: Balanced
|
Addressed #16 (review):
|
There was a problem hiding this comment.
🟡 Changes recommended
State saves on Metal can fail validation or assert because serialized tensor destinations are passed to an API requiring page-aligned addresses and page-sized lengths that those destinations do not guarantee.
1 open issue
Review effort: Balanced
GCC's -Wcast-qual rejected passing the address of a const split as the meta device's userdata (CI builds with -Werror). The splits are now non-const.
…getter The async read wraps the destination with newBufferWithBytesNoCopy, which Metal documents for page-aligned memory only. Reading a state through the host writer's queued reads sends Metal destinations inside the serialized state: in test-llama-context on Qwen3.5-0.8B, 3868 of 4094 async reads had a destination off a 16 KiB page boundary, against 0 of 224 before. A destination that is not page-aligned is now read once the queue is done, as for a backend without an async getter; an aligned one keeps the async read. With this, none of the 226 async reads is unaligned, and the state cases pass with Metal API validation on, as before on that macOS, with the same per-case results as the base.
|
Addressed #16 (review):
|
There was a problem hiding this comment.
🔵 Needs a closer look
Backend-owner validation is needed for CANN’s async reads into caller-owned host memory and state-save correctness, which the reported CUDA, Metal, and RPC runs do not cover.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
The async getter asserted the tensor was not quantized: a quantized tensor may be held in CANN's own layout, which only the synchronous getter transforms back. A saved state now reads every tensor of a backend's buffer through its async getter, a quantized KV cache included, so the getter reads such a tensor once the stream is done instead of aborting.
|
Round on 4f39d1e, answering Copilot review #16 (review) (backend-owner validation for CANN):
|
… getter" This reverts commit 4f39d1e.
A backend may hold a quantized tensor in a layout of its own that only its synchronous getter reads back: CANN's async getter rejects quantized tensors, and its synchronous getter converts the whole tensor even for a partial read. The host writer now reads a quantized tensor (a quantized KV cache) with ggml_backend_tensor_get, as the base does, and queues every other tensor of a backend's buffer as before.
|
Round on fcae8cd:
|
…el's tensors For a model split across nodes, configure_comm also sets GGML_RPC_GET_ASYNC and GGML_RPC_SET_ASYNC, as it sets the mesh settings: the head queues its reads and writes of the ranks' tensors instead of waiting on each round trip. llama.cpp leaves both off for other RPC clients; only the split reaches the ranks over RPC. A setting already in the environment wins. The meta backend's async read is correct from spiceai/llama.cpp#16, which this pin carries.
Pin spiceai/llama.cpp b11999-spiceai.1: upstream b11984 and then b11999 merged into spiceai-b11803, which is merged in again at spiceai/llama.cpp#16. This brings Mistral Large 4 (mistral-large4) and its DSpark drafter, and DeepSeek-V4.1 now runs on upstream's deepseek41 model with the fork's fixes on top. The fork-patch ledger is re-audited against the new revision: spiceai#14's RPC graph serialization, spiceai#16's asynchronous state reads and glm5-next's DFlash layer inputs get rows, spiceai#15's LLAMA_KV_PAD joins the opt-in levers, and the DeepSeek-V4 zero output-group check gets a row and a guard, a crafted GGUF header that must fail its load on that check.
…el's tensors (spiceai#15831) For a model split across nodes, configure_comm also sets GGML_RPC_GET_ASYNC and GGML_RPC_SET_ASYNC, as it sets the mesh settings: the head queues its reads and writes of the ranks' tensors instead of waiting on each round trip. llama.cpp leaves both off for other RPC clients; only the split reaches the ranks over RPC. A setting already in the environment wins. The meta backend's async read is correct from spiceai/llama.cpp#16, which this pin carries.


What this changes
Saving a sequence's state read every tensor with its own synchronous
ggml_backend_tensor_get. The host writer now queues each read from a buffer that a non-host backend uses on that backend and synchronizes each backend once. The meta backend's async read now reads exactly what its synchronous read does, with each device's part queued on that device: tensors split in several segments, and reads at an offset (one sequence's cells of a recurrent state). A device whose slices in a buffer are all empty holds them in a dummy buffer with no data; the async read skips those slices, as the synchronous read does. Metal's async read takes a page-aligned destination only, so it now reads any other destination once its queue is done. A quantized tensor (a quantized KV cache) is still read synchronously, as before: a backend may keep one in a layout of its own that only its synchronous getter reads back, and CANN's async getter rejects quantized tensors.Over RPC, a read of one row is queued on the backend either way, but a read of several rows of a split tensor (a 2D read) is queued only with
GGML_RPC_GET_ASYNC=1, #8's opt-in. Without it, each such read still waits for its own round trip, as every read did before this change. A synchronous command goes through the same queue as the queued reads and waits for them, so the two can be mixed.Why
On a tensor split over RPC a hybrid model's recurrent state is dozens of tensors read from every rank, so a prompt-cache checkpoint after each prompt cost one round trip per tensor. On GLM-5.3 split three ways a checkpoint save went from about 72 ms to 28 ms with this change, on an earlier base.
How it was measured
Three DGX Spark (GB10) nodes over 200 GbE RDMA serving GLM-5.3 (Q2_K_XL) split three ways (
-sm tensor, the full-mesh all-reduce from #8), driven by the Spice runtime's llama.cpp engine with the head's own GPU reached over the NIC's RDMA loopback. Single-request decode over 8 fixed prompts, temperature 0: each prompt runs for 200 tokens and for 1 token, and the decode rate is(tokens − 1) / (T200 − T1). In the symmetric form an untimed one-token request on an unrelated prompt runs before each timed request, so both start from the same prompt-cache state; the standard form runs them back to back. Six sessions per run (one model load each), interleaved, three benches per session; every arm's replies are compared prompt by prompt.spiceai-b11803at a337bb5LLAMA_KV_PAD=1024Every arm ran with
GGML_RPC_GET_ASYNCon, as every lever run on this lab does; without it the RPC part of this change covers only one-row reads (not measured). Replies: both Z sessions are identical to P on 8/8 prompts; M differs from P on 7/8 (see #15). 8 prompts per bench is too few samples for a P99, so none is stated.Z carries this change together with the graph serializer/deserializer (#14): +5.7% symmetric / +4.1% standard over P, with no reply changed. Alone, on an earlier base, it measured about +1.2% on GLM single-request and multi-turn throughput; it was not measured alone on this base (not run).
Testing
test-meta-async-read(new): tensors split in two segments across two slices of the CPU device, along axis 0 and axis 1, with and without an empty slice; the synchronous and async reads of the whole tensor and of each row must return the bytes written. On 47073f7 it aborts on the empty slice (ggml-backend.cpp:461: GGML_ASSERT(tensor->data != NULL && "tensor not allocated") failed, exit 134, after the synchronous read of the same split matched); on 4371531 all 8 reads match (exit 0).test-meta-graph-slotspasses on 4371531.test-llama-contextwith CUDA on one GB10 (a non-host backend: the KV cache is inCUDA0buffers, all 37 layers offloaded), base a337bb5 vs 4371531. These are the state cases: save/load/replay, sequence copy over host and device io, recurrent rollback and replay.models: 136 passed, 0 failedon both builds, with identical per-case tables. Rollback and replay ran on the 12 recurrent architectures. Which device held their caches is not shown in these logs.test-llama-contexton Qwen3-0.6B Q8_0 and Qwen3.5-0.8B (hybrid), KV cache inMTL0buffers. Metal's async read wraps the destination withnewBufferWithBytesNoCopy, which Metal documents for page-aligned memory. Before d6c212f, 3868 of the 4094 async reads (counted under lldb on Qwen3.5) had a destination that is not page-aligned, against 0 of 224 on the base. The cases still passed, with Metal API validation on too. Since d6c212f such a destination is read once the queue is done: 0 of 226 async reads are unaligned. Both models pass with Metal API validation on (12 passed, 3 skipped; 15 passed) with per-case results identical to the base.test-llama-contexton Qwen3-0.6B,ggml_metal_get_tensor_asynccalls counted under lldb. With-ctk q8_0 -ctv q8_0 -fa on: base 70, previous head 77, fcae8cd 70, so its K and V are read synchronously as on the base. With f16: 90, 97, 97. Per-case results are identical to the base in both. On the base itself, with q8_0,cp_dandrffail andcp_d_ssegfaults, identically on every build, so that comparison leaves outcp_d_s.