Skip to content
Open
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
188 changes: 188 additions & 0 deletions docs/receipts/glm-metal-moe-sh8/perf-receipt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
{
"schema": 1,
"pack_id": "tf-receipt-v1",
"pack_sha256": "ab2a5d0ff262007e3ef4714fa46e62b2b2454b3f97b652ca18da49e4e0583c42",
"generated_at_utc": "2026-12-04T03:54:00Z",
"claim": "Fuse 8-bit/64 shared expert with 4-bit/64 routed MoE on Metal",
"git": {
"base_sha": "9cd11ef068adf656f9a84dd7014ae3b64d230887",
"head_sha": "ba662f20305179afd364aa527f52eb5b591923f8"
},
"environment": {
"chip": "apple-m3-ultra",
"os": "darwin",
"arch": "arm64",
"checkpoint_id": "Jundot/GLM-5.3-Flash-oQ4e-mtp",
"draft": true,
"base_binary_sha256": "65a6b05d7abeeeb7b0f8a1ca05e6719ac944bc37d772ef6f4d486dafe320bb84",
"head_binary_sha256": "2cc903024a67b3b97340ea0087b8277970647e8fcf08cae07bd4238ab20d3da8"
},
"control": {
"prompt_id": "prose_seeded",
"seed_a": 424242,
"seed_b": 989898,
"base_token_sha256": "bdaf94e247b8dab05a726f7c25bd0994b0658c3155d6d76aeeafc02f81c6a0cc",
"alt_token_sha256": "d443925eb6f2beb617638a36d07747264d37c0a8543db8d0e216ae790d2480b2",
"diverged": true
},
"draft_check": {
"prompt_id": "prose_greedy",
"plain_token_sha256": "a6faf066863cd535f96ed84b54124ee937b2bfcc7580e8faf32ea8444a6e970f",
"draft_token_sha256": "a6faf066863cd535f96ed84b54124ee937b2bfcc7580e8faf32ea8444a6e970f",
"equal": true
},
"order": [
"base",
"head",
"head",
"base",
"base",
"head",
"head",
"base"
],
"rounds": [
{
"index": 0,
"arm": "base",
"agg_tok_s": 58.342
},
{
"index": 1,
"arm": "head",
"agg_tok_s": 59.723
},
{
"index": 2,
"arm": "head",
"agg_tok_s": 59.449
},
{
"index": 3,
"arm": "base",
"agg_tok_s": 58.086
},
{
"index": 4,
"arm": "base",
"agg_tok_s": 58.169
},
{
"index": 5,
"arm": "head",
"agg_tok_s": 59.722
},
{
"index": 6,
"arm": "head",
"agg_tok_s": 60.609
},
{
"index": 7,
"arm": "base",
"agg_tok_s": 57.879
}
],
"summary": {
"mean_base_tok_s": 58.119,
"mean_head_tok_s": 59.876,
"gain_pct": 3.023,
"base_noise_pct": 0.8
},
"prompts": {
"prose_greedy": {
"base": {
"tokens": 128,
"prompt_tokens": 32,
"cached_tokens": 0,
"complete": true,
"token_sha256": "a6faf066863cd535f96ed84b54124ee937b2bfcc7580e8faf32ea8444a6e970f",
"output_sha256": "f90d69b6b489f033c102d14a8a6140f93e815a015b0846a261c9effc49934ffa",
"tok_s": 52.485,
"ttft_s": 0.2643
},
"head": {
"tokens": 128,
"prompt_tokens": 32,
"cached_tokens": 0,
"complete": true,
"token_sha256": "a6faf066863cd535f96ed84b54124ee937b2bfcc7580e8faf32ea8444a6e970f",
"output_sha256": "f90d69b6b489f033c102d14a8a6140f93e815a015b0846a261c9effc49934ffa",
"tok_s": 57.932,
"ttft_s": 0.2641
},
"token_equal": true
},
"code_greedy": {
"base": {
"tokens": 128,
"prompt_tokens": 38,
"cached_tokens": 0,
"complete": true,
"token_sha256": "2dd6efe66ce35ae24f61b8b62c2033e6e1f09aa06de7da8a0a11cb59b46c7778",
"output_sha256": "adf3285e8009af1c127a83015266ae82455430b913a900bee4fa3088a303ead2",
"tok_s": 55.187,
"ttft_s": 0.2963
},
"head": {
"tokens": 128,
"prompt_tokens": 38,
"cached_tokens": 0,
"complete": true,
"token_sha256": "2dd6efe66ce35ae24f61b8b62c2033e6e1f09aa06de7da8a0a11cb59b46c7778",
"output_sha256": "adf3285e8009af1c127a83015266ae82455430b913a900bee4fa3088a303ead2",
"tok_s": 55.8,
"ttft_s": 0.2966
},
"token_equal": true
},
"reasoning_greedy": {
"base": {
"tokens": 128,
"prompt_tokens": 47,
"cached_tokens": 0,
"complete": true,
"token_sha256": "822e08b8ebf69c562c5f43ae3500eb7d9bfdf0d04b9dbca1fec0eb81bb245ec2",
"output_sha256": "e6b1e83c187cfe456547603d0a0f8bb8df521d37dc0db167a3b8e835af06fa5a",
"tok_s": 65.148,
"ttft_s": 0.3198
},
"head": {
"tokens": 128,
"prompt_tokens": 47,
"cached_tokens": 0,
"complete": true,
"token_sha256": "822e08b8ebf69c562c5f43ae3500eb7d9bfdf0d04b9dbca1fec0eb81bb245ec2",
"output_sha256": "e6b1e83c187cfe456547603d0a0f8bb8df521d37dc0db167a3b8e835af06fa5a",
"tok_s": 68.229,
"ttft_s": 0.3201
},
"token_equal": true
},
"prose_seeded": {
"base": {
"tokens": 64,
"prompt_tokens": 25,
"cached_tokens": 0,
"complete": true,
"token_sha256": "bdaf94e247b8dab05a726f7c25bd0994b0658c3155d6d76aeeafc02f81c6a0cc",
"output_sha256": "2543d8ca727db7bb1a6e125e1ccfca68cfdbec7606cdebe71bdaaa530b549498",
"tok_s": 62.91,
"ttft_s": 0.234
},
"head": {
"tokens": 64,
"prompt_tokens": 25,
"cached_tokens": 0,
"complete": true,
"token_sha256": "bdaf94e247b8dab05a726f7c25bd0994b0658c3155d6d76aeeafc02f81c6a0cc",
"output_sha256": "2543d8ca727db7bb1a6e125e1ccfca68cfdbec7606cdebe71bdaaa530b549498",
"tok_s": 63.232,
"ttft_s": 0.234
},
"token_equal": true
}
},
"exactness_sha256": "142d8d9479751bcc3b33ae7cc0224231758c22566a94af6d0d118ab16ab7d359",
"receipt_sha256": "534f236f726c0c7602b1e3ef9c952c977258dff3d73264e755995b12f559cfb3"
}
16 changes: 16 additions & 0 deletions zig/src/core/contract/glm_sh8_records.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! GLM-5.3-Flash mixed 4/64 routed + 8/64 shared fused MoE launches on Metal.
const std = @import("std");
const M = @import("limits.zig").MetalLaunch;

