Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
477403d
glm: a kept state that goes stale drops its cache entry at once
heitke Oct 14, 2026
8be4d32
glm: expire dead cache states at the host boundary, borrow safely
heitke Oct 14, 2026
027ddea
Merge remote-tracking branch 'origin/main' into glm-cache-slot-guard
heitke Oct 14, 2026
5a4987b
Living Weights: the learner, pocket and fits move to zig/src/living, …
ZackSample Oct 15, 2026
75978a1
Living Weights learns into any family described as data: the family's…
ZackSample Oct 15, 2026
c6418f3
Flash Next's embedding row matches the host dequant of the same MLX g…
AdrianBinDC Oct 7, 2026
1dca5c1
Layer 0's first hyper-connection matches its host check on one token,…
AdrianBinDC Oct 7, 2026
cd57bfc
Layer 0's linear-attention projection matches the host group-32 dot o…
AdrianBinDC Oct 7, 2026
37dec96
Layer 0's linear-attention convolution, recurrence and output multipl…
AdrianBinDC Oct 7, 2026
2590dfc
Layer 0's second hyper-connection matches its host check on one token…
AdrianBinDC Oct 7, 2026
58fed6c
Layer 0's experts match the host check on one token, the top 10 and t…
AdrianBinDC Oct 7, 2026
69c4888
Layer 3's full attention matches the host check on one decode token, …
AdrianBinDC Oct 7, 2026
8a4add2
Layer 7's full attention matches the host check on one decode token, …
AdrianBinDC Oct 7, 2026
40472fb
Layer 11's full attention matches the host check on one decode token,…
AdrianBinDC Oct 7, 2026
307312a
Layer 15's full attention matches the host check on one decode token,…
AdrianBinDC Oct 7, 2026
d91b049
Layer 19's full attention matches the host check on one decode token,…
AdrianBinDC Oct 7, 2026
fcd0c15
Layer 23's full attention matches the host check on one decode token,…
AdrianBinDC Oct 7, 2026
0b4eab2
Layer 27's full attention matches the host check on one decode token,…
AdrianBinDC Oct 7, 2026
fbb2776
Layer 31's full attention matches the host check on one decode token,…
AdrianBinDC Oct 7, 2026
43961f6
Layer 35's full attention matches the host check on one decode token,…
AdrianBinDC Oct 7, 2026
8949cd1
Layer 39's full attention matches the host check on one decode token,…
AdrianBinDC Oct 7, 2026
2bb8a78
Layer 43's full attention matches the host check on one decode token,…
AdrianBinDC Oct 7, 2026
8c50b42
Layer 47's full attention matches the host check on one decode token,…
AdrianBinDC Oct 7, 2026
1bbed57
Layer 2's n-gram table matches the host dequant on one decode token, …
AdrianBinDC Oct 7, 2026
994c2ff
The final mix and the word scores match the host check on one decode …
AdrianBinDC Oct 7, 2026
7f0794d
One drafts-off prompt matches the Python engine, eight greedy token i…
AdrianBinDC Oct 7, 2026
0d04c43
Keep the drafts-off prompt match after splitting the forward and pack…
AdrianBinDC Oct 7, 2026
785896e
Flash Next CUDA: prompt ids and positions upload on the compute strea…
ZackSample Oct 11, 2026
8391cac
native/cuda.zig takes each CUDA family's prompt-cache copies, cache r…
ZackSample Oct 15, 2026
bd35940
CUDA experts over a window's or a prompt chunk's rows, each row with …
ZackSample Oct 15, 2026
2484c93
triton_aot_pack.py: a constexpr Triton folded from None is left out, …
ZackSample Oct 15, 2026
5cf4291
Flash Next CUDA: prompts and decode rounds on one batched pass, with …
ZackSample Oct 15, 2026
035b7f9
Flash Next CUDA: the MTP head, its draft chains, verify windows and t…
ZackSample Oct 15, 2026
1ac9bd0
Flash Next CUDA serves several streams through the lane core: drafted…
ZackSample Oct 15, 2026
62865ab
Flash Next CUDA: buffers are zeroed on the compute stream, so a pass …
ZackSample Oct 15, 2026
8d32cd3
Docs: Flash Next on CUDA, its recipe, the CUDA and Flash Next pages, …
ZackSample Oct 15, 2026
b45e26a
Flash Next CUDA refuses the config.json settings its kernels fix: RMS…
ZackSample Oct 15, 2026
9fa628b
Flash Next CUDA: a failed zero fill frees the buffer it was filling
ZackSample Oct 15, 2026
475df92
Flash Next CUDA: a pass's uploads read pinned staging the scratch own…
ZackSample Oct 15, 2026
1e86036
Core: router logits and softmax top-k rows for every CUDA family (rou…
ZackSample Oct 15, 2026
794d273
Core: token rows in groups of 32 with a copy a stream, and RMS rows w…
ZackSample Oct 15, 2026
ea7d098
Flash Next CUDA: its own glue kernels, flashnext_hc.cu and flashnext_…
ZackSample Oct 15, 2026
38c9c63
Flash Next CUDA boots on its own glue; a captured set that holds ever…
ZackSample Oct 15, 2026
235ea01
Flash Next CUDA: TF_FLASHNEXT_OWNED runs named entry points on our gl…
ZackSample Oct 15, 2026
7746bb9
Docs: Flash Next on CUDA boots on its own kernels; a captured set is …
ZackSample Oct 15, 2026
e7163c5
Flash Next CUDA: each layer_types label must be the exact role the ke…
ZackSample Oct 15, 2026
4b46157
Flash Next CUDA: a failed upload or record drains the stream before t…
ZackSample Oct 15, 2026
354f60d
CUDA: a Gated DeltaNet layer's input-gradient backward, every gradien…
ZackSample Oct 15, 2026
c106f2e
CUDA: input gradients of 4-bit (group 32) products, routed SwiGLU exp…
ZackSample Oct 15, 2026
a41cc2c
CUDA: gated attention's input gradients over 4-bit projections (a hea…
ZackSample Oct 15, 2026
0211f4d
Flash Next CUDA: the hyper-connections' and the pocket's input gradie…
ZackSample Oct 15, 2026
8e234db
Merge lw-b-core (#878, Living Weights as a core module) onto lw-c-fnb…
ZackSample Oct 15, 2026
e2779f6
Flash Next CUDA: a taught folder's pocket in every pass, and the batc…
ZackSample Oct 15, 2026
41ad4d4
Flash Next CUDA: Living Weights' trainer over the batched pass, and f…
ZackSample Oct 15, 2026
f5d6a4c
Living Weights learns into Qwen3.8 Flash Next on CUDA (--slide, GB10)…
ZackSample Oct 15, 2026
bad3a2a
Docs: Living Weights on Qwen3.8 Flash Next (CUDA, GB10), as measured;…
ZackSample Oct 15, 2026
514e4ee
Flash Next's pocket leaves an untaught stream's rows: a lesson writer…
ZackSample Oct 16, 2026
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
Next Next commit
glm: a kept state that goes stale drops its cache entry at once
A resident prompt state died (its slot's rows overwritten, or its pool
blocks given up) left the Store serving the dead entry: every later
lookup found it, its restore failed, and the pass prefilled the whole
prompt from the start — the repeated full-refill cascade seen on live
servers with long conversations. The backend now tells the prompt cache
the moment a state goes stale (Slots.expire), so the Store drops that
entry immediately and the next turn keeps a fresh state instead of
paying for the same failure again.
  • Loading branch information
heitke committed Oct 14, 2026
commit 477403dfce496f0f345772408089250b42ea8c7f
22 changes: 11 additions & 11 deletions zig/src/core/prompt_cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ const learned = @import("prompt_learn.zig");
/// A family's copy of one state.
pub const Saved = *anyopaque;

pub fn singleReclaim(_: *anyopaque, dir: [:0]const u8, key: u64) !u64 {
var buf: [1100]u8 = undefined;
return @import("lanes").learned_dirs.fileBytes(try std.fmt.bufPrintSentinel(&buf, "{s}/{x:0>16}.bin", .{ dir, key }, 0));
}
pub fn singleForgetChecked(_: *anyopaque, dir: [:0]const u8, key: u64) !void {
var buf: [1100]u8 = undefined;
try @import("lanes").learned_dirs.unlink(try std.fmt.bufPrintSentinel(&buf, "{s}/{x:0>16}.bin", .{ dir, key }, 0));
}
pub const singleReclaim = imprint.singleReclaim; // the learned-state file helpers, beside the Imprint that owns them
pub const singleForgetChecked = imprint.singleForgetChecked;

/// What a family gives the cache: copies of its state while the prompt pass stands at a chunk end.
pub const Snapshots = struct {
ptr: *anyopaque,
Expand Down Expand Up @@ -114,9 +109,6 @@ pub const Counts = struct { hits: u64 = 0, misses: u64 = 0, kept: u64 = 0, evict
/// Where a prompt pass starts and where it stops to keep its state (marks sorted, in `a`).
pub const Plan = struct { from: u32 = 0, marks: []const u32 = &.{} };

/// A paged store's `keep`: whether the mark holds a state, and how many leading pages the stream swaps for the store's.
pub const Kept = struct { held: bool, shared: usize };

/// The entry a backend restores itself (null: the pass starts at 0) and the pass's marks.
pub const Lookup = struct { entry: ?*Entry = null, marks: []const u32 = &.{} };

Expand Down Expand Up @@ -176,6 +168,14 @@ pub const Store = struct {
return false;
}

/// A state that can never restore again (its rows were overwritten) leaves the store at once.
pub fn expired(s: *Store, saved: Saved) void {
for (s.entries.items, 0..) |e, i| if (e.tokens.len > 0 and e.saved == saved) {
s.counts.evicted += 1;
return s.remove(i);
};
}

/// What the budget leaves past the kept states and the spare storage: a family readies storage only inside it.
pub fn room(s: *const Store) u64 {
return s.budget -| s.held -| s.spare();
Expand Down
23 changes: 23 additions & 0 deletions zig/src/core/prompt_cache_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,26 @@ test "a kept state grows only into what the budget leaves, and evicts nothing to
var stranger: u8 = 0;
try std.testing.expect(!s.grow(&stranger, 0)); // a state the store doesn't hold
}

test "a state the backend reports dead leaves the store at once: the next turn never serves it" {
const gpa = std.testing.allocator;
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
const a = arena.allocator();
var f: Fake = .{ .gpa = gpa };
var s = Store.init(gpa, f.snapshots(), .{ .min_prompt = 0 }, 1000);
defer s.deinit();
const t1 = [_]u32{ 1, 2, 3, 4, 5, 6 };
_ = f.pass(&s, &t1, try s.begin(a, &t1, 4, &.{}, &.{}, null, &.{}));
const t2 = [_]u32{ 9, 9, 9, 9, 9, 9 };
_ = f.pass(&s, &t2, try s.begin(a, &t2, 5, &.{}, &.{}, null, &.{}));
const e1 = s.find(&t1, &.{}, &.{}).?;
try std.testing.expectEqual(@as(usize, 2), s.entries.items.len);
s.expired(e1.saved); // the backend overwrote its rows: no later turn may look it up
try std.testing.expectEqual(@as(usize, 1), s.entries.items.len);
try std.testing.expectEqual(@as(?*pc.Entry, null), s.find(&t1, &.{}, &.{}));
try std.testing.expectEqual(@as(u64, 1), s.counts.evicted);
try std.testing.expectEqual(@as(usize, 1), f.live); // the fake's state storage went with it
s.expired(e1.saved); // a state the store no longer holds: nothing happens
try std.testing.expectEqual(@as(usize, 1), s.entries.items.len);
}
12 changes: 12 additions & 0 deletions zig/src/core/prompt_imprint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,18 @@ fn makePath(gpa: Allocator, dir: [:0]const u8) !void {
if (rc != 0 and std.c.errno(rc) != .EXIST) return error.ImprintDir;
}

/// A learned state's file under `imprint.dir`, read whole into a count (the single-rank reclaim).
pub fn singleReclaim(_: *anyopaque, dir: [:0]const u8, key: u64) !u64 {
var buf: [1100]u8 = undefined;
return @import("lanes").learned_dirs.fileBytes(try std.fmt.bufPrintSentinel(&buf, "{s}/{x:0>16}.bin", .{ dir, key }, 0));
}

/// A learned state's file under `imprint.dir` removed (the cap needs room, or it no longer reads back).
pub fn singleForgetChecked(_: *anyopaque, dir: [:0]const u8, key: u64) !void {
var buf: [1100]u8 = undefined;
try @import("lanes").learned_dirs.unlink(try std.fmt.bufPrintSentinel(&buf, "{s}/{x:0>16}.bin", .{ dir, key }, 0));
}

test {
_ = @import("prompt_imprint_test.zig");
_ = @import("prompt_imprint_fault_test.zig");
Expand Down
3 changes: 2 additions & 1 deletion zig/src/core/prompt_radix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub const Rules = pc.Rules;
pub const Limits = struct { pages: usize = 0, snaps: usize = 0 };

pub const Plan = pc.Plan;
pub const Kept = pc.Kept;
/// A paged store's `keep`: whether the mark holds a state, and how many leading pages the stream swaps for the store's.
pub const Kept = struct { held: bool, shared: usize };

pub const Store = struct {
tree: Tree,
Expand Down
43 changes: 42 additions & 1 deletion zig/src/families/glm/kept.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ pub const Grow = struct {
f: *const fn (ctx: *anyopaque, saved: *anyopaque, extra: u64) bool,
};

/// The prompt cache's ear: a kept state went stale (never restorable), so its entry is dropped.
pub const Expire = struct {
ctx: *anyopaque,
f: *const fn (ctx: *anyopaque, saved: *anyopaque) void,
};

/// Keep slot `i`'s prompt state at `at` tokens (its pass stands there) as snapshot `id` (0: the next); `home`: resident.
pub fn save(sl: *Slots, i: u32, at: u32, id: u32, home: bool) !*snapshot.Snap {
const slot = try sl.slotAt(i);
Expand Down Expand Up @@ -51,11 +57,17 @@ pub fn usable(sl: *const Slots, snap: *const snapshot.Snap) bool {
return sl.snaps.get(snap.id) == snap and !snap.stale;
}

/// A state went stale (its rows are gone): the prompt cache drops its entry, so no turn serves it again.
pub fn wentStale(sl: *Slots, snap: *snapshot.Snap) void {
if (sl.expire) |x| x.f(x.ctx, snap);
}

/// Slot `i` writes its MLA rows from `at` on: its resident states past `at` that were not copied out go stale.
pub fn overwrite(sl: *Slots, i: u32, at: u32) void {
var it = sl.snaps.valueIterator();
while (it.next()) |snap| if (snap.*.home == i and snap.*.at > at) {
while (it.next()) |snap| if (snap.*.home == i and snap.*.at > at and !snap.*.stale) {
snap.*.stale = true;
wentStale(sl, snap.*);
};
}

Expand Down Expand Up @@ -273,3 +285,32 @@ test "a slot copies out only what the budget grants, and rank 1 copies only ids
try std.testing.expectError(error.SnapshotOutOfStep, copyIds(&sl, 0, &.{1})); // stale
try copyIds(&sl, 0, &.{});
}

test "a state that goes stale tells the prompt cache, once, so its entry is dropped at once" {
const gpa = std.testing.allocator;
var slots: [2]Slot = undefined;
for (&slots) |*s| s.* = .{ .s = undefined, .held = undefined, .last = undefined };
var sl = testSlots(&slots);
defer sl.snaps.deinit(gpa);
var states: [3]snapshot.Snap = undefined;
for (&states, [_]u32{ 0, 0, 1 }, [_]u32{ 64, 128, 64 }, 1..) |*x, home, at, k| {
x.* = .{ .id = @intCast(k), .at = at, .buf = undefined, .bytes = 0, .home = home };
try sl.snaps.put(gpa, x.id, x);
}
const Ear = struct {
var expired: [2]u32 = .{ 0, 0 };
var calls: usize = 0;
fn f(_: *anyopaque, saved: *anyopaque) void {
const snap: *snapshot.Snap = @ptrCast(@alignCast(saved));
expired[calls % 2] = snap.id;
calls += 1;
}
};
var unused: u8 = 0;
sl.expire = .{ .ctx = &unused, .f = Ear.f };
overwrite(&sl, 0, 32); // states 1 and 2 lie past 32 in slot 0; state 3 is slot 1's
try std.testing.expect(states[0].stale and states[1].stale and !states[2].stale);
try std.testing.expectEqual(@as(usize, 2), Ear.calls); // each dead state reported once
overwrite(&sl, 0, 16); // already stale: no second report
try std.testing.expectEqual(@as(usize, 2), Ear.calls);
}
2 changes: 2 additions & 0 deletions zig/src/families/glm/pool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const snapshot = @import("snapshot.zig");
const Engine = @import("engine.zig").Engine;
const Slots = @import("slots.zig").Slots;
const Slot = @import("slots.zig").Slot;
const kept_mod = @import("kept.zig");

/// A stream's span of the pool, in blocks of `Pool.block` tokens.
pub const Region = struct { start: u32, blocks: u32 };
Expand Down Expand Up @@ -126,6 +127,7 @@ fn evictHeld(sl: *Slots, spare: ?*const snapshot.Snap) bool {
const snap = pick orelse return false;
snap.held = null;
snap.stale = true;
kept_mod.wentStale(sl, snap);
return true;
}

Expand Down
1 change: 1 addition & 0 deletions zig/src/families/glm/slots.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub const Slots = struct {
launched: ?mtl.CommandBuffer = null, // keeps and drafts committed ahead: the window after them encodes while they run
digest: u64 = 0, // the last window's picks hashed: a pair's ranks compare theirs
grow: ?kept_mod.Grow = null, // the prompt cache's leave to make a resident state whole before its slot writes below it
expire: ?kept_mod.Expire = null, // the prompt cache's ear: a state that went stale can never restore, so its entry goes
ring: Ref, // u32 [ring_len]: tokens by handle, a queued round's input read from its slot on the GPU
ring_at: u32 = 0,
ring_cb: [ring_len]?mtl.CommandBuffer = @splat(null), // the queued round writing each slot (retained) until read
Expand Down
7 changes: 7 additions & 0 deletions zig/src/native/glm_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub fn open(gpa: Allocator, io: std.Io, dir: []const u8, window: u32, speed_up:
h.host.cache = &h.cache.?;
h.host.info_.prompt_cache = true;
h.slots.grow = .{ .ctx = h, .f = growSnap }; // a resident state made whole before its slot writes below it
h.slots.expire = .{ .ctx = h, .f = expireSnap }; // a state that went stale drops its cache entry at once
}
errdefer if (h.cache) |*store| store.deinit();
if (learn) |root| {
Expand Down Expand Up @@ -141,6 +142,12 @@ fn growSnap(ctx: *anyopaque, saved: *anyopaque, extra: u64) bool {
return if (h.cache) |*store| store.grow(saved, extra) else false;
}

/// Slots.expire: a state that lost its rows can never restore, so its entry leaves the store at once.
fn expireSnap(ctx: *anyopaque, saved: *anyopaque) void {
const h: *Host = @ptrCast(@alignCast(ctx));
if (h.cache) |*store| store.expired(saved);
}

fn follow(h: *Host) void {
glm.mirror.follow(h.eng, &h.slots) catch |err| std.log.err("speed-up mode: following rank 0 ended: {s}", .{@errorName(err)});
}
Expand Down
Loading