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
20 changes: 16 additions & 4 deletions docs/profiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
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 automatic paging policy (eight entries). It does not
migrate every `TF_*` or `TENSORFOLD_*` variable yet. Other settings continue through their existing readers. `fast-exact` and `fast`
switches in its first table, their `_ALL` companions, and GLM's and Nemotron's paging policies (eight 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 @@ -39,6 +39,7 @@ 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`.
- 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.
- Flash Next on CUDA: `TF_FLASHNEXT_KV_PAGES`, `TF_FLASHNEXT_KV_PAGES_ALL`.

Expand All @@ -60,11 +61,22 @@ explicitly the registry's coverage, not an assertion that the whole environment
model type/backend and arbitrary environment variables are omitted. Numeric zero for count switches and disabled exact-one
flags do not count as changes. Dependency checks still determine whether a configured switch can be used.

`TF_KV_PAGES` uses the registry's `auto` syntax: unset or exactly `auto` leaves the family's automatic choice,
For GLM CUDA, `TF_KV_PAGES` uses the registry's `auto` syntax: unset or exactly `auto` leaves the family's automatic choice,
while every other explicit value is a policy override, including `off` and `0`. An explicit policy may select the
same storage as automatic mode at a particular rank count. The GLM selection line below reports that distinction;
the generic registry line does not infer rank count or precision from the environment.
This row is scoped to GLM CUDA; Nemotron's same-named setting keeps its existing reader and semantics.
The registry has separate CUDA rows for GLM and Nemotron's same-named setting, with disjoint family scopes.
`capabilities --json` lists both rows under `registered_switches`; select a row by `id`, family and backend rather
than assuming the environment name alone identifies its policy. Lookup remains shared and returns the raw value.
An optional `report_values` list takes precedence over `syntax` for reporting exact overrides; null retains the
existing syntax rule. Nemotron's row lists `0` and `all`, while GLM's row keeps its `auto` rule.

For Nemotron (`nemotron_h` and `nemotron_h_omni`), paging is on by default. Exactly `TF_KV_PAGES=0` disables it;
exactly `TF_KV_PAGES=all` puts every stream on pages for checks. Those two values appear as registered overrides.
Every other value, including empty text, `on`, `off`, `auto`, `00`, uppercase spellings and whitespace, keeps
Nemotron's existing `on` policy and adds no override to the line. In particular, use `0`, not `off`, to disable
Nemotron paging. These are its existing parser rules, not new aliases or defaults. The later load/admission path
can still select windows for learning or a Triton-backed engine; this line reports the environment policy.

Values are bounded and terminal controls are escaped. Credentials, paths, server addresses, and unrelated environment
variables are never enumerated by this report. Each process's environment remains authoritative on restart; idle
Expand Down
25 changes: 22 additions & 3 deletions zig/src/core/profile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub const Switch = enum {
pub const Syntax = enum { count, one, auto };
pub const Entry = struct {
id: Switch,
/// When present, these exact values alone differ from the family default; otherwise use syntax.
report_values: ?[]const []const u8 = null,
families: []const []const u8,
backend: []const u8 = "cuda",
class: Class,
Expand All @@ -35,6 +37,10 @@ pub const Entry = struct {
}

pub fn differs(e: Entry, raw: []const u8) bool {
if (e.report_values) |values| {
for (values) |v| if (std.mem.eql(u8, raw, v)) return true;
return false;
}
return switch (e.syntax) {
.one => std.mem.eql(u8, raw, "1"),
.count => (std.fmt.parseInt(usize, raw, 10) catch return true) != 0,
Expand All @@ -55,18 +61,31 @@ pub const entries = [_]Entry{
.{ .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" },
.{ .id = .TF_KV_PAGES, .families = &.{ "nemotron_h", "nemotron_h_omni" }, .class = .identical, .report_values = &.{ "0", "all" }, .effect = "Nemotron KV pool pages by default; exact 0 disables, exact all pages every stream", .evidence = "#781" },
};

comptime {
if (entries.len != @typeInfo(Switch).@"enum".field_names.len) @compileError("Every profile switch needs one registry entry");
for (std.enums.values(Switch)) |id| {
var found = false;
for (entries) |e| if (e.id == id) {
found = true;
};
if (!found) @compileError("Every profile switch needs a registry entry");
}
for (entries, 0..) |e, i| {
if (@backingInt(e.id) != i) @compileError("Profile entries must follow Switch order");
if (e.stable != null) @compileError("The stable foundation must not supply new switch defaults");
for (entries[0..i]) |previous| {
if (previous.id != e.id) continue;
for (e.families) |family| if (previous.applies(family, e.backend))
@compileError("Profile switch rows must have disjoint family/backend scopes");
}
}
}

/// Rows for one variable share its lookup and stable default; reporting uses each row's family/backend scope.
pub fn entry(id: Switch) Entry {
return entries[@backingInt(id)];
for (entries) |e| if (e.id == id) return e;
unreachable;
}

/// A present environment value wins byte for byte, even empty, zero, or invalid for the family's parser.
Expand Down
33 changes: 32 additions & 1 deletion zig/src/core/profile_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test "stable profile: live lookup is exactly the legacy libc lookup for every re
}
}

test "stable profile: registry covers its switches once, scopes metadata and never supplies a default" {
test "stable profile: registry scopes metadata and never supplies a default" {
for (p.entries) |e| {
try t.expectEqual(e.id, p.entry(e.id).id);
try t.expect(e.name().len > 0 and e.families.len > 0 and e.effect.len > 0 and e.evidence.len > 0);
Expand All @@ -30,6 +30,37 @@ test "stable profile: registry covers its switches once, scopes metadata and nev
try t.expectEqual(p.Class.experimental, p.entry(.TF_FLASHNEXT_KV_PAGES).class);
}

test "stable profile: shared paging variable has disjoint GLM and Nemotron reporting policies" {
var glm: usize = 0;
var nemotron: usize = 0;
var omni: usize = 0;
for (p.entries) |e| {
if (e.id != .TF_KV_PAGES) continue;
if (e.applies("glm5_next", "cuda")) {
glm += 1;
try t.expectEqual(p.Syntax.auto, e.syntax);
try t.expect(e.differs("on"));
try t.expect(e.differs("off"));
try t.expect(!e.differs("auto"));
}
if (e.applies("nemotron_h", "cuda")) {
nemotron += 1;
try t.expectEqual(@as(usize, 2), e.report_values.?.len);
try t.expectEqualStrings("0", e.report_values.?[0]);
try t.expectEqualStrings("all", e.report_values.?[1]);
for ([_][]const u8{ "0", "all" }) |raw| try t.expect(e.differs(raw));
for ([_][]const u8{ "", "on", "off", "1", "auto", "00", "+0", "ALL", "false", " all ", "0\n" }) |raw|
try t.expect(!e.differs(raw));
}
if (e.applies("nemotron_h_omni", "cuda")) omni += 1;
try t.expectEqual(p.Class.identical, e.class);
try t.expect(e.stable == null);
}
try t.expectEqual(@as(usize, 1), glm);
try t.expectEqual(@as(usize, 1), nemotron);
try t.expectEqual(@as(usize, 1), omni);
}

test "stable profile: explicit off, empty, malformed and ordinary values are returned without coercion" {
for (p.entries) |e| for ([_][:0]const u8{ "", "0", "1", "0000", "+0", "256", "-1", "false", "nan", " 1 ", "bad\nvalue" }) |raw| {
const got = p.resolve(.stable, e.id, raw).?;
Expand Down
30 changes: 28 additions & 2 deletions zig/src/families/nemotron/cuda_native.zig
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,38 @@ fn closeCarveout(gpa: std.mem.Allocator, c: *cuda.Carveout) void {
gpa.destroy(c);
}

const Paging = enum { off, on, all };

/// TF_KV_PAGES: 0 gives every stream a window at its prefill, as before the pool; all puts every stream on pages.
fn paging() enum { off, on, all } {
const v = env("TF_KV_PAGES") orelse return .on;
fn paging() Paging {
return pagingOf(core.profile.get(.TF_KV_PAGES));
}

fn pagingOf(raw: ?[]const u8) Paging {
const v = raw orelse return .on;
return if (std.mem.eql(u8, v, "0")) .off else if (std.mem.eql(u8, v, "all")) .all else .on;
}

test "Nemotron paging: raw values preserve the original default-on expression" {
const values = [_]?[]const u8{ null, "", "0", "all", "on", "off", "auto", "1", "00", "+0", "false", "ALL", " all ", "0\n", "-1", "256" };
for (values) |raw| {
const expected: Paging = if (raw) |v|
(if (std.mem.eql(u8, v, "0")) .off else if (std.mem.eql(u8, v, "all")) .all else .on)
else
.on;
try std.testing.expectEqual(expected, pagingOf(raw));
}
}

test "Nemotron paging: registered lookup preserves the native reader's live environment policy" {
const raw = env("TF_KV_PAGES");
const expected: @TypeOf(paging()) = if (raw) |v|
(if (std.mem.eql(u8, v, "0")) .off else if (std.mem.eql(u8, v, "all")) .all else .on)
else
.on;
try std.testing.expectEqual(expected, paging());
}

fn env(name: [:0]const u8) ?[]const u8 {
return std.mem.span(platform.c.getenv(name) orelse return null);
}
Expand Down
32 changes: 32 additions & 0 deletions zig/src/server/cli_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,38 @@ test "stable profile: default and explicit spelling preserve every argument and
}
}

test "stable profile: capabilities describe the shared paging variable by family without changing its name" {
var output: std.Io.Writer.Allocating = .init(std.testing.allocator);
defer output.deinit();
try capabilities(&output.writer, .{ .version = "test" });
const decoded = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, output.written(), .{});
defer decoded.deinit();
const entries = decoded.value.object.get("profiles").?.object.get("registered_switches").?.array.items;
var policies: usize = 0;
for (entries) |row| {
const fields = row.object;
if (!std.mem.eql(u8, fields.get("id").?.string, "TF_KV_PAGES")) continue;
policies += 1;
try std.testing.expectEqualStrings("cuda", fields.get("backend").?.string);
try std.testing.expect(fields.get("stable").? == .null);
const families = fields.get("families").?.array.items;
if (fields.get("report_values").? == .null) {
try std.testing.expectEqualStrings("auto", fields.get("syntax").?.string);
try std.testing.expectEqual(@as(usize, 1), families.len);
try std.testing.expectEqualStrings("glm5_next", families[0].string);
} else {
const values = fields.get("report_values").?.array.items;
try std.testing.expectEqual(@as(usize, 2), values.len);
try std.testing.expectEqualStrings("0", values[0].string);
try std.testing.expectEqualStrings("all", values[1].string);
try std.testing.expectEqual(@as(usize, 2), families.len);
try std.testing.expectEqualStrings("nemotron_h", families[0].string);
try std.testing.expectEqualStrings("nemotron_h_omni", families[1].string);
}
}
try std.testing.expectEqual(@as(usize, 2), policies);
}

test "max frames CLI bounds and engine request preserve the default" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
Expand Down
16 changes: 16 additions & 0 deletions zig/src/server/profile_line.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,19 @@ test "profile line: control bytes and overlong values stay bounded on a single l
try std.testing.expect(line.len < 200);
for (line) |byte| try std.testing.expect(byte >= 0x20 and byte <= 0x7e);
}

test "profile line: Nemotron paging reports exact off and all, independently of GLM's auto default" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
for ([_][]const u8{ "nemotron_h", "nemotron_h_omni" }) |family| {
for ([_][]const u8{ "0", "all" }) |raw| {
const expected = try std.fmt.allocPrint(a, "profile: stable; registered overrides: TF_KV_PAGES={s} (identical, env)", .{raw});
try std.testing.expectEqualStrings(expected, try format(a, .stable, family, "cuda", TestEnv{ .glm_pages = raw }));
}
for ([_]?[]const u8{ null, "", "1", "on", "off", "auto", "00", "+0", "false", "ALL", " all ", "0\n" }) |raw|
try std.testing.expectEqualStrings("profile: stable; registered overrides: none", try format(a, .stable, family, "cuda", TestEnv{ .glm_pages = raw }));
for ([_][]const u8{ "mlx", "rocm", "xpu" }) |backend|
try std.testing.expectEqualStrings("profile: stable; registered overrides: none", try format(a, .stable, family, backend, TestEnv{ .glm_pages = "0" }));
}
}
Loading