Skip to content
Merged
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

- HIP: a routed prompt plan keeps the stacked shared expert whole up to the GEMM tile instead of cutting it into 8-row items, and on gfx1151 a prompt pass of 33 rows or more takes the GEMM tile for every expert; decode, lane rounds and the MTP draft head keep the 8-row tile, and outputs keep their bytes (#3521, fixes #3161). Thanks to @AbrahamCete.

- CUDA: the Windows Qwen3.5 diagnostic build compiles again, with named refusals where a POSIX stage or pipe is needed; 5/5 Windows GNU cross-build steps pass (#4112, refs #4105). Thanks to @AIFlowML.

- Tests: the serve-config fixtures no longer depend on a group-friendly umask, while the permission refusals stay; 39/39 host tests pass under umask 002 and 022 on Linux and macOS (#4115, fixes #4113). Thanks to @AIFlowML.
Expand Down
21 changes: 12 additions & 9 deletions zig/kernels/hip/ops/moe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

namespace {

// Stable-sorts the pairs by expert into members and cuts items of at most `tile` pairs; one block, counted per segment.
extern "C" __global__ void tf_moe_route(const int* picks, int pairs, int experts, int tile, int* members, int* items,
int capacity) {
// Stable-sorts the pairs by expert into members and cuts items of at most `tile` pairs, the `shared_expert`'s of at
// most `shared_tile` (-1: none); one block, counted per segment.
extern "C" __global__ void tf_moe_route(const int* picks, int pairs, int experts, int tile, int shared_expert,
int shared_tile, int* members, int* items, int capacity) {
const auto cut = [=](int e) { return e == shared_expert ? shared_tile : tile; };
extern __shared__ int shared[];
int* counts = shared;
int* starts = shared + experts;
Expand Down Expand Up @@ -42,7 +44,7 @@ extern "C" __global__ void tf_moe_route(const int* picks, int pairs, int experts
int own_c = 0, own_n = 0;
for (int e = lo; e < hi; ++e) {
own_c += counts[e];
own_n += (counts[e] + tile - 1) / tile;
own_n += (counts[e] + cut(e) - 1) / cut(e);
}
scan_c[t] = own_c;
scan_n[t] = own_n;
Expand All @@ -58,7 +60,7 @@ extern "C" __global__ void tf_moe_route(const int* picks, int pairs, int experts
for (int e = lo; e < hi; ++e) {
starts[e] = start;
start += counts[e];
end += (counts[e] + tile - 1) / tile;
end += (counts[e] + cut(e) - 1) / cut(e);
ends[e] = end;
}
__syncthreads();
Expand All @@ -77,12 +79,13 @@ extern "C" __global__ void tf_moe_route(const int* picks, int pairs, int experts
else hi = mid;
}
int e = lo < experts - 1 ? lo : experts - 1;
int reps = (counts[e] + tile - 1) / tile;
const int te = cut(e);
int reps = (counts[e] + te - 1) / te;
int within = slot - (ends[e] - reps);
int count = counts[e] - within * tile;
int count = counts[e] - within * te;
items[slot * 3] = e;
items[slot * 3 + 1] = starts[e] + within * tile;
items[slot * 3 + 2] = count < 0 ? 0 : (count > tile ? tile : count);
items[slot * 3 + 1] = starts[e] + within * te;
items[slot * 3 + 2] = count < 0 ? 0 : (count > te ? te : count);
}
}

Expand Down
18 changes: 15 additions & 3 deletions zig/src/core/contract/hip_records_router.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Gate 5's records of the router over the stored bf16 rows (decode.hip: tf_router_bf16).
//! Gate 5's records of the router over the stored bf16 rows (decode.hip: tf_router_bf16) and the route (ops.hip).
const std = @import("std");
const limits = @import("limits.zig");
const Record = @import("hip_records.zig").Record;
Expand All @@ -16,8 +16,19 @@ fn routeTile(comptime name: []const u8, comptime symbol: []const u8) Record {
};
}

/// The same launch in bf16, whose weights are the wider activation's rows.
pub const records = [2]Record{
/// The route plan's one block: the launcher's most LDS, 1,024 experts' counts, starts and ends, 16 segments, the scan.
const plan: Record = .{
.launch = .{ .name = "moe route tf_moe_route", .symbol = "tf_moe_route", .image = "ops", .threads = 256, .dynamic = 3 * 1024 * 4 + 16 * 1024 * 2 + 2 * 256 * 4 },
.usage = .{
.{ .vgprs = 63, .sgprs = 28, .lds = 0, .private = 0, .bound = 1024, .wave = 32 },
.{ .vgprs = 63, .sgprs = 28, .lds = 0, .private = 0, .bound = 1024, .wave = 32 },
.{ .vgprs = 63, .sgprs = 28, .lds = 0, .private = 0, .bound = 1024, .wave = 32 },
.{ .vgprs = 64, .sgprs = 27, .lds = 0, .private = 0, .bound = 1024, .wave = 32 },
},
};

/// The same launch in bf16, whose weights are the wider activation's rows; then the route plan the picks feed.
pub const records = [3]Record{
routeTile("router tf_router_bf16_f16", "tf_router_bf16_f16"),
.{
.launch = .{ .name = "router tf_router_bf16", .symbol = "tf_router_bf16", .image = "decode", .threads = 128 },
Expand All @@ -28,4 +39,5 @@ pub const records = [2]Record{
.{ .vgprs = 36, .sgprs = 65, .lds = 0, .private = 0, .bound = 128, .wave = 32 },
},
},
plan,
};
1 change: 1 addition & 0 deletions zig/src/hip/kernel_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ test {
_ = @import("kernel_tests/gdn_window.zig");
_ = @import("kernel_tests/mamba.zig");
_ = @import("kernel_tests/decode.zig");
_ = @import("kernel_tests/route.zig");
_ = @import("kernel_tests/tail_pin.zig");
_ = @import("kernel_tests/chain.zig");
_ = @import("kernel_tests/attention.zig");
Expand Down
2 changes: 1 addition & 1 deletion zig/src/hip/kernel_tests/affine/cells.zig
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub fn routedShape(c: Class, template: registry.Shape, tokens: usize) registry.S
s.items = @intCast(selectorCount(c, tokens));
s.plan_pairs = @intCast(tokens * c.slots);
s.expert_rows = moe.expertRows(tokens, routedSlots(c), c.experts);
s.m = @intCast(@min(moe.tileOf(tokens, false), tokens));
s.m = @intCast(@min(moe.itemRowsOf(tokens, false, true, c.shared, moe.card_prompt_from), tokens));
return s;
}

Expand Down
10 changes: 7 additions & 3 deletions zig/src/hip/kernel_tests/affine/product.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ pub const Product = struct {
fn planExperts(p: *Product, pairs: usize) !void {
const c = p.c;
// served: serving's tile, and the capacity it sizes the plan for (one token: an item a slot)
const tile = if (c.served) moe.tileOf(c.rows, false) else c.item_rows;
const tile = if (c.served) moe.tileOf(c.rows, false, c.path == .prefill, moe.card_prompt_from) else c.item_rows;
// served: the stacked shared expert (the last id) takes serving's own tile, whole below 64 rows in a prompt
const shared: ?u32 = if (c.served and c.shared) @intCast(c.experts) else null;
const shared_tile = if (c.served) moe.sharedTileOf(c.rows, false, c.path == .prefill) else tile;
const size = if (c.exact) c.rows else moe.bucket(c.rows);
const cap = if (c.served and c.rows > 1) moe.capacity(size * c.slots, c.experts + @intFromBool(c.shared)) else 0;
const order = try gpa.alloc(i32, pairs);
Expand All @@ -144,7 +147,8 @@ pub const Product = struct {
while (i < pairs) {
const e = p.expert_of[@intCast(order[i])];
var j = i;
while (j < pairs and j - i < tile and p.expert_of[@intCast(order[j])] == e) j += 1;
const cut = if (shared != null and shared.? == e) shared_tile else tile;
while (j < pairs and j - i < cut and p.expert_of[@intCast(order[j])] == e) j += 1;
items[count * 3 ..][0..3].* = .{ @intCast(e), @intCast(i), @intCast(j - i) };
p.max_rows = @max(p.max_rows, j - i);
count += 1;
Expand All @@ -154,7 +158,7 @@ pub const Product = struct {
std.debug.assert(cap == 0 or count <= cap);
std.debug.assert(!slot_order or count == pairs);
p.items = @max(count, cap);
if (c.served) p.max_rows = @min(tile, c.rows);
if (c.served) p.max_rows = @min(moe.itemRowsOf(c.rows, false, c.path == .prefill, shared != null, moe.card_prompt_from), c.rows);
p.plan = try Buffer.fromHost(p.rig.base, items[0 .. p.items * 3]);
errdefer {
p.plan.?.free();
Expand Down
60 changes: 0 additions & 60 deletions zig/src/hip/kernel_tests/ops.zig
Original file line number Diff line number Diff line change
Expand Up @@ -208,66 +208,6 @@ test "argmax and top-k follow torch's order: the larger value, then the lower in
try std.testing.expectEqual(@as(i32, -1), gi[2 * stride]);
}

test "the MoE route sorts pairs by expert, stably, into items of at most a tile" {
const t = try Rig.open(1 << 20);
defer t.close();
const o = t.ops(&t.on);
var rng: rig.Rng = .{ .state = 0x2017E };
for ([_][3]usize{
.{ 257, 512, 16 },
.{ 128, 37, 4 },
.{ 64, 1000, 64 },
.{ 513, 11, 8 },
.{ 513, 2816, 8 },
}) |shape| {
const experts = shape[0];
const pairs = shape[1];
const tile = shape[2];
const picks = try gpa.alloc(i32, pairs);
defer gpa.free(picks);
// a skewed pick so some experts fill several tiles and others none
for (picks) |*p| p.* = @intCast(@min(experts - 1, (rng.next() >> 40) % 16 * (rng.next() >> 50) % experts));
const counts = try gpa.alloc(usize, experts);
defer gpa.free(counts);
@memset(counts, 0);
for (picks) |p| counts[@intCast(p)] += 1;
var capacity: usize = 0;
for (counts) |c| capacity += (c + tile - 1) / tile;
capacity += 3;
var dev_picks = try t.upload(picks);
defer dev_picks.free();
var members = try t.alloc(pairs * 4);
defer members.free();
var items = try t.alloc(capacity * 12);
defer items.free();
try o.moeRoute(at(dev_picks), pairs, experts, tile, at(members), at(items), capacity);
try t.stream.synchronize();
const gm = try rig.download(i32, members);
defer gpa.free(gm);
const gi = try rig.download(i32, items);
defer gpa.free(gi);
var place: usize = 0;
var slot: usize = 0;
for (0..experts) |e| {
const start = place;
for (picks, 0..) |p, i| if (p == e) {
try std.testing.expectEqual(@as(i32, @intCast(i)), gm[place]);
place += 1;
};
var left = counts[e];
var first = start;
while (left > 0) : (slot += 1) {
const take = @min(left, tile);
try std.testing.expectEqualSlices(i32, &.{ @intCast(e), @intCast(first), @intCast(take) }, gi[slot * 3 ..][0..3]);
first += take;
left -= take;
}
}
// the slots past the plan are empty items
for (slot..capacity) |s| try std.testing.expectEqual(@as(i32, 0), gi[s * 3 + 2]);
}
}

fn silu(x: f64) f64 {
return x / (1 + @exp(-x));
}
Expand Down
177 changes: 177 additions & 0 deletions zig/src/hip/kernel_tests/route.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
//! tf_moe_route's plans: each expert's pairs cut into items of its tile, the stacked shared expert's of its own.
const std = @import("std");
const rig = @import("rig.zig");
const Rig = rig.Rig;
const gpa = rig.gpa;
const at = rig.at;
const moe_plan = @import("../moe.zig");

/// tf_moe_route's plan of `picks`, checked: pairs in pick order, items of at most each expert's tile, then empty.
fn expectRoute(t: anytype, o: anytype, picks: []const i32, experts: usize, tile: usize, shared: ?usize, shared_tile: usize, capacity: usize) ![]i32 {
const pairs = picks.len;
var dev_picks = try t.upload(picks);
defer dev_picks.free();
var members = try t.alloc(pairs * 4);
defer members.free();
var items = try t.alloc(capacity * 12);
defer items.free();
try o.moeRoute(at(dev_picks), pairs, experts, tile, shared, shared_tile, at(members), at(items), capacity);
try t.stream.synchronize();
const gm = try rig.download(i32, members);
defer gpa.free(gm);
const gi = try rig.download(i32, items);
errdefer gpa.free(gi);
var place: usize = 0;
var slot: usize = 0;
for (0..experts) |e| {
const start = place;
var left: usize = 0;
for (picks, 0..) |p, i| if (p == e) {
try std.testing.expectEqual(@as(i32, @intCast(i)), gm[place]);
place += 1;
left += 1;
};
const cut = if (shared != null and shared.? == e) shared_tile else tile;
var first = start;
while (left > 0) : (slot += 1) {
const take = @min(left, cut);
try std.testing.expectEqualSlices(i32, &.{ @intCast(e), @intCast(first), @intCast(take) }, gi[slot * 3 ..][0..3]);
first += take;
left -= take;
}
}
// the slots past the plan are empty items
try std.testing.expect(slot <= capacity);
for (slot..capacity) |s| try std.testing.expectEqual(@as(i32, 0), gi[s * 3 + 2]);
return gi;
}

test "the MoE route sorts pairs by expert, stably, into items of at most a tile, the shared expert's of its own" {
const t = try Rig.open(1 << 20);
defer t.close();
const o = t.ops(&t.on);
var rng: rig.Rng = .{ .state = 0x2017E };
const none = std.math.maxInt(usize);
// experts, pairs, tile, the shared expert (`none`: every expert takes the tile) and its tile
for ([_][5]usize{
.{ 257, 512, 16, none, 0 },
.{ 128, 37, 4, none, 0 },
.{ 64, 1000, 64, none, 0 },
.{ 513, 11, 8, none, 0 },
.{ 513, 2816, 8, none, 0 },
// expert 0 fills most: a shared tile above and below the routed one; the last id, where empty slots clamp
.{ 257, 512, 8, 0, 128 },
.{ 513, 2816, 32, 0, 16 },
.{ 257, 512, 8, 256, 128 },
}) |shape| {
const experts = shape[0];
const pairs = shape[1];
const tile = shape[2];
const shared: ?usize = if (shape[3] == none) null else shape[3];
const shared_tile = if (shared == null) tile else shape[4];
const picks = try gpa.alloc(i32, pairs);
defer gpa.free(picks);
// a skewed pick so some experts fill several tiles and others none
for (picks) |*p| p.* = @intCast(@min(experts - 1, (rng.next() >> 40) % 16 * (rng.next() >> 50) % experts));
const counts = try gpa.alloc(usize, experts);
defer gpa.free(counts);
@memset(counts, 0);
for (picks) |p| counts[@intCast(p)] += 1;
var capacity: usize = 0;
for (counts, 0..) |c, e| {
const cut = if (shared != null and shared.? == e) shared_tile else tile;
capacity += (c + cut - 1) / cut;
}
capacity += 3;
gpa.free(try expectRoute(t, o, picks, experts, tile, shared, shared_tile, capacity));
}
}

/// Each row's top-k distinct routed experts, half from six hot ones, then the shared one, as tf_moe_select writes them.
fn drawPicks(rng: *rig.Rng, picks: []i32, routed: usize, top_k: usize) void {
const slots = top_k + 1;
for (0..picks.len / slots) |r| {
const row = picks[r * slots ..][0..slots];
for (row[0..top_k], 0..) |*e, k| {
const pool: u64 = if (k < top_k / 2) 6 else routed;
while (true) {
e.* = @intCast(rng.next() % pool);
if (std.mem.indexOfScalar(i32, row[0..k], e.*) == null) break;
}
}
row[top_k] = @intCast(routed);
}
}

/// The most rows an item of the plan holds, and how many items the routed experts and the shared one fill.
const Items = struct { most: usize = 0, routed: usize = 0, shared: usize = 0 };

fn itemsOf(gi: []const i32, capacity: usize, shared: usize) Items {
var n: Items = .{};
for (0..capacity) |s| {
const count: usize = @intCast(@max(gi[s * 3 + 2], 0));
if (count == 0) continue;
n.most = @max(n.most, count);
if (gi[s * 3] == shared) n.shared += 1 else n.routed += 1;
}
return n;
}

/// The items the routed experts of `picks` fill at `tile` pairs an item.
fn cutsAt(picks: []const i32, routed: usize, tile: usize) usize {
var n: usize = 0;
for (0..routed) |e| n += (std.mem.count(i32, picks, &.{@intCast(e)}) + tile - 1) / tile;
return n;
}

test "a 33 to 63 row prompt plan: every expert whole where the GEMM tile starts at 33, routed 8-row items at 64" {
const t = try Rig.open(1 << 20);
defer t.close();
var o = t.ops(&t.on);
var rng: rig.Rng = .{ .state = 0x3161 };
const gemm_rows = 128;
// routed experts, top-k and the prompt's rows: the 35B-A3B's 256 below 64 rows and past, Flash Next's 512
for ([_][3]usize{ .{ 256, 8, 33 }, .{ 256, 8, 40 }, .{ 256, 8, 56 }, .{ 256, 8, 63 }, .{ 256, 8, 64 }, .{ 256, 8, 130 }, .{ 512, 10, 41 } }) |shape| {
const routed = shape[0];
const top_k = shape[1];
const rows = shape[2];
const experts = routed + 1;
const slots = top_k + 1;
const picks = try gpa.alloc(i32, rows * slots);
defer gpa.free(picks);
drawPicks(&rng, picks, routed, top_k);
// the hot experts hold more than 8 pairs, so the two starts plan differently below 64 rows
const used = cutsAt(picks, routed, rows);
try std.testing.expect(cutsAt(picks, routed, 8) > used);
const capacity = moe_plan.capacity(moe_plan.bucket(rows) * slots, experts);
// a prompt pass on a card that starts the GEMM tile at 33 (gfx1151) and on one left at 64
o.prefill = true;
for ([_]usize{ 33, 64 }) |from| {
const tile = moe_plan.tileOf(rows, false, true, from);
try std.testing.expectEqual(@as(usize, if (rows < from) 8 else gemm_rows), tile);
if (from == moe_plan.card_prompt_from) try std.testing.expectEqual(tile, moe_plan.tileFor(o, rows));
const gi = try expectRoute(t, o, picks, experts, tile, routed, moe_plan.sharedTileFor(o, rows), capacity);
defer gpa.free(gi);
const n = itemsOf(gi, capacity, routed);
try std.testing.expectEqual(cutsAt(picks, routed, tile), n.routed);
// from the start up to 127 rows every routed expert is one item; the shared one is whole on both cards
if (rows >= from and rows < gemm_rows) try std.testing.expectEqual(used, n.routed);
try std.testing.expectEqual((rows + gemm_rows - 1) / gemm_rows, n.shared);
// the launches' m is the most rows an item holds
try std.testing.expectEqual(n.most, @min(moe_plan.itemRowsOf(rows, false, true, true, from), rows));
}
// decode and the MTP draft head (neither switch) and a lane round keep the 8-row tile below 64, shared too
o.prefill = false;
for ([_]bool{ false, true }) |window| {
o.window = window;
const tile = moe_plan.tileFor(o, rows);
try std.testing.expectEqual(@as(usize, if (rows < 64 or window) 8 else gemm_rows), tile);
try std.testing.expectEqual(tile, moe_plan.sharedTileFor(o, rows));
const gi = try expectRoute(t, o, picks, experts, tile, routed, tile, capacity);
defer gpa.free(gi);
const n = itemsOf(gi, capacity, routed);
try std.testing.expectEqual(n.most, @min(moe_plan.itemRowsFor(o, rows, true), rows));
}
o.window = false;
}
}
Loading
Loading