Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/llama.h
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,9 @@ extern "C" {
// Check if the memory supports shifting
LLAMA_API bool llama_memory_can_shift(llama_memory_t mem);

// Check if the memory supports removing the last tokens in the sequence
LLAMA_API bool llama_memory_can_rm_suffix(llama_memory_t mem);

//
// State / sessions
//
Expand Down
8 changes: 8 additions & 0 deletions src/llama-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3360,6 +3360,14 @@ bool llama_memory_can_shift(llama_memory_t mem) {
return mem->get_can_shift();
}

bool llama_memory_can_rm_suffix(llama_memory_t mem) {
if (!mem) {
return false;
}

return mem->get_can_rm_suffix();
}

// llama state API

// deprecated
Expand Down
4 changes: 4 additions & 0 deletions src/llama-kv-cache-iswa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ bool llama_kv_cache_iswa::get_can_shift() const {
return kv_base->get_size() == kv_swa->get_size();
}

bool llama_kv_cache_iswa::get_can_rm_suffix() const {
return kv_base->get_can_rm_suffix() && kv_swa->get_can_rm_suffix();
}

void llama_kv_cache_iswa::state_write(llama_io_write_i & io, llama_seq_id seq_id, llama_state_seq_flags flags) const {
if ((flags & LLAMA_STATE_SEQ_FLAGS_PARTIAL_ONLY) == 0) {
kv_base->state_write(io, seq_id, flags);
Expand Down
3 changes: 2 additions & 1 deletion src/llama-kv-cache-iswa.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class llama_kv_cache_iswa : public llama_memory_i {

llama_memory_context_ptr init_update(llama_context * lctx, bool optimize) override;

bool get_can_shift() const override;
bool get_can_shift() const override;
bool get_can_rm_suffix() const override;

void clear(bool data) override;

Expand Down
4 changes: 4 additions & 0 deletions src/llama-kv-cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,10 @@ bool llama_kv_cache::get_can_shift() const {
return true;
}

bool llama_kv_cache::get_can_rm_suffix() const {
return true;
}

uint32_t llama_kv_cache::get_size() const {
const auto & cells = v_cells[seq_to_stream[0]];

Expand Down
3 changes: 2 additions & 1 deletion src/llama-kv-cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ class llama_kv_cache : public llama_memory_i {

llama_memory_context_ptr init_update(llama_context * lctx, bool optimize) override;

bool get_can_shift() const override;
bool get_can_shift() const override;
bool get_can_rm_suffix() const override;

void clear(bool data) override;

Expand Down
7 changes: 5 additions & 2 deletions src/llama-memory-hybrid-iswa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ llama_memory_context_ptr llama_memory_hybrid_iswa::init_update(llama_context * l
}

bool llama_memory_hybrid_iswa::get_can_shift() const {
// Shifting is trivially supported for recurrent
return mem_attn->get_can_shift();
return mem_attn->get_can_shift() && mem_recr->get_can_shift();
}

bool llama_memory_hybrid_iswa::get_can_rm_suffix() const {
return mem_attn->get_can_rm_suffix() && mem_recr->get_can_rm_suffix();
}

void llama_memory_hybrid_iswa::clear(bool data) {
Expand Down
3 changes: 2 additions & 1 deletion src/llama-memory-hybrid-iswa.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class llama_memory_hybrid_iswa : public llama_memory_i {

llama_memory_context_ptr init_update(llama_context * lctx, bool optimize) override;

bool get_can_shift() const override;
bool get_can_shift() const override;
bool get_can_rm_suffix() const override;

void clear(bool data) override;

Expand Down
7 changes: 5 additions & 2 deletions src/llama-memory-hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ llama_memory_context_ptr llama_memory_hybrid::init_update(llama_context * lctx,
}

bool llama_memory_hybrid::get_can_shift() const {
// Shifting is trivially supported for recurrent
return mem_attn->get_can_shift();
return mem_attn->get_can_shift() && mem_recr->get_can_shift();
}

bool llama_memory_hybrid::get_can_rm_suffix() const {
return mem_attn->get_can_rm_suffix() && mem_recr->get_can_rm_suffix();
}

void llama_memory_hybrid::clear(bool data) {
Expand Down
3 changes: 2 additions & 1 deletion src/llama-memory-hybrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class llama_memory_hybrid : public llama_memory_i {

llama_memory_context_ptr init_update(llama_context * lctx, bool optimize) override;

bool get_can_shift() const override;
bool get_can_shift() const override;
bool get_can_rm_suffix() const override;

void clear(bool data) override;

Expand Down
4 changes: 4 additions & 0 deletions src/llama-memory-recurrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,10 @@ bool llama_memory_recurrent::get_can_shift() const {
return true;
}

bool llama_memory_recurrent::get_can_rm_suffix() const {
return false;
}

size_t llama_memory_recurrent::total_size() const {
size_t size = 0;
for (const auto & [_, buf] : ctxs_bufs) {
Expand Down
4 changes: 3 additions & 1 deletion src/llama-memory-recurrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ class llama_memory_recurrent : public llama_memory_i {
// find a contiguous slot of memory cells and emplace the ubatch there
bool find_slot(const llama_ubatch & ubatch);

bool get_can_shift() const override;
bool get_can_shift() const override;
bool get_can_rm_suffix() const override;


// state write/load

Expand Down
3 changes: 2 additions & 1 deletion src/llama-memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ struct llama_memory_i {
virtual llama_memory_context_ptr init_update(llama_context * lctx, bool optimize) = 0;

// getters
virtual bool get_can_shift() const = 0;
virtual bool get_can_shift() const = 0;
virtual bool get_can_rm_suffix() const = 0;

//
// ops
Expand Down
4 changes: 3 additions & 1 deletion tools/server/server-context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ struct server_context_impl {
slot.prompt.tokens.has_mtmd = mctx != nullptr;

// try speculative decoding
{
if (llama_memory_can_rm_suffix(llama_get_memory(ctx))) {
slot.spec = common_speculative_init(params_base.speculative, slot.ctx);
if (slot.spec) {
if (mctx) {
Expand All @@ -763,6 +763,8 @@ struct server_context_impl {
} else {
SLT_INF(slot, "%s", "speculative decoding context not initialized\n");
}
} else {
SLT_WRN(slot, "%s", "speculative decoding not supported by this context (no memory_rm_suffix support)\n");
}

SLT_INF(slot, "new slot, n_ctx = %d\n", slot.n_ctx);
Expand Down
Loading