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

- Nemotron 3.5 Bolt on CUDA: the shared expert runs on the side stream in decode windows, as on Super, so decode on a GB10 is 2.9-3.0% faster at one stream and 1.4-2.1% at four, with identical replies (#2847, for #2840).

- Prompt cache: `TF_CACHE_EVICT=decayed_resume` (off by default) gives a resumed snapshot bounded recency credit when the RAM store evicts, so agent conversations that keep returning outlive one-off prompts; through the real store at 16 GiB, agent traces rebuild 24-34% fewer prompt rows and at 4 GiB 4-18% fewer, no cell worse, replies unchanged (#3258, #3241).

- Qwen 3.5 on CUDA reserves its forward scratch at load in its own allocation, sized to `--context` (capped at the largest prompt pass) plus the verify window, instead of always reserving the largest pass inside the weights arena, and a pass past the reservation is refused before any kernel runs; with a short `--context` the model starts on a smaller GPU (the 4B in MLX 4-bit at `--context 1024` with a 0.5 GiB memory reserve starts on a 4 GiB RTX 3050 Laptop GPU where it was refused), and on a GB10 the 27B's tokens and speed match main's (#1879, refs #1827). Thanks to @robertosantosx2.
Expand Down
51 changes: 37 additions & 14 deletions zig/src/families/nemotron/cuda_forward.zig
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,30 @@ pub const Forward = struct {
const ex = m.experts;
const K = c.top_k;
const pairs = rows * K;
const tile: usize = if (prompt) cuda.experts.promptTile(ex.format) else 16;
const g = o.grouped();
// shared expert: one pair a row, its own act/out rows; a decode window forks it, joined before the add
const sh = lt.shared;
const in_window = !prompt and rows <= state.windowRows(c);
const sd = if (in_window) f.side else null;
const sh_act = if (in_window) b.sact else b.act;
const sh_out = if (in_window) b.sout else b.ymoe;
const sh_up: cuda.experts.Rows = .{ .x = b.y, .stride = c.hidden, .slots = 1 };
const sh_down: cuda.experts.Rows = .{ .x = sh_act, .stride = sh.width };
var sh_plan = b.plan;
if (in_window) {
sh_plan = b.latentPlan(c, rows);
if (sd) |side| {
try side.fork.record(o.s);
try side.s.wait(side.fork);
try g.decode(side.s, .up, sh_up, sh, sh_plan, sh_act, kern.maxItems(rows, 1, 16), rows, 16, -1);
try g.decode(side.s, .down_f32, sh_down, sh, sh_plan, sh_out, kern.maxItems(rows, 1, 16), rows, 16, -1);
try side.join.record(side.s);
}
}
try f.glue.route(b.y, m.router, m.bias, b.part, b.pick, b.wts, rows, c.hidden, ex.experts, K, K, c.routed_scaling, c.norm_topk);
try f.mark(.route);
if (prompt) try o.prefillDense(b.y, lt.fc1, b.lat, rows) else try o.dense(b.y, b.xs, lt.fc1, b.lat, rows);
const tile: usize = if (prompt) cuda.experts.promptTile(ex.format) else 16;
const g = o.grouped();
try o.plan(b.pick, pairs, ex.experts, tile, b.plan);
try f.mark(.plan);
var items = kern.maxItems(pairs, ex.experts, tile);
Expand All @@ -350,20 +369,24 @@ pub const Forward = struct {
}
try o.latentSum(b.ymoe, b.wts, b.comb, rows, K, c.latent);
if (prompt) try o.prefillDense(b.comb, lt.fc2, b.delta, rows) else try o.dense(b.comb, 0, lt.fc2, b.delta, rows);
// the shared expert: every row's pair with expert 0 of a one-expert layer, relu^2 between as the routed ones
const sh = lt.shared;
try o.plan(b.sids, rows, 1, tile, b.plan);
items = kern.maxItems(rows, 1, tile);
const sh_up: cuda.experts.Rows = .{ .x = b.y, .stride = c.hidden, .slots = 1 };
const sh_down: cuda.experts.Rows = .{ .x = b.act, .stride = sh.width };
if (prompt) {
try g.prompt(o.s, .up, sh_up, sh, b.plan, b.act, items, rows, tile, -1);
try g.prompt(o.s, .down_f32, sh_down, sh, b.plan, b.ymoe, items, rows, tile, -1);
if (in_window) {
if (sd == null) {
try g.decode(o.s, .up, sh_up, sh, sh_plan, sh_act, kern.maxItems(rows, 1, 16), rows, 16, -1);
try g.decode(o.s, .down_f32, sh_down, sh, sh_plan, sh_out, kern.maxItems(rows, 1, 16), rows, 16, -1);
}
} else {
try g.decode(o.s, .up, sh_up, sh, b.plan, b.act, items, rows, tile, -1);
try g.decode(o.s, .down_f32, sh_down, sh, b.plan, b.ymoe, items, rows, tile, -1);
try o.plan(b.sids, rows, 1, tile, b.plan);
items = kern.maxItems(rows, 1, tile);
if (prompt) {
try g.prompt(o.s, .up, sh_up, sh, b.plan, sh_act, items, rows, tile, -1);
try g.prompt(o.s, .down_f32, sh_down, sh, b.plan, sh_out, items, rows, tile, -1);
} else {
try g.decode(o.s, .up, sh_up, sh, b.plan, sh_act, items, rows, tile, -1);
try g.decode(o.s, .down_f32, sh_down, sh, b.plan, sh_out, items, rows, tile, -1);
}
}
try o.addBf16(b.delta, b.delta, b.ymoe, true, rows * c.hidden);
if (sd) |side| try o.s.wait(side.join);
try o.addBf16(b.delta, b.delta, sh_out, true, rows * c.hidden);
try f.mark(.shared_wait);
}

Expand Down
73 changes: 51 additions & 22 deletions zig/src/families/nemotron/cuda_state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ fn lanePartBytes(c: Config) usize {
}
return most;
}
const scratch_count = 47;
const scratch_count = 48;

/// Bytes of the shared expert's fixed plans (Buffers.shared): shared_words, or a latent MoE's plan for every row count.
/// Bytes of the fixed shared plans: shared_words, or a latent MoE's (Bolt's too) plan for every row count.
fn sharedBytes(c: Config) usize {
return (if (c.latent > 0 and !c.bolt) windowRows(c) * Buffers.latentPlanWords(c) else shared_words) * 4;
return (if (c.latent > 0) windowRows(c) * Buffers.latentPlanWords(c) else shared_words) * 4;
}

/// Scratch windows and chunks share on one stream: window logits, chunk io, activations, the expert plan.
Expand All @@ -132,6 +132,9 @@ fn scratchSizes(c: Config, nch: usize) [scratch_count]usize {
const ck4 = if (c.ck and lat) @import("cuda_latent_ck.zig").rowsBytes(c, R) else 0;
// its passes of prompt_tokens rows or more write up's rows to ck4: bf16 activations only for the calls under them
const act_rows: usize = if (c.ck and lat) cuda.ck.prompt_tokens - 1 else R;
// Bolt's shared expert runs beside the routed pairs: its own bf16 activations and fp32 rows, a window wide
const sact = if (c.bolt) W * c.shared_width * 2 else act_rows * own * sw * 2;
const sout = if (c.bolt) W * D * 4 else 0;
return .{
W * c.vocab * 2, 16, R * 4,
R * D * 2, c.vocab * 2, 16,
Expand All @@ -145,10 +148,10 @@ fn scratchSizes(c: Config, nch: usize) [scratch_count]usize {
pairs * own * 4, items * 12, 8,
pairs * own * 4, (pairs * own + 1023) / 1024 * plan_experts * 4, act_rows * ns * c.expert_width * 2,
ymoe, sharedBytes(c), R * kern.pocket_width * 4,
R * c.latent * 2, R * c.latent * 2, act_rows * own * sw * 2,
R * own * 4, job, ck8,
ck4, lanePartBytes(c), W * 32,
W * 4, W * 4,
R * c.latent * 2, R * c.latent * 2, sact,
sout, R * own * 4, job,
ck8, ck4, lanePartBytes(c),
W * 32, W * 4, W * 4,
};
}

Expand Down Expand Up @@ -294,6 +297,7 @@ pub const Buffers = struct {
lat: u64, // a latent MoE's rows in the latent width (bf16)
comb: u64, // its routed pairs' latent sum a row (bf16)
sact: u64, // its shared expert's activations (bf16)
sout: u64, // a Bolt shared expert's fp32 rows, apart from the routed pairs it runs beside
sids: u64, // its shared expert's pair ids, share i % own (cuda_latent.initShareIds)
job: u64, // rank 0's job to the other ranks: a header and the rows their shares read (cuda_worker.zig)
ck8: u64, // the checkpoint's math: e4m3 rows an FP8 projection reads
Expand Down Expand Up @@ -347,11 +351,11 @@ pub const Buffers = struct {
/// Every scratch field, in scratchSizes' order.
fn scratchPtrs(b: *Buffers) [scratch_count]*u64 {
return .{
&b.logits, &b.p_meta, &b.p_ids, &b.p_hidden, &b.p_logits, &b.p_sampled, &b.emb, &b.h[0], &b.h[1], &b.y,
&b.xs, &b.delta, &b.proj, &b.p_xc, &b.sy, &b.g, &b.gxs, &b.qkv, &b.q, &b.att,
&b.axs, &b.po, &b.pm, &b.pl, &b.part, &b.pick, &b.wts, &b.plan.members, &b.plan.items, &b.plan.counts,
&b.plan.rank, &b.plan.hist, &b.act, &b.ymoe, &b.shared, &b.pocket, &b.lat, &b.comb, &b.sact, &b.sids,
&b.job, &b.ck8, &b.ck4, &b.lane_part, &b.draw_rules, &b.draw_meta, &b.draw_tokens,
&b.logits, &b.p_meta, &b.p_ids, &b.p_hidden, &b.p_logits, &b.p_sampled, &b.emb, &b.h[0], &b.h[1], &b.y,
&b.xs, &b.delta, &b.proj, &b.p_xc, &b.sy, &b.g, &b.gxs, &b.qkv, &b.q, &b.att,
&b.axs, &b.po, &b.pm, &b.pl, &b.part, &b.pick, &b.wts, &b.plan.members, &b.plan.items, &b.plan.counts,
&b.plan.rank, &b.plan.hist, &b.act, &b.ymoe, &b.shared, &b.pocket, &b.lat, &b.comb, &b.sact, &b.sout,
&b.sids, &b.job, &b.ck8, &b.ck4, &b.lane_part, &b.draw_rules, &b.draw_meta, &b.draw_tokens,
};
}

Expand All @@ -366,13 +370,18 @@ pub const Buffers = struct {
return latentPlanAt(c).counts + 2;
}

/// A shared expert's pairs a row: a latent MoE's row holds every share, Bolt's one pair with its one expert.
fn sharedPairs(c: Config) usize {
return if (c.latent > 0 and !c.bolt) c.ownShares() else 1;
}

/// Where a latent shared plan's items and counts start, in words from its members.
fn latentPlanAt(c: Config) struct { items: usize, counts: usize } {
const pairs = windowRows(c) * c.ownShares();
return .{ .items = pairs, .counts = pairs + 3 * kern.maxItems(pairs, c.ownShares(), 16) };
const pairs = windowRows(c) * sharedPairs(c);
return .{ .items = pairs, .counts = pairs + 3 * kern.maxItems(pairs, sharedPairs(c), 16) };
}

/// A latent MoE's shared plan at `rows` rows (1 to max_rows) as initShared wrote it; rank and hist are b.plan's.
/// A latent MoE's (or Bolt's) shared plan at `rows` rows (1 to windowRows), as initShared wrote it.
pub fn latentPlan(b: *const Buffers, c: Config, rows: usize) kern.Plan {
const at = latentPlanAt(c);
const members = b.shared + (rows - 1) * latentPlanWords(c) * 4;
Expand All @@ -381,9 +390,12 @@ pub const Buffers = struct {

/// Writes every row count's shared plan: a latent MoE's (row, share) pairs by Ops.plan, else pairs in row order.
pub fn initShared(b: *const Buffers, ops: kern.Ops, c: Config) !void {
if (c.latent > 0 and !c.bolt) {
try latent.initShareIds(ops.k.latent.?, ops.s, b.sids, pass_rows, c.ownShares());
for (1..windowRows(c) + 1) |rows| try ops.plan(b.sids, rows * c.ownShares(), c.ownShares(), 16, b.latentPlan(c, rows));
if (c.latent > 0) {
if (c.bolt) {
const z: [pass_rows]u32 = @splat(0);
try ops.upload(b.sids, std.mem.sliceAsBytes(&z));
} else try latent.initShareIds(ops.k.latent.?, ops.s, b.sids, pass_rows, c.ownShares());
for (1..windowRows(c) + 1) |rows| try ops.plan(b.sids, rows * sharedPairs(c), sharedPairs(c), 16, b.latentPlan(c, rows));
return ops.s.synchronize();
}
var w: [shared_words]u32 = undefined;
Expand All @@ -397,10 +409,6 @@ pub const Buffers = struct {
w[at..][0..8].* = .{ @intCast(c.experts), 0, n, @intCast(c.experts + 1), shared_rows, n, 2, 2 };
}
try ops.upload(b.shared, std.mem.sliceAsBytes(&w));
if (c.latent > 0) {
const z: [pass_rows]u32 = @splat(0);
try ops.upload(b.sids, std.mem.sliceAsBytes(&z));
}
try ops.s.synchronize();
}

Expand Down Expand Up @@ -474,3 +482,24 @@ test "each row count's latent shared plan holds its pairs and items, apart from
}
}
}

test "Bolt's shared slot holds a plan for every window row count" {
var c: Config = undefined;
c.latent = 1024;
c.bolt = true;
c.shares = 2;
c.world = 1;
var b: Buffers = undefined;
b.shared = 0;
b.plan.rank = 0;
b.plan.hist = 0;
const W = windowRows(c);
try std.testing.expect(sharedBytes(c) >= W * Buffers.latentPlanWords(c) * 4); // main sizes Bolt as shared_words
for (1..W + 1) |rows| {
const p = b.latentPlan(c, rows);
try std.testing.expect(p.items - p.members >= rows * 4); // one pair a row
try std.testing.expect(p.counts - p.items >= 3 * kern.maxItems(rows, 1, 16) * 4);
const end = p.counts + 8;
try std.testing.expect(if (rows < W) end <= b.latentPlan(c, rows + 1).members else end <= sharedBytes(c));
}
}
29 changes: 21 additions & 8 deletions zig/tests/cuda/shared_fork.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ const check = @import("check.zig");
const hold_bytes = 256 << 20;
const shared_streams = [_]usize{ 2, 4, 8 };

/// MODEL in both precisions: every latent MoE layer at 1 to max_rows rows, forked against one stream, byte for byte.
/// MODEL in both precisions where built: each latent MoE layer at 1 to windowRows rows, forked against one stream.
pub fn run(gpu: check.Gpu, model: []const u8) !void {
for ([_]nemotron.Precision{ .checkpoint, .full }) |p| try layers(gpu, model, p);
}

/// Each layer, lone at every width and shared by 2, 4 and 8 streams, side stream held back, against one stream.
fn layers(gpu: check.Gpu, model: []const u8, precision: nemotron.Precision) !void {
const gpa = gpu.gpa;
const e = try nemotron.Engine.init(gpa, gpu.io, gpu.ctx, model, null, .{ .context = 4096, .graphs = false, .precision = precision });
const e = nemotron.Engine.init(gpa, gpu.io, gpu.ctx, model, null, .{ .context = 4096, .graphs = false, .precision = precision }) catch |err| switch (err) {
error.PrecisionNotBuilt => return, // Bolt has no checkpoint-math shared expert; its full path runs below
else => return err,
};
defer e.deinit();
const c = e.c;
var plain = e.forward(null);
Expand All @@ -25,8 +28,8 @@ fn layers(gpu: check.Gpu, model: []const u8, precision: nemotron.Precision) !voi
const side = forked.side orelse return error.NoSideStream;
var moes: std.ArrayList(nemotron.weights.MoE) = .empty;
defer moes.deinit(gpa);
for (e.w.blocks) |blk| if (blk.kind == .moe) try moes.append(gpa, blk.moe);
if (e.w.mtp) |m| try moes.append(gpa, m.moe);
for (e.w.blocks) |blk| if (blk.kind == .moe and (blk.moe.latent != null or blk.moe.bolt != null)) try moes.append(gpa, blk.moe);
if (e.w.mtp) |m| if (m.moe.latent != null or m.moe.bolt != null) try moes.append(gpa, m.moe);
var hold = try cuda.DeviceBuffer.alloc(e.ctx.d, hold_bytes);
defer hold.free();
const most = nemotron.state.windowRows(c);
Expand Down Expand Up @@ -85,15 +88,25 @@ const Trial = struct {
for (t.x[0 .. rows * c.hidden]) |*v| v.* = @truncate(@as(u32, @bitCast((t.rng.float(f32) - 0.5) * 4.0)) >> 16);
try e.ops().upload(b.y, std.mem.sliceAsBytes(t.x[0 .. rows * c.hidden]));
const len = rows * c.hidden * 2;
try nemotron.latent.run(t.plain, moe, rows);
try pass(t.plain, moe, rows);
try e.ops().download(t.want[0..len], b.delta);
// the one-stream pass left the right shared rows behind: a sum that skips the join must read NaN instead
const shared_rows = b.ymoe + @as(u64, rows * c.top_k * c.ownShares()) * c.latent * 4;
try e.ops().fill32(shared_rows, 0x7fc00000, rows * c.ownShares() * c.hidden);
if (moe.bolt != null) {
try e.ops().fill32(b.sout, 0x7fc00000, rows * c.hidden);
} else {
const shared_rows = b.ymoe + @as(u64, rows * c.top_k * c.ownShares()) * c.latent * 4;
try e.ops().fill32(shared_rows, 0x7fc00000, rows * c.ownShares() * c.hidden);
}
for (0..8) |_| try t.held.copy(t.hold + hold_bytes / 2, t.hold, hold_bytes / 2);
try nemotron.latent.run(forked, moe, rows);
try pass(forked, moe, rows);
try e.ops().download(t.got[0..len], b.delta);
try e.stream.synchronize();
try check.sameBytes(what, t.got[0..len], t.want[0..len]);
}
};

/// A layer's experts: Bolt's latentExperts, else the generic latent MoE's shared expert on its own plan.
fn pass(f: *const nemotron.Forward, moe: nemotron.weights.MoE, rows: usize) !void {
if (moe.bolt != null) return f.experts(moe, rows, false);
return nemotron.latent.run(f, moe, rows);
}
Loading