Skip to content

Commit 14e58c4

Browse files
author
FixBot
committed
Fix slot selection logic in get_available_slot
The bug was likely introduced in PR ikawrakow#973 when the similarity calculation was changed from LCP to token-level similarity, but sim_best was still initialized to 0 instead of -1.0f. When slot_prompt_similarity threshold was set high (e.g., 0.8) and no slot met the threshold, sim_best stayed at 0, causing ret to remain nullptr. This led to the system getting stuck without selecting any slot. This fix: - Changed sim_best initialization from 0 to -1.0f - Added best_slot variable to track the best slot found during similarity search - Only set ret = best_slot after the loop completes - Removed redundant ret == nullptr check This ensures that even when no slot meets the slot_prompt_similarity threshold, the system still identifies the best available slot and falls back to LRU correctly. Related: PR ikawrakow#973 (Server: Handle context shift better), PR ikawrakow#1285 (Fix slot prompt updating)
1 parent 62a7dca commit 14e58c4

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

examples/server/server-context.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,10 @@ server_slot* server_context::get_available_slot(const server_task& task) {
690690
bool update_cache = false;
691691

692692
// find the slot that has at least n% prompt similarity
693-
if (ret == nullptr && slot_prompt_similarity != 0.0f) {
693+
if (slot_prompt_similarity != 0.0f) {
694694
int max_lcp_len = 0;
695-
float sim_best = 0;
695+
float sim_best = -1.0f;
696+
server_slot* best_slot = nullptr;
696697

697698
for (server_slot& slot : slots) {
698699
// skip the slot if it is not available
@@ -720,13 +721,14 @@ server_slot* server_context::get_available_slot(const server_task& task) {
720721
float sim_cur = sim.second;
721722

722723
// select the current slot if the criteria match
723-
if (sim_cur > sim_best && sim_cur > slot_prompt_similarity) {
724+
if (sim_cur > slot_prompt_similarity && sim_cur > sim_best) {
724725
sim_best = sim_cur;
725726
max_lcp_len = lcp_len.first;
726-
ret = &slot;
727+
best_slot = &slot;
727728
}
728729
}
729-
if (ret != nullptr) {
730+
if (best_slot != nullptr) {
731+
ret = best_slot;
730732
LOG_VERBOSE("selected slot by lcp similarity", {
731733
{"id_slot", ret->id},
732734
{"max_lcp_len", max_lcp_len},
@@ -749,7 +751,6 @@ server_slot* server_context::get_available_slot(const server_task& task) {
749751
ret = &slot;
750752
}
751753
}
752-
753754
if (ret != nullptr) {
754755
LOG_VERBOSE("selected slot by lru", {
755756
{"id_slot", ret->id},
@@ -766,7 +767,6 @@ server_slot* server_context::get_available_slot(const server_task& task) {
766767
if (exclude_think) {
767768
auto temp = tokens.get_text_tokens_exclude_think(ret->ctx, ret->params.think_tokens);
768769
server_tokens cache_exclude_think = server_tokens(temp, false);
769-
770770
temp = task.tokens.get_text_tokens_exclude_think(ret->ctx, ret->params.think_tokens);
771771
server_tokens prompt_exclude_think = server_tokens(temp, false);
772772

@@ -799,15 +799,13 @@ server_slot* server_context::get_available_slot(const server_task& task) {
799799
LLAMA_LOG_INFO("updating prompt cache\n");
800800
// copy cache tokens
801801
copy_data_to_cached_prompt(tokens, *ret);
802-
803802
ret->prompt_save(*prompt_cache);
804803
LLAMA_LOG_INFO("prompt cache save took %.2f ms\n", (ggml_time_us() - t_start) / 1000.0);
805804
}
806805
// has prompts saved earlier to load
807806
if (prompt_cache && !prompt_cache->states.empty()) {
808807
const int64_t t_start = ggml_time_us();
809808
copy_data_to_cached_prompt(tokens, *ret);
810-
811809
ret->prompt_load(*prompt_cache, task.tokens);
812810
prompt_cache->update();
813811

@@ -821,6 +819,7 @@ server_slot* server_context::get_available_slot(const server_task& task) {
821819
return ret;
822820
}
823821

822+
824823
bool server_context::launch_slot_with_task(server_slot& slot, server_task& task) {
825824
slot_params defaults;
826825
defaults.speculative = params_base.speculative;

0 commit comments

Comments
 (0)