@@ -2719,7 +2719,7 @@ struct llama_kv_cell {
27192719};
27202720
27212721// ring-buffer of cached KV data
2722- struct llama_kv_cache {
2722+ struct llama_kv_self_cache {
27232723 bool has_shift = false;
27242724 bool do_defrag = false;
27252725 bool v_trans = true; // the value tensor is transposed
@@ -2820,7 +2820,7 @@ struct llama_rs_seq_meta {
28202820};
28212821
28222822// ring-buffered tree of cached recurrent state data
2823- struct llama_rs_cache {
2823+ struct llama_rs_self_cache {
28242824
28252825 uint32_t head = 0; // first state used for the last slot
28262826 uint32_t size = 0;
@@ -3444,12 +3444,12 @@ struct llama_rs_cache {
34443444 }
34453445};
34463446
3447- struct llama_past {
3447+ struct llama_kv_cache {
34483448 // key + value cache for self attention
3449- llama_kv_cache kv;
3449+ llama_kv_self_cache kv;
34503450
34513451 // recurrent state cache for state space models
3452- llama_rs_cache rs;
3452+ llama_rs_self_cache rs;
34533453
34543454 std::vector<struct ggml_context *> ctxs;
34553455 std::vector<ggml_backend_buffer_t> bufs;
@@ -3463,7 +3463,7 @@ struct llama_past {
34633463 return size;
34643464 }
34653465
3466- ~llama_past () {
3466+ ~llama_kv_cache () {
34673467 for (struct ggml_context * ctx : ctxs) {
34683468 ggml_free(ctx);
34693469 }
@@ -3949,7 +3949,7 @@ struct llama_context {
39493949 struct llama_cparams cparams;
39503950 struct llama_sampling sampling;
39513951 struct llama_sbatch sbatch;
3952- struct llama_past cache;
3952+ struct llama_kv_cache cache;
39533953 struct llama_control_vector cvec;
39543954
39553955 std::unordered_map<struct llama_lora_adapter *, float> lora_adapters;
@@ -4195,8 +4195,8 @@ static size_t llama_get_device_memory(const llama_model & model, int device) {
41954195// kv and rs cache helpers
41964196//
41974197
4198- static bool llama_past_init (
4199- struct llama_past & cache,
4198+ static bool llama_kv_cache_init (
4199+ struct llama_kv_cache & cache,
42004200 const llama_context * ctx,
42014201 ggml_type type_k,
42024202 ggml_type type_v,
@@ -4300,11 +4300,11 @@ static bool llama_past_init(
43004300 // no buffer was needed, so this is fine
43014301 return true;
43024302 }
4303- LLAMA_LOG_ERROR("%s: failed to allocate buffer for past cache\n", __func__);
4303+ LLAMA_LOG_ERROR("%s: failed to allocate buffer for kv cache\n", __func__);
43044304 return false;
43054305 }
43064306 ggml_backend_buffer_clear(buf, 0);
4307- LLAMA_LOG_INFO("%s: %10s past cache size = %8.2f MiB\n", __func__, ggml_backend_buffer_name(buf), ggml_backend_buffer_get_size(buf)/1024.0/1024.0);
4307+ LLAMA_LOG_INFO("%s: %10s KV buffer size = %8.2f MiB\n", __func__, ggml_backend_buffer_name(buf), ggml_backend_buffer_get_size(buf)/1024.0/1024.0);
43084308 cache.bufs.push_back(buf);
43094309 }
43104310
@@ -4315,9 +4315,9 @@ static bool llama_past_init(
43154315// updates the cache head
43164316// Note: On success, it's important that cache.head points
43174317// to the first cell of the slot.
4318- static bool llama_past_find_slot (
4319- struct llama_past & cache,
4320- const struct llama_ubatch & batch) {
4318+ static bool llama_kv_cache_find_slot (
4319+ struct llama_kv_cache & cache,
4320+ const struct llama_ubatch & batch) {
43214321 const uint32_t kv_size = cache.kv.size;
43224322 const uint32_t rs_size = cache.rs.size;
43234323 const uint32_t n_tokens = batch.n_tokens;
@@ -4563,7 +4563,7 @@ static bool llama_past_find_slot(
45634563}
45644564
45654565// find how many KV cells are currently in use
4566- static uint32_t llama_kv_cache_cell_max(const struct llama_kv_cache & cache) {
4566+ static uint32_t llama_kv_cache_cell_max(const struct llama_kv_self_cache & cache) {
45674567 for (uint32_t i = cache.size; i > 0; --i) {
45684568 const llama_kv_cell & cell = cache.cells[i - 1];
45694569
@@ -4576,7 +4576,7 @@ static uint32_t llama_kv_cache_cell_max(const struct llama_kv_cache & cache) {
45764576}
45774577
45784578// find how many recurrent state cells are currently in use
4579- static uint32_t llama_rs_cache_cell_max(const struct llama_rs_cache & cache) {
4579+ static uint32_t llama_rs_cache_cell_max(const struct llama_rs_self_cache & cache) {
45804580 for (uint32_t i = cache.size; i > 0; --i) {
45814581 const llama_rs_cell & cell = cache.cells[i - 1];
45824582
@@ -4588,7 +4588,7 @@ static uint32_t llama_rs_cache_cell_max(const struct llama_rs_cache & cache) {
45884588 return 0;
45894589}
45904590
4591- static void llama_past_clear(struct llama_past & cache) {
4591+ static void llama_past_clear(struct llama_kv_cache & cache) {
45924592 if (cache.kv.size > 0) {
45934593 for (uint32_t i = 0; i < cache.kv.size; ++i) {
45944594 llama_kv_cell & kv_cell = cache.kv.cells[i];
@@ -4623,7 +4623,7 @@ static void llama_past_clear(struct llama_past & cache) {
46234623}
46244624
46254625static llama_pos llama_past_seq_rm(
4626- struct llama_past & cache,
4626+ struct llama_kv_cache & cache,
46274627 llama_seq_id seq_id,
46284628 llama_pos p0,
46294629 llama_pos p1) {
@@ -4722,7 +4722,7 @@ static llama_pos llama_past_seq_rm(
47224722}
47234723
47244724static llama_pos llama_past_seq_cp(
4725- struct llama_past & cache,
4725+ struct llama_kv_cache & cache,
47264726 llama_seq_id seq_id_src,
47274727 llama_seq_id seq_id_dst,
47284728 llama_pos p0,
@@ -4786,7 +4786,7 @@ static llama_pos llama_past_seq_cp(
47864786 return n_past;
47874787}
47884788
4789- static void llama_past_seq_keep(struct llama_past & cache, llama_seq_id seq_id) {
4789+ static void llama_past_seq_keep(struct llama_kv_cache & cache, llama_seq_id seq_id) {
47904790 if (cache.rs.size > 0) {
47914791 uint32_t new_head = cache.rs.size;
47924792
@@ -4837,7 +4837,7 @@ static void llama_past_seq_keep(struct llama_past & cache, llama_seq_id seq_id)
48374837}
48384838
48394839static void llama_past_seq_add(
4840- struct llama_past & cache,
4840+ struct llama_kv_cache & cache,
48414841 llama_seq_id seq_id,
48424842 llama_pos p0,
48434843 llama_pos p1,
@@ -4905,7 +4905,7 @@ static void llama_past_seq_add(
49054905}
49064906
49074907static void llama_past_seq_div(
4908- struct llama_past & cache,
4908+ struct llama_kv_cache & cache,
49094909 llama_seq_id seq_id,
49104910 llama_pos p0,
49114911 llama_pos p1,
@@ -4945,7 +4945,7 @@ static void llama_past_seq_div(
49454945 }
49464946}
49474947
4948- static llama_pos llama_past_seq_pos_max(struct llama_past & cache, llama_seq_id seq_id) {
4948+ static llama_pos llama_past_seq_pos_max(struct llama_kv_cache & cache, llama_seq_id seq_id) {
49494949 llama_pos result = -1;
49504950
49514951 if (cache.rs.size > 0) {
@@ -4970,7 +4970,7 @@ static llama_pos llama_past_seq_pos_max(struct llama_past & cache, llama_seq_id
49704970 return result;
49714971}
49724972
4973- static void llama_kv_cache_defrag(struct llama_kv_cache & cache) {
4973+ static void llama_kv_cache_defrag(struct llama_kv_self_cache & cache) {
49744974 cache.do_defrag = true;
49754975}
49764976
@@ -9772,7 +9772,7 @@ static void llm_build_kv_store(
97729772 struct ggml_context * ctx,
97739773 const llama_hparams & hparams,
97749774 const llama_cparams & cparams,
9775- const llama_kv_cache & kv,
9775+ const llama_kv_self_cache & kv,
97769776 struct ggml_cgraph * graph,
97779777 struct ggml_tensor * k_cur,
97789778 struct ggml_tensor * v_cur,
@@ -10129,7 +10129,7 @@ static struct ggml_tensor * llm_build_moe_ffn(
1012910129static struct ggml_tensor * llm_build_kqv(
1013010130 struct ggml_context * ctx,
1013110131 struct llama_context & lctx,
10132- const llama_kv_cache & kv,
10132+ const llama_kv_self_cache & kv,
1013310133 struct ggml_cgraph * graph,
1013410134 struct ggml_tensor * wo,
1013510135 struct ggml_tensor * wo_b,
@@ -10260,7 +10260,7 @@ static struct ggml_tensor * llm_build_kqv(
1026010260static struct ggml_tensor * llm_build_kv(
1026110261 struct ggml_context * ctx,
1026210262 struct llama_context & lctx,
10263- const llama_kv_cache & kv,
10263+ const llama_kv_self_cache & kv,
1026410264 struct ggml_cgraph * graph,
1026510265 struct ggml_tensor * wo,
1026610266 struct ggml_tensor * wo_b,
@@ -10344,7 +10344,7 @@ static struct ggml_tensor * llm_build_mamba(
1034410344 int il) {
1034510345 const llama_model & model = lctx.model;
1034610346 const llama_hparams & hparams = model.hparams;
10347- const llama_rs_cache & rs = lctx.cache.rs;
10347+ const llama_rs_self_cache & rs = lctx.cache.rs;
1034810348 const int64_t d_conv = hparams.ssm_d_conv;
1034910349 const int64_t d_inner = hparams.ssm_d_inner;
1035010350 const int64_t d_state = hparams.ssm_d_state;
@@ -10661,8 +10661,8 @@ struct llm_build_context {
1066110661 const llama_hparams & hparams;
1066210662 const llama_cparams & cparams;
1066310663 const llama_ubatch & batch;
10664- const llama_kv_cache & kv_self;
10665- const llama_rs_cache & rs_self;
10664+ const llama_kv_self_cache & kv_self;
10665+ const llama_rs_self_cache & rs_self;
1066610666
1066710667 const int64_t n_embd;
1066810668 const int64_t n_layer;
@@ -17367,17 +17367,11 @@ static int llama_decode_internal(
1736717367 if (hparams.causal_attn) {
1736817368 llama_kv_cache_update(&lctx);
1736917369
17370- // if we have enough unused cells before the current head ->
17371- // better to start searching from the beginning of the cache, hoping to fill it
17372- if (kv_self.head > kv_self.used + 2*n_tokens) {
17373- kv_self.head = 0;
17374- }
17375-
17376- if (!llama_past_find_slot(lctx.cache, ubatch)) {
17370+ if (!llama_kv_cache_find_slot(lctx.cache, ubatch)) {
1737717371 return 1;
1737817372 }
1737917373
17380- // TODO: move into llama_past_find_slot
17374+ // TODO: move into llama_kv_cache_find_slot
1738117375 if (kv_self.size > 0) {
1738217376 // a heuristic, to avoid attending the full cache if it is not yet utilized
1738317377 // after enough generations, the benefit from this heuristic disappears
@@ -19557,7 +19551,7 @@ struct llama_context * llama_new_context_with_model(
1955719551 }
1955819552 ctx->backends.push_back(ctx->backend_cpu);
1955919553
19560- if (!llama_past_init (ctx->cache, ctx, type_k, type_v, cparams.offload_kqv)) {
19554+ if (!llama_kv_cache_init (ctx->cache, ctx, type_k, type_v, cparams.offload_kqv)) {
1956119555 LLAMA_LOG_ERROR("%s: llama_kv_cache_init() failed for self-attention cache\n", __func__);
1956219556 llama_free(ctx);
1956319557 return nullptr;
@@ -19575,7 +19569,7 @@ struct llama_context * llama_new_context_with_model(
1957519569 memory_size_s += ggml_nbytes(s);
1957619570 }
1957719571
19578- LLAMA_LOG_INFO("%s: SSM state size = %8.2f MiB, R (%s): %7.2f MiB, S (%s): %7.2f MiB\n", __func__,
19572+ LLAMA_LOG_INFO("%s: RS self size = %8.2f MiB, R (%s): %7.2f MiB, S (%s): %7.2f MiB\n", __func__,
1957919573 (float)(memory_size_r + memory_size_s) / (1024.0f * 1024.0f),
1958019574 ggml_type_name(GGML_TYPE_F32), (float)memory_size_r / (1024.0f * 1024.0f),
1958119575 ggml_type_name(GGML_TYPE_F32), (float)memory_size_s / (1024.0f * 1024.0f));
@@ -19592,7 +19586,7 @@ struct llama_context * llama_new_context_with_model(
1959219586 memory_size_v += ggml_nbytes(v);
1959319587 }
1959419588
19595- LLAMA_LOG_INFO("%s: KV cache size = %8.2f MiB, K (%s): %7.2f MiB, V (%s): %7.2f MiB\n", __func__,
19589+ LLAMA_LOG_INFO("%s: KV self size = %8.2f MiB, K (%s): %7.2f MiB, V (%s): %7.2f MiB\n", __func__,
1959619590 (float)(memory_size_k + memory_size_v) / (1024.0f * 1024.0f),
1959719591 ggml_type_name(type_k), (float)memory_size_k / (1024.0f * 1024.0f),
1959819592 ggml_type_name(type_v), (float)memory_size_v / (1024.0f * 1024.0f));
@@ -20052,7 +20046,7 @@ void llama_kv_cache_view_free(struct llama_kv_cache_view * view) {
2005220046}
2005320047
2005420048void llama_kv_cache_view_update(const struct llama_context * ctx, struct llama_kv_cache_view * view) {
20055- const llama_kv_cache & kv_self = ctx->cache.kv;
20049+ const llama_kv_self_cache & kv_self = ctx->cache.kv;
2005620050 if (uint32_t(view->n_cells) < kv_self.size || view->cells == nullptr) {
2005720051 view->n_cells = int32_t(kv_self.size);
2005820052 void * p = realloc(view->cells, sizeof(struct llama_kv_cache_view_cell) * view->n_cells);
@@ -20333,7 +20327,7 @@ struct llama_data_write {
2033320327 }
2033420328 }
2033520329
20336- void write_kv_cache_meta(const llama_kv_cache & kv_self, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id = -1) {
20330+ void write_kv_cache_meta(const llama_kv_self_cache & kv_self, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id = -1) {
2033720331
2033820332 for (const auto & range : cell_ranges) {
2033920333 for (uint32_t i = range.first; i < range.second; ++i) {
@@ -20353,7 +20347,7 @@ struct llama_data_write {
2035320347 }
2035420348 }
2035520349
20356- void write_rs_cache_meta(const llama_rs_cache & rs_self, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id = -1) {
20350+ void write_rs_cache_meta(const llama_rs_self_cache & rs_self, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges, llama_seq_id seq_id = -1) {
2035720351
2035820352 for (const auto & range : cell_ranges) {
2035920353 for (uint32_t i = range.first; i < range.second; ++i) {
@@ -20374,7 +20368,7 @@ struct llama_data_write {
2037420368 }
2037520369
2037620370 void write_kv_cache_data(const struct llama_context * ctx, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) {
20377- const struct llama_kv_cache & kv_self = ctx->cache.kv;
20371+ const struct llama_kv_self_cache & kv_self = ctx->cache.kv;
2037820372 const struct llama_hparams & hparams = ctx->model.hparams;
2037920373
2038020374 const uint32_t v_trans = kv_self.v_trans ? 1 : 0;
@@ -20455,7 +20449,7 @@ struct llama_data_write {
2045520449 }
2045620450
2045720451 void write_rs_cache_data(const struct llama_context * ctx, const std::vector<std::pair<uint32_t, uint32_t>> & cell_ranges) {
20458- const struct llama_rs_cache & rs_self = ctx->cache.rs;
20452+ const struct llama_rs_self_cache & rs_self = ctx->cache.rs;
2045920453 const struct llama_hparams & hparams = ctx->model.hparams;
2046020454
2046120455 const uint32_t n_layer = hparams.n_layer;
@@ -20503,8 +20497,8 @@ struct llama_data_write {
2050320497 }
2050420498
2050520499 void write_cache(const struct llama_context * ctx, llama_seq_id seq_id = -1) {
20506- const struct llama_kv_cache & kv_self = ctx->cache.kv;
20507- const struct llama_rs_cache & rs_self = ctx->cache.rs;
20500+ const struct llama_kv_self_cache & kv_self = ctx->cache.kv;
20501+ const struct llama_rs_self_cache & rs_self = ctx->cache.rs;
2050820502 std::vector<std::pair<uint32_t, uint32_t>> kv_cell_ranges; // ranges, from inclusive, to exclusive
2050920503 std::vector<std::pair<uint32_t, uint32_t>> rs_cell_ranges; // ranges, from inclusive, to exclusive
2051020504 uint32_t kv_cell_count = 0;
@@ -20692,8 +20686,8 @@ struct llama_data_read {
2069220686
2069320687 bool read_kv_cache_meta(struct llama_context * ctx, uint32_t cell_count) {
2069420688 if (cell_count == 0) { return true; }
20695- struct llama_past & cache = ctx->cache;
20696- struct llama_kv_cache & kv_self = cache.kv;
20689+ struct llama_kv_cache & cache = ctx->cache;
20690+ struct llama_kv_self_cache & kv_self = cache.kv;
2069720691
2069820692 // whole KV cache restore
2069920693
@@ -20734,8 +20728,8 @@ struct llama_data_read {
2073420728
2073520729 bool read_rs_cache_meta(struct llama_context * ctx, uint32_t cell_count) {
2073620730 if (cell_count == 0) { return true; }
20737- struct llama_past & cache = ctx->cache;
20738- struct llama_rs_cache & rs_self = cache.rs;
20731+ struct llama_kv_cache & cache = ctx->cache;
20732+ struct llama_rs_self_cache & rs_self = cache.rs;
2073920733
2074020734 // whole RS cache restore
2074120735
@@ -20781,7 +20775,7 @@ struct llama_data_read {
2078120775 bool read_kv_cache_data(struct llama_context * ctx, uint32_t cell_count) {
2078220776 if (cell_count == 0) { return true; }
2078320777 const struct llama_hparams & hparams = ctx->model.hparams;
20784- struct llama_kv_cache & kv_self = ctx->cache.kv;
20778+ struct llama_kv_self_cache & kv_self = ctx->cache.kv;
2078520779 uint32_t v_trans;
2078620780 uint32_t n_layer;
2078720781 read_to(&v_trans, sizeof(v_trans));
@@ -20895,7 +20889,7 @@ struct llama_data_read {
2089520889 bool read_rs_cache_data(struct llama_context * ctx, uint32_t cell_count) {
2089620890 if (cell_count == 0) { return true; }
2089720891 const struct llama_hparams & hparams = ctx->model.hparams;
20898- struct llama_rs_cache & rs_self = ctx->cache.rs;
20892+ struct llama_rs_self_cache & rs_self = ctx->cache.rs;
2089920893 uint32_t n_layer;
2090020894 read_to(&n_layer, sizeof(n_layer));
2090120895
@@ -20970,7 +20964,7 @@ struct llama_data_read {
2097020964
2097120965 // single sequence
2097220966
20973- llama_past & cache = ctx->cache;
20967+ llama_kv_cache & cache = ctx->cache;
2097420968 llama_ubatch batch = ctx->sbatch.reserve_ubatch(cell_count, /* has_embd */ false);
2097520969 batch.n_tokens = cell_count;
2097620970 batch.n_seq_tokens = cell_count;
@@ -20992,7 +20986,7 @@ struct llama_data_read {
2099220986 }
2099320987 batch.n_seq_id[0] = 1;
2099420988 batch.seq_id[0] = &seq_id;
20995- if (!llama_past_find_slot (cache, batch)) {
20989+ if (!llama_kv_cache_find_slot (cache, batch)) {
2099620990 LLAMA_LOG_ERROR("%s: failed to find available cells in kv cache\n", __func__);
2099720991 return false;
2099820992 }
0 commit comments