From 6ae7ef88b72becd144df67d260824bbba16340da Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Sun, 6 Dec 2026 11:55:57 +0000 Subject: [PATCH] EXLR8: record each Metal launch's M1 and M2 thread limits The kernel contract's limits check needs every non-adapting Metal launch to carry the thread count an M1 and an M2 pipeline take, so the EXLR8 launches failed it with "no compiled thread limit recorded". These are the limits Xcode's metal-tt compiled for both families (tf-kernel-contract --offline on an M5): the same value on M1 and M2, and 448 threads or more for every 256-thread launch. Three launches needed more than a number: - tf_exlr8_project_16: a plain M1/M2 build matched no thread count, so the kernel now reserves its 256 threads with max_total_threads_per_threadgroup(256), and the record says so. The arithmetic is unchanged; only the register budget is fixed. - tf_exlr8_prompt and tf_exlr8_dense_prompt use the tensor units, which Xcode cannot build for M1/M2 ahead of time; they are marked offline = false like the other tensor-unit kernels. The split kernels already size their groups to the pipeline (adapts). A new test checks that every record fits M1 and M2 one of these ways. --- zig/kernels/metal/core/exlr8_projection.metal | 16 +++--- zig/src/core/contract/exlr8_records.zig | 53 +++++++++++++++---- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/zig/kernels/metal/core/exlr8_projection.metal b/zig/kernels/metal/core/exlr8_projection.metal index 64db16b6a3..111c239358 100644 --- a/zig/kernels/metal/core/exlr8_projection.metal +++ b/zig/kernels/metal/core/exlr8_projection.metal @@ -260,9 +260,11 @@ inline void tf_exlr8_split(const device float* X, const device uchar* W, const d } } +// 16 rows reserve their 256 threads: a plain M1/M2 build of that kernel fits no thread count (contract --offline). +#define TF_EXLR8_RESERVE_256 [[kernel, max_total_threads_per_threadgroup(256)]] // Capacities 1-4 reuse the inactive input tile for final sums; capacities 8 and 16 keep separate sums. -#define TF_EXLR8_PROJECT(R) \ - kernel void tf_exlr8_project_##R(const device float* X [[buffer(0)]], const device uchar* W [[buffer(1)]], \ +#define TF_EXLR8_PROJECT(R, KERNEL) \ + KERNEL void tf_exlr8_project_##R(const device float* X [[buffer(0)]], const device uchar* W [[buffer(1)]], \ const device ulong* bases [[buffer(2)]], const device half* halves [[buffer(3)]], \ const device char* signs [[buffer(4)]], const device TfExlr8Job* jobs [[buffer(5)]], \ constant TfExlr8Rows& a [[buffer(6)]], device uchar* Y [[buffer(7)]], \ @@ -278,11 +280,11 @@ inline void tf_exlr8_split(const device float* X, const device uchar* W, const d default: if (j.k & 65536u) tf_exlr8_job<0, R>(X, W, bases, halves, signs, a, j, Y, members, sg, lane, tid, xs, zs); break; \ } \ } -TF_EXLR8_PROJECT(1) -TF_EXLR8_PROJECT(2) -TF_EXLR8_PROJECT(4) -TF_EXLR8_PROJECT(8) -TF_EXLR8_PROJECT(16) +TF_EXLR8_PROJECT(1, kernel) +TF_EXLR8_PROJECT(2, kernel) +TF_EXLR8_PROJECT(4, kernel) +TF_EXLR8_PROJECT(8, kernel) +TF_EXLR8_PROJECT(16, TF_EXLR8_RESERVE_256) // A job on 256 S threads for few long jobs (core/exlr8_job.zig's splits); its bits are the one-set kernel's. #define TF_EXLR8_SPLIT(R, S) \ kernel void tf_exlr8_split_##R##_##S(const device float* X [[buffer(0)]], const device uchar* W [[buffer(1)]], \ diff --git a/zig/src/core/contract/exlr8_records.zig b/zig/src/core/contract/exlr8_records.zig index 49942b5630..d96ac54f4e 100644 --- a/zig/src/core/contract/exlr8_records.zig +++ b/zig/src/core/contract/exlr8_records.zig @@ -1,4 +1,4 @@ -//! EXLR8 v4 launch geometry. Compiler-specific register and pipeline limits remain unmeasured. +//! EXLR8 v4 launch geometry. Metal M1/M2 pipeline limits are measured; CUDA register use remains unmeasured. const std = @import("std"); const registry = @import("registry.zig"); const limits = @import("limits.zig"); @@ -6,9 +6,13 @@ const R = registry.CudaRecord; fn cudaRecord(comptime image: []const u8, comptime symbol: []const u8, threads: u32) R { return .{ .launch = .{ .name = image ++ " " ++ symbol, .symbol = symbol, .image = image, .threads = threads }, .usage = registry.none, .later_pending = true }; } -fn metalRecord(comptime symbol: []const u8, threads: u32, shared: u32, adapts: bool) limits.MetalLaunch { - return .{ .name = "EXLR8 " ++ symbol, .threads = threads, .tg_memory = shared, .adapts = adapts }; +/// Threads an M1 and an M2 pipeline take (the same for both here), as metal-tt compiles it (contract --offline). +fn metalRecord(comptime symbol: []const u8, threads: u32, shared: u32, m12: u32) limits.MetalLaunch { + return .{ .name = "EXLR8 " ++ symbol, .threads = threads, .tg_memory = shared, .m1 = m12, .m2 = m12 }; } +const project_m12 = [_]u32{ 704, 640, 576, 448 }; +const dense_m12 = [_]u32{ 576, 576, 512, 448, 576 }; +const narrow_m12 = [_]u32{ 640, 576, 576 }; const capacities = [_]u32{ 1, 2, 4, 8, 16 }; const splits = [_][2]u32{ .{ 1, 2 }, .{ 1, 4 }, .{ 2, 2 }, .{ 2, 4 }, .{ 4, 2 } }; pub const cuda = blk: { @@ -24,18 +28,45 @@ pub const cuda = blk: { pub const metal = blk: { var result: [21]limits.MetalLaunch = undefined; for (capacities, 0..) |rows, i| { - result[i] = metalRecord(std.fmt.comptimePrint("tf_exlr8_project_{d}", .{rows}), 256, rows * 128 * 8, false); - result[i + 5] = metalRecord(std.fmt.comptimePrint("tf_exlr8_dense_{d}", .{rows}), 256, rows * 128 * 4, false); + const project = std.fmt.comptimePrint("tf_exlr8_project_{d}", .{rows}); + // 16 rows: a plain M1/M2 build matches no thread count, so the source reserves its 256 threads. + result[i] = if (rows == 16) + .{ .name = "EXLR8 " ++ project, .threads = 256, .tg_memory = rows * 128 * 8, .reserved = 256 } + else + metalRecord(project, 256, rows * 128 * 8, project_m12[i]); + const dense = std.fmt.comptimePrint("tf_exlr8_dense_{d}", .{rows}); + result[i + 5] = metalRecord(dense, 256, rows * 128 * 4, dense_m12[i]); + } + for (splits, 0..) |split, i| { + const symbol = std.fmt.comptimePrint("tf_exlr8_split_{d}_{d}", .{ split[0], split[1] }); + const shared = split[0] * (640 * split[1] - 384) * 4; + result[i + 10] = .{ + .name = "EXLR8 " ++ symbol, + .threads = 256 * split[1], + .tg_memory = shared, + .adapts = true, + }; + } + result[15] = metalRecord("tf_exlr8_dense_decode", 32, 0, 1024); + // Tensor-unit (Metal 4) kernels: Xcode cannot build them for M1/M2 ahead of time. + result[16] = .{ .name = "EXLR8 tf_exlr8_prompt", .threads = 256, .tg_memory = 31232, .offline = false }; + result[17] = .{ .name = "EXLR8 tf_exlr8_dense_prompt", .threads = 64, .tg_memory = 10240, .offline = false }; + for ([_]u32{ 1, 2, 4 }, 0..) |rows, i| { + const narrow = std.fmt.comptimePrint("tf_exlr8_dense_narrow_{d}", .{rows}); + result[18 + i] = metalRecord(narrow, 256, rows * 128 * 4, narrow_m12[i]); } - for (splits, 0..) |split, i| result[i + 10] = metalRecord(std.fmt.comptimePrint("tf_exlr8_split_{d}_{d}", .{ split[0], split[1] }), 256 * split[1], split[0] * (640 * split[1] - 384) * 4, true); - result[15] = metalRecord("tf_exlr8_dense_decode", 32, 0, false); - result[16] = metalRecord("tf_exlr8_prompt", 256, 31232, false); - result[17] = metalRecord("tf_exlr8_dense_prompt", 64, 10240, false); - for ([_]u32{ 1, 2, 4 }, 0..) |rows, i| result[18 + i] = metalRecord(std.fmt.comptimePrint("tf_exlr8_dense_narrow_{d}", .{rows}), 256, rows * 128 * 4, false); break :blk result; }; -test "EXLR8 launch geometry retains all capacities and splits, without invented compiler measurements" { +test "EXLR8 launch geometry retains all capacities and splits, without invented CUDA register counts" { try std.testing.expectEqual(@as(usize, 16), cuda.len); for (cuda) |record| for (record.usage) |usage| try std.testing.expectEqual(@as(u32, 0), usage.regs); for (metal) |record| try std.testing.expect(record.tg_memory <= 32768 and record.threads <= 1024); } +test "every EXLR8 Metal launch fits M1 and M2" { + // A recorded limit, its reserved threads, groups sized to the pipeline, or tensor units (no M1/M2 build at all). + for (metal) |record| { + const recorded = record.m1 >= record.threads and record.m2 >= record.threads; + const tensor_units = !record.offline and record.threads <= 256; + try std.testing.expect(recorded or record.reserved >= record.threads or record.adapts or tensor_units); + } +}