Skip to content

Commit d26f5cf

Browse files
committed
kv-cache : fix defrag condition
ggml-ci
1 parent 0f1c1ca commit d26f5cf

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/llama-context.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ struct llama_batch_manager : public llama_batch_manager_i {
481481

482482
// reserve a worst case graph if needed
483483
if (lctx.need_reserve) {
484-
LLAMA_LOG_DEBUG("%s: reserving a worst case graph\n", __func__);
484+
LLAMA_LOG_ERROR("%s: reserving a worst case graph\n", __func__);
485485

486486
const auto & cparams = lctx.cparams;
487487
const auto & model = lctx.model;
@@ -532,11 +532,13 @@ struct llama_batch_manager : public llama_batch_manager_i {
532532

533533
// decide if we need to defrag the kv cache
534534
if (cparams.causal_attn && cparams.defrag_thold >= 0.0f) {
535-
const float fragmentation = kv_self.n >= 128 ? 1.0f - float(kv_self.used)/float(kv_self.n) : 0.0f;
535+
// - do not defrag small contexts (i.e. < 2048 tokens)
536+
// - count the padding towards the number of used tokens
537+
const float fragmentation = kv_self.n >= 2048 ? 1.0f - float(kv_self.used + lctx.get_ctx_padding(cparams))/float(kv_self.n) : 0.0f;
536538

537539
// queue defragmentation for next llama_kv_cache_update
538540
if (fragmentation > cparams.defrag_thold) {
539-
//LLAMA_LOG_INFO("fragmentation: %.2f\n", fragmentation);
541+
LLAMA_LOG_DEBUG("%s: fragmentation: %.2f - requesting defrag\n", __func__, fragmentation);
540542

541543
kv_self.defrag();
542544
}

0 commit comments

Comments
 (0)