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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel
- HIP on gfx12 under `--policy matrix=on`: the WMMA dense tile's big block takes 64-deep K steps over swizzled LDS rows, its barrier orders LDS only, and the block thresholds are tuned again, with the same output bytes: Qwen3-Reranker-8B ranks 16 documents in 0.694 s instead of 0.720 s and Qwen3-Embedding-8B embeds 100 short inputs in 0.946 s instead of 0.974 s on a Radeon AI PRO R9700. The kernel contract checks the new block byte for byte against a frozen copy of the old one (part of #2245). Thanks to @laudney.
- HIP on gfx12 under `--policy matrix=on`: the WMMA dense tile's big block takes 64-deep K steps over swizzled LDS rows, its barrier orders LDS only, and the block thresholds are tuned again, with the same output bytes: Qwen3-Reranker-8B ranks 16 documents in 0.697 to 0.705 s instead of 0.720 to 0.727 s and Qwen3-Embedding-8B embeds 100 short inputs in 0.946 to 0.956 s instead of 0.969 to 0.995 s on a Radeon AI PRO R9700 (the best run of each of four alternated rounds). The kernel contract checks the new block byte for byte against a frozen copy of the old one (part of #2245). Thanks to @laudney.
- Fabric: a pipeline stage's boot and round loop is one driver, generic over an engine binding, so a CUDA, Metal or XPU stage writes only its binding; nothing uses it yet. A stop that a middle stage receives now passes on instead of closing the ring (#2241, part of #2005). Thanks to @jschmied.
- KV pool: a family's pool group is sized by its bytes a row over its layers, and a paged CUDA load prints a line of each group's rows a page and padding (the window rows and state outside or beside the pool too), apart from the unchanged startup line; every family stays one group, so nothing else changes (#2444 C2, refs #2444).
- KV pool: a family's pool group is sized by its bytes a row over its layers, and a paged CUDA load prints a line of each group's rows a page and padding (the window rows and state outside or beside the pool too), apart from the unchanged startup line; every family stays one group, so nothing else changes (#2444 C2, refs #2444). Thanks to @jschmied.
- KV pool: a family's pool group is sized by its bytes a row over its layers, and a paged CUDA load prints a line of each group's rows a page and padding (the window rows and state outside or beside the pool too), apart from the unchanged startup line; every family stays one group, so nothing else changes (#2532, part of #2444). Thanks to @jschmied.

- LFM2 on Macs: a non-finite value in a masked key no longer reaches prompt attention's output; finite prompts run the same kernels as before, and prefill is level (#2328, part of #1009).
- Release checks: on a Mac release binary, the private-path check no longer reads the code signature's hash bytes as text, which could spell a path by chance and refuse a clean build; the exact build root is still refused there, and the rest of the binary is checked as before (#2524).
Expand Down
5 changes: 5 additions & 0 deletions zig/gemma4_shared_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,12 @@ test "Gemma shared: snapshots spend pool pages without new device allocations" {
defer f.deinit();
const layout = fwd.pages.layout(&f.m);
try t.expectEqual(@as(u64, 7864384), layout.fixed_bytes);
try t.expectEqual(fwd.pages.pageBytes(&f.m), layout.pageBytes()); // the pool's page is the kernels' stride
try t.expectEqual(@as(u64, 524288), layout.pageBytes());
try t.expectEqual(layout.pageBytes(), layout.group().bytes() * fwd.pages.tokens); // one group sized by its row's bytes
const window = layout.window.?; // a sliding row against a global one: kv heads x head width
try t.expectEqual(layout.group().row * geo.sliding_kv_heads * cfg.sliding_dim, window.row * geo.global_kv_heads * cfg.global_dim);
try t.expectEqual(@as(u32, @intCast(fwd.pages.ownLayers(&f.m))), layout.group().layers + window.layers);
try t.expectEqual(@as(u64, 5237760), g4.snap.copyBytes(&f.m, 2048, 2048));
try t.expectEqual(@as(usize, 10), g4.snap.blobPages(&f.m, 2048, 2048));
const ids = try prompt();
Expand Down
112 changes: 112 additions & 0 deletions zig/src/core/kv_groups.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//! A pool's groups sized by bytes: a kind's bytes a row, the rows one page's bytes hold of it, and what it pads.
const std = @import("std");
const Allocator = std.mem.Allocator;

/// What a group caches: every row, a sliding window's rows, a stream's fixed state, a drafter's rows.
pub const Kind = enum { full, window, state, draft };

/// One kind's bytes: a row in one layer and the layers holding it (a state: a stream's bytes, one layer).
pub const Group = struct {
kind: Kind,
row: u64,
layers: u32 = 1,

/// Bytes one row adds over every layer of the group (a state: a stream's bytes).
pub fn bytes(g: Group) u64 {
return g.row * g.layers;
}

/// A group of `layers` layers whose rows add `total` bytes over all of them; null when they don't split evenly.
pub fn split(kind: Kind, total: u64, layers: u32) ?Group {
if (layers == 0 or total % layers != 0) return null;
return .{ .kind = kind, .row = total / layers, .layers = layers };
}
};

/// A group on pages: whole rows a page holds (0 for a state), the pages it takes, and the bytes they leave unused.
pub const Fit = struct { rows: u64, pages: u64, pad: u64 };

/// Rows of `row` bytes in a page of `page` bytes, and the tail past them.
pub fn rows(row: u64, page: u64) Fit {
const n = page / row;
return .{ .rows = n, .pages = 1, .pad = page - n * row };
}

/// Whole pages of `page` bytes that hold `bytes`, and the tail of the last one.
pub fn state(bytes: u64, page: u64) Fit {
const n = (bytes + page - 1) / page;
return .{ .rows = 0, .pages = n, .pad = n * page - bytes };
}

/// What the load report reads: the pool's group and page, and the kinds beside it in or outside the pool.
pub const Shape = struct {
full: Group,
named: bool = true, // the family names the full group's layers; false: its row is a token's bytes over all of them
page_rows: u32,
window: ?Group = null, // rows outside the pool: a sliding window's rings
run: u64 = 0, // a stream's state as a run of the pool's pages
snap: u64 = 0, // a kept snapshot's slot outside the pool

/// One layer's share of a page: the rows of the full group's layer a page holds.
pub fn layerPage(s: Shape) u64 {
return s.full.row * s.page_rows;
}

pub fn pageBytes(s: Shape) u64 {
return s.full.bytes() * s.page_rows;
}
};

/// The load report: a line of each group's bytes a row, rows a page and padding, apart from the startup line.
pub fn line(a: Allocator, s: Shape) ![]u8 {
var w: std.Io.Writer.Allocating = .init(a);
errdefer w.deinit();
const o = &w.writer;
const page = s.pageBytes();
try o.print("KV page groups: one page {f}, {d} rows of the full group; full: ", .{ size(page), s.page_rows });
try rowsPart(o, s.full, s);
if (s.window) |g| {
try o.writeAll("; window, outside the pool: ");
try rowsPart(o, g, s);
}
if (s.run > 0) try statePart(o, "state run, in the pool", s.run, page);
if (s.snap > 0) try statePart(o, "snapshot, outside the pool", s.snap, page);
return w.toOwnedSlice();
}

fn rowsPart(o: *std.Io.Writer, g: Group, s: Shape) !void {
const f = rows(g.row, s.layerPage());
if (g.kind == .full and !s.named) {
return o.print("layers not named, {f} a token, {d} rows a page, {f} padding ({d:.1} %)", .{ size(g.row), f.rows, size(f.pad), pct(f.pad, s.layerPage()) });
}
try o.print("{d} layer{s} x {f} a row", .{ g.layers, if (g.layers == 1) "" else "s", size(g.row) });
if (g.kind != .full) try o.print(" ({d:.2}x a full row)", .{ratio(g.row, s.full.row)});
try o.print(", {d} rows a layer's page, {f} padding ({d:.1} %)", .{ f.rows, size(f.pad), pct(f.pad, s.layerPage()) });
}

fn statePart(o: *std.Io.Writer, what: []const u8, bytes: u64, page: u64) !void {
const f = state(bytes, page);
try o.print("; {s}: {f} on {d} page{s}, {f} padding ({d:.1} %)", .{ what, size(bytes), f.pages, if (f.pages == 1) "" else "s", size(f.pad), pct(f.pad, f.pages * page) });
}

fn ratio(x: u64, y: u64) f64 {
return @as(f64, @floatFromInt(x)) / @as(f64, @floatFromInt(y));
}

fn pct(pad: u64, of: u64) f64 {
return if (of == 0) 0 else 100 * ratio(pad, of);
}

/// Bytes as B, KiB or MiB.
const Size = struct {
bytes: u64,
pub fn format(z: Size, o: *std.Io.Writer) !void {
if (z.bytes < 1 << 10) return o.print("{d} B", .{z.bytes});
if (z.bytes < 1 << 20) return o.print("{d:.2} KiB", .{ratio(z.bytes, 1 << 10)});
return o.print("{d:.2} MiB", .{ratio(z.bytes, 1 << 20)});
}
};

fn size(bytes: u64) Size {
return .{ .bytes = bytes };
}
107 changes: 107 additions & 0 deletions zig/src/core/kv_groups_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//! Groups sized by bytes: rows a page, state pages and padding, one group == today's layout, the load report.
const std = @import("std");
const kv = @import("kv_pool.zig");
const groups = @import("kv_groups.zig");
const t = std.testing;

test "a page holds whole rows of a group, and the tail past them is its padding" {
try t.expectEqual(groups.Fit{ .rows = 3, .pages = 1, .pad = 100 }, groups.rows(300, 1000));
try t.expectEqual(groups.Fit{ .rows = 4, .pages = 1, .pad = 0 }, groups.rows(250, 1000));
try t.expectEqual(groups.Fit{ .rows = 0, .pages = 1, .pad = 1000 }, groups.rows(1001, 1000));
}

test "a state takes whole pages, the last one's tail padding" {
try t.expectEqual(groups.Fit{ .rows = 0, .pages = 3, .pad = 500 }, groups.state(2500, 1000));
try t.expectEqual(groups.Fit{ .rows = 0, .pages = 2, .pad = 0 }, groups.state(2000, 1000));
try t.expectEqual(groups.Fit{ .rows = 0, .pages = 1, .pad = 999 }, groups.state(1, 1000));
try t.expectEqual(groups.Fit{ .rows = 0, .pages = 0, .pad = 0 }, groups.state(0, 1000));
}

test "a group splits its bytes a row over its layers only when they divide evenly" {
try t.expectEqual(groups.Group{ .kind = .window, .row = 16384, .layers = 50 }, groups.Group.split(.window, 50 * 16384, 50).?);
try t.expectEqual(@as(?groups.Group, null), groups.Group.split(.window, 50 * 16384 + 1, 50));
try t.expectEqual(@as(?groups.Group, null), groups.Group.split(.window, 0, 0));
}

/// Gemma 4's pages: 256 rows of each global layer; a sliding row `times` a global one.
fn gemma(times: u64) kv.Layout {
const row = 2 * 4 * 512 * 2;
var l = kv.Layout.ofRows(.{ .kind = .full, .row = row, .layers = 10 }, 256, 1 << 30, 0);
l.window = .{ .kind = .window, .row = times * row, .layers = 50 };
return l;
}

test "sliding rows 2x and 4x a global row fill a layer's page with half and a quarter the rows, no padding" {
const s2 = gemma(2).shape();
try t.expectEqual(groups.Fit{ .rows = 128, .pages = 1, .pad = 0 }, groups.rows(s2.window.?.row, s2.layerPage()));
const s4 = gemma(4).shape();
try t.expectEqual(groups.Fit{ .rows = 64, .pages = 1, .pad = 0 }, groups.rows(s4.window.?.row, s4.layerPage()));
try t.expectEqual(groups.Fit{ .rows = 256, .pages = 1, .pad = 0 }, groups.rows(s2.full.row, s2.layerPage()));
}

/// Today's layouts written by hand, each family's form: token bytes over every layer, one group.
const today = [_]kv.Layout{
.{ .fixed_bytes = 1 << 30, .token_bytes = 10 * 8192, .page_tokens = 256, .snap_bytes = 0 },
.{ .fixed_bytes = 180 << 20, .token_bytes = 13_056, .page_tokens = 512, .snap_bytes = 0 },
.{ .fixed_bytes = 64 << 20, .token_bytes = 20_480, .page_tokens = 256, .snap_bytes = 48 << 20, .run_bytes = 51 << 20 },
.{ .fixed_bytes = 0, .token_bytes = 1, .page_tokens = 4, .snap_bytes = 0, .run_bytes = 3 },
};

test "one group sized by bytes a row is today's layout: the same pages, runs, needs and plans" {
const by_rows = kv.Layout.ofRows(.{ .kind = .full, .row = 8192, .layers = 10 }, 256, 1 << 30, 0);
try t.expectEqual(today[0].pageBytes(), by_rows.pageBytes());
try t.expectEqual(today[0].token_bytes, by_rows.group().bytes());
for (today) |l| {
try t.expectEqual(l.token_bytes, l.group().bytes());
try t.expectEqual((l.run_bytes + l.pageBytes() - 1) / l.pageBytes(), l.runPages());
for ([_]u64{ 0, 1, 255, 256, 8191, 70_000 }) |n| try t.expectEqual(l.pages(n) + l.runPages(), l.need(n, 0, 0));
}
for ([_]u64{ 0, 1, 4096, 4097, 1 << 20 }) |run| {
var l = today[2];
l.run_bytes = run;
try t.expectEqual((run + l.pageBytes() - 1) / l.pageBytes(), l.runPages());
}
const b: kv.Budget = .{ .room = 40 << 30, .engine = 2 << 30, .lanes = 4, .cache = 4 << 30, .window = 8192 };
try t.expectEqual(try kv.plan(today[0], b), try kv.plan(by_rows, b));
}

test "the load report shows a Qwen state run's pages and padding at 256-row pages" {
var arena = std.heap.ArenaAllocator.init(t.allocator);
defer arena.deinit();
const shown = try groups.line(arena.allocator(), today[2].shape());
const want = "KV page groups: one page 5.00 MiB, 256 rows of the full group; full: layers not named, 20.00 KiB a token, " ++
"256 rows a page, 0 B padding (0.0 %); state run, in the pool: 51.00 MiB on 11 pages, 4.00 MiB padding " ++
"(7.3 %); snapshot, outside the pool: 48.00 MiB on 10 pages, 2.00 MiB padding (4.0 %)";
try t.expectEqualStrings(want, shown);
}

test "a family that doesn't name its layers shows bytes a token, never one layer; one named layer shows as one" {
var arena = std.heap.ArenaAllocator.init(t.allocator);
defer arena.deinit();
for (today) |l| {
try t.expect(!l.shape().named);
const shown = try groups.line(arena.allocator(), l.shape());
try t.expect(std.mem.indexOf(u8, shown, "layers not named") != null and std.mem.indexOf(u8, shown, " x ") == null);
}
const one = kv.Layout.ofRows(.{ .kind = .full, .row = 7168, .layers = 1 }, 512, 0, 0);
const want = "KV page groups: one page 3.50 MiB, 512 rows of the full group; full: 1 layer x 7.00 KiB a row, " ++
"512 rows a layer's page, 0 B padding (0.0 %)";
try t.expectEqualStrings(want, try groups.line(arena.allocator(), one.shape()));
}

test "the load report shows a window outside the pool by its bytes a row against a full row" {
var arena = std.heap.ArenaAllocator.init(t.allocator);
defer arena.deinit();
const shown = try groups.line(arena.allocator(), gemma(2).shape());
const want = "KV page groups: one page 20.00 MiB, 256 rows of the full group; full: 10 layers x 8.00 KiB a row, " ++
"256 rows a layer's page, 0 B padding (0.0 %); window, outside the pool: 50 layers x 16.00 KiB a row " ++
"(2.00x a full row), 128 rows a layer's page, 0 B padding (0.0 %)";
try t.expectEqualStrings(want, shown);
}

test "an uneven window row leaves its padding in each layer's page" {
var l = gemma(2);
l.window = .{ .kind = .window, .row = 3000, .layers = 2 };
const s = l.shape();
try t.expectEqual(groups.Fit{ .rows = 699, .pages = 1, .pad = 2097152 - 699 * 3000 }, groups.rows(3000, s.layerPage()));
}
27 changes: 26 additions & 1 deletion zig/src/core/kv_pool.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//! One pool of KV pages for every stream: a family's layout, page ids and their holders, and the streams' leases.
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const groups = @import("kv_groups.zig");

test {
_ = @import("kv_groups_test.zig");
}

/// A family's stream memory: a fixed part at any length, paged caches a token, and a kept snapshot's slot.
pub const Layout = struct {
Expand All @@ -14,6 +19,26 @@ pub const Layout = struct {
snap_bytes: u64,
/// Bytes a running stream takes from the pages beside its keys and values, as one run of whole pages (0: none).
run_bytes: u64 = 0,
/// Layers `token_bytes` spans, so a row is token_bytes / full_layers in each (0: not named, one row a token).
full_layers: u32 = 0,
/// A sliding window's rows outside the pool, by bytes a row a layer (null: none, or not named).
window: ?groups.Group = null,

/// A layout whose full group is `full`, sized by its bytes a row over its layers.
pub fn ofRows(full: groups.Group, page_tokens: u32, fixed_bytes: u64, snap_bytes: u64) Layout {
return .{ .fixed_bytes = fixed_bytes, .token_bytes = full.bytes(), .page_tokens = page_tokens, .snap_bytes = snap_bytes, .full_layers = full.layers };
}

/// The pool's one group: every row of every paged layer.
pub fn group(l: Layout) groups.Group {
const n = @max(l.full_layers, 1);
return .{ .kind = .full, .row = l.token_bytes / n, .layers = n };
}

/// The groups the load report shows: the pool's, the window outside it, the state run and a snapshot slot.
pub fn shape(l: Layout) groups.Shape {
return .{ .full = l.group(), .named = l.full_layers > 0, .page_rows = l.page_tokens, .window = l.window, .run = l.run_bytes, .snap = l.snap_bytes };
}

pub fn pageBytes(l: Layout) u64 {
return l.token_bytes * l.page_tokens;
Expand All @@ -31,7 +56,7 @@ pub const Layout = struct {

/// Consecutive pages a running stream's `run_bytes` take.
pub fn runPages(l: Layout) usize {
return @intCast((l.run_bytes + l.pageBytes() - 1) / l.pageBytes());
return @intCast(groups.state(l.run_bytes, l.pageBytes()).pages);
}
};

Expand Down
7 changes: 6 additions & 1 deletion zig/src/families/gemma4/cuda_pages.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ pub fn pageBytes(m: anytype) u64 {

pub fn layout(m: anytype) kv.Layout {
var fixed: u64 = perSlot(m) * 4;
var sliding: u64 = 0; // bytes a row over the sliding layers' rings
for (0..ownLayers(m)) |i| if (!cfg.global(i)) {
fixed += 2 * m.cacheLen(i) * m.geo.kvHeads(i) * cfg.headDim(i) * 2;
sliding += 2 * m.geo.kvHeads(i) * cfg.headDim(i) * 2;
};
return .{ .fixed_bytes = fixed, .token_bytes = pageBytes(m) / tokens, .page_tokens = tokens, .snap_bytes = 0 };
const n: u32 = @intCast(globalLayers(m));
var l = kv.Layout.ofRows(.{ .kind = .full, .row = 2 * m.geo.global_kv_heads * cfg.global_dim * 2, .layers = n }, tokens, fixed, 0);
l.window = kv.groups.Group.split(.window, sliding, @intCast(ownLayers(m) - n));
return l;
}

pub fn view(m: anytype, slot: ?usize) ?cuda.window_attention.Pages {
Expand Down
11 changes: 11 additions & 0 deletions zig/src/families/kolibri1/forward.zig
Original file line number Diff line number Diff line change
Expand Up @@ -579,3 +579,14 @@ test "the f64 angle table against core's words: 19 of 64 differ, first at 7" {
try std.testing.expectEqual(@as(?usize, 7), first);
try std.testing.expectEqual(@as(u32, 1), max_ulp);
}

test "a page's bytes: the pool's layout and the kernels' stride agree" {
const config = @import("config.zig");
var why: config.Why = .{};
var m: Model = undefined;
m.c = try config.parse(std.testing.allocator, config.pinned, &why);
m.context = 32768;
m.ring = ringSize(prompt_rows, m.c.window);
try std.testing.expectEqual(m.pageBytes(), m.layout().pageBytes());
try std.testing.expectEqual(@as(u32, 10), m.layout().group().layers);
}
6 changes: 5 additions & 1 deletion zig/src/families/kolibri1/paging.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ pub fn layout(m: *const Model) kv.Layout {
for (c.full[0..c.layers]) |full| if (!full) {
fixed += 2 * m.ring * c.kv_heads * c.head_dim * 2;
};
return .{ .fixed_bytes = fixed, .token_bytes = m.pageBytes() / page_tokens, .page_tokens = page_tokens, .snap_bytes = 0 };
const row: u64 = 2 * c.kv_heads * c.head_dim * 2; // K and V a layer, full and sliding alike
const n = fullLayers(m);
var l = kv.Layout.ofRows(.{ .kind = .full, .row = row, .layers = @intCast(n) }, page_tokens, fixed, 0);
if (c.layers > n) l.window = .{ .kind = .window, .row = row, .layers = @intCast(c.layers - n) };
return l;
}

/// Slots a backend holds: the model's, or (paged) as many as a round takes.
Expand Down
Loading
Loading