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
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
llama_add_compile_flags()

# Model-free scheduling validator: compile the same traversal used by server-context.
# Model-free: placed checkpoints keep her head through a full store (CONTINUUM-ENGINE 4.7.3, llama.cpp#121).
add_executable(test-server-checkpoint-policy test-server-checkpoint-policy.cpp)
target_compile_features(test-server-checkpoint-policy PRIVATE cxx_std_17)
add_test(NAME test-server-checkpoint-policy COMMAND test-server-checkpoint-policy)
set_tests_properties(test-server-checkpoint-policy PROPERTIES LABELS "main")

add_executable(test-server-prefill test-server-prefill.cpp)
target_compile_features(test-server-prefill PRIVATE cxx_std_17)
add_test(NAME test-server-prefill COMMAND test-server-prefill)
Expand Down
153 changes: 153 additions & 0 deletions tests/test-server-checkpoint-policy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// what this catches: placed checkpoints (server-checkpoint-policy.h, CONTINUUM-ENGINE 4.7.3) losing her
// head checkpoint, the restore point when her next prompt diverges before her view end. Model-free: the
// rules run on a list shaped like the slot's. Codex's review of llama.cpp#121 asked for exactly this: the
// head's exact position pinned BEFORE the store fills and asserted after; the pre-fix rule shown to lose
// it; capacity exhaustion and a failed checkpoint copy covered without claiming more retention than the
// capacity allows; upstream behaviour kept without view spans.
#include "../tools/server/server-checkpoint-policy.h"

#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <list>

static void require(bool condition, const char * message) {
if (!condition) {
std::cerr << "FAILED: " << message << '\n';
std::exit(1);
}
}

struct ckpt {
int64_t n_tokens;
int id_task;
int serial; // identity: a checkpoint is the same one only if its serial matches
};

using store = std::list<ckpt>;

// create_checkpoint's order: make room (evict while full), then copy; a failed copy stores nothing
static void create(store & s, size_t capacity, int32_t view_end, ckpt c, bool copy_ok = true) {
while (s.size() >= capacity) {
s.erase(checkpoint_to_evict(s, view_end));
}
if (copy_ok) {
s.push_back(c);
}
}

static void head_survives_a_full_store_across_many_acts() {
const size_t capacity = 4;
store s;
int serial = 0;
const int32_t view_end = 100000;
create(s, capacity, view_end, { 9655, 1, ++serial });
const int head_serial = s.front().serial; // pinned before the store fills
for (int act = 2; act < 60; ++act) {
create(s, capacity, view_end, { 9655 + act * 1000, act, ++serial });
require(s.size() <= capacity, "never more than the capacity");
require(s.front().serial == head_serial && s.front().n_tokens == 9655,
"her exact head checkpoint (position 9655, the one pinned before filling) survives every act");
}
}

static void the_pre_fix_rule_loses_the_head() {
const size_t capacity = 4;
store s;
for (int i = 0; i < 4; ++i) {
s.push_back({ 9655 + i * 1000, i, i });
}
const int head_serial = s.front().serial;
// the rule before the fix: always the oldest
s.erase(s.begin());
s.push_back({ 20000, 9, 9 });
require(s.front().serial != head_serial, "the pre-fix rule evicts her head (the defect this guards)");
// the rule after
store t;
for (int i = 0; i < 4; ++i) {
t.push_back({ 9655 + i * 1000, i, i });
}
create(t, capacity, /*view_end=*/50000, { 20000, 9, 9 });
require(t.front().serial == head_serial, "the placed rule keeps it");
(void) capacity;
}

static void capacity_one_keeps_only_what_fits() {
store s;
create(s, 1, 50000, { 9655, 1, 1 });
create(s, 1, 50000, { 30000, 2, 2 });
require(s.size() == 1 && s.front().serial == 2, "with room for one, the head goes when it is all there is");
}

static void a_failed_copy_keeps_the_head_and_stores_nothing() {
store s;
for (int i = 0; i < 4; ++i) {
s.push_back({ 9655 + i * 1000, i, i });
}
create(s, 4, 50000, { 20000, 9, 9 }, /*copy_ok=*/false);
require(s.size() == 3, "room was made, the failed copy stored nothing: one fewer, never a claim of four");
require(s.front().serial == 0, "her head is still there");
}

static void a_view_end_checkpoint_supersedes_earlier_acts_but_the_head() {
store s = { { 9655, 1, 1 }, { 20000, 2, 2 }, { 30000, 3, 3 }, { 40000, 7, 4 } };
const ckpt kept = { 45000, 7, 5 };
s.push_back(kept);
size_t erased = erase_superseded_checkpoints(s, s.back(), /*view_end=*/45000, /*id_task=*/7, [](const ckpt &) {});
require(erased == 2, "the two checkpoints earlier acts left are superseded");
require(s.size() == 3 && s.front().serial == 1, "her head stays");
for (const auto & c : s) {
require(c.serial == 1 || c.id_task == 7, "only the head and this act's checkpoints remain");
}
store u = { { 9655, 1, 1 }, { 20000, 2, 2 } };
u.push_back({ 25000, 7, 3 });
require(erase_superseded_checkpoints(u, u.back(), /*view_end=*/30000, 7, [](const ckpt &) {}) == 0,
"a checkpoint not AT her view end supersedes nothing");
}

