Skip to content

Studio cluster: pair machines on a home network, RPC over mutual TLS - #14274

Closed
danielhanchen wants to merge 118 commits into
cluster/n-node-servingfrom
cluster/home-link
Closed

danielhanchen wants to merge 118 commits into
cluster/n-node-servingfrom
cluster/home-link

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Stacked on #13726 (n-node serving). Pair machines on a home network once, then Studio splits a GGUF across them over llama.cpp RPC, with every byte between machines carried over mutually authenticated TLS. No accounts, no third party service, no internet needed.

Why

ggml-rpc-server has no authentication and accepts a single client: anyone who can reach its port can read and write tensor memory on that machine. Today the only safe setup is a trusted VPN. This PR keeps ggml-rpc-server bound to 127.0.0.1 on every worker and puts a paired, encrypted link in front of it, so a home cluster is safe on an ordinary LAN.

How it works

master                                        worker
llama-server --rpc 127.0.0.1:R1  ->  relay  ==TLS 1.3, mutual==>  agent  ->  ggml-rpc-server on 127.0.0.1
  1. Pairing, once per worker. On the master, unsloth cluster pair prints a one-time token (default 10 min). On the worker, unsloth cluster join and paste the token at the hidden prompt (or pipe it to --token-stdin). The token is never part of a command line, where other local users could read it.
    • The master creates a private cluster CA on first use (P-256). The token carries the master's certificate fingerprint and a 32 byte secret.
    • The worker connects over TLS, checks the fingerprint before sending anything, and proves it holds the secret with an HMAC over the whole handshake transcript. The secret itself never crosses the wire.
    • The worker sends a CSR; the master issues a worker certificate naming its node id and role. Both sides record the other's exact certificate fingerprint.
    • Pairing completes only when both sides have saved it: the worker persists its identity and acknowledges, then the master records the worker and confirms.
    • Tokens are single use, expire, and lock after a few wrong proofs.
  2. Serving. unsloth worker runs the agent: mutual TLS 1.3 on port 50070, and a supervised ggml-rpc-server on loopback. A connection is accepted only if its certificate chains to the cluster CA AND its fingerprint, node id and role match the registry.
  3. Studio. Paired workers become cluster nodes automatically (precedence: env, then cluster.json, then paired workers). For each worker, Studio opens a loopback relay and hands llama-server the relay's 127.0.0.1 endpoint, so llama.cpp itself is unchanged.
  4. Discovery. Workers announce on UDP multicast (239.255.77.77:50079). The master accepts announcements only from paired fingerprints, and always verifies the address over mutual TLS before using it. Discovery is only a hint.

Other commands: unsloth cluster list (status of each worker, falling back to discovery for one that moved), discover, remove <worker> (tombstoned; an open session is cut within seconds), leave (a worker leaves; a master with no workers is reset). cluster join --port records the port unsloth worker will serve on.

Native TLS path (no relay)

When the llama.cpp bundle on both ends carries ggml-rpc's own mutual TLS 1.3 transport (ggml-rpc-server --tls-cert/--tls-key/--tls-ca/--tls-allow, client endpoint tls://<server sha256>@host:port; the llama.cpp change is unslothai/llama.cpp#309, a pin candidate), Studio skips the relay:

master                                              worker
llama-server --rpc tls://<fp>@worker:50053  ==TLS 1.3, mutual==>  ggml-rpc-server (allowlist: this master)
  • The master asks a worker for native mode over the paired agent link; the worker restarts its rpc-server on the LAN with the cluster certificate, the cluster CA and an allowlist holding only that master's fingerprint. The master pins the worker's fingerprint in the endpoint and passes its own certificate through GGML_RPC_TLS_* environment variables, never argv.
  • Revocation is unchanged: the agent re-checks the master every 2 s and restarts the rpc-server (which cuts the live session) when the master is removed or the worker leaves.
  • Any refusal, an old bundle on either side, or UNSLOTH_CLUSTER_NATIVE_TLS=0 falls back to the relay; nothing ever falls back to plaintext. unsloth worker --native-port picks the TLS port (default 50053).

Details worth knowing

  • GGML_RPC_NO_RDMA=1 is set on both ends of a secure link, so RPC cannot negotiate RDMA around the TLS relay.
  • One RPC session per worker at a time, as with ggml-rpc-server itself; a second client gets an immediate "busy" instead of hanging.
  • If the agent dies, its ggml-rpc-server goes with it: PR_SET_PDEATHSIG on Linux, and on macOS and Windows the next agent stops one left behind (pid plus creation time, so a reused pid is never touched).
  • TCP keepalive on every link, so a master that loses power does not leave a worker reporting busy.
  • State lives under the Studio root (<studio>/cluster), keys are written 0600 atomically, the peer registry is file locked.
  • The worker passes Studio's library environment to ggml-rpc-server, so CUDA and ROCm builds find their runtime.

