| 361 | } |
| 362 | |
| 363 | bool llama_kv_cache_unified::update(llama_context & lctx) { |
| 364 | bool updated = false; |
| 365 | |
| 366 | auto * sched = lctx.get_sched(); |
| 367 | |
| 368 | if (cells.get_has_shift()) { |
| 369 | if (!get_can_shift()) { |
| 370 | GGML_ABORT("The current KV cache / model configuration does not support K-shift"); |
| 371 | } |
| 372 | |
| 373 | LLAMA_LOG_DEBUG("%s: applying K-shift\n", __func__); |
| 374 | |
| 375 | // apply K-shift if needed |
| 376 | if (hparams.rope_type != LLAMA_ROPE_TYPE_NONE) { |
| 377 | ggml_backend_sched_reset(sched); |
| 378 | |
| 379 | auto * gf = lctx.graph_init(); |
| 380 | |
| 381 | auto res = build_graph_shift(lctx.get_cparams(), lctx.get_ctx_compute(), gf); |
| 382 | if (!res) { |
| 383 | LLAMA_LOG_ERROR("%s: failed to build graph for K-shift\n", __func__); |
| 384 | return updated; |
| 385 | } |
| 386 | |
| 387 | if (!ggml_backend_sched_alloc_graph(sched, gf)) { |
| 388 | LLAMA_LOG_ERROR("%s: failed to allocate compute graph for K-shift\n", __func__); |
| 389 | return updated; |
| 390 | } |
| 391 | |
| 392 | res->set_inputs(nullptr); |
| 393 | |
| 394 | if (lctx.graph_compute(gf, false) != GGML_STATUS_SUCCESS) { |
| 395 | LLAMA_LOG_ERROR("%s: failed to compute K-shift\n", __func__); |
| 396 | return updated; |
| 397 | } |
| 398 | |
| 399 | updated = true; |
| 400 | } |
| 401 | |
| 402 | cells.reset_shift(); |
| 403 | } |
| 404 | |
| 405 | if (do_defrag) { |
| 406 | LLAMA_LOG_DEBUG("%s: defragmenting KV cache\n", __func__); |
| 407 | |
| 408 | if (defrag_prepare(lctx.graph_max_nodes())) { |
| 409 | ggml_backend_sched_reset(sched); |
| 410 | |
| 411 | auto * gf = lctx.graph_init(); |
| 412 | |
| 413 | auto res = build_graph_defrag(lctx.get_cparams(), lctx.get_ctx_compute(), gf); |
| 414 | if (!res) { |
| 415 | LLAMA_LOG_ERROR("%s: failed to build graph for defrag\n", __func__); |
| 416 | return updated; |
| 417 | } |
| 418 | |
| 419 | if (!ggml_backend_sched_alloc_graph(sched, gf)) { |
| 420 | LLAMA_LOG_ERROR("%s: failed to allocate compute graph for defrag\n", __func__); |