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: 6 additions & 4 deletions app/serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2074,12 +2074,14 @@ int serve_child(const Options& given) {
return;
}
const size_t n = h.n_tokens;
const double prefill_ms = h.prefill_ms, restore_ms = h.restore_ms;
const double prefill_ms = h.prefill_ms, restore_ms = h.restore_ms, share_ms = h.share_ms;
forward(backends[pick].port, held, -1, id, drop_model, set_model, o.precise, q, r, &watch, &body,
[t0, n, prefill_ms, restore_ms, slot] {
[t0, n, prefill_ms, restore_ms, share_ms, slot] {
const double ttft = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
std::fprintf(stderr, "1bit serve: hybrid: %zu prompt tokens: prefill %.0f ms, restore %.0f ms, first byte %.0f ms\n",
n, prefill_ms, restore_ms, ttft);
// share_ms is the target-1 fd handoff (0 on the file route, where restore_ms is the file)
std::fprintf(stderr, "1bit serve: hybrid: %zu prompt tokens: prefill %.0f ms, restore %.0f ms,"
" share %.3f ms, first byte %.0f ms\n",
n, prefill_ms, restore_ms, share_ms, ttft);
if (slot->owns_lock()) slot->unlock();
});
return;
Expand Down
222 changes: 219 additions & 3 deletions app/serve_hybrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
#if !defined(_WIN32)
#include <fcntl.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
Expand All @@ -70,7 +72,8 @@ struct Outcome {
bool handed_over = false; // the slot holds the provider's KV; the body carries id_slot/cache_prompt
size_t n_tokens = 0;
double prefill_ms = 0; // the provider's own figure when it gave one, else wall time
double restore_ms = 0;
double restore_ms = 0; // file route: the restore; shared route: the adopt
double share_ms = 0; // shared route: export + fd handoff, before the provider runs
std::string why; // when not handed over: the reason, for one stderr line
};

Expand Down Expand Up @@ -138,13 +141,106 @@ inline Run run_provider(const std::vector<std::string>& argv, const std::vector<
#endif
}

#if !defined(_WIN32)

// Target 1, cross-process: the decoder exports each of its KV buffers as a dma-buf and hands the fds over a unix
// socket (Linux cannot reopen a dma-buf from /proc/<pid>/fd, measured). The engine is the middle of that: it
// listens, receives {fd, offset, size, layout} per region, and passes the fds on to the provider as inherited
// descriptors, so the provider maps the decoder's own KV memory instead of writing a state file.
struct KvShare {
std::string header; // the decoder's JSON: region descriptors + the context params it built its cache with
std::vector<int> fds; // the regions, in the decoder's buffer order

void close_all() {
for (int fd : fds) {
if (fd >= 0) ::close(fd);
}
fds.clear();
}
~KvShare() { close_all(); }
};

// bind + listen on the slot's socket path; -1 on failure (why set)
inline int kvshare_listen(const std::string& path, std::string* why) {
const int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) { *why = std::string("socket: ") + std::strerror(errno); return -1; }
sockaddr_un addr{};
addr.sun_family = AF_UNIX;
if (path.size() >= sizeof(addr.sun_path)) { *why = "socket path too long"; ::close(fd); return -1; }
std::memcpy(addr.sun_path, path.c_str(), path.size() + 1);
::unlink(path.c_str());
if (::bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0) {
*why = "bind " + path + ": " + std::strerror(errno);
::close(fd);
return -1;
}
if (::listen(fd, 1) != 0) { *why = std::string("listen: ") + std::strerror(errno); ::close(fd); return -1; }
return fd;
}

