Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/environment-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ reports the setting's own value, does not count as logging.
| `TF_FLASHNEXT_SHARD_HEAD` | release, tool, test | other |
| `TF_FLASHNEXT_SHARED_SIDE` | release, tool, test | other |
| `TF_FLASHNEXT_SPLIT_DENSE` | release, tool, test | other |
| `TF_FLASHNEXT_STATE_PAGES` | release, tool, test | other |
| `TF_FLASHNEXT_TABLE_WARM` | release, tool, test | other |
| `TF_FLASHNEXT_TEMPERATURE` | release, tool, test | other |
| `TF_FLASHNEXT_TOP_K` | release, tool, test | other |
Expand Down
1 change: 1 addition & 0 deletions docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ stops the start with the variable, the supplied text and the accepted forms.
| `TF_FLASHNEXT_GATHER_EARLY` | Exactly `0` or `1`; default off. `1` gathers a shared pass's n-gram rows for every stream in one pool call, so the pass shares one distinct-row read and one set of parked readers; the rows, their order and every reply are the per-stream call's |
| `TF_FLASHNEXT_PROFILE` | Exactly `0` or `1`; default off |
| `TF_FLASHNEXT_VERIFY_HOST_LOGITS` | Exactly `0` or `1`; default off |
| `TF_FLASHNEXT_STATE_PAGES` | Exactly `0` or `1`; default off; `1` needs `TF_FLASHNEXT_KV_PAGES` |
| `TF_FLASHNEXT_PREFILL_MTP_LEAN` | Exactly `0` or `1`; default off. For eligible lane-free prompt-only MTP rounds, `1` keeps only the KV/indexer caches, skips unread proposal work and graph capture, and bounds each prompt piece by 3,072 rows and one quarter of the admitted scratch capacity; capacity permitting, pieces remain wider than the default 256 rows |
| `TF_FLASHNEXT_DRAFTS` | Whole draft count; default 6, then clamped to 1 through 15 |
| `TF_FLASHNEXT_DRAFTS_GREEDY` | Whole draft count; default 8, then clamped to 1 through 15 |
Expand Down
3 changes: 2 additions & 1 deletion docs/profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ Coverage includes:
dense/MoE, Flash Next, Kolibri-1, Gemma 4, Spark2.5, Dory, LFM2.5-24B-A2B, Muse Spark Open, Ripple,
Inkling 2 Mini):
`TENSORFOLD_PROMPT_CACHE_KEEP_OTHERS`, exact `1` only.
- Flash Next on CUDA: `TF_FLASHNEXT_KV_PAGES`, `TF_FLASHNEXT_KV_PAGES_ALL`.
- Flash Next on CUDA: `TF_FLASHNEXT_KV_PAGES`, `TF_FLASHNEXT_KV_PAGES_ALL`, `TF_FLASHNEXT_STATE_PAGES` (exact `1`, with
`TF_FLASHNEXT_KV_PAGES`).
- Dory on CUDA: `TENSORFOLD_DORY_KV_PAGES`, full-attention caches in pool pages unless it is exactly `0`; unset,
every stream's caches are contiguous and made at open.
- Gemma 4 on CUDA: `TENSORFOLD_GEMMA4_KV_PAGES`, global caches in pool pages by default, where `0` keeps them linear.
Expand Down
17 changes: 17 additions & 0 deletions zig/src/core/kv_groups.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,28 @@ pub fn state(bytes: u64, page: u64) Fit {
return .{ .rows = 0, .pages = n, .pad = n * page - bytes };
}

/// A stream's state as pieces on whole pages (kv_pieces.zig): how many, the pages they take, their bytes.
pub const Pieces = struct {
n: u32,
pages: u32,
bytes: u64,

/// The bytes the pages hold past the pieces: alignment and the tail no piece fits.
pub fn pad(p: Pieces, page: u64) u64 {
return p.pages * page - p.bytes;
}
};

