Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions hives/dwarfstar/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out/
vendor/
95 changes: 95 additions & 0 deletions hives/dwarfstar/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# dwarfstar: the DwarfStar engine (antirez/ds4), linked into a hum worker.
#
# make out/dwarfstar-bee and the Metal kernels beside it
# make check out/dwarfstar-bee-stub: the bee with no engine, for the
# thrum side, on any machine
# make fetch the pinned ds4 source into vendor/ds4
# make clean drop out/
#
# ds4 is a C library, and a C program takes a C library by compiling its
# sources into its own objects: exactly the objects ds4's own Makefile
# links into every binary on macOS, with its flags, so the numerics match
# upstream. The source is pinned by commit and fetched once; point DS4_DIR
# at a checkout you already have and nothing is fetched. The engine's
# Metal backend is what is wired, so the bee builds on macOS.
#
# The thrum side is the C client in thrum-clients/c, through thrum.mk,
# which brings libsodium and cJSON in through pkg-config.

DS4_REPO ?= antirez/ds4
DS4_SHA ?= 5379c1c9ac315f1540371b06a3db88491d4059ff
DS4_URL ?= https://codeload.github.com/$(DS4_REPO)/tar.gz/$(DS4_SHA)
DS4_DIR ?= vendor/ds4

BUILD := out
BIN := $(BUILD)/dwarfstar-bee
STUB := $(BUILD)/dwarfstar-bee-stub

THRUM_BUILD := $(BUILD)/thrum-c
include ../../thrum-clients/c/thrum.mk

DS4_CFLAGS ?= -O3 -ffast-math -g -mcpu=native -Wall -Wextra -std=c99
DS4_OBJCFLAGS ?= -O3 -ffast-math -g -mcpu=native -Wall -Wextra -fobjc-arc
DS4_LDLIBS := -lm -pthread -framework Foundation -framework Metal
DS4_CORE := ds4 ds4_image ds4_distributed ds4_tp ds4_ssd ds4_layer_pack ds4_engram
DS4_SOURCES := $(DS4_CORE:%=$(DS4_DIR)/%.c) $(DS4_DIR)/ds4_metal.m
DS4_OBJ := $(DS4_CORE:%=$(BUILD)/ds4/%.o) $(BUILD)/ds4/ds4_metal.o

