Skip to content
Closed
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 @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- Core: Drama 3's `torch.save` reader moves to `core/torch_pth.zig` (its zip container to `core/torch_zip.zig`) and its WAV code to `core/wav.zig`; the reader also opens a root of nested state dicts, naming tensors by their keys joined with `/` (`openNested`), and returns a tensor's stored bytes at a given dtype and shape (`readRaw`), with host tests; Drama 3's generated and served audio stays byte-identical on Metal (#3719, for #3615). Thanks to @BobClawblaw.

- Qwen 3.5/3.6/3.8 on HIP: a prompt chunk before the last stops its last layer after the keys and values, and the final chunk runs that layer only over the rows that are read, through the shared prompt-tail rule CUDA and Metal use (#2845, for #2829).

- Qwen3.8-27B on Metal: on a prompt's last chunk, the last layer's q projection, o projection, MLP and final norm run only over the 128-row block that holds the row the head reads, while every row still writes its keys, values and recurrent state; a chunk whose last layer has another reader (drafter taps, images, MoE, a tensor-parallel pair, the ANE share) runs every row as before, and a new `test-qwen27-prompt-tail` step checks the block byte for byte against an every-row pass (#2826, refs #2165). Thanks to @GumbiiDigital.
Expand Down Expand Up @@ -117,6 +119,7 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel
- CUDA: with `TF_FLASHNEXT_KV_PAGES` on Linux, the Flash Next page pool no longer takes its whole arena at load: its 256 MiB pieces are mapped as streams first reach them, and a helper thread maps the next ones between requests, so at 262,144 x 3 on a GB10 about 14 GiB more stays free after load (MemAvailable 40.8-41.6 GiB against 26.5-27.0). Replies are unchanged; without `TF_FLASHNEXT_KV_PAGES`, and on other systems, nothing changes (#3066). Thanks to @Arminova.

## 1.3.0
- CUDA lanes: a one-stream reply keeps the draft depth it priced until another depth wins by 5% on the expected tokens a millisecond, and a probe one draft deeper is verified instead of being trimmed by the lone window's own prices; one noisy round no longer leaves a reply at one draft for the rest of it (#3468).

- CLI: `tensorfold models` lists the chat checkpoints from the supported-models guide that fit this machine, largest first, with the memory each needs, a quality score from TensorFold Web's quality suite (receipts in `docs/receipts/quality-suite`) and a rough one-stream speed (measured on this chip, or estimated from another chip by memory bandwidth); `--all` adds the rest, `--json` adds `machine` and `catalog` to schema 1, and the cached checkpoints follow as before (#3483).

Expand Down
4 changes: 2 additions & 2 deletions zig/build/drama3_metal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const std = @import("std");
const dir = "zig/kernels/metal/drama3";
const sources = [_][]const u8{ "dense.metal", "gdn.metal", "attn.metal", "moe.metal", "nvfp4.metal", "guidance.metal", "sample.metal", "codec.metal" };

/// Drama in the Metal engines: its `core` is the engine's safetensors, the server's tokenizer and frame_rounds.
/// Drama in the Metal engines: its `core` is the engine's safetensors, torch_pth and wav, the tokenizer, frame_rounds.
pub fn addNative(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, metal: *std.Build.Module, native: *std.Build.Module, metal_std: []const u8) void {
const tokenizer = native.import_table.get("tokenizer") orelse tok: {
const t = b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tokenizer.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true });
native.addImport("tokenizer", t);
break :tok t;
};
const rounds = b.createModule(.{ .root_source_file = b.path("zig/src/core/frame_rounds.zig"), .target = target, .optimize = optimize });
const shim = b.addWriteFiles().add("drama3_core.zig", "pub const safetensors = @import(\"tensorfold\").safetensors;\npub const tokenizer = @import(\"tokenizer\");\npub const frame_rounds = @import(\"frame_rounds\");\n");
const shim = b.addWriteFiles().add("drama3_core.zig", "pub const safetensors = @import(\"tensorfold\").safetensors;\npub const tokenizer = @import(\"tokenizer\");\npub const frame_rounds = @import(\"frame_rounds\");\npub const torch_pth = @import(\"tensorfold\").torch_pth;\npub const wav = @import(\"tensorfold\").wav;\n");
const core = b.createModule(.{ .root_source_file = shim, .target = target, .optimize = optimize, .imports = &.{
.{ .name = "tensorfold", .module = native.import_table.get("tensorfold").? },
.{ .name = "tokenizer", .module = tokenizer },
Expand Down
2 changes: 1 addition & 1 deletion zig/build/residency_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn compileHost(b: *std.Build, metal: *std.Build.Module, engine: *std.Build.M
const bytes = b.createModule(.{ .root_source_file = b.addWriteFiles().add("residency-drama-empty.zig", "pub const bytes: []const u8 = &.{};\n") });
const tokenizer = b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tokenizer.zig"), .target = b.graph.host, .link_libc = true });
const rounds = b.createModule(.{ .root_source_file = b.path("zig/src/core/frame_rounds.zig"), .target = b.graph.host });
const shim = b.addWriteFiles().add("residency-drama-core.zig", "pub const safetensors = @import(\"tensorfold\").safetensors;\npub const tokenizer = @import(\"tokenizer\");\npub const frame_rounds = @import(\"frame_rounds\");\n");
const shim = b.addWriteFiles().add("residency-drama-core.zig", "pub const safetensors = @import(\"tensorfold\").safetensors;\npub const tokenizer = @import(\"tokenizer\");\npub const frame_rounds = @import(\"frame_rounds\");\npub const torch_pth = @import(\"tensorfold\").torch_pth;\npub const wav = @import(\"tensorfold\").wav;\n");
const core = b.createModule(.{ .root_source_file = shim, .target = b.graph.host, .imports = &.{ .{ .name = "tensorfold", .module = engine }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "frame_rounds", .module = rounds } } });
const drama = b.createModule(.{ .root_source_file = b.path("zig/src/families/drama3/drama3_metal.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "core", .module = core }, .{ .name = "drama3_metallib", .module = bytes } } });
host.addImport("core", core);
Expand Down
2 changes: 1 addition & 1 deletion zig/build/slide_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn metal(b: *std.Build, m: anytype) void {
const tokenizer = b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tokenizer.zig"), .target = target, .link_libc = true });
const rounds = b.createModule(.{ .root_source_file = b.path("zig/src/core/frame_rounds.zig"), .target = target });
const files = b.addWriteFiles();
const core = b.createModule(.{ .root_source_file = files.add("slide-drama-core.zig", "pub const safetensors = @import(\"tensorfold\").safetensors;\npub const tokenizer = @import(\"tokenizer\");\npub const frame_rounds = @import(\"frame_rounds\");\n"), .target = target, .imports = &.{ .{ .name = "tensorfold", .module = m.engine }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "frame_rounds", .module = rounds } } });
const core = b.createModule(.{ .root_source_file = files.add("slide-drama-core.zig", "pub const safetensors = @import(\"tensorfold\").safetensors;\npub const tokenizer = @import(\"tokenizer\");\npub const frame_rounds = @import(\"frame_rounds\");\npub const torch_pth = @import(\"tensorfold\").torch_pth;\npub const wav = @import(\"tensorfold\").wav;\n"), .target = target, .imports = &.{ .{ .name = "tensorfold", .module = m.engine }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "frame_rounds", .module = rounds } } });
const bytes = b.createModule(.{ .root_source_file = files.add("slide-inert-metallib.zig", "pub const bytes: []const u8 = &.{};\n") });
native.addImport("kimi_k3", @import("metal_targets.zig").kimiModule(b, target, .Debug, m.metal, m.engine, m.lanes));
native.addImport("kimi_metallib", bytes);
Expand Down
46 changes: 35 additions & 11 deletions zig/src/core/lanes/depth.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const copy_price = @import("copy_price.zig");
pub const State = struct {
p: []f64,
rounds: u64 = 0,
last: ?i64 = null, // the depth this rule last chose: another must beat it by depth_margin
probe: ?i64 = null, // a probe's depth for the next round (a lone window keeps it)
plain: ?u64 = null,
wait: ?u64 = null,
ms: ?Table = null, // plain_guard: the stream's own round times
Expand Down Expand Up @@ -173,18 +175,33 @@ pub const Rule = struct {
const want: i64 = if (p[0] < 0.8) 1 else if (p[0] < 0.9) 2 else 3;
return @max(1, @min(most, want));
}
var best = (try r.bestRows(who, p, most, false)).rows;
const st = &who.state.*.?;
const st = try r.state(who);
var best = (try r.bestRows(who, p, most, false, st.last)).rows;
st.rounds += 1;
if (best == 0) return @intFromBool(r.probePlain(st));
if (best == 0) {
st.last = null; // plain holds no depth: the next drafted depth is chosen fresh
st.probe = null;
return @intFromBool(r.probePlain(st));
}
st.plain = 0;
st.wait = r.k.depth_probe_every;
if (best < most and st.rounds % r.k.depth_probe_every == 0) best += 1;
st.last = best;
st.probe = null;
if (best < most and st.rounds % r.k.depth_probe_every == 0) {
best += 1;
st.probe = best;
}
return best;
}

/// The rows with the most expected tokens a ms (0: plain) and the best of one or more, from each row's chance.
fn bestRows(r: *Rule, who: Who, chances: []const f64, most: i64, comptime copy: bool) !struct { rows: i64, drafted: i64 } {
/// The stream's estimate state, allocated from its prior on first use.
fn state(r: *Rule, who: Who) !*State {
_ = try r.rates(who);
return &who.state.*.?;
}

/// The rows with the most expected tokens a ms (0: plain); `hold` stays unless another beats it by depth_margin.
fn bestRows(r: *Rule, who: Who, chances: []const f64, most: i64, comptime copy: bool, hold: ?i64) !struct { rows: i64, drafted: i64 } {
var best: i64 = 1;
var best_rate: f64 = -1.0;
if (copy or r.plain_guard) {
Expand All @@ -197,6 +214,7 @@ pub const Rule = struct {
}
var drafted: i64 = 1;
var drafted_rate: f64 = -1.0;
var held_rate: f64 = -1.0;
var expected: f64 = 1.0;
var run: f64 = 1.0;
var d: i64 = 1;
Expand All @@ -205,15 +223,21 @@ pub const Rule = struct {
const at: usize = @intCast(d - 1);
run *= if (at < chances.len) chances[at] else chances[chances.len - 1];
expected += run;
if (expected / cost > best_rate) {
const rate = expected / cost;
if (hold != null and d == hold.?) held_rate = rate;
if (rate > best_rate) {
best = d;
best_rate = expected / cost;
best_rate = rate;
}
if (expected / cost > drafted_rate) {
if (rate > drafted_rate) {
drafted = d;
drafted_rate = expected / cost;
drafted_rate = rate;
}
}
// a noisy round must not flip a held depth: another wins it only by depth_margin
if (hold) |h| {
if (best != 0 and h >= 1 and h <= most and h != best and held_rate >= 0.0 and best_rate < held_rate * r.k.depth_margin) best = h;
}
return .{ .rows = best, .drafted = drafted };
}

Expand All @@ -236,7 +260,7 @@ pub const Rule = struct {
pub fn copyRows(r: *Rule, who: Who, chances: []const f64, rows: i64, take: bool) !i64 {
if (rows <= 0 or !r.copy_guard or r.family_costs.empty()) return rows;
if ((try copy_price.plain(r, who)) == null) return rows;
const best = try r.bestRows(who, chances, rows, true);
const best = try r.bestRows(who, chances, rows, true, null);
const st = &who.state.*.?;
if (best.rows > 0) {
if (take) {
Expand Down
1 change: 1 addition & 0 deletions zig/src/core/lanes/depth_constants.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub const Constants = struct {
depth_probe_every: u64 = 8, // rounds between probes one depth deeper
cost_rate: f64 = 0.2, // weight of the newest round in a depth's measured time
plain_margin: f64 = 1.05, // a drafted depth must beat plain rounds' tokens a ms by this much
depth_margin: f64 = 1.05, // another depth must beat the one a stream holds by this much: a round's estimate is noisy
plain_wait_most: u64 = 128, // plain rounds between probes at most
draft_slack: i64 = 2, // drafts a stream offers past what its last shared round granted it
grant_slack: i64 = 1, // granted depths: drafts past a grant that verified every draft the stream held
Expand Down
67 changes: 67 additions & 0 deletions zig/src/core/lanes/depth_noise_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//! The depth rule against a round's noise, and the probe a lone window must keep for it to recover.
const std = @import("std");
const depth = @import("depth.zig");
const win = @import("windows.zig");
const Stream = @import("stream.zig").Stream;
const Table = @import("table.zig").Table;
const t = std.testing;

fn rule(gpa: std.mem.Allocator, family: *const Table, shared: *const Table, most: u32, prior: []const f64) !depth.Rule {
return .{ .gpa = gpa, .prior = prior, .most_drafts = most, .family_costs = family, .shared_costs = shared, .mtp_step_ms = 0.5, .plain_guard = true, .node_probabilities = false, .batch_rows = 8 };
}

test "depth: a noisy round does not take a stream off the depth it holds" {
const gpa = t.allocator;
var family: Table = .{};
defer family.deinit(gpa);
for ([_]f64{ 16, 20, 24 }, 1..) |ms, rows| try family.put(gpa, @intCast(rows), ms); // forward ms by rows
const shared: Table = .{};
var r = try rule(gpa, &family, &shared, 2, &.{ 0.72, 0.54 });
defer r.deinit();
var st: ?depth.State = null;
defer if (st) |*s| s.deinit(gpa);
const who: depth.Who = .{ .state = &st, .draft_room = 100, .finished = false, .forced = false };
try r.observeCost(1, 18.1, false, who);
try r.observeCost(2, 20.8, false, who);
try t.expectEqual(@as(i64, 2), try r.depth(who)); // 1.72/18.1 against 2.1088/20.8: two drafts lead
// a round inflated by the probe that followed it: two drafts now trail one, but by less than the margin
for (0..12) |_| try r.observeCost(2, 22.65, false, who);
try t.expectEqual(@as(i64, 2), try r.depth(who));
// a depth that keeps losing by more than the margin still gives way
try r.observeCost(2, 30.0, false, who);
try t.expectEqual(@as(i64, 1), try r.depth(who));
}

test "depth: a probe asks one draft past the depth a stream holds" {
const gpa = t.allocator;
var family: Table = .{};
defer family.deinit(gpa);
for ([_]f64{ 16, 20, 24, 30 }, 1..) |ms, rows| try family.put(gpa, @intCast(rows), ms);
const shared: Table = .{};
var r = try rule(gpa, &family, &shared, 3, &.{ 0.72, 0.54, 0.5 });
defer r.deinit();
var st: ?depth.State = null;
defer if (st) |*s| s.deinit(gpa);
const who: depth.Who = .{ .state = &st, .draft_room = 100, .finished = false, .forced = false };
try r.observeCost(1, 18.1, false, who);
try r.observeCost(2, 20.8, false, who);
try r.observeCost(3, 25.0, false, who);
for (1..8) |_| try t.expectEqual(@as(i64, 2), try r.depth(who));
try t.expectEqual(@as(i64, 3), try r.depth(who)); // every eighth round probes one deeper
try t.expectEqual(@as(?i64, 3), st.?.probe);
}

test "windows: a lone window keeps the probe a depth rule asked for" {
var s: Stream = undefined;
const probed: win.Plan = .{ .stream = &s, .position = 0, .kind = .head, .held = 2, .probe_rows = 2 };
var counts = [_]u32{1};
win.keepProbe(&probed, &counts);
try t.expectEqual(@as(u32, 2), counts[0]);
counts[0] = 3;
win.keepProbe(&probed, &counts); // an allocator already deeper than the probe keeps its own choice
try t.expectEqual(@as(u32, 3), counts[0]);
const priced: win.Plan = .{ .stream = &s, .position = 0, .kind = .head, .held = 2 };
counts[0] = 1;
win.keepProbe(&priced, &counts);
try t.expectEqual(@as(u32, 1), counts[0]);
}
1 change: 1 addition & 0 deletions zig/src/core/lanes/lanes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ test {
_ = @import("grammar_parallel_test.zig");
_ = @import("pricing_test.zig");
_ = @import("depth_probe_test.zig");
_ = @import("depth_noise_test.zig");
_ = @import("proposal_external_test.zig");
_ = @import("lone_costs_test.zig");
_ = @import("depth_floor_test.zig");
Expand Down
14 changes: 7 additions & 7 deletions zig/src/core/lanes/lone_copy_price_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,22 @@ fn digest(got: Run) [5]u64 {
return .{ got.rounds, got.drafted, got.accepted, got.copies[0] + got.copies[1], got.copy_rows };
}

/// Values from main e7e24243d, before lone copies were priced (the same configurations, clocks and copier).
/// Values from main e7e24243d, before lone copies were priced; re-pinned where the depth margin holds a depth (#3468).
fn pinned(expected: [5]u64, got: Run) !void {
try std.testing.expectEqual(expected, digest(got));
}

test "lone copy price: head windows without a copy proposed keep main's depths" {
var silent = hip(true);
silent.copy_drafts = false; // no copy is ever proposed
try pinned(.{ 40, 82, 55, 0, 0 }, try run(silent, long_context, .{}, 1, 96));
try pinned(.{ 39, 138, 56, 0, 0 }, try run(silent, short_context, .{}, 1, 96));
try pinned(.{ 40, 84, 55, 0, 0 }, try run(silent, long_context, .{}, 1, 96));
try pinned(.{ 39, 139, 56, 0, 0 }, try run(silent, short_context, .{}, 1, 96));
}

test "lone copy price: root copies and GLM's own copy rates stay as on main" {
var root = hip(true);
root.root_copies = true;
try pinned(.{ 52, 424, 43, 26, 365 }, try run(root, long_context, .{}, 1, 96));
try pinned(.{ 52, 423, 43, 26, 366 }, try run(root, long_context, .{}, 1, 96));
var glm = root; // lanes/../glm/cuda_copy_policy.zig: root and context copies priced by each stream's own rate
glm.context_copies = true;
glm.own_copy_rates = true;
Expand Down Expand Up @@ -458,16 +458,16 @@ test "lone copy price: kept copy prices need two timed copies on a lone stream,
}

test "lone copy price: engines that leave copies unpriced decide two requests in a row as before" {
// e5478c4ab: the trail of two requests in a row on one engine, hashed, where the copy gate is off
// e5478c4ab: two requests in a row on one engine, hashed, copy gate off; re-pinned for the depth margin (#3468)
var tile = hip(true);
tile.lone_cost_tile = 2; // CUDA's lone window prices, which keep their own calibration (lanes/lone_costs.zig)
var head = hip(true);
head.copy_guard = false;
var copies = hip(false);
copies.copy_guard = false;
const cases = [_]struct { m: cfgs.Model, expected: [2]u64 }{
.{ .m = tile, .expected = .{ 14840352178686162370, 75 } },
.{ .m = head, .expected = .{ 12942564931587194512, 69 } },
.{ .m = tile, .expected = .{ 14952552964951112534, 75 } },
.{ .m = head, .expected = .{ 2990995193398607308, 69 } },
.{ .m = copies, .expected = .{ 14652143946053147409, 62 } },
};
for (cases) |c| {
Expand Down
Loading