static void without_view_spans_the_oldest_goes() {
store s = { { 9655, 1, 1 }, { 20000, 2, 2 } };
create(s, 2, /*view_end=*/-1, { 30000, 3, 3 });
require(s.front().serial == 2, "without view spans, upstream's rule: the oldest goes");
require(erase_superseded_checkpoints(s, s.back(), -1, 3, [](const ckpt &) {}) == 0, "and nothing is superseded");
}

// the caller's cold path (Codex on #127): a prompt filled from an empty store, with view spans, stopping
// at user turns by checkpoint_at_user_start and creating a checkpoint at each stop, must make her head at
// the FIRST user turn; later turns add none; the view end adds the next one
static void a_cold_prompt_with_view_spans_makes_her_head() {
const int64_t user_starts[] = { 9655, 20000, 30000, 40000 };
const int64_t view_end = 45000;
store s;
int serial = 0;
for (const int64_t pos : user_starts) {
if (checkpoint_at_user_start(/*view_spans=*/true, s.empty(), pos, /*last_user_pos=*/40000,
s.empty() ? 0 : s.back().n_tokens, /*step=*/1024)) {
create(s, 32, view_end, { pos, 1, ++serial });
}
}
require(s.size() == 1 && s.front().n_tokens == 9655, "the head is made at the first user turn, and only there");
create(s, 32, view_end, { view_end, 1, ++serial }); // the view-end stop
require(s.size() == 2 && s.front().n_tokens == 9655 && s.back().n_tokens == view_end, "head, then her view end");
// the rule before this fix skipped every user-start stop when the request carried view spans, so the
// only checkpoint a cold prompt got was its view end's: no head
store before;
create(before, 32, view_end, { view_end, 1, 99 });
require(before.size() == 1 && before.front().n_tokens == view_end, "the pre-fix path's store has no head (the defect this guards)");
// without view spans, upstream: the last user message and step-spaced turns
require(checkpoint_at_user_start(false, false, 40000, 40000, 30000, 1024), "upstream: her last user message");
require(!checkpoint_at_user_start(false, false, 30500, 40000, 30000, 1024), "upstream: not within a step");
require(!checkpoint_at_user_start(true, false, 40000, 40000, 30000, 1024), "with view spans, no blind step checkpoint");
}

int main() {
a_cold_prompt_with_view_spans_makes_her_head();
head_survives_a_full_store_across_many_acts();
the_pre_fix_rule_loses_the_head();
capacity_one_keeps_only_what_fits();
a_failed_copy_keeps_the_head_and_stores_nothing();
a_view_end_checkpoint_supersedes_earlier_acts_but_the_head();
without_view_spans_the_oldest_goes();
std::cout << "test-server-checkpoint-policy: ok\n";
return 0;
}
60 changes: 60 additions & 0 deletions tools/server/server-checkpoint-policy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

// Continuum fork (CONTINUUM-ENGINE 4.7.3, placed checkpoints): which of a slot's context checkpoints go,
// as pure rules over the slot's list (oldest first), so a native test proves them without a model.
// A checkpoint here needs `n_tokens` (its position) and `id_task` (the act that made it).

#include <cstddef>
#include <cstdint>

// Whether the prompt fill stops at a user turn's start (at `pos`) so a checkpoint is made there. The
// first boundary always gets one: with an empty store it is her head, her restore point when her next
// prompt diverges before her view end (Codex on #127: with view spans the head was never made on a cold
// prompt). After it, with view spans, the view end is where her next prompt diverges, so blind step
// checkpoints are not made (up to 32 x ~110 MB per slot thrashed the M5's host); without view spans,
// upstream's rule: at her last user message, or a step past the newest checkpoint.
inline bool checkpoint_at_user_start(bool view_spans, bool store_empty, int64_t pos, int64_t last_user_pos,
int64_t newest, int64_t step) {
if (store_empty) {
return true;
}
if (view_spans) {
return false;
}
return pos == last_user_pos || pos > newest + step;
}

// The checkpoint to erase to make room for one more. With view spans (view_end >= 0) her head (the
// oldest) is her restore point when her next prompt diverges before her view end, so the oldest AFTER
// it goes first; the head goes only when it is all there is. Without view spans, the oldest goes.
template <typename List>
typename List::iterator checkpoint_to_evict(List & checkpoints, int32_t view_end) {
auto victim = checkpoints.begin();
if (view_end >= 0 && checkpoints.size() >= 2) {
++victim;
}
return victim;
}

