Skip to content

Commit 7808bee

Browse files
committed
Metal registry: buffers join and leave the residency set staged, committed by the idle keepalive between requests
With FLASHNEXT_RESIDENCY=all a host holds every live buffer; a buffer made or freed during a request is added or removed without a commit (a commit per buffer stalled a reply at 5 tok/s), and the keepalive's next tick, which runs only while no request does, commits them. M5 Ultra, one 262k stream, 6 GiB prompt cache, iogpu.disable_wired_collector at its default 0: 30 of 30 mixed replies steady (209/138 tok/s code/prose), the first token after 10-30 s idle 0.09-0.10 s, a fresh 25k-token prompt after a minute idle 5.4 s (no set: 2.1-2.4 s and 24 s; the startup-only set: 10-30% of replies at 50-100 tok/s).
1 parent 8964b1c commit 7808bee

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

‎zig/src/metal/device.zig‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ pub const Buffer = struct {
3838
}
3939
};
4040

41-
/// FLASHNEXT_RESIDENCY=all: every live buffer, from the first, so a host's residency set can hold the whole footprint
42-
/// and each later buffer joins it as made (rounds that touched both held and unheld memory ran up to 3x slower).
41+
/// FLASHNEXT_RESIDENCY=all: every live buffer, from the first, so a host's residency set holds the whole footprint.
42+
/// Later buffers join and freed ones leave the set staged; `flush` commits them while no request runs (a commit per
43+
/// buffer mid-request stalled a reply at 5 tok/s; a set that misses buffers made some rounds up to 3x slower).
4344
pub const registry = struct {
4445
var lock: std.atomic.Value(bool) = .init(false);
4546
var on: ?bool = null;
4647
var live: std.AutoArrayHashMapUnmanaged(Id, void) = .empty;
4748
var set: ?residency.ResidencySet = null;
49+
var dirty = false; // staged adds and removes the set has not committed
4850

4951
fn enabled() bool {
5052
if (on == null) on = if (std.c.getenv("FLASHNEXT_RESIDENCY")) |v| std.mem.eql(u8, std.mem.span(v), "all") else false;
@@ -64,9 +66,9 @@ pub const registry = struct {
6466
acquire();
6567
defer release();
6668
live.put(std.heap.c_allocator, b.id, {}) catch {}; // a miss only leaves this buffer to macOS
67-
if (set) |s| { // no requestResidency: it walks the whole set (a 5 tok/s reply while the prompt cache allocated)
69+
if (set) |s| {
6870
s.add(b);
69-
s.commit();
71+
dirty = true;
7072
}
7173
}
7274

@@ -77,10 +79,21 @@ pub const registry = struct {
7779
if (!live.swapRemove(b.id)) return;
7880
if (set) |s| {
7981
s.remove(b);
80-
s.commit();
82+
dirty = true;
8183
}
8284
}
8385

86+
/// Commit the adds and removes staged since the last flush; the idle keepalive calls it between requests.
87+
pub fn flush() void {
88+
if (!enabled()) return;
89+
acquire();
90+
defer release();
91+
const s = set orelse return;
92+
if (!dirty) return;
93+
s.commit();
94+
dirty = false;
95+
}
96+
8497
/// Whether FLASHNEXT_RESIDENCY=all asks for the registry.
8598
pub fn active() bool {
8699
return enabled();

‎zig/src/metal/keepalive.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub const Target = struct {
1313
/// The core ticker's tick: one command buffer that touches every set and commits.
1414
pub fn tick(ctx: *anyopaque) void {
1515
const t: *Target = @ptrCast(@alignCast(ctx));
16+
@import("device.zig").registry.flush(); // buffers made or freed by the last request: the set again whole
1617
const pool = @import("objc.zig").Pool.push();
1718
defer pool.pop();
1819
const cmd = t.queue.commandBuffer();

0 commit comments

Comments
 (0)