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

- GLM-5.3-Flash on CUDA: `TF_GLM_KV_SHARE` moves to `TF_KV_SHARE`. With `TF_KV_PAGES=on`, `TF_KV_SHARE=n` keeps GLM's prompts in the shared prompt store that Spark 2.5, Gemma 4 and Kolibri-1 use, with the same tokens, resume marks and memory as GLM's own store, which goes. A server started with `TF_GLM_KV_SHARE` set to anything but 0 or an empty value stops before the weights load and names `TF_KV_SHARE` (#2211).

- Qwen 3.5 and 3.6 on CUDA: a prompt chunk's last layer runs q, attention, o and the MLP over the kept row's 128-row tile only, and k and v over every row, so the cache is unchanged and the last row's logits stay bit-equal; on a GB10 first tokens come 1 to 3% sooner. A model served with an MTP head, int8 or images keeps the full last layer (#2229, closes #2165). Thanks to @jschmied and @jungjipdo.

- An engine can refuse a request it cannot serve as asked in words the client can use: the server asks one optional `Engine.VTable.refusal` when the request is whole and answers 400 `invalid_request_error` before any header on chat, completions, Responses and Messages, streamed or not. An engine without the entry behaves exactly as today (#2082). Thanks to @shuuul.
Expand Down
6 changes: 3 additions & 3 deletions docs/profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
the same profile. It enables no additional optimization and supplies no new environment values.

This is the foundation of [#1660](https://github.com/ashhart/TensorFold/issues/1660), beginning with the CUDA KV
switches in its first table, their `_ALL` companions, and GLM's and Nemotron's paging policies (eight variables).
switches in its first table, their `_ALL` companions, and GLM's and Nemotron's paging policies (seven variables).
It does not migrate every `TF_*` or `TENSORFOLD_*` variable yet. Other settings continue through their existing readers. `fast-exact` and `fast`
are refused by name; they are not aliases
for `stable` and cannot enable unqualified optimizations.
Expand Down Expand Up @@ -42,9 +42,9 @@ profile supplies no value. Registry metadata comes directly from the same table
Initial coverage is:

- Qwen3.5 dense/MoE on CUDA: `TF_QWEN_KV_PAGES`, `TF_QWEN_KV_PAGES_ALL`, `TF_QWEN_KV_SHARE`.
- GLM on CUDA: `TF_GLM_KV_SHARE`, `TF_KV_PAGES`, `TF_GLM_KV_HOST`, `TF_GLM_KV_HOST_SHARED`.
- GLM on CUDA: `TF_KV_PAGES`, `TF_GLM_KV_HOST`, `TF_GLM_KV_HOST_SHARED`.
- Nemotron and Nemotron Omni on CUDA: `TF_KV_PAGES`, with their existing default-on parser.
- Gemma 4, Kolibri-1, and Spark2.5 on the core CUDA page adapter: `TF_KV_SHARE`, where the adapter is served.
- Gemma 4, Kolibri-1, Spark2.5 and GLM on the core CUDA page adapter: `TF_KV_SHARE`, where the adapter is served.
- Flash Next on CUDA: `TF_FLASHNEXT_KV_PAGES`, `TF_FLASHNEXT_KV_PAGES_ALL`.

`identical` records the existing switch's token-preserving design and linked evidence; it is not a claim that the
Expand Down
2 changes: 1 addition & 1 deletion docs/recipes/glm-5.3-flash-cuda.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ the model took 1.9 GiB more at startup. That's two fresh servers each, with the
there.

At TP=3 with `TF_GLM_ROCE_MULTI=1` (#1524) the gain is a little under half: on three GB10s at `--parallel 8` with
`TF_GLM_KV_SHARE=32` and `TENSORFOLD_PREFILL_SHARE=0.7`, not the table's settings, cold Python-source prompts of 8K to
`TF_KV_SHARE=32` and `TENSORFOLD_PREFILL_SHARE=0.7`, not the table's settings, cold Python-source prompts of 8K to
128K tokens came 3.2 to 3.4% sooner with the wire and 7.3 to 8.4% sooner without it, two fresh servers each. The wire's
extra time is in the side stream's exchanges and grows when solo doubles a pass (#1994). `TF_GLM_OVERLAP_PIECES`
(1 to 8, the same on every rank; unset, 8 when `TF_GLM_CHUNK_SOLO=4096` and `TF_GLM_ROCE_MULTI=1` are both set at three
Expand Down
2 changes: 1 addition & 1 deletion zig/build/cuda_engines.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn glmCuda(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.bu
m.addImport("lanes", lanes);
m.addImport("heat", heat);
m.addImport("tp", @import("tp.zig").module(b, target, optimize, cuda)); // over the engine's own cuda module
m.addImport("engine_api", api); // TF_GLM_KV_SHARE's prompt_radix store: family()'s, the server's
m.addImport("engine_api", api); // TF_KV_SHARE's prompt_radix store: family()'s, the server's
return m;
}

Expand Down
7 changes: 4 additions & 3 deletions zig/build/glm_family_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ pub fn steps(b: *std.Build, cuda: *std.Build.Module, glm: *std.Build.Module, mod
const pages_run = b.addRunArtifact(b.addTest(.{ .root_module = pages, .filters = &.{"GLM pages:"} }));
b.step("test-glm-pages-host", "Actual GLM page ownership, refusal and rollback without devices").dependOn(&pages_run.step);
step.dependOn(&pages_run.step);
const image_cache = b.createModule(.{ .root_source_file = b.path("zig/glm_image_cache_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "cuda", .module = cuda }, .{ .name = "lanes", .module = mods.lanes }, .{ .name = "core", .module = mods.core } } });
const api = glm.import_table.get("engine_api").?; // the family's own (the shared store's types): one module, one set of types
const adapter = b.createModule(.{ .root_source_file = b.path("zig/src/native/cuda_prompt_pages.zig"), .target = host, .link_libc = true, .imports = &.{ .{ .name = "core", .module = mods.core }, .{ .name = "engine_api", .module = api } } });
const image_cache = b.createModule(.{ .root_source_file = b.path("zig/glm_image_cache_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "cuda", .module = cuda }, .{ .name = "lanes", .module = mods.lanes }, .{ .name = "core", .module = mods.core }, .{ .name = "prompt_pages", .module = adapter } } });
const image_cache_run = b.addRunArtifact(b.addTest(.{ .root_module = image_cache }));
b.step("test-glm-image-cache-host", "Image-content prefix reuse and suffix rows across both ranks without devices").dependOn(&image_cache_run.step);
b.step("test-glm-image-cache-host", "Image-content prefix reuse over the shared store and suffix rows across both ranks without devices").dependOn(&image_cache_run.step);
step.dependOn(&image_cache_run.step);
const page_contract = b.createModule(.{ .root_source_file = b.path("zig/tests/cuda/contract_glm_controls_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm_cuda", .module = glm }, .{ .name = "cuda", .module = cuda }, .{ .name = "core", .module = mods.core } } });
const page_contract_run = b.addRunArtifact(b.addTest(.{ .root_module = page_contract, .filters = &.{"GLM page contract:"} }));
b.step("test-glm-page-contract-host", "GLM paged contract case and planted-control inventory without devices").dependOn(&page_contract_run.step);
step.dependOn(&page_contract_run.step);
const api = glm.import_table.get("engine_api").?; // the family's own (TF_GLM_KV_SHARE's store): one module, one set of types
const admission = b.createModule(.{ .root_source_file = b.path("zig/glm_admission_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "lanes", .module = mods.lanes }, .{ .name = "engine_api", .module = api } } });
const admission_run = b.addRunArtifact(b.addTest(.{ .root_module = admission, .filters = &.{"GLM CUDA admission:"} }));
b.step("test-glm-admission-host", "Real GLM admission capabilities through the lane worker without GPU work").dependOn(&admission_run.step);
Expand Down
82 changes: 54 additions & 28 deletions zig/glm_image_cache_host.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! GLM content-keyed image resumes with the real prefix store, image admission, wire spans and splice dispatch.
//! GLM content-keyed image resumes with the real shared prompt store, image admission, wire spans and splice dispatch.
const std = @import("std");
const glm = @import("glm");
const cuda = @import("cuda");
const lanes = @import("lanes");
const core = @import("core");
const prompt_pages = @import("prompt_pages");
const t = std.testing;
const a = t.allocator;
const D = glm.vision_config.out_hidden;
const pc = glm.prefix.radix;
const N = 8193;

const Model = struct {
Expand All @@ -16,68 +16,94 @@ const Model = struct {
restored: u32 = 0,
live: usize = 0,
const Snap = struct { kept: glm.engine.Kept, words: [2]u64 };
fn bytes(_: *anyopaque, _: u32) u64 {
return @sizeOf(Snap);
}
fn save(ptr: *anyopaque, _: ?*anyopaque, at: u32) anyerror!pc.Saved {
const m: *Model = @ptrCast(@alignCast(ptr));
fn save(m: *Model, at: u32) !*anyopaque {
const s = try a.create(Snap);
m.serial += 1;
s.* = .{ .kept = .{ .id = m.serial, .at = at }, .words = m.words };
m.live += 1;
return s;
}
fn restore(ptr: *anyopaque, owner: ?*anyopaque, saved: pc.Saved) anyerror!void {
const m: *Model = @ptrCast(@alignCast(ptr));
/// The words come back now; the kept state waits in `owner` for the pass's start, as GLM's snap_restore leaves it.
fn restore(m: *Model, owner: ?*anyopaque, saved: *anyopaque) !void {
const s: *Snap = @ptrCast(@alignCast(saved));
m.words = s.words;
m.restored = s.kept.at;
try glm.prefix.Prefix.restore(ptr, owner, saved);
const o: *glm.share.Owner = @ptrCast(@alignCast(owner orelse return error.NoGlmOwner));
o.pending = &s.kept;
}
fn drop(ptr: *anyopaque, saved: pc.Saved) void {
const m: *Model = @ptrCast(@alignCast(ptr));
fn drop(m: *Model, saved: *anyopaque) void {
a.destroy(@as(*Snap, @ptrCast(@alignCast(saved))));
m.live -= 1;
}
};

/// The decls the server's adapter reads of GLM (`share_*`, `snap_*` and its grid), over the cache's pool and model.
const Family = struct {
pub const snap_grid = glm.native.snap_grid;
fn of(p: *anyopaque) *Cache {
return @ptrCast(@alignCast(p));
}
pub fn share_pool(p: *anyopaque) ?*core.kv_pool.Pool {
return &of(p).pages.pool;
}
pub fn share_attach(p: *anyopaque, prompts: ?core.kv_prompts.Prompts) void {
of(p).pages.prompts = prompts;
}
pub fn snap_bytes(_: *anyopaque, _: u32) u64 {
return @sizeOf(Model.Snap);
}
pub fn snap_save(p: *anyopaque, _: ?*anyopaque, at: u32) anyerror!*anyopaque {
return of(p).model.save(at);
}
pub fn snap_restore(p: *anyopaque, owner: ?*anyopaque, saved: *anyopaque) anyerror!void {
return of(p).model.restore(owner, saved);
}
pub fn snap_drop(p: *anyopaque, saved: *anyopaque) void {
of(p).model.drop(saved);
}
};

const Cache = struct {
pages: glm.pages.Pages = undefined,
leases: [1]core.kv_pool.Lease = .{.{}},
used: [1]bool = .{false},
ids: [16]u32 = undefined,
prefix: *glm.prefix.Prefix = undefined,
store: *prompt_pages.Adapter = undefined,
model: Model = .{},
e: glm.engine.Engine = undefined, // what GLM's share glue reads: the allocator and the pages
fn init(c: *Cache) !void {
c.pages.pool = try core.kv_pool.Pool.init(a, 64, 512);
errdefer c.pages.pool.deinit();
c.pages.layout = .{ .fixed_bytes = 0, .token_bytes = 1, .page_tokens = 512, .snap_bytes = 0 };
c.pages.leases = &c.leases;
c.pages.used = &c.used;
c.pages.prompts = null;
try t.expect(c.pages.pool.hold(&c.ids));
errdefer for (c.ids) |id| c.pages.pool.release(id);
try c.leases[0].ids.appendSlice(a, &c.ids);
errdefer c.leases[0].ids.deinit(a);
c.prefix = try glm.prefix.Prefix.initWith(a, &c.pages, 2048, 8, .{ .ptr = &c.model, .vtable = &.{ .bytes = Model.bytes, .save = Model.save, .restore = Model.restore, .drop = Model.drop } });
c.e.gpa = a;
c.e.pages = &c.pages;
c.store = (try prompt_pages.Adapter.open(Family, a, c, 512, 8)).?;
}
fn deinit(c: *Cache) void {
c.prefix.deinit();
c.store.close();
for (c.ids) |id| c.pages.pool.release(id);
c.leases[0].ids.deinit(a);
c.pages.pool.deinit();
std.debug.assert(c.model.live == 0);
}
fn keep(c: *Cache, s: *const lanes.Stream, at: u32, words: [2]u64) void {
c.model.words = words;
var keeps: glm.prefix.Keeps = undefined;
const marks = c.prefix.marks(0, s, &.{at}, &keeps).?;
var keeps: glm.share.Keeps = undefined;
const marks = glm.share.marks(&c.e, 0, s, &.{at}, &keeps).?;
marks.call(marks.ctx, at);
}
fn begin(c: *Cache, s: *const lanes.Stream) !u32 {
var owner: glm.prefix.Owner = .{};
var owner: glm.share.Owner = .{};
var adopt: std.ArrayList(u32) = .empty;
defer adopt.deinit(a);
const plan = try c.prefix.beginStream(s, &owner, &adopt);
const plan = try glm.share.begin(&c.e, s, &owner, &adopt);
defer a.free(plan.marks);
defer c.pages.give(adopt.items);
if (plan.from > 0) try t.expectEqual(plan.from, owner.pending.?.at);
Expand Down Expand Up @@ -117,19 +143,19 @@ test "GLM image cache: the actual lane opening enables a content-keyed image loo
e.capacity = N + 256;
e.pages = &c.pages;
var link: glm.follow.Link = undefined;
var back: glm.lanes.Lanes = .{ .x = .{ .e = &e, .link = &link }, .prefix = c.prefix };
var back: glm.lanes.Lanes = .{ .x = .{ .e = &e, .link = &link } };
defer back.lanes.deinit(a);
// Refuse before a device operation, after the actual cache lookup and adopt rollback.
c.pages.used = &.{};
const b = back.backend();
try t.expectError(error.LaneMemory, b.vtable.prefill_step.?(b.ptr, &s, 0));
try t.expectEqual(@as(u64, 1), c.prefix.store.tree.counts.misses);
try t.expectEqual(@as(u64, 1), c.store.store.tree.counts.misses);
c.pages.used = &c.used;
c.keep(&s, 6144, .{ 31, 47 });
c.pages.used = &.{};
try t.expectError(error.LaneMemory, b.vtable.prefill_step.?(b.ptr, &s, 0));
try t.expectEqual(@as(u32, 6144), c.model.restored);
try t.expectEqual(@as(u64, 1), c.prefix.store.tree.counts.hits);
try t.expectEqual(@as(u64, 1), c.store.store.tree.counts.hits);
try t.expectEqual(@as(usize, 0), back.lanes.count());
for (c.ids[0..12]) |id| try t.expectEqual(@as(u32, 2), c.pages.pool.holders(id));
}
Expand Down Expand Up @@ -168,21 +194,21 @@ test "GLM image cache: A B A and a changed grid resume only matching content and
try t.expectEqual([2]u64{ 23, 29 }, c.model.words);
try t.expectEqual(@as(u32, 2048), try c.begin(&sg));
sa.key = &.{};
try t.expect(!glm.prefix.cacheable(&sa));
try t.expect(!glm.share.cacheable(&sa));
try t.expectError(error.ImageCacheKeyRequired, c.begin(&sa));
sa.key = ka[0..10];
try t.expect(!glm.prefix.cacheable(&sa));
try t.expect(!glm.share.cacheable(&sa));
sa.key = &ka;
sa.prompt_logprobs = 0;
try t.expect(!glm.prefix.cacheable(&sa));
try t.expect(!glm.share.cacheable(&sa));
sa.prompt_logprobs = null;
sa.untaught = true;
try t.expect(!glm.prefix.cacheable(&sa));
try t.expect(!glm.share.cacheable(&sa));
sa.untaught = false;
sa.images = &.{};
sa.key = &.{};
try t.expectEqualSlices(u32, &ids, glm.prefix.cacheKey(&sa).?);
try t.expect(glm.prefix.cacheable(&sa));
try t.expectEqualSlices(u32, &ids, glm.share.cacheKey(&sa).?);
try t.expect(glm.share.cacheable(&sa));
}

const Device = struct {
Expand Down
5 changes: 5 additions & 0 deletions zig/glm_snapshot_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,8 @@ test "GLM snapshots: with the engine's own sequence on pages too, a prefix's siz
for (ats, sizes) |at, s| try t.expectEqual(s, f.e.stateBytes(at));
}
}

test "GLM snapshots: TF_KV_SHARE's adapter reads GLM's decls as the store it replaced: the 2,048 grid, a lookahead of 1" {
const r = adapter.cacheRules(glm.native);
try std.testing.expect(r.planned and r.grid == 2048 and r.lookahead == 1 and r.min_prompt == 0 and r.warm == .off and !r.fork);
}
4 changes: 1 addition & 3 deletions zig/src/core/profile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub const Switch = enum {
TF_QWEN_KV_PAGES,
TF_QWEN_KV_PAGES_ALL,
TF_QWEN_KV_SHARE,
TF_GLM_KV_SHARE,
TF_KV_SHARE,
TF_FLASHNEXT_KV_PAGES,
TF_FLASHNEXT_KV_PAGES_ALL,
Expand Down Expand Up @@ -58,8 +57,7 @@ pub const entries = [_]Entry{
.{ .id = .TF_QWEN_KV_PAGES, .families = qwen, .class = .identical, .effect = "KV pool page size; memory and workload tradeoffs", .evidence = "#1357" },
.{ .id = .TF_QWEN_KV_PAGES_ALL, .families = qwen, .class = .debug, .syntax = .one, .effect = "Put the engine's own stream on pages too", .evidence = "#1357" },
.{ .id = .TF_QWEN_KV_SHARE, .families = qwen, .class = .identical, .effect = "Keep prompt pages and bounded recurrent snapshots", .evidence = "#1357" },
.{ .id = .TF_GLM_KV_SHARE, .families = &.{"glm5_next"}, .class = .identical, .effect = "Keep prompt states on pool pages; requires TF_KV_PAGES", .evidence = "#1510" },
.{ .id = .TF_KV_SHARE, .families = &.{ "gemma4", "gemma4_unified", "kolibri1", "spark2_5" }, .class = .identical, .effect = "Keep prompt states over the core KV pool", .evidence = "#1657" },
.{ .id = .TF_KV_SHARE, .families = &.{ "gemma4", "gemma4_unified", "kolibri1", "spark2_5", "glm5_next" }, .class = .identical, .effect = "Keep prompt states over the core KV pool", .evidence = "#1657" },
.{ .id = .TF_FLASHNEXT_KV_PAGES, .families = flash, .class = .experimental, .effect = "KV pool page size; fast-profile qualification pending", .evidence = "#1660" },
.{ .id = .TF_FLASHNEXT_KV_PAGES_ALL, .families = flash, .class = .debug, .syntax = .one, .effect = "Put every stream on pool pages for checks", .evidence = "#1660" },
.{ .id = .TF_KV_PAGES, .families = &.{"glm5_next"}, .class = .identical, .syntax = .auto, .effect = "GLM KV pool pages; auto from two ranks with BF16, windows with FP8", .evidence = "#1322" },
Expand Down
4 changes: 2 additions & 2 deletions zig/src/families/glm/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub const prompt_solo = @import("cuda_prompt_solo.zig");
pub const prompt_logprobs = @import("cuda_prompt_logprobs.zig");
pub const engine = @import("cuda_engine.zig");
pub const pages = @import("cuda_pages.zig");
pub const prefix = @import("cuda_prefix.zig");
pub const share = @import("cuda_share.zig");
pub const open_owner = @import("cuda_open_owner.zig");
pub const images = @import("cuda_images.zig");
pub const follow = @import("cuda_follow.zig");
Expand Down Expand Up @@ -88,7 +88,7 @@ test {
_ = @import("cuda_zero_budget_test.zig");
_ = native;
_ = @import("cuda_qpack.zig");
_ = prefix;
_ = share;
_ = @import("cuda_mtp_chance.zig");
_ = @import("cuda_tile.zig");
_ = @import("heat"); // upstream's heat bands, run where the GLM ranks use them
Expand Down
2 changes: 1 addition & 1 deletion zig/src/families/glm/cuda_engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub const Engine = struct {
pages: ?*pages_mod.Pages = null, // the other streams' KV pages and sequences (cuda_pages), once the server attached them
index_split: bool = false, // the next prompt step's indexer rows split across the ranks: rank 0's choice, the chunk's flag 2
index_splits: u64 = 0, // prompt steps whose indexer rows split (glm-split-check counts them)
share_pages: bool = false, // TF_GLM_KV_SHARE: a kept state leaves its prefix's pages to the prompt cache that holds them
share_pages: bool = false, // TF_KV_SHARE: a kept state leaves its prefix's pages to the shared store that holds them

/// Opens rank `rank` of `world` over `comm`; the ranks agree on settings (cuda_ranks) before the weights load.
pub fn init(gpa: std.mem.Allocator, io: std.Io, d: *const cuda.Driver, ctx: *const cuda.Context, checkpoint: []const u8, lib: *const cuda.nccl.Library, comm: cuda.nccl.Comm, rank: usize, world: usize, o: Options) !*Engine {
Expand Down
2 changes: 1 addition & 1 deletion zig/src/families/glm/cuda_follow.zig
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub fn follow(e: *Engine, link: *Link) !void {
try link.rows(v.rows.ptr, v.rowCount() * vcfg.out_hidden * 2);
},
.snapshots => {
e.share_pages = h.flags & 1 != 0; // TF_GLM_KV_SHARE on rank 0: a kept state leaves the prompt cache's pages out
e.share_pages = h.flags & 1 != 0; // TF_KV_SHARE on rank 0: a kept state leaves the shared store's pages out
try @import("cuda_snapshots.zig").agree(e, h.a);
},
.drop => for (step.ids) |id| e.dropState(id),
Expand Down
Loading
Loading