Skip to content

llama_kv_cache_tokens_rm functioning on index and not position #3840

Closed
@MrJackSpade

Description

@MrJackSpade

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • [Y] I am running the latest code. Development is very rapid so there are no tagged versions as of now.
  • [Y] I carefully followed the README.md.
  • [Y] I searched using keywords relevant to my issue to make sure that I am creating a new issue that is not already open (or closed).
  • [Y] I reviewed the Discussions, and have a new bug or useful enhancement to share.

This bug is related to the issue in #3825 described here

#3825 (comment)

llama_eval calls llama_kv_cache_tokens_rm using n_past in an attempt to truncate all cache data beyond the point being currently evaluated.

llama_kv_cache_tokens_rm executes this cache clear by iterating over all cells of the kv cache, however instead of honoring the position property of the cell, it clears the cell out by using the index of that cell within the cache

static void llama_kv_cache_tokens_rm(struct llama_kv_cache & cache, int32_t c0, int32_t c1) {
    if (c0 < 0) c0 = 0;
    if (c1 < 0) c1 = cache.size;

    for (int32_t i = c0; i < c1; ++i) {
        cache.cells[i].pos = -1;
        cache.cells[i].delta = 0;
        cache.cells[i].seq_id.clear();
    }

    // Searching for a free slot can start here since we know it will be empty.
    cache.head = uint32_t(c0);
}

This functionality is not compatible with the new "ring cache" design, because given the following cache values

idx:  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
pos: -1 -1 -1 -1 -1 -1 -1  0  1  2  3  4  5  6  7  8

Calling Eval with an n_past of 8 in this scenario, will completely clear all data from the kv cache, when it should be expected to retain all data.

I'm not sure if the proper method to resolve this would be to modify llama_eval to point to the new llama_kv_cache_seq_rm method with a seq_id of zero, or if another adjustment should me made to resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions