Skip to content
Closed
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 @@ -5,6 +5,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel
## Unreleased

- Docs: the GLM-5.3-Flash CUDA recipe's Verified copy drafts section lists, for one checkpoint at TP=2 with `--parallel 4`, the loads that `TF_GLM_COPIES=1` sped up and the ones it slowed, and says the default stays off (#3335). Thanks to @Bizuayeu.
- Flash Next on CUDA: the mixed PLE shard and the nested shards leave the page cache once loaded, so a load no longer keeps their file pages resident beside the GPU copy (#3467, thanks @jschmied).

- Tests: the Qwen 3.8 27B Metal residency fixtures compile again on macOS after the DFlash2 target became a value (#3572).
- Tests: the kernel contract's Llama `gemm_untransposed` control edits the `TF_GEMM_NAX_KERNEL` instance line again, so its planted fault fails as it should (#3157, thanks @gilby, who found and fixed it first).

Expand Down
7 changes: 4 additions & 3 deletions zig/cuda_checkpoint_cache_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ test "CUDA load cache: DeepSeek finishes backbone and DSpark before the pack swe

test "CUDA load cache: Flash Next sweep follows MTP and retains live PLE files" {
const s = @embedFile("src/families/flashnext/cuda_lanes_load.zig");
const drop = std.mem.indexOf(u8, s, ".dropPageCache(io, dir, e.ngram.files.items)") orelse return error.NoPleExclusion;
const drop = std.mem.indexOf(u8, s, ".dropPageCache(gpa, io, dir, e.ngram.files.items)") orelse return error.NoPleExclusion;
try t.expect(std.mem.indexOf(u8, s, "mtp.Head.init").? < drop);
const owner = @embedFile("src/families/flashnext/cuda_prompt_wt.zig");
try t.expect(std.mem.indexOf(u8, owner, "if (!runtimePages(self.f.names.keys())) self.f.release(std.heap.page_allocator)") != null);
try t.expect(std.mem.indexOf(u8, owner, "if (runtimePage(name)) keep.append(scratch, self.f.get(name).?.bytes) catch return;") != null);
try t.expect(std.mem.indexOf(u8, owner, "self.f.releaseExcept(keep.items, scratch)") != null);
}

test "CUDA load cache: LFM2 decision model releases its direct-read fallback" {
Expand All @@ -137,7 +138,7 @@ test "CUDA load cache: every TP load scope defers file advice through all-rank r
const read = std.mem.indexOfPos(u8, source, start, last) orelse return error.NoLastLoad;
try t.expect(start < read and read < ready);
if (std.mem.eql(u8, path, "flashnext/cuda_lanes_load.zig")) {
const drop = std.mem.indexOf(u8, source, "dropPageCache(io,") orelse return error.NoSweep;
const drop = std.mem.indexOf(u8, source, "dropPageCache(gpa, io,") orelse return error.NoSweep;
try t.expect(drop < ready);
}
}
Expand Down
39 changes: 19 additions & 20 deletions zig/src/core/checkpoint_cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,15 @@ fn visit(io: Io, dir: []const u8, names: []const []const u8, comptime advise: an
}

/// Flash Next also closes shards while loading; its live PLE mappings must be excluded from the final sweep.
pub fn directory(io: Io, dir: []const u8, keep: anytype) void {
pub fn shards(io: Io, paths: []const [:0]u8, keep: anytype) void {
if (@import("builtin").os.tag != .linux) return;
walk(io, dir, keep, drop);
walk(io, paths, keep, drop);
}

fn walk(io: Io, dir: []const u8, keep: anytype, comptime advise: anytype) void {
var d = Io.Dir.cwd().openDir(io, dir, .{ .iterate = true }) catch return;
defer d.close(io);
var it = d.iterate();
entries: while (it.next(io) catch null) |e| {
if (e.kind != .file and e.kind != .sym_link) continue;
if (!std.mem.endsWith(u8, e.name, ".safetensors")) continue;
const f = d.openFile(io, e.name, .{}) catch continue;
/// `paths` from the index (checkpoint.shardFiles), so shards nested under tensors/ are reached; held inodes skipped.
fn walk(io: Io, paths: []const [:0]u8, keep: anytype, comptime advise: anytype) void {
entries: for (paths) |path| {
const f = Io.Dir.cwd().openFile(io, path, .{}) catch continue;
defer f.close(io);
const stat = f.stat(io) catch continue;
for (keep) |k| {
Expand Down Expand Up @@ -89,30 +85,33 @@ test "checkpoint cache: last read precedes release and close on success and refu
}
}

test "checkpoint cache: final sweep excludes live PLE, subdirectories and unrelated files" {
test "checkpoint cache: final sweep reaches the index's nested shards and excludes live PLE and aliases" {
var tmp = t.tmpDir(.{});
defer tmp.cleanup();
for ([_][]const u8{ "model.safetensors", "mtp.safetensors", "ple.safetensors", "config.json" }) |name|
try tmp.dir.createDirPath(t.io, "tensors");
for ([_][]const u8{ "tensors/model-1.safetensors", "tensors/model-2.safetensors", "tensors/ple.safetensors" }) |name|
try tmp.dir.writeFile(t.io, .{ .sub_path = name, .data = name });
try tmp.dir.createDirPath(t.io, "ssd.safetensors");
try tmp.dir.writeFile(t.io, .{ .sub_path = "ssd.safetensors/expert.safetensors", .data = "runtime" });
if (@import("builtin").os.tag != .windows) try tmp.dir.symLink(t.io, "ple.safetensors", "alias.safetensors", .{});
const ple = try tmp.dir.openFile(t.io, "ple.safetensors", .{});
if (@import("builtin").os.tag != .windows) try tmp.dir.symLink(t.io, "ple.safetensors", "tensors/alias.safetensors", .{});
const ple = try tmp.dir.openFile(t.io, "tensors/ple.safetensors", .{});
defer ple.close(t.io);
const keep = [_]struct { file: struct { file: Io.File } }{.{ .file = .{ .file = ple } }};
const path = try tmp.dir.realPathFileAlloc(t.io, ".", t.allocator);
defer t.allocator.free(path);
const root = try tmp.dir.realPathFileAlloc(t.io, ".", t.allocator);
defer t.allocator.free(root);
var paths: [5][:0]u8 = undefined;
for (&paths, [_][]const u8{ "model-1", "model-2", "ple", "alias", "missing" }) |*p, name|
p.* = try std.fmt.allocPrintSentinel(t.allocator, "{s}/tensors/{s}.safetensors", .{ root, name }, 0);
defer for (paths) |p| t.allocator.free(p);
const Spy = struct {
var count: usize = 0;
fn advise(f: Io.File) void {
var buf: [64]u8 = undefined;
const n = f.readPositionalAll(t.io, &buf, 0) catch unreachable;
std.debug.assert(std.mem.eql(u8, buf[0..n], "model.safetensors") or std.mem.eql(u8, buf[0..n], "mtp.safetensors"));
std.debug.assert(std.mem.startsWith(u8, buf[0..n], "tensors/model-"));
count += 1;
}
};
Spy.count = 0;
walk(t.io, path, &keep, Spy.advise);
walk(t.io, &paths, &keep, Spy.advise);
try t.expectEqual(@as(usize, 2), Spy.count);
}

Expand Down
2 changes: 1 addition & 1 deletion zig/src/families/flashnext/cuda_lanes_load.zig
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn open(comptime F: type, gpa: std.mem.Allocator, io: std.Io, ctx: *const cu
own.confidence = try @import("sys").environment.read(f64, "TF_FLASHNEXT_CONFIDENCE", mtp.default_confidence);
}
errdefer if (own.head) |h| h.deinit();
@import("cuda_prompt_wt.zig").dropPageCache(io, dir, e.ngram.files.items);
@import("cuda_prompt_wt.zig").dropPageCache(gpa, io, dir, e.ngram.files.items);
const window = @min(o.context + mtp.max_rows, prompt.max_tokens);
// and an attention block's block scores at the window (64 MiB at 262,144 tokens)
_ = try own.engine.reserveScores(window, @min(o.context, own.engine.chunk));
Expand Down
35 changes: 22 additions & 13 deletions zig/src/families/flashnext/cuda_prompt_wt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,32 @@ const Shard = struct {
}

fn close(self: *Shard) void {
// A shard may mix loaded tensors with the n-gram table still read at runtime.
if (!runtimePages(self.f.names.keys())) self.f.release(std.heap.page_allocator); // no keep list: scratch unused
self.drop(std.heap.page_allocator);
self.f.close(self.io);
}

/// The shard's pages out of the page cache, except the n-gram table's: a shard may mix both (embed, HC, MTP).
fn drop(self: *const Shard, scratch: std.mem.Allocator) void {
var keep: std.ArrayList([]const u8) = .empty;
defer keep.deinit(scratch);
for (self.f.names.keys()) |name| if (runtimePage(name)) keep.append(scratch, self.f.get(name).?.bytes) catch return;
self.f.releaseExcept(keep.items, scratch);
}

fn get(self: *const Shard, name: []const u8) ?Tensor {
return self.f.get(name);
}
};

fn runtimePages(names: []const []const u8) bool {
for (names) |name| if (std.mem.indexOf(u8, name, ".ple_embedding.ngram_embedding.") != null) return true;
return false;
fn runtimePage(name: []const u8) bool {
return std.mem.indexOf(u8, name, ".ple_embedding.ngram_embedding.") != null;
}

/// The final load-only sweep excludes the n-gram table's live mappings.
pub fn dropPageCache(io: std.Io, dir: []const u8, keep: anytype) void {
core.checkpoint.cache.directory(io, dir, keep);
/// The final load-only sweep over the index's shards excludes the n-gram table's live mappings.
pub fn dropPageCache(gpa: std.mem.Allocator, io: std.Io, dir: []const u8, keep: anytype) void {
const paths = core.checkpoint.shardFiles(gpa, io, dir) catch return;
defer core.checkpoint.freeShardFiles(gpa, paths);
core.checkpoint.cache.shards(io, paths, keep);
}

pub const Store = struct {
Expand Down Expand Up @@ -188,8 +196,8 @@ pub const Store = struct {
/// Borrowers expire here; their shards stay open, pages dropped, until the load ends (or close if not kept).
pub fn release(self: *Store) void {
for (self.held.items) |h| {
// the borrowers' pages leave this process (their bytes are copied), except the n-gram table's (Shard.close)
if (!runtimePages(h.file.f.names.keys())) h.file.f.release(self.gpa);
// the borrowers' pages leave this process (their bytes are copied), except the n-gram table's
h.file.drop(self.gpa);
self.released.append(self.gpa, h) catch {
var kept = h.file;
kept.close();
Expand Down Expand Up @@ -295,7 +303,7 @@ pub const Store = struct {
} else try self.openShard(file);
if (self.file) |*f| {
// its pages dropped as close drops them: the n-gram table's stay
if (!runtimePages(f.f.names.keys())) f.f.release(self.gpa);
f.drop(self.gpa);
self.released.appendAssumeCapacity(.{ .rel = self.rel, .file = f.* });
}
self.rel = next.rel;
Expand Down Expand Up @@ -579,6 +587,7 @@ test {
}

test "Flash Next load cache: a mixed PLE shard keeps its runtime pages" {
try std.testing.expect(runtimePages(&.{ "language_model.model.norm.weight", "language_model.model.layers.1.ple.ple_embedding.ngram_embedding.shards.0.weight" }));
try std.testing.expect(!runtimePages(&.{ "language_model.model.norm.weight", "language_model.mtp.norm.weight" }));
try std.testing.expect(runtimePage("language_model.model.layers.1.ple.ple_embedding.ngram_embedding.shards.0.weight"));
try std.testing.expect(!runtimePage("language_model.model.norm.weight"));
try std.testing.expect(!runtimePage("language_model.mtp.norm.weight"));
}
4 changes: 2 additions & 2 deletions zig/tests/fixtures/gradient-serving.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ zig/src/families/flashnext/cuda_images.zig 5bd8a6c06cb69440522f4392794d9ef52b376
zig/src/families/flashnext/cuda_images_test.zig df984d54d9de1bb732544785cddf82a124de2a7019041ed1f7483b2a4a2d7c99 raw
zig/src/families/flashnext/cuda_lanes.zig 1ebf522d252b795b15d58a15eb38d9d3227c341df80522ad8a89d5ff3cfbdac4 raw main 4f6017ced (marks inside the pass), fbca90714 (shared solo-chunk rule, #2391), 80e0fd72c (#2753: non-finite draft rows refused by name; finite rows unchanged), dfd9ac5ef4 (#2585: TF_FLASHNEXT_* read through platform/environment.zig, malformed text refused at startup; valid values keep their meaning), #1953: the ladder's values through snap_intermediate
zig/src/families/flashnext/cuda_lanes_admission_test.zig 4b8052db793ba81f0e73e1b9bd77b1780bbc4fd550d90976d017da47e76faaa2 raw
zig/src/families/flashnext/cuda_lanes_load.zig 241d091d4b5199325665d25ea5d4def81fd15660351a199773c596ca2025fa18 raw main dfd9ac5ef4 (#2585: TF_FLASHNEXT_* read through platform/environment.zig, malformed text refused at startup; valid values keep their meaning)
zig/src/families/flashnext/cuda_lanes_load.zig 26596c5f9bb0fb01b96cb96b50d51a70cf8ef1ef4f2af91e33e6f4a0c74a6f10 raw main dfd9ac5ef4 (#2585: TF_FLASHNEXT_* read through platform/environment.zig, malformed text refused at startup; valid values keep their meaning), #3467: after load a shard drops its page cache except the n-gram ranges, and the sweep reaches nested shards; tensors and replies are unchanged
zig/src/families/flashnext/cuda_lanes_restore_test.zig f9947521a6d9c7d7380ca1b784a93a43b364c6fa5861cad15efd3538d2b8ceff raw
zig/src/families/flashnext/cuda_launch.zig f8bebb5ed074e6c8ab8cef667cb01fa2bb1a2b2d6a4d3874a92291390278d9f7 raw exports cuda_moe_exchange.zig for its device test; no served change; #2837 (Flash Next TP=2 graph replay)
zig/src/families/flashnext/cuda_media.zig 394601e8a807f5516f501efcaefa6fb3308f392ee322db7296b11c90814f8eea raw
Expand All @@ -157,7 +157,7 @@ zig/src/families/flashnext/cuda_prompt.zig 64dfa63d6c9608d04b524689047f3f2bcf193
zig/src/families/flashnext/cuda_prompt_load.zig 334d68cfca85c15c9d986d0c6b4f8f2a38569309b21bdac6d365935d73c115cd raw
zig/src/families/flashnext/cuda_prompt_main.zig 2543cd166dc80cd97b1ca1ce930a6150f9d36b52ee231744606f83bf54f0669d raw main dfd9ac5ef4 (#2585: the TF_FLASHNEXT_* startup check), c274b45cc1 (the check through the flashnext module)
zig/src/families/flashnext/cuda_prompt_step.zig e04030fb4a4fdb8780704a1bc0b5ba4b786159ab08a927fe32a636a893129d0c raw
zig/src/families/flashnext/cuda_prompt_wt.zig 9f0b8f853cc33e23c3ae3785c7031721615fc043e240161c84fb945e499e809f raw
zig/src/families/flashnext/cuda_prompt_wt.zig 4885be0ea938cfba80b34fbe9c160d200226761a73f8bd9d785172cbc7cef9bb raw #3467: after load a shard drops its page cache except the n-gram ranges, and the sweep reaches nested shards; tensors and replies are unchanged
zig/src/families/flashnext/cuda_qmm.zig 6d035feb053691cc9750a369c86a5463d41ed942764ad85b861bb628c106dccf raw
zig/src/families/flashnext/cuda_rank_engine.zig df6a83bcf80edfd94ac4474dbbc2e73818d40b7532430399af73f2eb95ccfa33 raw
zig/src/families/flashnext/cuda_rank_media.zig e5c268c8cf257934bb49fe401ba6213ea8b108258bbfd4d06edb990d568c3c2b raw
Expand Down