Skip to content

Studio: serve one GGUF across N machines over llama.cpp RPC - #18277

Open
danielhanchen wants to merge 9 commits into
cluster/rpc-server-bundlefrom
cluster/layer-split
Open

danielhanchen wants to merge 9 commits into
cluster/rpc-server-bundlefrom
cluster/layer-split

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Stacked on #18276 (ships ggml-rpc-server with the llama.cpp bundle). Refs #4858.

This replaces #13726. That PR sat on top of the DGX Spark toolkit (#10280) and two-Spark serving (#10323), about 30k lines that multi-machine serving does not use. Here the same N-node serving depends only on #18276. The two Spark PRs become optional and stack on top of this one.

What this does

Studio can split a GGUF that does not fit one machine across any number of machines through llama.cpp RPC: Macs over Metal, Linux boxes with NVIDIA or AMD GPUs, DGX Sparks, or a mix. This machine runs llama-server, and every other machine runs ggml-rpc-server and holds a share of the layers.

It is opt-in. Nothing changes until the operator lists workers in UNSLOTH_CLUSTER_NODES or <studio home>/cluster.json. UNSLOTH_SPARK_SERVING=0 turns it off.

On a load, core/inference/cluster_serving.py:

  1. Checks which workers answer with a TCP probe (one dead endpoint otherwise aborts llama.cpp's whole device listing). It then runs the coordinator's own llama-server --rpc ... --list-devices to read each worker's devices and free memory.
  2. Serves locally when the model plus KV cache fits this machine.
  3. Otherwise splits by layer:
    • --rpc lists the workers that exposed a device;
    • --device RPC0,...,RPCk,<local GPU> keeps the local device last, so the output layer stays here;
    • --tensor-split weights each device by its usable free memory.
    • Projectors, drafters and adapters are charged to the coordinator, which loads them.
    • When the bundled llama-server has --pipeline-groups (server: --pipeline-groups, run the slots over several contexts of one model llama.cpp#187), the split gets pipeline groups sized to the slots.
  4. Refuses a load that no set of devices can hold, with the numbers, instead of failing mid-load.

Nodes marked managed get ggml-rpc-server started over non-interactive ssh. A reload strips exactly the block this module appended, so inherited extras are planned afresh. The owner-only GET /api/inference/spark/status reports the nodes, the devices, the plan and the exact arguments.

User guide: studio/MULTI_NODE_SERVING.md.

Code layout

Tests

  • test_cluster_serving.py (220 tests): config, device listing, placement, launch arguments, pipeline groups, reload stripping, managed workers, refusal, and that nothing runs without a cluster config.
  • test_rpc_serving.py:
    • the shared helpers, the owner-only status route, the route wiring (access refused before the split is touched, the reload gate, a load cancelled during startup);
    • that without a provider the hooks route nothing and count one replica;
    • that a registered provider answers every hook.
  • test_cluster_serving_e2e.py (with UNSLOTH_TEST_RPC_BIN and UNSLOTH_TEST_GGUF): three real ggml-rpc-server workers on loopback, the planner's own probe, a real llama-server, and every worker logs a model buffer. It passes on this branch with no Spark code in the tree.
  • The same tests with every PR stacked:

Evidence (measured on #13726; the planner code is unchanged)

Run Args Studio produced Result
CPU, 4 rpc-servers on loopback, through before_load --rpc 127.0.0.1:51060,...,51063 --device RPC0,RPC1,RPC2,RPC3 -sm layer --tensor-split 0.25,0.25,0.25,0.25 --cache-ram 0 every worker got a model buffer (28.28 / 28.28 / 28.28 / 186.97 MiB); completion " Paris, and the capital of the United"
CUDA, 3 GPU rpc-servers + CUDA coordinator, Qwen3-8B Q4_K_M --device RPC0,RPC1,RPC2,CUDA0 37/37 layers offloaded; CUDA0 1395.57 MiB, RPC 1115.76 / 972.01 / 972.01 MiB; completion " Paris, and the capital of Germany is"
One worker down unreachable endpoint left out and named in status split over the remaining workers, load succeeded

tests/test_cluster_serving_e2e.py repeats the loopback run as a test when UNSLOTH_TEST_RPC_BIN and UNSLOTH_TEST_GGUF are set (skipped otherwise): 3 workers, the planner's own probe, a real llama-server, and a check that each worker logged a model buffer.

Not in this PR

cluster_serving plans a layer split over the ggml-rpc-server workers listed in
UNSLOTH_CLUSTER_NODES or cluster.json: it lists each worker's devices through
the llama-server that will launch, sizes the KV cache and largest layer from
the GGUF header, weights --tensor-split by usable free memory, adds
--pipeline-groups when the bundle has it, starts managed workers over ssh and
refuses a load no set of devices can hold.

rpc_serving holds the load hooks the inference routes call and the helpers the
planner shares (binary resolution and capability probes, GGUF and KV sizing,
launch files, ssh and peer processes). The status stays at
GET /api/inference/spark/status, the toggle at UNSLOTH_SPARK_SERVING and the
field at spark_topology, so an optional serving provider can take the hooks
over without changing them.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T16:28:04.538974Z 750cecc Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1f335ceb4a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Outdated
Comment on lines +18296 to +18300
if (
str(getattr(request, "gguf_variant", "") or "").strip().lower()
!= str(source[1] or "").strip().lower()
):
return None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Resolve model identity before deciding which extras to inherit

When a same-model reload omits a previously explicit gguf_variant, or names the Hub repo instead of its snapshot directory, this helper rejects inheritance even if the resolved GGUF is unchanged. If the planner then chooses a layer split, it populates request.llama_extra_args with its new block, causing _resolve_inherited_extra_args to return early instead of performing its existing canonical-model/resolved-variant comparison. Consequently, previously supplied context, RoPE, or chat-template arguments silently disappear on these reloads. Resolve the identity consistently with the existing inheritance helper before materializing the planned extras.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 29733d9: the planner now also inherits when the request resolves to the same cached GGUF file as the stored extras (repo id vs snapshot path, or an omitted variant that resolves to the same quant), with the canonical model identity checked first so two files sharing a stem stay distinct. It runs off the event loop. Covered by test_split_extras_follow_the_same_gguf_whatever_the_reload_names_it.

Comment on lines +869 to +871
tail = tokens[end:]
if not tail or (len(tail) == 2 and tail[0] == PIPELINE_GROUPS_FLAG):
return tokens[:start]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Strip only split blocks known to have been generated by Studio

This fallback removes a caller-authored two-device RPC configuration solely because its arguments match the hard-coded shape, without requiring any recorded managed block. For example, with _MANAGED_BLOCKS empty, --rpc 10.0.0.2:50052 --device RPC0,CUDA0 -sm layer --tensor-split 0.5,0.5 --cache-ram 0 is reduced to an empty list. Since _resolve_inherited_extra_args now invokes this stripping even when cluster serving was never configured, a subsequent same-model reload that omits extras silently loses the user's remote placement and may no longer fit locally. Require provenance rather than treating this argument pattern as necessarily Studio-owned.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 29733d9: strip_layer_split_extra_args strips only blocks the cluster recorded; the fixed two-node shape is no longer assumed to be Studio's here (two-Spark serving, which does append that block, strips its own as the provider). Covered by test_a_callers_own_rpc_placement_is_never_read_as_ours.

…ed split blocks

The planner decided which extras a reload inherits by comparing the typed
model path and variant, so a reload naming the repo instead of its snapshot,
or dropping a variant it sent before, planned without them; its split block
then made the route skip its own inheritance and the user's context and RoPE
arguments disappeared. It now also accepts a request that resolves to the same
cached GGUF file, decided off the event loop.

strip_layer_split_extra_args removed any argument list shaped like a two-node
split even when Studio never added it. Only blocks the cluster recorded are
stripped now; an installed provider may strip its own.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 590b115a82

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Outdated
if not _split_extras_share_a_source(request, source, resolved):
return None
# Our own split block, shadow-stripped as the route would, so it is never read as a caller --rpc.
return drop_managed_flags(_shadow_stripped_inherited(request, stored))[0]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Resolve the effective template before stripping inherited extras

When a same-model Gemma-4 reload omits both llama_extra_args and chat_template_override, this call retains a previously supplied --chat-template-file because it does not pass the effective bundled template to _shadow_stripped_inherited. If the planner chooses a layer split, it materializes those inherited arguments into request.llama_extra_args, so _resolve_inherited_extra_args subsequently returns early and never performs its existing template stripping. The stale extra then overrides the bundled template at launch, changing prompt formatting and potentially breaking thinking/tool behavior. Resolve and pass the effective template before materializing the inherited extras, or preserve the later inheritance normalization.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 981d700: _split_inherited_extra_args now resolves the effective chat template (the user override, else the bundled gemma-4 template) and passes it to _shadow_stripped_inherited, so the planner's inherited extras are stripped exactly as the route would strip them, including the stale --chat-template-file. Covered by test_split_extras_drop_a_template_file_the_bundled_gemma4_template_replaces, which fails on the previous head.

The planner's inherited extras replace the route's own inheritance, so they
must be stripped the same way. Without the effective template, a gemma-4
reload kept a stale --chat-template-file that then overrode the bundled
template at launch.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 981d700079

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

f"a micro-batch of {min(batch, ubatch)} is placed by the caller",
)
return request
model_bytes, kv_bytes, local_extra = await self._size(request, n_parallel, flags, ss)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Size the split using the effective parallel-slot count

When a hybrid/recurrent GGUF load supplies a larger slot count through llama_extra_args than through request.n_parallel or the app default, _size still receives only the latter count. It consequently underestimates per-sequence recurrent state, checkpoints, and slot-dependent compute buffers, while the larger override survives into the launch. The planner can therefore approve a split that runs out of memory during loading. Resolve the effective slot count before sizing, or decline automatic placement when the override differs, as the existing local placement paths do with _extra_args_n_parallel.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in c17d788: the planner reads the slot count that will launch (an extras -np/--parallel, inline or not, else LLAMA_ARG_N_PARALLEL) with the same _extra_args_n_parallel the local placement uses, and sizes recurrent state, checkpoints and compute for it. Covered by test_the_split_is_sized_for_the_slot_count_that_launches (3 of its 4 cases fail on the previous head).

An extras --parallel or LLAMA_ARG_N_PARALLEL wins at launch, but the planner
priced recurrent state, checkpoints and slot-dependent compute for
request.n_parallel, so it could approve a split that ran out of memory while
loading. It now sizes with the caller's count when one is set.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: c17d78879f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: c17d78879f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen danielhanchen added >3000# new-feature# security# Moves a security boundary: sandbox, allowlist, permission or network guard labels Nov 19, 2026
@danielhanchen danielhanchen self-assigned this Nov 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 750cecc77b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

This branch has not been deployed

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

Labels

new-feature# security# Moves a security boundary: sandbox, allowlist, permission or network guard >3000#

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant