From aad414bfbfa949296257ffd47b4b0a4d609e4410 Mon Sep 17 00:00:00 2001 From: Bizuayeu <30091157+Bizuayeu@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:01:23 +0900 Subject: [PATCH 1/4] glm cuda: TF_GLM_EXPERT_FORM names the expert form, so launches compare at one form GLM races its classic and factored NVFP4 expert forms when a build first loads and keeps the winner under a key that carries the build's hash. The race's window sums can land inside its 1% margin, so the same code built twice can serve different forms, and a comparison of two builds then carries the form along with the change it measures. TF_GLM_EXPERT_FORM=classic or factored names the form: rank 0 sets it on every rank before the warm-up, skips the race, and times the windows and the draft step at that form. A load that times nothing (no drafts, or too little scratch context) still sends the named form to every rank and logs `glm: expert form (untimed)`; unnamed, such a load stays on classic as before. The named form ends the cache identity, so its record and the race's never stand in for each other; unset or blank keeps the race and its key as they were. The startup line keeps its words and puts (TF_GLM_EXPERT_FORM) right after the form's word, which ends MTP's line. Any other text stops the load by name, as GLM's other rows do. --- docs/environment.md | 1 + docs/recipes/glm-5.3-flash-cuda.md | 10 +- zig/glm_lane_costs_host.zig | 26 ++++- zig/src/families/glm/cuda_env_test.zig | 14 ++- .../families/glm/cuda_lane_cost_measure.zig | 24 ++-- zig/src/families/glm/cuda_lane_costs.zig | 104 +++++++++++++++--- zig/src/platform/environment.zig | 1 + 7 files changed, 143 insertions(+), 37 deletions(-) diff --git a/docs/environment.md b/docs/environment.md index ec9622d3cad..55707374235 100644 --- a/docs/environment.md +++ b/docs/environment.md @@ -48,6 +48,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_PROMPT_ROWS` | Native serving: a multiple of 16 from 128 through 2048; unset keeps the chip/family default | | `TF_ANE_COLUMNS` | Whole u32 count; zero disables the experimental share; default zero | | `TF_FLASHNEXT_SCALE_INDEX` | Flash Next on Metal before tensor units: exactly `0` or `1`; default 1, where the 4-bit expert kernels (decode, grouped and prompt gathers) read each group's scale and bias through a 16-bit index into its tensor's table of pairs, the same values from fewer weight bytes, and the routed experts' stored scales and biases are not kept in memory; `0` keeps and reads the stored arrays; ignored with `--ssd-experts` | diff --git a/docs/recipes/glm-5.3-flash-cuda.md b/docs/recipes/glm-5.3-flash-cuda.md index 76bb177a8df..b882983940f 100644 --- a/docs/recipes/glm-5.3-flash-cuda.md +++ b/docs/recipes/glm-5.3-flash-cuda.md @@ -134,12 +134,16 @@ bits. MTP also times windows of 1, 2, 3, 4, 6, 8, 12, 16, 24 and 32 rows, with s 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. 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 -modes, dense/head bits, KV placement and the recorded NCCL channel/P2P settings. The DFlash2 form-only records have -separate keys from window tables. An identity too long for its buffer skips both price caches with a log line and -still measures the required work; it never truncates an identity or refuses the model for that reason. +modes, dense/head bits, KV placement, the recorded NCCL channel/P2P settings and a form `TF_GLM_EXPERT_FORM` names, so +a named form never reads the race's record. The DFlash2 form-only records have separate keys from window tables. An +identity too long for its buffer skips both price caches with a log line and still measures the required work; it +never truncates an identity or refuses the model for that reason. An older TP=2 GB10 receipt, before the unused DFlash2 timing was removed, had one empty-price-cache load of 85.2 s and other loads of 70-75 s. Its drafter mode is not recorded. That single sample does not estimate this startup path. diff --git a/zig/glm_lane_costs_host.zig b/zig/glm_lane_costs_host.zig index 4216a8f5b22..9a626876b0a 100644 --- a/zig/glm_lane_costs_host.zig +++ b/zig/glm_lane_costs_host.zig @@ -27,7 +27,7 @@ const Recording = struct { test "GLM load prices: DFlash2 races expert forms without timing unused windows or head steps" { var r: Recording = .{}; - const kept = try costs.measurements.measure(&r, true); + const kept = try costs.measurements.measure(&r, true, null); try t.expectEqual([3]bool{ false, true, true }, r.forms); try t.expectEqual(@as(usize, 0), r.drafts); for (r.windows, 0..) |count, i| { @@ -41,7 +41,7 @@ test "GLM load prices: DFlash2 races expert forms without timing unused windows test "GLM load prices: MTP retains every timed width and the two-level head slope" { var r: Recording = .{}; - const kept = try costs.measurements.measure(&r, false); + 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| { @@ -55,6 +55,22 @@ test "GLM load prices: MTP retains every timed width and the two-level head slop try t.expect(costs.tableFor(kept, true) == null); } +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); + 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(@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)); + }; +} + 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; @@ -99,14 +115,14 @@ test "GLM load prices: an oversized NCCL setting disables the cache without reje defer a.free(long); @memset(long, 'x'); try t.expectEqual(@as(c_int, 0), env.setenv(variable, long, 1)); - try t.expect((try costs.keyParts(&e, &ctx, &name, &shape)) == null); + try t.expect((try costs.keyParts(&e, &ctx, &name, &shape, null)) == null); try t.expectEqual(@as(c_int, 0), env.setenv(variable, "SYS", 1)); - const parts = (try costs.keyParts(&e, &ctx, &name, &shape)) orelse return error.ExpectedShortIdentity; + const parts = (try costs.keyParts(&e, &ctx, &name, &shape, null)) orelse return error.ExpectedShortIdentity; try t.expectEqualStrings("glm-cuda", parts[0]); var dr: @TypeOf(e.drafter.?.*) = undefined; dr.bits = 4; dr.checkpoint_digest = @splat(2); e.drafter = &dr; - const form = (try costs.keyParts(&e, &ctx, &name, &shape)) orelse return error.ExpectedFormIdentity; + const form = (try costs.keyParts(&e, &ctx, &name, &shape, null)) orelse return error.ExpectedFormIdentity; try t.expectEqualStrings("glm-cuda-form", form[0]); } diff --git a/zig/src/families/glm/cuda_env_test.zig b/zig/src/families/glm/cuda_env_test.zig index cb457eda5a4..a74685852f0 100644 --- a/zig/src/families/glm/cuda_env_test.zig +++ b/zig/src/families/glm/cuda_env_test.zig @@ -21,6 +21,7 @@ const counts = [_][:0]const u8{"TF_GLM_DFLASH2_LOG"}; const reals = [_][:0]const u8{"TF_GLM_DRAFT_CONFIDENCE"}; /// The window GLM may serve past config.json's: GLM's raisedCeiling reads it at the open, after these rows' check. const windows = [_][:0]const u8{"TF_GLM_CONTEXT_CEILING"}; +const words = [_][:0]const u8{"TF_GLM_EXPERT_FORM"}; /// The row by name without `rule`, so a missing row fails an assertion instead of panicking. fn row(name: []const u8) !e.Rule { @@ -58,12 +59,23 @@ test "GLM strict env: every GLM reader's name is a glm row, and the glm scope ho for (counts) |name| try t.expectEqual(e.Kind.u64, (try row(name)).kind); for (reals) |name| try t.expectEqual(e.Kind.real, (try row(name)).kind); for (windows) |name| try t.expectEqual(e.Kind.u64, (try row(name)).kind); + for (words) |name| try t.expectEqual(e.Kind.word, (try row(name)).kind); var n: usize = 0; for (e.rules) |r| if (r.scope == .glm) { try t.expect(std.mem.startsWith(u8, r.name, "TF_GLM_")); n += 1; }; - try t.expectEqual(switches.len + counts.len + reals.len + windows.len, n); + try t.expectEqual(switches.len + counts.len + reals.len + windows.len + words.len, n); +} + +test "GLM strict env: the expert form takes classic or factored, leaves blank to the race, and refuses the rest by name" { + for ([_]?[]const u8{ null, "", " ", " \t\r\n" }) |raw| { + try t.expectEqual(@as(?u64, null), try e.value(?u64, words[0], raw, null)); + if (raw) |v| try check(words[0], v, true); + } + try t.expectEqual(@as(?u64, 0), try e.value(?u64, words[0], "classic", null)); + try t.expectEqual(@as(?u64, 1), try e.value(?u64, words[0], "factored", null)); + for ([_][]const u8{ "Classic", "FACTORED", "0", "1", "race", "auto", " classic", "factored " }) |raw| try check(words[0], raw, false); } test "GLM strict env: switches keep unset, blank, 0 and 1, and refuse words and padded digits by name" { diff --git a/zig/src/families/glm/cuda_lane_cost_measure.zig b/zig/src/families/glm/cuda_lane_cost_measure.zig index 7487bc8a972..f4d00545c80 100644 --- a/zig/src/families/glm/cuda_lane_cost_measure.zig +++ b/zig/src/families/glm/cuda_lane_cost_measure.zig @@ -8,8 +8,8 @@ const warm = 8; const levels = [2]usize{ 1, 8 }; pub const Kept = extern struct { ms: [widths.len]f64, step_ms: f64, fact: u64 }; -/// Warm the target, race its expert forms, then measure only the prices that this drafter consumes. -pub fn measure(timer: anytype, block: bool) !Kept { +/// Warm the target, race its expert forms unless `pin` names one, then measure only the prices this drafter consumes. +pub fn measure(timer: anytype, block: bool, pin: ?bool) !Kept { const Window = struct { timer: @TypeOf(timer), pub fn time(w: @This(), i: usize) !f64 { @@ -24,15 +24,19 @@ pub fn measure(timer: anytype, block: bool) !Kept { }; const w: Window = .{ .timer = timer }; const d: Draft = .{ .timer = timer }; + if (pin) |f| try timer.form(f); for (0..warm) |_| _ = try w.time(widths.len - 1); - var forms: [2]f64 = .{ 0, 0 }; - for (&forms, [_]bool{ false, true }) |*sum, fact| { - try timer.form(fact); - for (race) |i| sum.* += try rule.fastest(w, i); - if (!block) sum.* += try rule.fastest(d, levels[0]); - } - const fact = forms[1] < forms[0] * (1 - margin); - try timer.form(fact); + const fact = pin orelse race: { + var forms: [2]f64 = .{ 0, 0 }; + for (&forms, [_]bool{ false, true }) |*sum, f| { + try timer.form(f); + for (race) |i| sum.* += try rule.fastest(w, i); + if (!block) sum.* += try rule.fastest(d, levels[0]); + } + const won = forms[1] < forms[0] * (1 - margin); + try timer.form(won); + break :race won; + }; var k: Kept = .{ .ms = @splat(0), .step_ms = 0, .fact = @intFromBool(fact) }; if (block) return k; for (&k.ms, 0..) |*m, i| m.* = try rule.fastest(w, i); diff --git a/zig/src/families/glm/cuda_lane_costs.zig b/zig/src/families/glm/cuda_lane_costs.zig index 1e707010470..da81e98e7c3 100644 --- a/zig/src/families/glm/cuda_lane_costs.zig +++ b/zig/src/families/glm/cuda_lane_costs.zig @@ -59,16 +59,17 @@ pub fn tableFor(k: Kept, block: bool) ?Table { /// Rank 0 chooses expert forms; prices are null for DFlash2, no drafts, or too little scratch context. pub fn price(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: []const u8, x: follow.Leader) !?Table { const e = x.e; - if (!e.mtp or e.capacity < prefix + 2 * fw.window_rows + 2) return null; + const pin = try pinnedForm(); + if (!e.mtp or e.capacity < prefix + 2 * fw.window_rows + 2) return untimed(x, pin); var name: [256]u8 = undefined; var shape: [512]u8 = undefined; const block = e.drafter != null; - const parts = try keyParts(e, ctx, &name, &shape); + const parts = try keyParts(e, ctx, &name, &shape, pin); const key = if (parts) |v| cache.key(gpa, io, &v) catch null else null; - if (key) |k| if (cache.load(Kept, gpa, io, k)) |kept| if (measurements.reusable(kept, block)) { + if (key) |k| if (cache.load(Kept, gpa, io, k)) |kept| if (measurements.reusable(kept, block) and (pin == null or pin.? == (kept.fact != 0))) { try x.expertForm(kept.fact != 0); try x.idle(); - report(kept, "kept", block); + report(kept, "kept", block, pin != null); return tableFor(kept, block); }; const path = try std.fs.path.join(gpa, &.{ dir, "tokenizer.json" }); @@ -80,49 +81,77 @@ pub fn price(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: if (ids.len < prefix + fw.window_rows + 1) return error.CostTextTooShort; var s: Scratch = .{ .x = x, .io = io, .ids = ids, .most = @min(room, e.capacity - fw.window_rows - prefix - 2), .drafting = !block }; try s.begin(); - var kept = try measurements.measure(&s, block); + var kept = try measurements.measure(&s, block, pin); const reference = if (!block and parts != null) cache.referenceKey(&parts.?) else null; if (reference) |key_ref| if (cache.load(Kept, gpa, io, key_ref)) |r| if (rule.drifted(&kept.ms, &r.ms)) try rule.again(Windows{ .s = &s }, &kept.ms); try x.release(0); try x.idle(); if (key) |k| cache.save(Kept, gpa, io, k, kept); if (reference) |key_ref| cache.save(Kept, gpa, io, key_ref, kept); - report(kept, "timed", block); + report(kept, "timed", block, pin != null); return tableFor(kept, block); } -fn report(k: Kept, how: []const u8, block: bool) void { +/// TF_GLM_EXPERT_FORM on rank 0, which chooses every rank's form: null races classic and factored, else the one named. +fn pinnedForm() !?bool { + const i = (try sys.environment.read(?u64, "TF_GLM_EXPERT_FORM", null)) orelse return null; + return i == 1; +} + +/// A load that times nothing still sends a named form to every rank; unnamed, the forms stay classic. +fn untimed(x: anytype, pin: ?bool) !?Table { + const f = pin orelse return null; + try x.expertForm(f); + try x.idle(); + std.log.info(untimed_line, .{if (f) "factored" else "classic"}); + return null; +} + +/// The startup lines; a named form puts ` (TF_GLM_EXPERT_FORM)` right after the form's word. +const window_line = "glm: windows ({s}): {d:.2} ms at 1 row, {d:.2} at 4, {d:.2} at 32, a draft {d:.2} ms; experts {s}{s}"; +const form_line = "glm: expert form ({s}): {s}{s}; DFlash2 keeps its seed window and step prices"; +const untimed_line = "glm: expert form (untimed): {s} (TF_GLM_EXPERT_FORM)"; + +fn report(k: Kept, how: []const u8, block: bool, pinned: bool) void { + const form = if (k.fact != 0) "factored" else "classic"; if (block) { - std.log.info("glm: expert form ({s}): {s}; DFlash2 keeps its seed window and step prices", .{ how, if (k.fact != 0) "factored" else "classic" }); + std.log.info(form_line, .{ how, form, named(pinned) }); return; } - std.log.info("glm: windows ({s}): {d:.2} ms at 1 row, {d:.2} at 4, {d:.2} at 32, a draft {d:.2} ms; experts {s}", .{ how, k.ms[0], k.ms[3], k.ms[widths.len - 1], k.step_ms, if (k.fact != 0) "factored" else "classic" }); + std.log.info(window_line, .{ how, k.ms[0], k.ms[3], k.ms[widths.len - 1], k.step_ms, form, named(pinned) }); +} + +fn named(pinned: bool) []const u8 { + return if (pinned) " (TF_GLM_EXPERT_FORM)" else ""; } -/// Cache identity: the GPU and driver, this checkpoint and drafter, and the settings a window's time follows. -pub fn keyParts(e: *const engine.Engine, ctx: *const cuda.Context, name: []u8, shape: []u8) !?[3][]const u8 { - const at = cacheShape(e, try ctx.capability(), try ctx.d.version(), shape) orelse return null; +/// Cache identity: the GPU and driver, this checkpoint and drafter, the settings a window's time follows, a named form. +pub fn keyParts(e: *const engine.Engine, ctx: *const cuda.Context, name: []u8, shape: []u8, pin: ?bool) !?[3][]const u8 { + const at = cacheShape(e, try ctx.capability(), try ctx.d.version(), shape, pin) orelse return null; return .{ if (e.drafter != null) "glm-cuda-form" else "glm-cuda", ctx.name(name) catch "gpu", at }; } /// An oversized identity disables caching; it does not reject a valid model or truncate a key. -fn cacheShape(e: *const engine.Engine, sm: usize, driver: c_int, shape: []u8) ?[]const u8 { - return keyShape(e, sm, driver, shape) catch { +fn cacheShape(e: *const engine.Engine, sm: usize, driver: c_int, shape: []u8, pin: ?bool) ?[]const u8 { + return keyShape(e, sm, driver, shape, pin) catch { std.log.warn("glm: lane cost identity exceeds its buffer; skipping the lane cost cache", .{}); return null; }; } -fn keyShape(e: *const engine.Engine, sm: usize, driver: c_int, shape: []u8) ![]const u8 { +/// A named form ends the identity, so a race's kept prices and a named form's never stand in for each other. +fn keyShape(e: *const engine.Engine, sm: usize, driver: c_int, shape: []u8, pin: ?bool) ![]const u8 { const set = e.settings; const hex = std.fmt.bytesToHex(e.checkpoint_digest, .lower); const draft_hex = std.fmt.bytesToHex(if (e.drafter) |dr| dr.checkpoint_digest else @as([32]u8, @splat(0)), .lower); - return std.fmt.bufPrint(shape, "sm{d} cuda{d} ranks{d} {s} drafter{d}/{d}/{s} layers{d} l2{d}/{d}/{d}/{d}/{d} warps{d} kda{d} mm{d} bits{d}/{d} kv8{d} pages{d} host{d} roce{d} p2p{s}/{s}/{s}", .{ + const tail: []const u8 = if (pin) |f| (if (f) " form factored" else " form classic") else ""; + return std.fmt.bufPrint(shape, "sm{d} cuda{d} ranks{d} {s} drafter{d}/{d}/{s} layers{d} l2{d}/{d}/{d}/{d}/{d} warps{d} kda{d} mm{d} bits{d}/{d} kv8{d} pages{d} host{d} roce{d} p2p{s}/{s}/{s}{s}", .{ sm, driver, e.share.world, hex[0..], @intFromBool(e.drafter != null), if (e.drafter) |dr| dr.bits else @as(u8, 0), draft_hex[0..], set.layers, @intFromBool(set.l2pf.on), @intFromBool(set.l2pf.named), set.l2pf.mb, set.l2pf.ctas, set.l2pf.pace_gbps, e.m.experts.nvfp4.warps, @intFromBool(set.kda_wide), @intFromBool(set.tiled), set.dense_bits, set.head_bits, @intFromBool(set.kv8), @intFromBool(set.pages), set.kv_host, @intFromBool(set.roce_multi), sys.getenv("NCCL_P2P_LEVEL") orelse "", sys.getenv("NCCL_MIN_NCHANNELS") orelse "", sys.getenv("NCCL_MAX_NCHANNELS") orelse "", + tail, }); } @@ -214,7 +243,7 @@ test "GLM cost identity: the consumed key includes drafter precision and all tim dr.checkpoint_digest = @splat(2); e.drafter = &dr; var base: [512]u8 = undefined; - const before = try keyShape(&e, 121, 13000, &base); + const before = try keyShape(&e, 121, 13000, &base, null); for (0..11) |which| { const original = e.settings; switch (which) { @@ -232,7 +261,7 @@ test "GLM cost identity: the consumed key includes drafter precision and all tim else => unreachable, } var changed: [512]u8 = undefined; - try std.testing.expect(!std.mem.eql(u8, before, try keyShape(&e, 121, 13000, &changed))); + try std.testing.expect(!std.mem.eql(u8, before, try keyShape(&e, 121, 13000, &changed, null))); e.settings = original; e.drafter = &dr; dr.bits = 4; @@ -240,4 +269,43 @@ test "GLM cost identity: the consumed key includes drafter precision and all tim e.checkpoint_digest = @splat(1); e.m.experts.nvfp4.warps = 4; } + // a named form only ends the identity, so the race's key stays as it was + var classic: [512]u8 = undefined; + var factored: [512]u8 = undefined; + const pinned = try keyShape(&e, 121, 13000, &classic, false); + try std.testing.expect(std.mem.startsWith(u8, pinned, before) and pinned.len > before.len); + try std.testing.expect(!std.mem.eql(u8, pinned, try keyShape(&e, 121, 13000, &factored, true))); +} + +test "GLM cost identity: a named form's startup line ends at (TF_GLM_EXPERT_FORM), and a raced one as it did" { + var buf: [256]u8 = undefined; + const raced = try std.fmt.bufPrint(&buf, window_line, .{ "timed", 29.73, 44.02, 130.65, 1.53, "classic", named(false) }); + try std.testing.expect(std.mem.endsWith(u8, raced, "130.65 at 32, a draft 1.53 ms; experts classic")); + const kept = try std.fmt.bufPrint(&buf, window_line, .{ "kept", 29.73, 44.02, 130.65, 1.53, "factored", named(true) }); + try std.testing.expect(std.mem.endsWith(u8, kept, "a draft 1.53 ms; experts factored (TF_GLM_EXPERT_FORM)")); + const form = try std.fmt.bufPrint(&buf, form_line, .{ "timed", "classic", named(true) }); + try std.testing.expect(std.mem.startsWith(u8, form, "glm: expert form (timed): classic (TF_GLM_EXPERT_FORM); DFlash2")); + try std.testing.expectEqualStrings("glm: expert form (untimed): factored (TF_GLM_EXPERT_FORM)", try std.fmt.bufPrint(&buf, untimed_line, .{"factored"})); +} + +test "GLM cost identity: a load that times nothing sends a named form to every rank once, and nothing unnamed" { + const Leader = struct { + form: ?bool = null, + sent: usize = 0, + idles: usize = 0, + pub fn expertForm(l: *@This(), fact: bool) !void { + l.form = fact; + l.sent += 1; + } + pub fn idle(l: *@This()) !void { + l.idles += 1; + } + }; + for ([_]?bool{ null, false, true }) |pin| { + var l: Leader = .{}; + try std.testing.expect((try untimed(&l, pin)) == null); + try std.testing.expectEqual(pin, l.form); + try std.testing.expectEqual(@as(usize, @intFromBool(pin != null)), l.sent); + try std.testing.expectEqual(l.sent, l.idles); + } } diff --git a/zig/src/platform/environment.zig b/zig/src/platform/environment.zig index 2cf75312aeb..d4c7094238a 100644 --- a/zig/src/platform/environment.zig +++ b/zig/src/platform/environment.zig @@ -96,6 +96,7 @@ pub const rules = [_]Rule{ .{ .name = "TF_GLM_DRAFT_CONFIDENCE", .scope = .glm, .kind = .real, .max = 1, .forms = "a finite chain-chance cut from 0 to 1 (default 0.7)" }, // 2^31 - 32: GLM's slots, the window and its 32 verify rows (glm/cuda_native.zig's capacity), keep i32 positions .{ .name = "TF_GLM_CONTEXT_CEILING", .scope = .glm, .kind = .u64, .first_digit = true, .min = 1, .max = 2147483616, .forms = "a whole token count from 1 to 2147483616 (the window GLM-5.3-Flash may serve in place of config.json's)" }, + .{ .name = "TF_GLM_EXPERT_FORM", .scope = .glm, .kind = .word, .words = &.{ "classic", "factored" }, .forms = "classic or factored (unset races the two when a build first loads)" }, .{ .name = "TF_DSV41_MHC_DEFER", .scope = .dsv41, .kind = .toggle, .trim = " \t", .no_word = false, .forms = "0, 1, true, false, on, off or yes (any case)" }, .{ .name = "TF_DSV41_COEF_LATE", .scope = .dsv41, .kind = .toggle, .trim = " \t", .no_word = false, .forms = "0, 1, true, false, on, off or yes (any case)" }, .{ .name = "TF_DSV41_LOOKUP", .scope = .dsv41, .kind = .toggle, .trim = " \t", .forms = "0, 1, true, false, on, off, yes or no (any case); blank turns lookup drafts off" }, From c1149849babcb3ac284a9352339c441f4a8b8f7c Mon Sep 17 00:00:00 2001 From: Bizuayeu <30091157+Bizuayeu@users.noreply.github.com> Date: Fri, 13 Nov 2026 13:27:00 +0900 Subject: [PATCH 2/4] changelog: #3151 under Unreleased --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index faa5b46820a..ee6e6cd332c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -456,6 +456,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - GLM-5.3-Flash on Metal: the TP2 sparse-attention and KDA-rows kernels are the full kernels' templates at 32 heads, with the same compiled code; saved GLM prompt states on Metal are recomputed once after upgrading (#2786, part of #2251). Thanks to @jschmied. +- GLM-5.3-Flash on CUDA: `TF_GLM_EXPERT_FORM=classic` or `factored` names the expert form and skips the load's race between the two, so launches and builds compare at one form; a load that times nothing still takes the named form, and unset keeps the race (#3151). Thanks to @Bizuayeu. + - Metal: Qwen 3.5, LFM2-MoE, MiniMax-M3 (with its DFlash2 and DSpark drafters), Step-5 and Spark-X read MLX affine modules through one core reader, `core/quant/mlx_affine.zig`, and LFM3 takes its modules' stored shapes from it while keeping its own checks; loads are unchanged, and a module the reader refuses now reports `UnexpectedAffineTensor` and is named in the log (#2583, part of #2460). Thanks to @jschmied. ## 1.2.7 From 6e444499634ee4219f4b40434de985ed9d849bca Mon Sep 17 00:00:00 2001 From: Bizuayeu <30091157+Bizuayeu@users.noreply.github.com> Date: Sat, 14 Nov 2026 10:27:37 +0900 Subject: [PATCH 3/4] glm cuda: a lane cost record kept at one named expert form is not reused at the other The reuse check in price() moves into reuses(), which price() calls in its place. The new test keeps a classic record under a classic load's key in a temporary cache, then shows that a load naming factored finds nothing under its own key, and that reuses() refuses a record kept at the other named form while the race and the same form reuse it. --- zig/src/families/glm/cuda_lane_costs.zig | 40 +++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/zig/src/families/glm/cuda_lane_costs.zig b/zig/src/families/glm/cuda_lane_costs.zig index da81e98e7c3..da1233ad728 100644 --- a/zig/src/families/glm/cuda_lane_costs.zig +++ b/zig/src/families/glm/cuda_lane_costs.zig @@ -66,7 +66,7 @@ pub fn price(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: const block = e.drafter != null; const parts = try keyParts(e, ctx, &name, &shape, pin); const key = if (parts) |v| cache.key(gpa, io, &v) catch null else null; - if (key) |k| if (cache.load(Kept, gpa, io, k)) |kept| if (measurements.reusable(kept, block) and (pin == null or pin.? == (kept.fact != 0))) { + if (key) |k| if (cache.load(Kept, gpa, io, k)) |kept| if (reuses(kept, block, pin)) { try x.expertForm(kept.fact != 0); try x.idle(); report(kept, "kept", block, pin != null); @@ -92,6 +92,11 @@ pub fn price(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: return tableFor(kept, block); } +/// A kept record stands for this load when it holds prices and, under a named form, was kept at that form. +fn reuses(kept: Kept, block: bool, pin: ?bool) bool { + return measurements.reusable(kept, block) and (pin == null or pin.? == (kept.fact != 0)); +} + /// TF_GLM_EXPERT_FORM on rank 0, which chooses every rank's form: null races classic and factored, else the one named. fn pinnedForm() !?bool { const i = (try sys.environment.read(?u64, "TF_GLM_EXPERT_FORM", null)) orelse return null; @@ -309,3 +314,36 @@ test "GLM cost identity: a load that times nothing sends a named form to every r try std.testing.expectEqual(l.sent, l.idles); } } + +test "GLM cost identity: a record kept at one named form is not reused at the other" { + const a = std.testing.allocator; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const root = try tmp.dir.realPathFileAlloc(io, ".", a); + defer a.free(root); + var e: engine.Engine = undefined; + e.settings = .{}; + e.checkpoint_digest = @splat(1); + e.share.world = 2; + e.m.experts.nvfp4.warps = 4; + e.drafter = null; + var shapes: [2][512]u8 = undefined; + var keys: [2][32]u8 = undefined; + for ([_]bool{ false, true }, 0..) |fact, i| { + const parts = [_][]const u8{ "glm-cuda", "gpu", try keyShape(&e, 121, 13000, &shapes[i], fact) }; + keys[i] = cache.buildKey("engine", &parts); + } + var ms: [widths.len]f64 = undefined; + for (&ms, widths) |*m, w| m.* = 20 + @as(f64, @floatFromInt(w)); + cache.saveAt(Kept, a, io, keys[0], .{ .ms = ms, .step_ms = 1.5, .fact = 0 }, root); + // a load that names factored looks under its own key and finds nothing to reuse + try std.testing.expect(cache.loadAt(Kept, a, io, keys[1], root) == null); + // the classic load reads the record back and reuses it, as the race does; factored would refuse even this record + const classic = cache.loadAt(Kept, a, io, keys[0], root).?; + try std.testing.expect(reuses(classic, false, false) and reuses(classic, false, null)); + try std.testing.expect(!reuses(classic, false, true)); + var factored = classic; + factored.fact = 1; + try std.testing.expect(reuses(factored, false, true) and !reuses(factored, false, false)); +} From ea208d871da9a8b085d0567eaea25ae0a72567ff Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Tue, 17 Nov 2026 18:03:34 +0000 Subject: [PATCH 4/4] changelog: back under Unreleased --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee6e6cd332c..3aac163cff8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ 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_EXPERT_FORM=classic` or `factored` names the expert form and skips the load's race between the two, so launches and builds compare at one form; a load that times nothing still takes the named form, and unset keeps the race (#3151). Thanks to @Bizuayeu. - Security: on macOS and Windows, a library named without a folder is never loaded from the directory the server was started in; on macOS, name its folder in DYLD_FALLBACK_LIBRARY_PATH if it was only found there (#3100). - Prompt cache: with `--learn-conversations`, an image conversation's kept states stay in RAM as before, and the log now says so once per conversation ("kept RAM-only (ImageConversation)") instead of leaving disk silently unused; no prompt text or image content is logged, and replies are unchanged (#3008). @@ -456,8 +457,6 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - GLM-5.3-Flash on Metal: the TP2 sparse-attention and KDA-rows kernels are the full kernels' templates at 32 heads, with the same compiled code; saved GLM prompt states on Metal are recomputed once after upgrading (#2786, part of #2251). Thanks to @jschmied. -- GLM-5.3-Flash on CUDA: `TF_GLM_EXPERT_FORM=classic` or `factored` names the expert form and skips the load's race between the two, so launches and builds compare at one form; a load that times nothing still takes the named form, and unset keeps the race (#3151). Thanks to @Bizuayeu. - - Metal: Qwen 3.5, LFM2-MoE, MiniMax-M3 (with its DFlash2 and DSpark drafters), Step-5 and Spark-X read MLX affine modules through one core reader, `core/quant/mlx_affine.zig`, and LFM3 takes its modules' stored shapes from it while keeping its own checks; loads are unchanged, and a module the reader refuses now reports `UnexpectedAffineTensor` and is named in the log (#2583, part of #2460). Thanks to @jschmied. ## 1.2.7