// one message: [uint32 length][JSON header], the region fds riding along as SCM_RIGHTS
inline bool kvshare_recv(int lfd, KvShare* out, std::string* why) {
const int fd = ::accept(lfd, nullptr, nullptr);
if (fd < 0) { *why = std::string("accept: ") + std::strerror(errno); return false; }

char buf[4 + 65536];
std::vector<char> control(CMSG_SPACE(64 * sizeof(int)));
size_t got = 0;
bool have_control = false;
for (;;) {
if (got >= 4) {
uint32_t len = 0;
std::memcpy(&len, buf, 4);
if (len > sizeof(buf) - 4) { *why = "the decoder's header is too large"; ::close(fd); return false; }
if (got >= 4 + (size_t) len) break;
}
ssize_t n;
if (!have_control) {
msghdr msg{};
iovec iov{ buf, sizeof(buf) };
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = control.data();
msg.msg_controllen = control.size();
n = ::recvmsg(fd, &msg, 0);
if (n > 0) {
have_control = true;
for (cmsghdr* c = CMSG_FIRSTHDR(&msg); c != nullptr; c = CMSG_NXTHDR(&msg, c)) {
if (c->cmsg_level != SOL_SOCKET || c->cmsg_type != SCM_RIGHTS) continue;
const size_t n_fds = (c->cmsg_len - CMSG_LEN(0)) / sizeof(int);
for (size_t i = 0; i < n_fds; ++i) {
int region_fd = -1;
std::memcpy(&region_fd, CMSG_DATA(c) + i * sizeof(int), sizeof(int));
::fcntl(region_fd, F_SETFD, 0); // the provider inherits them through exec
out->fds.push_back(region_fd);
}
}
}
} else {
n = ::recv(fd, buf + got, sizeof(buf) - got, 0);
}
if (n < 0 && errno == EINTR) continue;
if (n <= 0) { *why = "the decoder closed the socket before sending the regions"; ::close(fd); return false; }
got += (size_t) n;
}
::close(fd);
uint32_t len = 0;
std::memcpy(&len, buf, 4);
out->header.assign(buf + 4, len);
return !out->fds.empty();
}

#endif

