* 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
* llama : drop the unused k-pool cache_safe graph API
The k-pool graphs no longer branch on cache_safe, so nothing reads
get_kpool_cache_safe() or the conditional new_pool_rep any more: both
models always pass the scatter target, which set_input_kpool now
requires instead of merely preferring.
Also drop the cache_safe copy in kpool_build_sizes(), a sizes-only
helper. The layout and state flag itself stays, it still decides which
pools a layout with shared cells must re-pool.
Assisted-by: pi:llama.cpp/MiMo-V2.6-Flash-MOPD
* tests : add a shared-seq graph reserve regression test
Decode a prompt into seq 0, share its cells with seq 1 via
llama_memory_seq_cp (what llama-batched-bench does for -pps), then keep
decoding both sequences. For the k-pool models sharing clears
cache_safe, which changes the graph topology while the pools keep
growing, so a scheduler that re-reserves with the current state
instead of the worst-case one aborts under GGML_SCHED_DEBUG_REALLOC=1.
The test registration sets that flag, and the test aborts on both
k-pool models before 2220411ec1.
kimi-linear and minimax-01 are skipped: they reserve the final pp graph
with n_seqs = 1 (see [TAG_RESERVE_DIAG_DECAY] in llama-context.cpp), so
every multi-seq graph has a different layout and re-reserves by design.
Assisted-by: pi:llama.cpp/MiMo-V2.6-Flash-MOPD
* cont : add TODOs
* cont : fix comment
* cuda: match the moe weighted reduction on empty ubatches
ggml_cuda_match_moe_weighted_reduction rejected tensors with zero
rows. A ubatch without outputs shrinks the last layer to zero rows
through inp_out_ids, so graph_optimize dropped its alloc dep there and
the scheduler graph lost one node compared to the reserved one. The
scheduler then re-reserved at the size of that ubatch, and the next
ubatch with the same node count but larger tensors aborted under
GGML_SCHED_DEBUG_REALLOC=1.
The compute loop already skips empty nodes before trying any fusion,
so the guard only made the alloc deps depend on the row count.
* tests: build the rollback test only where internal symbols link
The shared-seq case calls llm_arch_from_string, which libllama does
not export through LLAMA_API, so linking test-recurrent-state-rollback
fails on Windows with shared libraries. Its build now sits in the
NOT WIN32 OR NOT BUILD_SHARED_LIBS block, next to test-llama-archs and
the test registration it already lives under.
* tests: skip archs by name in the shared-seq reserve test
The skip of kimi-linear and minimax-01 went through llm_arch_from_string,
which libllama does not export through LLAMA_API, so the test could not
link on Windows with shared libraries. It now compares the
general.architecture string directly, and the test builds on every
platform again.
---------
Co-authored-by: Pascal <admin@serveurperso.com>
126 lines
4.4 KiB
C++
126 lines
4.4 KiB
C++
// Test for state restore with fragmented KV cache
|
|
// This tests the fix for: https://github.com/ggml-org/llama.cpp/issues/17527
|
|
// The issue was that state restore required contiguous KV cache slots,
|
|
// which fails when the cache is fragmented.
|
|
//
|
|
// The fix changes find_slot(ubatch, true) to find_slot(ubatch, false)
|
|
// in state_read_meta(), allowing non-contiguous slot allocation.
|
|
|
|
// TODO: merge with test-save-load-state.cpp
|
|
// TODO: merge with test-recurrent-state-rollback.cpp
|
|
|
|
#include "arg.h"
|
|
#include "common.h"
|
|
#include "llama.h"
|
|
|
|
#include <vector>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
int main(int argc, char ** argv) {
|
|
common_params params;
|
|
|
|
params.sampling.seed = 1234;
|
|
params.kv_unified = true;
|
|
params.n_parallel = 3;
|
|
params.n_ctx = 256;
|
|
|
|
common_init();
|
|
|
|
if (!common_params_parse(argc, argv, params, LLAMA_EXAMPLE_COMMON)) {
|
|
return 1;
|
|
}
|
|
|
|
// init
|
|
|
|
llama_backend_init();
|
|
|
|
common_init_result_ptr llama_init = common_init_from_params(params);
|
|
|
|
llama_model * model = llama_init->model();
|
|
llama_context * ctx = llama_init->context();
|
|
|
|
if (model == nullptr || ctx == nullptr) {
|
|
fprintf(stderr, "%s : failed to init\n", __func__);
|
|
return 1;
|
|
}
|
|
|
|
GGML_UNUSED(model);
|
|
|
|
// tokenize prompt
|
|
std::vector<llama_token> tokens(70, 1);
|
|
|
|
// interleave the 3 sequences:
|
|
// 01201230123...
|
|
common_batch batch(ctx);
|
|
for (size_t i = 0; i < tokens.size(); i++) {
|
|
for (int s = 0; s < params.n_parallel; ++s) {
|
|
batch.add(tokens[i], i, s, false);
|
|
}
|
|
}
|
|
batch.set_output(batch.size() - 1, true);
|
|
|
|
if (llama_process(ctx, LLAMA_PROCESS_TYPE_DECODE, batch.get())) {
|
|
fprintf(stderr, "%s : failed to decode seq 0\n", __func__);
|
|
return 1;
|
|
}
|
|
|
|
fprintf(stderr, "%s : processed prompt on seq 0, 1, 2 (%zu tokens each)\n", __func__, tokens.size());
|
|
|
|
// Save state of seq 1
|
|
std::vector<uint8_t> seq_state(llama_state_seq_get_size(ctx, 1));
|
|
const size_t ncopy = llama_state_seq_get_data(ctx, seq_state.data(), seq_state.size(), 1);
|
|
if (ncopy != seq_state.size()) {
|
|
fprintf(stderr, "%s : failed to save seq 1 state\n", __func__);
|
|
return 1;
|
|
}
|
|
fprintf(stderr, "%s : saved seq 1 state, %zu bytes\n", __func__, ncopy);
|
|
|
|
// clear seq 1 to create a "hole" in the KV cache (fragmentation)
|
|
// 0.20.20.20.2....
|
|
llama_memory_t mem = llama_get_memory(ctx);
|
|
llama_memory_seq_rm(mem, 1, -1, -1);
|
|
fprintf(stderr, "%s : cleared seq 1 to create fragmentation\n", __func__);
|
|
|
|
// Now the cache has holes where seq 1 was
|
|
// This creates fragmentation - there's no contiguous block large enough
|
|
// for the seq 1 state if we only look for contiguous slots
|
|
|
|
// Restore seq 1 state into seq 1 (should work with non-contiguous allocation)
|
|
// We use seq 1 since it's a valid sequence ID (0 to n_parallel-1)
|
|
// Before the fix, this would fail with "failed to find available cells in kv cache"
|
|
const size_t nset = llama_state_seq_set_data(ctx, seq_state.data(), seq_state.size(), 1);
|
|
if (nset != seq_state.size()) {
|
|
fprintf(stderr, "%s : FAILED to restore seq state into fragmented cache (got %zu, expected %zu)\n",
|
|
__func__, nset, seq_state.size());
|
|
fprintf(stderr, "%s : This is the bug - state restore fails with fragmented KV cache\n", __func__);
|
|
return 1;
|
|
}
|
|
fprintf(stderr, "%s : restored state into seq 1, %zu bytes\n", __func__, nset);
|
|
|
|
// Verify we can decode with the restored state
|
|
// Generate one token to verify the restored state is usable
|
|
auto sparams = llama_sampler_chain_default_params();
|
|
llama_sampler * smpl = llama_sampler_chain_init(sparams);
|
|
llama_sampler_chain_add(smpl, llama_sampler_init_dist(params.sampling.seed));
|
|
|
|
auto next_token = llama_sampler_sample(smpl, ctx, -1);
|
|
auto next_token_str = common_token_to_piece(ctx, next_token);
|
|
|
|
batch.clear();
|
|
batch.add(next_token, (int)tokens.size(), 1, true);
|
|
|
|
if (llama_process(ctx, LLAMA_PROCESS_TYPE_DECODE, batch.get())) {
|
|
fprintf(stderr, "%s : failed to decode with restored state\n", __func__);
|
|
llama_sampler_free(smpl);
|
|
return 1;
|
|
}
|
|
|
|
fprintf(stderr, "%s : successfully decoded with restored state, generated: '%s'\n", __func__, next_token_str.c_str());
|
|
fprintf(stderr, "%s : SUCCESS - state restore works with fragmented KV cache\n", __func__);
|
|
|
|
llama_sampler_free(smpl);
|
|
|
|
return 0;
|
|
}
|