/// What the load report reads: the pool's group and page, and the kinds beside it in or outside the pool.
pub const Shape = struct {
full: Group,
named: bool = true, // the family names the full group's layers; false: its row is a token's bytes over all of them
page_rows: u32,
window: ?Group = null, // rows outside the pool: a sliding window's rings
run: u64 = 0, // a stream's state as a run of the pool's pages
pieces: ?Pieces = null, // a stream's state as pieces on any of the pool's pages
fixed: u64 = 0, // a lane's fixed part outside the pool, shown when `outside` > 0
outside: u64 = 0, // of `fixed`, buffers wider than a page
snap: u64 = 0, // a kept snapshot's slot outside the pool

/// One layer's share of a page: the rows of the full group's layer a page holds.
Expand All @@ -70,6 +85,8 @@ pub fn line(a: Allocator, s: Shape) ![]u8 {
try rowsPart(o, g, s);
}
if (s.run > 0) try statePart(o, "state run, in the pool", s.run, page);
if (s.pieces) |p| try o.print("; state, in the pool as pieces: {d} pieces, {f} on {d} page{s} a lane, {f} padding ({d:.1} %)", .{ p.n, size(p.bytes), p.pages, if (p.pages == 1) "" else "s", size(p.pad(page)), pct(p.pad(page), p.pages * page) });
if (s.outside > 0) try o.print("; a lane's fixed part, outside the pool and counted at admission: {f}, {f} of it buffers wider than a page", .{ size(s.fixed), size(s.outside) });
if (s.snap > 0) try statePart(o, "snapshot, outside the pool", s.snap, page);
return w.toOwnedSlice();
}
Expand Down
107 changes: 107 additions & 0 deletions zig/src/core/kv_pieces.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//! A stream's state as pieces on whole pool pages: each piece inside one page, any pages, first-fit decreasing.
const std = @import("std");
const Allocator = std.mem.Allocator;
const groups = @import("kv_groups.zig");

/// Where a piece lies: its page among the stream's state pages (Lease.state), and its byte offset in that page.
pub const Place = struct { page: u32, offset: u64 };

/// Pieces placed on pages, in the caller's order; the same sizes and page always give the same map.
pub const Map = struct {
at: []Place,
sizes: []u64,
page: u64,
pages: u32,

pub fn deinit(m: *Map, gpa: Allocator) void {
gpa.free(m.at);
gpa.free(m.sizes);
m.* = undefined;
}

/// The pieces' count, pages and bytes, as the layout and the load report read them.
pub fn summary(m: Map) groups.Pieces {
var bytes: u64 = 0;
for (m.sizes) |n| bytes += n;
return .{ .n = @intCast(m.sizes.len), .pages = m.pages, .bytes = bytes };
}

/// Piece `i`'s device address: `ids` are the stream's state pages, `base` the pool's first page.
pub fn addr(m: Map, i: usize, base: u64, ids: []const u32) u64 {
return base + @as(u64, ids[m.at[i].page]) * m.page + m.at[i].offset;
}
};

/// `sizes` packed first-fit decreasing on pages of `page` bytes, each piece starting at a multiple of `alignment`.
pub fn pack(gpa: Allocator, sizes: []const u64, page: u64, alignment: u64) error{ PieceOverPage, OutOfMemory }!Map {
const order = try gpa.alloc(u32, sizes.len);
defer gpa.free(order);
for (order, 0..) |*o, i| o.* = @intCast(i);
const By = struct {
sizes: []const u64,
al: u64,
fn more(c: @This(), a: u32, b: u32) bool {
const x = std.mem.alignForward(u64, c.sizes[a], c.al);
const y = std.mem.alignForward(u64, c.sizes[b], c.al);
return x > y or (x == y and a < b);
}
};
std.sort.pdq(u32, order, By{ .sizes = sizes, .al = alignment }, By.more);
const at = try gpa.alloc(Place, sizes.len);
errdefer gpa.free(at);
var used: std.ArrayList(u64) = .empty; // bytes taken on each page so far
defer used.deinit(gpa);
for (order) |i| {
const n = std.mem.alignForward(u64, sizes[i], alignment);
if (n > page) return error.PieceOverPage;
const k = for (used.items, 0..) |u, k| {
if (page - u >= n) break k;
} else blk: {
try used.append(gpa, 0);
break :blk used.items.len - 1;
};
at[i] = .{ .page = @intCast(k), .offset = used.items[k] };
used.items[k] += n;
}
const own = try gpa.dupe(u64, sizes);
return .{ .at = at, .sizes = own, .page = page, .pages = @intCast(used.items.len) };
}

