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
23 changes: 23 additions & 0 deletions hives/dwarfstar/LICENSE-ds4
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MIT License

Copyright (c) 2026 The ds4.c authors
Copyright (c) 2023-2026 The ggml authors
Copyright (c) 2023 DeepSeek

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
52 changes: 29 additions & 23 deletions hives/dwarfstar/Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
# dwarfstar: the DwarfStar engine (antirez/ds4), linked into a hum nurse.
#
# 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 cJSON in through pkg-config.

DS4_REPO ?= antirez/ds4
DS4_SHA ?= 5379c1c9ac315f1540371b06a3db88491d4059ff
DS4_URL ?= https://codeload.github.com/$(DS4_REPO)/tar.gz/$(DS4_SHA)
Expand All @@ -24,6 +6,8 @@ DS4_DIR ?= vendor/ds4
BUILD := out
BIN := $(BUILD)/dwarfstar-bee
STUB := $(BUILD)/dwarfstar-bee-stub
TESTS := $(BUILD)/test-chat
CONFORM := $(BUILD)/conform-with-ds4

THRUM_BUILD := $(BUILD)/thrum-c
include ../../thrum-clients/c/thrum.mk
Expand All @@ -34,11 +18,13 @@ 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
DS4_SERVER_OBJ := $(BUILD)/ds4/ds4_help.o $(BUILD)/ds4/ds4_kvstore.o $(BUILD)/ds4/rax.o