Testing

  • tests/studio/test_cluster_link_pairing.py, _relay.py, _discovery.py: token round trip, transcript binding, wrong pin, wrong secret, expiry, replay, two concurrent joins with one token, revoked worker, CA signed but unregistered certificate, worker certificate used as master, truststore injected processes, malformed and oversized frames, byte exact relay in both directions with fragmented writes, backpressure, abrupt disconnects, busy refusal, killed agent takes its rpc-server with it.
  • tests/studio/test_cluster_link_e2e.py (needs UNSLOTH_TEST_LLAMA_BIN_DIR and UNSLOTH_TEST_TINY_GGUF): a master and two paired workers with separate state directories, real llama-server and ggml-rpc-server, a forced two worker layer split through the relays. Greedy output is identical to the same split over direct RPC; an unpaired client gets nothing; the rpc-server port is not reachable from a non loopback address.
  • studio/backend/tests/test_cluster_link_serving.py: paired workers in node selection, removal, spoofed announcements, failed status, relay cleanup.
  • unsloth_cli/tests/test_cluster_cli.py: the CLI wiring and its error messages.

Per token cost vs direct RPC (Qwen3-1.7B Q4_K_M, two workers on one VM so only the link differs, 3 alternating rounds per arm, greedy text identical to direct in every arm; direct vs direct repeats within about 1%):

link Colab G4 (48 vCPU) Kaggle T4x2 (4 vCPU)
native TLS (llama.cpp transport) 1.077x 1.093x
relay, asyncio (default) 1.10 to 1.12x 1.22x
relay, threaded sessions (UNSLOTH_CLUSTER_LINK_THREADED_SPLICE=1) 1.13 to 1.20x 1.20 to 1.24x
relay without TLS (test only) 1.03 to 1.04x 1.10x

TLS itself costs about 11 us per RPC round trip (C OpenSSL, 52 KB out / 16 KB back); the relay's cost is the extra hop through Python's TLS stack, which the native transport removes.

Tried

A script drives the real unsloth CLI the way a user would, on a master and two workers with separate state: cluster pair, cluster join, unsloth worker (worker B on a different port than it paired with, so only LAN discovery can find it), cluster list and discover, then Studio's own load planner, llama-server through the relays, the same split over direct RPC, a client without a certificate, and cluster remove. All 16 steps pass:

where code GPUs relayed / direct ms per token greedy text
Linux host, CPU build head none (CPU) identical
Kaggle T4x2, fresh clone, CUDA build 74c03fb one T4 per worker 15.7 / 13.0 (1.21x) identical
Colab L4, fresh clone, CUDA build 4ddb9cc both workers on one L4 8.71 / 7.29 (1.19x) identical

On the GPU runs Studio planned a 0.5 / 0.5 layer split over the two relays from the workers' own --list-devices (both T4s, resp. the L4, reported through the link). These run on one host over loopback.

Two physical machines on one gigabit LAN (CPU builds): master and worker on separate boxes, 14 of 14 steps pass: pair, join on the worker box, discovery over real LAN multicast (also after the worker moved to another port), Studio planning onto the remote worker, serving through the link with greedy text identical to direct RPC, a client without a cluster certificate refused, and cluster remove cutting a live session within seconds.

A second LAN run (20 of 20 steps) adds the native path (TLS build on both boxes) and timing, 3 rounds of 3 generations per arm, median ms per token: relay 11.37, relay with the llama.cpp write coalescing 12.29, native TLS 11.56; the whole model on the master box alone is 4.84. Greedy text is identical on every arm. On this LAN the network dominates, so relay and native are within noise of each other. Native also passes its checks there: Studio plans tls:// endpoints, a TLS client with a certificate the cluster never issued gets no device from the worker's TLS port while the master's own certificate does, and cluster remove cuts a live native session. A plaintext direct RPC run across the LAN was not done (that would expose an unauthenticated rpc-server on the lab network), so the LAN ratio against direct RPC is not measured; on one host it is 1.077x (G4) and 1.093x (T4) above.

Cross platform CI (unit tests, real counts): Linux, macOS on Apple Silicon and Windows all pass the cluster link, cluster serving and CLI suites.

