Skip to content

Commit e87eca1

Browse files
committed
arch : add LLM_KV_ATTENTION_RECURRENT_LAYERS
1 parent e0e3909 commit e87eca1

21 files changed

Lines changed: 51 additions & 77 deletions

src/llama-arch.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ static const std::map<llm_kv, const char *> LLM_KV_NAMES = {
245245
{ LLM_KV_ATTENTION_INDEXER_KEY_LENGTH, "%s.attention.indexer.key_length" },
246246
{ LLM_KV_ATTENTION_INDEXER_TOP_K, "%s.attention.indexer.top_k" },
247247
{ LLM_KV_ATTENTION_SHARED_KV_LAYERS, "%s.attention.shared_kv_layers" },
248+
{ LLM_KV_ATTENTION_RECURRENT_LAYERS, "%s.attention.recurrent_layers" },
248249

249250
{ LLM_KV_ROPE_DIMENSION_COUNT, "%s.rope.dimension_count" },
250251
{ LLM_KV_ROPE_DIMENSION_COUNT_SWA, "%s.rope.dimension_count_swa" },

src/llama-arch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ enum llm_kv {
249249
LLM_KV_ATTENTION_INDEXER_KEY_LENGTH,
250250
LLM_KV_ATTENTION_INDEXER_TOP_K,
251251
LLM_KV_ATTENTION_SHARED_KV_LAYERS,
252+
LLM_KV_ATTENTION_RECURRENT_LAYERS,
252253

253254
LLM_KV_ROPE_DIMENSION_COUNT,
254255
LLM_KV_ROPE_DIMENSION_COUNT_SWA,

src/llama-hparams.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
void llama_hparams::set_swa_pattern(uint32_t n_pattern, bool dense_first) {
99
if (dense_first) {
1010
for (uint32_t il = 0; il < n_layer; ++il) {
11-
swa_layers[il] = n_pattern == 0 || (il % n_pattern != 0);
11+
is_swa_impl[il] = n_pattern == 0 || (il % n_pattern != 0);
1212
}
1313
} else {
1414
for (uint32_t il = 0; il < n_layer; ++il) {
15-
swa_layers[il] = n_pattern == 0 || (il % n_pattern < (n_pattern - 1));
15+
is_swa_impl[il] = n_pattern == 0 || (il % n_pattern < (n_pattern - 1));
1616
}
1717
}
1818
}
1919

2020
bool llama_hparams::is_swa_any() const {
2121
for (uint32_t il = 0; il < n_layer; ++il) {
22-
if (swa_layers[il]) {
22+
if (is_swa_impl[il]) {
2323
return true;
2424
}
2525
}
@@ -195,7 +195,7 @@ uint32_t llama_hparams::n_embd_s() const {
195195

196196
bool llama_hparams::is_recurrent(uint32_t il) const {
197197
if (il < n_layer) {
198-
return recurrent_layer_arr[il];
198+
return is_recr_impl[il];
199199
}
200200

201201
GGML_ABORT("%s: il (%u) out of bounds (n_layer: %u)\n", __func__, il, n_layer);
@@ -207,7 +207,7 @@ uint32_t llama_hparams::n_pos_per_embd() const {
207207

208208
bool llama_hparams::is_swa(uint32_t il) const {
209209
if (il < n_layer) {
210-
return swa_layers[il];
210+
return is_swa_impl[il];
211211
}
212212

213213
GGML_ABORT("fatal error");

src/llama-hparams.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,11 @@ struct llama_hparams {
134134
llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE;
135135
// the size of the sliding window (0 - no SWA)
136136
uint32_t n_swa = 0;
137-
// if swa_layers[il] == 1, then layer il is SWA
138-
// if swa_layers[il] == 0, then layer il is dense (i.e. non-SWA)
137+
// if is_swa_impl[il] == 1, then layer il is SWA
138+
// if is_swa_impl[il] == 0, then layer il is dense (i.e. non-SWA)
139139
// by default, all layers are dense
140140
// note: using uint32_t type for compatibility reason
141-
std::array<uint32_t, LLAMA_MAX_LAYERS> swa_layers;
141+
std::array<uint32_t, LLAMA_MAX_LAYERS> is_swa_impl;
142142

143143
// for State Space Models
144144
uint32_t ssm_d_conv = 0;
@@ -151,7 +151,7 @@ struct llama_hparams {
151151
uint32_t n_embd_head_kda = 0;
152152

153153
// for hybrid state space models
154-
std::array<bool, LLAMA_MAX_LAYERS> recurrent_layer_arr;
154+
std::array<uint32_t, LLAMA_MAX_LAYERS> is_recr_impl;
155155

156156
bool ssm_dt_b_c_rms = false;
157157

src/llama-model-loader.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ namespace GGUFMeta {
146146
const enum gguf_type arr_type = gguf_get_arr_type(ctx, k);
147147
return ArrayInfo {
148148
arr_type,
149-
size_t(gguf_get_arr_n(ctx, k)),
149+
gguf_get_arr_n(ctx, k),
150150
arr_type == GGUF_TYPE_STRING ? nullptr : gguf_get_arr_data(ctx, k),
151151
};
152152
}
@@ -353,7 +353,6 @@ namespace GGUFMeta {
353353

354354
switch (arr_info.gt) {
355355
case GGUF_TYPE_BOOL:
356-
GGML_ASSERT((std::is_same<T, bool>::value)); break;
357356
case GGUF_TYPE_UINT32:
358357
case GGUF_TYPE_INT32: GGML_ASSERT((std::is_same<T, int32_t>::value) ||
359358
(std::is_same<T, uint32_t>::value)); break;
@@ -446,7 +445,7 @@ namespace GGUFMeta {
446445
}
447446

448447
if (n > N_MAX) {
449-
throw std::runtime_error(format("n > N_MAX: %u > %u for key %s", (uint32_t) n, (uint32_t) N_MAX, key.c_str()));
448+
throw std::runtime_error(format("n > N_MAX: %u > %u for key %s", n, (uint32_t) N_MAX, key.c_str()));
450449
}
451450

452451
if (gguf_get_kv_type(metadata, kid) == GGUF_TYPE_ARRAY) {
@@ -505,7 +504,6 @@ namespace GGUFMeta {
505504
// TODO: this is not very clever - figure out something better
506505
template bool llama_model_loader::get_key_or_arr<std::array<int, 4>> (enum llm_kv kid, std::array<int, 4> & result, uint32_t n, bool required);
507506
template bool llama_model_loader::get_key_or_arr<std::array<uint32_t, 512>>(enum llm_kv kid, std::array<uint32_t, 512> & result, uint32_t n, bool required);
508-
template bool llama_model_loader::get_key_or_arr<std::array<bool, 512>>(enum llm_kv kid, std::array<bool, 512> & result, uint32_t n, bool required);
509507
template bool llama_model_loader::get_key_or_arr<std::array<float, 512>>(enum llm_kv kid, std::array<float, 512> & result, uint32_t n, bool required);
510508

511509

src/llama-model-saver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ void llama_model_saver::add_kv_from_model() {
243243
add_kv(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale);
244244
add_kv(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count);
245245
add_kv(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step);
246-
add_kv(LLM_KV_FULL_ATTENTION_INTERVAL, hparams.recurrent_layer_arr, true);
247246

248247
add_kv(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, true);
249248
add_kv(LLM_KV_ATTENTION_HEAD_COUNT_KV, hparams.n_head_kv_arr, true);
@@ -277,6 +276,7 @@ void llama_model_saver::add_kv_from_model() {
277276
add_kv(LLM_KV_ATTENTION_INDEXER_HEAD_COUNT, hparams.indexer_n_head);
278277
add_kv(LLM_KV_ATTENTION_INDEXER_KEY_LENGTH, hparams.indexer_head_size);
279278
add_kv(LLM_KV_ATTENTION_INDEXER_TOP_K, hparams.indexer_top_k);
279+
add_kv(LLM_KV_ATTENTION_RECURRENT_LAYERS, hparams.is_recr_impl, true);
280280

281281
const float rope_scaling_factor = hparams.rope_freq_scale_train == 1.0f ? 0.0f : 1.0f/hparams.rope_freq_scale_train;
282282

src/llama-model.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,18 +1051,16 @@ void llama_model_base::load_hparams(llama_model_loader & ml) {
10511051
std::fill(hparams.n_head_arr.begin(), hparams.n_head_arr.end(), 0);
10521052
std::fill(hparams.n_head_kv_arr.begin(), hparams.n_head_kv_arr.end(), 0);
10531053
std::fill(hparams.n_ff_arr.begin(), hparams.n_ff_arr.end(), 0);
1054-
std::fill(
1055-
hparams.recurrent_layer_arr.begin(),
1056-
hparams.recurrent_layer_arr.end(),
1057-
llm_arch_is_recurrent(ml.get_arch()));
10581054

10591055
std::fill(hparams.rope_sections.begin(), hparams.rope_sections.end(), 0);
1060-
std::fill(hparams.swa_layers.begin(), hparams.swa_layers.end(), 0);
1056+
std::fill(hparams.is_swa_impl.begin(), hparams.is_swa_impl.end(), 0);
1057+
std::fill(hparams.is_recr_impl.begin(), hparams.is_recr_impl.end(), llm_arch_is_recurrent(ml.get_arch()) ? 1 : 0);
10611058

10621059
std::fill(hparams.xielu_alpha_n.begin(), hparams.xielu_alpha_n.end(), 0.0f);
10631060
std::fill(hparams.xielu_alpha_p.begin(), hparams.xielu_alpha_p.end(), 0.0f);
1064-
std::fill(hparams.xielu_beta.begin(), hparams.xielu_beta.end(), 0.0f);
1065-
std::fill(hparams.xielu_eps.begin(), hparams.xielu_eps.end(), 0.0f);
1061+
std::fill(hparams.xielu_beta.begin(), hparams.xielu_beta.end(), 0.0f);
1062+
std::fill(hparams.xielu_eps.begin(), hparams.xielu_eps.end(), 0.0f);
1063+
10661064
std::fill(hparams.swiglu_clamp_exp.begin(), hparams.swiglu_clamp_exp.end(), 0.0f);
10671065
std::fill(hparams.swiglu_clamp_shexp.begin(), hparams.swiglu_clamp_shexp.end(), 0.0f);
10681066

src/models/falcon-h1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void llama_model_falcon_h1::load_arch_hparams(llama_model_loader & ml) {
1111
ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
1212
ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group);
1313

14-
std::fill(hparams.recurrent_layer_arr.begin(), hparams.recurrent_layer_arr.end(), true);
14+
std::fill(hparams.is_recr_impl.begin(), hparams.is_recr_impl.end(), true);
1515

1616
switch (hparams.n_layer) {
1717
case 36:

src/models/gemma4.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
void llama_model_gemma4::load_arch_hparams(llama_model_loader & ml) {
44
hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
5-
ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.swa_layers, hparams.n_layer);
5+
ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl, hparams.n_layer);
66

77
uint32_t n_kv_shared_layers = 0;
88
ml.get_key(LLM_KV_ATTENTION_SHARED_KV_LAYERS, n_kv_shared_layers, false);

src/models/granite-hybrid.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void llama_model_granite_hybrid::load_arch_hparams(llama_model_loader & ml) {
2020

2121
// A layer is recurrent IFF the n_head_kv value is set to 0
2222
for (uint32_t i = 0; i < hparams.n_layer; ++i) {
23-
hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0;
23+
hparams.is_recr_impl[i] = hparams.n_head_kv(i) == 0;
2424
}
2525

2626
ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);

0 commit comments

Comments
 (0)