Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- CUDA: Gemma 4, Dory and Spark2.5 can keep conversations on disk through the shared kept-image store (#3135, part of #840). Thanks to @jschmied.

- CUDA: a dense Qwen 3.5 MLP's NVFP4 prompt gate, up and activation run in one launch for prompt rows past 16, with the same bytes (#3252). Thanks to @jschmied.

- Images: with `stream: true`, `POST /v1/images/generations` streams each denoising step's progress (`image_generation.progress`) and, with `preview_interval`, partial previews while the picture generates, then the PNG (#3245).
Expand Down
23 changes: 12 additions & 11 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1339,17 +1339,18 @@ deleted and its tokens are prefilled, and a write cut off midway leaves nothing
needs `--spill-gib` or `--learn-conversations`. With `--learn-conversations` too, every state is still written
behind, a staged write survives eviction of its RAM entry, and the cap is the smaller of `--learn-gib` and
`--spill-gib`. `--learn-min-free-gib` and `--learn-daily-gib` apply to spilled states as well. The same four Mac
families take both flags, and so do three CUDA ones; the others refuse them at startup. Without either flag nothing
changes, and the routes below stay off. `TENSORFOLD_ROWS_SYNC=1` selects the previous synchronous writer for
comparisons; its block and state files have the same bytes. That comparison writer retains blocking I/O; the bounded
disk wait uses the threaded writer. `TF_IMPRINT_VERIFY=1` reads threaded writes back against their owned staging before
publication.

On CUDA, Nemotron, the Qwen3.5 hybrids and Flash Next take `--spill-gib` and `--checkpoint-slots` with the same store
and cap as a Mac. At an eviction the engine thread stages a copy of the state into host memory, waiting for the GPU
work that produced it, then releases its RAM entry and goes on; the I/O thread writes and fsyncs the copy while the
engine decodes. Staging holds at most 1 GiB and eight jobs: a state whose new bytes (rows neither on disk nor being
written) do not fit what is free is not saved, so one past 1 GiB never is, and a later request for it prefills. When the
families take both flags, and so do Nemotron, the Qwen hybrids, Flash Next, Gemma 4, Dory and Spark2.5 on CUDA; the
others refuse them at startup. Without either flag nothing changes, and the routes below stay off.
`TENSORFOLD_ROWS_SYNC=1` selects the previous synchronous writer for comparisons; its block and state files have the
same bytes. That comparison writer retains blocking I/O; the bounded disk wait uses the threaded writer.
`TF_IMPRINT_VERIFY=1` reads threaded writes back against their owned staging before publication.

On CUDA, Nemotron, the Qwen3.5 hybrids, Flash Next, Gemma 4, Dory and Spark2.5 take `--spill-gib` and
`--checkpoint-slots` with the same store and cap as a Mac. At an eviction the engine thread stages a copy of the state
into host memory, waiting for the GPU work that produced it, then releases its RAM entry and goes on; the I/O thread
writes and fsyncs the copy while the engine decodes. Staging holds at most 1 GiB and eight jobs; for Gemma 4, Dory and
Spark2.5 it holds one whole state at the served window, within the RAM cache's budget. A state whose new bytes (rows
neither on disk nor being written) do not fit what is free is not saved, and a later request for it prefills. When the
engine closes, at a planned stop (SIGTERM or SIGINT) or an idle unload, it saves the states still in RAM, newest first,
within the same disk cap and 20-second stop budget. While other streams run, a queued request whose state is on disk has
it read ahead on an I/O thread, as described above.
Expand Down
5 changes: 3 additions & 2 deletions docs/optimization-ledger.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ last two hold the families with only a Metal, HIP or scoring backend.
| 5. State walk | open C[#2392] | open C[#2392] | open [#2215] | open C[#2392] | open C[#2392] |
| 6. KV ledger | done [#2127] | done [#2127] | done [#2127] | done [#2127] | done [#2127] |
| 7. Host KV tier | yes | open [#1501] | done [#1795] | yes | yes |
| 8. Disk cache | done C[#1358] | done C[#1359] M[#519] | done M¹ open C[#1411] | yes | done C[#1413] M¹ |
| 8. Disk cache | done C[#1358] | done C[#1359] M[#519] | done M¹ open C[#1411] | open C[#3135] | done C[#1413] M¹ |
| 9. FP8 KV | done [#886] | yes | done¹ | yes | yes |
| 10. FP4/FP8 decode | yes | yes | yes | no (affine) | yes |
| 11. Fused proj. | done [#1783] | done¹ | done¹ | done [#1783] | done [#1783] |
Expand Down Expand Up @@ -88,7 +88,7 @@ last two hold the families with only a Metal, HIP or scoring backend.
| 5. State walk | yes | yes | yes | yes | yes |
| 6. KV ledger | done [#2127] | done [#2127] | done [#2127] | yes | yes |
| 7. Host KV tier | yes | yes | yes | yes | yes |
| 8. Disk cache | yes | yes | yes | yes | done¹ |
| 8. Disk cache | yes | open [#3135] | open [#3135] | yes | done¹ |
| 9. FP8 KV | yes | yes | yes | yes | no (FP8 by design) |
| 10. FP4/FP8 decode | failed [#1448] | no (bf16) | yes | no (EXL3) | yes |
| 11. Fused proj. | done¹ | done [#1783] | yes | done¹ | done¹ |
Expand Down Expand Up @@ -524,6 +524,7 @@ below; these are estimates for planning a port, not results.
[#1394]: https://github.com/ashhart/TensorFold/pull/1394
[#1411]: https://github.com/ashhart/TensorFold/pull/1411
[#1413]: https://github.com/ashhart/TensorFold/pull/1413
[#3135]: https://github.com/ashhart/TensorFold/pull/3135
[#1423]: https://github.com/ashhart/TensorFold/pull/1423
[#1426]: https://github.com/ashhart/TensorFold/pull/1426
[#1434]: https://github.com/ashhart/TensorFold/pull/1434
Expand Down
2 changes: 1 addition & 1 deletion zig/build/cuda_fault_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const std = @import("std");
pub fn steps(b: *std.Build, cuda: *std.Build.Module, flashnext: *std.Build.Module, native: *std.Build.Module) void {
b.step("test-cuda-context-health-host", "Sticky CUDA context faults and real graph-instantiation refusal without devices").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = cuda, .filters = &.{ "CUDA context health:", "CUDA graph refusal:" } })).step);
b.step("test-flashnext-loader-host", "The threaded NVFP4 expert loader: the loop's words, no pinned or device memory left after a failed load, geometry first").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = flashnext, .filters = &.{"Flash Next expert loader:"} })).step);
b.step("test-flashnext-completion-host", "Actual CUDA sequence rollback drains asynchronous destinations before release").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = flashnext, .filters = &.{"Flash Next completion:"} })).step);
b.step("test-flashnext-completion-host", "Actual CUDA sequence rollback drains asynchronous destinations before release").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"Flash Next completion:"} })).step);
b.step("test-cuda-fault-admission-host", "Sticky context health reaches native status and queue admission").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"CUDA context health:"} })).step);
b.step("test-cuda-readiness-host", "Real native readiness and synchronous startup preparation without shaders").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"family readiness reaches"} })).step);
b.step("test-startup-log-host", "Ordered startup logs and early P2P facts without devices").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = cuda, .filters = &.{"startup log:"} })).step);
Expand Down
3 changes: 3 additions & 0 deletions zig/build/disk_identity_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pub fn steps(b: *std.Build, native: *std.Build.Module, all: *std.Build.Step) voi
const open = b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"CUDA disk open:"} }));
b.step("test-cuda-disk-open-host", "CUDA's disk store takes --spill-gib's mode and --checkpoint-slots").dependOn(&open.step);
all.dependOn(&open.step);
const kept = b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"CUDA kept:"} }));
b.step("test-cuda-kept-host", "The shared kept image over host memory, stale keys through the real store").dependOn(&kept.step);
all.dependOn(&kept.step);
const identity = b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"disk identity:"} }));
b.step("test-disk-identity-host", "Actual CUDA identity, resolved profile values and stale disk misses").dependOn(&identity.step);
all.dependOn(&identity.step);
Expand Down
118 changes: 118 additions & 0 deletions zig/src/families/dory/cuda_disk.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//! Dory's kept states for the disk store (native/cuda_kept.zig): their layout, a blank one, what they are read under.
const std = @import("std");
const cuda = @import("cuda");
const be = @import("cuda_backend.zig");
const snap = @import("cuda_snap.zig");
const lanes = @import("lanes");
const Allocator = std.mem.Allocator;

pub const Target = be.Dory;
pub const State = snap.Saved;
pub const probe_seed: u64 = 0x646f;

/// Every cache, K then V, as snap.each lays them: a full one's rows from position 0, a ring's tail the state's own.
pub fn parts(t: *const Target, at: u32, out: anytype, a: Allocator) !void {
const m = t.m;
const row = m.c.kvDim() * 2;
for (m.sched.cache_kind[0..m.sched.caches]) |kind| for (0..2) |_| {
if (kind == .full) {
try out.append(a, .{ .bytes = @as(u64, at) * row, .row = @intCast(row) });
} else try out.append(a, .{ .bytes = snap.tail(m.c.window, at) * row });
};
}

pub fn buffers(st: *const State) [2]?cuda.DeviceBuffer {
return .{ st.buf, null };
}

pub fn blank(t: *Target, at: u32) !*State {
return snap.blank(t.gpa, t.m, at);
}

pub fn drafts(_: *const Target) bool {
return false; // Dory's drafts come from the prompt's text: no head's state
}

pub fn context(t: *const Target) usize {
return t.m.context;
}

pub fn save(t: *Target, s: *lanes.Stream, at: u32) !*State {
return t.saveState(s, at);
}

pub fn drop(t: *Target, st: *State) void {
t.dropState(st);
}

/// The KV form past what the probe shows: loops and the state key, a ring's keys, a row, pages or whole caches.
pub fn facts(t: *const Target) [5]u64 {
const m = t.m;
const key = snap.keyOf(m.c);
return .{ key.loops, @backingInt(key.mode), m.c.window, m.c.kvDim(), @intFromBool(m.paged) };
}

const testing = std.testing;
const fwd = @import("cuda_model.zig");
const config = @import("config.zig");

fn pinned(loops: usize) !config.Config {
const text = try config.pinnedText(testing.allocator);
defer testing.allocator.free(text);
var why: config.Why = .{};
return (try config.parse(testing.allocator, text, &why)).withLoops(loops);
}

const Piece = struct { bytes: u64, row: u32 = 0 };

const Offsets = struct {
ends: *std.ArrayList(u64),
fn add(c: Offsets, _: u64, off: usize, len: usize) anyerror!void {
try c.ends.append(testing.allocator, off + len);
}
};

test "Dory disk: the parts are snap.each's caches in its order, and a part's bytes are its copies'" {
var m: fwd.Model = undefined;
m.c = try pinned(2);
m.sched = @import("schedule.zig").build(m.c);
m.context = 8192;
m.paging = null;
m.ring = fwd.ringSize(fwd.prompt_rows, m.c.window);
var ks: [64]u64 = @splat(1 << 32);
m.k = ks[0..m.sched.caches];
m.v = ks[0..m.sched.caches];
var t: Target = undefined;
t.m = &m;
for ([_]u32{ 1, 1023, 1024, 4000, 8000 }) |at| {
var list: std.ArrayList(Piece) = .empty;
defer list.deinit(testing.allocator);
try parts(&t, at, &list, testing.allocator);
var ends: std.ArrayList(u64) = .empty;
defer ends.deinit(testing.allocator);
try snap.each(&m, 0, at, 0, Offsets{ .ends = &ends }, Offsets.add);
var off: u64 = 0;
for (list.items) |p| {
off += p.bytes;
try testing.expect(std.mem.indexOfScalar(u64, ends.items, off) != null); // a part ends where a copy does
}
try testing.expectEqual(snap.bytes(&m, at), off);
}
}

