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

- DeepSeek-V4.1 on Metal: the target's own buffers (prompt scratch, each slot's KV, tails, forward and layer scratch, RoPE tables) are now held in a residency set on its queue, as the weights and the DSpark drafter already were. Before, a prompt chunk or decoder pass could wait 50-830 ms between commit and its first GPU instruction while macOS mapped them again, and a server's first request waited about 1.9 s. Replies are unchanged. On an M5 Ultra, cold prompts run about 5-7% faster (2K tokens 600 -> 631 tok/s, 8K 855 -> 917, 32K about 1,000 -> 1,063).
- DeepSeek-V4.1 on Metal: the target's own buffers (prompt scratch, each slot's KV, tails, forward and layer scratch, RoPE tables) now join the weights' residency set, inside its load limit (the set refuses and logs what would take it past 70% of RAM). Before, a prompt chunk or decoder pass could wait 20-830 ms between commit and its first GPU instruction while macOS mapped them again, and a server's first request waited about 1.9 s. Replies are unchanged. On an M5 Ultra, cold prompts run about 5-7% faster (2K tokens 600 -> 631 tok/s, 8K 855 -> 917, 30K about 1,000 -> 1,063) (#3321).
- DeepSeek-V4.1 on Metal: a cold prompt's first chunk now reads its Engram rows while the GPU runs the layers before each Engram layer, instead of reading them all before the chunk starts. Replies are unchanged. On an M5 Ultra, cold prompts start about 0.3 s sooner at 2K tokens, 0.55 s at 8K and 0.8 s at 32K.
- DeepSeek-V4.1 on Metal: a cold prompt's first chunk now reads its Engram rows while the GPU runs the layers before each Engram layer, instead of reading them all before the chunk starts. Replies are unchanged. On an M5 Ultra, cold prompts start about 0.3 s sooner at 2K tokens, 0.55 s at 8K and 0.8 s at 30K (#3318).
- Qwen3.8-27B on Metal (M1-M4): projections no longer barrier internally, and XS sums compile out before tensor units, with the same tokens (#2913, #2900).
Expand Down
2 changes: 1 addition & 1 deletion zig/src/families/deepseek_v41/metal/boot.zig
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub const Boot = struct {
const r = b.w.report();
std.log.info("dsv41 metal: {d} layers, {d} shards ({d} MiB mapped, {d} MiB copied), {d} slots of {d} positions, DSpark {s}", .{ c.layers, r.shards, r.shard_bytes >> 20, r.arena_bytes >> 20, o.slots, limit, if (b.ds != null) "on" else "off" });
const env: target_.Env = .{ .device = b.device, .queue = b.queue, .c = c, .p = &b.p, .tables = &b.tables, .s = &b.s, .model = b.w.view(), .lookup = lookup };
return serve.Metal.open(a, .{ .env = env, .top = &b.w.top, .dspark = if (b.ds) |*w| w else null, .owner = .{ .ctx = b, .close = close } }, .{ .slots = o.slots, .limit = limit, .drafts = o.drafts, .cache_bytes = o.cache_bytes });
return serve.Metal.open(a, .{ .env = env, .top = &b.w.top, .dspark = if (b.ds) |*w| w else null, .owner = .{ .ctx = b, .close = close }, .resident = if (b.w.resident) |*wr| wr else null }, .{ .slots = o.slots, .limit = limit, .drafts = o.drafts, .cache_bytes = o.cache_bytes });
}

fn readConfig(a: Allocator, io: std.Io, dir: []const u8, small: bool) !Config {
Expand Down
4 changes: 3 additions & 1 deletion zig/src/families/deepseek_v41/metal/serve.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub const Parts = struct {
/// the drafter's weights (null: serial windows, no drafts)
dspark: ?*const dspark.Weights,
owner: ?Owner = null,
/// the weights' residency owner the target's buffers join (null: none join)
resident: ?*mtl.resident.Resident = null,
};

pub const Owner = struct { ctx: *anyopaque, close: *const fn (ctx: *anyopaque) void };
Expand Down Expand Up @@ -49,7 +51,7 @@ pub const Metal = struct {
const m = try a.create(Metal);
errdefer a.destroy(m);
m.* = .{ .a = a, .parts = parts, .opt = opt, .t = undefined };
m.t = try target_.Target.init(a, parts.env, .{ .slots = opt.slots, .limit = opt.limit, .prompt = opt.prompt, .cache_bytes = opt.cache_bytes });
m.t = try target_.Target.init(a, parts.env, .{ .slots = opt.slots, .limit = opt.limit, .prompt = opt.prompt, .cache_bytes = opt.cache_bytes, .resident = parts.resident });
errdefer m.t.deinit();
const e = parts.env;
if (opt.drafts) if (parts.dspark) |w| {
Expand Down
36 changes: 36 additions & 0 deletions zig/src/families/deepseek_v41/metal/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub const Options = struct {
prompt: prompt.Opts = .{},
/// bytes of kept prompt states (0: no prompt cache)
cache_bytes: u64 = 0,
/// the weights' residency owner, which the target's own buffers join inside its load limit (null: none join)
resident: ?*mtl.resident.Resident = null,
};

/// What the target did, for the tests and the logs.
Expand Down Expand Up @@ -76,6 +78,8 @@ pub const Target = struct {
stats: Stats = .{},
/// a command buffer failed: the device state is no longer trusted (the health hook)
failed: bool = false,
/// the target's own buffers joined the weights' residency owner (they leave it before they are freed)
joined: bool = false,

pub fn init(a: Allocator, env: Env, opt: Options) !*Target {
const c = env.c;
Expand Down Expand Up @@ -107,9 +111,35 @@ pub const Target = struct {
made += 1;
}
t.slots = slots;
t.joinResident() catch {};
return t;
}

/// Every buffer the target binds besides the weights joins their residency owner, so none waits to be mapped.
fn joinResident(t: *Target) !void {
const r = t.opt.resident orelse return;
var bufs: std.ArrayList(mtl.Buffer) = .empty;
defer bufs.deinit(t.a);
try t.ownBuffers(&bufs);
var bytes: u64 = 0;
for (bufs.items) |b| bytes += b.length();
// the owner refuses (and logs) buffers that would take it past its load limit
t.joined = r.tryAddNow(bufs.items);
if (t.joined) std.log.info("dsv41 target: {d:.1} GiB of buffers joined the weights' residency set", .{mtl.ResidencySet.gibs(bytes)});
}

fn ownBuffers(t: *const Target, out: *std.ArrayList(mtl.Buffer)) !void {
try out.appendSlice(t.a, &.{ t.b.buf, t.pr.s.buf, t.pr.b.buf, t.env.s.buf, t.env.tables.window, t.env.tables.compressed });
for (t.slots) |*s| {
for (s.kv.regions) |r| {
try out.append(t.a, r.window);
inline for (.{ "pool", "keys", "carry", "inputs" }) |f| if (@field(r, f)) |b| try out.append(t.a, b);
}
if (s.kv.taps) |b| try out.append(t.a, b);
try out.appendSlice(t.a, &.{ s.tail.x, s.tail.pre, s.ptaps });
}
}

fn freeSlot(a: Allocator, s: *Slot) void {
s.kv.deinit();
s.tail.deinit();
Expand All @@ -119,6 +149,12 @@ pub const Target = struct {
}

pub fn deinit(t: *Target) void {
if (t.joined) {
var bufs: std.ArrayList(mtl.Buffer) = .empty;
defer bufs.deinit(t.a);
t.ownBuffers(&bufs) catch {};
for (bufs.items) |b| t.opt.resident.?.remove(b);
}
for (t.slots) |*s| freeSlot(t.a, s);
t.a.free(t.slots);
t.cache.deinit();
Expand Down
Loading