← Notes

llama.cpp seq_rm Returned false

2 min read#C++#llama.cpp#Autarkia

After clearing short-term memory, the next message made llama_decode fail with sequence positions inconsistent. The clear never reached C++, so the old KV cache survived, and the prefix trim ignored the return value of llama_memory_seq_rm, which returned false because the M-RoPE model can’t trim part of a sequence.

On August 24, with a local model, I ran clear short-term memory and sent another message; llama_decode failed with sequence positions inconsistent.

With a local model, Autarkia’s C++ scheduler keeps a KV cache per session and only computes what changed since the last turn. The clear stopped at the Python layer: history deleted, KV swap files on disk deleted, Cleared history logged, while the sequence in the C++ worker’s VRAM stayed as it was. The new prompt shared only a short prefix with the old cache, so the cache had to be trimmed to that length:

cpp
llama_memory_seq_rm(llama_get_memory(ctx_), seq_id, (llama_pos)common, -1);
cached_tokens->resize(common);

llama_memory_seq_rm() returns a bool. The header says removing a whole sequence always succeeds, while removing part of one returns false when unsupported. It appears five or six times in that file, mostly removing whole sequences (0, -1), where ignoring the result is fine; this call removes the tail and looks almost identical, so its result went unchecked too. The model was M-RoPE, which can’t trim part of a sequence, so it returned false, and the code carried on as if trimmed, decoding from common.

cpp
const bool partial_removed = llama_memory_seq_rm(
    llama_get_memory(ctx_), seq_id, (llama_pos)common, -1);
if (partial_removed) {
    cached_tokens->resize(common);
} else {
    // 不支持局部裁剪:整条清空,从 0 重新 prefill
    llama_memory_seq_rm(llama_get_memory(ctx_), seq_id, 0, -1);
    cached_tokens->clear();
    common = 0;
}

I also added a per-session KV cache clear, wired from C++ through to Python and called when short-term memory is cleared. The bug needs all three at once: a local model, the C++ scheduler, and continuing the same session after a clear; cloud models and the Python inference path have no such cache.