llama-server: Use reference-counted checkpoints instead of deep-copies (#30137)
* checkpoints: reduce overhead Assisted-by: Codex * rebase Adapt the slot save/restore checkpoint appendix from #26004 to the shared_ptr checkpoint list and the common_state_data buffers. Assisted-by: Claude Opus 5.5 --------- Co-authored-by: cwriter <cwriter@localhost>
This commit is contained in:
1 parent
1fa7f1c488
commit
e152332a15
4 files changed
+72
-41
No files matched your search
@@ -358,9 +358,12 @@ struct server_slot {
|
||||
return false;
|
||||
}
|
||||
|
||||
llama_state_seq_get_data_ext(ctx_tgt, cur->data.main.data(), cur_size_tgt, id, LLAMA_STATE_SEQ_FLAGS_NONE);
|
||||
if (ctx_dft) {
|
||||
llama_state_seq_get_data_ext(ctx_dft, cur->data.drft.data(), cur_size_dft, id, LLAMA_STATE_SEQ_FLAGS_NONE);
|
||||
const size_t n_tgt = llama_state_seq_get_data_ext(ctx_tgt, cur->data.main.data(), cur_size_tgt, id, LLAMA_STATE_SEQ_FLAGS_NONE);
|
||||
const size_t n_dft = ctx_dft ? llama_state_seq_get_data_ext(ctx_dft, cur->data.drft.data(), cur_size_dft, id, LLAMA_STATE_SEQ_FLAGS_NONE) : 0;
|
||||
if (n_tgt != cur_size_tgt || n_dft != cur_size_dft) {
|
||||
SLT_ERR(*this, "%s", "failed to save complete prompt state\n");
|
||||
prompt_cache.states.pop_back();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -2731,21 +2734,22 @@ private:
|
||||
for (auto it = slot.prompt.checkpoints.begin();
|
||||
slot.prompt.checkpoints.size() + 1 >= (size_t) params_base.n_ctx_checkpoints &&
|
||||
it != slot.prompt.checkpoints.end(); ) {
|
||||
if (it->id_task != id_task && last >= 0 && it->n_tokens <= last + params_base.checkpoint_min_step) {
|
||||
const auto & cur = **it;
|
||||
if (cur.id_task != id_task && last >= 0 && cur.n_tokens <= last + params_base.checkpoint_min_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);
|
||||
cur.pos_min, cur.pos_max, cur.n_tokens, (float) cur.size() / 1024 / 1024);
|
||||
|
||||
it = slot.prompt.checkpoints.erase(it);
|
||||
continue;
|
||||
}
|
||||
|
||||
last = it->n_tokens;
|
||||
last = cur.n_tokens;
|
||||
++it;
|
||||
}
|
||||
|
||||
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();
|
||||
const auto & cur = *slot.prompt.checkpoints.front();
|
||||
|
||||
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);
|
||||
@@ -2757,8 +2761,8 @@ private:
|
||||
{
|
||||
const int64_t n_tokens_new = slot.prompt.n_tokens() - n_tokens_cur;
|
||||
for (auto it = slot.prompt.checkpoints.begin(); it != slot.prompt.checkpoints.end(); ) {
|
||||
if (it->n_tokens == n_tokens_new) {
|
||||
SLT_TRC(slot, "superseding context checkpoint at n_tokens = %" PRId64 "\n", it->n_tokens);
|
||||
if ((*it)->n_tokens == n_tokens_new) {
|
||||
SLT_TRC(slot, "superseding context checkpoint at n_tokens = %" PRId64 "\n", (*it)->n_tokens);
|
||||
it = slot.prompt.checkpoints.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
@@ -2766,7 +2770,8 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
auto & cur = slot.prompt.checkpoints.emplace_back();
|
||||
auto ckpt = std::make_shared<common_prompt_checkpoint>();
|
||||
auto & cur = *ckpt;
|
||||
|
||||
cur.id_task = id_task;
|
||||
|
||||
@@ -2780,6 +2785,8 @@ private:
|
||||
// stash the draft's speculative state with the checkpoint
|
||||
common_speculative_get_state(spec.get(), slot.id, cur.data_spec);
|
||||
|
||||
slot.prompt.checkpoints.push_back(std::move(ckpt));
|
||||
|
||||
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, cur.pos_min,
|
||||
@@ -2799,7 +2806,8 @@ private:
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ckpt_read_buf(std::ifstream & ifs, std::vector<uint8_t> & buf, size_t n_avail, size_t & n_read) {
|
||||
template <typename Buf>
|
||||
static bool ckpt_read_buf(std::ifstream & ifs, Buf & buf, size_t n_avail, size_t & n_read) {
|
||||
uint64_t n = 0;
|
||||
// check the size against the bytes left in the file before allocating, the size field may be corrupted
|
||||
if (!ckpt_read(ifs, &n, sizeof(n), n_read) || n > n_avail - n_read) {
|
||||
@@ -2814,7 +2822,8 @@ private:
|
||||
n_written += size;
|
||||
}
|
||||
|
||||
static void ckpt_write_buf(std::ofstream & ofs, const std::vector<uint8_t> & buf, size_t & n_written) {
|
||||
template <typename Buf>
|
||||
static void ckpt_write_buf(std::ofstream & ofs, const Buf & buf, size_t & n_written) {
|
||||
const uint64_t n = buf.size();
|
||||
ckpt_write(ofs, &n, sizeof(n), n_written);
|
||||
if (n > 0) {
|
||||
@@ -2839,7 +2848,8 @@ private:
|
||||
ckpt_write(ofs, &magic, sizeof(magic), n_written);
|
||||
ckpt_write(ofs, &version, sizeof(version), n_written);
|
||||
ckpt_write(ofs, &count, sizeof(count), n_written);
|
||||
for (const auto & cur : slot.prompt.checkpoints) {
|
||||
for (const auto & ckpt : slot.prompt.checkpoints) {
|
||||
const auto & cur = *ckpt;
|
||||
ckpt_write(ofs, &cur.n_tokens, sizeof(cur.n_tokens), n_written);
|
||||
ckpt_write(ofs, &cur.pos_min, sizeof(cur.pos_min), n_written);
|
||||
ckpt_write(ofs, &cur.pos_max, sizeof(cur.pos_max), n_written);
|
||||
@@ -2877,9 +2887,10 @@ private:
|
||||
SLT_WRN(slot, "invalid context checkpoint appendix in '%s' - ignored\n", filepath.c_str());
|
||||
return 0;
|
||||
}
|
||||
std::list<common_prompt_checkpoint> checkpoints;
|
||||
std::list<std::shared_ptr<common_prompt_checkpoint>> checkpoints;
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
common_prompt_checkpoint cur;
|
||||
auto ckpt = std::make_shared<common_prompt_checkpoint>();
|
||||
auto & cur = *ckpt;
|
||||
cur.id_task = -1; // not created by a task - marks a checkpoint restored from a slot file
|
||||
if (!ckpt_read(ifs, &cur.n_tokens, sizeof(cur.n_tokens), n_read) ||
|
||||
!ckpt_read(ifs, &cur.pos_min, sizeof(cur.pos_min), n_read) ||
|
||||
@@ -2895,23 +2906,23 @@ private:
|
||||
SLT_WRN(slot, "invalid context checkpoint appendix in '%s' - ignored\n", filepath.c_str());
|
||||
return 0;
|
||||
}
|
||||
checkpoints.push_back(std::move(cur));
|
||||
checkpoints.push_back(std::move(ckpt));
|
||||
if (checkpoints.size() > (size_t) params_base.n_ctx_checkpoints) {
|
||||
checkpoints.pop_front();
|
||||
}
|
||||
}
|
||||
// the slot file does not check the draft context - test-load one draft checkpoint, drop the draft data if it does not fit
|
||||
if (ctx_dft != nullptr && !checkpoints.empty() && !checkpoints.back().data_dft.empty()) {
|
||||
const bool ok = checkpoints.back().load_dft(ctx_dft, slot.id, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY);
|
||||
if (ctx_dft != nullptr && !checkpoints.empty() && !checkpoints.back()->data_dft.empty()) {
|
||||
const bool ok = checkpoints.back()->load_dft(ctx_dft, slot.id, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY);
|
||||
llama_memory_seq_rm(llama_get_memory(ctx_dft), slot.id, -1, -1);
|
||||
if (!ok) {
|
||||
SLT_WRN(slot, "draft context checkpoint data in '%s' does not match the draft context - dropped\n", filepath.c_str());
|
||||
for (auto & cur : checkpoints) {
|
||||
cur.clear_dft();
|
||||
cur->clear_dft();
|
||||
}
|
||||
}
|
||||
}
|
||||
slot.prompt.checkpoints = std::move(checkpoints);
|
||||
slot.prompt.checkpoints.assign(checkpoints.begin(), checkpoints.end());
|
||||
SLT_INF(slot, "restored %zu context checkpoint(s) from '%s'\n", slot.prompt.checkpoints.size(), filepath.c_str());
|
||||
return n_read;
|
||||
}
|
||||
@@ -4254,7 +4265,8 @@ private:
|
||||
const auto it = std::find_if(
|
||||
slot.prompt.checkpoints.rbegin(),
|
||||
slot.prompt.checkpoints.rend(),
|
||||
[&](const auto & cur) {
|
||||
[&](const auto & ckpt) {
|
||||
const auto & cur = *ckpt;
|
||||
// guarantee that a checkpoint will result in at least one token being processed [TAG_PROMPT_LOGITS]
|
||||
SLT_TRC(slot, "checking checkpoint with [%d, %d] against %d...\n", cur.pos_min, cur.pos_max, pos_min_thold);
|
||||
// workaround for [TAG_CHECKPOINTS_FIX_POS_MIN]
|
||||
@@ -4268,10 +4280,12 @@ private:
|
||||
bool do_reset = it == slot.prompt.checkpoints.rend();
|
||||
|
||||
if (!do_reset) {
|
||||
const auto & cur = **it;
|
||||
|
||||
// restore the context checkpoint
|
||||
if (!it->load_tgt(ctx_tgt, slot.id, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY) ||
|
||||
!it->load_dft(ctx_dft, slot.id, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY)) {
|
||||
if (it->id_task != -1) {
|
||||
if (!cur.load_tgt(ctx_tgt, slot.id, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY) ||
|
||||
!cur.load_dft(ctx_dft, slot.id, LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY)) {
|
||||
if (cur.id_task != -1) {
|
||||
GGML_ABORT("failed to restore context checkpoint\n");
|
||||
}
|
||||
// restored from a slot file, not guaranteed to load - fall back to full prompt re-processing
|
||||
@@ -4281,12 +4295,14 @@ private:
|
||||
}
|
||||
|
||||
if (!do_reset) {
|
||||
// restore the draft's speculative state
|
||||
common_speculative_set_state(spec.get(), slot.id, it->data_spec);
|
||||
const auto & cur = **it;
|
||||
|
||||
pos_next = std::min(pos_next, std::max(it->pos_min + 1, it->pos_max));
|
||||
n_past = std::min(slot.prompt.tokens.size_up_to_pos(pos_next), (size_t) it->n_tokens);
|
||||
SLT_TRC(slot, "restored context checkpoint (pos_min = %d, pos_max = %d, n_tokens = %" PRId64 ", n_past = %d, size = %.3f MiB)\n", it->pos_min, it->pos_max, it->n_tokens, n_past, (float) it->size() / 1024 / 1024);
|
||||
// restore the draft's speculative state
|
||||
common_speculative_set_state(spec.get(), slot.id, cur.data_spec);
|
||||
|
||||
pos_next = std::min(pos_next, std::max(cur.pos_min + 1, cur.pos_max));
|
||||
n_past = std::min(slot.prompt.tokens.size_up_to_pos(pos_next), (size_t) cur.n_tokens);
|
||||
SLT_TRC(slot, "restored context checkpoint (pos_min = %d, pos_max = %d, n_tokens = %" PRId64 ", n_past = %d, size = %.3f MiB)\n", cur.pos_min, cur.pos_max, cur.n_tokens, n_past, (float) cur.size() / 1024 / 1024);
|
||||
}
|
||||
|
||||
if (do_reset) {
|
||||
@@ -4301,7 +4317,7 @@ private:
|
||||
{
|
||||
// erase any checkpoints with pos_max > pos_next, or that cover tokens not shared with the new prompt
|
||||
for (auto it = slot.prompt.checkpoints.begin(); it != slot.prompt.checkpoints.end();) {
|
||||
const auto & cur = *it;
|
||||
const auto & cur = **it;
|
||||
if (cur.pos_max > pos_next || cur.n_tokens > n_prefix) {
|
||||
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);
|
||||
@@ -4504,7 +4520,7 @@ private:
|
||||
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 + params_base.checkpoint_min_step) {
|
||||
if (pos == last_user_pos || checkpoints.empty() || pos > checkpoints.back()->n_tokens + params_base.checkpoint_min_step) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -4578,7 +4594,7 @@ private:
|
||||
do_checkpoint = do_checkpoint && (
|
||||
slot.prompt.checkpoints.empty() ||
|
||||
is_last_user_message || near_prompt_end ||
|
||||
n_tokens_start > slot.prompt.checkpoints.back().n_tokens + params_base.checkpoint_min_step);
|
||||
n_tokens_start > slot.prompt.checkpoints.back()->n_tokens + params_base.checkpoint_min_step);
|
||||
SLT_DBG(slot, "main/do_checkpoint = %s, pos_min = %d, pos_max = %d\n", do_checkpoint ? "yes" : "no", pos_min, pos_max);
|
||||
|
||||
// note: we create the checkpoint before calling llama_decode(), so the current batch is not
|
||||
|
||||
Reference in new issue
Block a user