/// Every piece inside one page of the map's and no two overlapping; a wrong map is refused by name.
pub fn check(gpa: Allocator, m: Map) error{ PieceOverPage, PiecesOverlap, OutOfMemory }!void {
const order = try gpa.alloc(u32, m.at.len);
defer gpa.free(order);
for (order, 0..) |*o, i| {
o.* = @intCast(i);
if (m.at[i].page >= m.pages or m.at[i].offset + m.sizes[i] > m.page) return error.PieceOverPage;
}
const By = struct {
at: []const Place,
fn less(c: @This(), a: u32, b: u32) bool {
const x = c.at[a];
const y = c.at[b];
return x.page < y.page or (x.page == y.page and x.offset < y.offset);
}
};
std.sort.pdq(u32, order, By{ .at = m.at }, By.less);
for (order[0..order.len -| 1], order[@min(1, order.len)..]) |a, b| {
const x = m.at[a];
const y = m.at[b];
if (x.page == y.page and x.offset + m.sizes[a] > y.offset) return error.PiecesOverlap;
}
}

test {
_ = @import("kv_pieces_test.zig");
}

/// The n-th highest of distinct `ids` (n <= ids.len), so the n highest are those at or above it; maxInt for n = 0.
pub fn nthHighest(ids: []const u32, n: usize) u32 {
var cut: u32 = std.math.maxInt(u32);
for (0..n) |_| {
var best: u32 = 0;
for (ids) |id| best = if (id < cut and id >= best) id else best;
cut = best;
}
return cut;
}
103 changes: 103 additions & 0 deletions zig/src/core/kv_pieces_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//! The packer on Flash Next's sizes (256-row pages with its MTP head): page counts, checks and refusals.
const std = @import("std");
const pieces = @import("kv_pieces.zig");
const t = std.testing;
const gpa = t.allocator;

// Flash Next's state and draft pieces, whole layers: recurrence, conv tail, MTP tail and projection, a page head
const rec: u64 = 48 * 128 * 128 * 4;
const conv: u64 = 3 * 10240 * 2;
const tail: u64 = 9 * 4 * 2560 * 2;
const proj: u64 = 32 * 16480 * 2;
const page_head: u64 = 256 * (2 * 2 * 256 * 2 + 128 * 2) * 13 + 64 * 128 * 2 * 13;

fn flashNext(late: bool) !std.ArrayList(u64) {
var s: std.ArrayList(u64) = .empty;
errdefer s.deinit(gpa);
try s.appendNTimes(gpa, rec, 36);
try s.appendNTimes(gpa, conv, 36);
try s.append(gpa, tail);
try s.appendNTimes(gpa, rec, 36); // spares
try s.appendNTimes(gpa, conv, 36); // saved conv windows
try s.appendNTimes(gpa, proj, 36); // saved projection rows
try s.appendSlice(gpa, &.{ tail, 32 * 4 * 2560 * 2, 4 * 2560 * 2, 32768 * 2, 32 * 16 + 32 * 64 * 6 });
if (late) {
try s.append(gpa, 256);
try s.appendNTimes(gpa, 48 * (16 * 128 + 48 * 128 + 48 * 2) * 4, 36);
}
return s;
}

