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

- Qwen 3.5, 3.6 and 3.8 on CUDA: a served one-row step no longer copies the recurrent state for a rollback that can't happen, so decode is 1-2% faster at one stream and about 4% at four on a GB10, with the same tokens (#2933, fixes #2908).

- Metal: Recursive's Flint (40B-A2.5B MoE) is served natively from `RecursiveSI/flint-int4` on M5-class Macs, with its kernels and 2048-wide router under the kernel contract, a 262,144-token window, copy drafts and the prompt cache; `FLINT_LOAD_LIMIT_GB` caps its memory, and Step-5 now shares the same load budget, so a `STEP5_LOAD_LIMIT_GB` above the Mac's RAM is refused by name (#1177, fixes #1175). Thanks to @BobClawblaw.

- CI: a newer push to main cancels main's older No GPU run (a landing train pushes one commit a PR, and the train's own run has tested the tree), the nightly keeps its own run, and the app checks run on pushes to main only (#3230).
Expand Down
4 changes: 2 additions & 2 deletions zig/dense_serving_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ test "dense serving: owned CUDA images retain their reviewed lane and decode byt
try std.testing.expectEqualStrings("ceff4ee13e97b5aa93cbe7a1edc9a524c78aaa145dd1e5c82dcb05a3011aae23", &hash(group[0..16095]));
try std.testing.expectEqualStrings("8780fd710be3ce2f04e53fa1e69eb062809d3bec6391d9bd49983d0588e374d0", &hash(group));
try std.testing.expectEqualStrings("45ab2be277c7c17e6cdc6041db45611f2554c6e88f09a295e88751b1c51be1f5", &hash(@embedFile("kernels/cuda/qmm_prefill.cu")));
// 01b45cd1d's bytes: tf_q35_norm_shift and tf_q35_head_norm_shift for BF16 checkpoints, under the contract (#2383)
try std.testing.expectEqualStrings("7438579264a99fd0ea99ac242afc68015c8b13a8cd3f10c43b93644376b87205", &hash(@embedFile("kernels/cuda/qwen35.cu")));
// #2933's bytes: served one-row steps skip the rollback copy nothing reads (#2908); replies unchanged
try std.testing.expectEqualStrings("f16fa3d48336cfe7bcdc071ef791247d8ef3c06e26a9eaa95a54e1febd803332", &hash(@embedFile("kernels/cuda/qwen35.cu")));
try std.testing.expectEqualStrings("7d916f0bb75d636ed15d8f30a7108cf05a6c5a833bd76beb8b627e6567a641db", &hash(@embedFile("kernels/cuda/qwen35.cuh")));
// 67711a6aa7's bytes: the opt-in DSpine draft kernels (#2633); default serving unchanged
try std.testing.expectEqualStrings("df06cb0ef07d12ca2296f7d1691678d0649a148f4abcbb3cb69f0d839e45295c", &hash(@embedFile("kernels/cuda/qwen35_draft.cu")));
Expand Down
6 changes: 4 additions & 2 deletions zig/kernels/cuda/qwen35.cu
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,10 @@ extern "C" __global__ void tf_q35_conv_commit(Geo geo, const __nv_bfloat16* __re
if (recorded(P)) {
// gdn_pre logged each row's tail
if (sg.rows != 1) return;
__nv_bfloat16* before = reinterpret_cast<__nv_bfloat16*>(sg.before + geo.nl * geo.rec() * sizeof(float)) + (size_t)layer * geo.conv_rows();
for (int r = 0; r < TAPS - 1; ++r) before[(size_t)r * C + c] = CS[(size_t)r * C + c];
if (sg.before != 0) {
__nv_bfloat16* before = reinterpret_cast<__nv_bfloat16*>(sg.before + geo.nl * geo.rec() * sizeof(float)) + (size_t)layer * geo.conv_rows();
for (int r = 0; r < TAPS - 1; ++r) before[(size_t)r * C + c] = CS[(size_t)r * C + c];
}
}
// ascending: slot r takes [r + rows], which is either a later state slot (not yet written) or a row
for (int r = 0; r < TAPS - 1; r++) {
Expand Down
2 changes: 1 addition & 1 deletion zig/kernels/cuda/qwen35_slots.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ __device__ __forceinline__ void q35_slots(Geo geo, const __nv_bfloat16* q, const
}
}
}
if (sg.rows == 1)
if (sg.rows == 1 && sg.before != 0)
#pragma unroll
for (int r = 0; r < R; ++r)
#pragma unroll
Expand Down
59 changes: 47 additions & 12 deletions zig/qwen35_recurrent_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn finish(e: *q.engine.Engine, c: *q.engine.Cache) void {
fn forwardDone(e: *q.engine.Engine, c: *q.engine.Cache, rows: usize) !void {
try rec.mark(c, false);
if (rows == 1) try rec.mark(c, true);
c.advanced(rows, true);
c.advanced(rows, true, true);
_ = Driver.sync(e.stream.handle);
rec.completed(e);
}
Expand Down Expand Up @@ -128,10 +128,10 @@ test "Shared verification: all window transitions keep log capacity within the o
for (1..65) |old| for (1..65) |rows| {
try c.reset(&e);
rec.completed(&e);
try rec.prepare(&e, &c, old, true);
try rec.prepare(&e, &c, old, true, true);
try forwardDone(&e, &c, old);
if (old == 1) try c.keep(&e, &.{}) else try c.keep(&e, &.{0});
try rec.prepare(&e, &c, rows, true);
try rec.prepare(&e, &c, rows, true, true);
try std.testing.expect(c.log.len >= e.g.logBytes(rows));
try std.testing.expect((c.before != null) == (rows == 1));
try std.testing.expect(e.rec_scratch.residentBytes() <= bound);
Expand All @@ -141,7 +141,7 @@ test "Shared verification: all window transitions keep log capacity within the o
try forwardDone(&e, &c, rows);
if (rows == 1) {
try c.settle(&e);
try rec.prepare(&e, &c, 1, true);
try rec.prepare(&e, &c, 1, true, true);
try std.testing.expectEqual(e.g.logBytes(1), c.log.len);
}
};
Expand All @@ -156,7 +156,7 @@ test "Shared verification: a real empty keep retains its snapshot until the queu
engine(&e, &d);
var c = try q.engine.Cache.init(a, &e, 128);
defer finish(&e, &c);
try rec.prepare(&e, &c, 1, true);
try rec.prepare(&e, &c, 1, true, true);
try forwardDone(&e, &c, 1);
const before = c.before.?.ptr;
try c.keep(&e, &.{});
Expand All @@ -174,6 +174,41 @@ test "Shared verification: a real empty keep retains its snapshot until the queu
try std.testing.expectEqual(@as(usize, 0), e.rec_scratch.residentBytes());
}

test "Shared verification: a served one-row step leases no rollback copy and an empty keep refuses" {
Driver.reset();
var d: cuda.Driver = undefined;
var e: q.engine.Engine = undefined;
e.kit = null;
engine(&e, &d);
var c = try q.engine.Cache.init(a, &e, 2);
defer finish(&e, &c);
// a fresh cache: no copy leased, and the staged segment reads none
try rec.prepare(&e, &c, 1, true, false);
try std.testing.expect(c.before == null);
try std.testing.expectEqual(@as(u64, 0), q.engine.Engine.stagedSeg(&c, &e, c.len, 0, 1, false).before);
try rec.mark(&c, false);
c.advanced(1, true, false);
_ = Driver.sync(e.stream.handle);
rec.completed(&e);
try std.testing.expectError(error.QwenNoRollbackCopy, c.keep(&e, &.{}));
try std.testing.expectEqual(@as(usize, 1), c.len);
try c.keep(&e, &.{0});
try std.testing.expectEqual(@as(usize, 1), c.len);
try std.testing.expect(c.last == null);
// an earlier window's lease stays in the cache; the served step still stages no copy of it
try rec.prepare(&e, &c, 1, true, true);
try std.testing.expect(c.before != null);
try rec.prepare(&e, &c, 1, true, false);
try std.testing.expect(c.before != null);
try std.testing.expectEqual(@as(u64, 0), q.engine.Engine.stagedSeg(&c, &e, c.len, 0, 1, false).before);
try rec.mark(&c, false);
c.advanced(1, true, false);
_ = Driver.sync(e.stream.handle);
rec.completed(&e);
try std.testing.expectError(error.QwenNoRollbackCopy, c.keep(&e, &.{}));
try std.testing.expectEqual(@as(usize, 2), c.len);
}

test "Shared verification: allocation refusal retains a queued keep's old log owner" {
Driver.reset();
var d: cuda.Driver = undefined;
Expand All @@ -187,16 +222,16 @@ test "Shared verification: allocation refusal retains a queued keep's old log ow
var other_driver: cuda.Driver = undefined;
engine(&foreign, &other_driver);
defer rec.shutdown(&foreign);
try std.testing.expectError(error.ForeignRecurrentQueue, rec.prepare(&foreign, &c, 8, true));
try std.testing.expectError(error.InvalidRecurrentWindow, rec.prepare(&e, &c, 65, true));
try std.testing.expectError(error.ForeignRecurrentQueue, rec.prepare(&foreign, &c, 8, true, true));
try std.testing.expectError(error.InvalidRecurrentWindow, rec.prepare(&e, &c, 65, true, true));
try std.testing.expectEqual(@as(usize, 0), e.rec_scratch.residentBytes());
try rec.prepare(&e, &c, 64, true);
try rec.prepare(&e, &c, 64, true, true);
try forwardDone(&e, &c, 64);
try c.keep(&e, &.{0});
const usage = cuda.memory.usage(false);
const prior = try cuda.setAllocationLimit(usage.device + usage.host);
defer _ = cuda.setAllocationLimit(prior) catch unreachable;
try std.testing.expectError(error.OutOfDeviceMemory, rec.prepare(&e, &c, 32, true));
try std.testing.expectError(error.OutOfDeviceMemory, rec.prepare(&e, &c, 32, true, true));
try std.testing.expectEqual(e.g.logBytes(64), e.rec_scratch.residentBytes());
try std.testing.expectEqual(@as(usize, 0), Driver.unsafe_frees);
_ = Driver.sync(e.stream.handle);
Expand Down Expand Up @@ -227,12 +262,12 @@ test "Shared verification: streams cycling window widths reuse their scratch ins
for (0..4) |cycle| {
for (widths, 0..) |rows, i| {
const other = widths[(i + 3) % widths.len];
try rec.prepare(&e, &c, rows, true);
try rec.prepare(&e, &o, other, true);
try rec.prepare(&e, &c, rows, true, true);
try rec.prepare(&e, &o, other, true, true);
try forwardDone(&e, &c, rows);
try rec.mark(&o, false);
if (other == 1) try rec.mark(&o, true);
o.advanced(other, true);
o.advanced(other, true, true);
try std.testing.expect(e.rec_scratch.residentBytes() <= e.rec_scratch.cap);
}
if (cycle == 0) settled = Driver.allocs;
Expand Down
6 changes: 3 additions & 3 deletions zig/qwen35_shared_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ test "Shared verification: empty multi-row keep preserves the root and completed
var cache: q.engine.Cache = .{ .state = b, .kv = b, .log = b, .cap = 256, .len = 7, .logits = &.{} };
Driver.launches = 0;
Driver.copies = 0;
cache.advanced(64, true);
cache.advanced(64, true, true);
try cache.keep(&e, &.{});
try std.testing.expectEqual(@as(usize, 7), cache.len);
try std.testing.expectEqual(@as(usize, 0), Driver.copies);
cache.advanced(1, true);
cache.advanced(1, true, true);
try std.testing.expect(cache.last != null);
try cache.settle(&e);
try std.testing.expectEqual(@as(usize, 8), cache.len);
try std.testing.expect(cache.last == null);
try std.testing.expectEqual(@as(usize, 0), Driver.launches);
cache.advanced(128, false);
cache.advanced(128, false, true);
try std.testing.expect(cache.last == null);
try std.testing.expectEqual(@as(usize, 136), cache.len);
// the before copy sits inside the log's bytes (one overlaid block), so a stream holds one state beside it
Expand Down
9 changes: 5 additions & 4 deletions zig/src/families/qwen3_5/cuda_cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ pub const Cache = struct {
/// Positions the newest forward wrote up to: a tap ring holds them until the next forward, whatever `keep` lands.
written: usize = 0,
logits: []u16,
/// The newest verify window, until its path is kept: where it started, its rows.
last: ?struct { start: usize, rows: usize } = null,
/// The newest verify window, until its path is kept: where it started, its rows, and whether it copied `before`.
last: ?struct { start: usize, rows: usize, undo: bool = true } = null,
/// Kept rows (rows 0..pend-1 of `pbuf`) the state does not hold yet: recorded slot scans replay them.
pend: c_int = 0,
/// The pending heads (pendBytes), allocated at the first deferred keep.
Expand Down Expand Up @@ -252,8 +252,8 @@ pub const Cache = struct {
}

/// Publish a completed forward's metadata, including one-row slots that already advanced in place.
pub fn advanced(self: *Cache, rows: usize, record: bool) void {
self.last = if (record) .{ .start = self.len, .rows = rows } else null;
pub fn advanced(self: *Cache, rows: usize, record: bool, undo: bool) void {
self.last = if (record) .{ .start = self.len, .rows = rows, .undo = undo } else null;
self.len += rows;
self.written = self.len;
}
Expand Down Expand Up @@ -302,6 +302,7 @@ pub const Cache = struct {
const last = self.last orelse return error.NothingToKeep;
if (path.len == 0) {
if (last.rows == 1) {
if (!last.undo) return error.QwenNoRollbackCopy;
try recurrent.mark(self, true);
try self.state.copyFrom(0, (self.before orelse return error.MissingQwenBefore).ptr, e.g.stateBytes(), e.stream.handle);
}
Expand Down
19 changes: 13 additions & 6 deletions zig/src/families/qwen3_5/cuda_engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub const Mlp = layers.Mlp;
const Fns = layers.Fns;
pub const Keeps = layers.Keeps;
const Kv8 = layers.Kv8;
/// A window of one stream in a forward: `parents`, for a tree, names each row's parent row (-1: the first row).
pub const Part = struct { cache: *Cache, rows: usize, parents: ?[]const i32 = null, images: ?*const Images = null };
/// A window: for a tree `parents` names each row's parent (-1: first row); `undo` keeps a one-row rollback copy.
pub const Part = struct { cache: *Cache, rows: usize, parents: ?[]const i32 = null, images: ?*const Images = null, undo: bool = true };

/// An image prompt's rows for its prompt chunks: the staged tables' rows.
pub const Images = Round.Images;
Expand Down Expand Up @@ -433,6 +433,13 @@ pub const Engine = struct {
for (parts) |p| if (p.images != null and record) return error.InvalidQwenImages;
}

/// A window's staged segment: one-row windows that skip their rollback copy read no `before`.
pub fn stagedSeg(c: *Cache, e: *const Engine, len: usize, row0: usize, rows: usize, undo: bool) Seg {
var sg = c.fold(c.seg(e, len, row0, rows));
if (rows == 1 and !undo) sg.before = 0;
return sg;
}

/// One forward over `parts`' windows, with every input admitted before an unkept verify is settled.
pub fn forward(e: *Engine, parts: []const Part, ids: []const u32, keep: Keep) !void {
try e.admit(parts, ids, keep);
Expand All @@ -449,11 +456,11 @@ pub const Engine = struct {
try p.cache.settle(e);
// pending kept rows ride in a recorded slot scan; any other pass reads the state as written
if (!(record and keep != .hidden)) try p.cache.flush(e);
try recurrent.prepare(e, p.cache, p.rows, record);
try recurrent.prepare(e, p.cache, p.rows, record, p.undo);
}
var row0: usize = 0;
for (parts, 0..) |p, i| {
segs[i] = p.cache.fold(p.cache.seg(e, p.cache.len, row0, p.rows));
segs[i] = stagedSeg(p.cache, e, p.cache.len, row0, p.rows, p.undo);
row0 += p.rows;
}
if (keep == .draws) {
Expand Down Expand Up @@ -485,7 +492,7 @@ pub const Engine = struct {
var shape = Shape.init(total, parts.len, record, e.n_taps > 0, @intCast(Round.variant(slot_peak)));
if (record) for (parts) |p| {
try recurrent.mark(p.cache, false);
if (p.rows == 1) try recurrent.mark(p.cache, true);
if (p.rows == 1 and p.undo) try recurrent.mark(p.cache, true);
};
shape.hidden = keep == .hidden;
shape.rows = keep == .rows;
Expand All @@ -498,7 +505,7 @@ pub const Engine = struct {
e.features = if (images) |im| im.features else 0;
defer e.features = 0;
if (g.whole == 0) try e.launchShape(shape) else try stage_fwd.run(e, shape, total);
for (parts) |p| p.cache.advanced(p.rows, record);
for (parts) |p| p.cache.advanced(p.rows, record, p.undo);
if (!g.heads) return recurrent.completed(e); // a stage without the head: no logits to keep
if (e.held) |h| return h.hold(st, shape, total); // rank 0 with rounds in flight: its head runs at `land`
try e.land(parts, keep, total);
Expand Down
4 changes: 2 additions & 2 deletions zig/src/families/qwen3_5/cuda_native.zig
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ pub const Lanes = struct {
try self.e.penalties.window(self.gpa, stream, cache.len, token);
try self.e.penalties.row(self.gpa, 0, stream.penalties, &.{});
}
try self.e.forward(&.{.{ .cache = cache, .rows = 1 }}, &.{token}, .{ .draws = &.{pick(stream.sampling, position)} });
try self.e.forward(&.{.{ .cache = cache, .rows = 1, .undo = self.e.learning != null }}, &.{token}, .{ .draws = &.{pick(stream.sampling, position)} });
cache.last = null;
return self.e.tokens[0];
}
Expand Down Expand Up @@ -499,7 +499,7 @@ pub const Lanes = struct {
var masked = false;
while (at + n < windows.len and n < E.max_segs and total + windows[at + n].rows() <= self.e.verify_rows) : (n += 1) {
const w = windows[at + n];
parts[n] = .{ .cache = self.caches.get(w.stream).?, .rows = w.rows(), .parents = w.parents };
parts[n] = .{ .cache = self.caches.get(w.stream).?, .rows = w.rows(), .parents = w.parents, .undo = self.e.learning != null };
ids[total] = w.pending;
@memcpy(ids[total + 1 ..][0..w.tokens.len], w.tokens);
for (w.positions, 0..) |p, r| picks[total + r] = pick(w.stream.sampling, p);
Expand Down
2 changes: 1 addition & 1 deletion zig/src/families/qwen3_5/cuda_pend_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn release(c: *caches.Cache) void {

/// A recorded window of `most` rows, then `path` kept.
fn keepAfterWindow(e: *E, c: *caches.Cache, path: []const u32) !void {
c.advanced(most, true);
c.advanced(most, true, true);
try c.keep(e, path);
}

Expand Down
4 changes: 2 additions & 2 deletions zig/src/families/qwen3_5/cuda_recurrent.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ fn ensure(e: *Engine, c: *Cache, before: bool, bytes: usize, reuse_pending: bool
if (before) c.before = b else c.log = b;
}

pub fn prepare(e: *Engine, c: *Cache, rows: usize, record: bool) !void {
pub fn prepare(e: *Engine, c: *Cache, rows: usize, record: bool, undo: bool) !void {
const pool = c.owner orelse return;
if (pool != &e.rec_scratch) return error.ForeignRecurrentQueue;
if (!record) return release(c); // its scratch stays for another window until idle or the cap frees it
const n = try common.rowClass(rows, @import("cuda_engine.zig").most_window);
if (rows > 1) clear(c, true);
try ensure(e, c, false, e.g.logBytes(n), rows == 1);
if (rows == 1) try ensure(e, c, true, e.g.stateBytes(), false);
if (rows == 1 and undo) try ensure(e, c, true, e.g.stateBytes(), false);
}

pub fn mark(c: *Cache, before: bool) !void {
Expand Down
Loading