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 @@ -23,6 +23,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel
- Docs: the cache benchmark receipts say which tool-call content they count (#1999). Thanks to @garvrao80.

- `zig build test-info-config` runs info's issue-934 test again: it was built from the CUDA CLI root, which never reached it (#2006). Thanks to @jishnuvenugopal.
- Gemma 4, MiniMax M3, Kolibri-1 and Spark on CUDA prioritize verified root and context copies through their existing drafts switch, using the same four-token match policy as Nemotron (#1910).
- Gemma 4, MiniMax M3, Kolibri-1, Spark and Dory on CUDA can opt into verified root and context copies with a four-token match. Their existing suffix-copy policy remains the default until each checkpoint passes its device campaign (#1910).

- The Nemotron XPU loader and the XPU safetensors reader read shard headers through core, so a malformed or oversized header is refused before anything is read (#1919). Thanks to @jschmied.

Expand Down
11 changes: 11 additions & 0 deletions docs/recipes/cuda-lane-copies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Verified copies on CUDA lane hosts

Gemma 4, MiniMax M3, Kolibri-1, Spark and Dory can opt into the lane core's root and context copies with a four-token match, as Nemotron does. The existing verification widths and model arithmetic are unchanged. Copies come from request history, then the ordinary target verifies them; a mismatched token stops the accepted prefix.

The default retains each family's existing suffix-copy policy and eight-token match. Set the matching load-time option to 1 to try root and context copies: `TF_GEMMA4_COPIES`, `TF_MINIMAX_COPIES`, `TF_KOLIBRI_COPIES`, `TF_SPARK_COPIES` or `TF_DORY_COPIES`. Unset or 0 keeps the existing policy. Other values refuse startup by name. Keep MiniMax at its default policy until a runnable CUDA checkpoint has a qualifying cell.

`--no-drafts` and a request with `"draft": false` take plain rounds. Copies do not need a neural drafter. When a drafter is loaded, the core can prefer a matched copy before asking the head for more drafts. Head depth and target verification capacity remain separate; MiniMax's seven-draft DSpark proposal does not limit the target's variable-width copy windows.

`zig build test-family-copies-host` runs the real family facts through the lane planner with an instrumented target. It pins main's default and drafts-off facts, the four-token entry threshold, greedy/seeded plain equality, shared streams, resumed prefixes, a rejected copy, cancellation and the next request. These host fixtures do not establish whole-checkpoint exactness or speed.

Qualify each checkpoint with its CUDA contract and matched main, policy-off, policy-on and plain controls. Retain all repeats in fresh OFF/ON/ON/OFF runs for chat and code, greedy and seeded, one and eight streams. Every token must agree, and no cell may be slower than main or copies off beyond the repeat spread. A missing or slower cell keeps the existing default policy. No device gain is claimed for these opt-in policies.
1 change: 1 addition & 0 deletions zig/build/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
@import("rows_host.zig").tests(b, native_modules.api, step);
const native = native_modules.engines;
b.step("test-draft-cuda-host", "CUDA draft source metadata without devices").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"draft sources:"} })).step);
@import("cuda_family_host.zig").registerCopyFamilies(b, native, step);
const copies = b.step("test-nemotron-copies-host", "Nemotron CUDA copy gates and plain equality without devices");
for ([_]std.builtin.OptimizeMode{ .debug, .fast }) |mode| {
const rt = runtime(b, host, mode, &.{});
Expand Down
6 changes: 6 additions & 0 deletions zig/build/cuda_family_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,9 @@ pub fn steps(b: *std.Build, cuda: *std.Build.Module, mods: anytype, step: *std.B
live_run.has_side_effects = true;
b.step("test-qwen35-live-host", "Live CUDA launch buckets and startup warmups without devices").dependOn(&live_run.step);
}

pub fn registerCopyFamilies(b: *std.Build, native: *std.Build.Module, step: *std.Build.Step) void {
const family_copies = b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"CUDA lane copies:"} }));
b.step("test-family-copies-host", "Actual Gemma, MiniMax, Spark, Kolibri and Dory copy policies through the lane core").dependOn(&family_copies.step);
step.dependOn(&family_copies.step);
}
27 changes: 27 additions & 0 deletions zig/src/cuda/priority_copies.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! Load-time opt-in for unqualified root/context copies; suffix copies keep the existing lane facts.
const std = @import("std");

pub fn enabled(drafts: bool, value: ?[]const u8) !bool {
const v = value orelse return false;
if (std.mem.eql(u8, v, "0")) return false;
if (std.mem.eql(u8, v, "1")) return drafts;
return error.BadCopyPolicy;
}

