modelservice

command
v0.6.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

modelservice — one copy of the weights, many processes

The shape of Apple's on-device model daemon, built from this repo's mach and iosurface bindings: a service loads "model weights" into an IOSurface once, registers a bootstrap port, and any number of client processes use them — token streams over raw mach messages, and the weights themselves attachable zero-copy via a port-passed surface.

Transport note: this is x/mach bootstrap + mach messages (the route examples/iosurface/tensorshare proved), not XPC — apple/xpc is not generated into the module yet (see design/xpc.md's gates). If it ships, this demo is its first adopter.

Two verbs:

  • generate — client sends a prompt and a reply port; the service streams tokens back one mach message each. Each token folds a strided read of the weight pages into its seed, so the weights are load-bearing: unmap them and tokens stop.
  • attach — the service mints a send right with IOSurfaceCreateMachPort and hands the whole weights surface to the client (MoveSend), which maps the same physical pages and verifies the checksum. Gigabytes cross the process boundary, zero bytes copied.

Every process prints its phys_footprint from task_info(TASK_VM_INFO) — the kernel's ledger, the number Activity Monitor and jetsam use — so the "one copy" claim carries its own evidence.

Run

go run ./examples/mach/modelservice              # orchestrated demo
go run ./examples/mach/modelservice -mib 4096    # 4 GiB of weights

The orchestrated mode spawns the service and four clients: two generating concurrently, one attaching the weights, and one SIGKILLed mid-stream — the service logs the failed send and keeps serving.

Manual mode, separate terminals:

go run ./examples/mach/modelservice -serve -service com.tmc.modelservice
go run ./examples/mach/modelservice -client -service com.tmc.modelservice -prompt "hello"
go run ./examples/mach/modelservice -attach -service com.tmc.modelservice

Verified 2026-08-11 (macOS 26.x, Apple silicon)

[service] service: weights loaded: 1024 MiB, checksum 16777215754, footprint 1032 MiB
[service] service: listening on bootstrap "com.tmc.modelservice.48303"
[attach-3] attach: verified 1024 MiB of weights in place at 4.65 GiB/s — zero bytes copied; footprint 6 MiB -> 7 MiB
demo: SIGKILLed victim-4 mid-stream
[service] service: generate #3: client vanished mid-stream (mach: mach_msg send: 0x10000003) — stream dropped, service continues
[client-1] client: "unified memory serves many masters" -> "…12 tokens…" (footprint 6 MiB)
demo: all surviving clients done; one weights copy (1024 MiB) served 2 generations + 1 attach + 1 dead client

The ledger line is the demo: the attach client reads the full 1 GiB of weights in place and its footprint moves 6 → 7 MiB. The pages are resident once, in the service, and every client sees them.

Client death is handled where it surfaces: the dead client's reply port becomes a dead name, mach_msg returns MACH_SEND_INVALID_DEST (0x10000003), and the service drops that stream and continues.

What mlx-go-lm would keep

The protocol shape (bootstrap rendezvous, reply-port streaming, surface handoff for large tensors) is exactly what an lmserve daemon needs for "N CLI tools, one resident model": swap the synthetic weights for MLX buffers and the stand-in decode for real generation. Pairs with examples/dispatch/memorysqueeze (the service is the natural place for the pressure hook) and row 12 of design/mlx-demo-ideas.md (a service that forks sessions).

Documentation

Overview

Command modelservice demonstrates one resident copy of model weights serving many client processes — the shape of Apple's own on-device model daemon, built from this repo's mach and iosurface bindings.

The service loads its "weights" (a deterministic float32 tensor) into an IOSurface once, registers a bootstrap port, and serves two verbs over raw mach messages:

  • generate: the client sends a prompt and a reply port; the service streams tokens back one message at a time. Every token is derived by reading a strided walk of the weight pages, so the weights are load-bearing, not decoration.
  • attach: the service hands the client the weights surface's mach port. The client maps the same physical pages and verifies the checksum — gigabytes of weights cross process boundaries with zero bytes copied.

Every process prints its phys_footprint from task_info(TASK_VM_INFO), the kernel's own ledger, so the "one copy" claim carries its evidence.

The default mode orchestrates the whole demo: it spawns the service and three clients (two generating concurrently, one attaching the weights), then SIGKILLs a fourth client mid-stream and shows the service shrugging it off.

go run ./examples/mach/modelservice              # orchestrated demo
go run ./examples/mach/modelservice -mib 4096    # 4 GiB of weights

Manual mode, separate terminals:

go run ./examples/mach/modelservice -serve -service com.tmc.modelservice
go run ./examples/mach/modelservice -client -service com.tmc.modelservice -prompt "hello"
go run ./examples/mach/modelservice -attach -service com.tmc.modelservice

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL