Skip to content

Commit 2048b59

Browse files
ishaangandhingxson
andauthored
server : fix crash when using verbose output with input tokens that are not in printable range (#12178) (#12338)
* Fix DOS index bug * Remove new APIs * remove extra line * Remove from API * Add extra newline * Update examples/server/server.cpp --------- Co-authored-by: Xuan-Son Nguyen <[email protected]>
1 parent f08f4b3 commit 2048b59

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

examples/server/server.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,18 @@ struct server_context {
20402040
return ret;
20412041
}
20422042

2043+
bool can_be_detokenized(const struct llama_context * ctx, const std::vector<llama_token> & tokens) {
2044+
const llama_model * model = llama_get_model(ctx);
2045+
const llama_vocab * vocab = llama_model_get_vocab(model);
2046+
const int32_t n_vocab = llama_vocab_n_tokens(vocab);
2047+
for (const auto & token : tokens) {
2048+
if (token < 0 || token >= n_vocab) {
2049+
return false;
2050+
}
2051+
}
2052+
return true;
2053+
}
2054+
20432055
bool launch_slot_with_task(server_slot & slot, const server_task & task) {
20442056
slot.reset();
20452057
slot.id_task = task.id;
@@ -2054,6 +2066,11 @@ struct server_context {
20542066
slot.lora = task.params.lora;
20552067
}
20562068

2069+
bool can_detokenize = can_be_detokenized(ctx, slot.prompt_tokens);
2070+
if (!can_detokenize) {
2071+
send_error(task, "Prompt contains invalid tokens", ERROR_TYPE_INVALID_REQUEST);
2072+
return false;
2073+
}
20572074
SLT_DBG(slot, "launching slot : %s\n", safe_json_to_str(slot.to_json()).c_str());
20582075

20592076
if (slot.n_predict > 0 && slot.params.n_predict > slot.n_predict) {

0 commit comments

Comments
 (0)