Skip to content

rpc : typed request helpers and a single validation step on the server - #32651

Merged
am17an merged 2 commits into
masterfrom
aman/rpc-typed-msg
Nov 23, 2026
Merged

am17an merged 2 commits into
masterfrom
aman/rpc-typed-msg

Conversation

@am17an

@am17an am17an commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Overview

ggml-rpc builds every message by hand from packed structs, and the server validates client data separately in each handler. This PR keeps the wire format and the protocol version, and changes how the code builds and checks messages.

Typed messages. One table, rpc_msg_types<cmd>, maps each fixed-size command to its request and response structs. The client sends them with dispatcher->call<CMD>() / call_async<CMD>() and the server serves them with rpc_serve<CMD>(), so each case in rpc_serve_client is one line and passing the wrong struct for a command does not compile. SET_TENSOR, SET_TENSOR_2D and GRAPH_COMPUTE get header structs, written with rpc_writer and parsed with a bounds-checked rpc_reader.

One validation step.

  • deserialize_tensor rejects what ggml_new_tensor would assert on (op range, ne[0] not a multiple of the block size, element count overflow) and computes the tensor size without overflow before the buffer check.
  • rpc_check_region replaces the per-handler bounds checks. It checks the request against the tensor size, which ggml_backend_tensor_set/get/memset assert on. Master checks against the buffer, so a request inside the buffer but past the end of the tensor aborts the server.
  • graph_compute builds the graph in one pass, because the client already sends tensors in topological order (add_tensor is post-order). There is no recursion. Unknown or duplicate ids, duplicate nodes, sources sent after their users, compute nodes with unallocated operands, and views outside their view_src buffer are rejected. A tensor may still be its own source, which ggml_cast relies on.

The file goes from 3432 to 3212 lines.

Additional information

This overlaps with several open hardening PRs that add the same checks in place: #31018, #31190, #31843, #30474, #25670 and #31322. #31217 changes ggml_nbytes instead; here the size is checked on the wire values before the tensor is created.

Tested with CPU rpc-servers, against master built from the same base:

  • test-backend-ops with an RPC device (local hook, not part of this PR): 24801/24801 pass, the same set as master
  • greedy generation identical to master on Qwen3-4B, gemma-4-E2B and Qwen3.5-9B: one server, layers split over two servers, old client with new server, and new client with old server
  • -sm tensor over two servers and rpc-server -c (cache save and load) match master
  • test-rpc-multi-server and test-rpc-unreachable pass

Not tested yet: GPU servers, RDMA.

Requirements

Client and server share one table (rpc_msg_types) that maps each
fixed-size command to its request and response structs. The client sends
them with dispatcher->call<CMD>() and call_async<CMD>(), the server serves
them with rpc_serve<CMD>(), so the per-command code in rpc_serve_client
and at the call sites is gone and a wrong struct for a command does not
compile. SET_TENSOR, SET_TENSOR_2D and GRAPH_COMPUTE get header structs,
written with rpc_writer and parsed with a bounds-checked rpc_reader. The
wire format is unchanged.

Validation of client data is now in one place:
- deserialize_tensor rejects what ggml_new_tensor would assert on (op
  range, ne[0] not a multiple of the block size, element count overflow)
  and computes the tensor size without overflow before the buffer check
- rpc_check_region replaces the per-handler bounds checks and checks the
  request against the tensor size, which ggml_backend_tensor_set/get/memset
  assert on, so an in-buffer but out-of-tensor request no longer aborts
- graph_compute builds the graph in one pass over the tensors, which the
  client sends in topological order: no recursion, and unknown or
  duplicate ids, duplicate nodes, sources sent after their users, compute
  nodes with unallocated operands and views outside their view_src buffer
  are rejected; a tensor may be its own source (ggml_cast)
- GET_ALLOC_SIZE fails if a source tensor is invalid
- a client-sized allocation that fails closes the connection instead of
  terminating the server
@am17an
am17an requested a review from a team as a code owner November 20, 2026 09:21
@am17an am17an added the refactoring Refactoring label Nov 20, 2026
@github-actions github-actions Bot added the ggml changes relating to the ggml tensor library for machine learning label Nov 20, 2026

@rgerganov rgerganov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

still going through this, some initial comments

Comment thread ggml/src/ggml-rpc/ggml-rpc.cpp Resolved
Comment thread ggml/src/ggml-rpc/ggml-rpc.cpp Resolved
Comment thread ggml/src/ggml-rpc/ggml-rpc.cpp Resolved