class Prefill {
public:
// provider: the program; elfs: passed as --elfs when not empty; slot_dir: the backend's
public: // provider: the program; elfs: passed as --elfs when not empty; slot_dir: the backend's
// --slot-save-path (the state files go there, by bare name, as llama-server requires)
Prefill(std::string provider, std::string elfs, std::string slot_dir, std::string model)
: provider_(std::move(provider)), elfs_(std::move(elfs)), slot_dir_(std::move(slot_dir)), model_(std::move(model)) {
if (const char* t = std::getenv("ONEBIT_HYBRID_PREFILL_TIMEOUT_S"); t && *t) timeout_s_ = std::max(1, std::atoi(t));
// target 1: the decoder exports its KV region (ONEBIT_KV_SHARE, which this process's children inherit) and
// the provider prefills into it instead of writing a state file
if (const char* v = std::getenv("ONEBIT_KV_SHARE"); v && *v) shared_ = true;
}

const std::string& provider() const { return provider_; }
Expand Down Expand Up @@ -194,6 +290,14 @@ class Prefill {
const std::vector<int32_t> ids = tokenize(port, prompt, &o.why);
if (ids.size() < 2) { if (o.why.empty()) o.why = "prompt shorter than 2 tokens"; return o; }
o.n_tokens = ids.size();
if (shared_) {
Outcome shared = prepare_shared(port, ids, o);
if (shared.handed_over) {
body["id_slot"] = 0;
body["cache_prompt"] = true;
}
return shared;
}
const unsigned n = ++seq_;
const std::string stem = "hybrid-" + std::to_string(n);
const std::filesystem::path tokens = std::filesystem::path(slot_dir_) / (stem + ".tokens");
Expand Down Expand Up @@ -261,8 +365,120 @@ class Prefill {
return o;
}

private:
#if !defined(_WIN32)
// Target 1: the provider prefills into the decoder's own KV memory. No state file is written or read; the
// only things that cross a boundary are the region fds and a few hundred bytes of JSON.
Outcome prepare_shared(int port, const std::vector<int32_t>& ids, Outcome& o) {
const unsigned n = ++seq_;
const std::string stem = "kvshare-" + std::to_string(n);
const std::filesystem::path tokens = std::filesystem::path(slot_dir_) / (stem + ".tokens");
const std::filesystem::path layout = std::filesystem::path(slot_dir_) / (stem + ".json");
const std::filesystem::path sockpath = std::filesystem::path(slot_dir_) / (stem + ".sock");
{
std::ofstream f(tokens);
for (int32_t id : ids) f << id << '\n';
if (!f) { o.why = "cannot write " + tokens.string(); return o; }
}

// 1. listen, so the decoder has somewhere to hand the fds; 2. it exports and connects
std::string why;
const int lfd = kvshare_listen(sockpath.string(), &why);
if (lfd < 0) { o.why = "kv share: " + why; return o; }
httplib::Client c("127.0.0.1", port);
c.set_read_timeout(600);
const auto t0 = std::chrono::steady_clock::now();
auto r = c.Post("/slots/0?action=share", json{{"socket", sockpath.string()}}.dump(), "application/json");
if (!r || r->status != 200) {
::close(lfd);
std::error_code ec;
std::filesystem::remove(tokens, ec);
std::filesystem::remove(sockpath, ec);
o.why = "share answered " + std::to_string(r ? r->status : -1) + (r ? ": " + r->body.substr(0, 200) : "");
return o;
}
// the decoder numbers every export; adopt has to quote it or it refuses the cells
uint64_t share_id = 0;
try {
share_id = json::parse(r->body).at("share_id").get<uint64_t>();
} catch (const std::exception&) {
::close(lfd);
std::error_code ec;
std::filesystem::remove(tokens, ec);
std::filesystem::remove(sockpath, ec);
o.why = "share reply has no share_id";
return o;
}
KvShare share;
const bool got = kvshare_recv(lfd, &share, &why);
::close(lfd);
std::error_code ec;
std::filesystem::remove(sockpath, ec);
o.share_ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
if (!got) {
std::filesystem::remove(tokens, ec);
o.why = "kv share: " + (why.empty() ? std::string("no regions from the decoder") : why);
return o;
}
{
std::ofstream f(layout);
f << share.header;
if (!f) { std::filesystem::remove(tokens, ec); o.why = "cannot write " + layout.string(); return o; }
}

// 3. the provider maps those fds as its own KV tensors and prefills the prompt into them
std::string fds_arg;
for (size_t i = 0; i < share.fds.size(); ++i) {
fds_arg += (i ? "," : "") + std::to_string(share.fds[i]);
}
std::vector<std::string> argv = {provider_, "--tokens", tokens.string(), "--model", model_,
"--layout", layout.string(), "--fds", fds_arg};
if (!elfs_.empty()) { argv.push_back("--elfs"); argv.push_back(elfs_); }
const auto t1 = std::chrono::steady_clock::now();
const Run run = run_provider(argv, {"ONEBIT_HYBRID_BACKEND_URL=http://127.0.0.1:" + std::to_string(port)}, timeout_s_);
o.prefill_ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t1).count();
std::filesystem::remove(tokens, ec);
std::filesystem::remove(layout, ec);
if (run.code != 0) {
o.why = run.code == -2 ? "provider timed out after " + std::to_string(timeout_s_) + " s"
: "provider exited " + std::to_string(run.code);
return o;
}

// 4. the decoder takes the cells over: no file, no copy (llama_kv_cells_adopt)
json adopt = {{"tokens", ids}, {"share_id", share_id}};
const auto t2 = std::chrono::steady_clock::now();
auto ra = c.Post("/slots/0?action=adopt", adopt.dump(), "application/json");
o.restore_ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t2).count();
if (!ra || ra->status != 200) {
o.why = "adopt answered " + std::to_string(ra ? ra->status : -1) + (ra ? ": " + ra->body.substr(0, 200) : "");
return o;
}
try {
const json j = json::parse(ra->body);
const size_t adopted = j.value("n_adopted", size_t(0));
if (adopted != ids.size()) {
o.why = "adopt took " + std::to_string(adopted) + " of " + std::to_string(ids.size()) + " cells";
return o;
}
if (j.contains("timings") && j["timings"].contains("adopt_ms")) o.restore_ms = j["timings"]["adopt_ms"].get<double>();
} catch (const std::exception&) {
o.why = "adopt reply is not JSON";
return o;
}
o.handed_over = true;
return o;
}
#else
Outcome prepare_shared(int, const std::vector<int32_t>&, Outcome& o) {
o.why = "the shared KV handoff needs unix sockets (POSIX only)";
return o;
}
#endif

