diff --git a/tools/server/CMakeLists.txt b/tools/server/CMakeLists.txt index 5c92f28fbc24..899cb0504b69 100644 --- a/tools/server/CMakeLists.txt +++ b/tools/server/CMakeLists.txt @@ -21,6 +21,7 @@ add_library(${TARGET} STATIC server-routes-learn.cpp server-routes-control.cpp engine/memory_pool_llama.cpp + engine/mind_table_resident.cpp engine/responder_http.cpp engine/turn_results.cpp server-mapped-file.cpp diff --git a/tools/server/engine/mind_table.h b/tools/server/engine/mind_table.h new file mode 100644 index 000000000000..40a3c70cb758 --- /dev/null +++ b/tools/server/engine/mind_table.h @@ -0,0 +1,74 @@ +#pragma once + +// Continuum fork (CONTINUUM-ENGINE 4.12.1, 4.11.9): where each resident mind lives (a lane, parked in the pool, or on +// flash) and the policy for moving her. Every move it makes is memory_pool's (the one allocator); this class decides +// which mind and where, never how bytes move. Today's implementation is the #139 resident-minds code +// (mind_table_resident); a mind is never dropped or truncated, only moved between homes. +// +// Outcomes are typed: a caller matches each. Nothing here retries in a loop on its own or purges a mind. + +#include "memory_pool.h" + +#include "../server-mind.h" + +#include +#include +#include + +struct server_slot; + +// which minds make_room may send to flash +enum class room_from : uint8_t { + parked, // only minds parked in the pool + parked_then_idle_lanes, // parked first, then a mind on an idle lane +}; + +// which host bytes host_bytes counts +enum class host_count : uint8_t { + resident, // what stays: the parked minds' checkpoints + with_in_flight, // also a flashed mind's RAM copy until her file has landed (freed within ms of the write) +}; + +// What the table is built from: startup facts, never tuned at runtime. +struct mind_table_config { + int32_t n_seq = 0; // sequence ids the pool has (lanes + parked) + int32_t n_lanes = 0; // slots that decode + int32_t n_minds = 0; // resident minds the core asked for; 0 = the table is off + bool kv_unified = false; // parking needs the unified pool + std::string page_dir; // where flash pages live +}; + +class mind_table { +public: + virtual ~mind_table() = default; + + // whether resident minds are on for this engine (n_minds > 0, unified pool, a page directory) + virtual bool enabled() const = 0; + + // where `page` is now, starting her journey toward the pool if she is on flash (the core asks ahead of her turn) + virtual mind_place prefetch(mind_page page) = 0; + + // put `page` on `slot` for her turn: from the slot itself, a parked sequence, or flash. false: she could not be + // seated (the pool cannot hold her even after make_room); she stays where she was, never lost + [[nodiscard]] virtual bool seat(server_slot & slot, mind_page page) = 0; + + // free room by sending a mind to flash: the coldest parked mind, and with parked_then_idle_lanes an idle lane's + // mind when none is parked. false: no move of that scope exists (none parked, every lane busy). Each caller names + // its scope: freeing host bytes for a checkpoint asks parked minds only; a placement that found no cells may also + // take an idle lane's mind. + [[nodiscard]] virtual bool make_room(room_from scope) = 0; + + // release a learn pin wherever her checkpoints are (a lane, parked, or on flash) + virtual pin_release release_pin(mind_page page, uint64_t pin) = 0; + + // landed flash writes free their RAM copies; failed ones are written again + virtual void reap() = 0; + + // host bytes the table's minds hold outside the pool (their checkpoints; their RAM copies by `count`) + virtual size_t host_bytes(host_count count) const = 0; + + // parked sequence ids lent to a learning side sequence and given back + [[nodiscard]] virtual std::optional lend_id() = 0; + virtual void return_id(llama_seq_id id) = 0; + virtual size_t free_ids() const = 0; +}; diff --git a/tools/server/server-context-minds.inc b/tools/server/engine/mind_table_resident.cpp similarity index 79% rename from tools/server/server-context-minds.inc rename to tools/server/engine/mind_table_resident.cpp index e5d28c8490db..879a268df6e6 100644 --- a/tools/server/server-context-minds.inc +++ b/tools/server/engine/mind_table_resident.cpp @@ -1,3 +1,6 @@ +// Continuum fork (CONTINUUM-ENGINE 4.12.1): the resident-minds table of #139 behind mind_table. Moved here whole from +// server-context-minds.inc (deleted); the engine's fields it read became its dependencies (mind_table_config, the +// memory pool, the page-IO worker, the slots). // Methods of server_context_impl: resident minds and the switch between them (server-mind.h). // // Every mind the core names has exactly one home: @@ -10,6 +13,93 @@ // The pool runs out of CELLS, not of minds: when a decode finds no room, the coldest parked mind goes to flash // (was: the idle slot's mind was purged). A mind on flash comes back by mapping her file and inserting it. +#include "mind_table_resident.h" + +#include "../server-mapped-file.h" +#include "../server-page-io.h" +#include "../server-slot.h" + +#include +#include + +namespace { + +class mind_table_resident final : public mind_table { +public: + mind_table_resident(const mind_table_config & cfg, memory_pool & pool, server_page_io_pool & page_io, + std::vector & slots) + : cfg_(cfg), pool_(pool), page_io_(page_io), slots_(slots) { + if (cfg_.n_minds <= 0) { + return; // the table is off: no parked ids, enabled() is false + } + // the parked ids are what the context reports beyond the lanes (llama_n_seq_max), never assumed + for (int32_t s = cfg_.n_seq - 1; s >= cfg_.n_lanes; --s) { + park_free.push_back(s); + } + minds.reserve((size_t) cfg_.n_minds * 2); + SRV_INF("resident minds: %d lanes, %zu parked sequence ids (context reports %d), flash at %s\n", + cfg_.n_lanes, park_free.size(), cfg_.n_seq, cfg_.page_dir.c_str()); + } + +private: + const mind_table_config cfg_; + memory_pool & pool_; + server_page_io_pool & page_io_; + std::vector & slots_; + + // the coldest parked mind goes to flash; else an idle lane's mind does (moved here from the slots fragment's + // try_clear_idle_slots: one make-room policy, one owner) + bool make_room(room_from scope) override { + if (flash_coldest()) { + return true; + } + if (scope == room_from::parked) { + return false; + } + for (auto & slot : slots_) { + if (!slot.is_processing() && slot.page.valid() && slot.prompt.n_tokens() > 0) { + resident_mind m; + m.page = slot.page; + m.lora = slot.lora; + m.last_use = ++mind_clock; + m.generation = slot.page_generation + 1; + m.prompt = std::move(slot.prompt); + slot.prompt.clear(); + if (flash_out(m, slot.id)) { + slot.page = mind_page{}; + minds.push_back(std::move(m)); + return true; + } + slot.prompt = std::move(m.prompt); // she stays whole on her lane + } + } + return false; + } + + std::optional lend_id() override { + if (park_free.empty()) { + return std::nullopt; + } + const llama_seq_id id = park_free.back(); + park_free.pop_back(); + return id; + } + void return_id(llama_seq_id id) override { park_free.push_back(id); } + + size_t host_bytes(host_count count) const override { + size_t b = 0; + for (const auto & m : minds) { + for (const auto & c : m.prompt.checkpoints) { + b += c.size(); + } + if (count == host_count::with_in_flight && m.ram) { + b += m.ram->size(); + } + } + return b; + } + size_t free_ids() const override { return park_free.size(); } + struct resident_mind { mind_page page; server_prompt prompt; // her tokens, checkpoints and view end (host bookkeeping) @@ -38,8 +128,8 @@ // 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) { + pin_release release_pin(mind_page page, uint64_t pin) override { + for (auto & s : slots_) { if (s.page == page) { return unpin(s.prompt.checkpoints, pin) ? pin_release::released : pin_release::not_pinned; } @@ -99,33 +189,9 @@ std::vector park_free; // free parked sequence ids (sized once at load) std::vector minds; // parked and flashed minds (reserved once at load) uint64_t mind_clock = 0; - uint64_t learn_pin_clock = 0; // learn pins issued by this process (a pin's id; 0 is none) - // At load: the parked ids are what the context reports beyond the lanes (llama_n_seq_max), never assumed. - bool minds_init() { - const int32_t n_seq = (int32_t) llama_n_seq_max(ctx_tgt); - const int32_t n_lane = params_base.n_parallel; - if (params_base.n_minds <= 0) { - return true; - } - if (!params_base.kv_unified) { - SRV_ERR("%s", "--minds needs --kv-unified: a parked mind shares the pool's cells by sequence id\n"); - return false; - } - if (params_base.slot_save_path.empty()) { - SRV_ERR("%s", "--minds needs --slot-save-path: a mind the pool cannot hold goes to flash, never dropped\n"); - return false; - } - for (int32_t s = n_seq - 1; s >= n_lane; --s) { - park_free.push_back(s); - } - minds.reserve((size_t) params_base.n_minds * 2); - SRV_INF("resident minds: %d lanes, %zu parked sequence ids (context reports %d), flash at %s\n", - n_lane, park_free.size(), n_seq, params_base.slot_save_path.c_str()); - return true; - } - bool minds_on() const { return !park_free.empty() || !minds.empty(); } + bool enabled() const override { return !park_free.empty() || !minds.empty(); } resident_mind * find_mind(mind_page page) { for (auto & m : minds) { @@ -137,13 +203,13 @@ } void seq_move(llama_seq_id from, llama_seq_id to) { - GGML_ASSERT(pool->move(from, to) == edit_outcome::done); // whole-sequence removals are never refused + GGML_ASSERT(pool_.move(from, to) == edit_outcome::done); // whole-sequence removals are never refused } std::string mind_file(mind_page page) const { char name[64]; std::snprintf(name, sizeof(name), "mind-%016llx%016llx.bin", (unsigned long long) page.hi, (unsigned long long) page.lo); - return params_base.slot_save_path + name; + return cfg_.page_dir + name; } // The coldest parked mind leaves the pool for flash. False: nothing is parked. @@ -171,7 +237,7 @@ resident_mind * cold = &m; const int64_t t0 = ggml_time_us(); auto bytes = std::make_shared>(); - const seq_state_size n_state = pool->state_size(seq, LLAMA_STATE_SEQ_FLAGS_NONE); + const seq_state_size n_state = pool_.state_size(seq, LLAMA_STATE_SEQ_FLAGS_NONE); const size_t n_tgt = n_state.tgt; const size_t n_dft = n_state.dft; // her checkpoints go down with her (they are host bytes too: ~100 MB each on a 35B hybrid) @@ -185,13 +251,13 @@ std::memcpy(w, h, sizeof(h)); w += sizeof(h); ckpt_write(cold->prompt.checkpoints, w + n_tgt + n_dft); - if (pool->save(seq, LLAMA_STATE_SEQ_FLAGS_NONE, { w, n_tgt }, { w + n_tgt, n_dft }) != save_outcome::saved) { + if (pool_.save(seq, LLAMA_STATE_SEQ_FLAGS_NONE, { w, n_tgt }, { w + n_tgt, n_dft }) != save_outcome::saved) { 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 - GGML_ASSERT(pool->remove(seq, -1, -1, kv_contexts::both) == edit_outcome::done); + GGML_ASSERT(pool_.remove(seq, -1, -1, kv_contexts::both) == edit_outcome::done); cold->seq = -1; cold->file = mind_file(cold->page); cold->ram = bytes; // the record holds her bytes until her file has landed @@ -209,7 +275,7 @@ auto bytes = m.ram; auto done = m.written; done->store(0, std::memory_order_release); - page_io->submit(path, [path, bytes, done] { + page_io_.submit(path, [path, bytes, done] { FILE * f = std::fopen(path.c_str(), "wb"); bool ok = f != nullptr && std::fwrite(bytes->data(), 1, bytes->size(), f) == bytes->size(); ok = (f != nullptr && std::fclose(f) == 0) && ok; @@ -219,7 +285,7 @@ // Between steps: a landed file releases her RAM copy; a failed write keeps it (she lives in RAM) and is // written again. Never waits. - void minds_reap() { + void reap() override { for (auto & m : minds) { if (!m.ram || !m.written) { continue; @@ -275,8 +341,8 @@ // coldest parked mind to flash, and stops when none is left to send bool ok = false; while (true) { - GGML_ASSERT(pool->remove(seq, -1, -1, kv_contexts::both) == edit_outcome::done); - ok = pool->load(seq, LLAMA_STATE_SEQ_FLAGS_NONE, { r, h[0] }, { r + h[0], h[1] }) == load_outcome::loaded; + GGML_ASSERT(pool_.remove(seq, -1, -1, kv_contexts::both) == edit_outcome::done); + ok = pool_.load(seq, LLAMA_STATE_SEQ_FLAGS_NONE, { r, h[0] }, { r + h[0], h[1] }) == load_outcome::loaded; if (ok || !flash_coldest()) { break; } @@ -295,7 +361,7 @@ m.released.clear(); // her file is removed on the page-IO worker, ordered after its own write (it may still be landing) const std::string path = m.file; - page_io->submit(path, [path] { std::error_code ec; std::filesystem::remove(path, ec); }); + page_io_.submit(path, [path] { std::error_code ec; std::filesystem::remove(path, ec); }); m.file.clear(); m.ram.reset(); return true; @@ -303,8 +369,8 @@ // Ahead of her turn (the core names who is next): a mind on flash is inserted into a free parked sequence // now, while the others decode, so her switch is the sequence move. Returns where she is afterwards. - mind_place mind_prefetch(mind_page page) { - for (const auto & s : slots) { + mind_place prefetch(mind_page page) override { + for (const auto & s : slots_) { if (s.page == page) { return mind_place::lane; } @@ -339,7 +405,7 @@ // `slot` serves `page` next. The mind it holds (if any) is parked; hers comes back from where she is. // false: the switch could not complete (the leaving mind could not leave intact, or the arriving mind's state // could not be inserted): the request waits for a lane, never runs cold beside a stored version of her. - bool mind_switch(server_slot & slot, mind_page page) { + bool seat(server_slot & slot, mind_page page) override { slot.stats.mind_hi = page.hi; slot.stats.mind_lo = page.lo; if (slot.page == page) { @@ -427,3 +493,21 @@ (ggml_time_us() - t0 - t_out) / 1000.0, slot.prompt.n_tokens()); return true; } +}; + +} // namespace + +mind_table_build make_resident_minds(const mind_table_config & cfg, memory_pool & pool, server_page_io_pool & page_io, + std::vector & slots) { + mind_table_build out; + if (cfg.n_minds > 0 && !cfg.kv_unified) { + out.refusal = "--minds needs --kv-unified: a parked mind shares the pool's cells by sequence id"; + return out; + } + if (cfg.n_minds > 0 && cfg.page_dir.empty()) { + out.refusal = "--minds needs --slot-save-path: a mind the pool cannot hold goes to flash, never dropped"; + return out; + } + out.table = std::make_unique(cfg, pool, page_io, slots); + return out; +} diff --git a/tools/server/engine/mind_table_resident.h b/tools/server/engine/mind_table_resident.h new file mode 100644 index 000000000000..e12976983ccd --- /dev/null +++ b/tools/server/engine/mind_table_resident.h @@ -0,0 +1,21 @@ +#pragma once + +// Continuum fork (CONTINUUM-ENGINE 4.12.1): the #139 resident-minds table, built behind mind_table. The class itself +// is private to mind_table_resident.cpp; the engine holds only the interface. + +#include "mind_table.h" + +#include +#include +#include + +class server_page_io_pool; + +// the table, or why the configuration cannot have one (typed refusal, never a half-built table) +struct mind_table_build { + std::unique_ptr table; + std::string refusal; // non-empty when table is null +}; + +mind_table_build make_resident_minds(const mind_table_config & cfg, memory_pool & pool, server_page_io_pool & page_io, + std::vector & slots); diff --git a/tools/server/server-common.h b/tools/server/server-common.h index 6ea33aa2441c..9464aecb252a 100644 --- a/tools/server/server-common.h +++ b/tools/server/server-common.h @@ -362,7 +362,7 @@ json format_response_rerank( // // shared between server_slot and server_task_result_* -// Continuum fork: where the mind a request named came from when she took the lane (server-context-minds.inc) +// Continuum fork: where the mind a request named came from when she took the lane (engine/mind_table_resident.cpp) enum class mind_from : uint8_t { none, lane, parked, flash, ram, fresh, adopted, flash_pool_full }; struct server_slot_stats { diff --git a/tools/server/server-context-checkpoint.inc b/tools/server/server-context-checkpoint.inc index 88337fe9edca..06f23e8129f4 100644 --- a/tools/server/server-context-checkpoint.inc +++ b/tools/server/server-context-checkpoint.inc @@ -67,7 +67,7 @@ size_t host_budget = 0; // every host byte the engine holds beside the prompt cache: the lanes' checkpoints, the parked minds' - // checkpoints, and a flashed mind's RAM copy until her file has landed (server-context-minds.inc) + // checkpoints, and a flashed mind's RAM copy until her file has landed (engine/mind_table_resident.cpp) // `in_flight`: also a flashed mind's RAM copy (freed within ms of its write landing). The make-room decision // counts only what stays resident, so sending one mind down is seen to free her checkpoints. size_t checkpoint_bytes(bool in_flight = true) const { @@ -77,15 +77,7 @@ b += c.size(); } } - for (const auto & m : minds) { - for (const auto & c : m.prompt.checkpoints) { - b += c.size(); - } - if (in_flight && m.ram) { - b += m.ram->size(); - } - } - return b; + return b + residents->host_bytes(in_flight ? host_count::with_in_flight : host_count::resident); } // The prompt cache's share: the budget less every checkpoint (1 byte, not 0, when none is left: the @@ -110,7 +102,7 @@ return false; } // the coldest parked mind goes down to flash first (her checkpoints with her): the minds acting keep theirs - while (checkpoint_bytes(false) + bytes > host_budget && minds_on() && flash_coldest()) { + while (checkpoint_bytes(false) + bytes > host_budget && residents->enabled() && residents->make_room(room_from::parked)) { } size_t held = checkpoint_bytes(false); // what stays resident: copies on their way to flash are leaving while (held + bytes > host_budget) { diff --git a/tools/server/server-context-impl.h b/tools/server/server-context-impl.h index 733e75d39b33..c3a56c86ee88 100644 --- a/tools/server/server-context-impl.h +++ b/tools/server/server-context-impl.h @@ -6,6 +6,7 @@ #include "server-slot.h" #include "engine/memory_pool_llama.h" +#include "engine/mind_table_resident.h" #include "engine/responder_http.h" #include "engine/turn_results.h" @@ -120,6 +121,9 @@ struct server_context_impl { // the engine's one memory allocator (CONTINUUM-ENGINE 4.12.1), built in load_model once both contexts exist std::unique_ptr pool; + // where each resident mind lives and which to move (CONTINUUM-ENGINE 4.12.1), built in load_model after the pool + std::unique_ptr residents; + uint64_t learn_pin_clock = 0; // learn pins issued by this process (a pin's id; 0 is none); the learner's llama_model * model_dft = nullptr; llama_context * ctx_dft = nullptr; @@ -362,7 +366,6 @@ struct server_context_impl { #include "server-context-learn.inc" #include "server-context-learn-checkpoint.inc" #include "server-context-page.inc" -#include "server-context-minds.inc" #include "server-context-checkpoint.inc" @@ -437,8 +440,8 @@ struct server_context_impl { #endif void update_slots() { - if (minds_on()) { - minds_reap(); // a landed flash write frees her RAM copy; a failed one is written again + if (residents->enabled()) { + residents->reap(); // a landed flash write frees her RAM copy; a failed one is written again } const bool learning_at_start = bool(learning); publish_busy_slots(); diff --git a/tools/server/server-context-learn.inc b/tools/server/server-context-learn.inc index 5cddba9410ae..4e70b8a9b81d 100644 --- a/tools/server/server-context-learn.inc +++ b/tools/server/server-context-learn.inc @@ -445,7 +445,7 @@ } } // a side sequence: her own sequence is never touched (refused, typed, before anything is changed) - if (L.side && park_free.empty()) { + if (L.side && residents->free_ids() == 0) { return refused("no_side_sequence", "no side sequence is free to learn on (launch with --minds; every parked id holds a mind)"); } // the tokens from the last prompt token on, each labelled with the next @@ -478,8 +478,9 @@ // her shared prefix [0, side_from) re-tagged onto a side sequence (attention cells, nothing copied), // the checkpoint's recurrent state loaded there, then the served prompt's own tokens [side_from, n0) // decoded on it; the learn's own restore re-decodes one token there. Her sequence is never touched. - learn_seq = park_free.back(); - park_free.pop_back(); + const std::optional lent = residents->lend_id(); + GGML_ASSERT(lent.has_value()); // free_ids() was checked above, on this thread + learn_seq = *lent; llama_memory_seq_rm(llama_get_memory(ctx_tgt), learn_seq, -1, -1); llama_memory_seq_cp(llama_get_memory(ctx_tgt), slot.id, learn_seq, 0, side_from); if (cp != nullptr) { @@ -499,7 +500,7 @@ } if (rc != 0) { llama_memory_seq_rm(llama_get_memory(ctx_tgt), learn_seq, -1, -1); - park_free.push_back(learn_seq); + residents->return_id(learn_seq); ggml_opt_result_free(result); common_set_adapter_lora(ctx_tgt, slot.lora); settle_learning_residency(L.lora_id); @@ -632,7 +633,7 @@ // learned on a side sequence: it goes, her own sequence was never touched. The pin is released only // when the learn committed; an abandoned or failed one keeps it, so a retry still finds it. llama_memory_seq_rm(llama_get_memory(ctx_tgt), pending.side, -1, -1); - park_free.push_back(pending.side); + residents->return_id(pending.side); if (pending.began && ok && !pending.drained) { for (auto & c : slot.prompt.checkpoints) { if (c.pin != 0 && c.n_tokens == n0) { diff --git a/tools/server/server-context-load.inc b/tools/server/server-context-load.inc index 3313a6d213c8..aa76294e0e8e 100644 --- a/tools/server/server-context-load.inc +++ b/tools/server/server-context-load.inc @@ -233,9 +233,6 @@ if (!page_io) { page_io = std::make_unique((size_t) params_base.n_parallel); } - if (!minds_init()) { - return false; - } // try speculative decoding if (ctx_tgt_seq_rm_type != COMMON_CONTEXT_SEQ_RM_TYPE_NO) { @@ -259,6 +256,20 @@ } pool = std::make_unique(ctx_tgt, ctx_dft); + { + mind_table_config mcfg; + mcfg.n_seq = (int32_t) llama_n_seq_max(ctx_tgt); + mcfg.n_lanes = params_base.n_parallel; + mcfg.n_minds = params_base.n_minds; + mcfg.kv_unified = params_base.kv_unified; + mcfg.page_dir = params_base.slot_save_path; + mind_table_build built = make_resident_minds(mcfg, *pool, *page_io, slots); + if (!built.table) { + SRV_ERR("%s\n", built.refusal.c_str()); + return false; + } + residents = std::move(built.table); + } for (int i = 0; i < params_base.n_parallel; i++) { server_slot & slot = slots[i]; diff --git a/tools/server/server-context-page.inc b/tools/server/server-context-page.inc index 5f862f06e9d2..8cb55a3cace6 100644 --- a/tools/server/server-context-page.inc +++ b/tools/server/server-context-page.inc @@ -681,7 +681,7 @@ return; } std::vector().swap(job.body); - slot->page = mind_page{}; // a core page restore: the arriving request's mind adopts it (server-context-minds.inc) + slot->page = mind_page{}; // a core page restore: the arriving request's mind adopts it (engine/mind_table_resident.cpp) // 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 diff --git a/tools/server/server-context-slots.inc b/tools/server/server-context-slots.inc index 4861c9bdfed2..5f42f866bc42 100644 --- a/tools/server/server-context-slots.inc +++ b/tools/server/server-context-slots.inc @@ -117,9 +117,9 @@ } } - // a named mind switches by sequence moves (server-context-minds.inc), never through the prompt cache - if (ret && minds_on() && task.params.page.valid()) { - if (!mind_switch(*ret, task.params.page)) { + // a named mind switches by sequence moves (engine/mind_table_resident.cpp), never through the prompt cache + if (ret && residents->enabled() && task.params.page.valid()) { + if (!residents->seat(*ret, task.params.page)) { return nullptr; // the switch could not complete intact: this request waits (never runs cold) } update_cache = false; @@ -166,28 +166,9 @@ return res; } - // resident minds: the pool's coldest parked mind goes to flash, whole; an idle lane's mind is never purged - if (minds_on()) { - if (flash_coldest()) { - return true; - } - for (auto & slot : slots) { - if (!slot.is_processing() && slot.page.valid() && slot.prompt.n_tokens() > 0) { - resident_mind m; - m.page = slot.page; - m.lora = slot.lora; - m.last_use = ++mind_clock; - m.generation = slot.page_generation + 1; - m.prompt = std::move(slot.prompt); - slot.prompt.clear(); - if (flash_out(m, slot.id)) { - slot.page = mind_page{}; - minds.push_back(std::move(m)); - return true; - } - slot.prompt = std::move(m.prompt); // she stays whole on her lane - } - } + // resident minds: the coldest parked mind goes to flash, else an idle lane's mind does (mind_table) + if (residents->enabled() && residents->make_room(room_from::parked_then_idle_lanes)) { + return true; } for (auto & slot : slots) { diff --git a/tools/server/server-context-tasks.inc b/tools/server/server-context-tasks.inc index 89b166c956aa..b23f03f95eb2 100644 --- a/tools/server/server-context-tasks.inc +++ b/tools/server/server-context-tasks.inc @@ -222,7 +222,7 @@ { auto res = std::make_unique(); res->id = task.id; - res->outcome = learn_release(task.params.page, task.params.learn_release_pin); + res->outcome = residents->release_pin(task.params.page, task.params.learn_release_pin); turn_sink->deliver(std::move(res)); } break; case SERVER_TASK_TYPE_MIND_PREFETCH: @@ -230,7 +230,7 @@ const int64_t t0 = ggml_time_us(); auto res = std::make_unique(); res->id = task.id; - res->place = minds_on() ? mind_prefetch(task.params.page) : mind_place::minds_off; + res->place = residents->enabled() ? residents->prefetch(task.params.page) : mind_place::minds_off; res->elapsed_ms = (ggml_time_us() - t0) / 1000.0; turn_sink->deliver(std::move(res)); } break; diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index 586575f77e5b..81ff540b27e0 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -128,12 +128,12 @@ server_context_meta server_context::get_meta() const { { "allocated_tokens", llama_n_ctx(impl->ctx_tgt) }, { "max_tokens_per_sequence", impl->n_ctx_slot }, { "sequence_capacity", llama_n_seq_max(impl->ctx_tgt) }, - // the engine owns mind switches (server-context-minds.inc): a request naming its "page" is switched by + // the engine owns mind switches (engine/mind_table_resident.cpp): a request naming its "page" is switched by // sequence moves; the core reads this BEFORE dispatch to skip its own page save/restore (Codex on #139) { "resident_minds", json { - { "enabled", impl->minds_on() }, + { "enabled", impl->residents->enabled() }, { "lanes", impl->params_base.n_parallel }, - { "parked_ids", (int64_t) impl->park_free.size() }, + { "parked_ids", (int64_t) impl->residents->free_ids() }, { "incarnation", mind_incarnation() }, } }, }, @@ -614,7 +614,7 @@ std::unique_ptr server_routes::handle_slots_restore(const } // Continuum fork: POST /minds/prefetch {"page": ""}: her state comes from flash into a parked sequence now, -// ahead of her turn (server-context-minds.inc). The reply names where she is. +// ahead of her turn (engine/mind_table_resident.cpp). The reply names where she is. // an abandoned turn's learn pin, released (the core sends the turn receipt's mind.page and mind.pin) std::unique_ptr server_routes::handle_learn_release(const server_http_req & req) { auto res = create_response(); diff --git a/tools/server/server-context.h b/tools/server/server-context.h index 461704104b0a..44461cabe0bd 100644 --- a/tools/server/server-context.h +++ b/tools/server/server-context.h @@ -189,7 +189,7 @@ struct server_routes { server_http_context::handler_t post_prompt_cache_limit; server_http_context::handler_t get_page_io, post_page_io_budget; // the KV page IO budget (server-page-io.h) server_http_context::handler_t post_learn_release; // an abandoned turn's learn pin, released - server_http_context::handler_t post_minds_prefetch; // the next mind, from flash into the pool (server-context-minds.inc) + server_http_context::handler_t post_minds_prefetch; // the next mind, from flash into the pool (engine/mind_table_resident.cpp) server_http_context::handler_t get_train; server_http_context::handler_t post_train; server_http_context::handler_t post_train_cancel; diff --git a/tools/server/server-slot.h b/tools/server/server-slot.h index 3af54932f620..260f6d49a75c 100644 --- a/tools/server/server-slot.h +++ b/tools/server/server-slot.h @@ -328,7 +328,7 @@ struct server_slot { server_prompt prompt; - // Continuum fork: the mind whose sequence this lane holds (server-context-minds.inc); invalid: anonymous + // Continuum fork: the mind whose sequence this lane holds (engine/mind_table_resident.cpp); invalid: anonymous mind_page page; uint64_t page_generation = 0; // her state's generation on this lane diff --git a/tools/server/server-task.h b/tools/server/server-task.h index e7ae1eeff1f6..76024a2f427c 100644 --- a/tools/server/server-task.h +++ b/tools/server/server-task.h @@ -61,7 +61,7 @@ enum server_task_type { SERVER_TASK_TYPE_SLOT_RESTORE, SERVER_TASK_TYPE_SLOT_ERASE, SERVER_TASK_TYPE_MIND_PREFETCH, - SERVER_TASK_TYPE_LEARN_RELEASE, // Continuum fork: an abandoned turn's learn pin, released (server-context-minds.inc) // Continuum fork: the next mind, from flash into a parked sequence, ahead of her turn + SERVER_TASK_TYPE_LEARN_RELEASE, // Continuum fork: an abandoned turn's learn pin, released (engine/mind_table_resident.cpp) // Continuum fork: the next mind, from flash into a parked sequence, ahead of her turn SERVER_TASK_TYPE_SLOT_PAGE_IO_DONE, // Continuum fork: a save's or restore's file IO finished on its worker SERVER_TASK_TYPE_SET_LORA, SERVER_TASK_TYPE_LOAD_LORA,