pub fn read(name: [*:0]const u8, drafts: bool) !bool {
const value = if (std.c.getenv(name)) |v| std.mem.span(v) else null;
return enabled(drafts, value) catch |err| {
std.log.err("{s}: expected 0 or 1 for verified root/context copies", .{name});
return err;
};
}

test "CUDA lane copies: priority policy defaults off and respects the serving drafts switch" {
for ([_]bool{ false, true }) |drafts| {
try std.testing.expect(!try enabled(drafts, null));
try std.testing.expect(!try enabled(drafts, "0"));
try std.testing.expectEqual(drafts, try enabled(drafts, "1"));
for ([_][]const u8{ "", "2", "true", "-1", "1 " }) |bad|
try std.testing.expectError(error.BadCopyPolicy, enabled(drafts, bad));
}
}
2 changes: 2 additions & 0 deletions zig/src/cuda/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

pub const abi = @import("abi.zig");
pub const Driver = @import("driver.zig").Driver;
pub const priority_copies = @import("priority_copies.zig");
pub const copy_source = @import("copy_source.zig");
pub const completion = @import("completion.zig");
pub const paging_test_device = if (@import("builtin").is_test) @import("paging_test_device.zig") else struct {};
Expand Down Expand Up @@ -75,6 +76,7 @@ pub const pack = @import("pack.zig");
pub const graph_cache = @import("graph_cache.zig");

