@@ -93,6 +93,7 @@ llama_context::llama_context(
93
93
}
94
94
95
95
cparams.n_ubatch = std::min (cparams.n_batch , params.n_ubatch == 0 ? params.n_batch : params.n_ubatch );
96
+
96
97
cparams.op_offload = params.op_offload ;
97
98
98
99
const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max ;
@@ -176,8 +177,9 @@ llama_context::llama_context(
176
177
// init the memory module
177
178
if (!hparams.vocab_only ) {
178
179
llama_memory_params params_mem = {
179
- /* .type_k =*/ params.type_k ,
180
- /* .type_v =*/ params.type_v ,
180
+ /* .type_k =*/ params.type_k ,
181
+ /* .type_v =*/ params.type_v ,
182
+ /* .swa_full =*/ params.swa_full ,
181
183
};
182
184
183
185
memory.reset (model.create_memory (params_mem, cparams));
@@ -947,8 +949,6 @@ int llama_context::decode(llama_batch & inp_batch) {
947
949
948
950
// find KV slot
949
951
if (!kv_self->find_slot (ubatch)) {
950
- LLAMA_LOG_WARN (" %s: failed to find KV cache slot for ubatch of size %d\n " , __func__, ubatch.n_tokens );
951
-
952
952
return 1 ;
953
953
}
954
954
@@ -2093,6 +2093,7 @@ llama_context_params llama_context_default_params() {
2093
2093
/* .flash_attn =*/ false ,
2094
2094
/* .no_perf =*/ true ,
2095
2095
/* .op_offload =*/ true ,
2096
+ /* .swa_full =*/ true ,
2096
2097
};
2097
2098
2098
2099
return result;
@@ -2467,6 +2468,15 @@ void llama_kv_self_seq_div(
2467
2468
kv->seq_div (seq_id, p0, p1, d);
2468
2469
}
2469
2470
2471
+ llama_pos llama_kv_self_seq_pos_min (llama_context * ctx, llama_seq_id seq_id) {
2472
+ const auto * kv = ctx->get_kv_self ();
2473
+ if (!kv) {
2474
+ return -1 ;
2475
+ }
2476
+
2477
+ return kv->seq_pos_min (seq_id);
2478
+ }
2479
+
2470
2480
// deprecated
2471
2481
llama_pos llama_kv_cache_seq_pos_max (llama_context * ctx, llama_seq_id seq_id) {
2472
2482
return llama_kv_self_seq_pos_max (ctx, seq_id);
@@ -2475,7 +2485,7 @@ llama_pos llama_kv_cache_seq_pos_max(llama_context * ctx, llama_seq_id seq_id) {
2475
2485
llama_pos llama_kv_self_seq_pos_max (llama_context * ctx, llama_seq_id seq_id) {
2476
2486
const auto * kv = ctx->get_kv_self ();
2477
2487
if (!kv) {
2478
- return 0 ;
2488
+ return - 1 ;
2479
2489
}
2480
2490
2481
2491
return kv->seq_pos_max (seq_id);
@@ -2637,7 +2647,21 @@ int32_t llama_encode(
2637
2647
int32_t llama_decode (
2638
2648
llama_context * ctx,
2639
2649
llama_batch batch) {
2640
- const int ret = ctx->decode (batch);
2650
+ int ret = ctx->decode (batch);
2651
+
2652
+ // defrag and try again
2653
+ // TODO: distinguish return code when we are sure that even after defrag there is no space available
2654
+ if (ret == 1 ) {
2655
+ llama_kv_self_defrag (ctx);
2656
+ ret = ctx->decode (batch);
2657
+
2658
+ if (ret == 1 ) {
2659
+ LLAMA_LOG_WARN (" %s: failed to find KV cache slot for batch of size %d\n " , __func__, batch.n_tokens );
2660
+
2661
+ return ret;
2662
+ }
2663
+ }
2664
+
2641
2665
if (ret != 0 ) {
2642
2666
LLAMA_LOG_ERROR (" %s: failed to decode, ret = %d\n " , __func__, ret);
2643
2667
}
0 commit comments