diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 408d42e577dd..583e68a19cfc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,10 +2,10 @@ 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-checkpoint-store test-server-checkpoint-store.cpp) +target_compile_features(test-server-checkpoint-store PRIVATE cxx_std_17) +add_test(NAME test-server-checkpoint-store COMMAND test-server-checkpoint-store) +set_tests_properties(test-server-checkpoint-store PROPERTIES LABELS "main") add_executable(test-server-prefill test-server-prefill.cpp) target_compile_features(test-server-prefill PRIVATE cxx_std_17) diff --git a/tests/test-server-checkpoint-policy.cpp b/tests/test-server-checkpoint-policy.cpp deleted file mode 100644 index 5943b236654a..000000000000 --- a/tests/test-server-checkpoint-policy.cpp +++ /dev/null @@ -1,153 +0,0 @@ -// 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 -#include -#include -#include - -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; - -// 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; -} diff --git a/tests/test-server-checkpoint-store.cpp b/tests/test-server-checkpoint-store.cpp new file mode 100644 index 000000000000..f7a84bc6da42 --- /dev/null +++ b/tests/test-server-checkpoint-store.cpp @@ -0,0 +1,211 @@ +// what this catches: the one owner of a mind's checkpoints (server-checkpoint-store.h) losing what her next act +// needs. Model-free: the store runs over a checkpoint shaped like the server's. Covers placed checkpoints +// (CONTINUUM-ENGINE 4.7.3: her head, the restore point when her next prompt diverges before her view end, kept +// while anything else may go; Codex on llama.cpp#121 and #127) and the learn pins and the single eviction order +// that replaced five separate erase sites (2026-11-16: the close-checkpoint eviction erased pins, and the host +// budget and the count evicted in two different orders). +#include "../tools/server/server-checkpoint-store.h" + +#include +#include +#include + +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 + uint64_t pin = 0; + int32_t pos_max = 0; + size_t bytes = 100; + size_t size() const { return bytes; } +}; + +using store = checkpoint_store; +static auto quiet = [](const ckpt &) {}; + +static store::admission at(int id_task, size_t capacity, int32_t view_end, int64_t step = 0) { + return { id_task, step, capacity, view_end }; +} + +// create_checkpoint's order: make room, copy (may fail: nothing stored), keep +static void create(store & s, const store::admission & a, ckpt c, bool copy_ok = true, uint64_t pin = 0) { + s.make_room(a, quiet); + if (copy_ok) { + s.keep(std::move(c), a, pin, quiet); + } +} + +static bool holds(const store & s, int serial) { + for (const auto & c : s) { + if (c.serial == serial) { + return true; + } + } + return false; +} + +static void head_survives_a_full_store_across_many_acts() { + store s; + int serial = 0; + create(s, at(1, 4, 100000), { 9655, 1, ++serial }); + const int head = s.front().serial; + for (int act = 2; act < 60; ++act) { + create(s, at(act, 4, 100000), { 9655 + act * 1000, act, ++serial }); + require(s.size() <= 4, "never more than the capacity"); + require(s.front().serial == head && s.front().n_tokens == 9655, "her exact head survives every act"); + } +} + +static void capacity_one_keeps_only_what_fits() { + store s; + create(s, at(1, 1, 50000), { 9655, 1, 1 }); + create(s, at(2, 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) { + create(s, at(i, 8, 50000), { 9655 + i * 1000, i, i }); + } + create(s, at(9, 4, 50000), { 20000, 9, 9 }, /*copy_ok=*/false); + require(s.size() == 3, "room was made, the failed copy stored nothing"); + require(s.front().serial == 0, "her head is still there"); +} + +static void a_view_end_checkpoint_supersedes_earlier_acts_but_the_head() { + store s; + create(s, at(1, 32, -1), { 9655, 1, 1 }); + create(s, at(2, 32, -1), { 20000, 2, 2 }); + create(s, at(3, 32, -1), { 30000, 3, 3 }); + create(s, at(7, 32, -1), { 40000, 7, 4 }); + create(s, at(7, 32, 45000), { 45000, 7, 5 }); + require(s.size() == 3 && s.front().serial == 1, "the two earlier acts' checkpoints are superseded; 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; + create(u, at(1, 32, -1), { 9655, 1, 1 }); + create(u, at(2, 32, -1), { 20000, 2, 2 }); + create(u, at(7, 32, 30000), { 25000, 7, 3 }); + require(u.size() == 3, "a checkpoint not AT her view end supersedes nothing"); +} + +static void without_view_spans_the_oldest_goes() { + store s; + create(s, at(1, 2, -1), { 9655, 1, 1 }); + create(s, at(2, 2, -1), { 20000, 2, 2 }); + create(s, at(3, 2, -1), { 30000, 3, 3 }); + require(s.front().serial == 2 && s.size() == 2, "without view spans, upstream's rule: the oldest goes"); +} + +static void a_cold_prompt_with_view_spans_makes_her_head() { + const int64_t user_starts[] = { 9655, 20000, 30000, 40000 }; + const int32_t view_end = 45000; + store s; + int serial = 0; + for (const int64_t pos : user_starts) { + if (s.stop_at_user_start(/*view_spans=*/true, pos, /*last_user_pos=*/40000, /*step=*/1024)) { + create(s, at(1, 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, at(1, 32, view_end), { view_end, 1, ++serial }); + require(s.size() == 2 && s.front().n_tokens == 9655 && s.back().n_tokens == view_end, "head, then her view end"); + store up; + create(up, at(1, 32, -1), { 30000, 1, 1 }); + require(up.stop_at_user_start(false, 40000, 40000, 1024), "upstream: her last user message"); + require(!up.stop_at_user_start(false, 30500, 40000, 1024), "upstream: not within a step"); + require(!up.stop_at_user_start(true, 40000, 40000, 1024), "with view spans, no blind step checkpoint"); +} + +// ---- the defects the single owner closes ---------------------------------------------------------------------- + +static void a_pin_survives_the_close_checkpoint_eviction() { + store s; + create(s, at(1, 32, -1, /*step=*/1000), { 3000, 1, 1 }); + create(s, at(1, 32, -1, 1000), { 3300, 1, 2 }, true, /*pin=*/7); // her pinned turn end + create(s, at(2, 32, -1, 1000), { 3305, 2, 3 }); // her next act, close by + require(holds(s, 2), "the pinned checkpoint stays however close an earlier one sits (no pin survived her next turn before)"); + store control; + create(control, at(1, 32, -1, 1000), { 3000, 1, 1 }); + create(control, at(1, 32, -1, 1000), { 3300, 1, 2 }); + create(control, at(2, 32, -1, 1000), { 3305, 2, 3 }); + require(!holds(control, 2), "the same checkpoint unpinned is evicted as too close (the rule still works)"); +} + +static void a_full_store_of_pins_keeps_one_more() { + store s; + create(s, at(1, 2, 50000), { 1000, 1, 1 }, true, 1); + create(s, at(2, 2, 50000), { 2000, 2, 2 }, true, 2); + create(s, at(3, 2, 50000), { 3000, 3, 3 }); + require(s.size() == 3 && holds(s, 1) && holds(s, 2), "pins are never evicted for count: one more is kept until they are released"); + require(s.release(1), "releasing pin 1 finds it"); + create(s, at(4, 2, 50000), { 4000, 4, 4 }); + require(!holds(s, 1) || !holds(s, 3), "once released, it may go"); + require(holds(s, 2), "the other pin stays"); +} + +static void the_budget_and_the_count_evict_in_one_order() { + // head 1000, middle 2000, newest 3000: the middle goes first, then the head, her newest (her resume point) last + store s; + create(s, at(1, 32, 3000), { 1000, 1, 1 }); + create(s, at(2, 32, 3000), { 2000, 2, 2 }); + create(s, at(3, 32, -1), { 3000, 3, 3 }); + require(s.evict_one(3000, quiet) == 100 && !holds(s, 2), "budget: the middle one first"); + require(s.evict_one(3000, quiet) == 100 && !holds(s, 1), "budget: then her head"); + require(s.evict_one(3000, quiet) == 100 && s.empty(), "budget: her newest last"); + require(s.evict_one(3000, quiet) == 0, "budget: nothing left reads 0, never a false eviction"); + // the count with two checkpoints keeps her newest (it used to evict the one after her head, which was the newest) + store t; + create(t, at(1, 2, 5000), { 1000, 1, 1 }); + create(t, at(2, 2, 5000), { 2000, 2, 2 }); + create(t, at(3, 2, 5000), { 3000, 3, 3 }); + require(holds(t, 3) && t.size() == 2, "count: the new one is kept"); + store pinned; + create(pinned, at(1, 32, 5000), { 1000, 1, 1 }, true, 9); + require(pinned.evict_one(5000, quiet) == 0, "budget: a store of pins gives nothing"); +} + +static void a_release_is_by_id_never_by_position() { + store s; + create(s, at(1, 32, -1), { 3300, 1, 1 }, true, 1); + create(s, at(2, 32, -1), { 3300, 2, 2 }, true, 2); // another turn pinned at the same position + require(s.release(1) && !s.release(1), "pin 1 releases once"); + require(s.release(2), "pin 2 is still held after pin 1's release"); + require(!s.release(0), "0 names no pin"); +} + +static void a_diverged_prompt_drops_what_lies_past_it_and_a_learn_cuts_back() { + store s; + ckpt a = { 1000, 1, 1 }; a.pos_max = 999; + ckpt b = { 3300, 1, 2 }; b.pos_max = 3299; + create(s, at(1, 32, -1), a); + create(s, at(1, 32, -1), b, true, 4); + require(s.drop_past_position(2000, quiet) == 1 && holds(s, 1) && !holds(s, 2), "past the divergence goes, pinned or not"); + create(s, at(2, 32, -1), { 5000, 2, 3 }); + require(s.cut_back_to(1000) == 1 && s.size() == 1, "a learn's cut back keeps only what lies within it"); +} + +int main() { + a_cold_prompt_with_view_spans_makes_her_head(); + head_survives_a_full_store_across_many_acts(); + 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(); + a_pin_survives_the_close_checkpoint_eviction(); + a_full_store_of_pins_keeps_one_more(); + the_budget_and_the_count_evict_in_one_order(); + a_release_is_by_id_never_by_position(); + a_diverged_prompt_drops_what_lies_past_it_and_a_learn_cuts_back(); + std::cout << "test-server-checkpoint-store: ok\n"; + return 0; +} diff --git a/tools/server/server-checkpoint-policy.h b/tools/server/server-checkpoint-policy.h deleted file mode 100644 index 6b5ccaeec3b7..000000000000 --- a/tools/server/server-checkpoint-policy.h +++ /dev/null @@ -1,67 +0,0 @@ -#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 -#include - -// 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. -// A checkpoint pinned for learning is never the victim; end() when every candidate is pinned. -template -typename List::iterator checkpoint_to_evict(List & checkpoints, int32_t view_end) { - auto victim = checkpoints.begin(); - if (view_end >= 0 && checkpoints.size() >= 2) { - ++victim; - } - while (victim != checkpoints.end() && victim->pin != 0) { - ++victim; - } - if (victim == checkpoints.end() && !checkpoints.empty() && checkpoints.front().pin == 0) { - return checkpoints.begin(); // only her head is left unpinned - } - 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 -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 && it->pin == 0) { - on_erase(*it); - it = checkpoints.erase(it); - ++erased; - continue; - } - ++it; - } - return erased; -} diff --git a/tools/server/server-checkpoint-store.h b/tools/server/server-checkpoint-store.h new file mode 100644 index 000000000000..a17e96ddb775 --- /dev/null +++ b/tools/server/server-checkpoint-store.h @@ -0,0 +1,205 @@ +#pragma once + +// Continuum fork: ONE owner of a mind's context checkpoints. Every rule that decides whether one is kept lives +// here and nowhere else: when one is made at a user turn, how close two may sit, how many fit, which goes to make +// room (by count or under the host budget), what her view end supersedes, what a diverged prompt invalidates, +// and a learn pin's lifetime. The list is private: a caller reads it or asks a verb, so a new rule cannot be +// added at a call site that forgets the others (2026-11-16: the close-checkpoint eviction erased learn pins, and +// the budget and the count evicted in two different orders). +// +// Pure over the checkpoint type `C` (it needs `n_tokens`, `id_task`, `pin`, `pos_max` and `size()`), so a native +// test proves every rule without a model (tests/test-server-checkpoint-store.cpp). + +#include +#include +#include +#include + +template +class checkpoint_store { +public: + using list_type = std::list; + + checkpoint_store() = default; + explicit checkpoint_store(list_type restored) : cks(std::move(restored)) {} + + // ---- reading ---------------------------------------------------------------------------------------------- + typename list_type::const_iterator begin() const { return cks.begin(); } + typename list_type::const_iterator end() const { return cks.end(); } + typename list_type::const_reverse_iterator rbegin() const { return cks.rbegin(); } + typename list_type::const_reverse_iterator rend() const { return cks.rend(); } + size_t size() const { return cks.size(); } + bool empty() const { return cks.empty(); } + const C & front() const { return cks.front(); } + const C & back() const { return cks.back(); } + const list_type & all() const { return cks; } + + size_t bytes() const { + size_t b = 0; + for (const auto & c : cks) { + b += c.size(); + } + return b; + } + + // 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. 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. + bool stop_at_user_start(bool view_spans, int64_t pos, int64_t last_user_pos, int64_t step) const { + if (cks.empty()) { + return true; + } + if (view_spans) { + return false; + } + return pos == last_user_pos || pos > cks.back().n_tokens + step; + } + + // ---- making one ------------------------------------------------------------------------------------------- + struct admission { + int id_task; // the act making it + int64_t step; // checkpoints an earlier act left closer than this to the previous one go + size_t capacity; // the most this mind keeps + int32_t view_end; // her view end (-1: no view spans) + }; + + // Before a copy is made: the close ones earlier acts left go, then room by count (the one eviction order). + // A count already full of learn pins keeps one more until they are learned or released. + template + void make_room(const admission & a, OnErase on_erase) { + int64_t last = -1; + for (auto it = cks.begin(); it != cks.end();) { + if (it->id_task != a.id_task && it->pin == 0 && last >= 0 && it->n_tokens <= last + a.step) { + on_erase(*it); + it = cks.erase(it); + continue; + } + last = it->n_tokens; + ++it; + } + while (cks.size() >= a.capacity) { + auto v = victim(a.view_end); + if (v == cks.end()) { + break; + } + on_erase(*v); + cks.erase(v); + } + } + + // The copy made: kept, pinned when `pin` is nonzero, and her view end supersedes the checkpoints earlier acts + // left on this prefix (her head and every pin stay). Returns the stored checkpoint. + template + const C & keep(C && made, const admission & a, uint64_t pin, OnErase on_erase) { + made.pin = pin; + cks.push_back(std::move(made)); + const C & kept = cks.back(); + if (a.view_end >= 0 && kept.n_tokens == a.view_end) { + const C * head = &cks.front(); + for (auto it = cks.begin(); it != cks.end();) { + if (&*it != head && &*it != &kept && it->id_task != a.id_task && it->pin == 0) { + on_erase(*it); + it = cks.erase(it); + continue; + } + ++it; + } + } + return cks.back(); + } + + // ---- the host budget -------------------------------------------------------------------------------------- + // One checkpoint out, in the one eviction order; the bytes it held (0: nothing left that may go). + template + size_t evict_one(int32_t view_end, OnErase on_erase) { + auto v = victim(view_end); + if (v == cks.end()) { + return 0; + } + const size_t freed = v->size(); + on_erase(*v); + cks.erase(v); + return freed; + } + + // ---- her prompt moved ------------------------------------------------------------------------------------- + // A resume whose prompt diverged at `pos_next`: a checkpoint past it no longer describes her sequence, pinned + // or not (its turn cannot be learned from this sequence any more). + template + size_t drop_past_position(int32_t pos_next, OnErase on_erase) { + size_t n = 0; + for (auto it = cks.begin(); it != cks.end();) { + if (it->pos_max > pos_next) { + on_erase(*it); + it = cks.erase(it); + ++n; + continue; + } + ++it; + } + return n; + } + + // Her sequence cut back to its first `n_tokens` (the slot learn's restore): what lies past it goes. + size_t cut_back_to(int64_t n_tokens) { + const size_t before = cks.size(); + cks.remove_if([&](const C & c) { return c.n_tokens > n_tokens; }); + return before - cks.size(); + } + + // ---- learn pins --------------------------------------------------------------------------------------------- + // Pin `pin` released (an abandoned turn): the checkpoint stays as an ordinary one. True when one held it. + bool release(uint64_t pin) { + for (auto & c : cks) { + if (pin != 0 && c.pin == pin) { + c.pin = 0; + return true; + } + } + return false; + } + + // The turn ending at `n_tokens` was learned: its pin is done. + void learned_at(int64_t n_tokens) { + for (auto & c : cks) { + if (c.pin != 0 && c.n_tokens == n_tokens) { + c.pin = 0; + } + } + } + + // ---- whole sets (a page restored, a mind back from flash, a slot emptied) ---------------------------------- + void replace(list_type restored) { cks = std::move(restored); } + list_type take() { list_type out; out.swap(cks); return out; } + void clear() { list_type().swap(cks); } + +private: + list_type cks; + + // The one eviction order. Never a pinned checkpoint. First the oldest that is neither her head (with view + // spans, her restore point when her next prompt diverges before her view end) nor her newest (where her next + // act resumes: her context only appends); then her head; her newest last. end(): nothing may go. + typename list_type::iterator victim(int32_t view_end) { + auto newest = cks.end(); + for (auto it = cks.begin(); it != cks.end(); ++it) { + if (newest == cks.end() || it->n_tokens > newest->n_tokens) { + newest = it; + } + } + const auto head = view_end >= 0 ? cks.begin() : cks.end(); + for (auto it = cks.begin(); it != cks.end(); ++it) { + if (it != head && it != newest && it->pin == 0) { + return it; + } + } + if (head != cks.end() && head != newest && head->pin == 0) { + return head; + } + if (newest != cks.end() && newest->pin == 0) { + return newest; + } + return cks.end(); + } +}; diff --git a/tools/server/server-context-learn.inc b/tools/server/server-context-learn.inc index bb92ec76bd9e..e9edf60b09e5 100644 --- a/tools/server/server-context-learn.inc +++ b/tools/server/server-context-learn.inc @@ -634,11 +634,7 @@ llama_memory_seq_rm(llama_get_memory(ctx_tgt), pending.side, -1, -1); park_free.push_back(pending.side); if (pending.began && ok && !pending.drained) { - for (auto & c : slot.prompt.checkpoints) { - if (c.pin != 0 && c.n_tokens == n0) { - c.pin = 0; - } - } + slot.prompt.checkpoints.learned_at(n0); } } else if (!pending.began || !ok) { llama_memory_seq_rm(llama_get_memory(ctx_tgt), slot.id, n0, -1); @@ -648,7 +644,7 @@ if (cp) { cp->load_dft(ctx_dft, slot.id, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY); } } slot.prompt.tokens.keep_first(n0); - slot.prompt.checkpoints.remove_if([&](const common_prompt_checkpoint & c) { return c.n_tokens > n0; }); + slot.prompt.checkpoints.cut_back_to(n0); } learn_outcome out; out.learned_on = pending.side >= 0 ? learn_outcome::learned_on_kind::side_sequence : learn_outcome::learned_on_kind::slot; diff --git a/tools/server/server-context-minds.inc b/tools/server/server-context-minds.inc index cdb907a8aeb4..10b25bf98c80 100644 --- a/tools/server/server-context-minds.inc +++ b/tools/server/server-context-minds.inc @@ -25,23 +25,12 @@ std::vector released; // pins released while she was on flash }; - // release learn pin `pin` in `cks`; true when a checkpoint held it - static bool unpin(std::list & cks, uint64_t pin) { - for (auto & c : cks) { - if (c.pin == pin) { - c.pin = 0; - return true; - } - } - return false; - } - // An abandoned turn's learn pin, released wherever she is: on a lane, parked, or on flash (applied when her // checkpoints come back). The checkpoint stays as an ordinary one: the budget may now evict it. pin_release learn_release(mind_page page, uint64_t pin) { for (auto & s : slots) { if (s.page == page) { - return unpin(s.prompt.checkpoints, pin) ? pin_release::released : pin_release::not_pinned; + return s.prompt.checkpoints.release(pin) ? pin_release::released : pin_release::not_pinned; } } resident_mind * m = find_mind(page); @@ -52,7 +41,7 @@ m->released.push_back(pin); // her checkpoints are in her flash bytes return pin_release::released_on_flash; } - return unpin(m->prompt.checkpoints, pin) ? pin_release::released : pin_release::not_pinned; + return m->prompt.checkpoints.release(pin) ? pin_release::released : pin_release::not_pinned; } // her checkpoints as bytes beside her state: count, then per checkpoint its position fields, three sizes and @@ -190,14 +179,14 @@ const uint64_t h[3] = { n_tgt, n_dft, n_ck }; std::memcpy(w, h, sizeof(h)); w += sizeof(h); - ckpt_write(cold->prompt.checkpoints, w + n_tgt + n_dft); + ckpt_write(cold->prompt.checkpoints.all(), w + n_tgt + n_dft); if (llama_state_seq_get_data_ext(ctx_tgt, w, n_tgt, seq, LLAMA_STATE_SEQ_FLAGS_NONE) != n_tgt || (ctx_dft && llama_state_seq_get_data_ext(ctx_dft, w + n_tgt, n_dft, seq, LLAMA_STATE_SEQ_FLAGS_NONE) != n_dft)) { SRV_ERR("mind %016llx: her state could not be copied out of the pool; she stays where she is\n", (unsigned long long) cold->page.hi); return false; } const int64_t t_copy = ggml_time_us() - t0; - std::list().swap(cold->prompt.checkpoints); // their bytes are in hers now + cold->prompt.checkpoints.clear(); // their bytes are in hers now llama_memory_seq_rm(llama_get_memory(ctx_tgt), seq, -1, -1); if (ctx_dft) { llama_memory_seq_rm(llama_get_memory(ctx_dft), seq, -1, -1); @@ -307,9 +296,9 @@ } SRV_INF("mind %016llx%016llx back from %s: %.1f MiB inserted in %.2f ms\n", (unsigned long long) m.page.hi, (unsigned long long) m.page.lo, ram ? "RAM" : "flash", size / (1024.0 * 1024.0), (ggml_time_us() - t0) / 1000.0); - m.prompt.checkpoints = std::move(checkpoints); + m.prompt.checkpoints.replace(std::move(checkpoints)); for (const uint64_t pin : m.released) { - unpin(m.prompt.checkpoints, pin); // released while she was on flash + m.prompt.checkpoints.release(pin); // released while she was on flash } m.released.clear(); // her file is removed on the page-IO worker, ordered after its own write (it may still be landing) diff --git a/tools/server/server-context-page.inc b/tools/server/server-context-page.inc index 5672a599217d..cf30c503e039 100644 --- a/tools/server/server-context-page.inc +++ b/tools/server/server-context-page.inc @@ -713,7 +713,7 @@ slot->prompt.tokens = std::move(*restored); slot->lora = std::move(page_lora); if (job.checkpoints_ok) { - slot->prompt.checkpoints = std::move(job.checkpoints); + slot->prompt.checkpoints.replace(std::move(job.checkpoints)); SLT_INF(*slot, "restored %zu context checkpoints from the sidecar: %s\n", slot->prompt.checkpoints.size(), job.ckpt_path.c_str()); } else { slot->prompt.checkpoints.clear(); diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index 29537a88f4cf..a021384f875f 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -1,6 +1,6 @@ #include #include "server-context.h" -#include "server-checkpoint-policy.h" +#include "server-checkpoint-store.h" #include "server-view-end.h" #include "server-mapped-file.h" #include @@ -2695,7 +2695,7 @@ struct server_context_impl { } // Room for a checkpoint of `bytes` under the budget: the slot holding the most checkpoint bytes gives up its - // least valuable one (checkpoint_to_evict keeps her head while anything else is left), until it fits. False: + // least valuable one in her store's one eviction order (server-checkpoint-store.h), until it fits. False: // it cannot fit even with every checkpoint gone (larger than the budget); the caller makes none. bool make_room_for_checkpoint(size_t bytes) { if (host_budget == 0) { @@ -2709,13 +2709,11 @@ struct server_context_impl { } size_t held = checkpoint_bytes(false); // what stays resident: copies on their way to flash are leaving while (held + bytes > host_budget) { + // the lane holding the most gives one up, in her store's one eviction order (never a pin) server_slot * from = nullptr; size_t most = 0; for (auto & s : slots) { - size_t b = 0; - for (const auto & c : s.prompt.checkpoints) { - b += c.size(); - } + const size_t b = s.prompt.checkpoints.bytes(); if (b > most) { most = b; from = &s; @@ -2724,32 +2722,14 @@ struct server_context_impl { if (from == nullptr) { return false; } - // her newest checkpoint is where her next act resumes (her context only appends): it goes last. - // Otherwise the store's own order (her head kept while anything else is left), never a pinned one. - auto & cks = from->prompt.checkpoints; - auto newest = cks.end(); - for (auto it = cks.begin(); it != cks.end(); ++it) { - if (newest == cks.end() || it->n_tokens > newest->n_tokens) { - newest = it; - } - } - auto victim = cks.end(); - for (auto it = cks.begin(); it != cks.end(); ++it) { - if (it != newest && it->pin == 0) { - victim = it; - break; - } - } - if (victim == cks.end() && newest != cks.end() && newest->pin == 0) { - victim = newest; // all that is left - } - if (victim == cks.end()) { + const size_t freed = from->prompt.checkpoints.evict_one(from->prompt.view_end, [&](const common_prompt_checkpoint & c) { + SLT_WRN(*from, "host budget: erasing a checkpoint (n_tokens = %" PRId64 ", %.3f MiB) for a new one of %.3f MiB\n", + c.n_tokens, (float) c.size() / 1024 / 1024, (float) bytes / 1024 / 1024); + }); + if (freed == 0) { return false; // every checkpoint left is pinned for learning } - SLT_WRN(*from, "host budget: erasing a checkpoint (n_tokens = %" PRId64 ", %.3f MiB) for a new one of %.3f MiB\n", - victim->n_tokens, (float) victim->size() / 1024 / 1024, (float) bytes / 1024 / 1024); - held -= victim->size(); - from->prompt.checkpoints.erase(victim); + held -= freed; } return true; } @@ -2758,37 +2738,13 @@ struct server_context_impl { void create_checkpoint(server_slot & slot, const int64_t n_tokens_cur, llama_pos pos_min, llama_pos pos_max) { const int id_task = slot.task->id; - // evict checkpoints within min-step of a previous checkpoint, unless they were - // created by the current task - const int64_t step = checkpoint_step(slot); - int64_t last = -1; - for (auto it = slot.prompt.checkpoints.begin(); it != slot.prompt.checkpoints.end(); ) { - // a checkpoint pinned for learning stays until learned or released, however close (/learn/release) - if (it->id_task != id_task && it->pin == 0 && last >= 0 && it->n_tokens <= last + step) { - SLT_TRC(slot, "erasing context checkpoint too close to an earlier one (pos_min = %d, pos_max = %d, n_tokens = %" PRId64 ", size = %.3f MiB)\n", - it->pos_min, it->pos_max, it->n_tokens, (float) it->size() / 1024 / 1024); - - it = slot.prompt.checkpoints.erase(it); - continue; - } - - last = it->n_tokens; - ++it; - } - - while (slot.prompt.checkpoints.size() >= (size_t) params_base.n_ctx_checkpoints) { - // 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); - if (victim == slot.prompt.checkpoints.end()) { - break; // every checkpoint is pinned for learning: the store holds one more until they are learned - } - - SLT_WRN(slot, "erasing old context checkpoint (pos_min = %d, pos_max = %d, n_tokens = %" PRId64 ", size = %.3f MiB)\n", - victim->pos_min, victim->pos_max, victim->n_tokens, (float) victim->size() / 1024 / 1024); - - slot.prompt.checkpoints.erase(victim); - } + // the store makes room: the close ones earlier acts left, then by count (server-checkpoint-store.h) + const checkpoint_store::admission admission { + id_task, checkpoint_step(slot), (size_t) params_base.n_ctx_checkpoints, slot.prompt.view_end }; + slot.prompt.checkpoints.make_room(admission, [&](const common_prompt_checkpoint & it) { + SLT_TRC(slot, "erasing context checkpoint to make room (pos_min = %d, pos_max = %d, n_tokens = %" PRId64 ", size = %.3f MiB)\n", + it.pos_min, it.pos_max, it.n_tokens, (float) it.size() / 1024 / 1024); + }); // under the one host budget: quote the copy before making it (its peak), and make room first { @@ -2818,23 +2774,18 @@ struct server_context_impl { return; } - auto & kept = slot.prompt.checkpoints.emplace_back(std::move(cur)); - if (slot.task->params.learn_pin && kept.n_tokens == (int64_t) slot.task->n_tokens() - 1) { - kept.pin = ++learn_pin_clock; // the core will learn this turn: kept until learned or released (/learn/release) - slot.stats.mind_pin = kept.pin; + // a turn the core will learn keeps this checkpoint until learned or released (/learn/release) + const bool pin_it = slot.task->params.learn_pin && cur.n_tokens == (int64_t) slot.task->n_tokens() - 1; + const uint64_t pin = pin_it ? ++learn_pin_clock : 0; + const auto & kept = slot.prompt.checkpoints.keep(std::move(cur), admission, pin, [&](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); + }); + if (pin != 0) { + slot.stats.mind_pin = pin; slot.stats.mind_pinned_at = kept.n_tokens; } - // 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); - }); - fit_prompt_cache_to_budget(); // the cache yields what the checkpoints now hold SLT_TRC(slot, @@ -4141,15 +4092,9 @@ struct server_context_impl { { // erase any checkpoints with pos_max > pos_next - for (auto it = slot.prompt.checkpoints.begin(); it != slot.prompt.checkpoints.end();) { - const auto & cur = *it; - if (cur.pos_max > pos_next) { - SLT_TRC(slot, "erased invalidated context checkpoint (pos_min = %d, pos_max = %d, n_tokens = %" PRId64 ", n_swa = %d, pos_next = %d, size = %.3f MiB)\n", cur.pos_min, cur.pos_max, cur.n_tokens, n_swa, pos_next, (float) cur.size() / 1024 / 1024); - it = slot.prompt.checkpoints.erase(it); - } else { - ++it; - } - } + slot.prompt.checkpoints.drop_past_position(pos_next, [&](const common_prompt_checkpoint & cur) { + SLT_TRC(slot, "erased invalidated context checkpoint (pos_min = %d, pos_max = %d, n_tokens = %" PRId64 ", n_swa = %d, pos_next = %d, size = %.3f MiB)\n", cur.pos_min, cur.pos_max, cur.n_tokens, n_swa, pos_next, (float) cur.size() / 1024 / 1024); + }); } } @@ -4313,10 +4258,9 @@ struct server_context_impl { // 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 & checkpoints = slot.prompt.checkpoints; - 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 + if (slot.prompt.checkpoints.stop_at_user_start(view_end_pos >= 0, slot.prompt.n_tokens(), last_user_pos, + checkpoint_step(slot))) { + break; // server-checkpoint-store.h } } diff --git a/tools/server/server-task.h b/tools/server/server-task.h index e7ae1eeff1f6..ef97c388f4ef 100644 --- a/tools/server/server-task.h +++ b/tools/server/server-task.h @@ -1,6 +1,7 @@ #pragma once #include "server-mind.h" +#include "server-checkpoint-store.h" #include "common.h" #include "llama.h" @@ -772,7 +773,7 @@ struct server_task_result_learn : server_task_result { struct server_prompt { server_tokens tokens; - std::list checkpoints; + checkpoint_store checkpoints; // the one owner of her checkpoints (server-checkpoint-store.h) // Continuum fork: where the last prompt processed into these tokens put its view end, and the // first position of the checkpoints made for its end (-1 = unknown), for the checkpoint subset