test "Flash Next at 256-row pages with its head: pages and pieces as counted by hand, every piece inside one page" {
try t.expectEqual(@as(u64, 7_880_704), page_head);
for ([_]bool{ false, true }, [_]u32{ 37, 45 }, [_]u32{ 186, 223 }) |late, pages, n| {
var s = try flashNext(late);
defer s.deinit(gpa);
var m = try pieces.pack(gpa, s.items, page_head, 256);
defer m.deinit(gpa);
try pieces.check(gpa, m);
const sum = m.summary();
try t.expectEqual(n, sum.n);
try t.expectEqual(pages, sum.pages);
for (m.at) |p| try t.expectEqual(@as(u64, 0), p.offset % 256);
}
var s = try flashNext(false);
defer s.deinit(gpa);
var state = try pieces.pack(gpa, s.items[0..73], page_head, 256); // the stream's own state alone
defer state.deinit(gpa);
try t.expectEqual(@as(u32, 18), state.pages);
var all = try pieces.pack(gpa, s.items, page_head, 256);
defer all.deinit(gpa);
try t.expectEqual(@as(u64, 21_577_216), all.summary().pad(page_head));
}

test "packing is deterministic: the same sizes give the same places, piece order kept" {
var s = try flashNext(true);
defer s.deinit(gpa);
var a = try pieces.pack(gpa, s.items, page_head, 256);
defer a.deinit(gpa);
var b = try pieces.pack(gpa, s.items, page_head, 256);
defer b.deinit(gpa);
try t.expectEqualSlices(pieces.Place, a.at, b.at);
try t.expectEqualSlices(u64, s.items, a.sizes);
var ids: [45]u32 = undefined;
for (&ids, 0..) |*id, k| id.* = @intCast(90 - 2 * k); // any pages, in any order
for (a.at, 0..) |p, i| try t.expectEqual(0x1000 + @as(u64, ids[p.page]) * page_head + p.offset, a.addr(i, 0x1000, &ids));
}

test "a piece wider than a page is refused by name, and an empty list takes no page" {
try t.expectError(error.PieceOverPage, pieces.pack(gpa, &.{ 10, 4097 }, 4096, 256));
try t.expectError(error.PieceOverPage, pieces.pack(gpa, &.{4000}, 4096, 256 * 64)); // aligned past the page
var m = try pieces.pack(gpa, &.{}, 4096, 256);
defer m.deinit(gpa);
try t.expectEqual(@as(u32, 0), m.pages);
try pieces.check(gpa, m);
}

test "first fit decreasing: small pieces fill the larger ones' gaps, ties in the caller's order" {
var m = try pieces.pack(gpa, &.{ 100, 3000, 3000, 700, 1000 }, 4096, 256);
defer m.deinit(gpa);
try t.expectEqual(@as(u32, 2), m.pages);
try t.expectEqual(pieces.Place{ .page = 0, .offset = 0 }, m.at[1]);
try t.expectEqual(pieces.Place{ .page = 1, .offset = 0 }, m.at[2]);
try t.expectEqual(pieces.Place{ .page = 0, .offset = 3072 }, m.at[4]);
try t.expectEqual(pieces.Place{ .page = 1, .offset = 3072 }, m.at[3]);
try t.expectEqual(pieces.Place{ .page = 1, .offset = 3840 }, m.at[0]);
try pieces.check(gpa, m);
try t.expectEqual(@as(u64, 2 * 4096 - 7800), m.summary().pad(4096));
}

test "a wrong map is refused: two pieces overlapping, a piece past its page or past the map's pages" {
var m = try pieces.pack(gpa, &.{ 1000, 1000, 1000 }, 4096, 256);
defer m.deinit(gpa);
try pieces.check(gpa, m);
const keep = m.at[1];
m.at[1].offset = m.at[0].offset + 512;
try t.expectError(error.PiecesOverlap, pieces.check(gpa, m));
m.at[1] = .{ .page = 0, .offset = 3584 };
try t.expectError(error.PieceOverPage, pieces.check(gpa, m));
m.at[1] = .{ .page = 1, .offset = 0 };
try t.expectError(error.PieceOverPage, pieces.check(gpa, m));
m.at[1] = keep;
try pieces.check(gpa, m);
}
Loading
Loading