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; } }