diff --git a/zig/kernels/metal/core/lane_projection.metal b/zig/kernels/metal/core/lane_projection.metal index 1b81b979c5b..0c3cf622c16 100644 --- a/zig/kernels/metal/core/lane_projection.metal +++ b/zig/kernels/metal/core/lane_projection.metal @@ -5,6 +5,10 @@ using namespace mpp::tensor_ops; // 1-16 bf16 rows share each group's weight read; every row's sums and split-K order hold at every width. +#ifndef TF_SIMD_FRAGS +#define TF_SIMD_FRAGS 0 +#endif + #ifndef TF_PRECOMPUTE_SUMS #define TF_PRECOMPUTE_SUMS 0 #endif @@ -24,6 +28,14 @@ inline tf_raw tf_lane_fetch(const device uint* W, const device uint4* SB, int ti #else for (int i = 0; i < WP / 2; i++) raw.w[i] = uint2(0); #endif +#if TF_SIMD_FRAGS + const device uint2* sb2 = (const device uint2*)SB; + for (int f = 0; f < NF; f++) { + const int col = n0 + f * 16 + fn; + raw.s[f] = uint4(ok && col < TF_N ? sb2[((size_t)g * TF_N + col) / 2] : uint2(0), + ok && col + 8 < TF_N ? sb2[((size_t)g * TF_N + col + 8) / 2] : uint2(0)); + } +#else // Four lanes load the metadata quads and broadcast them; all 32 lanes fetch, even for inactive rows. const ushort leader = ushort((fn & 8) | ((fn >> 2) & 1)); for (int f = 0; f < NF; f++) { @@ -31,6 +43,7 @@ inline tf_raw tf_lane_fetch(const device uint* W, const device uint4* SB, int ti ? SB[((size_t)(ok ? g : 0) * TF_N + n0 + f * 16 + fn) / 4] : uint4(0); raw.s[f] = simd_shuffle(sb, leader); } +#endif return raw; } @@ -96,7 +109,8 @@ inline void tf_stage_codes(threadgroup uint* stage, thread const tf_raw& raw, in for (int r = 0; r < 2; r++) \ for (int j = 0; j < 4; j++) { \ const int i = f * 8 + r * 4 + j; \ - C[i] = fma(s[f][j], P[i], fma(bb[f][j], r ? xs1 : xs0, C[i])); \ + C[i] = fma(s[f][j], P[TF_SIMD_FRAGS ? r * 8 + f * 4 + j : i], \ + fma(bb[f][j], r ? xs1 : xs0, C[i])); \ } \ } @@ -110,7 +124,11 @@ inline void tf_stage_codes(threadgroup uint* stage, thread const tf_raw& raw, in const int lane = int(lanei), sg = int(sgi); const short qid = short(lane) >> 2; const short fm = (qid & 4) | ((short(lane) >> 1) & 3); +#if TF_SIMD_FRAGS + const short fn = ((short(lane) & 8) >> 1) | ((short(lane) & 1) << 1); +#else const short fn = ((qid & 2) | (short(lane) & 1)) * 4; +#endif const int M = mdims[0]; const int tile = tf_tile(int(tgx)); const int n0 = tile * NT; @@ -149,7 +167,10 @@ inline void tf_stage_codes(threadgroup uint* stage, thread const tf_raw& raw, in const int m = fm + 8 * r; const int nn = n0 + f * 16 + fn; if (m < M && nn < TF_N) - for (int j = 0; j < 4; j++) Y[(size_t)m * TF_N + nn + j] = static_cast(C[f * 8 + r * 4 + j]); + for (int j = 0; j < 4; j++) { + const int col = nn + (TF_SIMD_FRAGS ? (j & 1) + 8 * (j >> 1) : j); + if (col < TF_N) Y[(size_t)m * TF_N + col] = static_cast(C[f * 8 + r * 4 + j]); + } } } diff --git a/zig/src/core/lane_projection.zig b/zig/src/core/lane_projection.zig index 818f82ec1a7..d9e43ab0358 100644 --- a/zig/src/core/lane_projection.zig +++ b/zig/src/core/lane_projection.zig @@ -1,7 +1,8 @@ -//! Affine lane projections for 1-16 bf16 rows on Metal tensor units. No family or model dependency. +//! Affine lane projections for 1-16 bf16 rows on Metal. No family or model dependency. const std = @import("std"); const mtl = @import("metal"); const ks = @import("kernel_sources"); +const frags = @import("frags.zig"); pub const weight_layout = "u32[N/32][K/group][32][group*bits/32], low-bit-first codes"; pub const reg_layout = "u32[N/32][K/64][2][32 lanes][4] 4-bit codes, regBlock order"; @@ -214,10 +215,12 @@ pub const Projection = struct { pub fn init(a: std.mem.Allocator, device: mtl.Device, l: Layout) !Projection { try l.validate(); - if (!device.tensorUnits()) return error.TensorUnitsRequired; + if (!device.tensorUnits() and (l.format.bits != 6 or l.format.group != 32 or l.cooperative or l.reg or l.precompute_sums)) return error.TensorUnitsRequired; const text = try source(a, l); defer a.free(text); - const lib = try mtl.Library.fromSource(device, text, mtl.CompileOptions.mlx()); + const adapted = try frags.source(device, a, text); + defer a.free(adapted); + const lib = try mtl.Library.fromSource(device, adapted, mtl.CompileOptions.mlx()); defer lib.deinit(); const p = try mtl.Pipeline.init(device, lib, if (l.reg) "tf_lane_reg" else if (l.cooperative) "tf_lane_pair" else "tf_lane", false); errdefer p.deinit(); diff --git a/zig/tests/core_lane_projection.zig b/zig/tests/core_lane_projection.zig index f3dbd5ea3f2..bce8c3299a8 100644 --- a/zig/tests/core_lane_projection.zig +++ b/zig/tests/core_lane_projection.zig @@ -224,17 +224,65 @@ pub fn main(init: std.process.Init) !void { .n = try std.fmt.parseInt(usize, argv[4], 10), .k = try std.fmt.parseInt(usize, argv[5], 10), }); - if (argv.len != 1) return error.UnknownArgument; + const simd_only = argv.len == 2 and std.mem.eql(u8, argv[1], "--simd"); + const flashnext_only = argv.len == 2 and std.mem.eql(u8, argv[1], "--flashnext"); + if (argv.len != 1 and !simd_only and !flashnext_only) return error.UnknownArgument; try frozen.verify(); const pool = mtl.objc.Pool.push(); defer pool.pop(); - const device = try mtl.Device.init(); + const device = mtl.Device.init() catch |err| { + if (err != error.NoDevice) return err; + std.debug.print("SKIP lane projection: no Metal device\n", .{}); + return; + }; defer device.deinit(); - if (!device.tensorUnits()) return error.TensorUnitsRequired; + // MTLGPUFamilyMetal4 is 5002; tensor operations also require the Metal 4 runtime language. + if (mtl.CompileOptions.mlx().language < mtl.types.languageVersion(4, 0) or + !device.responds("supportsFamily:") or + !mtl.objc.msg(bool, device.id, "supportsFamily:", .{@as(isize, 5002)})) + { + std.debug.print("SKIP lane projection: requires a Metal 4 runtime and device\n", .{}); + return; + } + if (simd_only and device.tensorUnits()) { + std.debug.print("SKIP simdgroup layout: requires a device without tensor units\n", .{}); + return; + } const queue = try device.queue(); defer queue.deinit(); std.debug.print("Small synthetic projection checks on {s}; no model files\n", .{device.name()}); var counts: Counts = .{}; + if (!device.tensorUnits() or flashnext_only) { + for ([_]usize{ 1, 2, 4 }) |sk| for ([_]usize{ 1, 2 }) |pf| { + for ([_]core.Output{ .f32, .bf16 }) |out| { + try check(device, queue, .{ .n = 64, .k = 224, .format = .{ .bits = 6, .group = 32 }, .sk = sk, .pf = pf, .output = out }, &counts); + } + }; + try check(device, queue, .{ .n = 96, .k = 224, .format = .{ .bits = 6, .group = 32 }, .sk = 4, .pf = 2, .ranges = &.{.{ 1, 1 }}, .groups = .{ 1, 3 }, .output = .f32 }, &counts); + if (!device.tensorUnits()) { + var unsupported = core.Layout{ .n = 64, .k = 224, .format = .{ .bits = 6, .group = 32 } }; + for ([_]u8{ 4, 8 }) |bits| { + unsupported.format.bits = bits; + try std.testing.expectError(error.TensorUnitsRequired, core.Projection.init(a, device, unsupported)); + } + for ([_]usize{ 64, 128 }) |group| { + unsupported = .{ .n = 64, .k = 7 * group, .format = .{ .bits = 6, .group = group } }; + try std.testing.expectError(error.TensorUnitsRequired, core.Projection.init(a, device, unsupported)); + } + unsupported = .{ .n = 64, .k = 224, .format = .{ .bits = 6, .group = 32 }, .precompute_sums = true }; + try std.testing.expectError(error.TensorUnitsRequired, core.Projection.init(a, device, unsupported)); + unsupported = .{ .n = 64, .k = 256, .format = .{ .bits = 4, .group = 64 }, .cooperative = true }; + try std.testing.expectError(error.TensorUnitsRequired, core.Projection.init(a, device, unsupported)); + unsupported.cooperative = false; + unsupported.precompute_sums = true; + unsupported.reg = true; + try std.testing.expectError(error.TensorUnitsRequired, core.Projection.init(a, device, unsupported)); + } + std.debug.print("PASS Flash Next: {d} layouts, {d} bit-equal row/window pairs, {d} frozen windows, {d} CPU values, {d} refusals; max fp32 error {d:.9}, max roundoff-bound fraction {d:.6}\n", .{ + counts.layouts, counts.row_pairs, counts.golden_windows, counts.cpu_values, @as(usize, if (device.tensorUnits()) 0 else 7), counts.max_error, counts.max_fraction, + }); + return; + } for ([_]u8{ 4, 6, 8 }) |bits| for ([_]usize{ 32, 64, 128 }) |group| { for ([_]usize{ 1, 2, 4 }) |sk| for ([_]usize{ 1, 2 }) |pf| { const l = core.Layout{ .n = 64, .k = 7 * group, .format = .{ .bits = bits, .group = group }, .sk = sk, .pf = pf, .output = .f32 };