@rgerganov rgerganov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, nice overall improvement, thanks. I also did some tests and I haven't found regressions so far. FYI, I am also planning to add a CI job for the RPC backend soon.

Comment thread ggml/src/ggml-rpc/ggml-rpc.cpp Resolved
Comment thread ggml/src/ggml-rpc/ggml-rpc.cpp Resolved

@ggerganov ggerganov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did some tests with 2x DGX Sparks + RDMA - looks OK

@am17an
am17an merged commit 03813c5 into master Nov 23, 2026
23 checks passed
Cinderella-Man added a commit to Cinderella-Man/miner-llama that referenced this pull request Nov 24, 2026
Upstream's newest tag, 191 commits past b12483, merged on pascal-p106-v5 af00bb1 (the 14 landed branches).
26 files conflicted. Each kept both sides' intent; the list with reasons is the public data folder
docs/data/2026-11-24_c912_upstream_b12674/conflicts.tsv. The ones that change code:

- ggml.h, ggml.c, ggml-rpc.h: upstream's GGML_OP_SLEEP and GGML_OP_SIGMOID_BACK go after the fork's three ops after
  GLU, so the fork's op numbers stay; GGML_OP_COUNT 105, RPC patch version 6 (fork 4 + upstream 2).
- ggml-rpc.cpp: upstream's single validation step (ggml-org#32651) already rejects unknown ops, so the fork's own check goes;
  the client's FREE_BUFFER that forgets dropped graphs (C929) sends with upstream's typed call<>.
- mmid.cu: upstream's helper with duplicate expert ids (ggml-org#32505) dumps its store in pieces; the legacy tile layout
  (GGML_CUDA_MMQ_MOE_LEGACY) reads the expert's column count from n_dumped.
- mmq.cuh: the kernel takes kq_fp32 (C861) before upstream's force_tiling (GCN only); the force_tiling launch passes
  kq_fp32 through.
- ggml-cuda.cu: ggml-org#32239 (no matrix-vector fusion on any Pascal card) merged without a conflict; the fork keeps the
  fusion on compute 6.1 as v5 measured it per file, and GGML_CUDA_MMID_GLU_UNFUSE=2 gives upstream's rule (C893).
- llama-graph.cpp: upstream's automatic "<name>.scale" lookup (ggml-org#32669) replaces the explicit scale arguments, the cold
  tier's included (its scales carry their tensors' names); the down projection's rescale is upstream's use_amax rule
  (ggml-org#32677) and the fork's LLAMA_NO_MOE_DOWN_RESCALE condition; route-once's histogram before upstream's random routing
  (ggml-org#29539); the device-built KQ mask (LLAMA_GPU_KQ_MASK) moves into upstream's build_attn_iswa_impl (ggml-org#32429) and
  replaces only the default mask.
- llama-memory-hybrid-idx.cpp: ggml-org#32519 scores only the ubatch's sequences' k-pools; with the k-pool tables on the devices
  (LLAMA_QSA_KPOOL_DEV) the list keeps every sequence's pools, as the tables hold them (get_n_kpool); the pads avoid
  every pool's rep in both layouts (LLAMA_QSA_PAD_LINEAR's bound kept).
- llama-quant.cpp: N2P and N3P's fallbacks go into upstream's fallback_type_for (ggml-org#15550), with the row width; Q4_1_64
  in upstream's moved llama_ftype_get_default_type.
- glm5-next.cpp, qwen4exp.cpp: the fused lightning indexer is always on (ggml-org#32650): the in-place row variant
  (LLAMA_QSA_INDEXER_ROWS) without the removed switch.
- flint.cpp, qwen4exp.cpp, gemma4.cpp, test-llama-archs.cpp, test-backend-ops.cpp, server-context.cpp: the fork's
  code with upstream's signature changes, model-name helper, decision groups and label outputs (ggml-org#32553), gemma4's
  multi-sequence prune (ggml-org#32342) without device-built masks.

Carried upstream fixes now merged upstream: ggml-org#32434 (6026911: upstream's version taken, the fork's change kept is only
--ctx-checkpoints-share's copy and get_reuse's two trim arguments) and ggml-org#31913 (2b2da8a: upstream's fill, the same
change).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ggml changes relating to the ggml tensor library for machine learning refactoring Refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants