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
28 changes: 21 additions & 7 deletions app/serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,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
// --precise (or ONEBIT_PRECISE=1): every request re-prefills its whole conversation, so a follow-up turn
// takes the same path a cold run of that conversation takes and comes out bitwise identical to it
bool precise = false;
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 pm_router; // --pm-router model|laya: who picks the expert (default: model)
Expand Down Expand Up @@ -792,7 +795,7 @@ bool wants_stream(const std::string& body) {

// watch, where given, tracks the request for the progress watchdog (app/serve_watchdog.h).
void forward(int port, std::shared_ptr<InFlight> held, int draft_max, const std::string& our_id, bool drop_model,
const std::string& set_model, const httplib::Request& req, httplib::Response& res,
const std::string& set_model, bool precise, const httplib::Request& req, httplib::Response& res,
ProgressWatch* watch = nullptr) {
json body;
try {
Expand All @@ -803,6 +806,12 @@ void forward(int port, std::shared_ptr<InFlight> held, int draft_max, const std:
return;
}
if (drop_model) body.erase("model");
// --precise: a follow-up turn differs from a cold run of the same conversation because its new tokens form a
// different batch than the cold prefill does (different kernels, different reduction order), and a near-tie
// then flips. The only way to make the two bitwise identical is to take the cold path: re-prefill the whole
// conversation instead of continuing from the KV cache. That is what this costs - the TTFT of the whole
// conversation on every turn - which is why it is off by default.
if (precise) body["cache_prompt"] = false;
// MTP by load: a request's own draft length wins; otherwise the router's (see post)
if (draft_max >= 0 && !body.contains("speculative.n_max")) body["speculative.n_max"] = draft_max;
if (!set_model.empty()) body["model"] = set_model;
Expand Down Expand Up @@ -2046,7 +2055,7 @@ int serve_child(const Options& given) {
}
held = std::make_shared<InFlight>(&inflight[pick]);
++routed[pick];
forward(backends[pick].port, held, -1, id, backends[pick].drop_model, backends[pick].set_model, q, r, &watch);
forward(backends[pick].port, held, -1, id, backends[pick].drop_model, backends[pick].set_model, o.precise, q, r, &watch);
r.set_header("X-1bit-Route", route_hdr); // class, Laya confidence, device
return;
}
Expand All @@ -2056,7 +2065,7 @@ int serve_child(const Options& given) {
held = std::make_shared<InFlight>(&inflight[pick]);
}
++routed[pick];
forward(backends[pick].port, held, -1, id, drop_model, set_model, q, r, &watch);
forward(backends[pick].port, held, -1, id, drop_model, set_model, o.precise, q, r, &watch);
if (backends[pick].npu_retry && r.status >= 500 && !wants_stream(q.body) && all_alive()) {
// The NPU's first dispatch after idle can hit a one-off firmware timeout (TDR); the
// device recovers and the same request then runs. One retry, said aloud, for a reply
Expand All @@ -2066,7 +2075,7 @@ int serve_child(const Options& given) {
std::this_thread::sleep_for(std::chrono::seconds(1));
r.status = 200;
r.body.clear();
forward(backends[pick].port, held, -1, id, drop_model, set_model, q, r, &watch);
forward(backends[pick].port, held, -1, id, drop_model, set_model, o.precise, q, r, &watch);
}
};
srv.Post("/v1/chat/completions", post);
Expand Down Expand Up @@ -2105,7 +2114,7 @@ int serve_child(const Options& given) {
r.set_content(R"({"error":{"message":"backend did not become ready"}})", "application/json");
return;
}
forward(backends[0].port, nullptr, -1, id, drop_model, set_model, q, r, &watch);
forward(backends[0].port, nullptr, -1, id, drop_model, set_model, o.precise, q, r, &watch);
};
for (const char* path : {"/v1/responses", "/tokenize", "/detokenize", "/apply-template"})
srv.Post(path, llama ? httplib::Server::Handler(first_json) : httplib::Server::Handler(unsupported));
Expand Down Expand Up @@ -2138,7 +2147,7 @@ int serve_child(const Options& given) {
r.set_content(R"({"error":{"message":"model is loading"}})", "application/json");
return;
}
forward(rag[i].port, nullptr, -1, stem(model), true, "", q, r);
forward(rag[i].port, nullptr, -1, stem(model), true, "", false, q, r);
};
};
if (!o.role.empty()) {
Expand All @@ -2148,7 +2157,7 @@ int serve_child(const Options& given) {
r.set_content(R"({"error":{"message":"model is loading"}})", "application/json");
return;
}
forward(backends[0].port, nullptr, -1, id, true, "", q, r);
forward(backends[0].port, nullptr, -1, id, true, "", false, q, r);
};
if (o.role == "embedding")
for (const char* path : {"/v1/embeddings", "/embeddings"}) srv.Post(path, h);
Expand Down Expand Up @@ -2596,6 +2605,8 @@ void usage(FILE* out) {
" HRX_ALLOW_NO_SVM=1, which put the CWSR save area in GTT (docs/hrx.md)\n"
" [--ds4 PATH] [--ssd-streaming] DwarfStar (--device ds4; docs/dwarfstar.md)\n"
" [--mtp HEAD.gguf] [--mtp-max N] [--mtp-p-min P] multi-token prediction (hrx, cpu)\n"
" [--precise] re-prefill each request in full: follow-up turns come out\n"
" identical to a cold run, at the cost of the TTFT (ONEBIT_PRECISE=1)\n"
" [--no-mtp] a GGUF with its own MTP head drafts with it on hrx by default;\n"
" this (or ONEBIT_NO_MTP=1) turns that off\n"
" [--dflash DRAFT.gguf] DFlash draft model instead of --mtp; --mtp-max/--mtp-p-min apply\n"
Expand Down Expand Up @@ -2648,6 +2659,8 @@ void usage(FILE* out) {
int run_serve(int argc, char** argv) {
Options o;
if (const char* e = std::getenv("ONEBIT_MEM_CHECK"); e && std::string(e) == "0") o.mem_check = false;
// ONEBIT_PRECISE=1 is the same as --precise (every request re-prefills its conversation in full)
if (const char* e = std::getenv("ONEBIT_PRECISE"); e && *e && std::string(e) != "0") o.precise = true;
if (const char* e = std::getenv("ONEBIT_WATCHDOG_SECS"); e && *e) o.watchdog.secs = std::atof(e);
// ONEBIT_STRICT_DEVICE=0 / 1 (app/hrx_strict.h); a flag on the command line wins
o.strict = strict_from_env(std::getenv("ONEBIT_STRICT_DEVICE"));
Expand Down Expand Up @@ -2683,6 +2696,7 @@ int run_serve(int argc, char** argv) {
"Lemonade's own llamacpp backend serves Vulkan and ROCm (docs/serve.md)");
else if (a == "--mtp") o.mtp = next();
else if (a == "--no-mtp") o.no_mtp = true;
else if (a == "--precise") o.precise = true;
else if (a == "--dflash") o.dflash = next();
else if (a == "--moe-slots") {
const std::string v = next();
Expand Down
18 changes: 18 additions & 0 deletions docs/serve.md
Original file line number Diff line number Diff line change
Expand Up @@ -794,3 +794,21 @@ ctest runs `serve_e2e_hrx` and `serve_e2e_cpu`, and in general `serve_e2e_<devic
`/v1/models`, the reply rename, SSE relay), that no backend outlives a
SIGKILLed `serve`, and that a backend that exits on its own ends `serve` at once.
`watchdog_serve` runs everywhere too (see "When a backend hangs").

## `--precise`: a follow-up turn is bitwise identical to a cold run

A follow-up turn in the same conversation is not bit-identical to a cold run of that conversation. The
follow-up continues from the KV cache and prefills only its new user message (28 tokens in the test below),
where a cold run prefills the conversation in 512-token ubatches - and HRX picks a different kernel family for a
different token count (generic post-ops for 16 < T < 128, the WMMA prefill tiles for T >= 128), with a different
activation quantisation, split-K and combine tree, so the last bits differ and a near-tie flips. Measured on
2026-10-22, 10 greedy two-turn chats per model, follow-up against a cold run of the same conversation.

`--precise` (or `ONEBIT_PRECISE=1`) makes every request re-prefill its whole conversation, which is exactly the
path a cold run takes: with it, 10/10 chats came out identical on Qwen3.8-27B and 9/10 on Qwen3-4B (the
remaining 4B difference is at a 0.0385-nat tie with both arms prefilling the same tokens, i.e. a question about
that path's determinism rather than about batch size). The cost is the full time-to-first-token of the
conversation on every turn instead of only the new tokens: on the 27B a follow-up goes from 672 ms to 4785 ms
after a ~1.6k-token conversation and from 676 ms to 12348 ms after ~5.4k tokens. It is off by default, and the
token stream of a single turn is unchanged either way.

Loading