private:
std::string provider_, elfs_, slot_dir_, model_;
bool shared_ = false; // target 1: fds instead of a state file
int timeout_s_ = 600;
std::atomic<unsigned> seq_{0};
std::mutex slot_mu_;
Expand Down
32 changes: 32 additions & 0 deletions docs/hybrid.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,38 @@ match the version of the pinned llama.cpp (`third_party/llama.cpp`): the restore
Naming: `--prefill-provider PATH` or `ONEBIT_HYBRID_PREFILL=PATH`; `ONEBIT_HYBRID_ELFS=DIR` adds
`--elfs DIR`.

### The shared route: no state file at all (`ONEBIT_KV_SHARE=1`)

The contract above is the file route, and on Strix Halo the file is the whole cost: every byte of the
prompt's KV is written by the provider and read back by the decoder, through the page cache, for a
handoff that never leaves the box. `ONEBIT_KV_SHARE=1` on `1bit serve` (the child llama-server inherits
it) replaces the file with two dma-buf fds:

```
<provider> --tokens <ids file> --model <model> --layout <header file> --fds <n[,n...]>
```

| | |
|---|---|
| `--layout` | the header the decoder sent: `{"n_regions", "regions": [{"offset", "size", "layout"}], "params": {"n_ctx", "n_batch", "n_ubatch", "n_seq_max", "flash_attn", "type_k", "type_v"}}`. A provider builds its context from `params`; the per-region `layout` hash is what the importer checks |
| `--fds` | the region fds, in region order, inherited from serve (so no `/proc/<pid>/fd` reopening: Linux cannot reopen a dma-buf that way, measured) |
| `--out` | not used on this route |

Per request: `POST /slots/0?action=share {"socket": PATH}` makes the decoder export its KV buffers and
hand the fds over an AF_UNIX socket that serve is listening on (`SCM_RIGHTS`, one length-prefixed JSON
message); serve passes them to the provider as inherited descriptors; the provider maps them as its own
KV tensors and prefills the prompt into them; `POST /slots/0?action=adopt {"tokens": [...]}` makes the
decoder take cells 0..N-1 as positions 0..N-1 with no data movement (`llama_kv_cells_adopt`). The
provider never writes a file, and the only bytes that cross a boundary are the fds and a few hundred
bytes of JSON. `llama-kvshare-prefill` (llama.cpp `tools/kvshare-prefill`) is the reference provider:
a CPU prefill, so the plumbing can be gated with no NPU; the NPU engine is the real one, mapping the
same descriptors as XDNA BOs.

Limits, so nobody reads more into the flag than it does: the region is `llama_kv_cache`, so a hybrid
model's recurrent half (e.g. the 27B's GDN state) is not shared and `?action=share` refuses a hybrid
cache rather than hand over half a prompt. Both sides must be the same pinned llama.cpp, the same model
and `-fa`/`-c`/`-b`/`-ub`/KV types (`params` above); a mismatch is a refusal, not a wrong answer.

## Tests

- `tests/hybrid_route.sh` (CTest `hybrid_route`, no model, no NPU): `tests/fake_backend.py` stands in
Expand Down
Loading