Skip to content

Commit a5e409e

Browse files
committed
Flash Next shared sessions: FLASHNEXT_LOAD_LIMIT_GB lets the slots take the server past 70% of RAM, as GLM_LOAD_LIMIT_GB does for GLM
1 parent 77ebc3f commit a5e409e

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎zig/src/native/cache_fit.zig‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ pub fn fit(total: u64, ready: u64, gib: ?f64, over: bool) error{CacheOverCap}!Fi
2222
return .{ .budget = want, .room = room, .cap = cap };
2323
}
2424

25+
/// The ceiling a served model may hold: 70% of `total`, or `text` (a load limit in GB, as GLM_LOAD_LIMIT_GB) up to
26+
/// `most` (the smaller of RAM and the GPU's recommended working set); text that is not a number above 0 is refused.
27+
pub const Ceiling = struct { bytes: u64, default: u64, asked: bool = false, capped: bool = false };
28+
29+
pub fn ceiling(total: u64, most: u64, text: ?[]const u8) error{BadLoadLimit}!Ceiling {
30+
const default = total / 100 * SHARE_PERCENT;
31+
const t = std.mem.trim(u8, text orelse "", " \t");
32+
if (t.len == 0) return .{ .bytes = default, .default = default };
33+
const gb = std.fmt.parseFloat(f64, t) catch return error.BadLoadLimit;
34+
if (!std.math.isFinite(gb) or gb <= 0) return error.BadLoadLimit;
35+
const top: f64 = @floatFromInt(@min(total, most));
36+
return .{ .bytes = @intFromFloat(@min(gb * 1e9, top)), .default = default, .asked = true, .capped = gb * 1e9 > top };
37+
}
38+
2539
/// This machine's RAM (null where the OS does not say).
2640
pub fn ram() ?u64 {
2741
return std.process.totalSystemMemory() catch null;
@@ -91,6 +105,23 @@ test "a given budget that fits is kept, a larger one is refused unless the overr
91105
try std.testing.expectEqual(@as(u64, 64 * GiB), (try fit(512 * GiB, 172 * GiB, 64, false)).budget); // a 512 GB Mac has room
92106
}
93107

