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
138 changes: 136 additions & 2 deletions tools/server/server-context-page.inc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@
return kept_checkpoints;
}

// The `.dft` sidecar (#79): the draft context's KV for the sequence, once per page. Header: magic,
// version, the number of tokens it covers, an FNV-1a hash of exactly those tokens, an FNV-1a hash of the
// save's own `.lora` sidecar bytes (the adapters and their file revisions the state was computed under:
// the same tokens under other adapters are another computation), then the state's size and bytes.
// A save removes the old `.dft` before it publishes the page, so a save interrupted anywhere leaves either
// no draft (it drafts cold) or its own, never an earlier save's (Codex on #130).
static constexpr uint32_t page_dft_magic = 0x44524654u; // 'DRFT'
static constexpr uint32_t page_dft_version = 2;
static constexpr size_t page_dft_header = 2 * sizeof(uint32_t) + 4 * sizeof(uint64_t);

static uint64_t page_bytes_hash(const uint8_t * bytes, size_t n) {
uint64_t h = 0xcbf29ce484222325ull;
for (size_t i = 0; i < n; ++i) {
h ^= bytes[i];
h *= 0x100000001b3ull;
}
return h;
}

static uint64_t page_tokens_hash(const llama_token * tokens, size_t n) {
uint64_t h = 0xcbf29ce484222325ull;
for (size_t i = 0; i < n; ++i) {
const uint32_t t = (uint32_t) tokens[i];
for (int b = 0; b < 4; ++b) {
h ^= (t >> (8 * b)) & 0xffu;
h *= 0x100000001b3ull;
}
}
return h;
}

