Skip to content

ggml-rpc: serialize and deserialize a graph without allocating per tensor - #14

Merged
lukekim merged 2 commits into
spiceai-b11803from
lukim/b11803-rpc-graph-noalloc
Oct 29, 2026
Merged

lukekim merged 2 commits into
spiceai-b11803from
lukim/b11803-rpc-graph-noalloc

Conversation

@lukekim

@lukekim lukekim commented Oct 28, 2026 •

Copy link
Copy Markdown

What this changes

Two commits in ggml/src/ggml-rpc/ggml-rpc.cpp, no new switch:

  • Serialize without per-tensor allocation. A graph the server has not stored is serialized whole, and on a three-rank tensor split every new graph is about a hundred per-device subgraphs per rank. serialize_graph allocated an std::unordered_set node per visited tensor, ran snprintf for 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).
  • Deserialize without per-tensor allocation. graph_compute built two std::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, and create_node at 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.

arm build symmetric mean (range), tok/s standard mean (range), tok/s
P spiceai-b11803 at a337bb5 46.21 (45.34–47.28) 45.12 (44.58–45.65)
Z P + graph serializer/deserializer + async state read 48.86 (47.99–50.26) 46.99 (46.91–47.07)
M Z + LLAMA_KV_PAD=1024 50.14 (49.01–51.04) 48.32 (48.24–48.40)

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.

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

  • The whole-run reply comparison above: every token of every reply identical with and without these commits (Z vs P, 16 replies over two loads).
  • test-backend-ops (every op, test mode) on an RPC device: a local rpc-server over the CPU backend, with the CPU backend as the reference, in all four pairings of client and server builds (spiceai-b11803 a337bb5 and this branch 2186458). Each run reports 22189/22189 tests passed, and each run's 25,197 per-case results (22,189 OK, 3,008 not supported [CPU]) are identical to base/base, line for line. Registering the RPC server needed a few lines in test-backend-ops that are not part of this PR.
  • Not run: a byte-for-byte comparison of one graph serialized by both builds. The ids sent are the client's tensor addresses, so two builds' bytes differ anyway; the pairings above test the format instead.
  • Not run: upstream's CI suite on this branch (no CUDA/RPC runner here). The windows job failed once in test_router_download_model (download completed with errors) and passed when re-run on the same commit.

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.
Copilot AI balanced review requested due to automatic review settings October 28, 2026 21:35
@lukekim lukekim self-assigned this Oct 28, 2026
@github-actions github-actions Bot added the ggml label Oct 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 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

@lukekim

lukekim commented Oct 28, 2026

Copy link
Copy Markdown
Author

Addressed #14 (review):

  • RPC runtime validation: ran test-backend-ops (every op, test mode) on an RPC device, using a local rpc-server over the CPU backend with CPU as the reference. It ran in all four pairings of client and server builds: base a337bb5 and this branch 2186458, each as client and as server. Every run reports 22189/22189 tests passed. The 25,197 per-case results (22,189 OK, 3,008 not supported [CPU]) of the three pairings that use this branch are identical, line for line, to base/base. The new deserializer, with its reused index and created-tensor vector, ran the 22,189 graphs back to back on one server. Old and new peers read each other's graphs in both directions, which is what "the wire format is unchanged" now claims in the description, instead of byte identity.
  • Byte-for-byte comparison: not run. The ids on the wire are the client's tensor addresses, so two builds' bytes differ by construction.
  • CI: windows failed once in test_router_download_model (download completed with errors) and passed when re-run on 2186458.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approved

No active review issues are reported for the RPC graph serialization and deserialization allocation optimizations.

No open issues 🎉

Review effort: Balanced

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants