Skip to content

Commit ef5bb61

Browse files
committed
llama : use n_swa + n_ubatch cells for SWA cache
ggml-ci
1 parent ca69f32 commit ef5bb61

File tree

5 files changed

+12
-11
lines changed

5 files changed

+12
-11
lines changed

include/llama.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ extern "C" {
502502
LLAMA_API int32_t llama_model_n_layer (const struct llama_model * model);
503503
LLAMA_API int32_t llama_model_n_head (const struct llama_model * model);
504504
LLAMA_API int32_t llama_model_n_head_kv (const struct llama_model * model);
505+
LLAMA_API int32_t llama_model_n_swa (const struct llama_model * model);
505506

506507
// Get the model's RoPE frequency scaling factor
507508
LLAMA_API float llama_model_rope_freq_scale_train(const struct llama_model * model);

src/llama-kv-cache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,14 +1731,14 @@ llama_kv_cache_unified_iswa::llama_kv_cache_unified_iswa(
17311731
bool swa_full,
17321732
uint32_t kv_size,
17331733
uint32_t n_seq_max,
1734-
uint32_t n_batch,
1734+
uint32_t n_ubatch,
17351735
uint32_t n_pad) : hparams(model.hparams) {
17361736
llama_kv_cache_unified::layer_filter_cb filter_base = [&](int32_t il) { return !model.hparams.is_swa(il); };
17371737
llama_kv_cache_unified::layer_filter_cb filter_swa = [&](int32_t il) { return model.hparams.is_swa(il); };
17381738

17391739
const uint32_t size_base = kv_size;
17401740

1741-
uint32_t size_swa = std::min(size_base, GGML_PAD(hparams.n_swa*n_seq_max + n_batch, n_pad));
1741+
uint32_t size_swa = std::min(size_base, GGML_PAD(hparams.n_swa*n_seq_max + n_ubatch, n_pad));
17421742

17431743
// when using full-size SWA cache, we set the SWA cache size to be equal to the base cache size
17441744
if (swa_full) {

src/llama-kv-cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ class llama_kv_cache_unified_iswa : public llama_kv_cache {
339339
bool swa_full,
340340
uint32_t kv_size,
341341
uint32_t n_seq_max,
342-
uint32_t n_batch,
342+
uint32_t n_ubatch,
343343
uint32_t n_pad);
344344

345345
~llama_kv_cache_unified_iswa() = default;

src/llama-model.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13228,7 +13228,7 @@ llama_memory_i * llama_model::create_memory(const llama_memory_params & params,
1322813228
params.swa_full,
1322913229
cparams.n_ctx,
1323013230
cparams.n_seq_max,
13231-
cparams.n_batch,
13231+
cparams.n_ubatch,
1323213232
padding);
1323313233
} else {
1323413234
GGML_ASSERT(!hparams.is_swa_any());
@@ -13591,6 +13591,10 @@ int32_t llama_model_n_head_kv(const llama_model * model) {
1359113591
return model->hparams.n_head_kv();
1359213592
}
1359313593

13594+
int32_t llama_model_n_swa(const llama_model * model) {
13595+
return model->hparams.n_swa;
13596+
}
13597+
1359413598
// deprecated
1359513599
int32_t llama_n_ctx_train(const llama_model * model) {
1359613600
return llama_model_n_ctx_train(model);

tools/server/server.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,11 +2019,6 @@ struct server_context {
20192019
params_base.n_cache_reuse = 0;
20202020
SRV_WRN("%s\n", "cache_reuse is not supported by this context, it will be disabled");
20212021
}
2022-
2023-
if (!params_base.speculative.model.path.empty()) {
2024-
SRV_ERR("%s\n", "err: speculative decode is not supported by this context");
2025-
return false;
2026-
}
20272022
}
20282023

20292024
return true;
@@ -3217,9 +3212,10 @@ struct server_context {
32173212
}
32183213

32193214
if (slot.n_past > 0 && slot.n_past < (int) slot.cache_tokens.size()) {
3215+
const auto n_swa = llama_model_n_swa(model);
32203216
const auto pos_min = llama_kv_self_seq_pos_min(ctx, slot.id);
3221-
if (pos_min > 0) {
3222-
SLT_WRN(slot, "n_past = %d, cache_tokens.size() = %d, seq_id = %d, pos_min = %d\n", slot.n_past, (int) slot.cache_tokens.size(), slot.id, pos_min);
3217+
if (pos_min == -1 || pos_min > slot.n_past - n_swa) {
3218+
SLT_WRN(slot, "n_past = %d, cache_tokens.size() = %d, seq_id = %d, pos_min = %d, n_swa = %d\n", slot.n_past, (int) slot.cache_tokens.size(), slot.id, pos_min, n_swa);
32233219
SLT_WRN(slot, "forcing full prompt re-processing due to lack of cache data (likely due to SWA, see %s)\n",
32243220
"https://github.com/ggml-org/llama.cpp/pull/13194#issuecomment-2868343055");
32253221
slot.n_past = 0;

0 commit comments

Comments
 (0)