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
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,21 @@ FetchContent_Declare(httplib
GIT_TAG fe332fa06bac76a1c6d402c08f414052999347da) # v0.47.0
FetchContent_MakeAvailable(json httplib)

add_executable(onebit app/main.cpp app/serve.cpp app/recipes.cpp)
add_executable(onebit app/main.cpp app/serve.cpp app/recipes.cpp app/pm.cpp)
# config/recipes.json (tuned llama-server settings per model and route, docs/recipes.md) is
# compiled in as serve's default recipes; --recipes replaces them at run time.
file(READ ${CMAKE_SOURCE_DIR}/config/recipes.json ONEBIT_RECIPES_JSON)
file(WRITE ${CMAKE_BINARY_DIR}/generated/recipes_default.h
"// generated from config/recipes.json by CMakeLists.txt\n#pragma once\n"
"static const char kDefaultRecipesJson[] = R\"ONEBITRECIPES(${ONEBIT_RECIPES_JSON})ONEBITRECIPES\";\n")
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/config/recipes.json)
# config/pm-experts.json (the Project Manager's experts, docs/pm.md) is compiled in the same way;
# `serve --pm-experts FILE` replaces it at run time.
file(READ ${CMAKE_SOURCE_DIR}/config/pm-experts.json ONEBIT_PM_EXPERTS_JSON)
file(WRITE ${CMAKE_BINARY_DIR}/generated/pm_experts_default.h
"// generated from config/pm-experts.json by CMakeLists.txt\n#pragma once\n"
"static const char kDefaultPmExpertsJson[] = R\"ONEBITPM(${ONEBIT_PM_EXPERTS_JSON})ONEBITPM\";\n")
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${CMAKE_SOURCE_DIR}/config/pm-experts.json)
target_include_directories(onebit PRIVATE ${CMAKE_BINARY_DIR}/generated)
# The GGUF architectures the pinned llama.cpp fork builds a model for (registry/architectures.json,
# gguf_architectures.hrx, from tools/registry_build.py): serve refuses any other on hrx and cpu.
Expand All @@ -91,6 +98,7 @@ add_test(NAME smoke_serve COMMAND ${CMAKE_SOURCE_DIR}/tests/smoke_serve.sh $<TAR
add_test(NAME parallel_args COMMAND ${CMAKE_SOURCE_DIR}/tests/parallel_args.sh $<TARGET_FILE:onebit>)
add_test(NAME recipes_route COMMAND ${CMAKE_SOURCE_DIR}/tests/recipes_route.sh $<TARGET_FILE:onebit>)
add_test(NAME device_route COMMAND ${CMAKE_SOURCE_DIR}/tests/device_route.sh $<TARGET_FILE:onebit>)
add_test(NAME pm_route COMMAND ${CMAKE_SOURCE_DIR}/tests/pm_route.sh $<TARGET_FILE:onebit>)
add_test(NAME bench_selftest COMMAND ${CMAKE_SOURCE_DIR}/tests/bench_selftest.sh $<TARGET_FILE:onebit>)
add_test(NAME prism_route COMMAND ${CMAKE_SOURCE_DIR}/tests/prism_route.sh $<TARGET_FILE:onebit>)
add_test(NAME laya_gguf_route COMMAND ${CMAKE_SOURCE_DIR}/tests/laya_gguf_route.sh $<TARGET_FILE:onebit>)
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ Windows and 1bit OS ([docs/releases.md](docs/releases.md)). The first release sh
> ([docs/dwarfstar.md](docs/dwarfstar.md)). Step 4, the Laya router, has landed as an opt-in:
> `1bit serve --laya` classifies each conversation (code, prose, short, long document; 95.5% on
> 200 labelled requests) and a measured policy picks the device; the scorer runs on HRX at
> 15-16 ms a decision ([docs/laya.md](docs/laya.md)). Step 5, the model registry, has landed: of 334,413
> 15-16 ms a decision ([docs/laya.md](docs/laya.md)). ZAYA1-8B is the engine's Project Manager:
> `1bit serve --pm` (Lemonade entry `ZAYA1-8B-PM-1bit`) answers general chat itself and delegates
> code, long reasoning and agentic tasks to expert models Lemonade loads beside it
> ([docs/pm.md](docs/pm.md)). Step 5, the model registry, has landed: of 334,413
> HF text-generation models with an architecture, 94.71% are mapped to a backend and 64.2%
> have an architecture checked end to end on Strix Halo; a daily census keeps the counts
> current ([docs/registry.md](docs/registry.md)). The working engine is being ported from 1bit-MONSTER,
Expand Down
519 changes: 519 additions & 0 deletions app/pm.cpp

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions app/pm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2026 bong-water-water-bong
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// app/pm.h -- `1bit serve --pm`: the served model is the Project Manager (docs/pm.md).
//
// The resident model (ZAYA1-8B) keeps every conversation. On POST /v1/chat/completions
// the engine runs a tool loop instead of a plain forward: the PM gets a system prompt
// and two tools, experts() and delegate(expert, task, context). A delegate call becomes an
// OpenAI chat request to Lemonade for the expert's catalog id (Lemonade loads the expert;
// it allows two LLMs at once, so one expert runs beside the PM); the expert's answer comes
// back as the tool result, and the PM composes the final answer, which is streamed when the
// client asked for a stream. Every other route forwards unchanged. When Lemonade or the
// expert fails, the tool result says so and the PM answers alone, never silently.
#pragma once

#include <httplib.h>
#include <nlohmann/json.hpp>

#include <string>
#include <vector>

namespace onebit::pm {

struct Expert {
std::string key; // what the PM names in delegate(expert=...)
std::string id; // Lemonade catalog id (the `model` of the chat request)
std::string domain; // one line, shown to the PM
std::string use_when; // when to pick this expert, shown to the PM
int max_tokens = 0; // the expert's max_tokens (0: Lemonade's default)
};

struct Config {
std::vector<Expert> experts;
int max_rounds = 4; // PM rounds per request, tool rounds included
size_t max_result_bytes = 32768; // an expert's answer is cut here (UTF-8 safe) before the PM sees it
int delegate_timeout_s = 1800; // read timeout for one expert answer
bool thinking = false; // chat_template_kwargs.enable_thinking for PM rounds (the client's wins)
std::string system_prompt; // "" : the built-in prompt (pm.cpp)
std::string lemonade_url; // http://host:port of the Lemonade that runs the experts
std::string chat_path = "/api/v1/chat/completions";

const Expert* find(const std::string& key) const;
};

// config/pm-experts.json as built into this binary.
const char* default_experts_json();

// Parses an experts file (the built-in one when `file` is empty); throws std::runtime_error
// with the reason on a bad file.
Config load_config(const std::string& file, const std::string& lemonade_url);

// One tool call the PM made, from llama-server's tool_calls array or from the text of its
// answer (ZAYA's <zyphra_tool_call><function=NAME><parameter=K>V</parameter></function> form,
// Qwen's <tool_call>{"name":..,"arguments":{..}}</tool_call> form, or a bare JSON object).
struct ToolCall {
std::string id, name;
nlohmann::json arguments; // an object; a non-JSON arguments string becomes {"task": text}
};
std::vector<ToolCall> parse_tool_calls(const nlohmann::json& message, const std::vector<std::string>& tool_names);

// The PM handler for POST /v1/chat/completions. `backend_port` is the resident model's
// llama-server; our_id/drop_model/set_model are serve's usual model-name rules.
void handle_chat(const Config& cfg, int backend_port, const std::string& our_id, bool drop_model,
const std::string& set_model, const httplib::Request& req, httplib::Response& res);

} // namespace onebit::pm
40 changes: 40 additions & 0 deletions app/serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
// DwarfStar's own GGUFs; docs/dwarfstar.md)
// --laya with --device auto -> Laya classifies each conversation and the route
// policy picks the device (RFC #186, docs/laya.md)
// --pm [--pm-experts FILE] [--lemonade-url U] -> the model is the Project Manager: each chat
// completion runs a tool loop in which it answers
// or delegates to an expert model Lemonade runs
// (app/pm.cpp, docs/pm.md)
// --mtp <head.gguf> on hrx or cpu -> multi-token prediction: the model's MTP
// head drafts, the model verifies (docs/serve.md)
// --dflash <draft.gguf> on hrx or cpu -> a DFlash block-diffusion draft model
Expand All @@ -56,6 +60,7 @@

#include "gguf_meta.h"
#include "hrx_archs.h"
#include "pm.h"
#include "recipes.h"

#ifdef ONEBIT_MOE
Expand Down Expand Up @@ -148,6 +153,9 @@ struct Options {
std::string route_policy; // --route-policy FILE: request class -> device (default: built in)
std::string recipes; // --recipes FILE: tuned backend settings (default: built in, docs/recipes.md)
bool no_recipes = false; // --no-recipes: the backend gets serve's own flags only
bool pm = false; // --pm: the model is the Project Manager, delegating to experts (docs/pm.md)
std::string pm_experts; // --pm-experts FILE (default: config/pm-experts.json, built in)
std::string lemonade_url = "http://127.0.0.1:8000"; // --lemonade-url: the Lemonade that runs the experts
};

// HRX dlopens the HSA runtime, and a distro libhsa rejects gfx1151's
Expand Down Expand Up @@ -1166,6 +1174,15 @@ int serve_child(const Options& given) {
if (o.laya_auto || !o.laya_model.empty())
throw std::runtime_error("--laya routes among devices; a 1BP package runs on ds4 only");
}
// --pm (docs/pm.md): the model is the Project Manager. Its chat completions run the tool loop
// in app/pm.cpp against the one backend; the experts are Lemonade's models, reached by URL.
pm::Config pm_cfg;
if (o.pm) {
if (o.laya_auto || !o.laya_model.empty())
throw std::runtime_error("--pm does not combine with --laya: the Project Manager routes among models, Laya among devices");
if (!o.role.empty()) throw std::runtime_error("--pm does not combine with --embedding/--reranking");
pm_cfg = pm::load_config(o.pm_experts, o.lemonade_url);
}
std::vector<Launch> backends;
// --laya / --laya-model with --device auto (RFC #186): Laya classifies each conversation
// (code, prose, short, long_doc; laya/route.h) and the route policy (config/route-policy.json
Expand Down Expand Up @@ -1316,6 +1333,16 @@ int serve_child(const Options& given) {
// choosing and reserving are one step, or a burst of requests all read "empty"
std::shared_ptr<InFlight> held;
size_t pick = 0;
if (o.pm && q.path == "/v1/chat/completions") {
// the Project Manager's tool loop (app/pm.cpp); /v1/completions forwards as usual
{
std::lock_guard<std::mutex> lock(route_mu);
held = std::make_shared<InFlight>(&inflight[0]);
}
++routed[0];
pm::handle_chat(pm_cfg, backends[0].port, id, drop_model, set_model, q, r);
return;
}
#ifdef ONEBIT_LAYA
if (laya) {
// one decision per conversation: later turns reuse the class of the first
Expand Down Expand Up @@ -1494,6 +1521,13 @@ int serve_child(const Options& given) {
ready = true;
std::fprintf(stderr, "1bit serve: ready on http://%s:%d%s\n", o.host.c_str(), o.port,
laya ? " (laya routes each request; a device's backend starts when first picked)" : "");
if (o.pm) {
std::string experts;
for (const auto& e : pm_cfg.experts) experts += (experts.empty() ? "" : ", ") + e.key + "=" + e.id;
std::fprintf(stderr, "1bit serve: pm: %s is the project manager; experts through %s: %s (%s, %d rounds max)\n",
id.c_str(), o.lemonade_url.c_str(), experts.empty() ? "none" : experts.c_str(),
o.pm_experts.empty() ? "config/pm-experts.json" : o.pm_experts.c_str(), pm_cfg.max_rounds);
}
auto all_alive = [&] {
std::lock_guard<std::mutex> lock(start_mu);
for (auto& c : children) if (!c->alive()) return false;
Expand Down Expand Up @@ -1750,6 +1784,9 @@ void usage(FILE* out) {
" each conversation and the route policy picks its device (docs/laya.md)\n"
" [--recipes FILE | --no-recipes] tuned llama-server settings per model and route\n"
" (default: config/recipes.json, built in; docs/recipes.md)\n"
" [--pm [--pm-experts FILE] [--lemonade-url URL]] Project Manager: the model answers or\n"
" delegates each chat to an expert model Lemonade runs (default experts:\n"
" config/pm-experts.json, built in; URL default http://127.0.0.1:8000; docs/pm.md)\n"
" <model>: an NPU model directory (model.q4nx + npu/, or one a private NPU route serves),\n"
" an ONNX Runtime GenAI directory (genai_config.json: --device onnx),\n"
" a .gguf file, or with --device mlx a Hugging Face id (mlx-community/...)\n");
Expand Down Expand Up @@ -1806,6 +1843,9 @@ int run_serve(int argc, char** argv) {
else if (a == "--route-policy") o.route_policy = next();
else if (a == "--recipes") o.recipes = next();
else if (a == "--no-recipes") o.no_recipes = true;
else if (a == "--pm") o.pm = true;
else if (a == "--pm-experts") o.pm_experts = next();
else if (a == "--lemonade-url") o.lemonade_url = next();
else if (a == "-h" || a == "--help") { usage(stdout); return 0; }
else throw std::runtime_error("unknown option " + a);
}
Expand Down
39 changes: 39 additions & 0 deletions config/pm-experts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"about": "Experts for `1bit serve --pm` (docs/pm.md). The served model (ZAYA1-8B) is the Project Manager: it answers general conversation itself and delegates a task to one of these experts with its delegate(expert, task, context) tool. `id` is the model's Lemonade catalog id; the PM asks Lemonade to run it, so an expert must be an entry Lemonade can load. Lemonade keeps at most two LLMs loaded, so one expert runs beside the PM at a time. Replace with --pm-experts FILE.",
"pm": {
"max_rounds": 4,
"max_result_bytes": 32768,
"delegate_timeout_s": 1800,
"thinking": false
},
"experts": [
{
"expert": "code",
"id": "Qwen2.5-Coder-7B-Instruct-1bit",
"domain": "programming",
"use_when": "Writing, fixing, explaining or reviewing code in any language; tests; shell commands; build errors. The default for code: light (4.7 GB) and quick to load.",
"max_tokens": 4096
},
{
"expert": "code-heavy",
"id": "Qwen3-Coder-30B-A3B-Instruct-1bit",
"domain": "programming, large tasks",
"use_when": "Large refactors, long repositories, multi-file changes or agentic coding where the 7B coder would not be enough. Costs an 18 GB load; prefer `code` unless the task is clearly big.",
"max_tokens": 8192
},
{
"expert": "reasoning",
"id": "Qwen3.8-27B-1bit",
"domain": "long reasoning, mathematics, analysis",
"use_when": "Multi-step mathematics, proofs, careful analysis of a long document, or any question where a long chain of reasoning decides the answer.",
"max_tokens": 8192
},
{
"expert": "agentic",
"id": "gpt-oss-20b-mxfp4-1bit",
"domain": "tool-heavy agentic tasks",
"use_when": "Tasks that call for planning many tool calls, structured outputs or function-calling-heavy workflows.",
"max_tokens": 4096
}
]
}
Loading
Loading