// The `.ckpt` sidecar: the kept checkpoints, each with its target, draft and speculative state.
template <typename Put>
static void page_write_checkpoints(const std::vector<const common_prompt_checkpoint *> & kept, Put && put) {
Expand Down Expand Up @@ -133,6 +164,9 @@
};
page_write_checkpoints(kept, count);
page_write_adapters(slot->lora, count);
if (ctx_dft != nullptr) {
count(nullptr, page_dft_header + llama_state_seq_get_size_ext(ctx_dft, slot->id, LLAMA_STATE_SEQ_FLAGS_NONE));
}
if (size_overflow) {
send_error(task, "Slot file size overflow", ERROR_TYPE_SERVER);
return;
Expand Down Expand Up @@ -164,7 +198,9 @@
size_t n_ckpt = 0, n_lora = 0;
page_write_checkpoints(kept, [&](const void *, size_t n) { n_ckpt += n; });
page_write_adapters(slot->lora, [&](const void *, size_t n) { n_lora += n; });
const uint64_t bytes = (uint64_t) n_body + packed.size() + n_ckpt + n_lora;
const size_t n_dft_state = ctx_dft != nullptr ? llama_state_seq_get_size_ext(ctx_dft, slot->id, LLAMA_STATE_SEQ_FLAGS_NONE) : 0;
const size_t n_dft = ctx_dft != nullptr ? page_dft_header + n_dft_state : 0;
const uint64_t bytes = (uint64_t) n_body + packed.size() + n_ckpt + n_lora + n_dft;
if (task.slot_action.page_reserved == 0) {
task.slot_action.page_asked_us = ggml_time_us();
auto parked = std::make_shared<server_task>(std::move(task));
Expand Down Expand Up @@ -201,13 +237,33 @@
job->ckpt_sidecar.reserve(n_ckpt);
page_write_checkpoints(kept, [&](const void * p, size_t n) { server_page_put(job->ckpt_sidecar, p, n); });
page_write_adapters(slot->lora, [&](const void * p, size_t n) { server_page_put(job->lora_sidecar, p, n); });
if (ctx_dft != nullptr) {
const uint64_t n_tok = job->tokens.size();
const uint64_t hash = page_tokens_hash(job->tokens.data(), job->tokens.size());
const uint64_t lhash = page_bytes_hash(job->lora_sidecar.data(), job->lora_sidecar.size());
const uint64_t n_st = n_dft_state;
job->dft_sidecar.resize(n_dft);
uint8_t * w = job->dft_sidecar.data();
auto put = [&](const void * src, size_t n) { std::memcpy(w, src, n); w += n; };
put(&page_dft_magic, sizeof(page_dft_magic)); put(&page_dft_version, sizeof(page_dft_version));
put(&n_tok, sizeof(n_tok)); put(&hash, sizeof(hash)); put(&lhash, sizeof(lhash)); put(&n_st, sizeof(n_st));
if (llama_state_seq_get_data_ext(ctx_dft, w, n_dft_state, slot->id, LLAMA_STATE_SEQ_FLAGS_NONE) != n_dft_state) {
job->dft_sidecar.clear(); // written as absent: the restore drafts cold, never on a torn state
}
}
job->copy_ms = (ggml_time_us() - t_copy) / 1000.0;

page_io_submit(task.id, std::move(job), page_write);
}

// The worker's share of a save: the page and its sidecars on disk.
static void page_write(server_page_job & job) {
{
// an earlier save's draft goes before this save's page is published, so no interruption can pair
// them; this save's draft is written last, whole, below
std::error_code ec;
std::filesystem::remove(job.filepath + ".dft", ec);
}
const server_page_file file { LLAMA_STATE_SEQ_MAGIC, LLAMA_STATE_SEQ_VERSION };
job.n_bytes = file.write(job.filepath, job.tokens.data(), job.tokens.size(), job.body, job.error);
if (job.n_bytes == 0) {
Expand All @@ -219,6 +275,26 @@
if (!server_page_write_bytes(job.filepath + ".lora", job.lora_sidecar)) {
SRV_WRN("slot %d: adapter sidecar could not be written: %s (this page will not restore)\n", job.id_slot, job.filepath.c_str());
}
{
// the draft KV, whole or not at all: a temp file renamed over, so a reader never sees a torn
// one; a page without one has none on disk (a stale .dft from an earlier save is removed)
const std::string dft_path = job.filepath + ".dft";
const std::string dft_tmp = dft_path + ".partial";
std::error_code ec;
bool ok = !job.dft_sidecar.empty() && server_page_write_bytes(dft_tmp, job.dft_sidecar);
if (ok) {
std::filesystem::rename(dft_tmp, dft_path, ec);
ok = !ec;
}
if (!ok) {
std::filesystem::remove(dft_tmp, ec);
std::filesystem::remove(dft_path, ec);
if (!job.dft_sidecar.empty()) {
SRV_WRN("slot %d: draft sidecar could not be written: %s (the restore drafts cold)\n", job.id_slot, dft_path.c_str());
}
}
std::vector<uint8_t>().swap(job.dft_sidecar);
}
// on disk now: the copy is freed here, and its bytes returned to the budget, not when the loop
// takes the completion
std::vector<uint8_t>().swap(job.body);
Expand Down Expand Up @@ -255,7 +331,7 @@
job->progress = std::move(progress);
// the bytes it will hold: the page and its sidecars as they are on disk (a missing file holds
// none and fails its read)
for (const char * suffix : { "", ".ckpt", ".lora" }) {
for (const char * suffix : { "", ".ckpt", ".lora", ".dft" }) {
std::error_code ec;
const auto n = std::filesystem::file_size(job->filepath + suffix, ec);
job->bytes += ec ? 0 : (uint64_t) n;
Expand Down Expand Up @@ -319,6 +395,7 @@
static void page_free_buffers(server_page_job & job) {
std::vector<uint8_t>().swap(job.body);
std::vector<int32_t>().swap(job.tokens);
std::vector<uint8_t>().swap(job.dft_sidecar);
job.checkpoints.clear();
}

Expand Down Expand Up @@ -350,6 +427,11 @@
job.error = "the page's adapter identity is missing or unreadable: " + lora_path;
return;
}
// the identity's exact bytes, which the draft sidecar names (page_apply_restore matches them)
server_page_reader whole(lora_path);
std::vector<uint8_t> bytes(whole.ok ? whole.size : 0);
whole.get(bytes.data(), bytes.size());
job.lora_hash = whole.ok ? page_bytes_hash(bytes.data(), bytes.size()) : 0;
}

const server_page_file file { LLAMA_STATE_SEQ_MAGIC, LLAMA_STATE_SEQ_VERSION };
Expand Down Expand Up @@ -418,6 +500,32 @@
if (!ok) {
job.checkpoints.clear();
}

// The draft KV (#79): read whole when its file is exactly a header plus the state it names; any
// other size is torn or foreign and is never allocated. Matching it to the restored tokens is the
// loop's (page_apply_restore).
{
server_page_reader dft(job.filepath + ".dft");
if (dft.ok && dft.size >= page_dft_header) {
uint32_t magic = 0, version = 0;
uint64_t n_tok = 0, hash = 0, lhash = 0, n_st = 0;
dft.get(&magic, sizeof(magic)); dft.get(&version, sizeof(version));
dft.get(&n_tok, sizeof(n_tok)); dft.get(&hash, sizeof(hash)); dft.get(&lhash, sizeof(lhash));
dft.get(&n_st, sizeof(n_st));
if (dft.ok && magic == page_dft_magic && version == page_dft_version && n_st == dft.size - page_dft_header) {
job.dft_sidecar.resize(page_dft_header + n_st);
uint8_t * w = job.dft_sidecar.data();
auto put = [&](const void * src, size_t n) { std::memcpy(w, src, n); w += n; };
put(&magic, sizeof(magic)); put(&version, sizeof(version));
put(&n_tok, sizeof(n_tok)); put(&hash, sizeof(hash)); put(&lhash, sizeof(lhash)); put(&n_st, sizeof(n_st));
dft.get(w, n_st);
job.dft_ok = dft.ok;
}
}
if (!job.dft_ok) {
std::vector<uint8_t>().swap(job.dft_sidecar);
}
}
}

// Hands a switch's file IO to page_io; its completion comes back to the loop as a task with the
Expand Down Expand Up @@ -574,6 +682,32 @@
}
std::vector<uint8_t>().swap(job.body);

// The draft's KV comes back with the page (#79) when it covers exactly these tokens; otherwise the
// draft sequence is cleared, never left holding another occupant's KV at these positions: it then
// drafts cold and the target still verifies every token
if (ctx_dft != nullptr) {
bool applied = false;
if (job.dft_ok) {
uint64_t n_tok = 0, hash = 0, lhash = 0, n_st = 0;
const uint8_t * r = job.dft_sidecar.data() + 2 * sizeof(uint32_t);
std::memcpy(&n_tok, r, sizeof(n_tok)); r += sizeof(n_tok);
std::memcpy(&hash, r, sizeof(hash)); r += sizeof(hash);
std::memcpy(&lhash, r, sizeof(lhash)); r += sizeof(lhash);
std::memcpy(&n_st, r, sizeof(n_st)); r += sizeof(n_st);
const auto & toks = job.tokens;
// the same tokens AND the same computation (adapters + revisions) as the page restored
if (n_tok == toks.size() && hash == page_tokens_hash(toks.data(), toks.size()) && lhash == job.lora_hash) {
llama_memory_seq_rm(llama_get_memory(ctx_dft), slot->id, -1, -1);
applied = llama_state_seq_set_data_ext(ctx_dft, r, n_st, slot->id, LLAMA_STATE_SEQ_FLAGS_NONE) == n_st;
}
}
if (!applied) {
llama_memory_seq_rm(llama_get_memory(ctx_dft), slot->id, -1, -1);
SLT_WRN(*slot, "no draft sidecar matching this page (%s.dft): the draft starts cold\n", job.filepath.c_str());
}
std::vector<uint8_t>().swap(job.dft_sidecar);
}

slot->prompt.clear();
slot->prompt.tokens = std::move(*restored);
slot->lora = std::move(page_lora);
Expand Down
7 changes: 6 additions & 1 deletion tools/server/server-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2567,7 +2567,12 @@ struct server_context_impl {
if (parts.tgt) {
ckpt.update_tgt(ctx_tgt, slot.id, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY);
}
if (parts.dft) {
// Continuum fork (#79): a draft whose memory rolls back by trimming (plain attention KV, the MTP
// head) has nothing a checkpoint must hold: on restore, slot.mem.seq_rm trims its live KV to the
// checkpoint. PARTIAL_ONLY returned that whole KV (~2 KB/token, 246 MB at 118k tokens) in EVERY
// checkpoint, 79% of each, up to n_ctx_checkpoints copies per slot in host RAM. A saved page
// carries the draft KV once instead (the `.dft` sidecar, server-context-page.inc).
if (parts.dft && ctx_dft_seq_rm_type != COMMON_CONTEXT_SEQ_RM_TYPE_PART) {
ckpt.update_dft(ctx_dft, slot.id, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY);
}
if (parts.spec) {
Expand Down
5 changes: 5 additions & 0 deletions tools/server/server-page-job.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ struct server_page_job {
std::vector<int32_t> tokens;
std::vector<uint8_t> ckpt_sidecar;
std::vector<uint8_t> lora_sidecar;
// the draft context's KV for this sequence, once per page (`.dft`): written by a save; read by a
// restore, then applied only when it covers exactly the restored tokens
std::vector<uint8_t> dft_sidecar;
bool dft_ok = false;
uint64_t lora_hash = 0; // the restored page's `.lora` bytes, which its draft must name

// restore: what the worker read (the adapter identity is checked on the loop, against the
// adapters loaded when it applies)
Expand Down