Skip to content

Commit b15fede

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

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/llama-context.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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)