Not covered here

  • Local users on a multi-user machine: on Linux the master's loopback relay serves only clients owned by the same user (checked in /proc/net/tcp); on macOS and Windows it relies on the loopback bind alone. On a worker in relay mode, the loopback rpc-server has no authentication of its own, so another local account on that worker could reach it; native TLS mode does not have this gap.

  • A Studio UI for pairing (CLI only for now).

  • Model prefetch on workers to cut the bytes sent at load time.

Screenshots

Pairing a worker, end to end

Real runs on one host: the master is a Studio install of this PR, the worker a second identity with its own state directory, named living-room-pc. The one-time token is replaced with x in the image (same length).

Before this PR there is no cluster command:

unsloth cluster pair before this PR

On the master, unsloth cluster pair prints a one-time token and waits for the worker:

unsloth cluster pair on the master

On the worker, unsloth cluster join asks for the token at a hidden prompt (it is never on the command line), then unsloth worker serves; ggml-rpc-server stays on 127.0.0.1:

unsloth cluster join on the worker

unsloth worker on the worker

Back on the master, unsloth cluster list shows the worker as ready:

unsloth cluster list on the master

Studio then splits a model across the paired worker and names it while loading and once loaded:

Load toast: starting split across living-room-pc

Load toast: sending layers to living-room-pc

Loaded toast: split across living-room-pc

With the worker stopped, a load says which paired worker was left out and how to bring it back:

Model loaded with a warning: living-room-pc not reachable

The same notice when the worker is stopped in the middle of a session, after a split load and a reload:

Model loaded with a warning after the worker stopped mid-session

unsloth cluster remove unpairs it:

unsloth cluster remove

unsloth cluster list after remove

Same split, before and after this PR

Studio's chat page, BEFORE this PR (6e46fdd1c, the #13726 head this PR is based on) and AFTER (this PR: d7c36804e for the first three, c25543521 for the lost-connection pair). Both sides are separate install.sh --local builds loading the same model the same way: Qwen3-0.6B Q4_K_M, layer split onto one CPU rpc-server on the same host through the #13726 node list (UNSLOTH_CLUSTER_NODES, UNSLOTH_CLUSTER_TOPOLOGY=layer_split). That worker comes from the node list, so it is named by its address; a worker paired with unsloth cluster pair is shown by its name.

While the model server starts: BEFORE says it is loading a cached model into memory, AFTER says the server is starting split across the worker.

Load toast while the model server starts, before and after

While the layers go to the worker: BEFORE shows a byte counter and an ETA with nothing about the split, AFTER says the layers are being sent to the worker and warmed up.

Load toast while the layers are sent, before and after

Loaded toast: AFTER adds which machines the model is split across.

Loaded toast, before and after

The worker is stopped mid-chat: BEFORE says the model server may have crashed, AFTER names the machines the model was split across and what to check.

Chat error after the worker stops, before and after

Not shown: the "Model loaded with a warning" notice for a paired worker that cannot be reached (it applies only to workers paired with unsloth cluster pair) and the refusal for a model too big for the cluster; both are covered by studio/backend/tests/test_cluster_ux.py.

…nsaction; use Registry.open and forget_identity
…eanly on SIGTERM; e2e harness no longer blocks on full pipes
… spoofed or flooded announcement cannot hide a paired worker
…usal; both arms run rpc-server with the same threads
…mutual TLS and keep only paired announcements
…losed, peers keep newer fields, a failed bind starts no rpc-server, clean pair errors, comma device lists, rpc-server gets Studio's library env
Winsock's shutdown() from another thread does not wake a recv() already
blocked on that socket, so a revoked (or half-ended) threaded session kept
the relay's local-client pump blocked until the client itself closed,
leaving the relay slot active. Wait for readability with WAIT_S first so
the pump re-checks session.aborted / the other direction's end.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 8c76e9914e

ℹ️ 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".

@NilayYadav

NilayYadav commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Tested 8c76e99 end to end with a real install.sh --local install, driving the real CLI and Studio's API. Everything below passed unless noted.

1. Four separate T4 machines (1 master + 3 workers)

Run twice: with the shipped llama.cpp bundle (relay path) and with a CUDA build of unslothai/llama.cpp#309 (native TLS path). Same outcomes in both.

