Skip to content

Commit cd36b5e

Browse files
authored
llama : remove deprecated llama_kv_self API (ggml-org#15472)
ggml-ci
1 parent 3f196be commit cd36b5e

File tree

3 files changed

+6
-297
lines changed

3 files changed

+6
-297
lines changed

include/llama.h

Lines changed: 0 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -663,111 +663,6 @@ extern "C" {
663663
// Check if the memory supports shifting
664664
LLAMA_API bool llama_memory_can_shift(llama_memory_t mem);
665665

666-
//
667-
// KV cache for self-attention (TODO: deprecate in favor of llama_memory)
668-
//
669-
670-
// Returns the number of tokens in the KV cache (slow, use only for debug)
671-
// If a KV cell has multiple sequences assigned to it, it will be counted multiple times
672-
DEPRECATED(LLAMA_API int32_t llama_kv_self_n_tokens(const struct llama_context * ctx),
673-
"Use llama_kv_self_seq_pos_max() and llama_kv_self_seq_pos_min() instead (https://github.com/ggml-org/llama.cpp/issues/13793)");
674-
675-
// Returns the number of used KV cells (i.e. have at least one sequence assigned to them)
676-
DEPRECATED(LLAMA_API int32_t llama_kv_self_used_cells(const struct llama_context * ctx),
677-
"Use llama_kv_self_seq_pos_max() and llama_kv_self_seq_pos_min() instead (https://github.com/ggml-org/llama.cpp/issues/13793)");
678-
679-
// Clear the KV cache - both cell info is erased and KV data is zeroed
680-
DEPRECATED(LLAMA_API void llama_kv_self_clear(
681-
struct llama_context * ctx),
682-
"Use llama_memory_clear() instead");
683-
684-
// Removes all tokens that belong to the specified sequence and have positions in [p0, p1)
685-
// Returns false if a partial sequence cannot be removed. Removing a whole sequence never fails
686-
// seq_id < 0 : match any sequence
687-
// p0 < 0 : [0, p1]
688-
// p1 < 0 : [p0, inf)
689-
DEPRECATED(LLAMA_API bool llama_kv_self_seq_rm(
690-
struct llama_context * ctx,
691-
llama_seq_id seq_id,
692-
llama_pos p0,
693-
llama_pos p1),
694-
"Use llama_memory_seq_rm() instead");
695-
696-
// Copy all tokens that belong to the specified sequence to another sequence
697-
// Note that this does not allocate extra KV cache memory - it simply assigns the tokens to the new sequence
698-
// p0 < 0 : [0, p1]
699-
// p1 < 0 : [p0, inf)
700-
DEPRECATED(LLAMA_API void llama_kv_self_seq_cp(
701-
struct llama_context * ctx,
702-
llama_seq_id seq_id_src,
703-
llama_seq_id seq_id_dst,
704-
llama_pos p0,
705-
llama_pos p1),
706-
"Use llama_memory_seq_cp() instead");
707-
708-
// Removes all tokens that do not belong to the specified sequence
709-
DEPRECATED(LLAMA_API void llama_kv_self_seq_keep(
710-
struct llama_context * ctx,
711-
llama_seq_id seq_id),
712-
"Use llama_memory_seq_keep() instead");
713-
714-
// Adds relative position "delta" to all tokens that belong to the specified sequence and have positions in [p0, p1)
715-
// If the KV cache is RoPEd, the KV data is updated accordingly:
716-
// - lazily on next llama_decode()
717-
// p0 < 0 : [0, p1]
718-
// p1 < 0 : [p0, inf)
719-
DEPRECATED(LLAMA_API void llama_kv_self_seq_add(
720-
struct llama_context * ctx,
721-
llama_seq_id seq_id,
722-
llama_pos p0,
723-
llama_pos p1,
724-
llama_pos delta),
725-
"Use llama_memory_seq_add() instead");
726-
727-
// Integer division of the positions by factor of `d > 1`
728-
// If the KV cache is RoPEd, the KV data is updated accordingly:
729-
// - lazily on next llama_decode()
730-
// p0 < 0 : [0, p1]
731-
// p1 < 0 : [p0, inf)
732-
DEPRECATED(LLAMA_API void llama_kv_self_seq_div(
733-
struct llama_context * ctx,
734-
llama_seq_id seq_id,
735-
llama_pos p0,
736-
llama_pos p1,
737-
int d),
738-
"Use llama_memory_seq_div() instead");
739-
740-
// Returns the smallest position present in the KV cache for the specified sequence
741-
// This is typically non-zero only for SWA caches
742-
// Note that all positions in the range [pos_min, pos_max] are guaranteed to be present in the KV cache
743-
// Return -1 if the sequence is empty
744-
DEPRECATED(LLAMA_API llama_pos llama_kv_self_seq_pos_min(
745-
struct llama_context * ctx,
746-
llama_seq_id seq_id),
747-
"Use llama_memory_seq_pos_min() instead");
748-
749-
// Returns the largest position present in the KV cache for the specified sequence
750-
// Note that all positions in the range [pos_min, pos_max] are guaranteed to be present in the KV cache
751-
// Return -1 if the sequence is empty
752-
DEPRECATED(LLAMA_API llama_pos llama_kv_self_seq_pos_max(
753-
struct llama_context * ctx,
754-
llama_seq_id seq_id),
755-
"Use llama_memory_seq_pos_max() instead");
756-
757-
// Defragment the KV cache
758-
// This will be applied:
759-
// - lazily on next llama_decode()
760-
DEPRECATED(LLAMA_API void llama_kv_self_defrag(struct llama_context * ctx),
761-
"simply remove this call, the context will automatically decide when to do a defragmentation based on 'defrag_thold'");
762-
763-
// Check if the context supports KV cache shifting
764-
DEPRECATED(LLAMA_API bool llama_kv_self_can_shift(const struct llama_context * ctx),
765-
"use llama_memory_can_shift() instead");
766-
767-
// Apply the KV cache updates (such as K-shifts, defragmentation, etc.)
768-
DEPRECATED(LLAMA_API void llama_kv_self_update(struct llama_context * ctx),
769-
"simply remove this call, updates are applied lazily on the next llama_decode()");
770-
771666
//
772667
// State / sessions
773668
//

src/llama-context.cpp

Lines changed: 4 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ llama_context::llama_context(
9393
// the batch has to be at least GGML_KQ_MASK_PAD because we will be padding the KQ_mask
9494
// this is required by GPU kernels in order to avoid out-of-bounds accesses (e.g. ggml_flash_attn_ext)
9595
// ref: https://github.com/ggerganov/llama.cpp/pull/5021
96-
// TODO: this padding is not needed for the cache-less context so we should probably move it to llama_context_kv_self
96+
// TODO: this padding is not needed for the cache-less context so we should probably move it to llama_memory
9797
if (cparams.n_batch < GGML_KQ_MASK_PAD) {
9898
LLAMA_LOG_WARN("%s: n_batch is less than GGML_KQ_MASK_PAD - increasing to %d\n", __func__, GGML_KQ_MASK_PAD);
9999
cparams.n_batch = GGML_KQ_MASK_PAD;
@@ -439,26 +439,12 @@ llama_memory_t llama_context::get_memory() const {
439439
return memory.get();
440440
}
441441

442-
// deprecated
443-
void llama_context::kv_self_defrag_sched() {
444-
if (!memory) {
445-
return;
446-
}
447-
448-
memory_force_optimize = true;
449-
}
450-
451-
// deprecated
452-
bool llama_context::kv_self_update(bool optimize) {
442+
bool llama_context::memory_update(bool optimize) {
453443
if (!memory) {
454444
return false;
455445
}
456446

457447
{
458-
// TODO: remove in the future
459-
optimize |= memory_force_optimize;
460-
memory_force_optimize = false;
461-
462448
const auto mctx = memory->init_update(this, optimize);
463449
switch (mctx->get_status()) {
464450
case LLAMA_MEMORY_STATUS_SUCCESS:
@@ -993,7 +979,7 @@ int llama_context::decode(const llama_batch & batch_inp) {
993979
bool did_optimize = false;
994980

995981
// handle any pending defrags/shifts
996-
kv_self_update(false);
982+
memory_update(false);
997983

998984
llama_memory_context_ptr mctx;
999985

@@ -1018,7 +1004,7 @@ int llama_context::decode(const llama_batch & batch_inp) {
10181004
if (!did_optimize) {
10191005
did_optimize = true;
10201006

1021-
if (kv_self_update(true)) {
1007+
if (memory_update(true)) {
10221008
LLAMA_LOG_DEBUG("%s: retrying batch size %d after cache optimization\n", __func__, balloc->get_n_tokens());
10231009

10241010
continue;
@@ -2338,11 +2324,6 @@ const llama_model * llama_get_model(const llama_context * ctx) {
23382324
return &ctx->get_model();
23392325
}
23402326

2341-
// deprecated
2342-
void llama_kv_self_update(llama_context * ctx) {
2343-
ctx->kv_self_update(false);
2344-
}
2345-
23462327
enum llama_pooling_type llama_pooling_type(const llama_context * ctx) {
23472328
return ctx->pooling_type();
23482329
}
@@ -2560,168 +2541,6 @@ bool llama_memory_can_shift(llama_memory_t mem) {
25602541
return mem->get_can_shift();
25612542
}
25622543

2563-
//
2564-
// kv cache
2565-
//
2566-
2567-
// deprecated
2568-
int32_t llama_kv_self_n_tokens(const llama_context * ctx) {
2569-
const auto * kv = llama_get_memory(ctx);
2570-
if (!kv) {
2571-
return 0;
2572-
}
2573-
2574-
int32_t res = 0;
2575-
2576-
for (uint32_t s = 0; s < ctx->get_cparams().n_seq_max; s++) {
2577-
const llama_pos p0 = kv->seq_pos_min(s);
2578-
const llama_pos p1 = kv->seq_pos_max(s);
2579-
2580-
if (p0 >= 0) {
2581-
res += (p1 - p0) + 1;
2582-
}
2583-
}
2584-
2585-
return res;
2586-
}
2587-
2588-
// deprecated
2589-
// note: this is the same as above - will be removed anyway, so it's ok
2590-
int32_t llama_kv_self_used_cells(const llama_context * ctx) {
2591-
const auto * kv = llama_get_memory(ctx);
2592-
if (!kv) {
2593-
return 0;
2594-
}
2595-
2596-
int32_t res = 0;
2597-
2598-
for (uint32_t s = 0; s < ctx->get_cparams().n_seq_max; s++) {
2599-
const llama_pos p0 = kv->seq_pos_min(s);
2600-
const llama_pos p1 = kv->seq_pos_max(s);
2601-
2602-
if (p0 >= 0) {
2603-
res += (p1 - p0) + 1;
2604-
}
2605-
}
2606-
2607-
return res;
2608-
}
2609-
2610-
// deprecated
2611-
void llama_kv_self_clear(llama_context * ctx) {
2612-
auto * kv = llama_get_memory(ctx);
2613-
if (!kv) {
2614-
return;
2615-
}
2616-
2617-
llama_memory_clear(kv, true);
2618-
}
2619-
2620-
// deprecated
2621-
bool llama_kv_self_seq_rm(
2622-
llama_context * ctx,
2623-
llama_seq_id seq_id,
2624-
llama_pos p0,
2625-
llama_pos p1) {
2626-
auto * kv = llama_get_memory(ctx);
2627-
if (!kv) {
2628-
return true;
2629-
}
2630-
2631-
return llama_memory_seq_rm(kv, seq_id, p0, p1);
2632-
}
2633-
2634-
// deprecated
2635-
void llama_kv_self_seq_cp(
2636-
llama_context * ctx,
2637-
llama_seq_id seq_id_src,
2638-
llama_seq_id seq_id_dst,
2639-
llama_pos p0,
2640-
llama_pos p1) {
2641-
auto * kv = llama_get_memory(ctx);
2642-
if (!kv) {
2643-
return;
2644-
}
2645-
2646-
llama_memory_seq_cp(kv, seq_id_src, seq_id_dst, p0, p1);
2647-
}
2648-
2649-
// deprecated
2650-
void llama_kv_self_seq_keep(llama_context * ctx, llama_seq_id seq_id) {
2651-
auto * kv = llama_get_memory(ctx);
2652-
if (!kv) {
2653-
return;
2654-
}
2655-
2656-
llama_memory_seq_keep(kv, seq_id);
2657-
}
2658-
2659-
// deprecated
2660-
void llama_kv_self_seq_add(
2661-
llama_context * ctx,
2662-
llama_seq_id seq_id,
2663-
llama_pos p0,
2664-
llama_pos p1,
2665-
llama_pos delta) {
2666-
auto * kv = llama_get_memory(ctx);
2667-
if (!kv) {
2668-
return;
2669-
}
2670-
2671-
llama_memory_seq_add(kv, seq_id, p0, p1, delta);
2672-
}
2673-
2674-
// deprecated
2675-
void llama_kv_self_seq_div(
2676-
llama_context * ctx,
2677-
llama_seq_id seq_id,
2678-
llama_pos p0,
2679-
llama_pos p1,
2680-
int d) {
2681-
auto * kv = llama_get_memory(ctx);
2682-
if (!kv) {
2683-
return;
2684-
}
2685-
2686-
llama_memory_seq_div(kv, seq_id, p0, p1, d);
2687-
}
2688-
2689-
// deprecated
2690-
llama_pos llama_kv_self_seq_pos_min(llama_context * ctx, llama_seq_id seq_id) {
2691-
auto * kv = llama_get_memory(ctx);
2692-
if (!kv) {
2693-
return -1;
2694-
}
2695-
2696-
return llama_memory_seq_pos_min(kv, seq_id);
2697-
}
2698-
2699-
// deprecated
2700-
llama_pos llama_kv_self_seq_pos_max(llama_context * ctx, llama_seq_id seq_id) {
2701-
auto * kv = llama_get_memory(ctx);
2702-
if (!kv) {
2703-
return -1;
2704-
}
2705-
2706-
return llama_memory_seq_pos_max(kv, seq_id);
2707-
}
2708-
2709-
// deprecated
2710-
void llama_kv_self_defrag(llama_context * ctx) {
2711-
// force defrag
2712-
ctx->kv_self_defrag_sched();
2713-
}
2714-
2715-
// deprecated
2716-
bool llama_kv_self_can_shift(const llama_context * ctx) {
2717-
auto * kv = llama_get_memory(ctx);
2718-
if (!kv) {
2719-
return false;
2720-
}
2721-
2722-
return llama_memory_can_shift(kv);
2723-
}
2724-
27252544
// llama state API
27262545

27272546
// deprecated

src/llama-context.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,8 @@ struct llama_context {
4646

4747
llama_memory_t get_memory() const;
4848

49-
// return true of the KV cache was updated
50-
// TODO: remove
51-
bool kv_self_update(bool optimize);
52-
void kv_self_defrag_sched();
49+
// return true if the memory was updated
50+
bool memory_update(bool optimize);
5351

5452
enum llama_pooling_type pooling_type() const;
5553

@@ -230,9 +228,6 @@ struct llama_context {
230228

231229
std::unique_ptr<llama_memory_i> memory;
232230

233-
// TODO: temporary, until the llama_kv_self_defrag() API is removed
234-
bool memory_force_optimize = false;
235-
236231
// decode output (2-dimensional array: [n_outputs][n_vocab])
237232
size_t logits_size = 0; // capacity (of floats) for logits
238233
float * logits = nullptr;

0 commit comments

Comments
 (0)