BEE_CFLAGS ?= -std=c11 -O2 -g -Wall -Wextra -Wpedantic -Wshadow
BEE_HDR := $(wildcard src/*.h) $(THRUM_HDR)

.PHONY: all check fetch clean darwin

all: $(BIN)

darwin:
@test "$$(uname -s)" = Darwin || { \
echo "dwarfstar links ds4's Metal engine, which needs macOS; make check builds the rest anywhere" >&2; \
exit 1; }

$(BIN): $(BUILD)/main.o $(BUILD)/models.o $(BUILD)/gpu.o $(BUILD)/shaders.o $(BUILD)/ds4_bridge.o $(DS4_OBJ) $(THRUM_LIB) | darwin
$(CC) -o $@ $(filter %.o,$^) $(THRUM_LIBS) $(DS4_LDLIBS)
@rm -rf $(BUILD)/metal && cp -R $(DS4_DIR)/metal $(BUILD)/metal
@echo "built $@"

check: $(STUB)

$(STUB): $(BUILD)/main.o $(BUILD)/models.o $(BUILD)/gpu.o $(BUILD)/stub/ds4_stub.o $(THRUM_LIB)
$(CC) -o $@ $(filter %.o,$^) $(THRUM_LIBS)
@echo "built $@"

$(BUILD)/main.o $(BUILD)/models.o $(BUILD)/gpu.o $(BUILD)/shaders.o: $(BUILD)/%.o: src/%.c $(BEE_HDR)
@mkdir -p $(dir $@)
$(CC) $(BEE_CFLAGS) $(THRUM_CFLAGS) -c -o $@ $<

$(BUILD)/stub/ds4_stub.o: src/ds4_stub.c $(BEE_HDR)
@mkdir -p $(dir $@)
$(CC) $(BEE_CFLAGS) -c -o $@ $<

$(BUILD)/ds4_bridge.o: src/ds4_bridge.c $(BEE_HDR) $(DS4_DIR)/ds4.h | darwin
@mkdir -p $(dir $@)
$(CC) $(BEE_CFLAGS) $(THRUM_CFLAGS) -I$(DS4_DIR) -c -o $@ $<

$(BUILD)/ds4/%.o: $(DS4_DIR)/%.c | darwin
@mkdir -p $(dir $@)
$(CC) $(DS4_CFLAGS) -I$(DS4_DIR) -I$(DS4_DIR)/metal -c -o $@ $<

$(BUILD)/ds4/ds4_metal.o: $(DS4_DIR)/ds4_metal.m | darwin
@mkdir -p $(dir $@)
$(CC) $(DS4_OBJCFLAGS) -I$(DS4_DIR) -I$(DS4_DIR)/metal -c -o $@ $<

$(DS4_SOURCES): | $(DS4_DIR)/ds4.h ;

$(DS4_DIR)/ds4.h:
@$(MAKE) --no-print-directory fetch

fetch:
@mkdir -p $(BUILD) $(DS4_DIR)
@echo "fetching $(DS4_REPO) @ $(DS4_SHA)"
@curl -fsSL "$(DS4_URL)" -o $(BUILD)/ds4.tar.gz
@tar xzf $(BUILD)/ds4.tar.gz --strip-components=1 -C $(DS4_DIR)
@rm -f $(BUILD)/ds4.tar.gz
@echo "ds4 source in $(DS4_DIR)"

clean:
rm -rf $(BUILD)
106 changes: 106 additions & 0 deletions hives/dwarfstar/Orchfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# dwarfstar: one unit per model, and each unit is its own bee.
#
# Every unit runs the same dwarfstar-bee, pinned to one GGUF and entered
# under its own label, so each model nestles with its own key, ids and
# headroom. A unit whose model is not on this machine logs so and stays
# down. The bees share the GPU through one lock: a model loads only when
# a prompt for it arrives, and hands the GPU to another model's bee when
# idle or asked. Set an ARG in the environment of
# `hum hive ./hives/dwarfstar install` to change it, for example
# DWARFSTAR_DIR=~/projects/ds4.
#
# MEMORY is the model's resident size with room for its KV cache.
# launchd records it as a soft resident-set limit and macOS does not
# enforce it; systemd enforces it as MemoryMax.

ARG HUM_SRC=
ARG DWARFSTAR_DIR=
ARG DWARFSTAR_CTX=32768
ARG DWARFSTAR_MAX_TOKENS=4096
ARG DWARFSTAR_IDLE_MS=300000
ARG DWARFSTAR_THREADS=8
ARG DS4_LOCK_FILE=/tmp/ds4.lock
ARG DS4FLASH_MODEL=ds4flash.gguf
ARG DS4FLASH_Q2_MODEL=gguf/DeepSeek-V4-Flash-IQ2XXS-w2Q2K-AProjQ8-SExpQ8-OutQ8-chat-v2-imatrix-0731.gguf
ARG DS41FLASH_Q2_MODEL=gguf/DeepSeek-V4.1-Flash-1017-IQ2_XXS-Q2_K-imatrix.gguf
ARG GLM53FLASH_Q2_MODEL=gguf/GLM-5.3-Flash-Q2.gguf

SERVICE hive-install
RUN ${HUM_SRC}/install
ONESHOT true

SERVICE ds4flash
RUN nice -n 10 ${HUM_SRC}/out/dwarfstar-bee
ENV DWARFSTAR_LABEL=ds4flash
ENV DWARFSTAR_MODEL=${DS4FLASH_MODEL}
ENV DWARFSTAR_MODELS=deepseek-v4-flash
ENV DWARFSTAR_DIR=${DWARFSTAR_DIR}
ENV DWARFSTAR_CTX=${DWARFSTAR_CTX}
ENV DWARFSTAR_MAX_TOKENS=${DWARFSTAR_MAX_TOKENS}
ENV DWARFSTAR_IDLE_MS=${DWARFSTAR_IDLE_MS}
ENV DWARFSTAR_THREADS=${DWARFSTAR_THREADS}
ENV DWARFSTAR_SSD_STREAMING=1
ENV DS4_LOCK_FILE=${DS4_LOCK_FILE}
MEMORY 104G
RESTART on-failure
RESTART_DELAY 5s
START_LIMIT_BURST 3
START_LIMIT_INTERVAL 1m
TIMEOUT_STOP 30s

SERVICE ds4flash-q2
RUN nice -n 10 ${HUM_SRC}/out/dwarfstar-bee
ENV DWARFSTAR_LABEL=ds4flash-q2
ENV DWARFSTAR_MODEL=${DS4FLASH_Q2_MODEL}
ENV DWARFSTAR_MODELS=deepseek-v4-flash-q2
ENV DWARFSTAR_DIR=${DWARFSTAR_DIR}
ENV DWARFSTAR_CTX=${DWARFSTAR_CTX}
ENV DWARFSTAR_MAX_TOKENS=${DWARFSTAR_MAX_TOKENS}
ENV DWARFSTAR_IDLE_MS=${DWARFSTAR_IDLE_MS}
ENV DWARFSTAR_THREADS=${DWARFSTAR_THREADS}
ENV DWARFSTAR_SSD_STREAMING=1
ENV DS4_LOCK_FILE=${DS4_LOCK_FILE}
MEMORY 96G
RESTART on-failure
RESTART_DELAY 5s
START_LIMIT_BURST 3
START_LIMIT_INTERVAL 1m
TIMEOUT_STOP 30s

SERVICE ds41flash-q2
RUN nice -n 10 ${HUM_SRC}/out/dwarfstar-bee
ENV DWARFSTAR_LABEL=ds41flash-q2
ENV DWARFSTAR_MODEL=${DS41FLASH_Q2_MODEL}
ENV DWARFSTAR_MODELS=deepseek-v4.1-flash,deepseek-v4.1-flash-q2
ENV DWARFSTAR_DIR=${DWARFSTAR_DIR}
ENV DWARFSTAR_CTX=${DWARFSTAR_CTX}
ENV DWARFSTAR_MAX_TOKENS=${DWARFSTAR_MAX_TOKENS}
ENV DWARFSTAR_IDLE_MS=${DWARFSTAR_IDLE_MS}
ENV DWARFSTAR_THREADS=${DWARFSTAR_THREADS}
ENV DWARFSTAR_SSD_STREAMING=1
ENV DS4_LOCK_FILE=${DS4_LOCK_FILE}
MEMORY 104G
RESTART on-failure
RESTART_DELAY 5s
START_LIMIT_BURST 3
START_LIMIT_INTERVAL 1m
TIMEOUT_STOP 30s

SERVICE glm53flash-q2
RUN nice -n 10 ${HUM_SRC}/out/dwarfstar-bee
ENV DWARFSTAR_LABEL=glm53flash-q2
ENV DWARFSTAR_MODEL=${GLM53FLASH_Q2_MODEL}
ENV DWARFSTAR_MODELS=glm-5.3-flash,glm-5.3-flash-q2
ENV DWARFSTAR_DIR=${DWARFSTAR_DIR}
ENV DWARFSTAR_CTX=${DWARFSTAR_CTX}
ENV DWARFSTAR_MAX_TOKENS=${DWARFSTAR_MAX_TOKENS}
ENV DWARFSTAR_IDLE_MS=${DWARFSTAR_IDLE_MS}
ENV DWARFSTAR_THREADS=${DWARFSTAR_THREADS}
ENV DWARFSTAR_SSD_STREAMING=1
ENV DS4_LOCK_FILE=${DS4_LOCK_FILE}
MEMORY 104G
RESTART on-failure
RESTART_DELAY 5s
START_LIMIT_BURST 3
START_LIMIT_INTERVAL 1m
TIMEOUT_STOP 30s
140 changes: 140 additions & 0 deletions hives/dwarfstar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
---
title: "dwarfstar"
description: "The DwarfStar engine linked into hum worker bees, one bee per model, sharing one GPU"
---

# dwarfstar

> _Worker bees with the DwarfStar engine inside them, one per model. A prompt tone goes into the model's token stream, and the answer comes back as chunk tones._

[DwarfStar](https://github.com/antirez/ds4) (ds4) is a narrow, fast
local inference engine for a few open-weight models: DeepSeek V4 and
V4.1 Flash, GLM 5.2 and 5.3, Qwen3.8 Flash Next and DeepSeek V4 PRO. It
is a C library with a CLI, a server and an agent on top.

This hive takes the library, not the server. The bee compiles the engine
from a pinned upstream commit and links it in, and speaks thrum through
the [C client](../../thrum-clients/c). Nothing is serialised to HTTP and
back: the engine's session, KV cache and token stream are the bee's own.

## Install

```sh
brew install pkgconf libsodium cjson
DWARFSTAR_DIR=~/ds4 hum hive ./hives/dwarfstar install
```

`install` builds the bee and grows every unit of the Orchfile under
orchd, in the `hum-dwarfstar` namespace. Each unit's bee enters itself
under the hive, as the unit's name, when it starts. A unit whose model is
not on this machine logs so and stays down. It builds on macOS, where
ds4's Metal backend is what it links.

## One unit, one model, one bee

| unit | model file (ARG) | answers to |
|---|---|---|
| `ds4flash` | `DS4FLASH_MODEL`, ds4's own default link `ds4flash.gguf` | `deepseek-v4-flash` |
| `ds4flash-q2` | `DS4FLASH_Q2_MODEL` | `deepseek-v4-flash-q2` |
| `ds41flash-q2` | `DS41FLASH_Q2_MODEL` | `deepseek-v4.1-flash`, `deepseek-v4.1-flash-q2` |
| `glm53flash-q2` | `GLM53FLASH_Q2_MODEL` | `glm-5.3-flash`, `glm-5.3-flash-q2` |

Every unit runs the same `dwarfstar-bee`, pinned with `DWARFSTAR_MODEL`
and `DWARFSTAR_MODELS`, so each model is its own participant: its own
key, its own ids in its hello, its own headroom, and `hum bee <id> exit`
takes one model off the mesh and leaves the rest. A model file named
differently on your machine is one ARG at install, for example
`DS41FLASH_Q2_MODEL=gguf/DeepSeek-V4.1-Flash-Q2.gguf`. Another model is
another unit.

Each unit carries its resources in the Orch spec: `MEMORY` (the model's
resident size with room for its KV cache), `nice -n 10`, SSD streaming
and threads as ds4-runner set them, `RESTART on-failure` with a start
limit, and a stop timeout. launchd records `MEMORY` as a soft
resident-set limit that macOS does not enforce; systemd enforces it as
`MemoryMax`. On a Mac, what keeps memory in hand is that one model is on
the GPU at a time.

## One GPU, handed between bees

A bee loads its model only when a prompt for it arrives. ds4 exits when
a second process opens the GPU, so the bees take a hive-wide lock first
(`$XDG_STATE_HOME/hum/hives/dwarfstar/gpu.lock`) and hold it for as long
as their engine is loaded:

- A bee that needs the GPU while another holds it files a want beside the
lock and waits up to `DWARFSTAR_WAIT_MS`. A cancel ends the wait.
- The holder gives the GPU back when it is idle and someone wants it,
after its current turn when a want has waited a minute, and on its own
after `DWARFSTAR_IDLE_MS` idle.
- Loading also checks ds4's own lock (`DS4_LOCK_FILE`), so a ds4 started
outside the hive is an error on the turn, not a dead bee.
- Each bee claims one slot of headroom while the GPU is its own or free,
and none while another model holds it, so a peer routes to the model
that can start now. A local prompt still runs; it waits for the
handoff. The claim changes with a `pulse` only when it changes.

## How a turn runs

- The engine keeps one session while it is loaded. Every turn syncs it
to the turn's whole prompt, and ds4 evaluates only what the KV cache
does not already hold.
- Turns run one at a time, in order. A `cancel` stops its sid's turn,
waiting or in flight, and drops its sid's queued turns, each answered
with `finish` `cancelled`.
- A turn ends in `finish` with `stop`, `length` (`DWARFSTAR_MAX_TOKENS`
or the context is full), `cancelled`, or `error` after an `error` tone.
- Chunks carry whole UTF-8 characters, even when a token ends inside one.
- The bee is stateless: a prompt's `text`, or its `content` petals, are
the conversation. Petals with roles become chat turns.

## Build

```sh
make # fetch the pinned ds4 source if needed, then build out/dwarfstar-bee
make DS4_DIR=~/src/ds4 # build against a ds4 checkout you already have
make check # out/dwarfstar-bee-stub: the bee with no engine, on any machine
```

The engine objects are exactly the ones ds4's own Makefile links on
macOS, compiled with its flags, so the numerics match upstream. The
`.metal` kernels are copied beside the bee, which points ds4 at them, so
the bee runs from any directory. The stub runs the thrum side and the GPU
handoff with no model: several stub bees on one lock trade the GPU the
way the real ones do.

## Environment

| name | meaning | default |
|---|---|---|
| `DWARFSTAR_MODEL` | pin the bee to this GGUF, absolute or under `DWARFSTAR_DIR` | every model under `DWARFSTAR_DIR` |
| `DWARFSTAR_MODELS` | the ids a pinned bee answers to, comma separated | earned from the file name |
| `DWARFSTAR_LABEL` | the label the bee enters as | `main` |
| `DWARFSTAR_DIR` | where the GGUFs live | `~/ds4` |
| `DWARFSTAR_CTX` | context size in tokens | `32768` |
| `DWARFSTAR_MAX_TOKENS` | cap on one turn | `4096` |
| `DWARFSTAR_TEMPERATURE` | sampling temperature | ds4's default |
| `DWARFSTAR_THREADS` | engine CPU threads | ds4's default |
| `DWARFSTAR_SSD_STREAMING` | `1` streams experts from SSD | off |
| `DWARFSTAR_IDLE_MS` | give the GPU back after this long idle | `300000` |
| `DWARFSTAR_WAIT_MS` | how long a turn waits for the GPU | `300000` |
| `DWARFSTAR_GPU_LOCK` | the hive-wide GPU lock | under the hive's state dir |
| `DWARFSTAR_METAL_DIR` | the `.metal` kernels | `metal/` beside the bee |
| `DS4_LOCK_FILE` | ds4's own lock | `/tmp/ds4.lock` |

## What is not here

- **The lattice.** ds4 can split a model across machines; hum's part
would be finding the bee that holds the missing layers and vouching
for it, which needs a capability claim with proof of possession.
- **Two models resident at once.** The handoff assumes one model fits;
a machine that holds two would need the lock to count, not exclude.
- **Vision, tools and thinking.** The engine has them; the bee does not
use them yet.
- **CUDA.** ds4 builds for CUDA on Linux; this Makefile wires Metal only.

## Licence

The engine is MIT, © Salvatore Sanfilippo, and credits llama.cpp and
GGML for the quantisation formats and kernels it inherits. Its
`LICENSE` travels with the fetched source.
11 changes: 11 additions & 0 deletions hives/dwarfstar/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# Build this hive's bee: fetch the pinned ds4 source if it is not here,
# compile the engine into the bee, and put the Metal kernels beside it.
# Nothing is installed into the system's PATH: the bee is built here and
# runs here. DS4_DIR points at a ds4 checkout to build against instead.
set -euo pipefail

here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$here"

make
26 changes: 26 additions & 0 deletions hives/dwarfstar/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
# Install this hive on a humd: build its bee and keep one unit per model
# running under orchd. Each unit's bee enters itself under the hive, as
# the unit's name, when it starts; a unit whose model is not on this
# machine stays down.
set -euo pipefail

here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
hive="${HUM_HIVE:-$(basename "$here")}"
state="${XDG_STATE_HOME:-$HOME/.local/state}/hum/hives/$hive"
orchd="$(command -v orchd || echo "$HOME/.local/bin/orchd")"
mkdir -p "$state"

"$here/build"

args=(--arg "HUM_SRC=$here")
while read -r name; do
if [ -n "${!name:-}" ]; then args+=(--arg "$name=${!name}"); fi
done < <(sed -n 's/^ARG \([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$here/Orchfile" | grep -vx -e HUM_SRC -e HUM_HIVE)

units=()
while read -r unit; do units+=("$unit"); done < <(sed -n 's/^SERVICE \([A-Za-z0-9_.-]*\)$/\1/p' "$here/Orchfile" | grep -vx hive-install)

run() { "$orchd" --project-dir "$here" --namespace "hum-$hive" --user --state-dir "$state/orch" "${args[@]}" "$@"; }
run fell >/dev/null 2>&1 || true
run grow "${units[@]}"
Loading
Loading