mirror of
https://github.com/ggml-org/llama.cpp.git
synced 2026-11-29 03:25:41 +00:00
llama : fix unexpected graph reallocation in the k-pool models
Both k-pool models built a graph shape that depends on state the
full-context reserve cannot know:
- qwen4exp branched on inp->cache_safe, which turns false as soon as
llama_memory_seq_cp shares cells (e.g. batched-bench -pps): the QSA
layers swapped scatter+gather for fill+concat and dropped the
new_pool_rep leaf, so the decode graph had 12 fewer nodes than the
reserved one
- glm5-next branched on gather = n_tokens <= 16 && n_kv > n_sel, so the
TG decode built the gather shape (7564 nodes) while the last reserve,
the PP one, had the dense shape (7762 nodes)
Either mismatch forces a decode-time re-reserve that drops the
worst-case sizing and bakes in the current state, so the next state
growth (n_pool, n_kv, n_new) needs more room at an unchanged graph size
and aborts under GGML_SCHED_DEBUG_REALLOC=1. Reproduce with, e.g.:
GGML_SCHED_DEBUG_REALLOC=1 ./bin/llama-batched-bench \
-hf ggml-org/GLM-5.3-Flash-GGUF:Q2_K -npp 2500 -ntg 32 -npl 1,2 \
-c 32768 -pps -kvu
Always scatter+gather the pooled keys, and pick gather from context
constants only: n_ubatch bounds every ubatch, top_k + kpool - 1 bounds
n_sel. Every graph of a context then shares one shape, which the
reserve covers, and the dense path measured faster than the gather path
at 2.5k and 16k context.
Assisted-by: pi:llama.cpp/MiMo-V2.6-Flash-MOPD
This commit is contained in:
parent
dd266785c2
commit
2220411ec1
3 changed files with 28 additions and 47 deletions
|
|
@ -816,7 +816,8 @@ void llama_memory_hybrid_idx_context::set_input_kpool(ggml_tensor * pool_cells,
|
|||
GGML_ASSERT(pool_mask->ne[0] == (int64_t) n_pool && pool_mask->ne[1] == (int64_t) n_tokens);
|
||||
GGML_ASSERT(tail_idxs->ne[0] == (int64_t) kpool - 1 && tail_idxs->ne[1] == (int64_t) n_tokens);
|
||||
GGML_ASSERT(pool_idxs->ne[0] == (int64_t) kpool && pool_idxs->ne[1] == (int64_t) n_pool);
|
||||
GGML_ASSERT(st.cache_safe == (new_pool_rep != nullptr));
|
||||
// a safe cache must be written back; models that re-pool every ubatch pass it in both modes
|
||||
GGML_ASSERT(!st.cache_safe || new_pool_rep != nullptr);
|
||||
GGML_ASSERT(ggml_backend_buffer_is_host(new_pool_idxs->buffer));
|
||||
GGML_ASSERT(new_pool_idxs->ne[0] == (int64_t) kpool && new_pool_idxs->ne[1] == (int64_t) n_new_g);
|
||||
if (new_pool_rep != nullptr) {
|
||||
|
|
|
|||
|
|
@ -263,7 +263,6 @@ public:
|
|||
// The scatter mask shape follows n_kv.
|
||||
res &= n_kv == idx->get_n_kv();
|
||||
res &= n_new == std::max(mctx->get_n_kpool_new(), 1u);
|
||||
res &= cache_safe == mctx->get_kpool_cache_safe();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
@ -282,7 +281,6 @@ public:
|
|||
const uint32_t kpool;
|
||||
uint32_t n_new = 0;
|
||||
uint32_t n_sel = 0;
|
||||
bool cache_safe = true;
|
||||
bool gather = false;
|
||||
uint32_t n_kv = 0;
|
||||
};
|
||||
|
|
@ -296,8 +294,7 @@ llama_model_glm5_next::llm_graph_input_kpool * llama_model_glm5_next::graph::bui
|
|||
const uint32_t n_kv = mctx_idx->get_n_kv();
|
||||
// a ubatch that completes no pool still builds one dummy entry, so the graph does not
|
||||
// change shape every kpool tokens
|
||||
const uint32_t n_new = std::max(mctx_hyb->get_n_kpool_new(), 1u);
|
||||
const bool cache_safe = mctx_hyb->get_kpool_cache_safe();
|
||||
const uint32_t n_new = std::max(mctx_hyb->get_n_kpool_new(), 1u);
|
||||
|
||||
// the fused lightning indexer wants an f16 mask
|
||||
const auto type_mask = cparams.fused_lid ? GGML_TYPE_F16 : GGML_TYPE_F32;
|
||||
|
|
@ -321,14 +318,16 @@ llama_model_glm5_next::llm_graph_input_kpool * llama_model_glm5_next::graph::bui
|
|||
|
||||
inp->n_kv = n_kv;
|
||||
|
||||
// Gather selected latents for small decode batches when n_kv exceeds n_sel.
|
||||
// Gather selected latents for small batches when the context exceeds the selection width.
|
||||
{
|
||||
constexpr int64_t max_ub = 16;
|
||||
|
||||
const int64_t n_top_pool = std::min<int64_t>(n_pool, hparams.indexer_top_k / kpool);
|
||||
const int64_t n_sel = kpool*n_top_pool + (hparams.indexer_kpool_select_tail ? kpool - 1 : 0);
|
||||
inp->n_sel = (uint32_t) n_sel;
|
||||
inp->gather = (int64_t) n_tokens <= max_ub && (int64_t) n_kv > n_sel;
|
||||
// both terms are context constants: n_ubatch bounds every ubatch and top_k + kpool - 1 bounds
|
||||
// n_sel, so the graph shape follows neither n_tokens nor n_kv, which the reserve cannot predict
|
||||
inp->gather = (int64_t) cparams.n_ubatch <= max_ub && (int64_t) cparams.n_ctx > hparams.indexer_top_k + kpool - 1;
|
||||
|
||||
// Both paths read the slot mask: gather adds it to the scores, scatter maps its dead slots to dump rows.
|
||||
inp->gather_mask = ggml_new_tensor_4d(ctx0, GGML_TYPE_F32, n_sel, 1, 1, n_tokens);
|
||||
|
|
@ -338,14 +337,13 @@ llama_model_glm5_next::llm_graph_input_kpool * llama_model_glm5_next::graph::bui
|
|||
}
|
||||
|
||||
inp->n_new = n_new;
|
||||
inp->cache_safe = cache_safe;
|
||||
|
||||
inp->new_pool_idxs = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, kpool, n_new);
|
||||
ggml_set_input(inp->new_pool_idxs);
|
||||
if (cache_safe) {
|
||||
inp->new_pool_rep = ggml_new_tensor_1d(ctx0, GGML_TYPE_I64, n_new);
|
||||
ggml_set_input(inp->new_pool_rep);
|
||||
}
|
||||
// the scatter target is part of the graph shape: llama_context reserves the full-context graph,
|
||||
// so this must not depend on cache_safe, which only the decode-time graph can know
|
||||
inp->new_pool_rep = ggml_new_tensor_1d(ctx0, GGML_TYPE_I64, n_new);
|
||||
ggml_set_input(inp->new_pool_rep);
|
||||
|
||||
return (llm_graph_input_kpool *) res->add_input(std::move(inp));
|
||||
}
|
||||
|
|
@ -814,20 +812,11 @@ ggml_tensor * llama_model_glm5_next::graph::build_kpool_select(
|
|||
pooled_new = ggml_reshape_2d(ctx0, pooled_new, n_embd_indexer, n_new);
|
||||
cb(pooled_new, "indexer_pool_k_new", il);
|
||||
|
||||
if (inp_kpool->cache_safe) {
|
||||
// Write before the pool gather.
|
||||
ggml_build_forward_expand(gf, kpool_cache.scatter_pooled(pooled_new, inp_kpool->new_pool_rep));
|
||||
}
|
||||
|
||||
ggml_tensor * pooled = nullptr;
|
||||
if (inp_kpool->cache_safe) {
|
||||
pooled = kpool_cache.gather_pooled(inp_kpool->pool_cells);
|
||||
} else {
|
||||
GGML_ASSERT(n_new <= n_pool);
|
||||
ggml_tensor * pad = ggml_fill(ctx0,
|
||||
ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd_indexer, n_pool - n_new), 0.0f);
|
||||
pooled = ggml_concat(ctx0, pooled_new, pad, 1);
|
||||
}
|
||||
// scatter the fresh pooled keys, then gather all n_pool of them by cell, in both cache modes:
|
||||
// the reserved graph cannot branch on cache_safe, and without sharing every pool is re-pooled
|
||||
// anyway (n_new == n_pool_real, layout order), so the gather returns exactly pooled_new
|
||||
ggml_build_forward_expand(gf, kpool_cache.scatter_pooled(pooled_new, inp_kpool->new_pool_rep));
|
||||
ggml_tensor * pooled = kpool_cache.gather_pooled(inp_kpool->pool_cells);
|
||||
pooled = ggml_reshape_3d(ctx0, pooled, n_embd_indexer, 1, n_pool);
|
||||
cb(pooled, "indexer_pool_k", il);
|
||||
|
||||
|
|
|
|||
|
|
@ -674,7 +674,6 @@ public:
|
|||
// the scatter mask shape follows n_kv
|
||||
res &= n_kv == idx->get_n_kv();
|
||||
res &= n_new == mctx->get_n_kpool_new();
|
||||
res &= cache_safe == mctx->get_kpool_cache_safe();
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
@ -693,7 +692,6 @@ public:
|
|||
uint32_t n_new = 0; // padded to a stable bound, never below 1
|
||||
uint32_t n_sel = 0;
|
||||
uint32_t n_kv = 0;
|
||||
bool cache_safe = true;
|
||||
};
|
||||
|
||||
llama_model_qwen4exp::llm_graph_input_kpool * llama_model_qwen4exp::graph::build_inp_kpool(const llama_memory_hybrid_idx_context * mctx_hyb) {
|
||||
|
|
@ -721,18 +719,17 @@ llama_model_qwen4exp::llm_graph_input_kpool * llama_model_qwen4exp::graph::build
|
|||
ggml_build_forward_expand(gf, inp->pool_mask);
|
||||
ggml_build_forward_expand(gf, inp->tail_idxs);
|
||||
|
||||
inp->n_kv = mctx_idx->get_n_kv();
|
||||
inp->n_new = mctx_hyb->get_n_kpool_new();
|
||||
inp->cache_safe = mctx_hyb->get_kpool_cache_safe();
|
||||
inp->n_kv = mctx_idx->get_n_kv();
|
||||
inp->n_new = mctx_hyb->get_n_kpool_new();
|
||||
// the top blocks plus the tail
|
||||
inp->n_sel = kpool*std::min<uint32_t>(n_pool, hparams.indexer_top_k / kpool) + kpool - 1;
|
||||
inp->n_sel = kpool*std::min<uint32_t>(n_pool, hparams.indexer_top_k / kpool) + kpool - 1;
|
||||
|
||||
inp->new_pool_idxs = ggml_new_tensor_2d(ctx0, GGML_TYPE_I32, kpool, inp->n_new);
|
||||
ggml_set_input(inp->new_pool_idxs);
|
||||
if (inp->cache_safe) {
|
||||
inp->new_pool_rep = ggml_new_tensor_1d(ctx0, GGML_TYPE_I64, inp->n_new);
|
||||
ggml_set_input(inp->new_pool_rep);
|
||||
}
|
||||
// the scatter target is part of the graph shape: llama_context reserves the full-context graph,
|
||||
// so this must not depend on cache_safe, which only the decode-time graph can know
|
||||
inp->new_pool_rep = ggml_new_tensor_1d(ctx0, GGML_TYPE_I64, inp->n_new);
|
||||
ggml_set_input(inp->new_pool_rep);
|
||||
inp->new_pool_pos = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, 4*inp->n_new);
|
||||
ggml_set_input(inp->new_pool_pos);
|
||||
|
||||
|
|
@ -790,17 +787,11 @@ ggml_tensor * llama_model_qwen4exp::graph::build_qsa_sel(
|
|||
pooled_new = ggml_reshape_2d(ctx0, pooled_new, idx_dim, n_new);
|
||||
cb(pooled_new, "indexer_pool_k_new", il);
|
||||
|
||||
ggml_tensor * pooled = nullptr;
|
||||
if (inp_kpool->cache_safe) {
|
||||
// write before the pool gather
|
||||
ggml_build_forward_expand(gf, kpool_cache.scatter_pooled(pooled_new, inp_kpool->new_pool_rep));
|
||||
pooled = kpool_cache.gather_pooled(inp_kpool->pool_cells);
|
||||
} else {
|
||||
// shared cells re-pool every pool, in layout order
|
||||
GGML_ASSERT(n_new < n_pool);
|
||||
ggml_tensor * pad = ggml_fill(ctx0, ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, idx_dim, n_pool - n_new), 0.0f);
|
||||
pooled = ggml_concat(ctx0, pooled_new, pad, 1);
|
||||
}
|
||||
// scatter the fresh pooled keys, then gather all n_pool of them by cell, in both cache modes:
|
||||
// the reserved graph cannot branch on cache_safe, and without sharing every pool is re-pooled
|
||||
// anyway (n_new == n_pool_real, layout order), so the gather returns exactly pooled_new
|
||||
ggml_build_forward_expand(gf, kpool_cache.scatter_pooled(pooled_new, inp_kpool->new_pool_rep));
|
||||
ggml_tensor * pooled = kpool_cache.gather_pooled(inp_kpool->pool_cells);
|
||||
pooled = ggml_reshape_3d(ctx0, pooled, idx_dim, 1, n_pool);
|
||||
cb(pooled, "indexer_k", il);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue