diff --git a/CHANGELOG.md b/CHANGELOG.md index e2fbc00b87c..a8438c575b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,10 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - CUDA: refresh 13 nvcc 13.0 register-count slots from the five-SM 13.0.88 audit; other compiler records and launch geometry stay unchanged (#2515, refs #2395). - CUDA: a request's finished stats list its prompt pass widths for Qwen3.8-27B, GLM-5.3-Flash and Nemotron, appended only where a pass is known complete. Telemetry only (#2304). +- GLM CUDA: bound the peer warm-up after NCCL init to 300 seconds, aborting its communicator and stopping every rank with the missing peers and observed transport named on timeout (#2512, part of #2347). + +- CUDA: a request's finished stats list its prompt pass widths for Qwen3.8-27B, GLM-5.3-Flash and Nemotron, appended + only where a pass is already known complete. Telemetry only (#2304). - Prompt cache: `--learn` opens its kept states in linear time, so a server with 100,000 of them starts about 5 s sooner (8.0x faster open); the same states and files are kept (#2316). diff --git a/zig/src/cuda/tp_watch.zig b/zig/src/cuda/tp_watch.zig index d13defa8d2b..9f55dc15a61 100644 --- a/zig/src/cuda/tp_watch.zig +++ b/zig/src/cuda/tp_watch.zig @@ -217,7 +217,7 @@ pub fn fatal(name: []const u8) void { } /// Ends this process at once: a stream may hold kernels that wait on the gone rank, which a normal exit would join. -fn stop() noreturn { +pub fn stop() noreturn { platform.c._exit(1); } @@ -491,9 +491,10 @@ const ProcessTest = struct { } } if (mode == 3) return error.InjectedParentFailure; - if (mode == 1) { + if (mode == 1 or mode == 4) { var record: Record = .{ .kind = error_kind, .rank = 1 }; - @memcpy(record.text[0..8], "peer OOM"); + const name = if (mode == 4) "PeerWarmupTimedOut" else "peer OOM"; + @memcpy(record.text[0..name.len], name); const data = std.mem.asBytes(&record); try std.testing.expectEqual(@as(isize, data.len), posix.system.write(pair[1], data.ptr, data.len)); } @@ -522,6 +523,7 @@ test "the real peer watcher exits on a closed line and on an error record, and r try ProcessTest.check(0); try ProcessTest.check(1); try ProcessTest.check(2); + try ProcessTest.check(4); } test "a failing parent check still closes the line, kills and reaps the watcher's child" { diff --git a/zig/src/cuda/tp_watch_child.zig b/zig/src/cuda/tp_watch_child.zig index bbf3ab26bd1..3f8a7532a5a 100644 --- a/zig/src/cuda/tp_watch_child.zig +++ b/zig/src/cuda/tp_watch_child.zig @@ -31,6 +31,7 @@ pub fn said(mode: u8) []const u8 { 0 => "[default] (err): rank 0: another rank's process ended (its line closed); " ++ "this rank stops instead of waiting on it\n", 1 => "[default] (err): rank 0: rank 1 stopped (peer OOM); this rank stops too\n", + 4 => "[default] (err): rank 0: rank 1 stopped (PeerWarmupTimedOut); this rank stops too\n", else => "", }; } diff --git a/zig/src/families/glm/cuda_native.zig b/zig/src/families/glm/cuda_native.zig index 20c56a06c2b..6db4e2e82f1 100644 --- a/zig/src/families/glm/cuda_native.zig +++ b/zig/src/families/glm/cuda_native.zig @@ -253,7 +253,7 @@ fn openRank(a: StartArgs, set: ranks.Settings) !Loaded { errdefer _ = own.group.lib.api.ncclCommDestroy(own.group.comm); cuda.p2p_report.beforePeers(&diag, .{ .lib = &own.group.lib, .comm = comm, .later = true }); own.progress.enter(.peer_warmup); - if (p.world > 1) try @import("cuda_warm_peers.zig").warm(ctx.d, &own.group, set.ring); + if (p.world > 1) try @import("cuda_warm_peers.zig").warm(io, ctx.d, &own.group, set.ring, &diag); cuda.p2p_report.afterInit(&diag, .{ .lib = &own.group.lib, .comm = comm, .d = ctx.d, .later = true }); const cap = capacity(o.context); own.progress.enter(.weights); diff --git a/zig/src/families/glm/cuda_warm_deadline.zig b/zig/src/families/glm/cuda_warm_deadline.zig new file mode 100644 index 00000000000..d9af05694c2 --- /dev/null +++ b/zig/src/families/glm/cuda_warm_deadline.zig @@ -0,0 +1,40 @@ +//! One deadline for the peer exchange, including blocking NCCL calls and stream cleanup. +const std = @import("std"); + +pub const timeout_ms: u64 = 300_000; + +/// The test supplies a short limit and a fake clock through the same boundary as the production warm-up. +pub fn run(clock: anytype, limit_ms: u64, action: anytype, args: anytype, comptime work: anytype) anyerror!void { + const Guard = struct { + const State = enum(u8) { waiting, finished, expired }; + state: std.atomic.Value(State) = .init(.waiting), + clock: @TypeOf(clock), + action: @TypeOf(action), + since: u64, + limit: u64, + + fn expire(g: *@This()) void { + if (g.state.cmpxchgStrong(.waiting, .expired, .acq_rel, .acquire) == null) g.action.expired(); + } + fn watch(g: *@This()) void { + while (g.state.load(.acquire) == .waiting) { + if (g.clock.now() -| g.since >= g.limit) { + g.expire(); + return; + } + g.clock.pause(); + } + } + fn finish(g: *@This()) bool { + if (g.clock.now() -| g.since >= g.limit) g.expire(); + return g.state.cmpxchgStrong(.waiting, .finished, .acq_rel, .acquire) != null; + } + }; + var guard: Guard = .{ .clock = clock, .action = action, .since = clock.now(), .limit = limit_ms }; + const watcher = try std.Thread.spawn(.{}, Guard.watch, .{&guard}); + const result = work(args); + const expired = guard.finish(); + watcher.join(); + if (expired) return error.PeerWarmupTimedOut; + return result; +} diff --git a/zig/src/families/glm/cuda_warm_peers.zig b/zig/src/families/glm/cuda_warm_peers.zig index de5b9ed0473..2b55a45d75f 100644 --- a/zig/src/families/glm/cuda_warm_peers.zig +++ b/zig/src/families/glm/cuda_warm_peers.zig @@ -1,16 +1,83 @@ //! Connect GLM's explicit prompt-exchange peers before weights consume the registration budget. +const std = @import("std"); const cuda = @import("cuda"); +const deadline = @import("cuda_warm_deadline.zig"); pub const bytes = 4 << 20; -pub fn warm(d: *const cuda.Driver, g: *const cuda.tp_group.Group, ring: bool) !void { +pub fn warm(io: std.Io, d: *const cuda.Driver, g: *cuda.tp_group.Group, ring: bool, diag: *const cuda.p2p_report.Session) !void { + return warmWith(d, g, ring, Clock{ .io = io }, deadline.timeout_ms, Action{ .g = g, .ring = ring, .diag = diag }); +} + +/// Production uses 300 s; host fixtures shorten only this injected clock/deadline and termination action. +pub fn warmWith(d: *const cuda.Driver, g: *cuda.tp_group.Group, ring: bool, clock: anytype, limit_ms: u64, action: anytype) !void { if (g.world <= 1) return; - var buf = try cuda.DeviceBuffer.alloc(d, 2 * bytes * g.world); - defer buf.free(); - var stream = try cuda.Stream.init(d, false); - defer stream.deinit(); - try post(g, stream, buf.ptr, ring); - try stream.synchronize(); + try deadline.run(clock, limit_ms, action, Work{ .d = d, .g = g, .ring = ring }, Work.run); +} + +const Work = struct { + d: *const cuda.Driver, + g: *cuda.tp_group.Group, + ring: bool, + fn run(a: Work) !void { + const d = a.d; + const g = a.g; + const ring = a.ring; + var buf = try cuda.DeviceBuffer.alloc(d, 2 * bytes * g.world); + defer buf.free(); + var stream = try cuda.Stream.init(d, false); + defer stream.deinit(); + try post(g, stream, buf.ptr, ring); + try stream.synchronize(); + } +}; + +const Clock = struct { + io: std.Io, + pub fn now(c: Clock) u64 { + return @intCast(@divTrunc(std.Io.Clock.awake.now(c.io).toNanoseconds(), std.time.ns_per_ms)); + } + pub fn pause(c: Clock) void { + std.Io.sleep(c.io, .fromMilliseconds(10), .awake) catch {}; + } +}; + +const Action = struct { + g: *cuda.tp_group.Group, + ring: bool, + diag: *const cuda.p2p_report.Session, + pub fn expired(a: Action) void { + timeoutWith(a.g, a.ring, a.diag, cuda.tp_watch, log); + } +}; + +fn log(text: []const u8) void { + std.log.err("{s}", .{text}); +} + +/// Publish the named cause before abort can release a blocked NCCL call with a less specific error. +pub fn timeoutWith(g: *cuda.tp_group.Group, ring: bool, diag: *const cuda.p2p_report.Session, watch: anytype, sink: cuda.startup_log.Sink) void { + var raw: [64 << 10]u8 = undefined; + const how = cuda.startup_watch.transport(diag.capture.read(&raw), @intCast(g.rank)); + var buf: [2048]u8 = undefined; + var w = std.Io.Writer.fixed(&buf); + timeoutLine(&w, g.rank, g.world, ring, how) catch {}; + sink(w.buffered()); + watch.fail("PeerWarmupTimedOut"); + g.abort(); + watch.stop(); +} + +pub fn timeoutLine(w: *std.Io.Writer, rank: usize, world: usize, ring: bool, how: u3) !void { + try w.print("rank {d}: peer warm-up: 4 MiB send/recv with rank(s) ", .{rank}); + const plan: cuda.relay_plan.Relay = .{ .rank = rank, .world = world, .n = 0 }; + var first = true; + for (0..world) |peer| { + if (peer == rank or (ring and peer != plan.next() and peer != plan.prev())) continue; + try w.print("{s}{d}", .{ if (first) "" else ",", peer }); + first = false; + } + try w.print(" not finished after {d} s (NCCL transport {s}); PeerWarmupTimedOut; stopping every rank", .{ deadline.timeout_ms / 1000, cuda.startup_watch.transportName(how) }); } /// The existing 4 MiB send/receive, restricted to neighbours only when ring exchange was selected. diff --git a/zig/src/families/glm/cuda_warm_peers_test.zig b/zig/src/families/glm/cuda_warm_peers_test.zig index 9460eb2e689..63af49c27e5 100644 --- a/zig/src/families/glm/cuda_warm_peers_test.zig +++ b/zig/src/families/glm/cuda_warm_peers_test.zig @@ -142,3 +142,7 @@ test "GLM warm peers: existing ring selector alone restricts warm-up, gather and } Fake.mode = null; } + +test { + _ = @import("cuda_warm_timeout_test.zig"); +} diff --git a/zig/src/families/glm/cuda_warm_timeout_test.zig b/zig/src/families/glm/cuda_warm_timeout_test.zig new file mode 100644 index 00000000000..1ee9f95f167 --- /dev/null +++ b/zig/src/families/glm/cuda_warm_timeout_test.zig @@ -0,0 +1,215 @@ +//! Real warm-up over fake NCCL and CUDA calls; the outer escape makes the unbounded control fail, not hang. +const std = @import("std"); +const cuda = @import("cuda"); +const warm = @import("cuda_warm_peers.zig"); +const t = std.testing; + +const Fake = struct { + const Stage = enum { posting, stream, cleanup }; + var stage: Stage = .posting; + var ms = std.atomic.Value(u64).init(0); + var entered = std.atomic.Value(bool).init(false); + var aborted = std.atomic.Value(bool).init(false); + var escaped = std.atomic.Value(bool).init(false); + var done = std.atomic.Value(bool).init(false); + var replies: ?u64 = null; + var stops: [16]?[]const u8 = @splat(null); + var mine: usize = 0; + var world: usize = 2; + var order: u8 = 0; + var abort_count: usize = 0; + var syncs: usize = 0; + var message: [2048]u8 = undefined; + var message_len: usize = 0; + var allocated: usize = 0; + var freed: usize = 0; + var posts: usize = 0; + + fn reset(rank: usize, count: usize, where: Stage, answer: ?u64) void { + ms.store(0, .release); + entered.store(false, .release); + aborted.store(false, .release); + escaped.store(false, .release); + done.store(false, .release); + stops = @splat(null); + stage = where; + replies = answer; + mine = rank; + world = count; + order = 0; + abort_count = 0; + syncs = 0; + message_len = 0; + allocated = 0; + freed = 0; + posts = 0; + } + fn pause() void { + std.Io.sleep(t.io, .fromMilliseconds(1), .awake) catch {}; + } + fn wait() void { + entered.store(true, .release); + while (!aborted.load(.acquire) and !escaped.load(.acquire)) { + if (replies) |at| if (ms.load(.acquire) >= at) return; + pause(); + } + } + fn escape() void { + for (0..500) |_| { + if (done.load(.acquire)) return; + if (@hasDecl(warm, "warmWith")) pause() else (Clock{}).pause(); + } + escaped.store(true, .release); + } + const Clock = struct { + pub fn now(_: @This()) u64 { + return ms.load(.acquire); + } + pub fn pause(_: @This()) void { + if (entered.load(.acquire)) { + const now_ms = ms.load(.acquire); + if (replies == null or now_ms < replies.?) _ = ms.fetchAdd(1, .acq_rel); + } + Fake.pause(); + } + }; + fn alloc(ptr: *u64, n: usize) callconv(.c) cuda.abi.Result { + ptr.* = 0x4000; + allocated = n; + return 0; + } + fn free(_: u64) callconv(.c) cuda.abi.Result { + freed += 1; + return 0; + } + fn stream(ptr: *cuda.abi.Stream, flags: c_uint) callconv(.c) cuda.abi.Result { + std.debug.assert(flags == 0); + ptr.* = null; + return 0; + } + fn sync(_: cuda.abi.Stream) callconv(.c) cuda.abi.Result { + syncs += 1; + if (stage == .stream or (stage == .cleanup and syncs > 1)) wait(); + return 0; + } + fn destroy(_: cuda.abi.Stream) callconv(.c) cuda.abi.Result { + return 0; + } + fn start() callconv(.c) cuda.nccl.Result { + return 0; + } + fn end() callconv(.c) cuda.nccl.Result { + if (stage == .posting) wait(); + return 0; + } + fn transfer(_: u64, n: usize, _: cuda.nccl.DataType, peer: c_int, _: cuda.nccl.Comm, _: cuda.abi.Stream) callconv(.c) cuda.nccl.Result { + std.debug.assert(n == 4 << 20 and peer != mine); + posts += 1; + return 0; + } + fn abort(_: cuda.nccl.Comm) callconv(.c) cuda.nccl.Result { + std.debug.assert(order == 1); + order = 2; + abort_count += 1; + aborted.store(true, .release); + return 0; + } + const Watch = struct { + pub fn fail(name: []const u8) void { + std.debug.assert(message_len > 0 and order == 0); + order = 1; + for (0..world) |rank| if (rank != mine) { + stops[rank] = name; + }; + } + pub fn stop() void { + std.debug.assert(order == 2 and aborted.load(.acquire)); + stops[mine] = "PeerWarmupTimedOut"; + order = 3; + } + }; + fn log(text: []const u8) void { + @memcpy(message[0..text.len], text); + message_len = text.len; + } + const Action = struct { + g: *cuda.tp_group.Group, + ring: bool, + pub fn expired(a: @This()) void { + if (@hasDecl(warm, "timeoutWith")) warm.timeoutWith(a.g, a.ring, &.{}, Watch, log); + } + }; + fn driver() cuda.Driver { + var d: cuda.Driver = undefined; + d.api.cuMemAlloc_v2 = alloc; + d.api.cuMemFree_v2 = free; + d.api.cuStreamCreate = stream; + d.api.cuStreamSynchronize = sync; + d.api.cuStreamDestroy_v2 = destroy; + return d; + } + fn group() cuda.tp_group.Group { + var g: cuda.tp_group.Group = .{ .rank = mine, .world = world, .comm = null, .lib = undefined }; + g.lib.api.ncclGroupStart = start; + g.lib.api.ncclGroupEnd = end; + g.lib.api.ncclSend = transfer; + g.lib.api.ncclRecv = transfer; + g.lib.api.ncclCommAbort = abort; + return g; + } + fn run(rank: usize, count: usize, ring: bool, where: Stage, answer: ?u64) !void { + reset(rank, count, where, answer); + var g = group(); + const d = driver(); + const guard = try std.Thread.spawn(.{}, escape, .{}); + defer { + done.store(true, .release); + guard.join(); + } + if (@hasDecl(warm, "warmWith")) { + try warm.warmWith(&d, &g, ring, Clock{}, 100, Action{ .g = &g, .ring = ring }); + } else { + try warm.warm(&d, &g, ring); + } + } +}; + +test "GLM warm peers: a missing peer in posting or stream wait aborts once and stops every rank by name" { + for ([_]Fake.Stage{ .posting, .stream, .cleanup }) |stage| for (0..2) |rank| { + try t.expectError(error.PeerWarmupTimedOut, Fake.run(rank, 2, false, stage, null)); + try t.expect(!Fake.escaped.load(.acquire)); + try t.expectEqual(@as(u64, 100), Fake.ms.load(.acquire)); + try t.expectEqual(@as(usize, 1), Fake.abort_count); + try t.expectEqual(@as(u8, 3), Fake.order); + for (Fake.stops[0..2]) |cause| try t.expectEqualStrings("PeerWarmupTimedOut", cause orelse "still waiting"); + var expected: [512]u8 = undefined; + const line = try std.fmt.bufPrint(&expected, "rank {d}: peer warm-up: 4 MiB send/recv with rank(s) {d} not finished after 300 s (NCCL transport unknown); PeerWarmupTimedOut; stopping every rank", .{ rank, 1 - rank }); + try t.expectEqualStrings(line, Fake.message[0..Fake.message_len]); + try t.expectEqual(@as(usize, 16 << 20), Fake.allocated); + try t.expectEqual(@as(usize, 1), Fake.freed); + try t.expectEqual(@as(usize, 2), Fake.posts); + }; +} + +test "GLM warm peers: a peer answering just inside the bound passes without abort or stopping another rank" { + for ([_]Fake.Stage{ .posting, .stream }) |stage| for (0..2) |rank| { + try Fake.run(rank, 2, false, stage, 99); + try t.expect(!Fake.escaped.load(.acquire)); + if (@hasDecl(warm, "warmWith")) try t.expectEqual(@as(u64, 99), Fake.ms.load(.acquire)); + try t.expectEqual(@as(usize, 0), Fake.abort_count); + try t.expectEqual(@as(usize, 0), Fake.message_len); + for (Fake.stops[0..2]) |cause| try t.expect(cause == null); + try t.expectEqual(@as(usize, 1), Fake.freed); + }; +} + +test "GLM warm peers: timeout names only selected ring peers and observed NCCL transport" { + if (!@hasDecl(warm, "timeoutLine")) return error.SkipZigTest; + var buf: [512]u8 = undefined; + var w = std.Io.Writer.fixed(&buf); + try warm.timeoutLine(&w, 1, 4, true, 5); + try t.expectEqualStrings("rank 1: peer warm-up: 4 MiB send/recv with rank(s) 0,2 not finished after 300 s (NCCL transport P2P+NET); PeerWarmupTimedOut; stopping every rank", w.buffered()); + w = std.Io.Writer.fixed(&buf); + try warm.timeoutLine(&w, 1, 4, false, 0); + try t.expect(std.mem.indexOf(u8, w.buffered(), "rank(s) 0,2,3") != null); +}