Skip to content

Commit 0acefcf

Browse files
committed
FLASHNEXT_LOAD_LIMIT_GB covers the prompt cache too: the automatic budget is what the sessions leave under it, up to the 16 GiB default
The slot checks reserved a given cache against the selected limit while the cache plan still read 70% of RAM, so an automatic cache could take the server past the limit and a raised limit did not raise a given cache's own gate. cache_fit.budgetUnder plans the cache under the same ceiling (fitUnder; budget, budgetBeside and fit keep 70% for the other hosts), and the automatic budget still stops at DEFAULT_CEILING. A test covers the automatic and the given budget under a limit. The recipe doc, the limit's comment and the OverMemoryLimit message name the contract.
1 parent acf45bb commit 0acefcf

4 files changed

Lines changed: 46 additions & 10 deletions

File tree

‎docs/recipes/qwen3.8-flash-next.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ New requests join between rounds, and cancelling a request releases its slot for
7474
`auto` and `--parallel 1` retain the existing single-reply driver. Shared sessions currently run on one Mac;
7575
`--parallel N` above one with `--speed-up` is refused. Use `--parallel 1` for the two-Mac link.
7676

77+
The ceiling is 70% of RAM, or `FLASHNEXT_LOAD_LIMIT_GB` (GB, up to the GPU's recommended working set). It covers the
78+
server's whole footprint: the model, the sessions and the prompt cache. Left out, `--prompt-cache-gib` takes what the
79+
sessions leave under it; a given budget must fit under the same ceiling, and only `--prompt-cache-over-cap` lets it
80+
pass.
81+
7782
Additional slots allocate caches for the selected context. Startup reserves their metadata and the prompt-cache
7883
budget, then checks the physical footprint before accepting requests. A fixed slot count that does not fit is
7984
refused; lower the count, context or prompt-cache budget. `--prompt-cache-over-cap` overrides only the prompt-cache

‎zig/src/native/cache_fit.zig‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ pub const Fit = struct { budget: u64, room: u64, cap: u64 };
1616

1717
/// For `total` bytes of RAM and `ready` once loaded: `gib` if it fits (or `over`), else what fits up to the ceiling.
1818
pub fn fit(total: u64, ready: u64, gib: ?f64, over: bool) error{CacheOverCap}!Fit {
19-
const cap = total / 100 * SHARE_PERCENT;
19+
return fitUnder(total / 100 * SHARE_PERCENT, ready, gib, over);
20+
}
21+
22+
/// `fit` under a given ceiling `cap` (bytes) instead of 70% of RAM: a load limit covers the cache as well.
23+
pub fn fitUnder(cap: u64, ready: u64, gib: ?f64, over: bool) error{CacheOverCap}!Fit {
2024
const room = cap -| ready -| MARGIN;
2125
const want = if (gib) |g| (if (g > 0) std.math.lossyCast(u64, g * GiB) else 0) else @min(room, DEFAULT_CEILING);
2226
if (want > room and !over) return error.CacheOverCap;
@@ -60,6 +64,11 @@ pub fn budget(gib: ?f64, over: bool, spare: u64, a: std.mem.Allocator, why: *[]c
6064

6165
/// budget, with `later` bytes the server takes after load (streams' caches on first use) counted as in use.
6266
pub fn budgetBeside(gib: ?f64, over: bool, spare: u64, later: u64, a: std.mem.Allocator, why: *[]const u8, rank: []const u8) !api.PromptCachePlan {
67+
return budgetUnder(null, gib, over, spare, later, a, why, rank);
68+
}
69+
70+
/// budgetBeside under a load limit `limit` (bytes; null: 70% of RAM), so the cache and the rest stay under one ceiling.
71+
pub fn budgetUnder(limit: ?u64, gib: ?f64, over: bool, spare: u64, later: u64, a: std.mem.Allocator, why: *[]const u8, rank: []const u8) !api.PromptCachePlan {
6372
const total = ram() orelse 0;
6473
const loaded = footprint() orelse 0;
6574
if (total == 0 or loaded == 0) {
@@ -69,13 +78,16 @@ pub fn budgetBeside(gib: ?f64, over: bool, spare: u64, later: u64, a: std.mem.Al
6978
}
7079
const ready = loaded + later;
7180
const streams = if (later > 0) try std.fmt.allocPrint(a, " and {d:.1} GiB its streams take on first use", .{gibs(later)}) else "";
72-
const f = fit(total, ready, gib, over) catch |e| {
73-
const left = (fit(total, ready, null, false) catch unreachable).room; // without a given budget it never refuses
74-
why.* = try std.fmt.allocPrint(a, "--prompt-cache-gib {d} would take this server past 70% of this Mac's memory: it holds {d:.1} GiB once loaded{s}, 70% of {d:.0} GiB is {d:.1} GiB, and {d} GiB stays free for prompt buffers, so {d:.1} GiB is left for kept prompt states. Pass a smaller one{s}, leave --prompt-cache-gib out for the default {d:.1} GiB, or add --prompt-cache-over-cap to keep {d} GiB anyway.", .{ gib.?, gibs(loaded), streams, gibs(total), gibs(total / 100 * SHARE_PERCENT), MARGIN >> 30, gibs(left), if (later > 0) ", serve a smaller --parallel" else "", gibs(@min(left, DEFAULT_CEILING)), gib.? });
81+
const cap = limit orelse total / 100 * SHARE_PERCENT;
82+
const f = fitUnder(cap, ready, gib, over) catch |e| {
83+
const left = (fitUnder(cap, ready, null, false) catch unreachable).room; // without a given budget it never refuses
84+
if (limit != null) {
85+
why.* = try std.fmt.allocPrint(a, "--prompt-cache-gib {d} would take this server past its load limit: it holds {d:.1} GiB once loaded{s}, the limit is {d:.1} GiB, and {d} GiB stays free for prompt buffers, so {d:.1} GiB is left for kept prompt states. Pass a smaller one{s}, leave --prompt-cache-gib out for the default {d:.1} GiB, raise the limit, or add --prompt-cache-over-cap to keep {d} GiB anyway.", .{ gib.?, gibs(loaded), streams, gibs(cap), MARGIN >> 30, gibs(left), if (later > 0) ", serve a smaller --parallel" else "", gibs(@min(left, DEFAULT_CEILING)), gib.? });
86+
} else why.* = try std.fmt.allocPrint(a, "--prompt-cache-gib {d} would take this server past 70% of this Mac's memory: it holds {d:.1} GiB once loaded{s}, 70% of {d:.0} GiB is {d:.1} GiB, and {d} GiB stays free for prompt buffers, so {d:.1} GiB is left for kept prompt states. Pass a smaller one{s}, leave --prompt-cache-gib out for the default {d:.1} GiB, or add --prompt-cache-over-cap to keep {d} GiB anyway.", .{ gib.?, gibs(loaded), streams, gibs(total), gibs(cap), MARGIN >> 30, gibs(left), if (later > 0) ", serve a smaller --parallel" else "", gibs(@min(left, DEFAULT_CEILING)), gib.? });
7587
return e;
7688
};
7789
const by_default = if (gib == null and f.room > f.budget) std.fmt.comptimePrint("; {d} GiB by default: --prompt-cache-gib takes more, up to what is free", .{DEFAULT_CEILING >> 30}) else "";
78-
std.log.info("prompt cache: {d:.1} GiB from {d:.1} GiB free under the 70% cap ({d:.1} GiB in use once loaded{s}, {d:.0} GiB of RAM, {d} GiB kept for prompt buffers){s}{s}{s}", .{ gibs(f.budget), gibs(f.room), gibs(loaded), streams, gibs(total), MARGIN >> 30, if (f.budget > f.room) ", past the cap by --prompt-cache-over-cap" else "", rank, by_default });
90+
std.log.info("prompt cache: {d:.1} GiB from {d:.1} GiB free under the {s} ({d:.1} GiB in use once loaded{s}, {d:.0} GiB of RAM, {d} GiB kept for prompt buffers){s}{s}{s}", .{ gibs(f.budget), gibs(f.room), if (limit != null) "load limit" else "70% cap", gibs(loaded), streams, gibs(total), MARGIN >> 30, if (f.budget > f.room) ", past the cap by --prompt-cache-over-cap" else "", rank, by_default });
7991
return .{
8092
.source = .physical_footprint,
8193
.budget_bytes = f.budget,
@@ -116,6 +128,25 @@ test "the default stops at 16 GiB, a given budget takes up to the room, and 0 tu
116128
try std.testing.expectEqual(@as(u64, 0), (try fit(ram128, ready, 0, false)).budget);
117129
}
118130

131+
test "under a load limit the automatic cache gets what the limit leaves, and a given one is held to the same limit" {
132+
// 100 GiB of RAM, a 60 GiB load limit, 50 GiB in use once the sessions are allocated (the case in review)
133+
const cap: u64 = 60 * GiB;
134+
const ready: u64 = 50 * GiB;
135+
const auto = try fitUnder(cap, ready, null, false);
136+
try std.testing.expectEqual(@as(u64, 8 * GiB), auto.budget); // 60 - 50 - 2, not the 16 GiB default 70% of RAM allows
137+
try std.testing.expect(ready + MARGIN + auto.budget <= cap);
138+
try std.testing.expectEqual(DEFAULT_CEILING, (try fit(100 * GiB, ready, null, false)).budget); // 70% of RAM: past the limit
139+
try std.testing.expectEqual(@as(u64, 8 * GiB), (try fitUnder(cap, ready, 8, false)).budget);
140+
try std.testing.expectError(error.CacheOverCap, fitUnder(cap, ready, 16, false));
141+
try std.testing.expectEqual(@as(u64, 16 * GiB), (try fitUnder(cap, ready, 16, true)).budget); // the override still passes it
142+
// a raised limit raises both: past 70% of RAM a given cache that fits under the limit needs no override, and the
143+
// automatic one still stops at the default ceiling
144+
const raised: u64 = 80 * GiB;
145+
try std.testing.expectEqual(@as(u64, 24 * GiB), (try fitUnder(raised, ready, 24, false)).budget);
146+
try std.testing.expectError(error.CacheOverCap, fit(100 * GiB, 60 * GiB, 16, false));
147+
try std.testing.expectEqual(DEFAULT_CEILING, (try fitUnder(raised, ready, null, false)).budget);
148+
}
149+
119150
test "a given budget that fits is kept, a larger one is refused unless the override is set" {
120151
const ram256: u64 = 256 * GiB;
121152
try std.testing.expectEqual(@as(u64, 4 * GiB), (try fit(ram256, 172 * GiB, 4, false)).budget);

‎zig/src/native/flashnext_batch_host.zig‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ 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.
24+
/// What the server may hold in all, sessions and prompt cache: 70% of RAM, or FLASHNEXT_LOAD_LIMIT_GB (GB, as
25+
/// GLM_LOAD_LIMIT_GB) up to the GPU's recommended working set; past the default the log names the risk, as GLM's does.
2626
fn ceiling(total: u64, working_set: u64) error{BadLoadLimit}!u64 {
2727
const text: ?[]const u8 = if (std.c.getenv("FLASHNEXT_LOAD_LIMIT_GB")) |v| std.mem.span(v) else null;
2828
const c = fit.ceiling(total, working_set, text) catch |err| {
2929
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.?});
3030
return err;
3131
};
3232
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) });
33+
if (c.bytes > c.default) std.log.warn("flash next: FLASHNEXT_LOAD_LIMIT_GB lets the server (sessions and prompt cache) 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) });
3434
return c.bytes;
3535
}
3636

@@ -65,7 +65,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, dir: []const u8, dump: ?[]const
6565
h.clock = .{ .io = io };
6666
h.core = lanes.Engine.init(gpa, &h.cfg, h.back.backend(), h.clock.clock());
6767
errdefer h.core.deinit();
68-
const plan = try fit.budget(cache_gib, over, 0, a, why, "");
68+
const plan = try fit.budgetUnder(cap, cache_gib, over, 0, 0, a, why, ""); // the cache under the same limit as the sessions
6969
eng.snap_pool.max = plan.budget_bytes;
7070
h.host = api.LaneHost.init(gpa, io, &h.core, .{ .name = "flashnext-zig", .lanes = slots, .greedy_only = true, .context_window = @intCast(window), .prefill_step = @intCast(eng.pr.step), .prompt_cache_plan = plan });
7171
h.cache = null;

‎zig/src/native/metal.zig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ fn openFlashNext(a: Allocator, gpa: Allocator, io: std.Io, o: api.Open, problem:
321321
}
322322
var why: []const u8 = "";
323323
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| {
324-
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)});
324+
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 and the given prompt cache do not fit under the load limit (70% of RAM, or FLASHNEXT_LOAD_LIMIT_GB) past the loaded model; fewer streams, a smaller --context or --prompt-cache-gib, or a higher FLASHNEXT_LOAD_LIMIT_GB" else try std.fmt.allocPrint(a, "the native Flash Next batching engine cannot open ({s})", .{@errorName(err)});
325325
return null;
326326
};
327327
}

0 commit comments

Comments
 (0)