Skip to content

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

Open
danielhanchen wants to merge 11 commits into
cluster/layer-splitfrom
cluster/pairing
Open

danielhanchen wants to merge 11 commits into
cluster/layer-splitfrom
cluster/pairing

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Stacked on #18277 (n-node serving). This replaces #14274, rebuilt on the Spark-free stack (#18276, #18277) with the same content. 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.
  • Before using the native endpoint, the master opens a TCP connection to the worker's TLS port itself, because the worker only checked that port locally. A firewalled port sends the worker back to its relay.
  • 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 the original #14274 was 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.

unsloth cluster pair on the master prints a one-time code; unsloth worker --join
on another machine enrols it. Pairing issues each worker a certificate from a
cluster CA kept on the master, and every connection after that is mutual TLS
with pinned certificates: the native TLS transport when the bundle's
ggml-rpc-server has it, an authenticated relay otherwise. Paired workers join
splits without a cluster.json, and cluster list / remove manage 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-22T15:10:28.244166Z 0ca8841 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: 34e92fba5f

ℹ️ 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 on lines +1420 to +1421
if thing is not None:
thing.stop()

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 Close native sessions before discarding the relay manager

If the master identity is reset while native-session revocation is still pending—for example, removing the workers and then running unsloth cluster leave --yes—the next status refresh reaches this cleanup through _relays_locked(). However, RelayManager.stop() only closes relay connections and cancels its tasks, including the native revocation watches; it never sends native_close. The native RPC connection bypasses those relays, and the worker still trusts the old master in its own registry, so the removed worker's live session can continue indefinitely. Close the tracked native sessions before stopping their manager, or preserve their revocation tasks until closure succeeds.

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 34e92fb: RelayManager.stop() now sends native_close to every node with a live native session (tracked by its revocation watch) before cancelling the watches and removing the relays, best effort with a 3 s timeout each. Covered by test_stopping_the_relays_closes_a_live_native_session, which fails on the previous relay.py.

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.

Correction: the fix commit is 19bd7e3 (the hash in my previous reply was the PR's earlier head).

Studio drops its relay manager when the master identity changes, for example
cluster leave right after cluster remove. That cancelled the revocation watch
before it sent native_close, and a native RPC session bypasses the relays, so
the removed worker kept serving it. stop() now closes every tracked native
session (best effort, 3 s each) before tearing the relays down.
# Conflicts:
#	studio/backend/tests/test_rpc_serving.py
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

# Conflicts:
#	studio/backend/tests/test_rpc_serving.py
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 05a78eaeab

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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 36626ef75c

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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Unknown error
ℹ️ 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 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: ded54c9b0c

ℹ️ 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 on lines +1339 to +1343
transport = "native_tls" if native_port else "link",
relay = endpoint,
name = getattr(peer, "name", None),
native_port = native_port or 0,
tls_fp = str(peer.fingerprint).lower() if native_port else 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 Fall back to the relay when the native port is unreachable

If the worker's agent port is reachable but its separate native RPC port is blocked by a firewall, native_ok still succeeds because the worker only checks that RPC accepts connections locally. This code then selects the native endpoint unconditionally. _reachable() checks only the agent, and _place() drops the worker after the native device probe fails without retrying its working mTLS relay. Consequently, automatic native negotiation excludes an otherwise usable worker and can prevent models from loading. Verify the native connection before selecting it, or retry through the relay when the native probe fails.

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 a2f732c. After native_ok, the master now opens a TCP connection to the worker's TLS port from this machine (_native_port_open, REACH_TIMEOUT_S). If it cannot connect, it sends native_close and keeps the worker on its mTLS relay. A worker behind a firewall that only allows the agent port is therefore still planned. The new tls_port_firewalled case of test_native_tls_falls_back_to_the_relay_never_plaintext and a real-socket test cover this. On the tailcat PR the direct dial is skipped for tunnelled workers, because the forward is the check there.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 1c1cd4b480

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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 0ca88413e9

ℹ️ 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 self-assigned this Nov 22, 2026

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant