diff --git a/.github/workflows/build-cpu.yml b/.github/workflows/build-cpu.yml index 690e371142ed..f919218e3e69 100644 --- a/.github/workflows/build-cpu.yml +++ b/.github/workflows/build-cpu.yml @@ -134,7 +134,7 @@ jobs: windows: name: windows / ${{ matrix.build }} - runs-on: windows-2025 + runs-on: windows-2022 env: OPENBLAS_VERSION: 0.3.23 @@ -161,7 +161,7 @@ jobs: - name: ccache uses: ggml-org/ccache-action@v1.2.21 with: - key: cpu-windows-2025-${{ matrix.build }} + key: cpu-windows-2022-${{ matrix.build }} variant: ccache evict-old-files: 1d save: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }} @@ -179,10 +179,14 @@ jobs: $lib = $(join-path $msvc 'bin\Hostx64\x64\lib.exe') & $lib /machine:x64 "/def:${env:RUNNER_TEMP}/openblas/lib/libopenblas.def" "/out:${env:RUNNER_TEMP}/openblas/lib/openblas.lib" /name:openblas.dll - - name: Install Ninja - id: install_ninja + - name: Install Windows build tools run: | choco install ninja + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + # Match the compiler to the pinned LLVM OpenMP runtime in ggml. + if ((clang --version | Out-String) -notmatch 'clang version 20\.') { + choco install llvm --version=20.1.8 --allow-downgrade -y + } - name: Build id: cmake_build @@ -224,7 +228,7 @@ jobs: env: GH_TOKEN: ${{ github.token }} with: - key: cpu-windows-2025-${{ matrix.build }} + key: cpu-windows-2022-${{ matrix.build }} older: 5m min: 1 dry-run: ${{ github.event_name != 'push' || github.ref != 'refs/heads/master' }} diff --git a/common/chat.cpp b/common/chat.cpp index 78d0f62a7d20..79e7a0222b7c 100644 --- a/common/chat.cpp +++ b/common/chat.cpp @@ -1276,12 +1276,13 @@ static common_chat_params common_chat_params_init_qwen3_coder(const common_chat_ (is_required ? required_args : optional_args).push_back(arg_rule); }); - // Accept required arguments in any order, as Qwen does not always adhere to the - // order provided. - auto args = p.permute("tool-" + name + "-args", required_args); - if (!optional_args.empty()) { - args = args + p.zero_or_more(p.choice(optional_args)); + // Optional arguments may precede, separate, or follow required arguments. + // Keep the required permutation intact, including its bound on grammar growth. + auto optional = optional_args.empty() ? p.eps() : p.zero_or_more(p.choice(optional_args)); + for (auto & required : required_args) { + required = required + optional; } + auto args = optional + p.permute("tool-" + name + "-args", required_args); auto func = p.tool(p.tool_open("\n") + p.tool_args(args) + diff --git a/src/llama-kv-cache.cpp b/src/llama-kv-cache.cpp index 7c58de300c7b..82b7fa8405fc 100644 --- a/src/llama-kv-cache.cpp +++ b/src/llama-kv-cache.cpp @@ -393,6 +393,10 @@ void llama_kv_cache::clear(bool data) { } bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) { + return seq_rm(seq_id, p0, p1, nullptr); +} + +bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1, placement_journal * journal) { const llama_kv_settle_on_return settle{ v_cells }; // TODO: refactor [TAG_KV_CACHE_SHARE_CELLS] if (other) { @@ -413,7 +417,22 @@ bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) { if (seq_id >= 0) { auto & cells = v_cells[seq_to_stream[seq_id]]; auto & head = v_heads[seq_to_stream[seq_id]]; - cells.note_cut(p0); // a checkpoint at or past p0 no longer holds what it counted on + if (!journal) { + cells.note_cut(p0); + } + + if (journal) { + std::vector indices; + for (uint32_t i = 0; i < cells.size(); ++i) { + if (cells.pos_in(i, p0, p1) && cells.seq_has(i, seq_id)) { + indices.push_back(i); + } + } + if (!indices.empty()) { + auto saved = cells.cp(indices); + journal->push_back({seq_to_stream[seq_id], std::move(indices), std::move(saved)}); + } + } uint32_t new_head = cells.size(); @@ -440,7 +459,9 @@ bool llama_kv_cache::seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1) { auto & head = v_heads[s]; uint32_t new_head = cells.size(); - cells.note_cut(p0); // every sequence in this stream loses what a checkpoint past p0 counted on + if (!journal) { + cells.note_cut(p0); + } for (uint32_t i = 0; i < cells.size(); ++i) { if (!cells.pos_in(i, p0, p1)) { @@ -778,16 +799,8 @@ llama_kv_cache::slot_info_vec_t llama_kv_cache::prepare(const std::vector v_heads_old; // old positions of the heads, before placing the ubatch - - std::vector v_cells; // copy of the old cells, before placing the ubatch - }; - - // remember the old state of the cells so we can restore it in the end - std::vector states; + const auto heads_before = v_heads; + placement_journal journal; bool success = true; @@ -802,37 +815,13 @@ llama_kv_cache::slot_info_vec_t llama_kv_cache::prepare(const std::vectorsinfo; - - for (uint32_t s = 0; s < sinfo.n_stream(); ++s) { - auto & cells = v_cells[sinfo.strm[s]]; - auto & head = v_heads[sinfo.strm[s]]; - - cells.set(sinfo.idxs[s], it->v_cells[s]); - head = it->v_heads_old[s]; - } + for (auto entry = journal.rbegin(); entry != journal.rend(); ++entry) { + v_cells[entry->stream].set(entry->indices, entry->cells); } + v_heads = heads_before; if (!success) { return {}; @@ -1122,6 +1111,10 @@ llama_kv_cache::slot_info llama_kv_cache::find_slot(const llama_ubatch & ubatch, } void llama_kv_cache::apply_ubatch(const slot_info & sinfo, const llama_ubatch & ubatch) { + apply_ubatch(sinfo, ubatch, nullptr); +} + +void llama_kv_cache::apply_ubatch(const slot_info & sinfo, const llama_ubatch & ubatch, placement_journal * journal) { const llama_kv_settle_on_return settle{ v_cells }; // TODO: refactor [TAG_KV_CACHE_SHARE_CELLS] if (other) { @@ -1138,6 +1131,12 @@ void llama_kv_cache::apply_ubatch(const slot_info & sinfo, const llama_ubatch & assert(ubatch.n_tokens == sinfo.n_stream()*sinfo.size()); for (uint32_t s = 0; s < sinfo.n_stream(); ++s) { + if (journal) { + const uint32_t stream = sinfo.strm[s]; + const auto & indices = sinfo.idxs[s]; + journal->push_back({stream, indices, v_cells[stream].cp(indices)}); + } + llama_pos first_overwritten = std::numeric_limits::max(); for (uint32_t ii = 0; ii < sinfo.size(); ++ii) { const uint32_t i = s*sinfo.size() + ii; @@ -1153,6 +1152,7 @@ void llama_kv_cache::apply_ubatch(const slot_info & sinfo, const llama_ubatch & seq_pos_max_rm[seq_id] = std::max(seq_pos_max_rm[seq_id], pos); + first_overwritten = std::min(first_overwritten, pos); cells.rm(idx); } @@ -1170,6 +1170,9 @@ void llama_kv_cache::apply_ubatch(const slot_info & sinfo, const llama_ubatch & cells.seq_add(idx, ubatch.seq_id[i][s]); } } + if (!journal && first_overwritten != std::numeric_limits::max()) { + v_cells[sinfo.strm[s]].note_cut(first_overwritten); + } } // note: we want to preserve the invariant that all positions between [pos_min, pos_max] for each sequence @@ -1188,7 +1191,7 @@ void llama_kv_cache::apply_ubatch(const slot_info & sinfo, const llama_ubatch & LLAMA_LOG_DEBUG("%s: purging positions [%d, %d] of sequence %d from KV cache\n", __func__, cells.seq_pos_min(s), seq_pos_max_rm[s], s); - seq_rm(s, cells.seq_pos_min(s), seq_pos_max_rm[s] + 1); + seq_rm(s, cells.seq_pos_min(s), seq_pos_max_rm[s] + 1, journal); } } @@ -1349,13 +1352,20 @@ llama_persona_move llama_kv_cache::persona_check_checkpoint(llama_seq_id seq_id, return llama_persona_move::invalid_seq; } out.pos = v_cells[seq_to_stream[seq_id]].seq_pos_max(seq_id); - out.lineage = v_cells[seq_to_stream[seq_id]].lineage(); + auto & cells = v_cells[seq_to_stream[seq_id]]; + out.lineage = cells.lineage(); + try { + out.mark = std::make_shared(out.pos); + cells.prepare_checkpoint(); + } catch (const std::bad_alloc &) { + return llama_persona_move::out_of_memory; + } return llama_persona_move::done; } void llama_kv_cache::persona_commit_checkpoint(llama_seq_id seq_id, llama_persona_checkpoint_blocks & out) { out.seq = seq_id; - out.serial = v_cells[seq_to_stream[seq_id]].mark_checkpoint(); // cuts are counted from here on, for this checkpoint only + v_cells[seq_to_stream[seq_id]].mark_checkpoint(out.mark); } llama_persona_move llama_kv_cache::persona_check_rewind(llama_seq_id seq_id, const llama_persona_checkpoint_blocks & in) const { @@ -1371,7 +1381,7 @@ llama_persona_move llama_kv_cache::persona_check_rewind(llama_seq_id seq_id, con // the cells must be the ones it was taken from, uncut at or below its position since (Codex on #153: a slot // reused by another mind must never receive these rows) const auto & cells = v_cells[seq_to_stream[seq_id]]; - if (cells.lineage() != in.lineage || cells.checkpoint() != in.serial || cells.cut_low() <= in.pos) { + if (cells.lineage() != in.lineage || !cells.holds_checkpoint(in.mark)) { return llama_persona_move::checkpoint_stale; } return llama_persona_move::done; diff --git a/src/llama-kv-cache.h b/src/llama-kv-cache.h index 9b2555b0872d..7d273a9521b9 100644 --- a/src/llama-kv-cache.h +++ b/src/llama-kv-cache.h @@ -247,6 +247,17 @@ class llama_kv_cache : public llama_memory_i { void set_input_v_rot(ggml_tensor * dst) const; private: + // Metadata changed by a speculative placement. Entries unwind in reverse order + // because a later purge can touch a cell already saved by an earlier placement. + struct cell_snapshot { + uint32_t stream; + std::vector indices; + llama_kv_cells cells; + }; + using placement_journal = std::vector; + void apply_ubatch(const slot_info & sinfo, const llama_ubatch & ubatch, placement_journal * journal); + bool seq_rm(llama_seq_id seq_id, llama_pos p0, llama_pos p1, placement_journal * journal); + const llama_model & model; const llama_hparams & hparams; diff --git a/src/llama-kv-cells.h b/src/llama-kv-cells.h index 19f2e43ad657..5f5c18afe783 100644 --- a/src/llama-kv-cells.h +++ b/src/llama-kv-cells.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -49,24 +50,39 @@ class llama_kv_cells { } std::swap(stale, other.stale); std::swap(lineage_id, other.lineage_id); - std::swap(lowest_cut, other.lowest_cut); - std::swap(checkpoint_serial, other.checkpoint_serial); + checkpoints.swap(other.checkpoints); } - // Continuum fork (CONTINUUM-ENGINE 4.15): whose history these cells hold, and the lowest position cut from it since - // her last checkpoint. A lineage is minted fresh whenever the cells are reset (or made), and travels with them on a - // swap, so a checkpoint follows her between streams and is refused for anyone else's cells in the same slot. + // Checkpoint handles own their positions. The cells retain a weak receipt for each + // live prefix. A cut revokes just the receipts whose prefix it changes. + using checkpoint_position = std::shared_ptr; uint64_t lineage() const { return lineage_id; } - llama_pos cut_low() const { return lowest_cut; } - void note_cut(llama_pos p0) { lowest_cut = std::min(lowest_cut, p0); } - // Only the newest checkpoint on a lineage rewinds: each take bumps the serial and starts counting cuts afresh, so - // an older one (whose cut receipt that reset erased) is refused by serial (Codex on #153). - uint64_t checkpoint() const { return checkpoint_serial; } - uint64_t mark_checkpoint() { lowest_cut = std::numeric_limits::max(); return ++checkpoint_serial; } + + void note_cut(llama_pos first) { + checkpoints.erase(std::remove_if(checkpoints.begin(), checkpoints.end(), [first](const auto & weak) { + const auto mark = weak.lock(); + return !mark || *mark >= first; + }), checkpoints.end()); + } + + void prepare_checkpoint() { + checkpoints.erase(std::remove_if(checkpoints.begin(), checkpoints.end(), [](const auto & weak) { + return weak.expired(); + }), checkpoints.end()); + checkpoints.reserve(checkpoints.size() + 1); + } + + void mark_checkpoint(const checkpoint_position & mark) { checkpoints.emplace_back(mark); } + + bool holds_checkpoint(const checkpoint_position & mark) const { + return mark && std::any_of(checkpoints.begin(), checkpoints.end(), [&](const auto & weak) { + return weak.lock() == mark; + }); + } void reset() { lineage_id = next_lineage(); - lowest_cut = std::numeric_limits::max(); + checkpoints.clear(); for (uint32_t i = 0; i < pos.size(); ++i) { pos[i] = -1; ext[i].reset(); @@ -543,8 +559,7 @@ class llama_kv_cells { return next.fetch_add(1, std::memory_order_relaxed); } uint64_t lineage_id = next_lineage(); - llama_pos lowest_cut = std::numeric_limits::max(); - uint64_t checkpoint_serial = 0; + std::vector> checkpoints; bool has_shift = false; diff --git a/src/llama-persona-blocks.h b/src/llama-persona-blocks.h index 81f921a3167a..3edcdfba9ec6 100644 --- a/src/llama-persona-blocks.h +++ b/src/llama-persona-blocks.h @@ -116,7 +116,7 @@ struct llama_persona_checkpoint_blocks { // reused by another mind, a restart, an eviction), so her rows are never spliced onto someone else's KV llama_persona_identity owner; uint64_t lineage = 0; - uint64_t serial = 0; // which checkpoint of that lineage: only the newest rewinds + llama_kv_cells::checkpoint_position mark; uint64_t rs_layout = 0; llama_rs_seq_blocks rs; }; diff --git a/tests/test-persona-blocks.cpp b/tests/test-persona-blocks.cpp index f22e24f9ff2e..027239bb0960 100644 --- a/tests/test-persona-blocks.cpp +++ b/tests/test-persona-blocks.cpp @@ -15,6 +15,9 @@ #include "llama.h" #include "../src/llama-context.h" +#include "../src/llama-batch.h" +#include "../src/llama-kv-cache.h" +#include "../src/llama-model.h" #include #include @@ -125,6 +128,47 @@ struct fixture { llama_pos pos_max(llama_seq_id seq) const { return llama_memory_seq_pos_max(llama_get_memory(ctx), seq); } }; +// Exercise placement and receipt ownership directly. Four metadata cells suffice to wrap +// the sliding window, independent of the architecture and weights of the fixture model. +static void test_checkpoint_after_swa_overwrite(const llama_model & model, bool fragmented) { + llama_kv_cache cache(model, model.hparams, GGML_TYPE_F16, GGML_TYPE_F16, false, false, true, + 4, 1, 1, 2, LLAMA_SWA_TYPE_STANDARD, nullptr, [](int) { return false; }, nullptr, nullptr); + llama_seq_id seq = 0; + llama_seq_id * ids = &seq; + llama_pos pos = 0; + int32_t count = 1; + llama_ubatch batch{}; + batch.n_seq_tokens = batch.n_seqs = batch.n_tokens = batch.n_seqs_unq = batch.n_pos = batch.b_equal_seqs = 1; + batch.pos = &pos; + batch.n_seq_id = &count; + batch.seq_id = &ids; + batch.seq_id_unq = &seq; + auto plan = [&] { + auto slots = cache.prepare({batch}); + require(slots.size() == 1, "one SWA slot planned"); + return slots.front(); + }; + auto take = [&](llama_persona_checkpoint_blocks & mark) { + require_move(cache.persona_check_checkpoint(seq, mark), llama_persona_move::done, "prepare SWA checkpoint"); + cache.persona_commit_checkpoint(seq, mark); + mark.taken = true; + }; + for (llama_pos i = 0; i < 4; ++i) { + pos = fragmented && i < 2 ? 1 - i : i; + cache.apply_ubatch(plan(), batch); + } + pos = 4; + llama_persona_checkpoint_blocks before{}, after{}; + take(before); + const auto slot = plan(); + require(cache.seq_pos_min(seq) == 0, "planning restores every purged prefix cell"); + require_move(cache.persona_check_rewind(seq, before), llama_persona_move::done, "planning preserves old KV"); + cache.apply_ubatch(slot, batch); + take(after); + require_move(cache.persona_check_rewind(seq, before), llama_persona_move::checkpoint_stale, "overwritten KV revoked"); + require_move(cache.persona_check_rewind(seq, after), llama_persona_move::done, "new sliding window retained"); +} + int main(int argc, char ** argv) { common_params params; params.kv_unified = false; // one attention stream per sequence @@ -198,6 +242,9 @@ int main(int argc, char ** argv) { return 0; } + test_checkpoint_after_swa_overwrite(*llama_get_model(f.ctx), false); + test_checkpoint_after_swa_overwrite(*llama_get_model(f.ctx), true); + const auto ref_a0 = f.reference(0, 1); // persona a on sequence 0 const auto ref_b1 = f.reference(1, 2); // persona b on sequence 1 @@ -438,7 +485,7 @@ int main(int argc, char ** argv) { // what this catches (Codex on #153 at 12a98779b): a newer take erasing the cut receipt an older checkpoint // needed. Take A, cut below it and refill a different suffix to the same length, take B: A is stale (by its - // serial), B still rewinds. + // cut receipt), B still rewinds. // A hybrid memory cuts its recurrent half only by a checkpoint rewind (a plain seq_rm mid-sequence is refused), // so the cut below A is a rewind to an earlier checkpoint taken at half her prompt. { @@ -456,12 +503,8 @@ int main(int argc, char ** argv) { f.decode_one(0, 7 % f.n_vocab, p); } require_move(st(llama_persona_checkpoint_take(f.ctx, 0, 0xA, 1, cp)), llama_persona_move::done, "checkpoint A"); - require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, early)), llama_persona_move::checkpoint_stale, "the earlier checkpoint is older than A on her lineage"); + require_move(st(llama_persona_checkpoint_rewind(f.ctx, 0, 0xA, 1, early)), llama_persona_move::done, "the head survives a later checkpoint on the unchanged prefix"); llama_persona_checkpoint_free(early); - f.n_prompt = half; - llama_memory_clear(llama_get_memory(f.ctx), true); - f.prompt(0, 1); - f.n_prompt = full; for (llama_pos p = half; p < full; ++p) { f.decode_one(0, 11 % f.n_vocab, p); // a different suffix, to the same length }