From 05d17ddba90c514b50736c2461af3502dc0159d2 Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Sat, 21 Nov 2026 15:13:09 +0000 Subject: [PATCH 1/4] contract: GLM's routed-expert row launches resolve to glm_rows.metal for gate 5 The 14 'glm moe rows' records (9e52d73ef) fell to the generic 'glm ' branch of the Metal limits resolver, whose key, 'moe', names no generated kernel: the device pass stopped at error.UnknownLaunch after glm_rows_wide_q8. They take the gather source the loader builds them from (glm/wide.zig), as 'glm rows ' does. --- zig/tests/contract/metal_limits.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zig/tests/contract/metal_limits.zig b/zig/tests/contract/metal_limits.zig index 5227472e77f..41c4f838c9d 100644 --- a/zig/tests/contract/metal_limits.zig +++ b/zig/tests/contract/metal_limits.zig @@ -278,7 +278,7 @@ fn resolve(a: std.mem.Allocator, device: mtl.Device, d: limits.MetalLaunch, comp } else if (std.mem.startsWith(u8, d.name, "h3 ")) { if (!assemble) return undefined; text = @import("h3_kernels").source; - } else if (std.mem.startsWith(u8, d.name, "glm gather ") or std.mem.startsWith(u8, d.name, "glm rows ")) { + } else if (std.mem.startsWith(u8, d.name, "glm gather ") or std.mem.startsWith(u8, d.name, "glm rows ") or std.mem.startsWith(u8, d.name, "glm moe rows ")) { if (!assemble) return undefined; text = tf.glm.wide.gather_source; } else if (std.mem.startsWith(u8, d.name, "nemotron experts ")) { From 3cd27068f7e369cb732ce1f985929048666209d9 Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Sat, 21 Nov 2026 16:54:37 +0000 Subject: [PATCH 2/4] contract: GLM's KDA and index texts carry the pre-M5 define the loader adds #3921 made kernels.zig put `#define TF_GLM_M3_FAST 1` before the KDA prompt passes and glm_index_nax.metal on a GPU without tensor units. The KDA and index contracts still built those texts without it, so on an M3 the loader check refused them ("GLM contract text is not one the loader compiles") and the whole GLM contract stopped before its first case. Both texts now follow the loader, and gate 5's pre-M5 texts carry the define too. --- zig/tests/contract/metal_glm_index.zig | 15 ++++++++++++--- zig/tests/contract/metal_glm_kda.zig | 14 +++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/zig/tests/contract/metal_glm_index.zig b/zig/tests/contract/metal_glm_index.zig index b8fdaa9ad48..4f5cae6c703 100644 --- a/zig/tests/contract/metal_glm_index.zig +++ b/zig/tests/contract/metal_glm_index.zig @@ -30,9 +30,11 @@ pub const Case = struct { new: mtl.Pipeline, old: mtl.Pipeline, - /// The loader's library as it compiles it (nax.h inlined for this GPU), and main's the same way. + /// The loader's library as it compiles it (pre-M5 define, nax.h inlined for this GPU), and main's the same way. pub fn text(a: std.mem.Allocator, device: mtl.Device) ![]u8 { - return tf.frags.source(device, a, sources.glm_index_nax); + const raw = try std.mem.concat(a, u8, &.{ m3Def(!device.tensorUnits()), sources.glm_index_nax }); + defer a.free(raw); + return tf.frags.source(device, a, raw); } pub fn init(s: *bits.Suite, control: bool) !Case { @@ -107,5 +109,12 @@ pub fn collect(s: *bits.Suite, only: ?[]const u8) !void { /// Gate 5's registration `glm launch index `: the loader's index library here, and as a pre-M5 GPU builds it. pub const prefix = "glm launch index "; pub fn limitsText(a: std.mem.Allocator, device: mtl.Device, _: []const u8) !bits.Texts { - return .{ .here = try Case.text(a, device), .pre_m5 = try tf.frags.precision.source(a, false, sources.glm_index_nax, sources.nax, false) }; + const raw = try std.mem.concat(a, u8, &.{ m3Def(true), sources.glm_index_nax }); + defer a.free(raw); + return .{ .here = try Case.text(a, device), .pre_m5 = try tf.frags.precision.source(a, false, raw, sources.nax, false) }; +} + +/// kernels.zig's pre-M5 dispatch define, put before glm_index_nax.metal on a GPU without tensor units. +fn m3Def(m3_fast: bool) []const u8 { + return if (m3_fast) "#define TF_GLM_M3_FAST 1\n" else ""; } diff --git a/zig/tests/contract/metal_glm_kda.zig b/zig/tests/contract/metal_glm_kda.zig index 075edefe8a0..985ffab0c5a 100644 --- a/zig/tests/contract/metal_glm_kda.zig +++ b/zig/tests/contract/metal_glm_kda.zig @@ -18,9 +18,10 @@ const eps: f32 = 1e-5; /// The window step (all heads, one Mac's 32 heads at TP2), and an Ultra's two groups a head with the norm after. pub const Form = enum { window, window_tp, parts }; -/// kernels.zig's KDA library: kda_rows' source, then the prompt passes, the window step and the parts appended. -pub fn text(a: std.mem.Allocator) ![]u8 { - return std.mem.concat(a, u8, &.{ comptime kernels.generatedSource("kda_rows"), sources.glm_kda_prompt, sources.glm_kda_window, sources.glm_kda_parts }); +/// kernels.zig's KDA library: kda_rows, the pre-M5 define (`m3_fast`), the prompt passes, window step and parts. +pub fn text(a: std.mem.Allocator, m3_fast: bool) ![]u8 { + const m3_def = if (m3_fast) "#define TF_GLM_M3_FAST 1\n" else ""; + return std.mem.concat(a, u8, &.{ comptime kernels.generatedSource("kda_rows"), m3_def, sources.glm_kda_prompt, sources.glm_kda_window, sources.glm_kda_parts }); } fn heads(f: Form) usize { @@ -48,7 +49,7 @@ pub const Case = struct { pub fn init(s: *bits.Suite, form: Form, control: bool) !Case { const loader = try s.production(); - const own = try text(s.a); + const own = try text(s.a, !s.device.tensorUnits()); defer s.a.free(own); _ = try loader.require(own); const body = if (!control) try s.a.dupe(u8, own) else if (form == .parts) try bits.edited(s.a, own, "void glm_kda_parts(", "const uint dv0 = part * (uint)DV;", "const uint dv0 = 0u * part;") else try bits.edited(s.a, own, "void glm_kda_window(", "P[(size_t)(r0 + (int)tid) * PS + BO + h]", "P[(size_t)r0 * PS + BO + h]"); @@ -193,7 +194,6 @@ pub fn collect(s: *bits.Suite, only: ?[]const u8) !void { /// Gate 5's registrations `glm launch kda `: the loader's KDA library. pub const prefix = "glm launch kda "; -pub fn limitsText(a: std.mem.Allocator, _: mtl.Device, _: []const u8) !bits.Texts { - const t = try text(a); - return .{ .here = t, .pre_m5 = t }; +pub fn limitsText(a: std.mem.Allocator, device: mtl.Device, _: []const u8) !bits.Texts { + return .{ .here = try text(a, !device.tensorUnits()), .pre_m5 = try text(a, true) }; } From 8816f7c0c194115641625f259c5862e2be235121 Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:01:46 +0000 Subject: [PATCH 3/4] contract: GLM's index case launches the pre-M5 kernel as the loader does; its record declares 18432 bytes On a GPU without tensor units the case compiles glm_index_nax.metal with TF_GLM_M3_FAST (#3921, #4397), whose threadgroups cover 32 rows with 4 simdgroups, but encode launched it as on M5: scoreRows' 64 or 128 rows a group and 2 x rows threads. Rows past 32 x the groups' count were never written (256 x 128 x 16384: rows 64 on, byte 1065728), so the shapes gate compared unwritten floats against main's scores. The case now launches it as selectKeys does (glm/prompt.zig: 32 rows, 128 threads); rows it wrote matched main's bytes already, and the kernel is unchanged. Before M5 the planted control moves into the pre-M5 branch (both simdgroups of a row pair on the same 32 blocks), since the M5 branch it edited is not compiled there. The pre-M5 kernel stages 128 x 72 bf16 (18432 bytes) of keys; the record declared 17408 (the M5 tile, 64 x 136), so gate 5's limit now declares 18432. --- zig/src/core/contract/glm_launch_records.zig | 2 +- zig/tests/contract/metal_glm_index.zig | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/zig/src/core/contract/glm_launch_records.zig b/zig/src/core/contract/glm_launch_records.zig index 5012795db21..951465117f6 100644 --- a/zig/src/core/contract/glm_launch_records.zig +++ b/zig/src/core/contract/glm_launch_records.zig @@ -39,7 +39,7 @@ pub const records = [_]M{ .{ .name = "glm launch prompt experts glm_gather_scatter_64", .threads = 128, .tg_memory = 9728, .offline = false }, .{ .name = "glm launch prompt dense glm_mm_grouped", .threads = 128, .tg_memory = 9728, .offline = false }, // a prompt chunk's index scores in 128-row tiles (nax.h: no M1/M2 build ahead of time) - .{ .name = "glm launch index glm_index_scores_nax", .threads = 256, .tg_memory = 17408, .offline = false }, + .{ .name = "glm launch index glm_index_scores_nax", .threads = 256, .tg_memory = 18432, .offline = false }, }; test "the GLM launches fit every Apple family" { diff --git a/zig/tests/contract/metal_glm_index.zig b/zig/tests/contract/metal_glm_index.zig index 4f5cae6c703..551f0f11f3b 100644 --- a/zig/tests/contract/metal_glm_index.zig +++ b/zig/tests/contract/metal_glm_index.zig @@ -27,6 +27,7 @@ fn stride(s: c.Shape) usize { pub const Case = struct { control: bool, + m3: bool, // no tensor units: the loader's pre-M5 build and launch new: mtl.Pipeline, old: mtl.Pipeline, @@ -42,13 +43,15 @@ pub const Case = struct { const own = try text(s.a, s.device); defer s.a.free(own); _ = try loader.require(own); - const body = if (control) try bits.edited(s.a, own, "void glm_index_scores_nax(", "const int tm = 16 * int(sg);", "const int tm = 16 * int(sg / 2);") else try s.a.dupe(u8, own); + const m3 = !s.device.tensorUnits(); + const plant: [2][]const u8 = if (m3) .{ "tn = 32 * int(sg % 2);", "tn = 0;" } else .{ "const int tm = 16 * int(sg);", "const int tm = 16 * int(sg / 2);" }; + const body = if (control) try bits.edited(s.a, own, "void glm_index_scores_nax(", plant[0], plant[1]) else try s.a.dupe(u8, own); defer s.a.free(body); const main_text = try tf.frags.source(s.device, s.a, main_index); defer s.a.free(main_text); const new = try bits.pipeline(s.device, body, "glm_index_scores_nax"); errdefer new.deinit(); - return .{ .control = control, .new = new, .old = try bits.pipeline(s.device, main_text, "glm_index_scores_nax") }; + return .{ .control = control, .m3 = m3, .new = new, .old = try bits.pipeline(s.device, main_text, "glm_index_scores_nax") }; } pub fn deinit(x: *Case) void { x.new.deinit(); @@ -95,8 +98,10 @@ pub const Case = struct { e.setBuffer(r[2].buf, r[2].off, 2); e.setBuffer(r[3].buf, r[3].off, 3); e.setValue(ScoreArgs{ .p0 = @intCast(s.k), .q_stride = QR, .w_stride = HI, .s_stride = @intCast(stride(s)), .rows = @intCast(s.rows) }, 4); - const tile: usize = if (new) tf.glm.prompt.scoreRows(@intCast(most), @intCast(s.rows)) else 64; // selectKeys', main's - e.dispatchGroups(mtl.Size.of((most + 63) / 64, (s.rows + tile - 1) / tile, 1), mtl.Size.of(2 * tile, 1, 1)); + // selectKeys' launch (glm/prompt.zig): 32 rows and 128 threads before M5, else scoreRows' tile; main's 64 rows + const tile: usize = if (!new) 64 else if (x.m3) 32 else tf.glm.prompt.scoreRows(@intCast(most), @intCast(s.rows)); + const threads: usize = if (new and x.m3) 128 else 2 * tile; + e.dispatchGroups(mtl.Size.of((most + 63) / 64, (s.rows + tile - 1) / tile, 1), mtl.Size.of(threads, 1, 1)); } }; From fc48bf28e9cffc409ab642b3afdd4564dac0f3af Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:15:43 +0000 Subject: [PATCH 4/4] changelog: #4397 under Unreleased --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2019928b913..cc5985cb744 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel ## Unreleased +- Kernel contract: the GLM Metal contract runs to the end on M1 to M4 Macs again (its KDA and index texts carry the pre-M5 define the loader adds), and gate 5 resolves GLM's routed-expert row launches on every Mac instead of stopping with `UnknownLaunch` (#4397). + - Lanes: #3728's lone-stream copy pricing is taken out again: on an M3 Ultra it made Qwen3.8-27B with the DFlash2 drafter verify drafted rows that did not land, so drafted decode on long prompts is back to 1.2.10's speed, with the same tokens (#4078). - Docs: the first world-4 Flash Next receipt on HIP, oQ2-MTP at `--tp 4` on four Radeon AI PRO R9700s, with a same-binary world-2 control run that separates the world effect from engine changes (#4210, refs #2401). Thanks to @timmyit.