Scenario Result
cluster pair / join / worker for 3 workers All paired, cluster list shows ready
Qwen3-4B Q4_K_M, default planner Stays on one GPU ("fits this machine")
Qwen3-32B Q4_K_M (18.4 GiB, does not fit one T4), relay run Auto layer split over 4 devices, ~5 GiB per GPU, answers correctly
Qwen3-4B forced split over 4 devices Text identical to single GPU
Worker killed mid-chat Next chat fails in ~2 s, cluster list shows unreachable, reload runs on the remaining 3 devices
Worker restarted Reload uses all 4 again
cluster remove during a live session Session cut immediately, reload uses the remaining workers

These were cloud GPU instances linked through TCP tunnels (I pointed peers.json at the forwarded addresses), so speed from this run is not meaningful.

2. One machine with 4 x A10, one GPU per node, loopback

  • Discovery: a worker serving on a port it did not pair with is found; cluster list shows ready (found by discovery).
  • Speed, tok/s (two runs each):
Path Qwen3-4B (2.3 GiB) Qwen3-32B (18.4 GiB)
One GPU 123–128 21.9–22.2
Split over 4, relay (bundle b12284) 88–93 20.9–21.2
Split over 4, native TLS (llama.cpp#309) 102–109 21.6–21.9
Split over 4, direct RPC (UNSLOTH_CLUSTER_NODES, #13726) 96–102 21.5–21.8

Text is identical to single GPU on every split path. On the direct path, killing a node fails the next chat and a reload runs on the remaining 3 devices.

3. One CPU-only Linux box, over its real LAN address

Master plus two workers with separate state, paired over 192.168.1.2 rather than loopback.

Scenario Result
Pair two workers over the LAN address Both paired
Discovery on the real NIC Worker on an unpaired port found at 192.168.1.2:50195, ready (found by discovery)
Qwen3-0.6B split over the two workers Loads and answers
Worker killed mid-chat Next chat fails, cluster list says unreachable (not running: start unsloth worker there), reload runs on the remaining worker
cluster remove during a live session Session cut, worker gone from the list

4. Suites

Branch Result
#10280 feature/dgx-spark-two-node 445 passed, 1 skipped (on a GPU box)
#10323 spark/serving-orchestrator 655 passed, 38 skipped
#13726 cluster/n-node-serving 1011 passed, 38 skipped
#14274 cluster/home-link 1290 passed, 39 skipped

These are the cluster and Spark test files under tests/studio, studio/backend/tests and unsloth_cli/tests, with test_cluster_link_e2e.py and test_cluster_serving_e2e.py run against real llama.cpp binaries. 37 of the skips on the last three are test_spark_offbox.py on a CPU-only image.

5. llama.cpp PRs, each against its base, CPU and a 3-GPU CUDA split

PR Result
unslothai/llama.cpp#187 --pipeline-groups Text matches base with 1 and 2 groups. On CUDA, 2 groups vs 1: 159–192 vs 109 tok/s at 8 parallel, ~400 vs ~335 at 32
unslothai/llama.cpp#191 tracer cpu_check.sh passes; trace on/off text identical; client and peers write traces with GPU events
unslothai/llama.cpp#199 weight upload Text matches base; old/new interop both ways. CUDA load 3.8 s vs 5.3 s (2.3 GiB), 21.6 s vs 35.8 s (18.4 GiB)
unslothai/llama.cpp#203 teardown abort Base aborts (SIGABRT) when the peer is killed or terminated; the PR exits 0
unslothai/llama.cpp#309 TLS transport test-rpc-tls.sh and test-rpc-multi-server.sh pass; plain-TCP text matches base; old/new interop both ways

Observations

  • The shipped bundle (b12284) has no --tls-* flags, so installs use the relay until rpc : one send per message and an optional mutual TLS 1.3 transport llama.cpp#309 is pinned.
  • When a worker dies mid-chat, the streaming chat (what the Studio UI uses) ends with a message naming the lost worker and the fix, but a non-streaming /v1/chat/completions request only gets the generic "Could not reach an upstream service".
  • On a master with no GPU, the default planner split a 0.8 GiB model across the two paired workers instead of running it locally ("0.8 GiB over 2 devices"). On GPU masters the same kind of load stays local. Flagging in case that is not intended.

@NilayYadav

Copy link
Copy Markdown
Collaborator

Follow-up: tested on two real MacBooks on home Wi-Fi, and on a GitHub macOS 15 runner.

Two MacBooks, same Wi-Fi

Master: M2 MacBook Pro. Worker: second MacBook. Both on 8c76e99 via install.sh --local --no-torch, shipped bundle (relay path).

Scenario Result
cluster pair on the master, cluster join + unsloth worker on the second Mac Paired over Wi-Fi
cluster list / cluster discover on the master mac-2 … ready, discovered at its Wi-Fi address
Qwen3-4B Q4_K_M, UNSLOTH_CLUSTER_TOPOLOGY=layer_split Split over RPC0,MTL0, --tensor-split 0.8126,0.1874 by free memory; load toast "split across this machine and mac-2"
Memory Worker's ggml-rpc-server 2.44 GiB (its 81% of the weights); master's llama-server 1.62 GiB
Speed ~21 tok/s warm over the relay (16–21 cold)
Worker killed mid-chat Partial answer kept, stream ends with "Lost connection to the model, which is split across this machine and mac-2. Check that unsloth worker is still running on mac-2, then reload the model." cluster list shows unreachable (not running: start unsloth worker there)
Worker restarted Back as ready with the same identity, no re-pairing; reload splits again

GitHub macOS 15 runner (M1, Metal)

Master and two workers on one runner, real install.sh --local.

  • Forced split over a worker plus local Metal (--device RPC0,MTL0), text identical to the unsplit load.
  • Worker agent killed with SIGKILL: its ggml-rpc-server stays behind, and the next unsloth worker cleans it up ("stopped ggml-rpc-server 5512 left by an agent that did not exit cleanly"), as described.
  • After cluster remove, a reload no longer uses the removed worker; cluster leave on a worker works.

macOS notes

  • Local Network privacy blocks the master. Until the app running the master (Terminal, VS Code, …) has Local Network access in System Settings → Privacy & Security, every connection from the master to a worker fails with "No route to host", while the worker can still connect in (so pairing works). cluster list only says unreachable (no route). It would help if that message pointed Mac users at the Local Network setting.
  • On the CI runner the worker logs cluster discovery: cannot announce ([Errno 65] No route to host). That's the runner's network, not the PR.

A dead llama-server serving a split model now returns the same
lost-connection text as the streaming chat on non-streaming
/v1/chat/completions, /v1/completions and /v1/messages, instead of the
generic upstream error.
On macOS every connection from the master to a worker fails with No
route to host until the app running Unsloth has Local Network access.
cluster list and the dropped-worker notice now say where to allow it.
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 5798f8a0f8

ℹ️ 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

Copy link
Copy Markdown
Member Author

@NilayYadav thanks for testing all of this, including the T4s, the A10 box, the LAN box and the two MacBooks on Wi-Fi. All three of your observations are fixed in 5798f8a0f:

  1. Non-streaming requests now name the lost worker. When a split worker dies, a non-streaming request gets the same "Lost connection to the model, which is split across ..." text the streaming chat shows, instead of "Could not reach an upstream service". This covers /v1/chat/completions, /v1/completions and /v1/messages.

    • The chat completions path keeps its 500 status.
    • The two passthrough paths return 502, matching the OpenAI passthrough.
    • With no split loaded, every path behaves as before.
  2. A CPU-only master keeps a model that fits its RAM on that machine. It now behaves like a GPU master:

    • a model plus its context that fits the master's RAM loads locally;
    • a model that does not fit still splits;
    • UNSLOTH_CLUSTER_TOPOLOGY=layer_split still forces the split, with backend sampling when a worker holds the output layer.

    This lives in Studio: serve one GGUF across N machines over llama.cpp RPC #13726's planner, so it is fixed there and merged up.

  3. The macOS Local Network hint. On macOS, when a worker has no route, cluster list and the "Model loaded with a warning" notice now say to allow Local Network access for the app running Unsloth (System Settings > Privacy & Security > Local Network). Other platforms are unchanged.

On the shipped bundle: until unslothai/llama.cpp#309 is in the prebuilt, installs use the relay. Your A10 numbers match what we saw: the native path was 102-109 tok/s against 88-93 for the relay at 4-way. Pinning #309 is the next step for that.

The stack (#10280 -> #10323 -> #13726 -> #14274) is also merged with current main, which resolves #10280's conflicts.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 0fdd0b0c16

ℹ️ 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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 6ccdc63b35

ℹ️ 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

Copy link
Copy Markdown
Member Author

Superseded by #18278, which has the same changes rebuilt on a stack that no longer needs the DGX Spark PRs: #18276 (ships ggml-rpc-server), then #18277 (N-node serving), #18278 (home pairing) and #18279 (pairing across networks).

#10280 and #10323 stay open as optional Spark extras stacked on top. Closing this one so review continues on #18278; the discussion here stays for reference.

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

Labels

edge case# 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.

2 participants