Skip to content

Commit b69f89e

Browse files
committed
native cuda: a family's options from the serve flags move to cuda_options.zig, so cuda.zig stays under 600 lines
#952's rank and follower wiring took native/cuda.zig to 611 lines. cuda_options.zig now sets the ranks, vision, --precision and fp8-cache options a family takes, unchanged, and openWith calls it. cuda.zig is 593 lines. The staging test's title now says it covers a failed first upload too.
1 parent 5a82c35 commit b69f89e

3 files changed

Lines changed: 31 additions & 21 deletions

File tree

‎zig/src/families/flashnext/cuda_batch_state.zig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ const FakeQueue = struct {
504504
}
505505
};
506506

507-
test "a pass whose second upload or whose record fails leaves no copy reading the staging when it is reused" {
507+
test "a pass whose first or second upload or whose record fails leaves no copy reading the staging when it is reused" {
508508
const Case = struct { fail_copy: ?usize, fail_record: bool };
509509
const cases = [_]Case{
510510
.{ .fail_copy = 1, .fail_record = false },

‎zig/src/native/cuda.zig‎

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const lanes = @import("lanes");
77
const nemotron = @import("nemotron");
88
const flashnext = @import("flashnext");
99
const family_memory = @import("cuda_family_memory.zig");
10+
const family_options = @import("cuda_options.zig");
1011
pub const follow = @import("cuda_follow.zig").follow;
1112
const lfm2 = @import("lfm2.zig");
1213
const glm = @import("glm_cuda");
@@ -324,27 +325,8 @@ fn openWith(comptime F: type, a: Allocator, gpa: Allocator, io: std.Io, o: api.O
324325
};
325326
var probe: Probe = .{ .a = a, .io = io, .ctx = &g.ctx, .held = held0, .problem = problem };
326327
var opts: F.Options = .{ .context = @intCast(window), .drafts = o.drafts, .segments = segments, .slide = o.slide };
327-
if (@hasField(F.Options, "rank")) {
328-
opts.tp = @intCast(o.tp);
329-
opts.rank = @intCast(o.rank);
330-
opts.master = if (o.master.len > 0) o.master else null;
331-
opts.master_port = o.master_port;
332-
}
333-
if (comptime @hasField(F.Options, "vision")) {
334-
if (o.vision) |vo| {
335-
const own: ?u32 = comptime if (@hasDecl(F, "default_image_tokens")) F.default_image_tokens else null;
336-
opts.vision = .{ .image_tokens = if (vo.image_tokens_set) vo.image_tokens else own orelse vo.image_tokens, .max_images = vo.max_images };
337-
}
338-
}
339-
if (o.precision) |p| {
340-
if (!@hasField(F.Options, "precision")) {
341-
problem.* = "--precision: this CUDA family reads no NVFP4 checkpoint; serve it without --precision";
342-
return null;
343-
}
344-
opts.precision = std.meta.stringToEnum(@typeInfo(@TypeOf(opts.precision)).optional.child, @tagName(p)).?;
345-
}
328+
if (!family_options.set(F, o, &opts, problem)) return null;
346329
if (@hasField(F.Options, "room")) opts.room = .{ .ctx = &probe, .read = Probe.read };
347-
if (@hasField(F.Options, "kv8")) opts.kv8 = o.kv8;
348330
if (weights > before.room(held0)) {
349331
problem.* = try std.fmt.allocPrint(a, "the checkpoint's {d:.1} GiB of weights do not fit the {d:.1} GiB the CUDA memory budget grants ({d:.1} GiB free less a {d:.1} GiB reserve{s}); free device memory or adjust TENSORFOLD_MEMORY_RESERVE_GIB / TENSORFOLD_CUDA_MEMORY_LIMIT_GB", .{ toGib(weights), toGib(before.room(held0)), toGib(before.free), toGib(before.reserve), if (before.limit != null) ", under TENSORFOLD_CUDA_MEMORY_LIMIT_GB" else "" });
350332
return null;

‎zig/src/native/cuda_options.zig‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//! A CUDA family's options from the serve flags it takes: its ranks, images, NVFP4 precision and fp8 caches.
2+
const std = @import("std");
3+
const api = @import("engine_api");
4+
5+
/// Sets what `o` asks of `F` in `opts`; false with `problem` set when F cannot take a flag given (--precision).
6+
pub fn set(comptime F: type, o: api.Open, opts: *F.Options, problem: *[]const u8) bool {
7+
if (@hasField(F.Options, "rank")) {
8+
opts.tp = @intCast(o.tp);
9+
opts.rank = @intCast(o.rank);
10+
opts.master = if (o.master.len > 0) o.master else null;
11+
opts.master_port = o.master_port;
12+
}
13+
if (comptime @hasField(F.Options, "vision")) {
14+
if (o.vision) |vo| {
15+
const own: ?u32 = comptime if (@hasDecl(F, "default_image_tokens")) F.default_image_tokens else null;
16+
opts.vision = .{ .image_tokens = if (vo.image_tokens_set) vo.image_tokens else own orelse vo.image_tokens, .max_images = vo.max_images };
17+
}
18+
}
19+
if (o.precision) |p| {
20+
if (!@hasField(F.Options, "precision")) {
21+
problem.* = "--precision: this CUDA family reads no NVFP4 checkpoint; serve it without --precision";
22+
return false;
23+
}
24+
opts.precision = std.meta.stringToEnum(@typeInfo(@TypeOf(opts.precision)).optional.child, @tagName(p)).?;
25+
}
26+
if (@hasField(F.Options, "kv8")) opts.kv8 = o.kv8;
27+
return true;
28+
}

0 commit comments

Comments
 (0)