Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
the dashboard's fill and memory numbers say what they mean
A /stats poll raced the engine thread's completion path: deliver() freed a
job that was still in the host's admitted list, and the fill-total reader
walked that list, so a poll at the wrong moment read freed memory. A job now
leaves the lists inside deliver, before finish runs it down; the status tests
poll at the finished event, where the old order still counted the dying job.

The GLM host's memory tile put the configured load limit in the peak field.
The limit is a plan, not a measurement: it moves to a limit field of its own,
the peak is the kernel's lifetime footprint peak, and the tile labels each
number for what it is. Mac family hosts share the reading through
cache_fit.hostMemory.

The page's ETA mixed the drains of different prompts whenever one fill
followed another without an idle poll between. The snapshot now carries
fill_run, a marker that changes when the filling set does, and the page
resets its samples on it; the label says the queue drains, which is what the
aggregate of several lanes' remaining work estimates.

Nemotron's prompt pass advanced fill_at past what the GPU had landed, so the
countdown reached zero while the last chunk still ran. It stores landed
tokens now, and a restored prefix counts as done the moment it lands.
  • Loading branch information
heitke committed Oct 14, 2026
commit ed8cdc92cadb66df55ab242099cb20fbd4ef6993
6 changes: 4 additions & 2 deletions zig/src/core/engine_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ pub const Open = struct {
/// An opened engine; ``close`` stops its thread and frees its backend.
pub const Opened = struct { engine: Engine, close: *const fn (ctx: *anyopaque) void, ctx: *anyopaque };

pub const Memory = struct { active: u64 = 0, cache: u64 = 0, peak: u64 = 0, pinned: u64 = 0 };
pub const Memory = struct { active: u64 = 0, cache: u64 = 0, peak: u64 = 0, pinned: u64 = 0, limit: ?u64 = null };

/// A backend's words for its own refusals (a request it cannot serve); null: the error's name.
pub const Explain = struct {
Expand Down Expand Up @@ -258,6 +258,8 @@ pub const Status = struct {
/// Filling prompts' total tokens and those still to prefill (null: no prompt is going in).
fill_prompt_tokens: ?u32 = null,
fill_tokens_left: ?u32 = null,
/// Changes whenever the filling set does: a reader pairs its drain samples with one fill.
fill_run: u64 = 0,
};

pub const SubmitError = error{ Closed, Busy, InvalidSpans };
Expand Down Expand Up @@ -415,9 +417,9 @@ test {
_ = @import("lane_host.zig");
_ = @import("lane_host_test.zig");
_ = @import("lane_host_fill_test.zig");
_ = @import("lane_host_status_test.zig");
_ = @import("lane_host_reuse_test.zig");
_ = @import("lane_host_warm_test.zig");
_ = @import("lane_host_fill_test.zig");
_ = @import("prompt_cache.zig");
_ = @import("prompt_radix.zig");
_ = @import("prompt_imprint.zig");
Expand Down
14 changes: 6 additions & 8 deletions zig/src/core/lane_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,11 @@ pub const LaneHost = struct {
}
}

/// Sends a stream's new tokens; true once it has finished (its job freed).
/// Sends a stream's new tokens; true once it has finished and left the lists a status read walks.
pub fn deliver(h: *LaneHost, job: *Job) bool {
h.send(job);
if (!job.stream.finished) return false;
h.remove(job);
const reason: Reason = switch (job.stream.reason) {
.length => .length,
.cancelled => .cancelled,
Expand Down Expand Up @@ -350,7 +351,7 @@ pub const LaneHost = struct {
else => h.drop(job, h.words(e)),
};
h.prefilled(job, began);
if (h.deliver(job)) h.remove(job);
_ = h.deliver(job);
return true;
}

Expand All @@ -375,7 +376,7 @@ pub const LaneHost = struct {
if (!done) return;
_ = h.filling.orderedRemove(at);
h.prefilled(job, job.began);
if (h.deliver(job)) h.remove(job);
_ = h.deliver(job);
}

/// A reply's grammar from the server's compiler (Info.structures engines get requests that carry one).
Expand Down Expand Up @@ -519,11 +520,8 @@ pub const LaneHost = struct {
var i: usize = 0;
while (i < h.admitted.items.len) {
const job = h.admitted.items[i];
if (h.deliver(job)) {
h.lock();
_ = h.admitted.orderedRemove(i);
h.unlock();
} else i += 1;
if (h.deliver(job)) continue; // it left the list: the next job slid into slot i
i += 1;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion zig/src/core/lane_host_lone.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ pub fn run(h: *LaneHost, job: *Job, began: i96) bool {
h.send(job);
return true;
}
if (h.deliver(job)) h.remove(job);
_ = h.deliver(job); // a finished job leaves the lists inside deliver, before finish frees it
return true;
}
5 changes: 4 additions & 1 deletion zig/src/core/lane_host_status.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ pub fn read(ctx: *anyopaque, out: *api.Status, stream_tokens: []u32) void {
fillTotals(h, out);
}

/// A started job whose prompt pass has not reported yet is filling: sum its prompt and what is left of it.
/// A started job whose prompt pass has not reported yet is filling; `fill_run` hashes the filling jobs' ids.
fn fillTotals(h: *LaneHost, out: *api.Status) void {
var total: u64 = 0;
var left: u64 = 0;
var filling = false;
var run: u64 = 0;
for (h.admitted.items) |job| {
if (!job.started or job.prefilled != null or job.stream.finished) continue;
filling = true;
const prompt = job.request.prompt.len;
total += prompt;
left += prompt -| job.stream.fill_at.load(.monotonic);
run = run *% 0x9E3779B97F4A7C15 ^ job.id ^ @as(u64, @truncate(@as(u96, @bitCast(job.began))));
}
if (!filling) return;
out.fill_prompt_tokens = @intCast(total);
out.fill_tokens_left = @intCast(left);
out.fill_run = run ^ total;
}

/// `n` tokens delivered now; marks older than the window drop.
Expand Down
216 changes: 216 additions & 0 deletions zig/src/core/lane_host_status_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
//! Status reads stay lifetime-safe: a finishing job has left the host's lists before its event fires.
const std = @import("std");
const lanes = @import("lanes");
const api = @import("engine_api.zig");
const LaneHost = @import("lane_host.zig").LaneHost;
const Reason = api.Reason;
const Id = api.Id;
const Event = api.Event;
const Request = api.Request;
const Engine = api.Engine;

const short_prompt = [_]u32{ 3, 1, 4, 1, 5, 9, 2, 6 };

// The finished event fires inside `finish`, before the host frees the job: this is the racing poll.
test "status at a request's finished event no longer counts the job that just ended" {
const gpa = std.testing.allocator;
var cfg = try lanes.Config.init(gpa, .{ .exact_width = 8, .gpu_tokens = true, .hidden_rows = true }, 8, 7);
defer cfg.deinit(gpa);
var target: lanes.fake.Fake = .{ .gpa = gpa, .prefill_chunks = 2 };
defer target.deinit();
var clock: lanes.fake.FixedClock = .{};
var core = lanes.Engine.init(gpa, &cfg, target.stepped(), clock.clock());
defer core.deinit();
var host = LaneHost.init(gpa, std.testing.io, &core, .{ .lanes = 2 });
try host.start();
defer host.stop();
const Watch = struct {
engine: Engine = undefined,
mutex: std.Io.Mutex = .init,
done: ?Reason = null,
running_at_finish: usize = 0,
fill_at_finish: bool = false,

fn event(ctx: *anyopaque, _: Id, e: *const Event) void {
const w: *@This() = @ptrCast(@alignCast(ctx));
w.mutex.lockUncancelable(std.testing.io);
defer w.mutex.unlock(std.testing.io);
switch (e.*) {
.finished => |f| {
var status: api.Status = .{};
w.engine.status(&status, &.{}); // the poll that raced with this completion
w.running_at_finish = status.running;
w.fill_at_finish = status.fill_tokens_left != null;
w.done = f.reason;
},
else => {},
}
}
fn wait(w: *@This()) Reason {
while (true) {
w.mutex.lockUncancelable(std.testing.io);
const d = w.done;
w.mutex.unlock(std.testing.io);
if (d) |r| return r;
std.Io.sleep(std.testing.io, .fromMilliseconds(1), .awake) catch {};
}
}
};
var watch: Watch = .{};
watch.engine = host.engine();
const request: Request = .{ .prompt = &short_prompt, .max_tokens = 4 };
try watch.engine.submit(1, &request, .{ .ctx = &watch, .event = Watch.event });
try std.testing.expectEqual(Reason.length, watch.wait());
try std.testing.expectEqual(@as(usize, 0), watch.running_at_finish); // it left `admitted` before the event
try std.testing.expect(!watch.fill_at_finish); // and its fill progress with it
var status: api.Status = .{};
watch.engine.status(&status, &.{});
try std.testing.expectEqual(@as(u32, 0), status.running); // the host is idle: nothing is listed
}

// A second request runs while the first completes: the poll at its finished event counts the survivor.
test "status at one request's finished event still counts the other request running" {
const gpa = std.testing.allocator;
var cfg = try lanes.Config.init(gpa, .{ .exact_width = 8, .gpu_tokens = true, .hidden_rows = true }, 8, 7);
defer cfg.deinit(gpa);
var target: lanes.fake.Fake = .{ .gpa = gpa, .prefill_chunks = 2 };
defer target.deinit();
var clock: lanes.fake.FixedClock = .{};
var core = lanes.Engine.init(gpa, &cfg, target.stepped(), clock.clock());
defer core.deinit();
var host = LaneHost.init(gpa, std.testing.io, &core, .{ .lanes = 2 });
try host.start();
defer host.stop();
const Watch = struct {
engine: Engine = undefined,
mutex: std.Io.Mutex = .init,
done: ?Reason = null,
running_at_finish: usize = 0,
fill_at_finish: bool = false,

fn event(ctx: *anyopaque, _: Id, e: *const Event) void {
const w: *@This() = @ptrCast(@alignCast(ctx));
w.mutex.lockUncancelable(std.testing.io);
defer w.mutex.unlock(std.testing.io);
switch (e.*) {
.finished => |f| {
var status: api.Status = .{};
w.engine.status(&status, &.{});
w.running_at_finish = status.running;
w.fill_at_finish = status.fill_tokens_left != null;
w.done = f.reason;
},
else => {},
}
}
fn wait(w: *@This()) Reason {
while (true) {
w.mutex.lockUncancelable(std.testing.io);
const d = w.done;
w.mutex.unlock(std.testing.io);
if (d) |r| return r;
std.Io.sleep(std.testing.io, .fromMilliseconds(1), .awake) catch {};
}
}
};
var first: Watch = .{};
first.engine = host.engine();
var second: Watch = .{};
second.engine = host.engine();
const quick: Request = .{ .prompt = &short_prompt, .max_tokens = 1 };
const long: Request = .{ .prompt = &short_prompt, .max_tokens = 100000 };
try second.engine.submit(1, &long, .{ .ctx = &second, .event = Watch.event });
try first.engine.submit(2, &quick, .{ .ctx = &first, .event = Watch.event });
try std.testing.expectEqual(Reason.length, first.wait());
try std.testing.expectEqual(@as(usize, 1), first.running_at_finish); // only the long one
try std.testing.expect(!first.fill_at_finish); // the long one decodes: it fills nothing
var status: api.Status = .{};
second.engine.status(&status, &.{});
try std.testing.expectEqual(@as(u32, 1), status.running);
second.engine.cancel(1);
try std.testing.expectEqual(Reason.cancelled, second.wait());
}

test "status polls through a cancellation, a failed admission and a requeue stay sane" {
const gpa = std.testing.allocator;
var cfg = try lanes.Config.init(gpa, .{ .exact_width = 8, .gpu_tokens = true, .hidden_rows = true }, 8, 7);
defer cfg.deinit(gpa);
var target: lanes.fake.Fake = .{ .gpa = gpa, .memory_lanes = 1 };
defer target.deinit();
var clock: lanes.fake.FixedClock = .{};
var core = lanes.Engine.init(gpa, &cfg, target.backend(), clock.clock());
defer core.deinit();
var host = LaneHost.init(gpa, std.testing.io, &core, .{ .lanes = 2 });
try host.start();
defer host.stop();
const e = host.engine();
const Poller = struct {
engine: Engine,
stop: std.atomic.Value(bool) = .init(false),
failed: std.atomic.Value(bool) = .init(false),

fn poll(ctx: *anyopaque) void {
const p: *@This() = @ptrCast(@alignCast(ctx));
while (!p.stop.load(.acquire)) {
var status: api.Status = .{};
p.engine.status(&status, &.{});
if (status.running > 2) p.failed.store(true, .release); // only two lanes exist
std.Io.sleep(std.testing.io, .fromMilliseconds(1), .awake) catch {};
}
}
};
var poller: Poller = .{ .engine = e };
const thread = try std.Thread.spawn(.{}, Poller.poll, .{&poller});
defer {
poller.stop.store(true, .release);
thread.join();
}
const running: Request = .{ .prompt = &short_prompt, .max_tokens = 100000 };
const small: Request = .{ .prompt = &short_prompt, .max_tokens = 4 };
var a = try gpa.create(Box);
defer gpa.destroy(a);
a.* = .{};
try e.submit(1, &running, .{ .ctx = a, .event = Box.event });
var b = try gpa.create(Box);
defer gpa.destroy(b);
b.* = .{};
try e.submit(2, &small, .{ .ctx = b, .event = Box.event }); // no memory for a second lane: it waits
std.Io.sleep(std.testing.io, .fromMilliseconds(20), .awake) catch {};
e.cancel(1);
try std.testing.expectEqual(Reason.cancelled, a.wait());
try std.testing.expectEqual(Reason.length, b.wait()); // admitted once the lane freed, then ran to its end
try std.testing.expect(!poller.failed.load(.acquire));
var status: api.Status = .{};
e.status(&status, &.{});
try std.testing.expectEqual(@as(u32, 0), status.running);
try std.testing.expect(status.fill_prompt_tokens == null and status.fill_tokens_left == null);
}

const Box = struct {
mutex: std.Io.Mutex = .init,
done: ?Reason = null,
message: []const u8 = "",
tokens: usize = 0,
fn event(ctx: *anyopaque, _: Id, e: *const Event) void {
const b: *@This() = @ptrCast(@alignCast(ctx));
b.mutex.lockUncancelable(std.testing.io);
defer b.mutex.unlock(std.testing.io);
switch (e.*) {
.tokens => |t| b.tokens += t.len,
.finished => |f| {
b.done = f.reason;
b.message = f.message;
},
else => {},
}
}
fn wait(b: *@This()) Reason {
while (true) {
b.mutex.lockUncancelable(std.testing.io);
const d = b.done;
b.mutex.unlock(std.testing.io);
if (d) |r| return r;
std.Io.sleep(std.testing.io, .fromMilliseconds(1), .awake) catch {};
}
}
};
3 changes: 2 additions & 1 deletion zig/src/families/glm/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@ pub const Backend = struct {
} else s.reuse_failed = true;
}
var next: usize = 0; // the planned chunk starts: a resumed pass cuts where a fresh one does
s.fill_at.store(at, .monotonic); // a restored prefix is done the moment it is restored
while (at < prompt.len) {
if (s.isCancelled()) return error.Cancelled; // the core releases the slot
while (next < s.chunks.len and s.chunks[next] <= at) next += 1;
const end: u32 = if (next < s.chunks.len) s.chunks[next] else @intCast(prompt.len);
const n = b.sl.chunkRows(at, end);
try b.copyOut(i, at);
try mirror.send(e, .chunk, &.{ i, at, n });
try b.sl.chunk(i, at, n);
try b.sl.chunk(i, at, n); // the chunk's compute completes here: `at` is landed progress
at += n;
s.fill_at.store(at, .monotonic); // the dashboard's prefill progress reads this between chunks
if (std.mem.indexOfScalar(u32, s.reuse.marks, at) != null) if (s.reuse.hook) |k| k.at(k.ptr, s, at);
Expand Down
3 changes: 2 additions & 1 deletion zig/src/families/nemotron/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ pub const Metal = struct {
s.cached = @intCast(at);
s.reuse_failed = at == 0;
}
s.fill_at.store(@intCast(at), .monotonic); // a restored prefix is done the moment it is restored
// a command buffer a chunk, each committed before the one ahead of it is waited on, so the GPU keeps a chunk queued
var k: usize = 0;
while (at < ids.len) {
Expand All @@ -324,7 +325,7 @@ pub const Metal = struct {
try self.submit(.prefill, self.next, Chunks{ .n = ids.len, .c = c, .at = at, .rows = rows });
while (self.flights.items.len > 1) try self.land();
at += rows;
s.fill_at.store(@intCast(at), .monotonic); // the dashboard's prefill progress reads this between chunks
s.fill_at.store(@intCast(at - rows), .monotonic); // landed tokens only: the newest chunk is still queued on the GPU
if (s.reuse.hook) |hk| if (std.mem.indexOfScalar(u32, s.reuse.marks, @intCast(at)) != null) hk.at(hk.ptr, s, @intCast(at));
}
if (self.trace and c.decide != lesson_route.none) try self.traceLessons(c);
Expand Down
Loading
Loading