// After her new view-end checkpoint `kept` (made by act `id_task`) is stored, the checkpoints an earlier
// act left on this prefix are superseded, except her head's: what stays is her head, her newest view
// end and this act's end checkpoints. Returns how many were erased; only a checkpoint AT her view end
// supersedes anything.
template <typename List, typename Checkpoint, typename OnErase>
size_t erase_superseded_checkpoints(List & checkpoints, const Checkpoint & kept, int32_t view_end, int id_task, OnErase on_erase) {
if (view_end < 0 || kept.n_tokens != view_end || checkpoints.empty()) {
return 0;
}
const auto * head = &checkpoints.front();
size_t erased = 0;
for (auto it = checkpoints.begin(); it != checkpoints.end();) {
if (&*it != head && it->id_task != id_task) {
on_erase(*it);
it = checkpoints.erase(it);
++erased;
continue;
}
++it;
}
return erased;
}
44 changes: 35 additions & 9 deletions tools/server/server-context.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <limits>
#include "server-context.h"
#include "server-checkpoint-policy.h"
#include "server-view-end.h"
#include "../../src/llama-ext.h"
#include "server-train.h"
Expand Down Expand Up @@ -744,6 +745,18 @@ struct server_slot {
res["kv_pos_max"] = llama_memory_seq_pos_max(mem, id);
}

// The slot's context checkpoints as they sit in host memory now (Continuum fork): their count
// and their bytes, so the core prices the checkpoint store from what it holds, not a guess.
{
size_t bytes = 0;
json at = json::array();
for (const auto & ckpt : prompt.checkpoints) {
bytes += ckpt.size();
at.push_back({ {"n_tokens", ckpt.n_tokens}, {"bytes", ckpt.size()} });
}
res["checkpoints"] = { {"count", prompt.checkpoints.size()}, {"bytes", bytes}, {"at", at} };
}

const auto & ptask = task ? task : task_prev;

if (ptask) {
Expand Down Expand Up @@ -2605,13 +2618,14 @@ struct server_context_impl {
}

while (slot.prompt.checkpoints.size() >= (size_t) params_base.n_ctx_checkpoints) {
// make room for the new checkpoint, if needed
const auto & cur = slot.prompt.checkpoints.front();
// make room for the new checkpoint, if needed: her head stays while there is anything else
// to evict (server-checkpoint-policy.h)
auto victim = checkpoint_to_evict(slot.prompt.checkpoints, slot.prompt.view_end);

SLT_WRN(slot, "erasing old context checkpoint (pos_min = %d, pos_max = %d, n_tokens = %" PRId64 ", size = %.3f MiB)\n",
cur.pos_min, cur.pos_max, cur.n_tokens, (float) cur.size() / 1024 / 1024);
victim->pos_min, victim->pos_max, victim->n_tokens, (float) victim->size() / 1024 / 1024);

slot.prompt.checkpoints.erase(slot.prompt.checkpoints.begin());
slot.prompt.checkpoints.erase(victim);
}

// the copy is made aside and kept only once complete
Expand All @@ -2631,6 +2645,16 @@ struct server_context_impl {

const auto & kept = slot.prompt.checkpoints.emplace_back(std::move(cur));

// Continuum fork (CONTINUUM-ENGINE 4.7.3, placed checkpoints): her new view-end checkpoint
// supersedes every checkpoint an earlier act left on this prefix except her head's (the oldest,
// the restore point when her next prompt diverges before her view end). What stays per slot is
// her head, her newest view end, and this act's end checkpoints.
erase_superseded_checkpoints(slot.prompt.checkpoints, kept, slot.prompt.view_end, id_task,
[&](const common_prompt_checkpoint & it) {
SLT_TRC(slot, "erasing a checkpoint her view end supersedes (n_tokens = %" PRId64 ", size = %.3f MiB)\n",
it.n_tokens, (float) it.size() / 1024 / 1024);
});

SLT_TRC(slot,
"created context checkpoint %d of %d (pos_min = %d, pos_max = %d, n_tokens = %" PRId64 ", size = %.3f MiB)\n",
(int) slot.prompt.checkpoints.size(), params_base.n_ctx_checkpoints, kept.pos_min,
Expand Down Expand Up @@ -4082,13 +4106,15 @@ struct server_context_impl {
/* is_prompt = */ true);
slot.prompt.tokens.push_back(cur_tok);

// break at the last user message, or at user messages at least min step past the last checkpoint
// break at the last user message, or at user messages at least min step past the last checkpoint.
// Continuum fork: not when the request carries her view spans: then the view end below is
// where her next prompt diverges, and blind step checkpoints only pile up (CONTINUUM-ENGINE
// 4.7.2-4.7.3; the M5, 2026-11-16: up to 32 x ~110 MB per slot thrashed the host)
if (do_checkpoint && spans.is_user_start(slot.prompt.n_tokens())) {
const auto pos = slot.prompt.n_tokens();
const auto & checkpoints = slot.prompt.checkpoints;

if (pos == last_user_pos || checkpoints.empty() || pos > checkpoints.back().n_tokens + checkpoint_step(slot)) {
break;
if (checkpoint_at_user_start(view_end_pos >= 0, checkpoints.empty(), slot.prompt.n_tokens(), last_user_pos,
checkpoints.empty() ? 0 : checkpoints.back().n_tokens, checkpoint_step(slot))) {
break; // server-checkpoint-policy.h
}
}

Expand Down