Skip to content

Commit

Permalink
server : reuse context chunks
Browse files Browse the repository at this point in the history
ggml-ci
  • Loading branch information
ggerganov committed Oct 12, 2024
1 parent edc2656 commit a6b048e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
54 changes: 52 additions & 2 deletions examples/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ struct server_context {
int slot_prompt_len = slot_prompt.size();

// length of the Longest Common Prefix between the current slot's prompt and the input prompt
int lcp_len = common_part(slot_prompt, prompt);
int lcp_len = longest_common_prefix(slot_prompt, prompt);

// fraction of the common substring length compared to the current slot's prompt length
similarity = static_cast<float>(lcp_len) / slot_prompt_len;
Expand Down Expand Up @@ -2042,12 +2042,61 @@ struct server_context {

if (slot.params.cache_prompt) {
// reuse any previously computed tokens that are common with the new prompt
slot.n_past = common_part(slot.cache_tokens, prompt_tokens);
slot.n_past = longest_common_prefix(slot.cache_tokens, prompt_tokens);

// push the prompt into the sampling context (do not apply grammar)
for (int i = 0; i < slot.n_past; ++i) {
common_sampler_accept(slot.smpl, slot.cache_tokens[i], false);
}

// EXPERIMENTAL: reuse chunks from the cached prompt by shifting them in the new position
if (1) {
size_t head_c = slot.n_past; // cache
size_t head_p = slot.n_past; // current prompt

while (head_c < slot.cache_tokens.size() &&
head_p < prompt_tokens.size() &&
!llama_token_is_control(model, slot.cache_tokens[head_c]) &&
!llama_token_is_control(model, prompt_tokens[head_p])) {

size_t n_match = 0;
while (head_c + n_match < slot.cache_tokens.size() &&
head_p + n_match < prompt_tokens.size() &&
!llama_token_is_control(model, slot.cache_tokens[head_c + n_match]) &&
!llama_token_is_control(model, prompt_tokens[head_p + n_match]) &&
slot.cache_tokens[head_c + n_match] == prompt_tokens[head_p + n_match]) {
n_match++;
}

if (n_match > 32) {
// shift the KV chunk [head_c, head_c + n_match) -> [head_p, head_p + n_match)
SLT_DBG(slot, "shifting KV cache [%zu, %zu) -> [%zu, %zu)\n", head_c, head_c + n_match, head_p, head_p + n_match);
//for (size_t i = head_p; i < head_p + n_match; i++) {
// SLT_DBG(slot, "cache token %3zu: %6d '%s'\n", i, prompt_tokens[i], common_token_to_piece(ctx, prompt_tokens[i]).c_str());
//}

const int64_t kv_shift = (int64_t) head_p - (int64_t) head_c;

llama_kv_cache_seq_rm (ctx, slot.id + 1, head_p, head_c);
llama_kv_cache_seq_add(ctx, slot.id + 1, head_c, -1, kv_shift);

for (size_t i = 0; i < n_match; i++) {
slot.cache_tokens[head_p + i] = slot.cache_tokens[head_c + i];

common_sampler_accept(slot.smpl, slot.cache_tokens[head_p + i], false);

slot.n_past++;
}

head_c += n_match;
head_p += n_match;
} else {
head_c += 1;
}
}

SLT_DBG(slot, "new slot.n_past = %d, cache_tokens.size() = %zu\n", slot.n_past, slot.cache_tokens.size());
}
}
}

Expand Down Expand Up @@ -3257,6 +3306,7 @@ int main(int argc, char ** argv) {

ctx_server.queue_tasks.on_new_task(std::bind(
&server_context::process_single_task, &ctx_server, std::placeholders::_1));

ctx_server.queue_tasks.on_update_slots(std::bind(
&server_context::update_slots, &ctx_server));

Expand Down
4 changes: 2 additions & 2 deletions examples/server/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ static std::string gen_chatcmplid() {
// other common utils
//

static size_t common_part(const std::vector<llama_token> & a, const std::vector<llama_token> & b) {
static size_t longest_common_prefix(const std::vector<llama_token> & a, const std::vector<llama_token> & b) {
size_t i;
for (i = 0; i < a.size() && i < b.size() && a[i] == b[i]; i++) {}

return i;
}

static size_t common_part(const std::string & a, const std::string & b) {
static size_t longest_common_prefix(const std::string & a, const std::string & b) {
size_t i;
for (i = 0; i < a.size() && i < b.size() && a[i] == b[i]; i++) {}

Expand Down

0 comments on commit a6b048e

Please sign in to comment.