Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- The paged prompt store (`prompt_radix`) can now serve a family whose cached rows read tokens past their page, as GLM's MTP rows do: with a lookahead, a page is matched and shared only while a prompt repeats the tokens past it, and a planned family cuts and resumes only on its grid, a whole number of pages. A store given rules it cannot keep (a zero page, or a planned grid that is no multiple of the page) now fails with `error.BadPromptRadixRules` in every build instead of a debug-only assert. No family turns these rules on yet, so HIP's families and every other path keep, resume and evict exactly as before, which a new golden test pins (#1659, part of #1510). Thanks to @Bizuayeu.

- MOSS-Image: `moss_image.noise` makes the pipeline's seeded starting latents (`torch.randn` on a seeded CPU generator) in the family's host code, byte for byte what x86 torch makes, on any CPU. ARM torch, on a Mac or a GB10, gives slightly different bits: for seed 42 at 1024x1024, 200,873 of 524,288 values differ, by at most 2.2e-6. `zig build test` checks 69 cases, 24 seeds and 9 sizes from 16x16 to 2048x2048, against torch 2.13.0's recorded latents, and `moss_image_case.py noise` records them. `tf-moss-image` still reads its latents from the case file, and nothing else changes (#1668, fixes #1530). Thanks to @BobClawblaw.

- Intel Arc Pro B70 (Xe2): the XPU backend serves Gemma 4 E4B (NVIDIA Nemotron 3.5 Bolt E4B) from `mlx_lm.convert` 4-bit checkpoints, with the per-layer input linears kept in BF16 (a checkpoint that quantizes them still loads, with a warning per tensor) and, optionally, 8-bit attention projections. It keeps prompt states in the prompt cache, runs up to 8 streams with shared forwards, draws the sampling top-k on the card and answers `/v1/decisions`; the audio tower and the drafter are not included yet. On the B70 it reads 2,048-token prompts at 6,772 tok/s and decodes one stream at 135.6 tok/s (3,950 and 118.6 with 8-bit attention), and 4 streams reach 426 tok/s together, each reply equal to the stream's reply alone. Qwen3.8 27B and Nemotron 3.5 Lightning on the XPU backend give the same replies at the same speeds (#1552, for #1222). Thanks to @Mirrdhyn.
Expand Down
44 changes: 27 additions & 17 deletions zig/src/core/prompt_radix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub const Counts = tree_mod.Counts;
pub const Node = tree_mod.Node;
pub const Tree = tree_mod.Tree;

/// The rules of prompt_cache.zig, paged: `page` set, `lookahead` and `planned` not (a row's bits ignore its chunk).
/// The rules of prompt_cache.zig, paged: `page` set; a planned store cuts and resumes only on `grid`, a multiple of it.
pub const Rules = pc.Rules;

/// What the tree may hold: pages, and snapshots.
Expand Down Expand Up @@ -54,8 +54,18 @@ pub const Store = struct {
const SHARED_KEYS = 64;

pub fn init(gpa: Allocator, family: Snapshots, pages: Pages, rules: Rules) !Store {
std.debug.assert(rules.page > 0 and rules.lookahead == 0 and !rules.planned);
return .{ .tree = try Tree.init(gpa, family, pages, rules.page), .rules = rules };
if (rules.page == 0 or (rules.planned and (rules.grid == 0 or rules.grid % rules.page != 0))) return error.BadPromptRadixRules;
return .{ .tree = try Tree.init(gpa, family, pages, rules.page, rules.lookahead), .rules = rules };
}

/// Where a pass may cut and resume: a page edge, or a planned store's grid.
fn step(s: *const Store) u32 {
return if (s.rules.planned) s.rules.grid else s.rules.page;
}

/// Tokens a resume leaves past its state: the lookahead it read, and at least one to prefill.
fn past(s: *const Store) u32 {
return @max(1, s.rules.lookahead);
}

pub fn deinit(s: *Store) void {
Expand All @@ -75,10 +85,10 @@ pub const Store = struct {
return s.limits.pages > 0 and s.limits.snaps > 0;
}

/// The deepest node with a state `prompt` resumes exactly: its tokens a prefix, at least one token left.
/// The deepest node whose state `prompt` resumes exactly: its tokens and lookahead a prefix, a token left.
pub fn find(s: *const Store, prompt: []const u32) ?*Node {
if (!s.on() or prompt.len < 2) return null;
return s.tree.deepest(prompt, (prompt.len - 1) / s.rules.page * s.rules.page);
if (!s.on() or prompt.len < s.past() + 1) return null;
return s.tree.deepest(prompt, (prompt.len - s.past()) / s.rules.page * s.rules.page, s.step());
}

/// Restores the longest state `prompt` resumes into `owner`, its pages into `adopt` (held once more); plans marks.
Expand All @@ -91,8 +101,8 @@ pub const Store = struct {
const marks_ = try s.fitting(a, planned);
errdefer a.free(marks_);
for (shared) |w| { // the shared cuts this pass keeps: their states serve other conversations too
const cut = w - w % s.rules.page;
if (std.mem.indexOfScalar(u32, marks_, cut) != null) s.noteShared(prompt[0..cut]);
const cut = w - w % s.step();
if (std.mem.indexOfScalar(u32, marks_, cut) != null) s.noteShared(prompt[0 .. cut + s.rules.lookahead]);
}
s.reserve(prompt, found, marks_);
var from: u32 = 0;
Expand Down Expand Up @@ -133,15 +143,15 @@ pub const Store = struct {
if (marks_.len == 0) return;
const last = marks_[marks_.len - 1];
while (true) {
const new = last / s.rules.page - s.tree.have(prompt[0..last]);
const new = last / s.rules.page - s.tree.have(prompt[0 .. last + s.rules.lookahead]);
if (s.tree.held_pages + new <= s.limits.pages and s.tree.snaps + marks_.len <= s.limits.snaps) return;
s.tree.evict(s.victim(prompt, from) orelse return);
}
}

/// A kept state a peer could not resume goes, so later prompts do not ask for it again.
pub fn forget(s: *Store, prompt: []const u32, at: u32) void {
const n = s.find(prompt[0..@min(prompt.len, at + 1)]) orelse return;
const n = s.find(prompt[0..@min(prompt.len, at + s.past())]) orelse return;
if (n.end != at) return;
s.tree.counts.failed += 1;
s.tree.evict(n);
Expand Down Expand Up @@ -185,10 +195,10 @@ pub const Store = struct {
if (stable > 0 and stable < history_len and stable >= history_len / 2) try want.append(a, stable);
}
try want.appendSlice(a, shared);
if (s.rules.tail) try want.append(a, @intCast(prompt.len - 1));
if (s.rules.tail and prompt.len > s.past()) try want.append(a, @intCast(prompt.len - s.past()));
for (want.items, 0..) |w, k| {
const m = w - w % s.rules.page;
if (m <= from or m >= prompt.len) continue;
const m = w - w % s.step();
if (m <= from or m >= prompt.len or m + s.rules.lookahead > prompt.len) continue;
if (k > 0) { // the history's mark always; the others only away from it, each other and the resume point
if (m - from < s.rules.min_gap) continue;
const near = for (out.items) |o| {
Expand All @@ -205,9 +215,9 @@ pub const Store = struct {
/// At `at` the tree takes the pages of `mine` it lacks and the snapshot, evicting to fit; `path` gets its pages.
pub fn keep(s: *Store, prompt: []const u32, at: u32, owner: ?*anyopaque, mine: []const u32, path: []u32) Kept {
const t = &s.tree;
if (!s.on() or at == 0 or at % s.rules.page != 0 or at > prompt.len or mine.len != at / s.rules.page or path.len != mine.len) return .{ .held = false, .shared = 0 };
if (!s.on() or at == 0 or at % s.step() != 0 or at + s.rules.lookahead > prompt.len or mine.len != at / s.rules.page or path.len != mine.len) return .{ .held = false, .shared = 0 };
t.clock += 1;
const tokens = prompt[0..at];
const tokens = prompt[0 .. at + s.rules.lookahead]; // a page past which the lookahead differs is another page
const flagged = std.mem.indexOfScalar(u64, s.shared_keys.items, sharedKey(tokens)) != null;
while (true) {
var last: ?*Node = null;
Expand Down Expand Up @@ -254,10 +264,10 @@ pub const Store = struct {
return x.used < y.used;
}

/// Whether `prompt` repeats every token up to the end of `n`.
/// Whether `prompt` repeats every token `n` holds: up to its end, and its lookahead.
fn extends(n: *const Node, prompt: []const u32) bool {
const parent = n.parent orelse return true;
return prompt.len >= n.end and std.mem.eql(u32, n.tokens, prompt[parent.end..n.end]) and extends(parent, prompt);
return prompt.len >= parent.end + n.tokens.len and std.mem.eql(u32, n.tokens, prompt[parent.end..][0..n.tokens.len]) and extends(parent, prompt);
}

/// Whether a later prompt kept a state below `n`.
Expand Down
206 changes: 206 additions & 0 deletions zig/src/core/prompt_radix_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,209 @@ test "a history every turn resumes again after one kept a longer state is a bran
f.leave(mine.items);
try std.testing.expectEqual(s.tree.snaps, f.live);
}

/// A golden pass: where it resumed and kept, then what the tree held and had counted.
const Row = struct { from: u32, marks: []const u32, snaps: usize, pages: usize, bytes: u64, hits: u64, misses: u64, kept: u64, evicted: u64, refused: u64 };

/// Two conversations over a shared system block, a growing and an edited turn, a third prompt that evicts, a refusal.
fn goldenRun(a: Allocator, f: *Fake, s: *Store, out: *std.ArrayList(Row)) !void {
var mine: std.ArrayList(u32) = .empty;
var edited = seq(42, 0);
edited[25] = 999;
var other: [32]u32 = undefined;
@memcpy(other[0..13], &seq(13, 0));
@memcpy(other[13..], &seq(19, 200));
const Pass = struct { prompt: []const u32, history: u32, shared: []const u32 };
const passes = [_]Pass{
.{ .prompt = &seq(30, 0), .history = 0, .shared = &.{12} },
.{ .prompt = &seq(42, 0), .history = 30, .shared = &.{12} },
.{ .prompt = &other, .history = 0, .shared = &.{12} },
.{ .prompt = &edited, .history = 30, .shared = &.{12} },
.{ .prompt = &seq(56, 0), .history = 42, .shared = &.{12} },
.{ .prompt = &seq(30, 300), .history = 20, .shared = &.{} },
.{ .prompt = &seq(92, 400), .history = 88, .shared = &.{} },
};
for (passes) |p| {
const plan = try pass(f, s, a, p.prompt, p.history, p.shared, &mine);
const c = s.tree.counts;
try out.append(a, .{ .from = plan.from, .marks = plan.marks, .snaps = s.tree.snaps, .pages = s.tree.held_pages, .bytes = s.tree.held(), .hits = c.hits, .misses = c.misses, .kept = c.kept, .evicted = c.evicted, .refused = c.refused });
f.leave(mine.items);
}
}

test "golden: lookahead 0 and no grid, as HIP's families and Qwen's CUDA path keep, resume and evict the same" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
var f: Fake = .{ .gpa = std.testing.allocator };
var s = try newStore(&f, .{ .page = 4, .min_gap = 16, .min_prompt = 0, .tail = true });
defer s.deinit();
s.limit(.{ .pages = 20, .snaps = 4 });
var rows: std.ArrayList(Row) = .empty;
try goldenRun(a, &f, &s, &rows);
// the store before it learned a lookahead and a grid kept exactly these
const want = [_]Row{
.{ .from = 0, .marks = &.{28}, .snaps = 1, .pages = 7, .bytes = 170, .hits = 0, .misses = 1, .kept = 1, .evicted = 0, .refused = 0 },
.{ .from = 28, .marks = &.{}, .snaps = 1, .pages = 7, .bytes = 170, .hits = 1, .misses = 1, .kept = 1, .evicted = 0, .refused = 0 },
.{ .from = 0, .marks = &.{28}, .snaps = 2, .pages = 11, .bytes = 310, .hits = 1, .misses = 2, .kept = 2, .evicted = 0, .refused = 0 },
.{ .from = 0, .marks = &.{28}, .snaps = 3, .pages = 12, .bytes = 420, .hits = 1, .misses = 3, .kept = 3, .evicted = 0, .refused = 0 },
.{ .from = 28, .marks = &.{40}, .snaps = 4, .pages = 15, .bytes = 550, .hits = 2, .misses = 3, .kept = 4, .evicted = 0, .refused = 0 },
.{ .from = 0, .marks = &.{20}, .snaps = 4, .pages = 20, .bytes = 600, .hits = 2, .misses = 4, .kept = 5, .evicted = 1, .refused = 0 },
.{ .from = 0, .marks = &.{}, .snaps = 4, .pages = 20, .bytes = 600, .hits = 2, .misses = 5, .kept = 5, .evicted = 1, .refused = 1 },
};
try std.testing.expectEqual(want.len, rows.items.len);
for (want, rows.items) |w, r| {
try std.testing.expectEqualSlices(u32, w.marks, r.marks);
var got = r;
got.marks = w.marks;
try std.testing.expectEqual(w, got);
}
try std.testing.expectEqual(s.tree.snaps, f.live);
}

test "lookahead 1: a state resumes when the token past it repeats, and not when it differs" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
var f: Fake = .{ .gpa = std.testing.allocator };
var s = try newStore(&f, .{ .page = 4, .min_gap = 8, .min_prompt = 0, .lookahead = 1 });
defer s.deinit();
s.limit(.{ .pages = 40, .snaps = 8 });
var mine: std.ArrayList(u32) = .empty;
const t1 = seq(24, 0);
var plan = try pass(&f, &s, a, &t1, 20, &.{}, &mine);
try std.testing.expectEqualSlices(u32, &.{20}, plan.marks);
f.leave(mine.items);
const t2 = seq(32, 0); // the next turn repeats token 20
plan = try pass(&f, &s, a, &t2, 28, &.{}, &mine);
try std.testing.expectEqual(@as(u32, 20), plan.from);
f.leave(mine.items);
var t3 = seq(32, 0); // a conversation parting exactly at token 20 cannot use the state there
t3[20] = 999;
plan = try pass(&f, &s, a, &t3, 28, &.{}, &mine);
try std.testing.expectEqual(@as(u32, 0), plan.from);
f.leave(mine.items);
try std.testing.expectEqual(@as(u32, 20), s.find(&seq(21, 0)).?.end); // its lookahead, the one token to prefill
try std.testing.expect(s.find(&seq(20, 0)) == null); // no token past the state: nothing to resume
try std.testing.expectEqual(s.tree.snaps, f.live);
}

/// Pages a second conversation takes from a first's state at 20 when it keeps at 20 too, parting from it at `branch`.
fn sharedAfter(branch: usize, lookahead: u32) !usize {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
var f: Fake = .{ .gpa = std.testing.allocator };
var s = try newStore(&f, .{ .page = 4, .min_gap = 8, .min_prompt = 0, .lookahead = lookahead });
defer s.deinit();
s.limit(.{ .pages = 40, .snaps = 8 });
var mine: std.ArrayList(u32) = .empty;
_ = try pass(&f, &s, a, &seq(24, 0), 20, &.{}, &mine);
f.leave(mine.items);
var second = seq(24, 0);
second[branch] = 999;
var own: [5]u32 = undefined;
f.lane(&own);
var path: [5]u32 = undefined;
const kept = s.keep(&second, 20, null, &own, &path);
f.leave(&own);
try std.testing.expect(kept.held);
try std.testing.expectEqual(s.tree.snaps, f.live);
var taken: usize = 0;
for (own, path) |o, p| taken += @intFromBool(o != p);
return taken;
}

test "lookahead 1: a conversation shares only the pages whose next token it still repeats" {
// parting at the state's end: the last page's rows read the token past it, so it is not shared
try std.testing.expectEqual(@as(usize, 5), try sharedAfter(20, 0));
try std.testing.expectEqual(@as(usize, 4), try sharedAfter(20, 1));
// parting at an inner page edge, and one token after it
try std.testing.expectEqual(@as(usize, 4), try sharedAfter(16, 0));
try std.testing.expectEqual(@as(usize, 3), try sharedAfter(16, 1));
try std.testing.expectEqual(@as(usize, 4), try sharedAfter(17, 1));
}

test "planned: marks and resumes sit on the grid, never on a page between" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
var f: Fake = .{ .gpa = std.testing.allocator };
var s = try newStore(&f, .{ .page = 4, .min_gap = 8, .min_prompt = 0, .tail = true, .planned = true, .grid = 8 });
defer s.deinit();
s.limit(.{ .pages = 40, .snaps = 8 });
// a page store would cut at 12 and 40; the grid of 8 moves the block's cut to 8, the tail joins the history's 40
try std.testing.expectEqualSlices(u32, &.{ 8, 40 }, try s.marks(a, &seq(48, 0), 0, 41, &.{12}, &.{}));
var mine: std.ArrayList(u32) = .empty;
var plan = try pass(&f, &s, a, &seq(24, 0), 20, &.{}, &mine);
try std.testing.expectEqualSlices(u32, &.{16}, plan.marks); // history 20, its grid point 16
f.leave(mine.items);
plan = try pass(&f, &s, a, &seq(40, 0), 36, &.{}, &mine);
try std.testing.expectEqual(@as(u32, 16), plan.from);
f.leave(mine.items);
try std.testing.expectEqual(s.tree.snaps, f.live);
}

test "planned: a keep off the grid is refused, one on it kept" {
var f: Fake = .{ .gpa = std.testing.allocator };
var s = try newStore(&f, .{ .page = 4, .min_gap = 8, .min_prompt = 0, .planned = true, .grid = 8 });
defer s.deinit();
s.limit(.{ .pages = 40, .snaps = 8 });
var own: [4]u32 = undefined;
f.lane(&own);
defer f.leave(&own);
var path: [4]u32 = undefined;
const prompt = seq(24, 0);
try std.testing.expect(!s.keep(&prompt, 12, null, own[0..3], path[0..3]).held);
try std.testing.expectEqual(@as(usize, 0), s.tree.snaps);
try std.testing.expect(s.keep(&prompt, 16, null, &own, &path).held);
try std.testing.expectEqual(@as(usize, 1), s.tree.snaps);
}

test "the tree's deepest state is the deepest at a multiple of the step it is asked for" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
var f: Fake = .{ .gpa = std.testing.allocator };
var s = try newStore(&f, rules);
defer s.deinit();
s.limit(.{ .pages = 40, .snaps = 8 });
var mine: std.ArrayList(u32) = .empty;
const prompt = seq(24, 0);
_ = try pass(&f, &s, a, &prompt, 12, &.{}, &mine); // a state at 12, off a grid of 8
f.leave(mine.items);
try std.testing.expectEqual(@as(u32, 12), s.tree.deepest(&prompt, 20, 4).?.end);
try std.testing.expect(s.tree.deepest(&prompt, 20, 8) == null);
}

test "GLM's shape, a grid of four pages and lookahead 1: a chat resumes at its history's grid point, sharing its pages" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
var f: Fake = .{ .gpa = std.testing.allocator };
var s = try newStore(&f, .{ .page = 4, .min_gap = 16, .min_prompt = 0, .lookahead = 1, .planned = true, .grid = 16 });
defer s.deinit();
s.limit(.{ .pages = 40, .snaps = 8 });
var mine: std.ArrayList(u32) = .empty;
var plan = try pass(&f, &s, a, &seq(40, 0), 36, &.{}, &mine);
try std.testing.expectEqualSlices(u32, &.{32}, plan.marks);
const first = try a.dupe(u32, mine.items[0..8]);
f.leave(mine.items);
plan = try pass(&f, &s, a, &seq(56, 0), 52, &.{}, &mine);
try std.testing.expectEqual(@as(u32, 32), plan.from);
try std.testing.expectEqualSlices(u32, first, mine.items[0..8]); // the resume adopted the first turn's pages
try std.testing.expectEqualSlices(u32, &.{48}, plan.marks);
f.leave(mine.items);
try std.testing.expectEqual(@as(usize, 2), s.tree.snaps);
try std.testing.expectEqual(@as(usize, 12), s.tree.held_pages);
try std.testing.expectEqual(s.tree.snaps, f.live);
}

test "a store refuses rules it cannot keep: no page, or a planned grid that is no multiple of the page" {
var f: Fake = .{ .gpa = std.testing.allocator };
try std.testing.expectError(error.BadPromptRadixRules, newStore(&f, .{ .page = 0 }));
try std.testing.expectError(error.BadPromptRadixRules, newStore(&f, .{ .page = 4, .planned = true }));
try std.testing.expectError(error.BadPromptRadixRules, newStore(&f, .{ .page = 4, .planned = true, .grid = 6 }));
var s = try newStore(&f, .{ .page = 4, .planned = true, .grid = 8 });
s.deinit();
}
Loading
Loading