test "Dory disk: another loop count, ring or paging is another key" {
var m: fwd.Model = undefined;
m.c = try pinned(2);
m.paged = false;
var t: Target = undefined;
t.m = &m;
const base = facts(&t);
m.c = try pinned(4);
try testing.expect(!std.mem.eql(u64, &base, &facts(&t)));
m.c = try pinned(2);
m.c.window -= 1;
try testing.expect(!std.mem.eql(u64, &base, &facts(&t)));
m.c = try pinned(2);
m.paged = true;
try testing.expect(!std.mem.eql(u64, &base, &facts(&t)));
}
1 change: 1 addition & 0 deletions zig/src/families/dory/cuda_native.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub const max_segments: u32 = 1;
pub const prompt_rows: u32 = be.prompt_rows;
/// A kept state is the caches through its position and nothing past it (no draft head reads a token on).
pub const snap_lookahead: u32 = 0;
pub const kept_view = @import("cuda_disk.zig"); // the disk store's image (native/cuda_kept.zig)
pub const takes_drafter = false;
pub const drafter_refusal = "--drafter: Dory on CUDA loads no drafter; drafts come from the prompt's own text";
pub const explains_open = true;
Expand Down
12 changes: 10 additions & 2 deletions zig/src/families/dory/cuda_snap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub const Saved = struct {
};

/// Positions a sliding ring keeps at `at`: a later row at `at` or past reads keys from at - window + 1 on.
fn tail(window: usize, at: usize) usize {
pub fn tail(window: usize, at: usize) usize {
return @min(at, window - 1);
}

Expand Down Expand Up @@ -77,7 +77,7 @@ fn fullCache(m: *const fwd.Model, j: usize) usize {
}

/// Each cache, K then V, of `slot`: rings' last rows, full caches' [from, at), one copy a run, in order.
fn each(m: *const fwd.Model, slot: usize, at: usize, from: usize, ctx: anytype, comptime f: fn (@TypeOf(ctx), dev: u64, off: usize, len: usize) anyerror!void) !void {
pub fn each(m: *const fwd.Model, slot: usize, at: usize, from: usize, ctx: anytype, comptime f: fn (@TypeOf(ctx), dev: u64, off: usize, len: usize) anyerror!void) !void {
const rb = m.c.kvDim() * 2;
var off: usize = 0;
for (0..m.sched.caches) |i| for ([_][]u64{ m.k, m.v }) |cache| {
Expand Down Expand Up @@ -135,6 +135,14 @@ fn pieceAt(m: *const fwd.Model, region: usize, id: u32) u64 {
return (if (region % 2 == 0) m.k else m.v)[i] + id * chunk(m);
}

/// A state of `at` positions with its storage unset: the disk store fills it (cuda_disk.zig).
pub fn blank(gpa: std.mem.Allocator, m: *const fwd.Model, at: u32) !*Saved {
const k = try gpa.create(Saved);
errdefer gpa.destroy(k);
k.* = .{ .at = at, .key = keyOf(m.c), .buf = try cuda.DeviceBuffer.alloc(m.d, @max(bytes(m, at), 256)) };
return k;
}

/// Copy `slot`'s state at `at` into new storage, queued on `s` behind the chunk that ends there.
pub fn save(gpa: std.mem.Allocator, m: *const fwd.Model, s: cuda.Stream, slot: usize, at: u32) !*Saved {
if (at == 0 or at > m.context) return error.NotAtMark;
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/dory/dory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub const cuda_pages = @import("cuda_pages.zig");
pub const cuda_run = @import("cuda_run.zig");
/// The native CUDA registry's entry (native/cuda.zig).
pub const native = @import("cuda_native.zig");
pub const cuda_disk = @import("cuda_disk.zig");

test {
_ = @import("config.zig");
Expand All @@ -27,6 +28,7 @@ test {
_ = @import("rope_test.zig");
_ = @import("cuda_kernels.zig");
_ = @import("cuda_backend.zig");
_ = cuda_disk;
_ = @import("cuda_snap.zig");
_ = @import("cuda_pages.zig");
_ = @import("cuda_run.zig");
Expand Down
Loading
Loading