test {
_ = priority_copies;
_ = nccl;
_ = @import("p2p_nvml.zig");
_ = @import("p2p_probe.zig");
Expand Down
3 changes: 2 additions & 1 deletion zig/src/families/dory/cuda_backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn fits(physical: usize, prompt: usize, reply: usize) bool {
}

pub const Dory = struct {
priority_copies: bool = false,
gpa: std.mem.Allocator,
d: *const cuda.Driver,
m: *fwd.Model,
Expand Down Expand Up @@ -125,7 +126,7 @@ pub const Dory = struct {

/// The round loop's facts: no draft head; host copies verified in windows up to 16 rows, streams sharing a forward.
pub fn facts(x: *const Dory) lanes.Model {
return .{ .exact_width = max_window, .gpu_tokens = false, .mtp = false, .speculate = false, .hidden_rows = true, .batch_rows = round_rows, .max_streams = @intCast(@min(x.m.slotCap(), round_rows)) };
return .{ .root_copies = x.priority_copies, .context_copies = x.priority_copies, .copy_match = if (x.priority_copies) 4 else 8, .exact_width = max_window, .gpu_tokens = false, .mtp = false, .speculate = false, .hidden_rows = true, .batch_rows = round_rows, .max_streams = @intCast(@min(x.m.slotCap(), round_rows)) };
}

fn self(ptr: *anyopaque) *Dory {
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/dory/cuda_native.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub fn fits(p: *anyopaque, prompt: u32, max_tokens: u32) bool {

/// The checkpoint at its loop count, the shared kernels and every stream's caches.
pub fn open(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: []const u8, kernels: ?[]const u8, o: Options) !Loaded {
const priority_copies = try cuda.priority_copies.read("TF_DORY_COPIES", o.drafts);
if (kernels != null) return error.CapturedKernels;
if (o.lanes_fixed and o.lanes > be.round_rows / be.max_window) return error.ParallelPastRound;
if (o.loops) |l| if (l == 0 or l > config.max_loops) return error.BadLoops;
Expand Down Expand Up @@ -122,6 +123,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: [
errdefer own.m.deinit();
own.m.graphed = !envOff("TENSORFOLD_DORY_GRAPHS"); // =0: every decode forward eager
own.db = try be.Dory.init(gpa, ctx.d, &own.m, &own.k, own.s);
own.db.priority_copies = priority_copies;
if (own.m.paged) {
std.log.info("Dory at {d} loops (per_loop KV): {d} caches a stream, the full ones ({d}) in pages of {d} positions, CUDA graphs {s}", .{ own.w.config.loops, own.m.sched.caches, own.m.sched.cacheCount(.full), pages.tokens, if (own.m.graphed) "on" else "off" });
} else std.log.info("Dory at {d} loops (per_loop KV): {d} caches a stream, {d} streams, CUDA graphs {s}", .{ own.w.config.loops, own.m.sched.caches, n, if (own.m.graphed) "on" else "off" });
Expand Down
7 changes: 6 additions & 1 deletion zig/src/families/gemma4/cuda_backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub const Keeper = struct { ctx: *anyopaque, bytes: *const fn (*anyopaque, usize
const Tap = struct { row: usize = 0, start: u64 = 0, rows: usize = 0, gen: u64 = 0 };

pub const Gemma = struct {
priority_copies: bool = false,
gpa: std.mem.Allocator,
d: *const cuda.Driver,
m: *fwd.Model,
Expand Down Expand Up @@ -182,7 +183,7 @@ pub const Gemma = struct {

/// The round loop's facts: no draft head; host copies verified in windows up to 16 rows, streams sharing a forward.
pub fn facts(x: *const Gemma) lanes.Model {
return .{ .exact_width = max_window, .gpu_tokens = false, .mtp = false, .speculate = false, .hidden_rows = true, .batch_rows = round_rows, .max_streams = @intCast(@min(if (x.m.paged) round_rows else x.m.slots, round_rows)) };
return .{ .root_copies = x.priority_copies, .context_copies = x.priority_copies, .copy_match = if (x.priority_copies) 4 else 8, .exact_width = max_window, .gpu_tokens = false, .mtp = false, .speculate = false, .hidden_rows = true, .batch_rows = round_rows, .max_streams = @intCast(@min(if (x.m.paged) round_rows else x.m.slots, round_rows)) };
}

fn self(ptr: *anyopaque) *Gemma {
Expand Down Expand Up @@ -582,15 +583,19 @@ test "a greedy pick under a grammar: the first largest allowed token, refusals b
test "zero grant: Gemma CUDA assistant and DFlash preserve tokens through a long shared skip" {
var m: fwd.Model = undefined;
m.slots = 16;
m.paged = false;
var b: Gemma = undefined;
b.m = &m;
b.priority_copies = true;
try lanes.shared_draft_check.check(b.facts(), true);
}

test "shared head costs: Gemma CUDA assistant and DFlash retain costed copy grants" {
var m: fwd.Model = undefined;
m.slots = 16;
m.paged = false;
var b: Gemma = undefined;
b.m = &m;
b.priority_copies = true;
try @import("cuda_head_cost_test.zig").check(b.facts());
}
2 changes: 2 additions & 0 deletions zig/src/families/gemma4/cuda_native.zig
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ fn positions(context: usize) usize {
}

pub fn open(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: []const u8, kernels: ?[]const u8, o: Options) !Loaded {
const priority_copies = try cuda.priority_copies.read("TF_GEMMA4_COPIES", o.drafts);
if (kernels != null) return error.CapturedKernels;
if (o.lanes_fixed and o.lanes > be.round_rows) return error.ParallelPastRound;
// TF_KV_SHARE keeps prompts as the pool's pages, so it pages too; otherwise the default, unless turned off
Expand Down Expand Up @@ -157,6 +158,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: [
errdefer own.m.deinit();
own.m.graphed = !envOff("TENSORFOLD_GEMMA4_GRAPHS"); // =0: every decode forward eager
own.gb = try be.Gemma.init(gpa, ctx.d, &own.m, &own.k, own.s);
own.gb.priority_copies = priority_copies;
errdefer own.gb.deinit();
if (o.vision) |lim| {
const v = try gpa.create(cv.Vision);
Expand Down
3 changes: 2 additions & 1 deletion zig/src/families/kolibri1/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const Lane = struct {
};

pub const Kolibri = struct {
priority_copies: bool = false,
gpa: std.mem.Allocator,
d: *const cuda.Driver,
m: *fwd.Model,
Expand Down Expand Up @@ -101,7 +102,7 @@ pub const Kolibri = struct {

/// The round loop's facts: no draft head; host copies verified in windows up to 16 rows, streams sharing a forward.
pub fn facts(x: *const Kolibri) lanes.Model {
return .{ .exact_width = max_window, .gpu_tokens = false, .mtp = false, .speculate = false, .hidden_rows = true, .batch_rows = round_rows, .max_streams = @intCast(@min(x.m.slots, round_rows)) };
return .{ .root_copies = x.priority_copies, .context_copies = x.priority_copies, .copy_match = if (x.priority_copies) 4 else 8, .exact_width = max_window, .gpu_tokens = false, .mtp = false, .speculate = false, .hidden_rows = true, .batch_rows = round_rows, .max_streams = @intCast(@min(x.m.slots, round_rows)) };
}

fn envOff(name: [*:0]const u8) bool {
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/kolibri1/native.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn positions(context: usize) !usize {

/// The checkpoint, the shared kernels and every stream's caches.
pub fn open(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: []const u8, kernels: ?[]const u8, o: Options) !Loaded {
const priority_copies = try cuda.priority_copies.read("TF_KOLIBRI_COPIES", o.drafts);
if (kernels != null) return error.CapturedKernels;
if (o.lanes_fixed and o.lanes > be.round_rows) return error.ParallelPastRound; // before the weights load
const context = try positions(o.context);
Expand All @@ -101,6 +102,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: [
own.m = try fwd.Model.init(gpa, ctx.d, own.s, &own.w, &own.k, context, n, be.prompt_rows);
errdefer own.m.deinit();
own.kb = try be.Kolibri.init(gpa, ctx.d, &own.m, &own.k, own.s);
own.kb.priority_copies = priority_copies;
return .{
.backend = own.kb.backend(),
.facts = own.kb.facts(),
Expand Down
4 changes: 4 additions & 0 deletions zig/src/families/minimax_m3/cuda_lanes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const Lane = struct {
};

pub const Lanes = struct {
priority_copies: bool = false,
x: follow.Leader,
lanes: std.AutoHashMapUnmanaged(*const lanes.Stream, Lane) = .empty,
drawn: [ring]u32 = undefined,
Expand Down Expand Up @@ -87,6 +88,9 @@ pub const Lanes = struct {
pub fn facts(self: *const Lanes) lanes.Model {
const head = self.x.e.drafter != null;
return .{
.root_copies = self.priority_copies,
.context_copies = self.priority_copies,
.copy_match = if (self.priority_copies) 4 else 8,
.exact_width = max_rows,
.gpu_tokens = false,
.mtp = head,
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/minimax_m3/cuda_native.zig
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: [
}

fn openRank(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: []const u8, o: Options, p: Place) !Loaded {
const priority_copies = try cuda.priority_copies.read("TF_MINIMAX_COPIES", o.drafts);
const own = try gpa.create(Owned);
errdefer gpa.destroy(own);
own.* = .{ .gpa = gpa, .e = undefined, .lanes = undefined, .place = p };
Expand Down Expand Up @@ -217,6 +218,7 @@ fn openRank(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: [
}
if (own.link) |*l| l.asleep = cuda.tp_watch.open();
own.lanes = .{ .x = .{ .e = own.e, .link = if (own.link) |*l| l else null } };
own.lanes.priority_copies = priority_copies;
return .{
.backend = own.lanes.backend(),
.facts = own.lanes.facts(),
Expand Down
3 changes: 2 additions & 1 deletion zig/src/families/spark25/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fn pagesOf(positions: usize) usize {
}

pub const Spark = struct {
priority_copies: bool = false,
gpa: std.mem.Allocator,
d: *const cuda.Driver,
m: *fwd.Model,
Expand Down Expand Up @@ -132,7 +133,7 @@ pub const Spark = struct {

/// The round loop's facts: no draft head; host copies verified in windows up to 16 rows, streams sharing a forward.
pub fn facts(x: *const Spark) lanes.Model {
return .{ .exact_width = max_window, .gpu_tokens = false, .mtp = false, .speculate = false, .hidden_rows = true, .batch_rows = round_rows, .max_streams = @intCast(@min(x.m.slotCap(), round_rows)) };
return .{ .root_copies = x.priority_copies, .context_copies = x.priority_copies, .copy_match = if (x.priority_copies) 4 else 8, .exact_width = max_window, .gpu_tokens = false, .mtp = false, .speculate = false, .hidden_rows = true, .batch_rows = round_rows, .max_streams = @intCast(@min(x.m.slotCap(), round_rows)) };
}

fn self(ptr: *anyopaque) *Spark {
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/spark25/native.zig
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ fn positions(context: usize) usize {
}

pub fn open(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: []const u8, kernels: ?[]const u8, o: Options) !Loaded {
const priority_copies = try cuda.priority_copies.read("TF_SPARK_COPIES", o.drafts);
if (kernels != null) return error.CapturedKernels;
if (o.lanes_fixed and o.lanes > be.round_rows) return error.ParallelPastRound;
const own = try gpa.create(Owned);
Expand All @@ -120,6 +121,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, ctx: *const cuda.Context, dir: [
errdefer own.m.deinit();
own.m.graphed = !envOff("TENSORFOLD_SPARK_GRAPHS"); // =0: every decode forward eager
own.sb = try be.Spark.init(gpa, ctx.d, &own.m, &own.k, own.s);
own.sb.priority_copies = priority_copies;
errdefer own.sb.deinit();
return .{
.backend = own.sb.backend(),
Expand Down
Loading
Loading