MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / update

Method update

smallthinker/src/llama-kv-cache-unified.cpp:363–438  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

361}
362
363bool 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__);

Callers 15

run_with_preset.pyFile · 0.45
merge_multifile_modelsFunction · 0.45
merge_multifile_modelsFunction · 0.45
get_tensorsMethod · 0.45
updateConversationNameFunction · 0.45
appendMsgFunction · 0.45
kv_self_updateMethod · 0.45
sha256sumFunction · 0.45
merge_multifile_modelsFunction · 0.45
__init__Method · 0.45

Calls 10

ggml_backend_sched_resetFunction · 0.85
get_schedMethod · 0.80
get_has_shiftMethod · 0.80
graph_initMethod · 0.80
get_ctx_computeMethod · 0.80
set_inputsMethod · 0.80
reset_shiftMethod · 0.80
graph_max_nodesMethod · 0.80
graph_computeMethod · 0.45

Tested by 1