Skip to content

Commit 4db34ac

Browse files
committed
refactor: Remove layer index from n_embd_k/v_s
Now that it's not used at all in the unified cache, we don't need to use the layer index to zero it out for attention layers. Branch: HybridRecurrentCache Signed-off-by: Gabe Goodhart <[email protected]>
1 parent f3ce1ac commit 4db34ac

File tree

4 files changed

+14
-20
lines changed

4 files changed

+14
-20
lines changed

src/llama-hparams.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ uint32_t llama_hparams::n_embd_v_gqa(uint32_t il) const {
6565
return n_embd_head_v * n_head_kv;
6666
}
6767

68-
uint32_t llama_hparams::n_embd_k_s(uint32_t il) const {
69-
if (!recurrent_layer(il)) {
70-
return 0;
71-
}
68+
uint32_t llama_hparams::n_embd_k_s() const {
7269
if (wkv_head_size != 0) {
7370
// for RWKV models
7471
return token_shift_count * n_embd;
@@ -79,10 +76,7 @@ uint32_t llama_hparams::n_embd_k_s(uint32_t il) const {
7976
return (ssm_d_conv > 0 ? ssm_d_conv - 1 : 0) * ssm_d_inner;
8077
}
8178

82-
uint32_t llama_hparams::n_embd_v_s(uint32_t il) const {
83-
if (!recurrent_layer(il)) {
84-
return 0;
85-
}
79+
uint32_t llama_hparams::n_embd_v_s() const {
8680
if (wkv_head_size != 0) {
8781
// corresponds to RWKV's wkv_states size
8882
return n_embd * wkv_head_size;

src/llama-hparams.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ struct llama_hparams {
184184

185185
// dimension of the rolling state embeddings
186186
// corresponds to Mamba's conv_states size or RWKV's token_shift states size
187-
uint32_t n_embd_k_s(uint32_t il = 0) const;
187+
uint32_t n_embd_k_s() const;
188188

189189
// dimension of the recurrent state embeddings
190-
uint32_t n_embd_v_s(uint32_t il = 0) const;
190+
uint32_t n_embd_v_s() const;
191191

192192
// whether or not the given layer is recurrent (for hybrid models)
193193
bool recurrent_layer(uint32_t il) const;

src/llama-kv-cache-recurrent.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ llama_kv_cache_recurrent::llama_kv_cache_recurrent(
6969
continue;
7070
}
7171

72-
const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i) + hparams.n_embd_k_s(i);
73-
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i) + hparams.n_embd_v_s(i);
72+
const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i) + hparams.n_embd_k_s();
73+
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i) + hparams.n_embd_v_s();
7474

7575
const char * dev_name = "CPU";
7676

@@ -754,7 +754,7 @@ void llama_kv_cache_recurrent::state_write_data(llama_io_write_i & io, const std
754754
// Iterate and write all the keys first, each row is a cell
755755
// Get whole range at a time
756756
for (uint32_t il = 0; il < n_layer; ++il) {
757-
const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s(il);
757+
const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s();
758758

759759
// Write key type
760760
const int32_t k_type_i = (int32_t)k_l[il]->type;
@@ -774,7 +774,7 @@ void llama_kv_cache_recurrent::state_write_data(llama_io_write_i & io, const std
774774

775775
if (!v_trans) {
776776
for (uint32_t il = 0; il < n_layer; ++il) {
777-
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s(il);
777+
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
778778

779779
// Write value type
780780
const int32_t v_type_i = (int32_t)v_l[il]->type;
@@ -795,7 +795,7 @@ void llama_kv_cache_recurrent::state_write_data(llama_io_write_i & io, const std
795795
// When v is transposed, we also need the element size and get the element ranges from each row
796796
const uint32_t kv_size = size;
797797
for (uint32_t il = 0; il < n_layer; ++il) {
798-
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s(il);
798+
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
799799

800800
// Write value type
801801
const int32_t v_type_i = (int32_t)v_l[il]->type;
@@ -942,7 +942,7 @@ bool llama_kv_cache_recurrent::state_read_data(llama_io_read_i & io, uint32_t ce
942942

943943
// For each layer, read the keys for each cell, one row is one cell, read as one contiguous block
944944
for (uint32_t il = 0; il < n_layer; ++il) {
945-
const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s(il);
945+
const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(il) + hparams.n_embd_k_s();
946946

947947
// Read type of key
948948
int32_t k_type_i_ref;
@@ -970,7 +970,7 @@ bool llama_kv_cache_recurrent::state_read_data(llama_io_read_i & io, uint32_t ce
970970

971971
if (!v_trans) {
972972
for (uint32_t il = 0; il < n_layer; ++il) {
973-
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s(il);
973+
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
974974

975975
// Read type of value
976976
int32_t v_type_i_ref;
@@ -998,7 +998,7 @@ bool llama_kv_cache_recurrent::state_read_data(llama_io_read_i & io, uint32_t ce
998998
} else {
999999
// For each layer, read the values for each cell (transposed)
10001000
for (uint32_t il = 0; il < n_layer; ++il) {
1001-
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s(il);
1001+
const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(il) + hparams.n_embd_v_s();
10021002

10031003
// Read type of value
10041004
int32_t v_type_i_ref;

src/llama-model.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9194,11 +9194,11 @@ struct llm_build_mamba : public llm_graph_context {
91949194
// (ab)using the KV cache to store the states
91959195
ggml_tensor * conv = build_recurrent_state(
91969196
gf, conv_states_all, state_copy,
9197-
hparams.n_embd_k_s(il), n_seqs);
9197+
hparams.n_embd_k_s(), n_seqs);
91989198
conv = ggml_reshape_3d(ctx0, conv, d_conv - 1, d_inner, n_seqs);
91999199
ggml_tensor * ssm = build_recurrent_state(
92009200
gf, ssm_states_all, state_copy,
9201-
hparams.n_embd_v_s(il), n_seqs);
9201+
hparams.n_embd_v_s(), n_seqs);
92029202
ssm = ggml_reshape_3d(ctx0, ssm, d_state, d_inner, n_seqs);
92039203

92049204
// {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}

0 commit comments

Comments
 (0)