pub const records = [_]M{
.{ .name = "glm sh8 two glm_moe_gateup_0_sh8_one", .threads = 128, .tg_memory = 0, .reserved = 512, .m1 = 512, .m2 = 512 },
.{ .name = "glm sh8 two glm_moe_down_0_sh8_one", .threads = 32, .tg_memory = 0, .m1 = 896, .m2 = 896 },
.{ .name = "glm sh8 four glm_moe_gateup_0_sh8", .threads = 512, .tg_memory = 0, .reserved = 512, .m1 = 512, .m2 = 512 },
.{ .name = "glm sh8 four glm_moe_down_0_sh8", .threads = 512, .tg_memory = 0, .m1 = 832, .m2 = 832 },
.{ .name = "glm sh8 pick glm_moe_gateup_0_sh8_pick", .threads = 32, .tg_memory = 0, .reserved = 512, .m1 = 512, .m2 = 512 },
.{ .name = "glm sh8 pick glm_moe_down_0_sh8_pick", .threads = 32, .tg_memory = 0, .m1 = 896, .m2 = 896 },
};

test "the GLM sh8 MoE launches fit every Apple family" {
try std.testing.expectEqual(@as(usize, 0), try @import("registry.zig").checkMetal(std.testing.allocator, &records, true));
}
1 change: 1 addition & 0 deletions zig/src/core/contract/registry_joins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pub const all_metal = @import("penalties_records.zig").records ++
@import("glm_tap_records.zig").records ++
@import("core_blocks_records.zig").records ++
@import("glm_dflash_records.zig").records ++
@import("glm_sh8_records.zig").records ++
@import("glm_mtp_records.zig").records ++
@import("row_wide_records.zig").records ++
@import("weight_only_records.zig").metal ++
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/glm/glm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ test {
_ = @import("../../core/copy_index.zig");
_ = @import("attn_check.zig");
_ = @import("eh_check.zig");
_ = @import("sh8_moe_test.zig");
_ = @import("launch_check.zig");
_ = @import("load_plan.zig");
_ = @import("page_cache.zig");
Expand Down Expand Up @@ -102,4 +103,5 @@ test {
_ = metal_dflash_head_depth;
_ = @import("draft_head_q4.zig");
_ = @import("mtp_opt_test.zig");
_ = @import("moe_sh8.zig");
}
52 changes: 42 additions & 10 deletions zig/src/families/glm/kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const affine_mm = @import("../../core/affine_mm.zig");
const mm_groups = @import("mm_groups.zig");
const hc = @import("../../core/hc.zig");
const wide = @import("wide.zig");
const moe_sh8 = @import("moe_sh8.zig");

/// The route's shape (config.zig refuses other checkpoints).
pub const route_shape: moe_route.Shape = .{ .hidden = 4096, .experts = 288, .topk = 8 };
Expand Down Expand Up @@ -63,6 +64,12 @@ pub const Kernels = struct {
moe_down_0_one: mtl.Pipeline,
moe_gateup_pick: mtl.Pipeline, // one row: each routed group replays the route select's picks, no select launch
moe_down_pick: mtl.Pipeline, // and the down projections of the picks in pick order
moe_gateup_0_sh8: mtl.Pipeline, // 4/64 routed experts fused with an 8/64 shared expert
moe_down_0_sh8: mtl.Pipeline,
moe_gateup_0_sh8_one: mtl.Pipeline,
moe_down_0_sh8_one: mtl.Pipeline,
moe_gateup_sh8_pick: mtl.Pipeline,
moe_down_sh8_pick: mtl.Pipeline,
combine_0: mtl.Pipeline, // their combine: the shared expert's row read from the last slot
moe_gateup_2: mtl.Pipeline,
moe_down_2: mtl.Pipeline,
Expand Down Expand Up @@ -263,17 +270,17 @@ pub fn instance(comptime key: []const u8, comptime params: []const u8, comptime

/// Generated kernel `key`'s source.
pub fn generatedSource(comptime key: []const u8) []const u8 {
return kernelOf(key).source;
return comptime kernelOf(key).source;
}

/// Generated kernel `key`'s first function.
pub fn generatedFunction(comptime key: []const u8) [:0]const u8 {
return kernelOf(key).functions[0];
return comptime kernelOf(key).functions[0];
}

fn kernelOf(comptime key: []const u8) sources.glm.Kernel {
@setEvalBranchQuota(20000);
for (sources.glm.all) |k| if (comptime std.mem.eql(u8, k.key, key)) return k;
for (sources.glm.all) |k| if (std.mem.eql(u8, k.key, key)) return k;
@compileError("no generated GLM kernel " ++ key);
}

Expand Down Expand Up @@ -305,6 +312,8 @@ fn pickSources(gpa: std.mem.Allocator, route_core: []const u8, gu: []const u8, d
return .{ gate_up, try edit(gpa, down, member, by_pick) };
}

pub const sh8Source = moe_sh8.sh8Source;

const m16_names = affine_mm.names ++ mm_groups.names;

pub fn mtpPrep(k: *const Kernels, e: mtl.ComputeEncoder, bits: u8, ids: anytype, emb: anytype, we: anytype, wh: anytype, h: anytype, out: anytype, rows: u32, width: u32, eps: f32) void {
Expand Down Expand Up @@ -357,7 +366,8 @@ fn assemble(gpa: std.mem.Allocator, device: mtl.Device, k: *Kernels, visitor: an
k.wide = null;
const m3_fast = !device.tensorUnits();
k.m3_fast = m3_fast;
var jobs: [generated.len + 29]Job = undefined;
const m3_slot = generated.len + 34;
var jobs: [m3_slot + 1]Job = undefined;
inline for (generated, 0..) |g, i| {
const src = comptime kernelOf(g.key);
const FT = @FieldType(Kernels, g.field);
Expand Down Expand Up @@ -470,32 +480,54 @@ fn assemble(gpa: std.mem.Allocator, device: mtl.Device, k: *Kernels, visitor: an
jobs[generated.len + 24] = .{ .device = device, .source = softmax_rows_src, .names = &.{"glm_softmax_rows"}, .out = @as(*[1]mtl.Pipeline, &k.softmax_rows) };
var mtp_out: [4]mtl.Pipeline = undefined;
jobs[generated.len + 27] = .{ .device = device, .source = sources.glm_mtp_opt, .names = &.{ "glm_mtp_prep_4b", "glm_mtp_prep_8b", "glm_head_top1_4b_one", "glm_reduce_top1" }, .out = &mtp_out };
const gu_from = "4096, 2048, 4, 8, 16, 128, 0, 4, 16, 8";
const dn_from = "2048, 4096, 4, 8, 16, 128, 0, 4, 16, 8";
const gu_one_from = "4096, 2048, 2, 8, 16, 128, 0, 4, 16, 8";
const dn_one_from = "2048, 4096, 2, 8, 16, 128, 0, 4, 16, 8";
const gu_sh8 = try sh8Source(gpa, gu0_src, true, gu_from, "4096, 2048, 4, 8, 16, 128, 0, 8, 16, 16", "glm_moe_gateup_0_sh8");
defer gpa.free(gu_sh8);
const dn_sh8 = try sh8Source(gpa, down0_src, false, dn_from, "2048, 4096, 4, 8, 16, 128, 0, 8, 16, 16", "glm_moe_down_0_sh8");
defer gpa.free(dn_sh8);
const gu_sh8_one = try sh8Source(gpa, gu0_src, true, gu_from, "4096, 2048, 2, 8, 16, 128, 0, 8, 16, 16", "glm_moe_gateup_0_sh8_one");
defer gpa.free(gu_sh8_one);
const dn_sh8_one = try sh8Source(gpa, down0_src, false, dn_from, "2048, 4096, 2, 8, 16, 128, 0, 8, 16, 16", "glm_moe_down_0_sh8_one");
defer gpa.free(dn_sh8_one);
const gu_sh8_pick = try sh8Source(gpa, picks[0], true, gu_one_from, "4096, 2048, 2, 8, 16, 128, 0, 8, 16, 16", "glm_moe_gateup_0_sh8_pick");
defer gpa.free(gu_sh8_pick);
const dn_sh8_pick = try sh8Source(gpa, picks[1], false, dn_one_from, "2048, 4096, 2, 8, 16, 128, 0, 8, 16, 16", "glm_moe_down_0_sh8_pick");
defer gpa.free(dn_sh8_pick);
jobs[generated.len + 28] = .{ .device = device, .source = gu_sh8, .names = &.{"glm_moe_gateup_0_sh8"}, .out = @as(*[1]mtl.Pipeline, &k.moe_gateup_0_sh8) };
jobs[generated.len + 29] = .{ .device = device, .source = dn_sh8, .names = &.{"glm_moe_down_0_sh8"}, .out = @as(*[1]mtl.Pipeline, &k.moe_down_0_sh8) };
jobs[generated.len + 30] = .{ .device = device, .source = gu_sh8_one, .names = &.{"glm_moe_gateup_0_sh8_one"}, .out = @as(*[1]mtl.Pipeline, &k.moe_gateup_0_sh8_one) };
jobs[generated.len + 31] = .{ .device = device, .source = dn_sh8_one, .names = &.{"glm_moe_down_0_sh8_one"}, .out = @as(*[1]mtl.Pipeline, &k.moe_down_0_sh8_one) };
jobs[generated.len + 32] = .{ .device = device, .source = gu_sh8_pick, .names = &.{"glm_moe_gateup_0_sh8_pick"}, .out = @as(*[1]mtl.Pipeline, &k.moe_gateup_sh8_pick) };
jobs[generated.len + 33] = .{ .device = device, .source = dn_sh8_pick, .names = &.{"glm_moe_down_0_sh8_pick"}, .out = @as(*[1]mtl.Pipeline, &k.moe_down_sh8_pick) };

const m3_src = if (m3_fast) try affine_mm.source(device, gpa, .{ .glm_m3_fast = true, .exact_nonfinite = true }) else try gpa.dupe(u8, "");
defer gpa.free(m3_src);
var m3_out: affine_mm.Pipes = undefined;
if (m3_fast) jobs[generated.len + 28] = .{ .device = device, .source = m3_src, .names = &affine_mm.names, .out = &m3_out };
const active_jobs = jobs[0 .. jobs.len - @intFromBool(!m3_fast)];
if (m3_fast) jobs[m3_slot] = .{ .device = device, .source = m3_src, .names = &affine_mm.names, .out = &m3_out };
const active_jobs = jobs[0 .. m3_slot + @intFromBool(m3_fast)];
if (comptime @TypeOf(visitor) != void) {
for (active_jobs, 0..) |j, i| {
const origins: []const []const u8 = switch (i) {
generated.len => &.{ sources.core_copy_u32, sources.glm_glue },
generated.len + 4 => &.{ sources.glm_attn, sources.nax },
generated.len + 5, generated.len + 6 => &.{ sources.core_moe_route, sources.glm_route_one },
generated.len + 7 => &.{ sources.core_affine_mm, sources.core_affine_nonfinite, sources.glm_mm_groups, sources.nax },
generated.len + 8, generated.len + 28 => &.{ sources.core_affine_mm, sources.core_affine_nonfinite, sources.nax },
generated.len + 8, m3_slot => &.{ sources.core_affine_mm, sources.core_affine_nonfinite, sources.nax },
generated.len + 9 => &.{ comptime kernelOf("kda_rows").source, sources.glm_kda_prompt, sources.glm_kda_window, sources.glm_kda_parts },
generated.len + 10, generated.len + 13 => &.{ sources.glm_sparse_nax, sources.nax },
generated.len + 11, generated.len + 14 => &.{ sources.glm_absorb_nax, sources.nax },
generated.len + 12 => &.{sources.core_hc_boundary},
generated.len + 15 => &.{ sources.glm_index_nax, sources.nax },
generated.len + 16 => &.{comptime kernelOf("moe_gateup_1").source},
generated.len + 17 => &.{comptime kernelOf("moe_down_1").source},
generated.len + 18, generated.len + 20 => &.{comptime kernelOf("moe_gateup_2").source},
generated.len + 19, generated.len + 21 => &.{comptime kernelOf("moe_down_2").source},
generated.len + 18, generated.len + 20, generated.len + 28, generated.len + 30 => &.{comptime kernelOf("moe_gateup_2").source},
generated.len + 19, generated.len + 21, generated.len + 29, generated.len + 31, generated.len + 33 => &.{comptime kernelOf("moe_down_2").source},
generated.len + 22 => &.{comptime kernelOf("moe_combine").source},
generated.len + 24 => &.{ sources.ops_softmax, sources.glm_softmax_rows },
generated.len + 25 => &.{ sources.core_moe_route, comptime kernelOf("moe_gateup_2").source },
generated.len + 25, generated.len + 32 => &.{ sources.core_moe_route, comptime kernelOf("moe_gateup_2").source },
generated.len + 26 => &.{comptime kernelOf("moe_down_2").source},
else => &.{j.source},
};
Expand Down
Loading
Loading