From 25d6e1e9b6b70b648b333933aaf3d197ed46024b Mon Sep 17 00:00:00 2001 From: Lumpiasty Date: Mon, 23 Nov 2026 10:41:22 +0100 Subject: [PATCH] server: merge user-message batch breaks when the rest of the prompt fits in one ubatch, keep the first-user-message break (F14) Knob LLAMA_USERSTART_MERGE=0 (upstream behaviour). Squashed from 2 fork commits (original subjects and messages below). ---- server: merge user-message batch breaks when the rest of the prompt fits in one ubatch (was 7e738d747) Upstream (#22929, #24176, #25420) ends a prompt batch at the start of the last user message (and of earlier ones on a fresh prompt) so that a context checkpoint lands there. On a hybrid/recurrent model with MoE experts on the host, that turns a typical chat turn (previous answer + new user message, under one ubatch) into two partial ubatches, and each one of >= 32 tokens uploads the host experts again. When the rest of the prompt from the current batch start fits in one ubatch, skip the user-start break. The near-end rule still checkpoints the batch start and the end-4 rule the prompt end, so a later prompt that diverges inside the last user message re-processes less than one ubatch more, in the same ubatch. Prompts longer than one ubatch, and batches after a media chunk, keep the upstream behaviour. LLAMA_USERSTART_MERGE=0 restores it everywhere. ---- server: F14 - keep the batch break at the first user message (system-prompt checkpoint) (was e7e907b7c) With LLAMA_USERSTART_MERGE on (the default), F14 skipped every user-message batch break when the rest of the prompt fits in one ubatch. On a fresh prompt that also dropped the break at the first user message, so no context checkpoint was left at the end of the system prompt, and a new chat with the same system prompt re-processed the system prompt from scratch (a recurrent/hybrid state cannot be cut back without a checkpoint). Keep that one break in the merged path: the batch ends right before the first user message, and the next batch starts there and gets the checkpoint (is_user_start, near-end rule). The batch only reaches the first user message when the prompt is processed from before it: checkpoints past the restore point are erased on reuse, so this is a fresh prompt or one that diverged inside the system prompt. Follow-up turns start past it and keep the merge (no break at the last user message). Boundaries below 8 tokens (only a BOS before the first user message) are skipped. Cost: on a fresh prompt the system prompt runs as its own batch again (the upstream layout for turn 1), measured before as part of turn 1's 6.08 vs 3.99 s TTFT (FORK.md F14). The unmerged path is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PZz44SLQvTXMyWGio6t9DZ --- tools/server/server-context.cpp | 36 ++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index 6d82e0b3c..6918f10a8 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -4935,6 +4935,36 @@ private: const int32_t n_decision_first = slot.task->type == SERVER_TASK_TYPE_DECISION ? slot.task->decision.pos_first() : -1; + // [fork F14] do not break at user-message starts when the rest of the prompt + // from here fits in one ubatch: the break turns one ubatch into two, and with + // MoE experts on the host each one of >= 32 tokens uploads the experts again. + // A checkpoint at this batch start (the near-end rule below) and at the end + // minus 4 still exist, so a later prompt that diverges inside the merged range + // re-processes less than one ubatch more. LLAMA_USERSTART_MERGE=0 disables. + static const bool userstart_merge = [] { + const char * env = getenv("LLAMA_USERSTART_MERGE"); + return env == nullptr || std::string(env) != "0"; + }(); + const bool merge_user_starts = userstart_merge && !has_mtmd && + slot.task->n_tokens() - slot.prompt.n_tokens() <= n_ubatch; + + // [fork F14] merged or not, keep the break at the first user message, the end of + // the system prompt: the checkpoint there is what a new chat with the same system + // prompt restores. The batch only reaches it when the prompt is processed from + // before it (a fresh prompt; checkpoints past the restore point are erased), so + // follow-up turns are not affected. Below 8 tokens there is no system prompt + // worth a checkpoint (only a BOS before the first user message). + int32_t first_user_pos = -1; + for (const auto & s : spans.spans) { + if (s.role == COMMON_CHAT_ROLE_USER) { + first_user_pos = (int32_t) s.pos; + break; + } + } + if (first_user_pos < 8) { + first_user_pos = -1; + } + // add prompt tokens for processing in the current batch while (slot.prompt.n_tokens() < slot.task->n_tokens() && batch.size() < n_batch) { // get next token to process @@ -4982,7 +5012,11 @@ 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 (merge_user_starts) { + if (pos == first_user_pos) { + break; + } + } else if (pos == last_user_pos || checkpoints.empty() || pos > checkpoints.back()->n_tokens + params_base.checkpoint_min_step) { break; } }