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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- Qwen3.5 on CUDA: MTP admission charges the target tap ring once per stream, and native inference reserves only its 64-row recurrent log unless slide learning needs rollback snapshots, so the admission bound matches what the engine allocates (fixes #5134). Thanks to @tacosta for the report.

- Metal: Inkling 2 with a DFlash 2 drafter now saves and restores the drafter's lane state in the prompt cache, so warm cache hits restore the drafter alongside the target (#5032, thanks @gilby).

- CUDA: drafted Qwen3.8-27B now sends a draft hold with any wide stream through the full head in one pass by default, winning back the 1.3.7 loss at 4 greedy streams (+2.3% to +3.1% on two GB10s) with identical tokens; `TF_QWEN_DRAFT_FULL_HEAD=0` restores the split path (#5490, fixes #4439).

- CUDA: `TF_CUDA_PREFILL_TRACE=1` (off by default) writes a cold-prefill stage ledger: prompt chunks and graph replay, drafter upload and trailing absorb, host syncs and copies, cache publication and memory peaks, fresh or resumed; with it unset nothing on the serving path changes.
Expand Down
2 changes: 1 addition & 1 deletion zig/build/cuda_family_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ pub fn steps(b: *std.Build, cuda: *std.Build.Module, mods: anytype, step: *std.B
const mtp_lifecycle = b.addRunArtifact(b.addTest(.{ .root_module = mods.qwen35, .filters = &.{"MTP context lifecycle:"} }));
b.step("test-qwen35-mtp-lifecycle-host", "MTP stream release and pooled context reset after interruption without devices").dependOn(&mtp_lifecycle.step);
step.dependOn(&mtp_lifecycle.step);
const mtp_memory = b.addRunArtifact(b.addTest(.{ .root_module = mods.qwen35, .filters = &.{"MTP startup peak"} }));
const mtp_memory = b.addRunArtifact(b.addTest(.{ .root_module = mods.qwen35, .filters = &.{ "MTP startup peak", "MTP on a pool lane: admission includes the target tap ring exactly once" } }));
b.step("test-qwen35-mtp-memory-host", "MTP startup device-memory accounting and overflow guards without a GPU").dependOn(&mtp_memory.step);
step.dependOn(&mtp_memory.step);
}
Expand Down
1 change: 1 addition & 0 deletions zig/qwen35_recurrent_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fn engine(e: *q.engine.Engine, d: *cuda.Driver) void {
e.stream = .{ .d = d, .handle = @ptrFromInt(17) };
e.g = .{ .hidden = 5120, .intermediate = 17408, .layers = 64, .vocab = 248320, .k_heads = 16, .v_heads = 48, .q_heads = 24, .kv_heads = 4, .eps = 1e-6, .tied = false, .context = 262144, .linear = @splat(false) };
for (&e.g.linear, 0..) |*linear, i| linear.* = i < 64 and (i + 1) % 4 != 0;
if (comptime @hasField(q.engine.Engine, "rollback_capable")) e.rollback_capable = true;
e.n_taps = 0;
e.kv8 = false;
e.geo = .{ .hid = 5120, .nk = 16, .nv = 48, .qh = 24, .kvh = 4, .nl = 48 };
Expand Down
51 changes: 51 additions & 0 deletions zig/qwen35_shared_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn engine(d: *cuda.Driver) q.engine.Engine {
e.d = d;
e.stream = .{ .d = d, .handle = null };
e.g = .{ .hidden = 256, .intermediate = 256, .layers = 1, .vocab = 32, .k_heads = 1, .v_heads = 1, .q_heads = 1, .kv_heads = 1, .eps = 1e-6, .tied = true, .context = 256, .linear = @splat(true) };
if (comptime @hasField(q.engine.Engine, "rollback_capable")) e.rollback_capable = true;
e.n_taps = 0;
e.kv8 = false;
e.geo = .{ .hid = 256, .nk = 1, .nv = 1, .qh = 1, .kvh = 1, .nl = 1 };
Expand All @@ -59,6 +60,36 @@ fn buffer(d: *cuda.Driver, data: []u8) cuda.DeviceBuffer {
return .{ .d = d, .ptr = @intFromPtr(data.ptr), .len = data.len, .borrowed = true };
}

test "Shared verification: inference reserves 64 log rows without a before-state" {
var d: cuda.Driver = undefined;
var e = engine(&d);
if (comptime @hasField(q.engine.Engine, "rollback_capable")) e.rollback_capable = false;
e.rec_scratch = q.engine.recurrent.Pool.init(a, @intFromPtr(&e));
e.rec_scratch.cap = q.engine.recurrent.bound(&e);
Owners.setup(&d, 0, 0);
defer Owners.balanced() catch unreachable;
const b: cuda.DeviceBuffer = .{ .d = &d, .ptr = 1, .len = e.g.stateBytes(), .borrowed = true };
var cache: q.engine.Cache = .{ .owner = &e.rec_scratch, .scratch = q.engine.recurrent.bound(&e), .state = b, .kv = b, .log = b, .cap = 256, .logits = &.{} };
defer {
q.engine.recurrent.release(&cache);
q.engine.recurrent.shutdown(&e);
}
try std.testing.expectEqual(e.g.logBytes(64), q.engine.recurrent.bound(&e));
const undo = if (comptime @hasField(q.engine.Engine, "rollback_capable")) e.rollback_capable else true;
try q.engine.recurrent.prepare(&e, &cache, 1, true, undo);
try std.testing.expect(cache.before == null);
try std.testing.expectEqual(e.g.logBytes(1), cache.log.len);
try q.engine.recurrent.mark(&cache, false);
cache.advanced(1, true, false);
q.engine.recurrent.completed(&e);
try q.engine.recurrent.prepare(&e, &cache, 64, true, undo);
try std.testing.expect(cache.before == null);
try std.testing.expectEqual(e.g.logBytes(64), cache.log.len);
try q.engine.recurrent.mark(&cache, false);
cache.advanced(64, true, true);
q.engine.recurrent.completed(&e);
}

test "Shared verification: empty keep restores every byte of an in-place advanced recurrence and conv tail" {
var d: cuda.Driver = undefined;
var e = engine(&d);
Expand All @@ -81,6 +112,26 @@ test "Shared verification: empty keep restores every byte of an in-place advance
try std.testing.expectEqual(@as(usize, 0), Driver.launches);
}

test "Shared verification: rollback-capable empty keep restores its snapshot" {
var d: cuda.Driver = undefined;
var e = engine(&d);
if (comptime @hasField(q.engine.Engine, "rollback_capable")) e.rollback_capable = true;
try std.testing.expectEqual(e.g.stateBytes() + e.g.logBytes(64), q.engine.recurrent.bound(&e));
const before = try a.alloc(u8, e.g.stateBytes());
defer a.free(before);
const state = try a.alloc(u8, before.len);
defer a.free(state);
for (before, 0..) |*x, i| x.* = @truncate(i *% 29 +% 47);
@memset(state, 0xdd);
const b = buffer(&d, state);
var cache: q.engine.Cache = .{ .state = b, .kv = b, .log = b, .cap = 256, .len = 17, .logits = &.{} };
cache.before = buffer(&d, before);
cache.advanced(1, true, true);
try cache.keep(&e, &.{});
try std.testing.expectEqualSlices(u8, before, state);
try std.testing.expectEqual(@as(usize, 17), cache.len);
}

test "Shared verification: keeping an advanced one-row slot does not apply the recurrence twice" {
var d: cuda.Driver = undefined;
var e = engine(&d);
Expand Down
7 changes: 7 additions & 0 deletions zig/src/core/lanes/accept.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ test "accept path follows matching children" {
try std.testing.expectEqualSlices(i32, &.{ -1, 0, 1 }, chain);
}

test "accept path keeps root when every draft differs" {
const gpa = std.testing.allocator;
const path = try acceptPath(gpa, &.{ 5, 6, 7, 8 }, &.{ -1, 0, 0, 1 }, &.{ 9, 9, 9, 9 });
defer gpa.free(path);
try std.testing.expectEqualSlices(u32, &.{0}, path);
}

test "sanitize drops orphans" {
const gpa = std.testing.allocator;
const t = try sanitizeTree(gpa, &.{ 1, 2, 3, 4 }, &.{ -1, 0, 5, 2 }, 4);
Expand Down
22 changes: 9 additions & 13 deletions zig/src/families/inkling2/metal/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@ const fwd = @import("forward.zig");
const Model = @import("model.zig").Model;
const snapshot = @import("snapshot.zig");
const img = @import("images.zig");
const dspark = @import("dspark.zig");
const dspark_state = @import("dspark_state.zig");
const drafter_snap = @import("drafter_snap.zig");
const cfg = @import("../config.zig");
const draws = @import("draw.zig");
const flight = @import("flight.zig");
const sample = @import("sample.zig");
const be = lanes.backend;

const SavedSnap = struct {
target: snapshot.Snap,
dspark: ?dspark_state.Snap = null,
};
pub const DrafterHandle = drafter_snap.Drafter;
pub const SavedSnap = drafter_snap.Saved;

pub const Options = struct { capacity: usize, streams: u32 = 4, budget: u64, images: ?img.Encoder = null, gpu_tokens: bool = false };
const shared_widths = [_]usize{ 24, 32, 48, 64 };
Expand Down Expand Up @@ -54,7 +51,7 @@ pub const Metal = struct {
shared: [shared_widths.len]lanes.config.Cost = undefined,
shared_timed: usize = 0,
tap_gen: u64 = 0,
drafter: ?*dspark.Drafter = null,
drafter: ?DrafterHandle = null,
verify_gen: u64 = 0,
pub var fail_first_draw_for_test: if (builtin.is_test) bool else void = if (builtin.is_test) false else {};

Expand Down Expand Up @@ -253,8 +250,8 @@ pub const Metal = struct {
try self.feed(cache, ids[cache.len..], stream, &pictures);
}

fn restoreSnap(self: *Metal, cache: *st.Cache, stream: *lanes.Stream, snap: *const SavedSnap) !void {
if (self.drafter) |d| try d.restoreSnap(stream, if (snap.dspark) |*ds| ds else null, snap.target.at);
pub fn restoreSnap(self: *Metal, cache: *st.Cache, stream: *lanes.Stream, snap: *const SavedSnap) !void {
if (self.drafter) |d| try d.restoreSnap(stream, if (snap.drafter) |*dr| dr else null, snap.target.at);
try snapshot.restore(cache, self.model.queue, &snap.target);
}

Expand Down Expand Up @@ -548,7 +545,7 @@ pub const Metal = struct {

pub fn snapBytes(ptr: *anyopaque, at: u32) u64 {
const self = cast(ptr);
return snapshot.bytes(at) + if (self.drafter != null) dspark_state.Snap.bytes(at) else 0;
return snapshot.bytes(at) + if (self.drafter) |d| d.snapBytes(at) else 0;
}

fn syncQueue(self: *Metal) !void {
Expand Down Expand Up @@ -578,7 +575,7 @@ pub const Metal = struct {
snap.* = .{ .target = try snapshot.save(self.model.device, self.model.queue, cache) };
errdefer snap.target.free();
if (self.drafter) |d| {
snap.dspark = try d.saveSnap(stream, at, cache.tap, self.tap_gen);
snap.drafter = try d.saveSnap(stream, at, cache.tap, self.tap_gen);
}
return snap;
}
Expand All @@ -589,8 +586,7 @@ pub const Metal = struct {

pub fn snapDrop(ptr: *anyopaque, saved: *anyopaque) void {
const snap: *SavedSnap = @ptrCast(@alignCast(saved));
if (snap.dspark) |*ds| ds.free();
snap.target.free();
snap.free();
cast(ptr).gpa.destroy(snap);
}
};
Expand Down
Loading
Loading