108+
test "the ceiling: 70% of RAM, or a load limit in GB up to RAM and the GPU's working set; bad text refused" {
109+
const ram256: u64 = 256 * GiB;
110+
const ws: u64 = 239_000_000_000;
111+
const d = try ceiling(ram256, ws, null);
112+
try std.testing.expectEqual(ram256 / 100 * 70, d.bytes);
113+
try std.testing.expect(!d.asked and !d.capped);
114+
const asked = try ceiling(ram256, ws, "215");
115+
try std.testing.expectEqual(@as(u64, 215_000_000_000), asked.bytes);
116+
try std.testing.expect(asked.asked and !asked.capped and asked.bytes > asked.default);
117+
try std.testing.expect((try ceiling(ram256, ws, " 300 ")).capped);
118+
try std.testing.expectEqual(ws, (try ceiling(ram256, ws, "300")).bytes);
119+
try std.testing.expectEqual(ram256 / 100 * 70, (try ceiling(ram256, ws, " ")).bytes);
120+
try std.testing.expectError(error.BadLoadLimit, ceiling(ram256, ws, "lots"));
121+
try std.testing.expectError(error.BadLoadLimit, ceiling(ram256, ws, "0"));
122+
try std.testing.expectError(error.BadLoadLimit, ceiling(ram256, ws, "-4"));
123+
}
124+
94125
test "the OS readings: RAM and this process's footprint" {
95126
if (builtin.os.tag != .macos) return error.SkipZigTest;
96127
const r = ram() orelse return error.NoRam;

‎zig/src/native/flashnext_batch_host.zig‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ const Host = struct {
2121
warm: mtl.keepalive.Target,
2222
};
2323

24+
/// 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
25+
/// GPU's recommended working set; past the default the log names the risk, as GLM's loader does.
26+
fn ceiling(total: u64, working_set: u64) error{BadLoadLimit}!u64 {
27+
const text: ?[]const u8 = if (std.c.getenv("FLASHNEXT_LOAD_LIMIT_GB")) |v| std.mem.span(v) else null;
28+
const c = fit.ceiling(total, working_set, text) catch |err| {
29+
std.log.err("flash next: FLASHNEXT_LOAD_LIMIT_GB={s} is not a load limit: give a number of GB above 0, or unset it for 70% of RAM", .{text.?});
30+
return err;
31+
};
32+
if (c.capped) std.log.info("flash next: FLASHNEXT_LOAD_LIMIT_GB={s} passes this GPU's recommended working set; the limit is {d:.1} GB", .{ text.?, fit.gibs(c.bytes) * 1.073741824 });
33+
if (c.bytes > c.default) std.log.warn("flash next: FLASHNEXT_LOAD_LIMIT_GB lets the sessions take this Mac to {d:.1} GiB, past the default {d:.1} GiB (70% of RAM): a load that wires most of the RAM can stall macOS until its watchdog kills the server", .{ fit.gibs(c.bytes), fit.gibs(c.default) });
34+
return c.bytes;
35+
}
36+
2437
pub fn open(gpa: std.mem.Allocator, io: std.Io, dir: []const u8, dump: ?[]const u8, window: i64, streams: u32, cache_gib: ?f64, over: bool, a: std.mem.Allocator, why: *[]const u8) !api.Opened {
2538
const pool = mtl.objc.Pool.push();
2639
defer pool.pop();
@@ -32,9 +45,10 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, dir: []const u8, dump: ?[]const
3245
eng.hostMode();
3346
const ready = fit.footprint() orelse return error.NoMemoryReading;
3447
const total = fit.ram() orelse return error.NoMemoryReading;
48+
const cap = try ceiling(total, eng.r.device.maxWorkingSet());
3549
const reserved = if (!over) (if (cache_gib) |g| (if (g > 0) std.math.lossyCast(u64, g * fit.GiB) else 0) else 0) else 0;
3650
const fixed = Session.overhead(tf.flashnext_replay.CAP) + tf.flashnext_replay.MAXR * tf.flashnext_replay.WIDE * 2;
37-
const available = total / 100 * fit.SHARE_PERCENT -| ready -| fit.MARGIN -| reserved;
51+
const available = cap -| ready -| fit.MARGIN -| reserved;
3852
if (available < fixed) return error.OverMemoryLimit;
3953
const room = available - fixed;
4054
const slots = try tf.flashnext_batch_meta.fit(streams, true, room, Session.bytes(capacity));
@@ -45,8 +59,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, dir: []const u8, dump: ?[]const
4559
h.back = try Backend.init(gpa, eng, slots, capacity);
4660
errdefer h.back.deinit();
4761
const allocated = fit.footprint() orelse return error.NoMemoryReading;
48-
if (allocated +| fit.MARGIN +| reserved > total / 100 * fit.SHARE_PERCENT)
49-
return error.OverMemoryLimit;
62+
if (allocated +| fit.MARGIN +| reserved > cap) return error.OverMemoryLimit;
5063
h.cfg = try lanes.Config.init(gpa, h.back.facts(), tf.flashnext_replay.MAXR, tf.flashnext_replay.MAXR - 1);
5164
errdefer h.cfg.deinit(gpa);
5265
h.clock = .{ .io = io };

‎zig/src/native/metal.zig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fn openFlashNext(a: Allocator, gpa: Allocator, io: std.Io, o: api.Open, problem:
307307
}
308308
var why: []const u8 = "";
309309
return @import("flashnext_batch_host.zig").open(gpa, io, o.dir, dump, if (window > 0) @min(window, tf.flashnext_replay.CAP - tf.flashnext_engine.MARGIN) else tf.flashnext_replay.CAP - tf.flashnext_engine.MARGIN, o.lanes, o.prompt_cache_gib, o.prompt_cache_over_cap, a, &why) catch |err| {
310-
problem.* = if (err == error.CacheOverCap) why else try std.fmt.allocPrint(a, "the native Flash Next batching engine cannot open ({s})", .{@errorName(err)});
310+
problem.* = if (err == error.CacheOverCap) why else if (err == error.BadLoadLimit) "FLASHNEXT_LOAD_LIMIT_GB is not a load limit: give a number of GB above 0, or unset it for 70% of RAM" else if (err == error.OverMemoryLimit) "the native Flash Next batching engine cannot open (OverMemoryLimit): the sessions do not fit under 70% of RAM past the loaded model; fewer streams, a smaller --context, or FLASHNEXT_LOAD_LIMIT_GB past the share" else try std.fmt.allocPrint(a, "the native Flash Next batching engine cannot open ({s})", .{@errorName(err)});
311311
return null;
312312
};
313313
}

0 commit comments

Comments
 (0)