Skip to content
Merged
14 changes: 9 additions & 5 deletions .github/workflows/build-cpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ jobs:

windows:
name: windows / ${{ matrix.build }}
runs-on: windows-2025
runs-on: windows-2022

env:
OPENBLAS_VERSION: 0.3.23
Expand All @@ -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' }}
Expand All @@ -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
Expand Down Expand Up @@ -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' }}
11 changes: 6 additions & 5 deletions common/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("<function=" + p.tool_name(p.literal(name)) + ">\n") +
p.tool_args(args) +
Expand Down
98 changes: 54 additions & 44 deletions src/llama-kv-cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<uint32_t> 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();

Expand All @@ -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)) {
Expand Down Expand Up @@ -778,16 +799,8 @@ llama_kv_cache::slot_info_vec_t llama_kv_cache::prepare(const std::vector<llama_
const llama_kv_settle_on_return settle{ v_cells };
llama_kv_cache::slot_info_vec_t res;

struct state_t {
slot_info sinfo; // slot info for the ubatch

std::vector<uint32_t> v_heads_old; // old positions of the heads, before placing the ubatch

std::vector<llama_kv_cells> 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<state_t> states;
const auto heads_before = v_heads;
placement_journal journal;

bool success = true;

Expand All @@ -802,37 +815,13 @@ llama_kv_cache::slot_info_vec_t llama_kv_cache::prepare(const std::vector<llama_
// remember the position that we found
res.push_back(sinfo_new);

// store the old state of the cells in the recovery stack
{
state_t state = { sinfo_new, v_heads, {} };

for (uint32_t s = 0; s < sinfo_new.n_stream(); ++s) {
auto & cells = v_cells[sinfo_new.strm[s]];

state.v_cells.push_back(cells.cp(sinfo_new.idxs[s]));
}

states.push_back(std::move(state));
}

// now emplace the ubatch
apply_ubatch(sinfo_new, ubatch);
apply_ubatch(sinfo_new, ubatch, &journal);
}

GGML_ASSERT(!states.empty() || !success);

// iterate backwards and restore the cells to their original state
for (auto it = states.rbegin(); it != states.rend(); ++it) {
const auto & sinfo = it->sinfo;

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 {};
Expand Down Expand Up @@ -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) {
Expand All @@ -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<llama_pos>::max();
for (uint32_t ii = 0; ii < sinfo.size(); ++ii) {
const uint32_t i = s*sinfo.size() + ii;

Expand All @@ -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);
}

Expand All @@ -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<llama_pos>::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
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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<const llama_pos>(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 {
Expand All @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/llama-kv-cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> indices;
llama_kv_cells cells;
};
using placement_journal = std::vector<cell_snapshot>;
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;

Expand Down
43 changes: 29 additions & 14 deletions src/llama-kv-cells.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <atomic>
#include <bitset>
#include <limits>
#include <memory>
#include <cassert>
#include <cstring>
#include <vector>
Expand Down Expand Up @@ -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<const llama_pos>;
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<llama_pos>::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<llama_pos>::max();
checkpoints.clear();
for (uint32_t i = 0; i < pos.size(); ++i) {
pos[i] = -1;
ext[i].reset();
Expand Down Expand Up @@ -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<llama_pos>::max();
uint64_t checkpoint_serial = 0;
std::vector<std::weak_ptr<const llama_pos>> checkpoints;

bool has_shift = false;

Expand Down
2 changes: 1 addition & 1 deletion src/llama-persona-blocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
Loading