Skip to content

Commit 7d9dd68

Browse files
committed
feat: Add layer filter to recurrent cache
Branch: HybridCache Signed-off-by: Gabe Goodhart <ghart@us.ibm.com>
1 parent 9eb1cd8 commit 7d9dd68

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

src/llama-kv-cache-recurrent.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
//
1616

1717
llama_kv_cache_recurrent::llama_kv_cache_recurrent(
18-
const llama_model & model,
19-
ggml_type type_k,
20-
ggml_type type_v,
21-
bool offload,
22-
uint32_t kv_size,
23-
uint32_t n_seq_max) : hparams(model.hparams), n_seq_max(n_seq_max) {
18+
const llama_model & model,
19+
layer_filter_cb && filter,
20+
ggml_type type_k,
21+
ggml_type type_v,
22+
bool offload,
23+
uint32_t kv_size,
24+
uint32_t n_seq_max) : hparams(model.hparams), n_seq_max(n_seq_max) {
2425
const int32_t n_layer = hparams.n_layer;
2526

2627
LLAMA_LOG_INFO("%s: kv_size = %u, n_seq_max = %u, type_k = '%s', type_v = '%s', n_layer = %d\n",
@@ -62,6 +63,11 @@ llama_kv_cache_recurrent::llama_kv_cache_recurrent(
6263
v_l.reserve(n_layer);
6364

6465
for (int i = 0; i < n_layer; i++) {
66+
if (filter && !filter(i)) {
67+
LLAMA_LOG_DEBUG("%s: layer %3d: skipped\n", __func__, i);
68+
continue;
69+
}
70+
6571
const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i) + hparams.n_embd_k_s();
6672
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i) + hparams.n_embd_v_s();
6773

@@ -87,8 +93,8 @@ llama_kv_cache_recurrent::llama_kv_cache_recurrent(
8793
ggml_tensor * v = ggml_new_tensor_1d(ctx, type_v, n_embd_v_gqa*kv_size);
8894
ggml_format_name(k, "cache_k_l%d", i);
8995
ggml_format_name(v, "cache_v_l%d", i);
90-
k_l.push_back(k);
91-
v_l.push_back(v);
96+
k_l[i] = k;
97+
v_l[i] = v;
9298
}
9399

94100
// allocate tensors and initialize the buffers to avoid NaNs in the padding

src/llama-kv-cache-recurrent.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
class llama_kv_cache_recurrent : public llama_kv_cache {
1717
public:
1818
llama_kv_cache_recurrent(
19-
const llama_model & model,
20-
ggml_type type_k,
21-
ggml_type type_v,
22-
bool offload,
23-
uint32_t kv_size,
24-
uint32_t n_seq_max);
19+
const llama_model & model,
20+
layer_filter_cb && filter,
21+
ggml_type type_k,
22+
ggml_type type_v,
23+
bool offload,
24+
uint32_t kv_size,
25+
uint32_t n_seq_max);
2526

2627
~llama_kv_cache_recurrent() = default;
2728

src/llama-model.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13213,6 +13213,7 @@ llama_memory_i * llama_model::create_memory(const llama_memory_params & params,
1321313213
{
1321413214
res = new llama_kv_cache_recurrent(
1321513215
*this,
13216+
nullptr,
1321613217
GGML_TYPE_F32,
1321713218
GGML_TYPE_F32,
1321813219
cparams.offload_kqv,

0 commit comments

Comments
 (0)