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
cuda disk: skip the identity probe where the window leaves no mark, s…
…o contexts under 16 load
  • Loading branch information
shantanugoel authored and ashhart committed Nov 28, 2026
commit 05c8a006cd2bdd68880f99b77989bd3f8e69df2e
2 changes: 2 additions & 0 deletions zig/src/native/cuda_kept.zig
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ pub fn Kept(comptime V: type) type {
var toks: [4096 + 40 + 7]u32 = undefined;
for (&toks, 0..) |*x, i| x.* = @intCast(1000 + i * 7919 % 50000);
const count = if (@hasDecl(V, "context")) @min(toks.len, V.context(t) / 2) else toks.len;
// the mark sits seven rows before the pass's end, past row 0: a window under 16 has nothing to keep or hash
if (count < 8) return .{ .hash = V.probe_seed, .rate = 0 };
const Hook = struct {
t: *Target,
saved: ?*State = null,
Expand Down
60 changes: 60 additions & 0 deletions zig/src/native/cuda_kept_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const cuda = @import("cuda");
const api = @import("engine_api");
const kept = @import("cuda_kept.zig");
const disk = @import("cuda_disk.zig");
const lanes = @import("lanes");
const rows = api.prompt_cache.rows;
const t = std.testing;

Expand Down Expand Up @@ -365,6 +366,65 @@ test "CUDA kept: Flash Next's permit takes a state at its served window whole, w
}
}

/// `View` as the identity probe drives it: a served window, and a state kept at the mark its pass is asked for.
const Probed = struct {
pub const Target = View.Target;
pub const State = View.State;
pub const probe_seed = View.probe_seed;
pub const buffers = View.buffers;
pub var window: usize = 0;
pub var passes: usize = 0;
pub var kept_at: u32 = 0;
pub fn context(_: *const Target) usize {
return window;
}
pub fn drafts(_: *const Target) bool {
return false;
}
pub fn save(x: *Target, _: *lanes.Stream, at: u32) !*State {
if (at == 0) return error.NothingToKeep; // as Flash Next, which keeps no empty state
kept_at = at;
const st = try View.blank(x, at);
@memset(hostOf(st.one), 3);
@memset(hostOf(st.two.?), 9);
return st;
}
pub fn drop(x: *Target, st: *State) void {
x.gpa.free(hostOf(st.one));
x.gpa.free(hostOf(st.two.?));
x.gpa.destroy(st);
}
fn prefill(_: *anyopaque, s: *lanes.Stream) anyerror!void {
passes += 1;
for (s.reuse.marks) |mark| s.reuse.hook.?.at(s.reuse.hook.?.ptr, s, mark);
}
fn release(_: *anyopaque, _: *lanes.Stream) void {}
};

test "CUDA kept: a window under 16 tokens skips the identity probe, and 16 probes with a mark past row 0" {
var d = Host.driver();
var target: View.Target = .{ .d = &d, .s = .{ .d = &d, .handle = @ptrFromInt(331) } };
var vtable: lanes.backend.Backend.VTable = undefined; // the probe calls these two and nothing else
vtable.prefill = Probed.prefill;
vtable.release = Probed.release;
const backend: lanes.backend.Backend = .{ .ptr = &target, .vtable = &vtable };
const W = kept.Kept(Probed);
for ([_]usize{ 1, 8, 14, 15 }) |window| {
Probed.window = window;
Probed.passes = 0;
const p = try W.probe(t.allocator, t.io, backend, &target);
try t.expectEqual(@as(usize, 0), Probed.passes);
try t.expectEqual(Probed.probe_seed, p.hash);
try t.expectEqual(@as(f64, 0), p.rate); // none measured: the store learns it from served prompts
}
Probed.window = 16;
Probed.passes = 0;
const p = try W.probe(t.allocator, t.io, backend, &target);
try t.expectEqual(@as(usize, 1), Probed.passes);
try t.expectEqual(@as(u32, 1), Probed.kept_at);
try t.expect(p.hash != Probed.probe_seed and p.rate > 0);
}

test "CUDA kept: Gemma 4, Dory and Spark2.5 take the store, a family without a view still refuses" {
const gemma = @import("gemma4_cuda");
const dory = @import("dory");
Expand Down