@@ -93,6 +93,7 @@ llama_context::llama_context(
9393 }
9494
9595 cparams.n_ubatch = std::min (cparams.n_batch , params.n_ubatch == 0 ? params.n_batch : params.n_ubatch );
96+
9697 cparams.op_offload = params.op_offload ;
9798
9899 const uint32_t n_ctx_per_seq = cparams.n_ctx / cparams.n_seq_max ;
@@ -176,8 +177,9 @@ llama_context::llama_context(
176177 // init the memory module
177178 if (!hparams.vocab_only ) {
178179 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 ,
181183 };
182184
183185 memory.reset (model.create_memory (params_mem, cparams));
@@ -947,8 +949,6 @@ int llama_context::decode(llama_batch & inp_batch) {
947949
948950 // find KV slot
949951 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-
952952 return 1 ;
953953 }
954954
@@ -2093,6 +2093,7 @@ llama_context_params llama_context_default_params() {
20932093 /* .flash_attn =*/ false ,
20942094 /* .no_perf =*/ true ,
20952095 /* .op_offload =*/ true ,
2096+ /* .swa_full =*/ true ,
20962097 };
20972098
20982099 return result;
@@ -2467,6 +2468,15 @@ void llama_kv_self_seq_div(
24672468 kv->seq_div (seq_id, p0, p1, d);
24682469}
24692470
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+
24702480// deprecated
24712481llama_pos llama_kv_cache_seq_pos_max (llama_context * ctx, llama_seq_id seq_id) {
24722482 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) {
24752485llama_pos llama_kv_self_seq_pos_max (llama_context * ctx, llama_seq_id seq_id) {
24762486 const auto * kv = ctx->get_kv_self ();
24772487 if (!kv) {
2478- return 0 ;
2488+ return - 1 ;
24792489 }
24802490
24812491 return kv->seq_pos_max (seq_id);
@@ -2637,7 +2647,21 @@ int32_t llama_encode(
26372647int32_t llama_decode (
26382648 llama_context * ctx,
26392649 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+
26412665 if (ret != 0 ) {
26422666 LLAMA_LOG_ERROR (" %s: failed to decode, ret = %d\n " , __func__, ret);
26432667 }
0 commit comments