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 @@ -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: the load races the classic and factored expert forms, which give the same bits, at each of its ten timed decode window widths and keeps the faster one at each (classic unless factored is more than 1% faster), instead of one form for every window; a window takes the form of the narrowest timed width that holds it, and `TF_GLM_EXPERT_FORM` names one form for every width (#3249). Thanks to @Bizuayeu.

- Pull: gated and private Hugging Face repos come down with your token: `HF_TOKEN`, then `HUGGING_FACE_HUB_TOKEN`, then the one token file the `hf` tools read (`HF_TOKEN_PATH`, else `$HF_HOME/token`, else `~/.cache/huggingface/token`). The token goes only to the hub's own host (`huggingface.co`, or `HF_ENDPOINT`'s), never to the storage hosts downloads are redirected to, and is never printed; a repo the hub refuses is named, with whether a token is missing or lacks access (#3524).

- Windows: when a checkpoint load releases the pages it has finished with, the process's working set is trimmed too, so those pages stop crowding a unified-memory budget; Linux and macOS are unchanged (#3866). Thanks to @ElderOrb.
Expand Down
2 changes: 1 addition & 1 deletion docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Diagnostics quote and escape the supplied text so it cannot create another termi
| `TF_GLM_DFLASH2_LOG` | Whole unsigned 64-bit count of DFlash2 rounds whose drafts are logged; default 0 |
| `TF_GLM_DRAFT_CONFIDENCE` | Finite number 0 through 1 where a DFlash2 chain's chance product stops it; default 0.7 |
| `TF_GLM_CONTEXT_CEILING` | GLM-5.3-Flash on CUDA: whole token count 1 through 2147483616, the window `--context` may ask for and `--context 0` serves, in place of config.json's 1048576; read at GLM's open. Past that length the model's quality is the operator's to check. Unset keeps config.json's window |
| `TF_GLM_EXPERT_FORM` | `classic` or `factored`: the expert form GLM's CUDA decode windows use (the same bits either way), also at a load that times nothing; unset or blank races the two when a build first loads with drafts and scratch context and keeps the winner, else classic; read on rank 0, which chooses every rank's form |
| `TF_GLM_EXPERT_FORM` | `classic` or `factored`: the expert form GLM's CUDA decode windows use at every width (the same bits either way), also at a load that times nothing; unset or blank races the two at each timed width when a build first loads with drafts and scratch context and keeps each width's winner, else classic; read on rank 0, which chooses every rank's forms |
| `TF_GLM_INDEX_CUDA` | GLM on CUDA: exactly `1` (both DSA indexer kernels), `0` (neither) or `scores` (scores only); unset or blank is `both` |
| `TF_GLM_KV_HOST_GIB` | GLM on CUDA: a finite GiB amount of at least 0 for the DSA layers' pinned host pages; unset or blank uses one full window and 16 rows a page |
| `TF_GLM_RANK` | GLM-5.3-Flash on CUDA: a whole rank index from 0 through 4294967295, set with `TF_GLM_WORLD`; unset with it gives one rank |
Expand Down
22 changes: 14 additions & 8 deletions docs/recipes/glm-5.3-flash-cuda.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,20 @@ range (0.25% at least), and inside the rest. Other checkpoints and sampled repli
## Window prices

Decode windows reach 32 rows and MTP chains 15 drafts. With drafts and enough scratch context, rank 0 races
classic NVFP4 experts against the factored form over real text on every rank, keeping the faster form with the same
bits. MTP also times windows of 1, 2, 3, 4, 6, 8, 12, 16, 24 and 32 rows, with straight lines between them, and a
chained draft step. Its startup line reports `glm: windows (timed)` and the expert form. DFlash2 keeps the existing
seed forward and step prices in lone and shared rounds; it times only the target expert-form race and reports
`glm: expert form (timed)`. No unused DFlash2 window or draft-step table is measured or published to the lane core.
`TF_GLM_EXPERT_FORM=classic` or `factored` names the form instead and skips the race, so launches compare at one
form, and a load that times nothing still runs it (`glm: expert form (untimed)`); a named form's startup line puts
`(TF_GLM_EXPERT_FORM)` after the form's word.
classic NVFP4 experts against the factored form, which gives the same bits, over real text on every rank at windows
of 1, 2, 3, 4, 6, 8, 12, 16, 24 and 32 rows. Five untimed sweeps of every width on both forms come first, then each
width's timed windows take the two forms in turn, classic first on even rounds and factored first on odd ones, so
neither form is timed in a load's first seconds, and a drift through the race or a window right after a switch of
form falls on both alike. Each width keeps its faster form, classic unless factored is more than 1% faster, and a
window runs on the form of the narrowest timed width that holds it. `TF_GLM_EXPERT_FORM=classic` or `factored`
names one form for every width instead and skips the race, so launches compare at one form, and a load that times
nothing still runs it (`glm: expert form (untimed)`); a named form's startup line puts `(TF_GLM_EXPERT_FORM)` after
the form's word. MTP prices each width at its kept form's time, with straight lines between them, and a chained
draft step. Its startup line reports `glm: windows (timed)` and the forms: one word when every width shares one,
else the more common form and the widths where the other runs, as in `experts factored, classic at 4 and 6 rows`.
DFlash2 keeps the existing seed forward and step prices in lone and shared rounds; it times only the target
expert-form race and reports `glm: expert form (timed)`. No unused DFlash2 window or draft-step table is measured or
published to the lane core.

The measurements stay in `~/.cache/tensorfold/window-costs`, keyed by the build, GPU/driver, full checkpoint and drafter
identity, drafter precision, rank count, layer limit, L2-prefetch options, loaded NVFP4 decode warps, KDA and matmul
Expand Down
150 changes: 116 additions & 34 deletions zig/glm_lane_costs_host.zig
Original file line number Diff line number Diff line change
@@ -1,76 +1,158 @@
//! The production load-time measurement controller over recording timers, without devices or a checkpoint.
const std = @import("std");
const costs = @import("glm").lane_costs;
const glm = @import("glm");
const costs = glm.lane_costs;
const cuda = @import("cuda");
const t = std.testing;
const m = costs.measurements;
const Recording = struct {
windows: [costs.widths.len]usize = @splat(0),
drafts: usize = 0,
forms: [3]bool = undefined,
changes: usize = 0,
fact: bool = false,
pub fn form(r: *@This(), fact: bool) !void {
if (r.changes >= r.forms.len) return error.TooManyFormChanges;
r.forms[r.changes] = fact;
fact: u64 = 0,
ratio: [costs.widths.len]f64 = @splat(0.8), // a factored window's time over a classic one's, by width
runs: usize = 0, // windows run so far
slow: usize = 0, // the first this many windows run 15 ms over, as in a load's first seconds
drift: f64 = 0, // a window's time moves by this fraction a window run before it
switched: [2]f64 = .{ 0, 0 }, // classic's and factored's fraction over right after a window on the other form
last: ?bool = null, // the last window's form, factored or not
pub fn form(r: *@This(), fact: u64) !void {
r.changes += 1;
r.fact = fact;
}
fn classic(i: usize) f64 {
return 20 + @as(f64, @floatFromInt(costs.widths[i]));
}
pub fn window(r: *@This(), i: usize) !f64 {
r.windows[i] += 1;
return (20 + @as(f64, @floatFromInt(costs.widths[i]))) * @as(f64, if (r.fact) 0.8 else 1);
defer r.runs += 1;
const f = (r.fact >> @intCast(i)) & 1 != 0;
defer r.last = f;
const after = if (r.last) |l| (if (l != f) r.switched[@intFromBool(f)] else 0) else 0;
const ms = classic(i) * (if (f) r.ratio[i] else 1) * (1 + after);
return ms * (1 + r.drift * @as(f64, @floatFromInt(r.runs))) + @as(f64, if (r.runs < r.slow) 15 else 0);
}
pub fn draft(r: *@This(), depth: usize) !f64 {
r.drafts += 1;
return 2 + 0.5 * @as(f64, @floatFromInt(depth));
}
};

test "GLM load prices: DFlash2 races expert forms without timing unused windows or head steps" {
/// Each width's windows in a race: the untimed sweeps on both forms, then seven timed on each.
const raced = 2 * m.sweeps + 14;

test "GLM load prices: DFlash2 races the forms at every width without timing head steps or keeping prices" {
var r: Recording = .{};
const kept = try costs.measurements.measure(&r, true, null);
try t.expectEqual([3]bool{ false, true, true }, r.forms);
const kept = try m.measure(&r, true, null);
try t.expectEqual(m.all, r.fact);
try t.expectEqual(m.all, kept.fact);
try t.expectEqual(@as(usize, 0), r.drafts);
for (r.windows, 0..) |count, i| {
const expected: usize = if (i == 1 or i == 3 or i == 5) 16 else if (i == costs.widths.len - 1) 8 else 0;
try t.expectEqual(expected, count);
}
try t.expect(costs.measurements.reusable(kept, true));
try t.expect(!costs.measurements.reusable(kept, false));
for (r.windows) |count| try t.expectEqual(raced, count);
try t.expect(m.reusable(kept, true));
try t.expect(!m.reusable(kept, false));
try t.expect(costs.tableFor(kept, true) == null);
}

test "GLM load prices: MTP retains every timed width and the two-level head slope" {
test "GLM load prices: MTP keeps each width's raced time as its price, then the two-level head slope" {
var r: Recording = .{};
const kept = try costs.measurements.measure(&r, false, null);
try t.expectEqual([3]bool{ false, true, true }, r.forms);
try t.expectEqual(@as(usize, 32), r.drafts);
for (r.windows, 0..) |count, i| {
const expected: usize = 8 + @as(usize, if (i == 1 or i == 3 or i == 5) 16 else if (i == costs.widths.len - 1) 8 else 0);
try t.expectEqual(expected, count);
const kept = try m.measure(&r, false, null);
try t.expectEqual(m.all, r.fact);
try t.expectEqual(@as(usize, 16), r.drafts);
for (r.windows, kept.ms, 0..) |count, ms, i| {
try t.expectEqual(raced, count);
try t.expectApproxEqAbs(Recording.classic(i) * 0.8, ms, 1e-12);
}
try t.expectApproxEqAbs(@as(f64, 0.5), kept.step_ms, 1e-12);
try t.expect(costs.measurements.reusable(kept, false));
try t.expect(!costs.measurements.reusable(kept, true));
try t.expect(m.reusable(kept, false));
try t.expect(!m.reusable(kept, true));
try t.expect(costs.tableFor(kept, false) != null);
try t.expect(costs.tableFor(kept, true) == null);
}

test "GLM load prices: each width keeps the form its windows ran faster on, and classic inside the margin" {
var r: Recording = .{};
r.ratio = .{ 0.995, 0.97, 0.97, 1.02, 1.02, 0.97, 0.97, 0.97, 0.97, 0.97 };
const kept = try m.measure(&r, false, null);
const want = m.all & ~@as(u64, 0b1_1001); // classic at 1 row (inside the margin), 4 and 6
try t.expectEqual(want, r.fact);
try t.expectEqual(want, kept.fact);
for (kept.ms, 0..) |ms, i| {
const f = if ((want >> @intCast(i)) & 1 != 0) r.ratio[i] else 1;
try t.expectApproxEqAbs(Recording.classic(i) * f, ms, 1e-12);
}
}

test "GLM load prices: a dip is timed again at each width's own form, not the last raced one" {
var r: Recording = .{};
r.ratio = @splat(0.97);
r.ratio[0] = 0.995; // inside the margin: 1 row stays classic
r.ratio[1] = 0.9; // 2 rows then dip under 1 row's price
const kept = try m.measure(&r, false, null);
try t.expectEqual(@as(u64, 0), kept.fact & 1);
try t.expect(r.windows[0] > raced);
try t.expectApproxEqAbs(Recording.classic(0), kept.ms[0], 1e-12); // a factored retime would lower it
}

test "GLM load prices: a named expert form is set once, before the warm-up, and no forms race" {
for ([_]bool{ false, true }) |pin| for ([_]bool{ false, true }) |block| {
var r: Recording = .{};
const kept = try costs.measurements.measure(&r, block, pin);
const kept = try m.measure(&r, block, pin);
try t.expectEqual(@as(usize, 1), r.changes);
try t.expectEqual(pin, r.forms[0]);
try t.expectEqual(@as(u64, @intFromBool(pin)), kept.fact);
try t.expectEqual(m.every(pin), r.fact);
try t.expectEqual(m.every(pin), kept.fact);
try t.expectEqual(@as(usize, if (block) 0 else 16), r.drafts);
for (r.windows, 0..) |count, i| {
const warm: usize = if (i == costs.widths.len - 1) 8 else 0;
try t.expectEqual(warm + @as(usize, if (block) 0 else 8), count);
}
try t.expect(costs.measurements.reusable(kept, block));
// as many untimed windows a width as a race's sweeps, then one fastest-of; DFlash2 times none
for (r.windows) |count| try t.expectEqual(@as(usize, if (block) 0 else 2 * m.sweeps + 8), count);
try t.expect(m.reusable(kept, block));
};
}

test "GLM load prices: a slow start outlasting the old warm-up decides no width's form and sets no price" {
var r: Recording = .{};
r.ratio = @splat(1); // the two forms alike: classic at every width
r.slow = 48; // longer than a warm-up at the widest alone, as the slowest observed start was
const kept = try m.measure(&r, false, null);
try t.expectEqual(@as(u64, 0), kept.fact);
for (kept.ms, 0..) |ms, i| try t.expectApproxEqAbs(Recording.classic(i), ms, 1e-12);
}

test "GLM load prices: a drift through the race falls on both forms alike" {
const Case = struct { drift: f64, ratio: f64, want: u64 };
// faster as it runs with the forms alike, then slower with factored 2% faster at every width
for ([_]Case{ .{ .drift = -0.0002, .ratio = 1, .want = 0 }, .{ .drift = 0.0002, .ratio = 0.98, .want = m.all } }) |c| {
var r: Recording = .{};
r.ratio = @splat(c.ratio);
r.drift = c.drift;
const kept = try m.measure(&r, true, null);
try t.expectEqual(c.want, kept.fact);
}
}

test "GLM load prices: a window run slow right after the other form decides no width's form" {
var r: Recording = .{};
r.ratio = @splat(0.97);
r.switched = .{ 0.04, 0.13 }; // classic 4% and factored 13% over right after the other form
const kept = try m.measure(&r, false, null);
try t.expectEqual(m.all, kept.fact);
for (kept.ms, 0..) |ms, i| try t.expectApproxEqAbs(Recording.classic(i) * 0.97, ms, 1e-12);
}

test "GLM load prices: a window takes the form of the narrowest timed width that holds it" {
try t.expectEqual(@as(u64, 0), m.rows(0));
try t.expectEqual(~@as(u64, 0), m.rows(m.all));
try t.expectEqual(@as(u64, 1 << 3), m.rows(1 << 3)); // 4 rows alone
try t.expectEqual(@as(u64, 0b11 << 4), m.rows(1 << 4)); // 6 rows: 5 and 6
try t.expectEqual(~@as(u64, 0) << 24, m.rows(1 << 9)); // 32 rows: 25 and on
var ks: glm.experts.Kernels = undefined;
ks.fact = m.rows(m.all & ~@as(u64, 0b1_1000));
for ([_]usize{ 0, 4, 5, 6 }) |rows| try t.expect(!ks.factored(rows));
for ([_]usize{ 1, 3, 7, 32, 33, 64, 4096 }) |rows| try t.expect(ks.factored(rows));
var k: m.Kept = .{ .ms = @splat(1), .step_ms = 0, .fact = m.all };
try t.expect(m.reusable(k, false));
k.fact = m.all + 1;
try t.expect(!m.reusable(k, false));
}

const Device = struct {
fn attribute(out: *c_int, attr: cuda.abi.DeviceAttribute, _: cuda.abi.Device) callconv(.c) cuda.abi.Result {
out.* = if (attr == .compute_capability_major) 12 else 1;
Expand Down Expand Up @@ -98,7 +180,7 @@ test "GLM load prices: an oversized NCCL setting disables the cache without reje
a.free(v);
} else _ = env.unsetenv(variable);
}
var e: @import("glm").engine.Engine = undefined;
var e: glm.engine.Engine = undefined;
e.settings = .{};
e.checkpoint_digest = @splat(1);
e.share.world = 2;
Expand Down
8 changes: 7 additions & 1 deletion zig/src/families/glm/cuda_experts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ pub const Kernels = struct {
nvfp4: cuda.experts.Nvfp4Experts,
b32: ?cuda.experts32.Nvfp4b32Experts, // loaded for a csf0_v1 checkpoint alone (wantsBlock32)
router: cuda.grouped.Router,
fact: bool = false, // decode windows on NVFP4's factored tiles (the same bits): load timing picks it (cuda_lane_costs)
fact: u64 = 0, // bit r - 1: a decode window of r rows on NVFP4's factored tiles (the same bits); load timing picks them by width (cuda_lane_costs)

/// Whether a decode window of `rows` runs on the factored tiles; past 64 rows, as 64 do.
pub fn factored(ks: *const Kernels, rows: usize) bool {
const r = @min(rows, 64);
return r > 0 and (ks.fact >> @intCast(r - 1)) & 1 != 0;
}

/// `plans`: the loaded experts module (its plan kernels); `block32`: block 32's image too, for these values.
pub fn load(d: *const cuda.Driver, plans: cuda.Module, sms: u32, block32: ?cuda.experts32.Values) !Kernels {
Expand Down
12 changes: 6 additions & 6 deletions zig/src/families/glm/cuda_follow.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub const Header = extern struct {
n: u32 = 0, // ids that follow
depth: u32 = 0, // draft: drafts to chain; chunk: where it starts; shared: its parts
flags: u32 = 0, // begin: sampled; draft: from the prompt (1), chances (2); prefill, chunk: prompt_logprobs (1); chunk: indexer rows split (2); verify, shared: whole rows; snapshots: pages shared
a: u64 = 0, // begin: the prompt cache's pages first in its table; keep: rows; prefill, chunk: the kept state it resumes (0: none); redraw: the prompt's length
a: u64 = 0, // begin: the prompt cache's pages first in its table; keep: rows; prefill, chunk: the kept state it resumes (0: none); redraw: the prompt's length; form: a bit a window's rows
seed: u64 = 0, // begin: the sampling seed; prefill, chunk: the kept state's position
temperature: f64 = 0,
top_p: f64 = 0,
Expand Down Expand Up @@ -323,10 +323,10 @@ pub const Leader = struct {
try x.e.draftBlock(reqs);
}

/// Every rank's decode windows on the factored expert form or not (the same bits; load timing picks it).
pub fn expertForm(x: Leader, fact: bool) !void {
try x.send(.{ .kind = @backingInt(Kind.form), .a = @intFromBool(fact) }, &.{});
x.e.m.experts.fact = fact;
/// Every rank's expert forms, bit r - 1 for a window of r rows (the same bits; load timing picks them by width).
pub fn expertForm(x: Leader, forms: u64) !void {
try x.send(.{ .kind = @backingInt(Kind.form), .a = forms }, &.{});
x.e.m.experts.fact = forms;
}

/// The other ranks leave their loops.
Expand Down Expand Up @@ -420,7 +420,7 @@ pub fn follow(e: *Engine, link: *Link) !void {
try e.prefillMany(decoded, &firsts);
},
.keep => try e.keep(@intCast(h.a)),
.form => e.m.experts.fact = h.a != 0,
.form => e.m.experts.fact = h.a,
.draft => {
const ids = try e.gpa.dupe(u32, step.ids);
defer e.gpa.free(ids);
Expand Down
Loading