From 5c7d65264f3dd007583a2f74572c26e8c7e6571e Mon Sep 17 00:00:00 2001 From: Ash <71890694+ashhart@users.noreply.github.com> Date: Sat, 14 Nov 2026 10:00:35 +0000 Subject: [PATCH 1/3] dsv41 metal: the target's buffers stay resident in their own set The prompt scratch and buffers, each slot's KV regions, tail and taps, the forward buffers, the layer scratch and the RoPE tables were in no residency set, so macOS could unmap them between command buffers and map them again when the next one was submitted. Target.init now holds them in one residency set on the engine's queue (requested resident at once), and deinit removes it before the buffers go. --- CHANGELOG.md | 4 +++ .../families/deepseek_v41/metal/target.zig | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3762679891..55e9f89133f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,10 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - Server: with thinking off, the reasoning effort a request names reaches the chat template (GLM's head follows it), and the reply reports it; a server's own `--reasoning-effort` no longer changes a thinking-off prompt (#3017, fixes #1911). Thanks to @ss-cong for the report. - Tests: host tests drive 200 refused, cancelled or failed requests down each lane-host path they reach (refusal at admission, cancels while queued and mid-request, backend errors, no lane memory, and a mix) and check none strands a lane, page or slot (#3015, for #2135). +- 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). +- 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). + - Tests: the cluster bring-up test of a stopped peer checks the membership's real promise instead of a two-second wall bound that failed on busy CI runners (#3310). diff --git a/zig/src/families/deepseek_v41/metal/target.zig b/zig/src/families/deepseek_v41/metal/target.zig index ef832b6798d..52316d676ac 100644 --- a/zig/src/families/deepseek_v41/metal/target.zig +++ b/zig/src/families/deepseek_v41/metal/target.zig @@ -76,6 +76,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 held resident on the queue (null: no residency sets on this OS) + resident: ?mtl.ResidencySet = null, pub fn init(a: Allocator, env: Env, opt: Options) !*Target { const c = env.c; @@ -107,9 +109,29 @@ pub const Target = struct { made += 1; } t.slots = slots; + t.resident = t.holdResident(); return t; } + /// Every buffer the target binds besides the weights, in one residency set on its queue: none waits to be mapped. + fn holdResident(t: *const Target) ?mtl.ResidencySet { + const set = t.env.device.residencySet(16 + 6 * t.slots.len * t.env.c.layers) catch return null; + for ([_]mtl.Buffer{ t.b.buf, t.pr.s.buf, t.pr.b.buf, t.env.s.buf, t.env.tables.window, t.env.tables.compressed }) |b| set.add(b); + for (t.slots) |*s| { + for (s.kv.regions) |r| { + set.add(r.window); + inline for (.{ "pool", "keys", "carry", "inputs" }) |f| if (@field(r, f)) |b| set.add(b); + } + if (s.kv.taps) |b| set.add(b); + for ([_]mtl.Buffer{ s.tail.x, s.tail.pre, s.ptaps }) |b| set.add(b); + } + set.commit(); + set.requestResidency(); + t.env.queue.addResidencySet(set); + std.log.info("dsv41 target: {d:.1} GiB of buffers kept resident in their own set", .{mtl.ResidencySet.gibs(set.allocatedSize())}); + return set; + } + fn freeSlot(a: Allocator, s: *Slot) void { s.kv.deinit(); s.tail.deinit(); @@ -119,6 +141,10 @@ pub const Target = struct { } pub fn deinit(t: *Target) void { + if (t.resident) |set| { + t.env.queue.removeResidencySet(set); + set.deinit(); + } for (t.slots) |*s| freeSlot(t.a, s); t.a.free(t.slots); t.cache.deinit(); From 6565884d37ceb1c1b1fcf856c25e4cfc43a5e63f Mon Sep 17 00:00:00 2001 From: Ash <71890694+ashhart@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:50:23 +0000 Subject: [PATCH 2/3] dsv41 metal: the target's buffers join the weights' residency set inside its load limit Review (D894): the target's own residency set bypassed the 70% wired budget the weights' Resident enforces. The target now hands its buffers to that owner (Resident.tryAddNow), which refuses and logs a join past its limit, and removes them from it before they are freed; boot passes the owner through serve.Parts. --- CHANGELOG.md | 1 + zig/src/families/deepseek_v41/metal/boot.zig | 2 +- zig/src/families/deepseek_v41/metal/serve.zig | 4 +- .../families/deepseek_v41/metal/target.zig | 48 +++++++++++-------- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55e9f89133f..58bca3b0569 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - 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). - 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). - Tests: the cluster bring-up test of a stopped peer checks the membership's real promise instead of a two-second wall bound that failed on busy CI runners (#3310). diff --git a/zig/src/families/deepseek_v41/metal/boot.zig b/zig/src/families/deepseek_v41/metal/boot.zig index 4c21cdf5c34..bdd3e60e783 100644 --- a/zig/src/families/deepseek_v41/metal/boot.zig +++ b/zig/src/families/deepseek_v41/metal/boot.zig @@ -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 { diff --git a/zig/src/families/deepseek_v41/metal/serve.zig b/zig/src/families/deepseek_v41/metal/serve.zig index b0d03fc2f11..36e355641f4 100644 --- a/zig/src/families/deepseek_v41/metal/serve.zig +++ b/zig/src/families/deepseek_v41/metal/serve.zig @@ -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 }; @@ -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| { diff --git a/zig/src/families/deepseek_v41/metal/target.zig b/zig/src/families/deepseek_v41/metal/target.zig index 52316d676ac..b9bbe717125 100644 --- a/zig/src/families/deepseek_v41/metal/target.zig +++ b/zig/src/families/deepseek_v41/metal/target.zig @@ -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. @@ -76,8 +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 held resident on the queue (null: no residency sets on this OS) - resident: ?mtl.ResidencySet = null, + /// 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; @@ -109,27 +111,33 @@ pub const Target = struct { made += 1; } t.slots = slots; - t.resident = t.holdResident(); + t.joinResident() catch {}; return t; } - /// Every buffer the target binds besides the weights, in one residency set on its queue: none waits to be mapped. - fn holdResident(t: *const Target) ?mtl.ResidencySet { - const set = t.env.device.residencySet(16 + 6 * t.slots.len * t.env.c.layers) catch return null; - for ([_]mtl.Buffer{ t.b.buf, t.pr.s.buf, t.pr.b.buf, t.env.s.buf, t.env.tables.window, t.env.tables.compressed }) |b| set.add(b); + /// 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| { - set.add(r.window); - inline for (.{ "pool", "keys", "carry", "inputs" }) |f| if (@field(r, f)) |b| set.add(b); + 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| set.add(b); - for ([_]mtl.Buffer{ s.tail.x, s.tail.pre, s.ptaps }) |b| set.add(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 }); } - set.commit(); - set.requestResidency(); - t.env.queue.addResidencySet(set); - std.log.info("dsv41 target: {d:.1} GiB of buffers kept resident in their own set", .{mtl.ResidencySet.gibs(set.allocatedSize())}); - return set; } fn freeSlot(a: Allocator, s: *Slot) void { @@ -141,9 +149,11 @@ pub const Target = struct { } pub fn deinit(t: *Target) void { - if (t.resident) |set| { - t.env.queue.removeResidencySet(set); - set.deinit(); + 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); From 3ab639d044425ed3418949cf93c50552ea3e41da Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:14:11 +0000 Subject: [PATCH 3/3] changelog: back under Unreleased --- CHANGELOG.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58bca3b0569..246834d3419 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). @@ -58,11 +60,6 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - Server: with thinking off, the reasoning effort a request names reaches the chat template (GLM's head follows it), and the reply reports it; a server's own `--reasoning-effort` no longer changes a thinking-off prompt (#3017, fixes #1911). Thanks to @ss-cong for the report. - Tests: host tests drive 200 refused, cancelled or failed requests down each lane-host path they reach (refusal at admission, cancels while queued and mid-request, backend errors, no lane memory, and a mix) and check none strands a lane, page or slot (#3015, for #2135). -- 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). -- 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). - - Tests: the cluster bring-up test of a stopped peer checks the membership's real promise instead of a two-second wall bound that failed on busy CI runners (#3310).