BEE_CFLAGS ?= -std=c11 -O2 -g -Wall -Wextra -Wpedantic -Wshadow
BEE_HDR := $(wildcard src/*.h) $(THRUM_HDR)
BEE_OBJ := $(BUILD)/main.o $(BUILD)/chat.o $(BUILD)/models.o $(BUILD)/gpu.o

.PHONY: all check fetch clean darwin
.PHONY: all check test conform fetch clean darwin

all: $(BIN)

Expand All @@ -47,24 +33,44 @@ 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
$(BIN): $(BEE_OBJ) $(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)
$(STUB): $(BEE_OBJ) $(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)
test: $(TESTS)
@$(TESTS)

$(TESTS): $(BUILD)/tests/test_chat.o $(BUILD)/chat.o
$(CC) -o $@ $^ $(THRUM_DEPS_LIBS)

conform: $(CONFORM)
@$(CONFORM)

$(CONFORM): $(BUILD)/tests/conform_with_ds4.o $(BUILD)/tests/ds4_reference.o $(BUILD)/chat.o $(DS4_SERVER_OBJ) $(DS4_OBJ) | darwin
$(CC) -o $@ $(filter %.o,$^) $(THRUM_DEPS_LIBS) $(DS4_LDLIBS)

$(BEE_OBJ) $(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 $@ $<
$(CC) $(BEE_CFLAGS) $(THRUM_CFLAGS) -c -o $@ $<

$(BUILD)/tests/test_chat.o $(BUILD)/tests/conform_with_ds4.o: $(BUILD)/tests/%.o: tests/%.c src/chat.h tests/ds4_reference.h
@mkdir -p $(dir $@)
$(CC) $(BEE_CFLAGS) $(THRUM_CFLAGS) -Isrc -c -o $@ $<

$(BUILD)/tests/ds4_reference.o: tests/ds4_reference.c tests/ds4_reference.h $(DS4_DIR)/ds4_server.c | darwin
@mkdir -p $(dir $@)
$(CC) $(DS4_CFLAGS) -Wno-unused-function -I$(DS4_DIR) -I$(DS4_DIR)/metal -c -o $@ $<

$(BUILD)/ds4_bridge.o: src/ds4_bridge.c $(BEE_HDR) $(DS4_DIR)/ds4.h | darwin
@mkdir -p $(dir $@)
Expand Down
15 changes: 0 additions & 15 deletions hives/dwarfstar/Orchfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
# 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 HUM_HIVE_STATE=
ARG DWARFSTAR_DIR=
Expand Down
115 changes: 91 additions & 24 deletions hives/dwarfstar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ and token stream are the bee's own.

| statefulness | richness | wire | hides |
|---|---|---|---|
| stateless | lean | `dwarfstar/core` | tendrils, sepals, pollen, and how a prompt asks its bloom to grow |
| stateless | medium | `dwarfstar/core` | sepals, pollen, and how a prompt asks its bloom to grow |

Each bee is a nurse and speaks `hello`, `prompt`, `cancel`, `unfurl`,
`wilt`, `error` and `pulse`.
`wilt`, `error`, `pulse`, `tendril-reach`, `tendril-result` and
`tendril-info`.

## Install

Expand Down Expand Up @@ -115,48 +116,102 @@ as their engine is loaded:
does not already hold.
- The bee is stateless: a prompt's `graft` is the turns before it (the
text petals of each bloom's `content` as the user's turn, of its
`grew` as the model's), and its own text petals, else its `text`, the
turn it asks for. `systemPrompt` is the system turn. Other petals are
dropped.
`grew` as the model's, its tendrils as the model's tool calls and its
grasps as what they came back with), and its own text petals, else its
`text`, the turn it asks for. `systemPrompt` is the system turn. Other
petals are dropped.
- A prompt for a model the bee does not answer to is refused with
`no_such_model`, one with no words with `empty-prompt`, and one whose
only words are in its graft with `cannot-grow-on`, since the engine
grows only from new words. A refusal is an `error` with no wilt.
`no_such_model`, and one with no words with `empty-prompt`. One with no
new words grows on when its graft ends in grasps, the model answering
what its tendrils grasped, and is otherwise refused with
`cannot-grow-on`, since the engine grows only from new words. A refusal
is an `error` with no wilt.
- Blooms grow one at a time, in order. A `cancel` stops its sid's bloom,
waiting or in flight, and drops its sid's queued blooms, each wilting
`cancel`.
waiting, in flight or awaiting its grasps, and drops its sid's queued
blooms, each wilting `cancel`.
- Thinking is off, and every bloom samples at `DWARFSTAR_TEMPERATURE`
with ds4's own top-p and min-p, up to `DWARFSTAR_MAX_TOKENS`. A
prompt's own `temperature`, `prune`, `season`, `frost`, `seed`,
`trellis`, `sepal`, `scion` and `tools` are not read.
- The answer unfurls as one `text` petal. An unfurl carries whole UTF-8
characters, even when a token ends inside one.
- A bloom wilts with `stop`, `max_tokens` (`DWARFSTAR_MAX_TOKENS` or
the context is full), `cancel`, or `error` after an `error` tone
(`gpu_busy`, `engine_unavailable`, `generation_failed`), and its
nectar is the tokens in and out.
`trellis`, `sepal` and `scion` are not read.
- The answer unfurls as `text` petals, each closed once it is whole. An
unfurl carries whole UTF-8 characters, even when a token ends inside
one.
- A bloom wilts with `stop`, `max_tokens` (`DWARFSTAR_MAX_TOKENS` across
the whole bloom, or the context is full), `cancel`, or `error` after an
`error` tone (`gpu_busy`, `engine_unavailable`, `generation_failed`,
`tendril_unanswered`), and its nectar is the tokens in and out, summed
over its rounds.
- The bee outlives humd: when humd restarts, it reconnects and nestles
again with its model still loaded, dropping the blooms the old humd
gave. Only a refusal, or its link failing, ends the bee, and its unit
restarts it.

## Tendrils

A prompt's `tools` are handed to the model in its own family's template,
the way ds4's server renders them, so the model can reach for them:

| family | how it is told its tools | how it reaches |
|---|---|---|
| DeepSeek V4 | a `## Tools` section after the system words, one JSON schema a line | a DSML `<|DSML|tool_calls>` block |
| DeepSeek V4.1 | the same preamble in a `<|System|>` turn | a DSML `<|DSML| calls>` block |
| GLM | a system turn listing `<tools>` | `<tool_call>` with `<arg_key>` and `<arg_value>` |
| Qwen | the system turn's `<tools>` | `<tool_call>` with `<function=…>` and `<parameter=…>` |

The bee asks the engine which family a model is when it loads. A bloom
with tools, or with tendrils or grasps in its graft, is rendered whole in
that template and tokenized as it stands; a bloom with neither is
encoded as it always was.

A bloom with tendrils grows in rounds:

- The model's words unfurl as they grow, held back only where a call
might be starting, and never the whitespace before one.
- When the model stops on a call, the bee reads it out of what grew,
mints a `callId` for each, unfurls each as a closed `tendril` petal
with its arguments as `sap`, and reaches with `tendril-reach`. humd
routes each reach: `bash` to the builder where the bloom builds, an
attendant's tool to that attendant, the forager's own tool back to the
forager.
- While its reaches are out, the bloom is parked and the engine grows the
next bloom in the queue, so a client that takes a tool call away and
never answers holds nothing up.
- Each `tendril-result` is told to the forager as `tendril-info`. Once
every reach has come back, the bloom goes to the front of the queue and
grows its next round from the whole conversation, its own calls
rendered exactly as it sampled them so the KV cache still holds them.
- A bloom whose reaches have not all come back within
`DWARFSTAR_REACH_MS` breaks off with `tendril_unanswered`. A new prompt
on the sid lets a parked bloom go.

`allowedTools` narrows the tools the model is told of, and a `tendrils`
whose `reach` is `none` tells it of none. A model that writes an argument
without saying whether it is a string, as GLM and Qwen do, has it read as
JSON unless the tool's schema says the argument is a string, or it is
not JSON.

## 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
make test # the chat templates and the tool-call readers, on any machine
make conform # the same, held to ds4's own server code at the pinned commit
make fetch # just fetch the pinned ds4 source into vendor/ds4
make clean # drop out/
```

`install` runs `build`, which runs `make`. 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.
flags, so the numerics match upstream. ds4 compiles its Metal kernels at
runtime from `.metal` files; they are copied beside the bee, and the bee
points every one of ds4's `DS4_METAL_*_SOURCE` overrides at its copy by
absolute path, so it runs from any directory, as it must under orchd.
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. Offered
tools, it reaches for the one its prompt names, and told what came back,
it says so, so the tendril rounds run with no model too.

## Environment

Expand All @@ -173,6 +228,7 @@ do.
| `DWARFSTAR_SSD_STREAMING` | stream experts from SSD, unless `0` | off |
| `DWARFSTAR_IDLE_MS` | give the GPU back after this long idle | `300000` |
| `DWARFSTAR_WAIT_MS` | how long a bloom waits for the GPU | `300000` |
| `DWARFSTAR_REACH_MS` | how long a parked bloom waits for its reaches to come back | `1800000` |
| `DWARFSTAR_GPU_LOCK` | the hive-wide GPU lock | none: the bee exits without it |
| `DWARFSTAR_METAL_DIR` | the `.metal` kernels | `metal/` beside the bee |
| `DS4_LOCK_FILE` | ds4's own lock | `/tmp/ds4.lock` |
Expand All @@ -193,8 +249,11 @@ under the hive's units.
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.
- **Vision and thinking.** The engine has them; the bee does not use
them.
- **Forcing a reach.** A `tendrils` whose `reach` is `required` or names
a tool, and its `parallel`, are not read: the model reaches as it
chooses.
- **CUDA and ROCm.** ds4 builds for both; this Makefile wires Metal only.

## Licence
Expand All @@ -203,3 +262,11 @@ The engine is MIT, under the notices in its `LICENSE`: the ds4.c
authors, DeepSeek, and the ggml authors, for the quantisation formats
and kernels it adapts from llama.cpp and GGML. The `LICENSE` travels with
the fetched source.

`src/chat.c` is ported from ds4's `ds4_server.c` at the pinned commit:
its chat templates, the tool preambles, and the readers of the calls a
model writes. It lives in this repository, not in the fetched source, so
ds4's notice is kept beside it in [`LICENSE-ds4`](LICENSE-ds4), and it is
MIT under that notice. `make conform` holds the port to ds4: it compiles
ds4's own server code beside it and checks that both render the same
prompts and read the same calls.
4 changes: 0 additions & 4 deletions hives/dwarfstar/build
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#!/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)"
Expand Down
4 changes: 0 additions & 4 deletions hives/dwarfstar/install
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#!/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)"
Expand Down
Loading
Loading