Skip to content
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- GLM-5.3-Flash on CUDA: EXL3 `mcg` routed experts load at any whole-bit width from 1 to 8, mixed by projection within a layer, where only 4-bit was admitted, and the prompt expert kernels gain a 3-bit form; the 4-bit path is unchanged (#2094, refs #2093). Thanks to @MiaAI-Lab.



- Qwen3.8-27B on Metal: a lone sampled stream's DFlash2 draft tree ranks its candidates with the stream's own keyed draw, so the drafts follow what the target samples; the target still decides every token, and greedy and multi-stream rounds are unchanged (#3978). Thanks to @hobbesd.
Expand Down
1 change: 1 addition & 0 deletions zig/build/glm_family_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub fn steps(b: *std.Build, cuda: *std.Build.Module, glm: *std.Build.Module, mod
b.step("test-glm-thin-weights-host", "TF_GLM_LAYERS's admission bytes by tensor from synthetic shard headers").dependOn(&thin_run.step);
step.dependOn(&thin_run.step);
b.step("test-glm-tp3-host", "Uneven KDA head shares through actual projection and load planning, without devices").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = glm, .filters = &.{"GLM TP3:"} })).step);
b.step("test-glm-exl3-host", "GLM EXL3 admission, mixed-width expert loading and scratch sizing, without devices").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = glm, .filters = &.{"GLM EXL3:"} })).step);
// TF_GLM_INDEX_CUDA: the launch rules, and (its own slower step) glm_index_*.cuh as host C++ against plain models
const index_run = b.addRunArtifact(b.addTest(.{ .root_module = glm, .filters = &.{"GLM index:"} }));
b.step("test-glm-index-host", "GLM's prompt indexer launch rules without devices").dependOn(&index_run.step);
Expand Down
60 changes: 55 additions & 5 deletions zig/exl3_prompt_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const Capture = struct {
3 => &.{ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 },
5 => &.{ 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4 },
11 => &.{ 8, 4, 8, 8, 4 },
12 => &.{ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4 },
13 => &.{ 8, 8, 8, 8, 8, 8, 8, 4, 4, 4 },
12, 14 => &.{ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4 },
13, 15 => &.{ 8, 8, 8, 8, 8, 8, 8, 4, 4, 4 },
else => &.{},
};
for (sizes, 0..) |size, j| {
Expand Down Expand Up @@ -57,7 +57,7 @@ fn run(rows: usize, prompt: bool, shared: bool) !void {
d.api.cuMemcpy2DAsync_v2 = Capture.copy;
const s: cuda.Stream = .{ .d = &d, .handle = null };
var k: ex.Experts = .{ .mod = undefined, .group = function(&d, 1), .rot_in = function(&d, 2), .grouped = @splat(function(&d, 3)), .gateup = function(&d, 4), .down_combine = function(&d, 5) };
if (comptime @hasField(ex.Experts, "prompt")) k.prompt = .{ .rotate = function(&d, 11), .gateup = function(&d, 12), .down = function(&d, 13) };
if (comptime @hasField(ex.Experts, "prompt")) k.prompt = .{ .rotate = function(&d, 11), .gateup = function(&d, 12), .down = function(&d, 13), .gateup_k6 = function(&d, 14), .down_k6 = function(&d, 15) };
const ek: glm.experts.Kernels = .{ .nvfp4 = undefined, .b32 = null, .router = .{ .plan_small = function(&d, 20), .plan_rank = function(&d, 21), .plan_offsets = function(&d, 22), .plan_scatter = function(&d, 23), .plan_rows = function(&d, 24), .plan_rank_rows = function(&d, 25), .plan_scatter_rows = function(&d, 26) } };
const w: glm.exl3.Experts = .{ .gt = 100, .ut = 200, .dt = 300, .k2 = 400, .suh_g = 500, .suh_u = 600, .svh_g = 700, .svh_u = 800, .suh_d = 900, .svh_d = 1000, .shx = shared };
const sc: glm.exl3.Scratch = .{ .picks = 0x100000000, .xg = 0, .xu = 0, .z = 0 };
Expand Down Expand Up @@ -167,14 +167,26 @@ test "exl3 prompt: decode uses item plans and keeps slot and input offsets acros
}
}

