return iparams;
}
- if (params.ctx_shift && !llama_kv_self_can_shift(lctx)) {
+ if (params.ctx_shift && !llama_memory_can_shift(llama_get_memory(lctx))) {
LOG_WRN("%s: KV cache shifting is not supported for this context, disabling KV cache shifting\n", __func__);
params.ctx_shift = false;
}
if (llama_model_has_decoder(model)) {
llama_decode(lctx, llama_batch_get_one(tmp.data(), std::min(tmp.size(), (size_t) params.n_batch)));
}
- llama_kv_self_clear(lctx);
+ llama_memory_clear(llama_get_memory(lctx), true);
llama_synchronize(lctx);
llama_perf_context_reset(lctx);
llama_set_warmup(lctx, false);
auto & smpl = spec->smpl;
auto & prompt = spec->prompt;
+ auto * mem = llama_get_memory(ctx);
+
int reuse_i = 0;
int reuse_n = 0;
result.reserve(params.n_draft);
if (reuse_n == 0) {
- llama_kv_self_clear(ctx);
+ llama_memory_clear(mem, false);
prompt.clear();
} else {
}
if (reuse_i > 0) {
- llama_kv_self_seq_rm (ctx, 0, 0, reuse_i);
- llama_kv_self_seq_add(ctx, 0, reuse_i, -1, -reuse_i);
+ llama_memory_seq_rm (mem, 0, 0, reuse_i);
+ llama_memory_seq_add(mem, 0, reuse_i, -1, -reuse_i);
prompt.erase(prompt.begin(), prompt.begin() + reuse_i);
}
if (reuse_n < (int) prompt.size()) {
- llama_kv_self_seq_rm (ctx, 0, reuse_n, -1);
+ llama_memory_seq_rm (mem, 0, reuse_n, -1);
prompt.erase(prompt.begin() + reuse_n, prompt.end());
}
}
for i in 1 ..< n_parallel {
- llama_kv_self_seq_cp(context, 0, Int32(i), 0, batch.n_tokens)
+ llama_memory_seq_cp(llama_get_memory(context), 0, Int32(i), 0, batch.n_tokens)
}
if n_parallel > 1 {
const enum llama_pooling_type pooling_type = llama_pooling_type(ctx);
// clear previous kv_cache values (irrelevant for embeddings)
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
// run model
LOG_INF("%s: n_tokens = %d, n_seq = %d\n", __func__, batch.n_tokens, n_seq);
}
// clear previous kv_cache values (irrelevant for embeddings)
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
llama_set_embeddings(ctx, true);
llama_set_causal_attn(ctx, false);
llama_token eos_token = llama_vocab_eos(vocab);
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
llama_set_embeddings(ctx, false);
llama_set_causal_attn(ctx, true);
}
batch->logits[batch->n_tokens - 1] = true;
- llama_kv_self_clear(context);
+ llama_memory_clear(llama_get_memory(context), false);
const auto t_pp_start = ggml_time_us();
if (llama_decode(context, *batch) != 0) {
LOGi("Benchmark text generation (tg)");
- llama_kv_self_clear(context);
+ llama_memory_clear(llama_get_memory(context), false);
const auto t_tg_start = ggml_time_us();
for (i = 0; i < tg; i++) {
const auto t_tg_end = ggml_time_us();
- llama_kv_self_clear(context);
+ llama_memory_clear(llama_get_memory(context), false);
const auto t_pp = double(t_pp_end - t_pp_start) / 1000000.0;
const auto t_tg = double(t_tg_end - t_tg_start) / 1000000.0;
extern "C"
JNIEXPORT void JNICALL
Java_android_llama_cpp_LLamaAndroid_kv_1cache_1clear(JNIEnv *, jobject, jlong context) {
- llama_kv_self_clear(reinterpret_cast<llama_context *>(context));
+ llama_memory_clear(llama_get_memory(reinterpret_cast<llama_context *>(context)), true);
}
}
batch.logits[Int(batch.n_tokens) - 1] = 1 // true
- llama_kv_self_clear(context)
+ llama_memory_clear(llama_get_memory(context), false)
let t_pp_start = DispatchTime.now().uptimeNanoseconds / 1000;
// bench text generation
- llama_kv_self_clear(context)
+ llama_memory_clear(llama_get_memory(context), false)
let t_tg_start = DispatchTime.now().uptimeNanoseconds / 1000;
let t_tg_end = DispatchTime.now().uptimeNanoseconds / 1000;
- llama_kv_self_clear(context)
+ llama_memory_clear(llama_get_memory(context), false)
let t_pp = Double(t_pp_end - t_pp_start) / 1000000.0
let t_tg = Double(t_tg_end - t_tg_start) / 1000000.0
func clear() {
tokens_list.removeAll()
temporary_invalid_cchars.removeAll()
- llama_kv_self_clear(context)
+ llama_memory_clear(llama_get_memory(context), true)
}
private func tokenize(text: String, add_bos: Bool) -> [llama_token] {
llama_model * model = llama_init.model.get();
llama_context * ctx = llama_init.context.get();
+ auto * mem = llama_get_memory(ctx);
+
const llama_vocab * vocab = llama_model_get_vocab(model);
// Tokenize the prompt
llama_decode(ctx, llama_batch_get_one(&inp.back(), 1));
for (int s = 1; s < W + G + 1; ++s) {
- llama_kv_self_seq_cp(ctx, 0, s, -1, -1);
+ llama_memory_seq_cp(mem, 0, s, -1, -1);
}
const auto t_enc_end = ggml_time_us();
// KV cache management
// if no verification token matched, we simply remove all cells from this batch -> no fragmentation
- llama_kv_self_seq_rm(ctx, -1, n_past, -1);
+ llama_memory_seq_rm(mem, -1, n_past, -1);
if (seq_id_best != 0) {
// if a verification token matched, we keep the best sequence and remove the rest
// this leads to some KV cache fragmentation
- llama_kv_self_seq_keep(ctx, seq_id_best);
- llama_kv_self_seq_cp (ctx, seq_id_best, 0, -1, -1);
- llama_kv_self_seq_rm (ctx, seq_id_best, -1, -1);
+ llama_memory_seq_keep(mem, seq_id_best);
+ llama_memory_seq_cp (mem, seq_id_best, 0, -1, -1);
+ llama_memory_seq_rm (mem, seq_id_best, -1, -1);
for (int s = 1; s < W + G + 1; ++s) {
- llama_kv_self_seq_cp(ctx, 0, s, -1, -1);
+ llama_memory_seq_cp(mem, 0, s, -1, -1);
}
}
}
// KV cache management
// clean the cache of draft tokens that weren't accepted
- llama_kv_self_seq_rm(ctx, 0, n_past, -1);
+ llama_memory_seq_rm(llama_get_memory(ctx), 0, n_past, -1);
common_batch_clear(batch_tgt);
common_batch_add(batch_tgt, draft[0], n_past, { 0 }, true);
llama_model * model = llama_init.model.get();
llama_context * ctx = llama_init.context.get();
+ auto * mem = llama_get_memory(ctx);
+
const llama_vocab * vocab = llama_model_get_vocab(model);
// load the prompts from an external file if there are any
// assign the system KV cache to all parallel sequences
for (int32_t i = 1; i <= n_clients; ++i) {
- llama_kv_self_seq_cp(ctx, 0, i, -1, -1);
+ llama_memory_seq_cp(mem, 0, i, -1, -1);
}
LOG_INF("\n");
if (batch.n_tokens == 0) {
// all sequences have ended - clear the entire KV cache
for (int i = 1; i <= n_clients; ++i) {
- llama_kv_self_seq_rm(ctx, i, -1, -1);
+ llama_memory_seq_rm(mem, i, -1, -1);
// but keep the system prompt
- llama_kv_self_seq_cp(ctx, 0, i, -1, -1);
+ llama_memory_seq_cp(mem, 0, i, -1, -1);
}
LOG_INF("%s: clearing the KV cache\n", __func__);
}
// delete only the generated part of the sequence, i.e. keep the system prompt in the cache
- llama_kv_self_seq_rm(ctx, client.id + 1, -1, -1);
- llama_kv_self_seq_cp(ctx, 0, client.id + 1, -1, -1);
+ llama_memory_seq_rm(mem, client.id + 1, -1, -1);
+ llama_memory_seq_cp(mem, 0, client.id + 1, -1, -1);
const auto t_main_end = ggml_time_us();
int n_past = 0;
+ auto * mem = llama_get_memory(ctx);
+
// fill the KV cache
for (int i = 0; i < n_ctx; i += n_batch) {
if (i > 0 && n_grp > 1) {
const int ib = i/n_batch - 1;
const int bd = n_batch_grp*(n_grp - 1);
- llama_kv_self_seq_add(ctx, 0, n_past - n_batch, n_past, ib*bd);
- llama_kv_self_seq_div(ctx, 0, n_past - n_batch + ib*bd, n_past + ib*bd, n_grp);
+ llama_memory_seq_add(mem, 0, n_past - n_batch, n_past, ib*bd);
+ llama_memory_seq_div(mem, 0, n_past - n_batch + ib*bd, n_past + ib*bd, n_grp);
- n_past = llama_kv_self_seq_pos_max(ctx, 0) + 1;
+ n_past = llama_memory_seq_pos_max(mem, 0) + 1;
}
common_batch_clear(batch);
LOG_INF("%s: shifting KV cache with %d\n", __func__, n_discard);
- llama_kv_self_seq_rm (ctx, 0, n_keep , n_keep + n_discard);
- llama_kv_self_seq_add(ctx, 0, n_keep + n_discard, n_ctx, -n_discard);
+ llama_memory_seq_rm (mem, 0, n_keep , n_keep + n_discard);
+ llama_memory_seq_add(mem, 0, n_keep + n_discard, n_ctx, -n_discard);
- n_past = llama_kv_self_seq_pos_max(ctx, 0) + 1;
+ n_past = llama_memory_seq_pos_max(mem, 0) + 1;
common_batch_clear(batch);
if (n_discard > 0) {
LOG_INF("%s: shifting KV cache with %d to free space for the answer\n", __func__, n_discard);
- llama_kv_self_seq_rm (ctx, 0, n_keep , n_keep + n_discard);
- llama_kv_self_seq_add(ctx, 0, n_keep + n_discard, n_ctx, -n_discard);
+ llama_memory_seq_rm (mem, 0, n_keep , n_keep + n_discard);
+ llama_memory_seq_add(mem, 0, n_keep + n_discard, n_ctx, -n_discard);
- n_past = llama_kv_self_seq_pos_max(ctx, 0) + 1;
+ n_past = llama_memory_seq_pos_max(mem, 0) + 1;
}
}
static void batch_process(llama_context * ctx, llama_batch & batch, float * output, int n_seq, int n_embd) {
// clear previous kv_cache values (irrelevant for embeddings)
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), false);
// run model
LOG_INF("%s: n_tokens = %d, n_seq = %d\n", __func__, batch.n_tokens, n_seq);
fprintf(stderr, "%s : seq 0 copied, %zd bytes\n", __func__, ncopy);
// erase whole kv
- llama_kv_self_clear(ctx3);
+ llama_memory_clear(llama_get_memory(ctx3), true);
fprintf(stderr, "%s : kv cache cleared\n", __func__);
// restore kv into seq 1
auto generate = [&](const std::string & prompt) {
std::string response;
- const bool is_first = llama_kv_self_seq_pos_max(ctx, 0) == 0;
+ const bool is_first = llama_memory_seq_pos_max(llama_get_memory(ctx), 0) == 0;
// tokenize the prompt
const int n_prompt_tokens = -llama_tokenize(vocab, prompt.c_str(), prompt.size(), NULL, 0, is_first, true);
while (true) {
// check if we have enough space in the context to evaluate this batch
int n_ctx = llama_n_ctx(ctx);
- int n_ctx_used = llama_kv_self_seq_pos_max(ctx, 0);
+ int n_ctx_used = llama_memory_seq_pos_max(llama_get_memory(ctx), 0);
if (n_ctx_used + batch.n_tokens > n_ctx) {
printf("\033[0m\n");
fprintf(stderr, "context size exceeded\n");
{
LOG_DBG("clear kv cache from any extra tokens, n_past = %d\n", n_past);
- llama_kv_self_seq_rm(ctx_tgt, 0, n_past, -1);
+ llama_memory_seq_rm(llama_get_memory(ctx_tgt), 0, n_past, -1);
}
if ((params.n_predict >= 0 && n_predict > params.n_predict) || has_eos) {
}
}
+ auto * mem_tgt = llama_get_memory(ctx_tgt);
+ auto * mem_dft = llama_get_memory(ctx_dft);
// Tokenize the prompt
std::vector<llama_token> inp;
{
LOG_DBG("keeping sequence %d, n_past_tgt = %d, n_past_dft = %d\n", s_keep, n_past_tgt, n_past_dft);
- llama_kv_self_seq_keep(ctx_dft, s_keep);
- llama_kv_self_seq_cp (ctx_dft, s_keep, 0, -1, -1);
- llama_kv_self_seq_keep(ctx_dft, 0);
+ llama_memory_seq_keep(mem_dft, s_keep);
+ llama_memory_seq_cp (mem_dft, s_keep, 0, -1, -1);
+ llama_memory_seq_keep(mem_dft, 0);
- llama_kv_self_seq_rm (ctx_tgt, s_keep, n_past_tgt, -1);
- llama_kv_self_seq_keep(ctx_tgt, s_keep);
- llama_kv_self_seq_cp (ctx_tgt, s_keep, 0, -1, -1);
- llama_kv_self_seq_keep(ctx_tgt, 0);
+ llama_memory_seq_rm (mem_tgt, s_keep, n_past_tgt, -1);
+ llama_memory_seq_keep(mem_tgt, s_keep);
+ llama_memory_seq_cp (mem_tgt, s_keep, 0, -1, -1);
+ llama_memory_seq_keep(mem_tgt, 0);
}
for (int s = 0; s < n_seq_dft; ++s) {
common_batch_clear(batch_dft);
common_batch_add (batch_dft, token_id, n_past_dft, { 0 }, true);
- llama_kv_self_seq_rm(ctx_dft, 0, n_past_dft, -1);
+ llama_memory_seq_rm(mem_dft, 0, n_past_dft, -1);
// LOG_DBG("dft batch: %s\n", LOG_BATCH_TOSTR_PRETTY(ctx_dft, batch_dft).c_str());
llama_decode(ctx_dft, batch_dft);
if (n_seq_cur < n_seq_dft && cur_p->data[f].p > p_draft_split) {
LOG_DBG("splitting seq %3d into %3d\n", s, n_seq_cur);
- llama_kv_self_seq_rm(ctx_dft, n_seq_cur, -1, -1);
- llama_kv_self_seq_cp(ctx_dft, s, n_seq_cur, -1, -1);
+ llama_memory_seq_rm(mem_dft, n_seq_cur, -1, -1);
+ llama_memory_seq_cp(mem_dft, s, n_seq_cur, -1, -1);
// all previous tokens from this branch are now also part of the new branch
for (int t = 0; t < batch_tgt.n_tokens; ++t) {
// evaluate the target model on the drafted tokens
{
- llama_kv_self_seq_keep(ctx_tgt, 0);
+ llama_memory_seq_keep(mem_tgt, 0);
for (int s = 1; s < n_seq_dft; ++s) {
- llama_kv_self_seq_cp(ctx_tgt, 0, s, -1, -1);
+ llama_memory_seq_cp(mem_tgt, 0, s, -1, -1);
}
// LOG_DBG("target batch: %s\n", LOG_BATCH_TOSTR_PRETTY(ctx_tgt, batch_tgt).c_str());
//
// Clear the memory contents
- LLAMA_API void llama_memory_clear(llama_memory_t mem);
+ // If data == true, the data buffers will also be cleared together with the metadata
+ LLAMA_API void llama_memory_clear(
+ llama_memory_t mem,
+ bool data);
// Removes all tokens that belong to the specified sequence and have positions in [p0, p1)
// Returns false if a partial sequence cannot be removed. Removing a whole sequence never fails
"Use llama_kv_self_seq_pos_max() and llama_kv_self_seq_pos_min() instead (https://github.com/ggml-org/llama.cpp/issues/13793)");
// Clear the KV cache - both cell info is erased and KV data is zeroed
- LLAMA_API void llama_kv_self_clear(
- struct llama_context * ctx);
+ DEPRECATED(LLAMA_API void llama_kv_self_clear(
+ struct llama_context * ctx),
+ "Use llama_memory_clear() instead");
// Removes all tokens that belong to the specified sequence and have positions in [p0, p1)
// Returns false if a partial sequence cannot be removed. Removing a whole sequence never fails
// seq_id < 0 : match any sequence
// p0 < 0 : [0, p1]
// p1 < 0 : [p0, inf)
- LLAMA_API bool llama_kv_self_seq_rm(
+ DEPRECATED(LLAMA_API bool llama_kv_self_seq_rm(
struct llama_context * ctx,
llama_seq_id seq_id,
llama_pos p0,
- llama_pos p1);
+ llama_pos p1),
+ "Use llama_memory_seq_rm() instead");
// Copy all tokens that belong to the specified sequence to another sequence
// Note that this does not allocate extra KV cache memory - it simply assigns the tokens to the new sequence
// p0 < 0 : [0, p1]
// p1 < 0 : [p0, inf)
- LLAMA_API void llama_kv_self_seq_cp(
+ DEPRECATED(LLAMA_API void llama_kv_self_seq_cp(
struct llama_context * ctx,
llama_seq_id seq_id_src,
llama_seq_id seq_id_dst,
llama_pos p0,
- llama_pos p1);
+ llama_pos p1),
+ "Use llama_memory_seq_cp() instead");
// Removes all tokens that do not belong to the specified sequence
- LLAMA_API void llama_kv_self_seq_keep(
+ DEPRECATED(LLAMA_API void llama_kv_self_seq_keep(
struct llama_context * ctx,
- llama_seq_id seq_id);
+ llama_seq_id seq_id),
+ "Use llama_memory_seq_keep() instead");
// Adds relative position "delta" to all tokens that belong to the specified sequence and have positions in [p0, p1)
// If the KV cache is RoPEd, the KV data is updated accordingly:
// - lazily on next llama_decode()
// p0 < 0 : [0, p1]
// p1 < 0 : [p0, inf)
- LLAMA_API void llama_kv_self_seq_add(
+ DEPRECATED(LLAMA_API void llama_kv_self_seq_add(
struct llama_context * ctx,
llama_seq_id seq_id,
llama_pos p0,
llama_pos p1,
- llama_pos delta);
+ llama_pos delta),
+ "Use llama_memory_seq_add() instead");
// Integer division of the positions by factor of `d > 1`
// If the KV cache is RoPEd, the KV data is updated accordingly:
// - lazily on next llama_decode()
// p0 < 0 : [0, p1]
// p1 < 0 : [p0, inf)
- LLAMA_API void llama_kv_self_seq_div(
+ DEPRECATED(void llama_kv_self_seq_div(
struct llama_context * ctx,
llama_seq_id seq_id,
llama_pos p0,
llama_pos p1,
- int d);
+ int d),
+ "Use llama_memory_seq_div() instead");
// Returns the smallest position present in the KV cache for the specified sequence
// This is typically non-zero only for SWA caches
// Note that all positions in the range [pos_min, pos_max] are guaranteed to be present in the KV cache
// Return -1 if the sequence is empty
- LLAMA_API llama_pos llama_kv_self_seq_pos_min(
+ DEPRECATED(LLAMA_API llama_pos llama_kv_self_seq_pos_min(
struct llama_context * ctx,
- llama_seq_id seq_id);
+ llama_seq_id seq_id),
+ "Use llama_memory_seq_pos_min() instead");
// Returns the largest position present in the KV cache for the specified sequence
// Note that all positions in the range [pos_min, pos_max] are guaranteed to be present in the KV cache
// Return -1 if the sequence is empty
- LLAMA_API llama_pos llama_kv_self_seq_pos_max(
+ DEPRECATED(LLAMA_API llama_pos llama_kv_self_seq_pos_max(
struct llama_context * ctx,
- llama_seq_id seq_id);
+ llama_seq_id seq_id),
+ "Use llama_memory_seq_pos_max() instead");
// Defragment the KV cache
// This will be applied:
"simply remove this call, the context will automatically decide when to do a defragmentation based on 'defrag_thold'");
// Check if the context supports KV cache shifting
- LLAMA_API bool llama_kv_self_can_shift(const struct llama_context * ctx);
+ DEPRECATED(LLAMA_API bool llama_kv_self_can_shift(const struct llama_context * ctx),
+ "use llama_memory_can_shift() instead");
// Apply the KV cache updates (such as K-shifts, defragmentation, etc.)
DEPRECATED(LLAMA_API void llama_kv_self_update(struct llama_context * ctx),
return memory.get();
}
+// deprecated
void llama_context::kv_self_defrag_sched() {
if (!memory) {
return;
memory_force_optimize = true;
}
+// deprecated
bool llama_context::kv_self_update(bool optimize) {
if (!memory) {
return false;
const uint32_t n_batch = std::min(this->n_batch(), n_ctx);
const uint32_t n_ubatch = std::min(this->n_ubatch(), n_batch);
- memory->clear();
+ memory->clear(true);
for (uint32_t pos_ctx = 0; pos_ctx < n_ctx; pos_ctx += n_batch) {
batch.n_tokens = n_batch;
return ctx->get_memory();
}
-void llama_memory_clear(llama_memory_t mem) {
- mem->clear();
+void llama_memory_clear(llama_memory_t mem, bool data) {
+ if (!mem) {
+ return;
+ }
+
+ mem->clear(data);
}
bool llama_memory_seq_rm(
llama_seq_id seq_id,
llama_pos p0,
llama_pos p1) {
+ if (!mem) {
+ return true;
+ }
+
return mem->seq_rm(seq_id, p0, p1);
}
llama_seq_id seq_id_dst,
llama_pos p0,
llama_pos p1) {
+ if (!mem) {
+ return;
+ }
+
mem->seq_cp(seq_id_src, seq_id_dst, p0, p1);
}
void llama_memory_seq_keep(
llama_memory_t mem,
llama_seq_id seq_id) {
+ if (!mem) {
+ return;
+ }
+
mem->seq_keep(seq_id);
}
llama_pos p0,
llama_pos p1,
llama_pos delta) {
+ if (!mem) {
+ return;
+ }
+
mem->seq_add(seq_id, p0, p1, delta);
}
llama_pos p0,
llama_pos p1,
int d) {
+ if (!mem) {
+ return;
+ }
+
mem->seq_div(seq_id, p0, p1, d);
}
llama_pos llama_memory_seq_pos_min(
llama_memory_t mem,
llama_seq_id seq_id) {
+ if (!mem) {
+ return -1;
+ }
+
return mem->seq_pos_min(seq_id);
}
llama_pos llama_memory_seq_pos_max(
llama_memory_t mem,
llama_seq_id seq_id) {
+ if (!mem) {
+ return -1;
+ }
+
return mem->seq_pos_max(seq_id);
}
bool llama_memory_can_shift(llama_memory_t mem) {
+ if (!mem) {
+ return false;
+ }
+
return mem->get_can_shift();
}
return res;
}
+// deprecated
void llama_kv_self_clear(llama_context * ctx) {
auto * kv = llama_get_memory(ctx);
if (!kv) {
return;
}
- llama_memory_clear(kv);
+ llama_memory_clear(kv, true);
}
+// deprecated
bool llama_kv_self_seq_rm(
llama_context * ctx,
llama_seq_id seq_id,
return llama_memory_seq_rm(kv, seq_id, p0, p1);
}
+// deprecated
void llama_kv_self_seq_cp(
llama_context * ctx,
llama_seq_id seq_id_src,
llama_memory_seq_cp(kv, seq_id_src, seq_id_dst, p0, p1);
}
+// deprecated
void llama_kv_self_seq_keep(llama_context * ctx, llama_seq_id seq_id) {
auto * kv = llama_get_memory(ctx);
if (!kv) {
llama_memory_seq_keep(kv, seq_id);
}
+// deprecated
void llama_kv_self_seq_add(
llama_context * ctx,
llama_seq_id seq_id,
llama_memory_seq_add(kv, seq_id, p0, p1, delta);
}
+// deprecated
void llama_kv_self_seq_div(
llama_context * ctx,
llama_seq_id seq_id,
llama_memory_seq_div(kv, seq_id, p0, p1, d);
}
+// deprecated
llama_pos llama_kv_self_seq_pos_min(llama_context * ctx, llama_seq_id seq_id) {
auto * kv = llama_get_memory(ctx);
if (!kv) {
return llama_memory_seq_pos_min(kv, seq_id);
}
+// deprecated
llama_pos llama_kv_self_seq_pos_max(llama_context * ctx, llama_seq_id seq_id) {
auto * kv = llama_get_memory(ctx);
if (!kv) {
ctx->kv_self_defrag_sched();
}
+// deprecated
bool llama_kv_self_can_shift(const llama_context * ctx) {
auto * kv = llama_get_memory(ctx);
if (!kv) {
}
}
-void llama_kv_cache_recurrent::clear() {
+void llama_kv_cache_recurrent::clear(bool data) {
for (int32_t i = 0; i < (int32_t) size; ++i) {
cells[i].pos = -1;
cells[i].seq_id.clear();
cells[i].src = -1;
cells[i].tail = -1;
}
+
head = 0;
used = 0;
- for (auto & buf : bufs) {
- ggml_backend_buffer_clear(buf.get(), 0);
+ if (data) {
+ for (auto & buf : bufs) {
+ ggml_backend_buffer_clear(buf.get(), 0);
+ }
}
}
if (!res) {
if (seq_id == -1) {
- clear();
+ clear(true);
} else {
seq_rm(seq_id, -1, -1);
}
return false;
}
- clear();
+ clear(true);
for (uint32_t i = 0; i < cell_count; ++i) {
kv_cell & cell = cells[i];
llama_memory_state_ptr init_update(llama_context * lctx, bool optimize) override;
- void clear() override;
+ void clear(bool data) override;
bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
hparams.n_swa, hparams.swa_type);
}
-void llama_kv_cache_unified_iswa::clear() {
- kv_base->clear();
- kv_swa ->clear();
+void llama_kv_cache_unified_iswa::clear(bool data) {
+ kv_base->clear(data);
+ kv_swa ->clear(data);
}
bool llama_kv_cache_unified_iswa::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) {
bool get_can_shift() const override;
- void clear() override;
+ void clear(bool data) override;
bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
}
}
-void llama_kv_cache_unified::clear() {
+void llama_kv_cache_unified::clear(bool data) {
cells.reset();
head = 0;
- for (auto & buf : bufs) {
- ggml_backend_buffer_clear(buf.get(), 0);
+ if (data) {
+ for (auto & buf : bufs) {
+ ggml_backend_buffer_clear(buf.get(), 0);
+ }
}
}
if (!res) {
if (seq_id == -1) {
- clear();
+ clear(true);
} else {
seq_rm(seq_id, -1, -1);
}
return false;
}
- clear();
+ clear(true);
for (uint32_t i = 0; i < cell_count; ++i) {
llama_pos pos;
bool get_can_shift() const override;
- void clear() override;
+ void clear(bool data) override;
bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) override;
void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) override;
// ops
//
- virtual void clear() = 0;
+ // if data == true, the data buffers will also be cleared together with the metadata
+ virtual void clear(bool data) = 0;
virtual bool seq_rm (llama_seq_id seq_id, llama_pos p0, llama_pos p1) = 0;
virtual void seq_cp (llama_seq_id seq_id_src, llama_seq_id seq_id_dst, llama_pos p0, llama_pos p1) = 0;
return 1;
}
+ auto * mem = llama_get_memory(ctx);
+
const int32_t n_kv_max = llama_n_ctx(ctx);
llama_batch batch = llama_batch_init(n_kv_max, 0, 1);
const auto t_pp_start = ggml_time_us();
- llama_kv_self_clear(ctx);
+ llama_memory_clear(mem, false);
if (!decode_helper(ctx, batch, ctx_params.n_batch)) {
LOG_ERR("%s: llama_decode() failed\n", __func__);
if (is_pp_shared) {
for (int32_t i = 1; i < pl; ++i) {
- llama_kv_self_seq_cp(ctx, 0, i, -1, -1);
+ llama_memory_seq_cp(mem, 0, i, -1, -1);
}
}
}
static bool get_hidden_layers(llama_context * ctx, std::vector<llama_token> & tokens) {
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
if (llama_decode(ctx, llama_batch_get_one(tokens.data(), tokens.size()))) {
fprintf(stderr, "%s : failed to eval\n", __func__);
return false;
const auto t_start = std::chrono::high_resolution_clock::now();
// clear the KV cache
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
llama_batch batch = llama_batch_init(n_batch, 0, 1);
test t(inst, lmodel, ctx);
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), false);
// cool off before the test
if (params.delay) {
}
for (int i = 0; i < params.reps; i++) {
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), false);
if (t.n_depth > 0) {
if (params.progress) {
return 1;
}
+ auto * mem = llama_get_memory(ctx);
+
const llama_vocab * vocab = llama_model_get_vocab(model);
auto chat_templates = common_chat_templates_init(model, params.chat_template);
}
// remove any "future" tokens that we might have inherited from the previous session
- llama_kv_self_seq_rm(ctx, -1, n_matching_session_tokens, -1);
+ llama_memory_seq_rm(mem, -1, n_matching_session_tokens, -1);
}
LOG_DBG("recalculate the cached logits (check): embd_inp.size() %zu, n_matching_session_tokens %zu, embd_inp.size() %zu, session_tokens.size() %zu\n",
LOG_DBG("context full, swapping: n_past = %d, n_left = %d, n_ctx = %d, n_keep = %d, n_discard = %d\n",
n_past, n_left, n_ctx, params.n_keep, n_discard);
- llama_kv_self_seq_rm (ctx, 0, params.n_keep , params.n_keep + n_discard);
- llama_kv_self_seq_add(ctx, 0, params.n_keep + n_discard, n_past, -n_discard);
+ llama_memory_seq_rm (mem, 0, params.n_keep , params.n_keep + n_discard);
+ llama_memory_seq_add(mem, 0, params.n_keep + n_discard, n_past, -n_discard);
n_past -= n_discard;
LOG_DBG("div: [%6d, %6d] / %6d -> [%6d, %6d]\n", ga_i + ib*bd, ga_i + ib*bd + ga_w, ga_n, (ga_i + ib*bd)/ga_n, (ga_i + ib*bd + ga_w)/ga_n);
LOG_DBG("shift: [%6d, %6d] + %6d -> [%6d, %6d]\n", ga_i + ib*bd + ga_w, n_past + ib*bd, dd, ga_i + ib*bd + ga_w + dd, n_past + ib*bd + dd);
- llama_kv_self_seq_add(ctx, 0, ga_i, n_past, ib*bd);
- llama_kv_self_seq_div(ctx, 0, ga_i + ib*bd, ga_i + ib*bd + ga_w, ga_n);
- llama_kv_self_seq_add(ctx, 0, ga_i + ib*bd + ga_w, n_past + ib*bd, dd);
+ llama_memory_seq_add(mem, 0, ga_i, n_past, ib*bd);
+ llama_memory_seq_div(mem, 0, ga_i + ib*bd, ga_i + ib*bd + ga_w, ga_n);
+ llama_memory_seq_add(mem, 0, ga_i + ib*bd + ga_w, n_past + ib*bd, dd);
n_past -= bd;
}
if (line == "/clear") {
ctx.n_past = 0;
- llama_kv_self_seq_rm(ctx.lctx, 0, 1, -1); // keep BOS
+ llama_memory_seq_rm(llama_get_memory(ctx.lctx), 0, 1, -1); // keep BOS
LOG("Chat history cleared\n\n");
continue;
}
const auto t_start = std::chrono::high_resolution_clock::now();
// clear the KV cache
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
llama_batch batch = llama_batch_init(n_batch, 0, 1);
const auto t_start = std::chrono::high_resolution_clock::now();
// clear the KV cache
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
for (int j = 0; j < num_batches; ++j) {
const int batch_start = start + j * n_batch;
return;
}
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
// decode all tasks [i0, i1)
if (!decode_helper(ctx, batch, batch_logits, n_batch, n_vocab)) {
return;
}
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
// decode all tasks [i0, i1)
if (!decode_helper(ctx, batch, batch_logits, n_batch, n_vocab)) {
return;
}
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
// decode all tasks [i0, i1)
if (!decode_helper(ctx, batch, batch_logits, n_batch, n_vocab)) {
}
// clear the KV cache
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
llama_batch batch = llama_batch_init(n_batch, 0, 1);
// Function to tokenize the prompt
static int tokenize_prompt(const llama_vocab * vocab, const std::string & prompt,
std::vector<llama_token> & prompt_tokens, const LlamaData & llama_data) {
- const bool is_first = llama_kv_self_seq_pos_max(llama_data.context.get(), 0) == 0;
+ const bool is_first = llama_memory_seq_pos_max(llama_get_memory(llama_data.context.get()), 0) == 0;
const int n_prompt_tokens = -llama_tokenize(vocab, prompt.c_str(), prompt.size(), NULL, 0, is_first, true);
prompt_tokens.resize(n_prompt_tokens);
// Check if we have enough space in the context to evaluate this batch
static int check_context_size(const llama_context_ptr & ctx, const llama_batch & batch) {
const int n_ctx = llama_n_ctx(ctx.get());
- const int n_ctx_used = llama_kv_self_seq_pos_max(ctx.get(), 0);
+ const int n_ctx_used = llama_memory_seq_pos_max(llama_get_memory(ctx.get()), 0);
if (n_ctx_used + batch.n_tokens > n_ctx) {
printf(LOG_COL_DEFAULT "\n");
printe("context size exceeded\n");
}
}
- if (!llama_kv_self_can_shift(ctx)) {
+ if (!llama_memory_can_shift(llama_get_memory(ctx))) {
if (params_base.ctx_shift) {
params_base.ctx_shift = false;
SRV_WRN("%s\n", "ctx_shift is not supported by this context, it will be disabled");
SRV_DBG("%s", "clearing KV cache\n");
// clear the entire KV cache
- llama_kv_self_clear(ctx);
+ llama_memory_clear(llama_get_memory(ctx), true);
clean_kv_cache = false;
}
// Erase token cache
const size_t n_erased = slot->cache_tokens.size();
- llama_kv_self_seq_rm(ctx, slot->id, -1, -1);
+ llama_memory_seq_rm(llama_get_memory(ctx), slot->id, -1, -1);
slot->cache_tokens.clear();
auto res = std::make_unique<server_task_result_slot_erase>();
SLT_WRN(slot, "slot context shift, n_keep = %d, n_left = %d, n_discard = %d\n", n_keep, n_left, n_discard);
- llama_kv_self_seq_rm (ctx, slot.id, n_keep , n_keep + n_discard);
- llama_kv_self_seq_add(ctx, slot.id, n_keep + n_discard, slot.n_past, -n_discard);
+ llama_memory_seq_rm (llama_get_memory(ctx), slot.id, n_keep , n_keep + n_discard);
+ llama_memory_seq_add(llama_get_memory(ctx), slot.id, n_keep + n_discard, slot.n_past, -n_discard);
// add generated tokens to cache
{
const int64_t kv_shift = (int64_t) head_p - (int64_t) head_c;
- llama_kv_self_seq_rm (ctx, slot.id, head_p, head_c);
- llama_kv_self_seq_add(ctx, slot.id, head_c, head_c + n_match, kv_shift);
+ llama_memory_seq_rm (llama_get_memory(ctx), slot.id, head_p, head_c);
+ llama_memory_seq_add(llama_get_memory(ctx), slot.id, head_c, head_c + n_match, kv_shift);
for (size_t i = 0; i < n_match; i++) {
slot.cache_tokens.set_token(head_p + i, slot.cache_tokens[head_c + i]);
}
if (slot.n_past > 0 && slot.n_past < (int) slot.cache_tokens.size()) {
- const auto pos_min = llama_kv_self_seq_pos_min(ctx, slot.id);
+ const auto pos_min = llama_memory_seq_pos_min(llama_get_memory(ctx), slot.id);
if (pos_min == -1) {
SLT_ERR(slot, "n_past = %d, cache_tokens.size() = %d, seq_id = %d, pos_min = %d\n", slot.n_past, (int) slot.cache_tokens.size(), slot.id, pos_min);
GGML_ABORT("pos_min == -1, but n_past > 0 - should not happen: https://github.com/ggml-org/llama.cpp/pull/13833#discussion_r2116181237");
}
// keep only the common part
- if (!llama_kv_self_seq_rm(ctx, slot.id, slot.n_past, -1)) {
+ if (!llama_memory_seq_rm(llama_get_memory(ctx), slot.id, slot.n_past, -1)) {
// could not partially delete (likely using a non-Transformer model)
- llama_kv_self_seq_rm(ctx, slot.id, -1, -1);
+ llama_memory_seq_rm(llama_get_memory(ctx), slot.id, -1, -1);
// there is no common part left
slot.n_past = 0;
slot.cache_tokens.push_back(id);
slot.cache_tokens.insert({ids.begin(), ids.end() - 1});
- llama_kv_self_seq_rm(ctx, slot.id, slot.n_past, -1);
+ llama_memory_seq_rm(llama_get_memory(ctx), slot.id, slot.n_past, -1);
for (size_t i = 0; i < ids.size(); ++i) {
completion_token_output result;