kv-cache: add LLAMA_STATE_SEQ_FLAGS_APPEND for incremental state restore - #21221
kv-cache: add LLAMA_STATE_SEQ_FLAGS_APPEND for incremental state restore#21221sammshen wants to merge 1 commit into
Conversation
Add a new flag LLAMA_STATE_SEQ_FLAGS_APPEND (value 2) that skips the seq_rm() call in state_read_meta, allowing incremental chunk-by-chunk restore to the same sequence via repeated state_seq_set_data_ext calls. This enables external KV cache systems to restore opaque state blobs one chunk at a time without each chunk clearing the previous one. - Add #define LLAMA_STATE_SEQ_FLAGS_APPEND 2 in llama.h - Thread flags parameter through state_read() to state_read_meta() - Gate seq_rm() on !(flags & LLAMA_STATE_SEQ_FLAGS_APPEND) - Default behavior (flags=0) is unchanged
|
But how do you store the chunks in the first place? We currently only store full sequences. |
|
Hi @sammshen, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
| void state_write_data(llama_io_write_i & io, const cell_ranges_t & cr) const; | ||
|
|
||
| bool state_read_meta(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, slot_info & sinfo, llama_seq_id dest_seq_id = -1); | ||
| bool state_read_meta(llama_io_read_i & io, uint32_t strm, uint32_t cell_count, slot_info & sinfo, llama_seq_id dest_seq_id = -1, llama_state_seq_flags flags = 0); |
|
@ggerganov thanks for the quick response! we can use the existing API via seq_cp. for instance: to serialize only positions [256, 512) from seq 0, we can do:
so store is seq_cp + state_seq_get_data + seq_rm and restore is state_seq_set_data_ext w/ APPEND flag (from this PR sorry I chose 256 here because I am planning on using this to integrate with LMCache's "chunk" size |
|
Ah, yes. Interesting. |
|
@ggerganov if the change doesn't make sense, I would happy to make any needed changes (or please lmk if you think it doesn't make sense in general, I can go back to the drawing board). I can link the LMCache integration once it's ready if that helps? (even though this PR has nothing to do with LMCache) |
Overview
state_read_metaunconditionally callsseq_rm(dest_seq_id, -1, -1)before restoring so everystate_seq_set_data_extwipes the sequence first, making chunk by chunk restore impossible (e.g. restoring chunk 1 destroys chunk 0)This PR adds a new flag
LLAMA_STATE_SEQ_FLAGS_APPEND(value2) that skips theseq_rmcall, allowing repeatedstate_seq_set_data_extcalls to append to the same sequence, using the existingllama_state_seq_flagsmechanism (precedentLLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLYis from #16382).Exact changes:
include/llama.h: add#define LLAMA_STATE_SEQ_FLAGS_APPEND 2src/llama-kv-cache.h: addflagsparam tostate_read_metasrc/llama-kv-cache.cpp: threadflagsthroughstate_read→state_read_meta, gateseq_rmon!(flags & APPEND)default behavior (
flags=0) is unchanged.motivation: this enables external KV cache systems (e.g. LMCache) to store and restore opaque state blobs one chunk at a time, without reassembling into a single monolithic blob or parsing the internal serialization format.
Requirements