test "exl3 prompt: the common dispatch requires verified mcg4 geometry and keeps every fallback" {
test "exl3 prompt: the common dispatch requires verified mcg3 or mcg4 geometry and keeps every fallback" {
const P = ex.prompt;
var l: ex.Layer = .{ .gate = 0, .up = 0, .down = 0, .gate_k2 = 0, .up_k2 = 0, .down_k2 = 0, .suh_g = 0, .suh_u = 0, .svh_g = 0, .svh_u = 0, .suh_d = 0, .svh_d = 0, .dims = 512, .width = 256, .experts = 9, .codebook = .mcg, .uniform_k2 = 8 };
try std.testing.expect(P.eligible(l));
l.uniform_k2 = 0;
try std.testing.expect(!P.eligible(l));
l.uniform_k2 = 6;
try std.testing.expect(!P.eligible(l));
try std.testing.expect(P.eligible(l));
for ([_]u32{ 4, 5, 7, 10 }) |k2| {
l.uniform_k2 = k2;
try std.testing.expect(!P.eligible(l));
}
l.uniform_k2 = 0;
for ([_][2]u32{ .{ 6, 0 }, .{ 0, 8 }, .{ 4, 8 }, .{ 6, 10 } }) |w| {
l.uniform_k2_gu = w[0];
l.uniform_k2_d = w[1];
try std.testing.expect(!P.eligible(l));
}
l.uniform_k2_gu = 0;
l.uniform_k2_d = 0;
l.uniform_k2 = 8;
for ([_]ex.Codebook{ .inst3, .mul1 }) |cb| {
l.codebook = cb;
Expand Down Expand Up @@ -250,3 +262,41 @@ test "exl3 prompt: GLM admission facts through shared lanes keep drafted plain a
for ([_]bool{ false, true }) |drafts| for ([_]usize{ 0, 1, 3, 7 }) |others| try std.testing.expectEqual(plain, try reply(drafts, others, seed));
}
}

test "exl3 prompt: each projection group launches the kernel of its own verified width, with the same arguments" {
var d: cuda.Driver = undefined;
d.api.cuLaunchKernel = Capture.launch;
const s: cuda.Stream = .{ .d = &d, .handle = null };
var k: ex.Experts = .{ .mod = undefined, .group = function(&d, 1), .rot_in = function(&d, 2), .grouped = @splat(function(&d, 3)), .gateup = function(&d, 4), .down_combine = function(&d, 5) };
k.prompt = .{ .rotate = function(&d, 11), .gateup = function(&d, 12), .down = function(&d, 13), .gateup_k6 = function(&d, 14), .down_k6 = function(&d, 15) };
const router: cuda.grouped.Router = .{ .plan_small = function(&d, 20), .plan_rank = function(&d, 21), .plan_offsets = function(&d, 22), .plan_scatter = function(&d, 23), .plan_rows = function(&d, 24), .plan_rank_rows = function(&d, 25), .plan_scatter_rows = function(&d, 26) };
const plan: cuda.grouped.Plan = .{ .items = 10000, .members = 20000, .counts = 30000, .rank = 40000, .hist = 50000 };
var l: ex.Layer = .{ .gate = 100, .up = 200, .down = 300, .gate_k2 = 400, .up_k2 = 400, .down_k2 = 400, .suh_g = 500, .suh_u = 600, .svh_g = 700, .svh_u = 800, .suh_d = 900, .svh_d = 1000, .dims = 512, .width = 256, .experts = 9, .codebook = .mcg, .limit = 10, .shared_input = true };
var want: [2]Capture.Call = undefined; // the 4-bit gate/up and down launches
for ([_][3]u32{ .{ 8, 0, 0 }, .{ 0, 6, 8 }, .{ 0, 6, 6 }, .{ 0, 8, 6 }, .{ 6, 0, 0 }, .{ 8, 8, 8 }, .{ 6, 6, 0 } }) |w| {
l.uniform_k2 = w[0];
l.uniform_k2_gu = w[1];
l.uniform_k2_d = w[2];
const sc = try ex.prompt.scratch(0x1000, l, 40, 8);
Capture.reset();
try k.prompt.run(&k, router, s, l, 0x200000000, 520, 0x300000000, 40, sc, plan, 0x400000000, 9);
const gu: usize = if ((if (w[1] != 0) w[1] else w[0]) == 6) 14 else 12;
const dn: usize = if ((if (w[2] != 0) w[2] else w[0]) == 6) 15 else 13;
try std.testing.expectEqual(@as(usize, 1), Capture.by(gu).len);
try std.testing.expectEqual(@as(usize, 1), Capture.by(dn).len);
try std.testing.expectEqual(@as(usize, 1), Capture.by(11).len);
for ([_]usize{ 12, 13, 14, 15 }) |id| if (id != gu and id != dn) try std.testing.expectEqual(@as(usize, 0), Capture.by(id).len);
const got = [2]Capture.Call{ Capture.by(gu)[0], Capture.by(dn)[0] };
if (w[0] == 8 and w[1] == 0) want = got;
for (got, want) |g, x| {
try std.testing.expectEqual(x.grid, g.grid);
try std.testing.expectEqual(x.block, g.block);
try std.testing.expectEqual(x.args, g.args);
}
}
l.uniform_k2 = 8;
l.uniform_k2_gu = 6; // disagrees with every projection's 8: not verified, so no prompt launch
l.uniform_k2_d = 0;
try std.testing.expect(!ex.prompt.eligible(l));
try std.testing.expectError(error.Exl3Shape, ex.prompt.scratch(0x1000, l, 40, 8));
}
21 changes: 16 additions & 5 deletions zig/glm_exl3_prompt_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn run(rows: usize, prompt: bool, shared: bool, count: usize) !void {
var k: glm.exl3.Kernels = undefined;
inline for (.{ "group", "rot_in", "gateup", "down_combine" }, .{ 9, 5, 13, 14 }) |field, id| @field(k, field) = Record.function(&d, id);
k.grouped = .{ Record.function(&d, 10), Record.function(&d, 11), Record.function(&d, 12) };
k.prompt = .{ .rotate = Record.function(&d, 6), .gateup = Record.function(&d, 7), .down = Record.function(&d, 8) };
k.prompt = .{ .rotate = Record.function(&d, 6), .gateup = Record.function(&d, 7), .down = Record.function(&d, 8), .gateup_k6 = Record.function(&d, 21), .down_k6 = Record.function(&d, 22) };
var ek: glm.experts.Kernels = undefined;
inline for (.{ "plan_small", "plan_rows", "plan_rank", "plan_offsets", "plan_scatter", "plan_rank_rows", "plan_scatter_rows" }, .{ 1, 18, 2, 3, 4, 19, 20 }) |field, id| @field(ek.router, field) = Record.function(&d, id);
const w = glm.exl3.Experts{ .gt = 101, .ut = 102, .dt = 103, .k2 = 104, .suh_g = 105, .suh_u = 106, .svh_g = 107, .svh_u = 108, .suh_d = 109, .svh_d = 110, .shx = shared };
Expand Down Expand Up @@ -169,12 +169,23 @@ fn guarded(text: []const u8, name: []const u8, payload: []const u8) bool {
return guard < @min(load, member);
}

/// Whether kernel `name`'s whole body is the call `call` (the guarded template at one width).
fn forwards(text: []const u8, name: []const u8, call: []const u8) bool {
const at = std.mem.indexOf(u8, text, name) orelse return false;
const tail = text[at..];
const open = std.mem.indexOf(u8, tail, "{\n") orelse return false;
const end = std.mem.indexOf(u8, tail, "\n}") orelse return false;
return open < end and std.mem.startsWith(u8, std.mem.trim(u8, tail[open + 2 .. end], " "), call) and std.mem.count(u8, tail[open + 2 .. end], ";") == 1;
}

test "GLM EXL3 prompt: shared-id guards precede payload loads and late-guard controls are caught" {
try t.expect(guarded(source, "tf_exl3_prompt_gu(", "tgateup<"));
try t.expect(guarded(source, "tf_exl3_prompt_down(", "members[first"));
try t.expect(guarded(source, "void prompt_gu(", "tgateup<"));
try t.expect(guarded(source, "void prompt_down(", "members[first"));
for ([_][2][]const u8{ .{ "tf_exl3_prompt_gu(", "prompt_gu<8>(" }, .{ "tf_exl3_prompt_gu_k6(", "prompt_gu<6>(" }, .{ "tf_exl3_prompt_down(", "prompt_down<8>(" }, .{ "tf_exl3_prompt_down_k6(", "prompt_down<6>(" } }) |k| try t.expect(forwards(source, k[0], k[1]));
try t.expect(!forwards(source, "tf_exl3_prompt_gu(", "prompt_gu<6>("));
const bad = try std.mem.replaceOwned(u8, t.allocator, source, "if (e == skip) return;", "const volatile int control_member = members[items[3 * item + 1]];\n if (e == skip) return;");
defer t.allocator.free(bad);
try t.expectEqual(@as(usize, 2), std.mem.count(u8, bad, "if (e == skip) return;"));
try t.expect(!guarded(bad, "tf_exl3_prompt_gu(", "tgateup<"));
try t.expect(!guarded(bad, "tf_exl3_prompt_down(", "members[first"));
try t.expect(!guarded(bad, "void prompt_gu(", "tgateup<"));
try t.expect(!guarded(bad, "void prompt_down(", "members[first"));
}
45 changes: 38 additions & 7 deletions zig/kernels/cuda/exl3_experts.cu
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ extern "C" __global__ void tf_exl3_prompt_rotate(const __nv_bfloat16* __restrict
for (int j = 0; j < 4; ++j) o[j] = __float2half_rn(v[j] * HAD_SCALE);
}

extern "C" __global__ void __launch_bounds__(256, 2) tf_exl3_prompt_gu(const half* __restrict__ Xg, const half* __restrict__ Xu,
// Gate and up of one width K2 (8: 4 bits, 6: 3 bits), the shared expert skipped before any payload read.
template <int K2>
__device__ __forceinline__ void prompt_gu(const half* __restrict__ Xg, const half* __restrict__ Xu,
const uint64_t* __restrict__ Tg, const uint64_t* __restrict__ Tu, const int* __restrict__ items, const int* __restrict__ counts,
const int* __restrict__ members, const half* __restrict__ svh_g, const half* __restrict__ svh_u, const half* __restrict__ suh_d,
half* __restrict__ xd, int K, int N, int xdiv, float limit, int act_mode, int skip) {
Expand All @@ -292,18 +294,34 @@ extern "C" __global__ void __launch_bounds__(256, 2) tf_exl3_prompt_gu(const hal
const int e = items[3 * item];
if (e == skip) return;

__shared__ __align__(16) unsigned char smem_raw[tsmem_bytes()]; // one buffer for either instantiation
__shared__ __align__(16) unsigned char smem_raw[tsmem_bytes<K2>()]; // one buffer for either instantiation
__shared__ int rows_sh[TPM];
if (xdiv > 1) tgateup<TPM>(Xg, Xu, Tg, Tu, items, counts, members, svh_g, svh_u, suh_d, xd, K, N, xdiv, limit, act_mode, smem_raw, rows_sh);
else tgateup<2 * TPM>(Xg, Xu, Tg, Tu, items, counts, members, svh_g, svh_u, suh_d, xd, K, N, 1, limit, act_mode, smem_raw, rows_sh);
if (xdiv > 1) tgateup<TPM, K2>(Xg, Xu, Tg, Tu, items, counts, members, svh_g, svh_u, suh_d, xd, K, N, xdiv, limit, act_mode, smem_raw, rows_sh);
else tgateup<2 * TPM, K2>(Xg, Xu, Tg, Tu, items, counts, members, svh_g, svh_u, suh_d, xd, K, N, 1, limit, act_mode, smem_raw, rows_sh);
}

extern "C" __global__ void __launch_bounds__(256, 2) tf_exl3_prompt_gu(const half* __restrict__ Xg, const half* __restrict__ Xu,
const uint64_t* __restrict__ Tg, const uint64_t* __restrict__ Tu, const int* __restrict__ items, const int* __restrict__ counts,
const int* __restrict__ members, const half* __restrict__ svh_g, const half* __restrict__ svh_u, const half* __restrict__ suh_d,
half* __restrict__ xd, int K, int N, int xdiv, float limit, int act_mode, int skip) {
prompt_gu<8>(Xg, Xu, Tg, Tu, items, counts, members, svh_g, svh_u, suh_d, xd, K, N, xdiv, limit, act_mode, skip);
}

// The same at 3 bits: gate and up both 3-bit mcg (tile_words<6>: 96-byte tiles), the arguments tf_exl3_prompt_gu's.
extern "C" __global__ void __launch_bounds__(256, 2) tf_exl3_prompt_gu_k6(const half* __restrict__ Xg, const half* __restrict__ Xu,
const uint64_t* __restrict__ Tg, const uint64_t* __restrict__ Tu, const int* __restrict__ items, const int* __restrict__ counts,
const int* __restrict__ members, const half* __restrict__ svh_g, const half* __restrict__ svh_u, const half* __restrict__ suh_d,
half* __restrict__ xd, int K, int N, int xdiv, float limit, int act_mode, int skip) {
prompt_gu<6>(Xg, Xu, Tg, Tu, items, counts, members, svh_g, svh_u, suh_d, xd, K, N, xdiv, limit, act_mode, skip);
}

// Block (256 model-width columns, item): y[row][slot][:] = the down epilogue of the item's pairs (this rank's share).
extern "C" __global__ void __launch_bounds__(256, 2) tf_exl3_prompt_down(const half* __restrict__ Xd, const uint64_t* __restrict__ Td,
template <int K2>
__device__ __forceinline__ void prompt_down(const half* __restrict__ Xd, const uint64_t* __restrict__ Td,
const int* __restrict__ items, const int* __restrict__ counts, const int* __restrict__ members, const half* __restrict__ svh_d,
float* __restrict__ y, int K, int D, int skip) {

__shared__ __align__(16) unsigned char smem_raw[tsmem_bytes()];
__shared__ __align__(16) unsigned char smem_raw[tsmem_bytes<K2>()];
__shared__ int rows_sh[TPM];
const int cb = blockIdx.x, item = blockIdx.y;
if (item >= counts[0]) return;
Expand All @@ -314,7 +332,7 @@ extern "C" __global__ void __launch_bounds__(256, 2) tf_exl3_prompt_down(const h
__syncthreads();
const int warp = threadIdx.x >> 5, lane = threadIdx.x & 31;
float acc[TMT][TNB][4];
tmain<1, TPM>(Xd, Xd, Td, Td, rows_sh, cnt, e, K, D >> 4, cb, 1, reinterpret_cast<half*>(smem_raw), acc);
tmain<1, TPM, K2>(Xd, Xd, Td, Td, rows_sh, cnt, e, K, D >> 4, cb, 1, reinterpret_cast<half*>(smem_raw), acc);
float* Ed = reinterpret_cast<float*>(smem_raw);
for (int hf = 0; hf < 2 && hf * TPH < cnt; ++hf) {
if (hf) __syncthreads();
Expand All @@ -336,3 +354,16 @@ extern "C" __global__ void __launch_bounds__(256, 2) tf_exl3_prompt_down(const h
}
}
}

extern "C" __global__ void __launch_bounds__(256, 2) tf_exl3_prompt_down(const half* __restrict__ Xd, const uint64_t* __restrict__ Td,
const int* __restrict__ items, const int* __restrict__ counts, const int* __restrict__ members, const half* __restrict__ svh_d,
float* __restrict__ y, int K, int D, int skip) {
prompt_down<8>(Xd, Td, items, counts, members, svh_d, y, K, D, skip);
}

// The same at 3 bits (3-bit mcg down), the arguments tf_exl3_prompt_down's.
extern "C" __global__ void __launch_bounds__(256, 2) tf_exl3_prompt_down_k6(const half* __restrict__ Xd, const uint64_t* __restrict__ Td,
const int* __restrict__ items, const int* __restrict__ counts, const int* __restrict__ members, const half* __restrict__ svh_d,
float* __restrict__ y, int K, int D, int skip) {
prompt_down<6>(Xd, Td, items, counts, members, svh_d, y, K, D, skip);
}
Loading
Loading