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
114 changes: 34 additions & 80 deletions zig/kernels/metal/glm_ep.metal
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,14 @@ kernel void ep_localize(const device int* PICK [[buffer(0)]], const device int*
if (theirs) THEIRS[o2 + b2] = i;
if (t == 0) { LCOUNT[0] = n0; COUNTS[0] = n1; COUNTS[1] = n2; atomic_store_explicit(CNT, uint(n1), memory_order_relaxed); }
}
// Exchange x writes its send slot once the peer's x - 1 landed (posted after our x - 2 reached it); give-ups counted
inline bool ep_slot_free(device atomic_uint* flag, uint x, device atomic_uint* gave_up, uint t, threadgroup uint* ok) {
if (t == 0) {
uint polls = 0;
*ok = 1u;
while (int(atomic_load_explicit(flag, memory_order_relaxed) - (x - 1u)) < 0) {
if (++polls > 400000000u) { atomic_fetch_add_explicit(gave_up, 1u, memory_order_relaxed); *ok = 0u; break; }
}
if (atomic_load_explicit(gave_up, memory_order_relaxed) != 0u) *ok = 0u;
}
threadgroup_barrier(mem_flags::mem_device | mem_flags::mem_threadgroup);
return *ok != 0u;
}
// This Mac's picks' outputs (Y [picks, 4096] bf16) packed in MINE order for the peer; a threadgroup a pick
kernel void ep_pack(const device uint* Y [[buffer(0)]], const device int* MINE [[buffer(1)]],
const device int* COUNTS [[buffer(2)]], device uint* OUT [[buffer(3)]], device atomic_uint* flag [[buffer(4)]],
constant uint& x [[buffer(5)]], device atomic_uint* gave_up [[buffer(6)]], uint3 tg [[threadgroup_position_in_grid]],
const device int* COUNTS [[buffer(2)]], device uint* OUT [[buffer(3)]],
const device ulong* previous [[buffer(4)]], constant ulong& expected [[buffer(5)]], uint3 tg [[threadgroup_position_in_grid]],
uint3 tpos [[thread_position_in_threadgroup]]) {
if (previous[0] != expected) return;
const int i = int(tg.y);
const uint t = tpos.x;
threadgroup uint ok;
if (!ep_slot_free(flag, x, gave_up, t, &ok)) return;
if (i >= COUNTS[0]) return;
const device uint* src = Y + size_t(MINE[i]) * WORDS;
device uint* dst = OUT + size_t(i) * WORDS;
Expand All @@ -62,10 +48,9 @@ inline float ep_mul_add(float acc, float a, float b) { return a * b + acc; }
#pragma clang fp contract(on)
kernel void ep_rcombine(const device float* YP [[buffer(0)]], const device float* WTS [[buffer(1)]],
constant int& rows [[buffer(2)]], device float* OUT [[buffer(3)]], device atomic_uint* CNT [[buffer(4)]],
device atomic_uint* flag [[buffer(5)]], constant uint& x [[buffer(6)]], device atomic_uint* gave_up [[buffer(7)]],
uint gid [[thread_position_in_grid]], uint t [[thread_index_in_threadgroup]]) {
threadgroup uint ok;
if (!ep_slot_free(flag, x, gave_up, t, &ok)) return;
const device ulong* previous [[buffer(5)]], constant ulong& expected [[buffer(6)]],
uint gid [[thread_position_in_grid]]) {
if (previous[0] != expected) return;
const int r = int(gid) / 4096, d = int(gid) % 4096;
if (gid == 0) atomic_store_explicit(CNT, uint(rows * 2), memory_order_relaxed);
if (r >= rows) return;
Expand All @@ -74,84 +59,56 @@ kernel void ep_rcombine(const device float* YP [[buffer(0)]], const device float
for (int k = 1; k < TOPK; k++) acc = ep_mul_add(acc, WTS[r * TOPK + k], y[size_t(k) * 4096]);
OUT[size_t(r) * 4096 + d] = acc;
}
// By rows: once the peer's sums land, each branch is both Macs' sums in rank order, rounded, plus the shared expert
kernel void ep_rfinal(device atomic_uint* flag [[buffer(0)]], constant uint& x [[buffer(1)]],
device atomic_uint* gave_up [[buffer(2)]], const device float* MINE [[buffer(3)]], device atomic_uint* PEER [[buffer(4)]],
// By rows: both sums in rank order, rounded, plus the shared expert. A missing epoch keeps the receive slot unread.
kernel void ep_rfinal(const device ulong* ready [[buffer(0)]], constant ulong& x [[buffer(1)]],
const device float* MINE [[buffer(3)]], const device uint* PEER [[buffer(4)]],
constant uint& rank [[buffer(5)]], const device bfloat* YS [[buffer(6)]], device bfloat* OUT [[buffer(7)]],
constant int& rows [[buffer(8)]], uint3 tg [[threadgroup_position_in_grid]], uint3 tpos [[thread_position_in_threadgroup]]) {
const uint t = tpos.x;
if (t == 0) {
uint polls = 0;
while (int(atomic_load_explicit(flag, memory_order_relaxed) - x) < 0) {
if (++polls > 400000000u) { atomic_fetch_add_explicit(gave_up, 1u, memory_order_relaxed); break; }
}
}
threadgroup_barrier(mem_flags::mem_device);
const int i = int(tg.x) * 256 + int(t);
if (i >= rows * 4096) return;
const float mine = MINE[i], peer = as_type<float>(atomic_load_explicit(&PEER[i], memory_order_relaxed));
const float mine = MINE[i], peer = ready[0] == x ? as_type<float>(PEER[i]) : 0.0f;
const float total = rank == 0u ? mine + peer : peer + mine;
OUT[i] = bfloat(total) + YS[i];
}
// TP: this Mac's fp32 partial rows into the send slot once it is free, and the entries the host sends (two a row)
// TP: this Mac's fp32 partial rows into the send slot, and the entries the host sends (two a row)
kernel void ep_rsend(const device float* PART [[buffer(0)]], constant int& rows [[buffer(1)]],
device float* OUT [[buffer(2)]], device atomic_uint* CNT [[buffer(3)]], device atomic_uint* flag [[buffer(4)]],
constant uint& x [[buffer(5)]], device atomic_uint* gave_up [[buffer(6)]],
uint gid [[thread_position_in_grid]], uint t [[thread_index_in_threadgroup]]) {
threadgroup uint ok;
if (!ep_slot_free(flag, x, gave_up, t, &ok)) return;
device float* OUT [[buffer(2)]], device atomic_uint* CNT [[buffer(3)]],
const device ulong* previous [[buffer(4)]], constant ulong& expected [[buffer(5)]], uint gid [[thread_position_in_grid]]) {
if (previous[0] != expected) return;
if (gid == 0) atomic_store_explicit(CNT, uint(rows * 2), memory_order_relaxed);
if (int(gid) < rows * 4096) OUT[gid] = PART[gid];
}
// TP: wait for the peer's partials of exchange x; each value is both Macs' partials added in rank order, rounded
kernel void ep_rsum(device atomic_uint* flag [[buffer(0)]], constant uint& x [[buffer(1)]],
device atomic_uint* gave_up [[buffer(2)]], const device float* MINE [[buffer(3)]], device atomic_uint* PEER [[buffer(4)]],
// TP: both Macs' partials added in rank order and rounded
kernel void ep_rsum(const device ulong* ready [[buffer(0)]], constant ulong& x [[buffer(1)]],
const device float* MINE [[buffer(3)]], const device uint* PEER [[buffer(4)]],
constant uint& rank [[buffer(5)]], device bfloat* OUT [[buffer(6)]], constant int& rows [[buffer(7)]],
uint3 tg [[threadgroup_position_in_grid]], uint3 tpos [[thread_position_in_threadgroup]]) {
const uint t = tpos.x;
if (t == 0) {
uint polls = 0;
while (int(atomic_load_explicit(flag, memory_order_relaxed) - x) < 0) {
if (++polls > 400000000u) { atomic_fetch_add_explicit(gave_up, 1u, memory_order_relaxed); break; }
}
}
threadgroup_barrier(mem_flags::mem_device);
const int i = int(tg.x) * 256 + int(t);
if (i >= rows * 4096) return;
const float mine = MINE[i], peer = as_type<float>(atomic_load_explicit(&PEER[i], memory_order_relaxed));
const float mine = MINE[i], peer = ready[0] == x ? as_type<float>(PEER[i]) : 0.0f;
OUT[i] = bfloat(rank == 0u ? mine + peer : peer + mine);
}
// Exchange x packed: the host sends it once the GPU gets here
kernel void ep_post(device atomic_uint* posted [[buffer(0)]], constant uint& x [[buffer(1)]],
device atomic_uint* gave_up [[buffer(2)]]) {
if (atomic_load_explicit(gave_up, memory_order_relaxed) == 0u) atomic_store_explicit(posted, x, memory_order_relaxed);
}
// Once the peer's exchange x lands (its flag follows its bytes), its entries (atomic reads) into their picks' rows of Y
kernel void ep_unpack(device atomic_uint* flag [[buffer(0)]], constant uint& x [[buffer(1)]],
device atomic_uint* gave_up [[buffer(2)]], device atomic_uint* IN [[buffer(3)]], const device int* THEIRS [[buffer(4)]],
// Copy the peer's entries into their picks' rows of Y
kernel void ep_unpack(const device ulong* ready [[buffer(0)]], constant ulong& x [[buffer(1)]],
const device uint* IN [[buffer(3)]], const device int* THEIRS [[buffer(4)]],
const device int* COUNTS [[buffer(5)]], device uint* Y [[buffer(6)]], uint3 tg [[threadgroup_position_in_grid]],
uint3 tpos [[thread_position_in_threadgroup]]) {
const uint t = tpos.x;
if (t == 0) {
uint polls = 0;
while (int(atomic_load_explicit(flag, memory_order_relaxed) - x) < 0) {
if (++polls > 400000000u) { atomic_fetch_add_explicit(gave_up, 1u, memory_order_relaxed); break; }
}
}
threadgroup_barrier(mem_flags::mem_device);
const int i = int(tg.y);
if (i >= COUNTS[1]) return;
device atomic_uint* src = IN + size_t(i) * WORDS;
const device uint* src = IN + size_t(i) * WORDS;
device uint* dst = Y + size_t(THEIRS[i]) * WORDS;
for (uint j = t; j < uint(WORDS); j += 256) dst[j] = atomic_load_explicit(&src[j], memory_order_relaxed);
const bool landed = ready[0] == x;
for (uint j = t; j < uint(WORDS); j += 256) dst[j] = landed ? src[j] : 0u;
}
// TP: each row's best logit of this Mac's vocabulary rows [lo, lo + n) and its token id to send (n = 0: none)
kernel void ep_amax_send(const device bfloat* LOGITS [[buffer(0)]], const device uint* PICKS [[buffer(1)]],
constant uint4& arg [[buffer(2)]], device uint* OUT [[buffer(3)]], device atomic_uint* CNT [[buffer(4)]],
device atomic_uint* flag [[buffer(5)]], constant uint& x [[buffer(6)]], device atomic_uint* gave_up [[buffer(7)]],
const device ulong* previous [[buffer(5)]], constant ulong& expected [[buffer(6)]],
uint t [[thread_index_in_threadgroup]]) {
threadgroup uint ok;
if (!ep_slot_free(flag, x, gave_up, t, &ok)) return;
if (previous[0] != expected) return;
const uint rows = arg.x, n = arg.y, lo = arg.z;
if (t == 0) atomic_store_explicit(CNT, 1u, memory_order_relaxed);
if (t >= rows) return;
Expand All @@ -161,18 +118,15 @@ kernel void ep_amax_send(const device bfloat* LOGITS [[buffer(0)]], const device
OUT[2 * t + 1] = has ? lo + p : 0xffffffffu;
}
// TP: once the peer's candidates land, each row's pick is the larger logit, the lower id on a tie (one Mac's rule)
kernel void ep_amax_merge(device atomic_uint* flag [[buffer(0)]], constant uint& x [[buffer(1)]],
device atomic_uint* gave_up [[buffer(2)]], const device uint* MINE [[buffer(3)]], device atomic_uint* PEER [[buffer(4)]],
kernel void ep_amax_merge(const device ulong* ready [[buffer(0)]], constant ulong& x [[buffer(1)]],
const device uint* MINE [[buffer(3)]], const device uint* PEER [[buffer(4)]],
constant uint& rows [[buffer(5)]], device uint* PICKS [[buffer(6)]], uint t [[thread_index_in_threadgroup]]) {
if (t == 0) {
uint polls = 0;
while (int(atomic_load_explicit(flag, memory_order_relaxed) - x) < 0) {
if (++polls > 400000000u) { atomic_fetch_add_explicit(gave_up, 1u, memory_order_relaxed); break; }
}
}
threadgroup_barrier(mem_flags::mem_device);
if (t >= rows) return;
const float a = as_type<float>(MINE[2 * t]), b = as_type<float>(atomic_load_explicit(&PEER[2 * t], memory_order_relaxed));
const uint ia = MINE[2 * t + 1], ib = atomic_load_explicit(&PEER[2 * t + 1], memory_order_relaxed);
if (ready[0] != x) {
PICKS[t] = 0u;
return;
}
const float a = as_type<float>(MINE[2 * t]), b = as_type<float>(PEER[2 * t]);
const uint ia = MINE[2 * t + 1], ib = PEER[2 * t + 1];
PICKS[t] = (b > a || (b == a && ib < ia)) ? ib : ia;
}
3 changes: 3 additions & 0 deletions zig/residency_recording_metal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,8 @@ pub const ComputeEncoder = struct {
pub fn dispatchGroups(_: @This(), _: Size, _: Size) void {}
pub fn dispatchIndirect(_: @This(), _: Buffer, _: usize, _: Size) void {}
pub fn barrier(_: @This()) void {}
pub fn dispatchType(_: @This()) DispatchType {
return .serial;
}
pub fn end(_: @This()) void {}
};
13 changes: 13 additions & 0 deletions zig/src/core/contract/glm_ep_records.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! GLM's Metal handoff launches with no threadgroup scratch.
const M = @import("limits.zig").MetalLaunch;

pub const records = [_]M{
.{ .name = "metal glm ep ep_pack", .threads = 256, .tg_memory = 0, .m1 = 1024, .m2 = 1024 },
.{ .name = "metal glm ep ep_unpack", .threads = 256, .tg_memory = 0, .m1 = 1024, .m2 = 1024 },
.{ .name = "metal glm ep ep_rcombine", .threads = 256, .tg_memory = 0, .m1 = 1024, .m2 = 1024 },
.{ .name = "metal glm ep ep_rfinal", .threads = 256, .tg_memory = 0, .m1 = 1024, .m2 = 1024 },
.{ .name = "metal glm ep ep_rsend", .threads = 256, .tg_memory = 0, .m1 = 1024, .m2 = 1024 },
.{ .name = "metal glm ep ep_rsum", .threads = 256, .tg_memory = 0, .m1 = 1024, .m2 = 1024 },
.{ .name = "metal glm ep ep_amax_send", .threads = 64, .tg_memory = 0, .m1 = 1024, .m2 = 1024 },
.{ .name = "metal glm ep ep_amax_merge", .threads = 64, .tg_memory = 0, .m1 = 1024, .m2 = 1024 },
};
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 @@ -68,6 +68,7 @@ pub const all_metal = @import("lane_q8_records.zig").records ++
@import("flashnext_pending.zig").records ++
@import("simd_pairs_records.zig").records ++
@import("simd_half_records.zig").records ++
@import("glm_ep_records.zig").records ++
@import("metal_pair_records.zig").records ++
@import("glm_rows_records.zig").records ++
@import("glm_launch_records.zig").records ++
Expand Down
39 changes: 33 additions & 6 deletions zig/src/fabric/mcdma.zig
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,19 @@ pub const Endpoint = struct {

/// Send `len` bytes at `local` to peer `offset`, then `value` at `flag`; head goes before `local`; keep till acked.
pub fn writeSignalFrom(self: *Endpoint, peer: u32, local: usize, offset: usize, len: usize, flag: usize, value: u64) rma.Error!void {
if (len == 0) return signal(self, peer, flag, value); // the library takes a zero-length write-and-signal as a dead peer
if (local < abi.ws_room or !self.in(local - abi.ws_room, len + abi.ws_room) or !self.in(offset, len) or !self.in(flag, 8)) return error.OutOfBounds;
return self.writeSignalFromUntil(peer, local, offset, len, flag, value, std.math.maxInt(u64));
}

/// The call deadline bounds admission to the native write. The library owns the timeout once inside that call.
pub fn writeSignalFromUntil(self: *Endpoint, peer: u32, local: usize, offset: usize, len: usize, flag: usize, value: u64, until: u64) rma.Error!void {
if (len > 0 and (local < abi.ws_room or !self.in(local - abi.ws_room, len + abi.ws_room) or !self.in(offset, len) or !self.in(flag, 8))) return error.OutOfBounds;
if (len == 0) return self.signalUntil(peer, flag, value, until);
if (flag % 8 != 0) return error.Unaligned;
const port = try self.portFor(peer);
try port.mutex.lock(try self.end());
const end_at = @min(until, try self.end());
try port.mutex.lock(end_at);
defer port.mutex.unlock();
if (try words.checkedNowNs() >= end_at) return error.Timeout;
if (port.link == roce) { // ConnectX has no fused message: the write, then the signal, on the one queue pair
try self.checkPort(port, self.library.api.write(port.peer.?, local, offset, len));
port.pending = true;
Expand Down Expand Up @@ -355,8 +362,14 @@ pub const Endpoint = struct {
}

fn flushPort(self: *Endpoint, port: *Port) rma.Error!void {
try self.flushPortUntil(port, try self.end());
}

fn flushPortUntil(self: *Endpoint, port: *Port, end_at: u64) rma.Error!void {
if (!port.pending) return;
const timeout = try self.left();
const now = try words.checkedNowNs();
if (now >= end_at) return error.Timeout;
const timeout = end_at - now;
try self.checkPort(port, self.library.api.flush(port.peer.?, timeout));
port.used = 0;
port.pending = false;
Expand Down Expand Up @@ -446,16 +459,21 @@ pub const Endpoint = struct {
}

fn signal(ptr: *anyopaque, peer: u32, offset: usize, value: u64) rma.Error!void {
const self = of(ptr);
return of(ptr).signalUntil(peer, offset, value, std.math.maxInt(u64));
}

fn signalUntil(self: *Endpoint, peer: u32, offset: usize, value: u64, until: u64) rma.Error!void {
if (offset % 8 != 0) return error.Unaligned;
if (!self.in(offset, 8)) return error.OutOfBounds;
if (peer == self.config.rank) {
@atomicStore(u64, @as(*u64, @ptrCast(@alignCast(self.memory.ptr + offset))), value, .release);
return;
}
const port = try self.portFor(peer);
try port.mutex.lock(try self.end());
const end_at = @min(until, try self.end());
try port.mutex.lock(end_at);
defer port.mutex.unlock();
if (try words.checkedNowNs() >= end_at) return error.Timeout;
try self.checkPort(port, self.library.api.signal(port.peer.?, offset, value));
port.pending = true;
}
Expand All @@ -480,6 +498,15 @@ pub const Endpoint = struct {
}
}

/// Retire a peer's posted writes without changing deadlines on concurrent control or benchmark calls.
pub fn flushPeerUntil(self: *Endpoint, peer: u32, until: u64) rma.Error!void {
const port = try self.portFor(peer);
const end_at = @min(until, try self.end());
try port.mutex.lock(end_at);
defer port.mutex.unlock();
try self.flushPortUntil(port, end_at);
}

fn kind(ptr: *anyopaque, peer: u32) rma.LinkKind {
const self = of(ptr);
if (peer == self.config.rank) return .local;
Expand Down
43 changes: 43 additions & 0 deletions zig/src/fabric/mcdma_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,49 @@ test "write2Signal refuses a bad flag before posting anything, over ConnectX as
}
}

test "an expired send deadline refuses payload and signal-only posts before calling the library" {
for ([_]c_int{ roce, abi.thunderbolt }) |link| {
Fake.reset(link);
const ep = try Fake.endpoint(Fake.cx);
defer ep.deinit();
const expired = try words.checkedNowNs();
try std.testing.expectError(error.Timeout, ep.writeSignalFromUntil(1, 1024, 2048, 64, 24, 3, expired));
try std.testing.expectError(error.Timeout, ep.writeSignalFromUntil(1, 0, 0, 0, 32, 4, expired));
try std.testing.expectEqual(@as(usize, 0), Fake.n);
try std.testing.expect(!ep.ports[0].pending);
}
}

test "a bounded peer flush retires a signal-only post only after native success" {
Fake.reset(abi.thunderbolt);
const ep = try Fake.endpoint(Fake.cx);
defer ep.deinit();
const budget = 500 * std.time.ns_per_ms;

var deadline = (try words.checkedNowNs()) + budget;
try ep.writeSignalFromUntil(1, 0, 0, 0, 32, 4, deadline);
try std.testing.expectEqualDeep(Fake.Call{ .signal = .{ 32, 4 } }, Fake.calls[0]);
try std.testing.expect(ep.ports[0].pending);
try std.testing.expectError(error.Timeout, ep.flushPeerUntil(1, try words.checkedNowNs()));
try std.testing.expectEqual(@as(usize, 1), Fake.n);
try std.testing.expect(ep.ports[0].pending);

try ep.flushPeerUntil(1, deadline);
try std.testing.expectEqual(@as(usize, 2), Fake.n);
try std.testing.expect(Fake.calls[1] == .flush);
try std.testing.expect(Fake.calls[1].flush > 0 and Fake.calls[1].flush <= budget);
try std.testing.expect(!ep.ports[0].pending);

deadline = (try words.checkedNowNs()) + budget;
try ep.writeSignalFromUntil(1, 1024, 2048, 64, 24, 5, deadline);
Fake.flush_status = 6;
try std.testing.expectError(error.Timeout, ep.flushPeerUntil(1, deadline));
try std.testing.expectEqual(@as(usize, 4), Fake.n);
try std.testing.expect(Fake.calls[3] == .flush);
try std.testing.expect(Fake.calls[3].flush > 0 and Fake.calls[3].flush <= budget);
try std.testing.expect(ep.ports[0].pending);
}

test "one endpoint with a Thunderbolt peer and a ConnectX peer picks each port's path and kind" {
Fake.reset(abi.thunderbolt);
Fake.mixed = true;
Expand Down
Loading