Skip to content

Commit 0261dcc

Browse files
Flash Next hosts hold their residency set only while it fits the GPU's working set
On a 256 GB M5 Ultra a 195.8 GiB set (four 262K sessions) slowed the GPU-side rounds about threefold, while sets under the working set held them at speed (BobClawblaw on ashhart#606). Both hosts now hold the set only while its buffers, the prompt cache's budget and a 2 GiB margin fit recommendedMaxWorkingSetSize, and log which way it went. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent add3da3 commit 0261dcc

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎zig/src/native/flashnext_batch_host.zig‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,25 @@ fn learnedStates(gpa: std.mem.Allocator, eng: *fx.Engine, root: []const u8, cap:
4242

4343
/// macOS unwires GPU memory after 1-2 s idle and the next request re-wires it all (2 s before its first token): the
4444
/// engine's buffers, every session's and the tower's join one residency set the idle keepalive uses.
45-
fn holdResident(h: *Host) !void {
45+
/// Only while the set, the prompt cache's `cache` bytes and a margin fit the GPU's recommended working set: a set past it
46+
/// slowed the GPU-side rounds about threefold on a 256 GB M5 Ultra (#606: 195.8 GiB held), one under it holds them at speed.
47+
fn holdResident(h: *Host, cache: u64) !void {
4648
var list: std.ArrayList(mtl.Buffer) = .empty;
4749
defer list.deinit(h.gpa);
4850
try list.appendSlice(h.gpa, h.eng.r.held.items);
4951
for (h.back.sessions) |s| try list.appendSlice(h.gpa, s.owned.items);
5052
try list.append(h.gpa, h.back.hidden.b);
5153
if (h.vision) |v| try list.appendSlice(h.gpa, &v.tower.buffers());
54+
var bytes: u64 = 0;
55+
for (list.items) |b| bytes += b.length();
56+
const room = h.eng.r.device.maxWorkingSet();
57+
if (bytes +| cache +| fit.MARGIN > room) {
58+
std.log.warn("flash next: no residency set: its {d:.1} GiB and the prompt cache's {d:.1} GiB pass the GPU's {d:.1} GiB working set; the first request after an idle second waits for the model's memory", .{ fit.gibs(bytes), fit.gibs(cache), fit.gibs(room) });
59+
return;
60+
}
5261
h.resident = try Resident.initTicked(h.eng.r.device, h.eng.r.queue, list.items, true);
5362
h.warm = (&h.resident.?).target();
54-
std.log.info("flash next: {d} buffers ({d:.1} GiB) held resident between requests", .{ list.items.len, fit.gibs(h.resident.?.set.allocatedSize()) });
63+
std.log.info("flash next: {d} buffers ({d:.1} GiB) held resident between requests, under the GPU's {d:.1} GiB working set", .{ list.items.len, fit.gibs(h.resident.?.set.allocatedSize()), fit.gibs(room) });
5564
}
5665

5766
/// What the sessions may take the server to: 70% of RAM, or FLASHNEXT_LOAD_LIMIT_GB (GB, as GLM_LOAD_LIMIT_GB) up to the
@@ -175,7 +184,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, dir: []const u8, dump: ?[]const
175184
h.host.explain = .{ .text = words };
176185
h.warm = .{ .queue = eng.r.queue };
177186
h.resident = null;
178-
if (std.c.getenv("FLASHNEXT_NO_RESIDENCY") == null) holdResident(h) catch |err| std.log.warn("flash next: no residency set ({s}): the first request after an idle second re-wires the model", .{@errorName(err)});
187+
if (std.c.getenv("FLASHNEXT_NO_RESIDENCY") == null) holdResident(h, plan.budget_bytes) catch |err| std.log.warn("flash next: no residency set ({s}): the first request after an idle second re-wires the model", .{@errorName(err)});
179188
errdefer if (h.resident) |*r| r.deinit();
180189
h.host.keepalive_target = .{ .ctx = &h.warm, .tick = mtl.keepalive.Target.tick };
181190
try h.host.start();

‎zig/src/native/flashnext_host.zig‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ pub fn open(gpa: Allocator, io: std.Io, dir: []const u8, dump: ?[]const u8, wind
485485
h.vision = vis;
486486
if (vis) |v| h.info_.vision = v.offer();
487487
// macOS unwires GPU memory after 1-2 s idle and the next request re-wires it all: one set the idle keepalive uses
488-
if (std.c.getenv("FLASHNEXT_NO_RESIDENCY") == null) holdResident(h) catch |err| std.log.warn("flash next: no residency set ({s}): the first request after an idle second re-wires the model", .{@errorName(err)});
488+
if (std.c.getenv("FLASHNEXT_NO_RESIDENCY") == null) holdResident(h, budget) catch |err| std.log.warn("flash next: no residency set ({s}): the first request after an idle second re-wires the model", .{@errorName(err)});
489489
errdefer if (h.resident) |*r| r.deinit();
490490
if (!eng.followsPeer() and budget > 0) h.cache = pc.Store.init(gpa, .{ .ptr = h, .vtable = &.{ .bytes = Snaps.bytes, .save = Snaps.save, .restore = Snaps.restore, .drop = Snaps.drop, .charged = Snaps.charged, .spare = Snaps.spare, .trim = Snaps.trim, .reuses = Snaps.reuses, .write = Snaps.write, .read = Snaps.read, .forget = Snaps.forget } }, .{ .lookahead = 1 }, budget);
491491
h.info_.prompt_cache = h.cache != null;
@@ -499,11 +499,20 @@ pub fn open(gpa: Allocator, io: std.Io, dir: []const u8, dump: ?[]const u8, wind
499499
}
500500

501501
/// The engine's buffers and the tower's in one residency set; the idle keepalive's ticks use it.
502-
fn holdResident(h: *Host) !void {
502+
/// Only while the set, the prompt cache's `cache` bytes and a margin fit the GPU's recommended working set: a set past it
503+
/// slowed the GPU-side rounds about threefold on a 256 GB M5 Ultra (#606: 195.8 GiB held), one under it holds them at speed.
504+
fn holdResident(h: *Host, cache: u64) !void {
503505
var list: std.ArrayList(mtl.Buffer) = .empty;
504506
defer list.deinit(h.gpa);
505507
try list.appendSlice(h.gpa, h.eng.r.held.items);
506508
if (h.vision) |v| try list.appendSlice(h.gpa, &v.tower.buffers());
509+
var bytes: u64 = 0;
510+
for (list.items) |b| bytes += b.length();
511+
const room = h.eng.r.device.maxWorkingSet();
512+
if (bytes +| cache +| cache_fit.MARGIN > room) {
513+
std.log.warn("flash next: no residency set: its {d:.1} GiB and the prompt cache's {d:.1} GiB pass the GPU's {d:.1} GiB working set; the first request after an idle second waits for the model's memory", .{ cache_fit.gibs(bytes), cache_fit.gibs(cache), cache_fit.gibs(room) });
514+
return;
515+
}
507516
h.resident = try Resident.initTicked(h.eng.r.device, h.eng.r.queue, list.items, true);
508517
h.warm = (&h.resident.?).target();
509518
}

0 commit comments

Comments
 (0)