device.json entry
Freeweights keeps device data incatalog/overlay/device.json, one entry per model id:
stream_quants is optional and sits beside quants. Each entry is a colibri pack that keeps ram_est bytes of weights in memory and reads the rest from disk. engine is always colibri, ram_est is required, tier is chat or background, and size is the download that must be free on disk. Old clients ignore the key. Colibri entries never go inside quants, because shipped iPhone builds decode engine as a fixed list.
A model with no entry has no device data. Clients show Size not checked and no Run button.
Freeweights routes
The index is one file so a client can badge a whole catalog page without a request per model. Both routes belong to the Freeweights Worker, see Freeweights.
Hardware
GET /api/local-engine/hardware returns the device profile and the budget the fit rule reads. See Local Engine: Hardware. An engine without the route answers 404, and clients fall back to /status.
Fit spec
The rule is written once inspec/on-device/fit.md. It defines the function, the inputs, the pick, the verdict for each kind of device, the exact label and why strings, and integer arithmetic for the boundaries.
spec/on-device/fit-vectors.json (80 cases) are the test. Three ports must pass them unchanged:
When the rule changes, change the spec and the vectors first, then the three ports in the same release.
Labels:
Fits, May be slow, Tight fit, Streams from disk, Won't fit, No WebGPU, No browser build, No build for this device, Size not checked.
Stream verdict
stream means the model does not fit in memory but a colibri pack can keep part of it resident and read the rest from disk. It runs after the normal pick and only replaces no, or na when there were no candidates. ok and slow always win. unknown is returned only when both quants and stream_quants are null or empty. It needs disk_free_bytes on the budget, which only Mac arm64, Windows x64, Linux x64 and Linux arm64 hosts set. A browser, a phone, Windows Arm and an Intel Mac never get stream for themselves. They show it for another device’s budget.
A pack qualifies when engine is colibri, size <= disk_free_bytes, and ram_est fits: on unified and cpu budgets ram_est <= fast_bytes, on discrete budgets ram_est x 10 <= spill_bytes x 6 (0.6 of system RAM, video memory is not used). The pick takes the chat tier before background, then the larger size, then the name in byte order. The why text is Keeps N GB in memory, reads the rest from disk for chat and Slow. Best for jobs you leave running for background, where N is ram_est in GB with one decimal. The full rule is in the fit spec.
How fast_bytes is made, by detector:
iPhone numbers come from the iOS memory spike: measure at cold start, never assume the entitlement is present.
DeviceRuntime
Every surface describes a place that can run models with one shape. The TypeScript type extendssrc/lib/local-models/types.ts in allternit-ai. The Swift type mirrors it in Core/OnDevice/, and Gizzi uses runtime/services/localEngine.ts.
The chat model picker groups them as On this device, My other devices and Cloud.
Browser engine
browser-webgpu runs models with transformers.js (4.3.2) in a worker. Source in allternit-ai: src/lib/local-models/runtimes/transformers-webgpu.ts and .worker.ts, with the UI-side state in src/lib/on-device/browser-models.ts and the budget in browser-budget.ts. The engine is a dynamic import, so none of it is in the main bundle until a model is downloaded or used.
- Budget.
probeBrowserGpu()asks for a WebGPU adapter once per page. No adapter, or a fallback (software) adapter, giveswebgpu: falseand the fit label No WebGPU. Otherwisefast_bytesis the smaller of the cap (2 GB on a computer, 0.45 GB on a phone browser) and the adapter’smaxBufferSizeandmaxStorageBufferBindingSize. The budget kind isunified. - Plan. The install reads the model’s device entry, takes the
webgpuquants that map to a transformers.js dtype, and picks one with the sharedfit()unless a quant is named (org/name:q4f16). Every file needs a sha256 from the signed Freeweights manifest. A file without one is never fetched. The worker keeps a file only when size and sha256 match. Errors:no_webgpu,no_browser_build, andquota(shown as “Not enough storage on this device”). - Storage. Verified files live in Cache Storage under
allternit-webgpu-models-v1. The ONNX Runtime wasm (26.9 MB) is over the Cloudflare Pages 25 MiB file limit, so it is not in the build. It is fetched once from jsDelivr, checked against pinned hashes, and kept with the model files. Bumping transformers.js means updating the version and hashes. - Registry. When at least one model is stored, the browser registers a
browser-webgpuDeviceRuntime, so the chat picker lists it andstreamDeviceTurn(src/lib/on-device/device-chat.ts) can answer from it. With none stored, it unregisters and does not load the engine. Idle unload is 300 seconds. - Offline. After a download,
makeAvailableOffline()(src/lib/offline-screens.ts) saves the chat, Model Lab and engine chunks for the service worker. The app CSP allowshuggingface.co,*.hf.co(download redirects) andblob:scripts. - Checked. Chrome headless, Qwen3-0.6B
q4f16: downloaded from the real Freeweights URLs, sha256 verified, 50.8 tokens per second, replying offline on a production build. Safari: not checked.
Android bridge
Chrome inside the Android app (a Trusted Web Activity) cannot call native code directly and the app must work with no network. The spike verdict chose the Custom Tabs postMessage channel: the page asks for a message port and the native engine streams tokens to it. On a Moto g play in airplane mode it delivered 500 of 500 messages in order across 22 runs, about 3,000 to 4,000 messages a second, with a round trip near 3 ms and no permission prompt. What the channel needs:- A
helloandwelcomehandshake that the page retries, because messages sent right after the port arrives are often dropped. - Origin
android-app://plus the package, checked by the native side. - The session held app-wide, not by a launcher activity that finishes.
- Tokens buffered while the app is in the background and batched (about every 30 ms) to cut calls.
use_as_originin the site’sassetlinks.json, or the page gets no port.- A first run online, so Chrome can save its link verification.
Android engine
The native side is theondevice module of the Android app (surfaces/allternit-mobile/android/ondevice/), with llama.cpp b12414 through JNI. The page side is src/lib/local-models/providers/android-app.ts in allternit-ai, which sends hello until the app answers welcome, checks the port’s origin is exactly android-app://<this host>, then registers the android-app DeviceRuntime (place this-device, offline true).
- Budget.
min(availMem + held - threshold, 0.45 x totalMem)fromActivityManager.MemoryInfo, reported as aunifiedDeviceBudget.heldis the size of the model already loaded. The page runs the shared TypeScript fit on it. A GGUF needssize x 1.2 + 0.4 GB. - GPU policy. OpenCL is allowed only when the SoC manufacturer is QTI and the model matches an Adreno 7xx or newer part (
GpuPolicy.kt, an allow-list). On a Moto g play 2024 (Adreno 610) ggml-opencl fails inside the driver andggml_abort()kills the process, so older and unknown SoCs get the CPU. - Downloads. The system DownloadManager, any network type, with a free-space check of twice the size plus headroom (
disk_fullotherwise). - Manifest check. The Freeweights manifest signature is verified with minisign (Ed25519, with BouncyCastle, about 3 MB added to the APK because Java Ed25519 needs API 33), then each file’s sha256.
- Lifecycle. One loaded model. A notification shows “is running on this phone” with Stop. There is no foreground service (since 0.7.2): the model runs while the app is open, like Google AI Edge Gallery and PocketPal. When you switch apps, Android may pause or end the process, and the model loads again on the next message. Unload on 5 minutes idle, Stop, the page’s
unload, or a memory trim. - Checked. Moto g play 2024, CPU only, airplane mode: signature and sha256 verified on the device, chat at 13.3 tokens per second,
hello/welcomethrough the test harness. The JVM tests pass (ondevice 10, app 5). The real PWA through the Trusted Web Activity, and OpenCL on an Adreno 7xx phone: not checked.
Device sharing
A device can serve the models it runs to its owner’s other devices. The host keeps one WebSocket to cloud-api’s model-host relay (platformcmd/allternit-cloud-api/src/routes/model_hosts.rs, PR 1960). The relay forwards chat completion requests to the host as frames and streams the reply back. The model runs on the host. Protocol and routes: Model host relay.
On the app side (allternit-ai src/lib/model-host/):
store.tskeeps the shared set (runtimeId|modelName, in localStorage underallternit.modelHost.shared) and holds a host connection while at least one shared model is running. It closes the connection when the last one stops and resumes when a shared model runs again.host-client.tsandprotocol.tscarry the frames.remote.tsis the client for a model on another device.ShareWithDevices.tsxis the per-model switch in This device.- The picker lists runtimes with
place: 'other-device'under My other devices, and shows a note when signed out or when the list cannot be refreshed. - Scope: only the owner’s own hosts. A host id that is not yours answers like an offline one. Up to 8 hosts online per user.
Engine packs
An engine pack is the signed download that carries one inference engine for one platform and backend. Desktop installs the pack that matches/hardware. Packs use the runtime package format from runtime.allternit.com.
- Layout:
pack.json, licence files, and the executable (for examplebin/llama-server). Metal packs embed their shaders so they are one file. pack.jsonis a superset of the runtime manifest. The shared fields areversion,build,platform,shellApiandfiles(a sha256 and size for each). Extra fields name the pack:pack_id,engine,backend,os,arch,min_os,entrypoint,source_commit.- Signing: macOS binaries are signed with Developer ID, hardened runtime and no entitlements, and the zip is notarized. The feed pointer and each manifest carry an Ed25519 signature, checked against the public key in Desktop.
- Verification: Desktop checks the manifest signature, then every file’s sha256, before it runs anything. Files already on disk are not downloaded again.
- Install path:
<runtime>/engines/<pack_id>/<entrypoint>, resolved before any engine found on the PATH. - The Mac MLX engine is a Swift executable,
allternit-mlx-server, built from the same package as the iPhone engine, withmlx-swift_Cmlx.bundlebeside it for the Metal library.
scripts/engine-packs/.
Desktop and iPhone build notes
- Desktop:
services/local-engine/src/engine_packs/downloads signed packs (Ed25519 on the feed and manifest, sha256 per file), installs with rollback, and takes only https feeds and the production key in release builds (a dev feed and key exist in debug builds).pick.rschooses the backend from/hardware. Routes:GET /engines/packsandPOST /engines/packs/ensure(202), proxied by allternit-api at/api/local-engine/engines/packs[/ensure]; progress also appears in/statusunderengine_packs. Job states:queued,downloading,verifying,installed,failed. The runtime launches from the verified entrypoint, waits through/health503 while loading, and unloads after 5 minutes idle (stop_reason: "idle").running(mem_bytes, tok_per_sec, ctx_used, ctx_max, idle_sec, idle_unload_sec) is on/runtimesand/status. Every reply has anallternitobject (tok_per_sec,first_token_ms,ctx_used,ctx_max,mem_bytes); a stream gets it on the usage chunk whenstream_options.include_usageis set. A MLX launch without the pack answers 501. Source: platform PR 2428. Checked on an M1 Pro: 47 unit tests, allternit-api smoke boot, Metal pack installed through a dev feed, chat on :3016 at 45.9 tokens per second (Qwen3-0.6B Q4_K_M), idle unload at 5 minutes. Not checked: MLX pack and chat, Windows and Linux packs, notarization, packaged Desktop. - Mac MLX: the Swift server depends on the same
OnDeviceLLMpackage as the iPhone.Packages/OnDeviceLLM/Package.swiftpins mlx-swiftexact: 0.31.3and mlx-swift-lmexact: 2.31.3(the first mlx-swift-lm release that runs Qwen3.5); KokoroKit pins the same mlx-swift. The MLX pack build and MLX chat on the Mac: not checked. - iPhone:
Core/OnDeviceholds the Swift port of the fit rule (Fit.swift), which passes the same 65 vectors (simulator). The budget is0.75 x os_proc_available_memory()measured at cold start (OnDeviceBudget.swift); the app does not use the increased-memory entitlement.OnDeviceRuntime.swiftruns MLX (mlx-swift 0.31.3, mlx-swift-lm 2.31.3), llama.cpp (b12414,LlamaEngine.swift) and Apple Foundation Models, and unloads on background, a memory warning and after 300 s idle. On an iPhone 15: Qwen3.5-0.8B 4-bit on MLX at 58.0 tokens per second; Qwen3.5-2B is Won’t fit without the entitlement. Source: platform PR 2437 (W7 + W8). Not checked: a llama.cpp reply on the phone, and the W8 app build on the device.