Repository navigation
ggml-rpc: serialize and deserialize a graph without allocating per tensor - #14
Merged
Merged
Conversation
A graph the server has not stored is serialized whole, and on a three-rank tensor split every new graph is a hundred or so per-device subgraphs per rank. Profiling a llama-server head over prompts that each need new graphs put most of a new graph's submit time in serialize_graph: a std::unordered_set node allocated per visited tensor, snprintf for every tensor's name, a fresh tensor vector grown per graph, and zero-filling an output buffer that is then overwritten whole. The visited set is now an open-addressing set and the tensor vector is kept across graphs on the serializing thread, names are copied up to their terminator and zero-padded as before, and the output is not zeroed first. The bytes sent are unchanged.
A graph the server has not stored arrives whole, and graph_compute built two std::unordered_map for it, one from each tensor's id to its serialized form and one from each id to the tensor made for it, allocating a node for every entry. Profiling a server rank while a llama-server head ran prompts that each needed new graphs put create_node at 7% of the rank's CPU time, half of it in those maps' inserts. The server now keeps an open-addressing index from id to the tensor's position in the request, and the tensors made so far in a vector by position, both reused across graphs. Of several tensors with one id the first is used, as before, and a tensor is recorded as made before its sources are, as before.
There was a problem hiding this comment.
🔵 Needs a closer look
The byte-equivalence claim and reused graph-deserialization state still require RPC runtime validation, as neither CI nor test-backend-ops on an RPC device was run.
No open issues 🎉
What changed in this PR
This PR reduces per-tensor allocation overhead when submitting new computation graphs through the RPC backend, while preserving the wire format and graph reconstruction behavior.
Changes:
- Reuses an open-addressing visited set and thread-local tensor vector during serialization.
- Replaces formatted tensor-name copying with bounded copying and removes redundant output-buffer initialization.
- Reuses a server-side ID index and tensor vector during deserialization, preserving first-match handling of duplicate IDs.
| File | Description |
|---|---|
ggml/src/ggml-rpc/ggml-rpc.cpp |
Optimizes graph serialization and deserialization with reusable storage and fewer per-tensor allocations. |
Review effort: Balanced
Author
|
Addressed #14 (review):
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
Two commits in
ggml/src/ggml-rpc/ggml-rpc.cpp, no new switch:serialize_graphallocated anstd::unordered_setnode per visited tensor, ransnprintffor every tensor name, grew a fresh tensor vector per graph and zero-filled an output buffer it then overwrote. The visited set is now open-addressing, the tensor vector is kept across graphs on the serializing thread, names are copied up to their terminator and zero-padded as before, and the output is not zeroed first. The wire format is unchanged: an older server reads this client's graphs and this server reads an older client's (see Testing).graph_computebuilt twostd::unordered_maps per new graph (id → serialized tensor, id → created tensor). The server now keeps an open-addressing index from id to the tensor's position in the request and the created tensors in a vector by position, both reused across graphs. Of several tensors with one id the first is used, and a tensor is recorded before its sources, as before.Why
A new graph's submit time on a tensor-parallel head is dominated by sending it: about 270 subgraph sends of roughly 200 ms per new graph on GLM-5.3 split three ways, about three per request (a prompt chunk of a new length, a checkpoint tail, a re-split). Profiling put most of that in
serialize_graph, andcreate_nodeat 7% of a rank's CPU time, half of it in the maps' inserts.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=1024Replies: 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.
P→Z is +5.7% symmetric / +4.1% standard with both ranges disjoint, and no reply changes. Z also carries the async state read (#16), which measured about +1.2% alone on an earlier base, so most of P→Z is these two commits; the two were not measured apart on this base (not run).
Testing
test-backend-ops(every op, test mode) on an RPC device: a localrpc-serverover the CPU backend, with the CPU backend as the reference, in all four pairings of client and server builds (spiceai-b11803a337bb5 and this branch 2186458). Each run reports22189/22189 tests passed, and each run's 25,197 per-case results (22,189 OK, 3,008not supported [CPU]) are identical to base/base, line for line. Registering the RPC server needed a few lines intest-backend-opsthat are not part of this PR.windowsjob failed once intest_router_download_model(download completed with errors) and passed when re-run on the same commit.