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

- CUDA: `TF_CUDA_SCHED=blocking` (off by default; also `auto`, `spin` or `yield`) sets how a served model's host thread waits for its GPU. Blocking frees the core the driver's default spins on a GB10, with identical tokens and pictures; on Inkling 2 Mini decode is about 2% slower (#5165).

- Inkling 2 Mini on Metal: decode fuses its AR, loop and conv steps and runs in one command buffer, with every output bit unchanged (#4593). Thanks to @gilby.

- Tools: `pr_check` refuses a new import from one model family into another (today's edges are the baseline and may only shrink; qwen3 to llama is allowed), beside its existing import-cycle ratchet (fixes #4804). Thanks to @jschmied for the proposal.
Expand Down
1 change: 1 addition & 0 deletions docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Diagnostics quote and escape the supplied text so it cannot create another termi
| `TENSORFOLD_DEVICE_CHECK` | Exactly `0` or `1`; default 1. `0` skips serve's pre-load check against the saved [device profile](device.md) |
| `TF_SHARED_PRICE_LOG` | Exactly `0` or `1`; default off. `1` logs, at an engine's first priced shared round, the load-time shared forward costs and head steps, then one `shared price: round` line a priced shared round: its trial flag, rows, time, each stream's drafts and next depth, the plain and row prices and each stream's acceptance estimates |
| `TF_CUDA_DEVICE` | Whole ordinal 0 through 4294967295, or empty; default 0 |
| `TF_CUDA_SCHED` | `auto`, `spin`, `yield` or `blocking`: how a CUDA server's host thread waits for its GPU, set on the primary context before it starts. Unset or blank keeps the driver's `auto`, which spins when the machine has more cores than active contexts. `blocking` sleeps until the GPU signals; it changes host scheduling only, not launches or tokens. Other values are refused at server startup. |
| `TF_CUDA_SEGMENTS` | Whole count 1 through 4, or empty; default 1 |
| `TENSORFOLD_CUDA_KERNELS` | A path without control characters, used as given: a captured kernel set's folder, or `native` for the engine's own kernels. Unset uses the binary's bundled set for the GPU when it has one, else the engine's own kernels. A family that runs only on the engine's own kernels (Qwen3 among them) refuses any other value; Qwen3 reads a blank value as unset |
| `TF_CUDA_CARVEOUT` | Exactly `0` or `1`; default off |
Expand Down
1 change: 1 addition & 0 deletions zig/src/native/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ fn openWith(comptime F: type, a: Allocator, gpa: Allocator, io: std.Io, asked: a
problem.* = try std.fmt.allocPrint(a, "{s}{d}: this machine has {d} CUDA device{s} (0 to {d})", .{ named, device, count, if (count == 1) "" else "s", @max(count, 1) - 1 });
return null;
}
try @import("cuda_schedule.zig").apply(&g.driver, @intCast(device)); // TF_CUDA_SCHED, before the context starts
g.ctx = try cuda.Context.init(&g.driver, @intCast(device));
defer if (!opened) g.ctx.deinit();
bound = &g.ctx;
Expand Down
2 changes: 2 additions & 0 deletions zig/src/native/cuda_qwen3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ pub fn open(a: std.mem.Allocator, gpa: std.mem.Allocator, io: std.Io, o: api.Ope
return null;
}
const ordinal = try device.setting(a, o.device, "TF_CUDA_DEVICE", 0, problem) orelse return null;
var held = try @import("cuda_schedule.zig").hold(ordinal); // TF_CUDA_SCHED, before the engine's context starts
defer if (held) |*d| d.close();
return qwen3.native.open(a, gpa, io, o, ordinal, problem, budget, windows, @import("sys").optional_libraries);
}
60 changes: 60 additions & 0 deletions zig/src/native/cuda_schedule.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//! TF_CUDA_SCHED: how a serve's host thread waits for its GPU, set on the primary context before the context starts.
const std = @import("std");
const cuda = @import("cuda");
const environment = @import("sys").environment;

/// The setting's words in its registered order, each the CU_CTX_SCHED_* flag of the same name.
const schedules = [_]cuda.Context.Schedule{ .auto, .spin, .yield, .blocking };

/// The schedule `raw` names; null when unset or blank (the driver's own `auto`), refused by name otherwise.
pub fn parse(raw: ?[]const u8) environment.Error!?cuda.Context.Schedule {
const i = (try environment.value(?u64, "TF_CUDA_SCHED", raw, null)) orelse return null;
return schedules[@intCast(i)];
}

/// Sets TF_CUDA_SCHED on `ordinal`'s primary context; unset leaves the context as it is. Call before cuda.Context.init.
pub fn apply(d: *const cuda.Driver, ordinal: c_int) !void {
const s = try parse(environment.rawText("TF_CUDA_SCHED")) orelse return;
try set(d, ordinal, s);
}

/// TF_CUDA_SCHED for an engine that opens its own driver; close it once its context starts. Off: null, ordinal unread.
pub fn hold(ordinal: u32) !?cuda.Driver {
return holdWith(environment.rawText("TF_CUDA_SCHED"), ordinal);
}

fn holdWith(raw: ?[]const u8, ordinal: u32) !?cuda.Driver {
const s = try parse(raw) orelse return null;
const device = std.math.cast(c_int, ordinal) orelse return error.NoSuchCudaDevice;
var d = try cuda.Driver.open();
errdefer d.close();
try set(&d, device, s);
return d;
}

fn set(d: *const cuda.Driver, ordinal: c_int, s: cuda.Context.Schedule) !void {
try cuda.Context.setSchedule(d, ordinal, s);
std.log.info("cuda schedule: {t}", .{s});
}

test "TF_CUDA_SCHED: unset and blank keep the driver's schedule, each word names its flag, others are refused" {
try std.testing.expectEqual(@as(?cuda.Context.Schedule, null), try parse(null));
try std.testing.expectEqual(@as(?cuda.Context.Schedule, null), try parse(""));
for (environment.rule("TF_CUDA_SCHED").words, schedules) |word, s| {
try std.testing.expectEqualStrings(word, @tagName(s));
try std.testing.expectEqual(@as(?cuda.Context.Schedule, s), try parse(word));
}
for ([_][]const u8{ "block", "4", "on", "blocking,spin" }) |bad| try std.testing.expectError(error.InvalidEnvironment, parse(bad));
}

test "TF_CUDA_SCHED hold: off returns before the ordinal is read; set, an ordinal past c_int is refused" {
const past = @as(u32, std.math.maxInt(c_int)) + 1;
for ([_]?[]const u8{ null, "", " " }) |off| {
try std.testing.expectEqual(@as(?cuda.Driver, null), try holdWith(off, std.math.maxInt(u32)));
try std.testing.expectEqual(@as(?cuda.Driver, null), try holdWith(off, past));
}
for (environment.rule("TF_CUDA_SCHED").words) |word| {
try std.testing.expectError(error.NoSuchCudaDevice, holdWith(word, std.math.maxInt(u32)));
try std.testing.expectError(error.NoSuchCudaDevice, holdWith(word, past));
}
}
4 changes: 3 additions & 1 deletion zig/src/native/drama_limit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ pub const Limit = struct {
pub fn init(l: *Limit, gpa: std.mem.Allocator, io: std.Io, device: u32) !void {
l.driver = try cuda.Driver.open();
errdefer l.driver.close();
l.ctx = try cuda.Context.init(&l.driver, std.math.cast(c_int, device) orelse return error.NoSuchCudaDevice);
const ordinal = std.math.cast(c_int, device) orelse return error.NoSuchCudaDevice;
try @import("cuda_schedule.zig").apply(&l.driver, ordinal); // TF_CUDA_SCHED, before the context starts (the engines' contexts are this one)
l.ctx = try cuda.Context.init(&l.driver, ordinal);
errdefer l.ctx.deinit();
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
Expand Down
4 changes: 4 additions & 0 deletions zig/src/native/env_defaults_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ test "env defaults: TF_DRAMA_REFERENCE_CACHE unset keeps no voices and more than
try std.testing.expectEqual(@as(?api.Opened, null), try drama_open.open(arena.allocator(), a, std.testing.io, .{ .dir = "", .model_type = "drama3" }, &problem));
try std.testing.expectEqualStrings("TF_DRAMA_REFERENCE_CACHE must be 0 (off) through 16 entries", problem);
}

test "env defaults: TF_CUDA_SCHED reader" {
_ = @import("cuda_schedule.zig");
}
1 change: 1 addition & 0 deletions zig/src/native/lfm2.zig
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ pub fn open(a: Allocator, gpa: Allocator, io: std.Io, o: api.Open, problem: *[]c
return null;
};
errdefer e.driver.close();
try @import("cuda_schedule.zig").apply(&e.driver, 0); // TF_CUDA_SCHED, before the context starts
e.ctx = try cuda.Context.init(&e.driver, 0);
errdefer e.ctx.deinit();
try e.ctx.makeCurrent();
Expand Down
1 change: 1 addition & 0 deletions zig/src/native/moss_image_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ fn openCheckpoint(gpa: Allocator, io: std.Io, dir: []const u8) !api.Opened {
};
h.driver = try cuda.Driver.open();
errdefer h.driver.close();
try @import("cuda_schedule.zig").apply(&h.driver, 0); // TF_CUDA_SCHED, before the context starts
h.ctx = try cuda.Context.init(&h.driver, 0);
errdefer h.ctx.deinit();
// the prompt encoder keeps its own context and stream over the same primary context, as its tool does
Expand Down
4 changes: 3 additions & 1 deletion zig/src/native/qwen4_cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ const P = struct {
const device = try @import("sys").environment.device(o.device, @import("sys").getenv("TF_CUDA_DEVICE"));
p.driver = try C.Driver.open();
errdefer p.driver.close();
p.context = try C.Context.init(&p.driver, std.math.cast(c_int, device) orelse return error.Qwen4Device);
const ordinal = std.math.cast(c_int, device) orelse return error.Qwen4Device;
try @import("cuda_schedule.zig").apply(&p.driver, ordinal); // TF_CUDA_SCHED, before the context starts
p.context = try C.Context.init(&p.driver, ordinal);
errdefer p.context.deinit();
const stream = try C.Stream.init(&p.driver, true);
p.storage = .{ .driver = &p.driver, .context = &p.context, .stream = stream };
Expand Down
2 changes: 2 additions & 0 deletions zig/src/native/zonos_open.zig
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pub fn open(a: Allocator, gpa: Allocator, io: std.Io, o: api.Open, problem: *[]c
Room.io = io;
var refusal: zonos_host.Refusal = .{};
const admit: zonos_host.Admit = .{ .ptr = &Room.gpa, .room = Room.room };
var held = try @import("cuda_schedule.zig").hold(device); // TF_CUDA_SCHED, before the engine's context starts
defer if (held) |*d| d.close();
return zonos_host.open(gpa, io, o.dir, .{ .context = @intCast(window), .device = device, .max_frames = o.max_frames, .admit = admit, .refusal = &refusal }) catch |e| {
if (e == error.InsufficientDeviceMemory) {
const text = "ZONOS3 Mini needs {d:.2} GiB of device memory at --context {d}; {d:.2} GiB is free for it after the reserve";
Expand Down
1 change: 1 addition & 0 deletions zig/src/platform/environment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const stock_rules = [_]Rule{
.{ .name = "TENSORFOLD_CUDA_KERNELS", .scope = .cuda, .kind = .path, .forms = "a captured kernel set's folder without control characters, or native for the engine's own kernels" },
.{ .name = "TF_NCCL_LIB", .scope = .cuda, .kind = .path, .forms = "an NCCL library path without control characters, or blank for libnccl.so.2" },
.{ .name = "TF_CUDA_DEVICE", .scope = .cuda, .kind = .device, .trim = " ", .forms = "a whole GPU ordinal from 0 to 4294967295, or empty for the default" },
.{ .name = "TF_CUDA_SCHED", .scope = .cuda, .kind = .word, .trim = " \t", .words = &.{ "auto", "spin", "yield", "blocking" }, .forms = "auto, spin, yield or blocking (how the host waits for the GPU; unset keeps the driver's auto)" },
.{ .name = "TF_CUDA_SEGMENTS", .scope = .cuda, .kind = .segments, .trim = " ", .forms = "a whole number from 1 to 4, or empty for the default" },
.{ .name = "TF_CUDA_CARVEOUT", .scope = .cuda, .kind = .flag, .forms = "0 or 1" },
.{ .name = "TF_CUDA_CARVEOUT_MIB", .scope = .cuda, .kind = .mib, .forms = "a positive whole MiB count fitting a native byte size, or empty for 1792 MiB" },
Expand Down
5 changes: 5 additions & 0 deletions zig/src/platform/environment_valid_cases.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ pub const valid = [_]Case{
.{ .name = "TF_QWEN_KV_HOST_GIB", .raw = "8", .n = 8 },
.{ .name = "TF_CACHE_EVICT", .raw = "main", .n = 0 },
.{ .name = "TF_CACHE_EVICT", .raw = "decayed_resume", .n = 1 },
.{ .name = "TF_CUDA_SCHED", .raw = "auto", .n = 0 },
.{ .name = "TF_CUDA_SCHED", .raw = "spin", .n = 1 },
.{ .name = "TF_CUDA_SCHED", .raw = "yield", .n = 2 },
.{ .name = "TF_CUDA_SCHED", .raw = "blocking", .n = 3 },
.{ .name = "TF_CUDA_SCHED", .raw = " blocking\t", .n = 3 },
.{ .name = "TF_FLASHNEXT_BATCH_DRAWS", .raw = "0", .n = 0 },
.{ .name = "TF_FLASHNEXT_BATCH_DRAWS", .raw = "1", .n = 1 },
.{ .name = "TENSORFOLD_KOLIBRI_GRAPHS", .raw = "0", .n = 0 },
Expand Down
Loading