Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
db452fa
Flash Next: config-driven top-k and shared expert width
BadAd84 Nov 14, 2026
6812ba9
tests: re-pin the serving sources this PR changes on today's main (10…
BadAd84 Nov 15, 2026
c7415fb
CHANGELOG: name the pull request in the config-driven widths entry
BadAd84 Nov 16, 2026
a064f46
Flash Next: drop the scratch's unread shared_w field
ashhart Nov 17, 2026
22b9ece
Flash Next: the MTP head takes the engine's widths, and the split com…
BadAd84 Nov 17, 2026
e84fdf7
Flash Next widths: pin sources merged with current main
ashhart Nov 20, 2026
4a65f31
Flash Next: block-FP8 projections load on the FP8G lane
BadAd84 Nov 21, 2026
bd78c69
Flash Next: the block-FP8 reader's refusal reason is a parameter
BadAd84 Nov 21, 2026
61392bb
Flash Next: the block-FP8 loader leans on the shared reader's refusals
BadAd84 Nov 21, 2026
1cd3331
Flash Next: the MXFP8 fixture answers the selector by name
BadAd84 Nov 21, 2026
42ff781
Flash Next: a block-FP8 projection's BF16 tail may end mid-block
BadAd84 Nov 21, 2026
a596160
Flash Next: config-driven top-k and shared expert width
BadAd84 Nov 14, 2026
74daf2e
tests: re-pin the serving sources this PR changes on today's main (10…
BadAd84 Nov 15, 2026
fa3bd7e
Flash Next: drop the scratch's unread shared_w field
ashhart Nov 17, 2026
f583c51
Flash Next: the MTP head takes the engine's widths, and the split com…
BadAd84 Nov 17, 2026
bbb526c
tests: re-pin merged Flash Next serving sources
ashhart Nov 23, 2026
2b64706
Merge #3523 head bbb526c112d6 into flashnext-block-fp8-projections-up2
BadAd84 Nov 23, 2026
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
9 changes: 9 additions & 0 deletions docs/recipes/flash-next-cuda.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ contract receipts. Compile success and host tests do not establish checkpoint fi
limits must be measured with the exact checkpoint, binary, device topology and request settings. Prefix retention
with MTP is outside this path's qualified scope; evaluate with `--prompt-cache-gib 0`.

A projection whose parts are block FP8 (e4m3 codes with an fp32 `weight_scale_inv` a 128 x 128 block) loads as stored
on the FP8G lane: its leading parts stack row for row into one packed weight whose rows past them are zero codes with
a scale of one, and a trailing BF16 part, such as an indexer projection stored beside block-FP8 q, k and v or the
DeltaNet in-projection's 48-row `in_proj_b` and `in_proj_a` beside its block-FP8 qkv and z, is held as the exact rows
a second BF16 matmul computes and copied into their columns after the lane's. The tail's rows need not fill a 128-row
block: a tail row is a zero code on the lane, so its product there is a zero that the copy replaces, and the
packers zero and one-fill the rows past the projection's own. A projection storing an MXFP8 `weight_scale` or affine
words is not this arm and takes the reader it took before.

### NVIDIA's NVFP4 checkpoint on two GB10s

`nvidia/Qwen3.8-Flash-Next-NVFP4` (the checkpoint SGLang's Flash Next recipes serve) loads as stored: NVFP4 routed
Expand Down
3 changes: 3 additions & 0 deletions zig/build/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/src/families/moss_image/cuda.zig"), .target = host, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "core", .module = mods.core } } }) })).step);
@import("flashnext_glue_host.zig").steps(b, cuda, mods.core, flashnextModule(b, host, .debug, cuda, mods.core, mods.lanes, mods.qwen_vision, mods.api), step);
@import("flashnext_scoring_host.zig").steps(b, cuda, mods.core, mods.lanes, flashnextModule(b, host, .debug, cuda, mods.core, mods.lanes, mods.qwen_vision, mods.api), step);
const block_fp8 = b.addRunArtifact(b.addTest(.{ .root_module = flashnextModule(b, host, .debug, cuda, mods.core, mods.lanes, mods.qwen_vision, mods.api), .filters = &.{"block FP8:"} }));
b.step("test-flashnext-block-fp8-host", "Block-FP8 projections and their BF16 tail on the FP8G lane, without a device").dependOn(&block_fp8.step);
step.dependOn(&block_fp8.step);
b.step("test-pocket-device-host", "Actual pocket device hooks under CUDA ABI failures").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = flashnextModule(b, host, .debug, cuda, mods.core, mods.lanes, mods.qwen_vision, mods.api), .filters = &.{"Learning persistence:"} })).step);
b.step("test-flashnext-readout-fused-host", "The fused read-out's down tile choice and its refusals by name, without a GPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = flashnextModule(b, host, .debug, cuda, mods.core, mods.lanes, mods.qwen_vision, mods.api), .filters = &.{"fused read-out:"} })).step);
b.step("test-living-host", "Family-neutral learner, saved form and commit gate on host").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mods.living })).step);
Expand Down
24 changes: 16 additions & 8 deletions zig/src/families/flashnext/cuda_batch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const low = wt.low;
const eps = wt.eps;
const vocab = wt.vocab;
const experts_n = wt.experts_n;
const slots = wt.slots;
const moe_w = wt.moe_w;
const tile = wt.tile;
const head_dim = wt.head_dim;
Expand Down Expand Up @@ -129,6 +128,15 @@ pub fn Pass(comptime Tri: type) type {

/// qmm.cu's arithmetic over m rows into out (bf16) or part (fp32 K slices); returns the slices (1 when folded).
pub fn lane(self: *Self, x: u64, xs: u64, w: Q4, out: u64, part: u64, m: usize, sk: usize) !usize {
if (w.tail) |t| {
// the FP8G rows over the whole output, its zero rows where the tail goes, then the tail copied in
if (sk != 1) return error.UnexpectedTensor;
const used = try tiles.matmul(self.k, self.stream, x, xs, w, out, part, self.s.native_part, m, sk);
if (used != 1) return error.UnexpectedTensor;
try self.k.nativeMatmul(self.stream, x, w.k, m, t.native, self.s.tail, self.s.native_part, .bf16);
try ops.copyCols(self.k, self.stream, self.s.tail, t.n, out + t.at * 2, w.n, t.n, m);
return 1;
}
if (try zs.lane(self.k, self.stream, self.s, self.zero_slices, x, w, part, m, sk)) |one| return one;
return tiles.matmul(self.k, self.stream, x, xs, w, out, part, self.s.native_part, m, sk);
}
Expand Down Expand Up @@ -180,9 +188,9 @@ pub fn Pass(comptime Tri: type) type {
try self.tri.hcWriteback(h, h, pss, s.moe_f32 + r0 * dims * 4, "*fp32", inj, dummy, "*bf16", dummy, "*bf16", rs, n, dims, streams, 4, 1, 1, 1);
return;
}
const y = s.y + r0 * slots * dims * 4;
const wts = s.wts + r0 * slots * 4;
try self.tri.hcWriteback(h, h, pss, dummy, "*bf16", inj, y, "*fp32", wts, "*fp32", rs, n, dims, streams, 2, 10, 11, 1);
const y = s.y + r0 * s.slots * dims * 4;
const wts = s.wts + r0 * s.slots * 4;
try self.tri.hcWriteback(h, h, pss, dummy, "*bf16", inj, y, "*fp32", wts, "*fp32", rs, n, dims, streams, 2, s.slots - 1, s.slots, 1);
}

/// A read-out over rows [r0, r0 + n) of the streams: mixed and its group sums, and the inject gates.
Expand Down Expand Up @@ -441,10 +449,10 @@ pub fn Pass(comptime Tri: type) type {
pub fn moeBlockKeep(self: *Self, lw: *const Layer, keep: LayerKeep) !void {
const s = self.s;
const n = self.rows;
const pairs = n * slots;
const pairs = n * s.slots;
try self.copy(keep.moe_x, s.mixed, n * dims * 2);
try self.tri.routerMany(s.mixed, lw.router.ptr, s.router_l, dims, n);
try self.tri.topkMany(s.router_l, s.picks, s.wts, n);
try self.tri.topkMany(s.router_l, s.picks, s.wts, n, s.slots);
try self.copy(keep.moe_ids, s.picks, pairs * 4);
try self.copy(keep.moe_wts, s.wts, pairs * 4);
const e = experts_n + 1;
Expand All @@ -454,13 +462,13 @@ pub fn Pass(comptime Tri: type) type {
// prompt rows on 64-pair factored tiles, expert_rows' bits
try self.k.router.route(self.stream, s.picks, pairs, e, tk.fexp_tile, plan);
const items64 = cdiv(pairs, tk.fexp_tile) + e;
try tiles.experts(self.stream, self.k.tiles.fexp_up, 2, s.mixed, dims, slots, lw.experts_up.ptr, dims / 32, moe_w / 32, plan, s.moe_act, moe_w, items64);
try tiles.experts(self.stream, self.k.tiles.fexp_up, 2, s.mixed, dims, s.slots, lw.experts_up.ptr, dims / 32, moe_w / 32, plan, s.moe_act, moe_w, items64);
try tiles.experts(self.stream, self.k.tiles.fexp_down, 4, s.moe_act, moe_w, 0, lw.experts_down.ptr, moe_w / 32, dims / 32, plan, s.y, dims, items64);
return;
}
try self.k.router.route(self.stream, s.picks, pairs, e, rows_tile, plan);
const items = cdiv(pairs, rows_tile) + e;
try self.k.moe.run(self.stream, true, s.mixed, dims, slots, lw.experts_up.ptr, dims / 32, moe_w / 32, plan, s.moe_act, moe_w, items);
try self.k.moe.run(self.stream, true, s.mixed, dims, s.slots, lw.experts_up.ptr, dims / 32, moe_w / 32, plan, s.moe_act, moe_w, items);
try self.k.moe.run(self.stream, false, s.moe_act, moe_w, 0, lw.experts_down.ptr, moe_w / 32, dims / 32, plan, s.y, dims, items);
}

Expand Down
7 changes: 3 additions & 4 deletions zig/src/families/flashnext/cuda_batch_hc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const dims = wt.dims;
const streams = wt.streams;
const wide = wt.wide;
const low = wt.low;
const slots = wt.slots;
const eps = wt.eps;
const pss_n = (dims / 256) * streams;

Expand Down Expand Up @@ -45,9 +44,9 @@ pub fn writebackNorm(pass: anytype, hc_w: wt.HcW, r0: usize, n: usize) !void {
pass.pending_f32 = false;
return pass.tri.hcWritebackNorm(h, h, pss, s.moe_f32 + r0 * dims * 4, inj, dummy, dummy, scale, norm, eps, rs, n, dims, streams, 4, 1, 1, 1);
}
const y = s.y + r0 * slots * dims * 4;
const wts = s.wts + r0 * slots * 4;
try pass.tri.hcWritebackNorm(h, h, pss, dummy, inj, y, wts, scale, norm, eps, rs, n, dims, streams, 2, 10, 11, 1);
const y = s.y + r0 * s.slots * dims * 4;
const wts = s.wts + r0 * s.slots * 4;
try pass.tri.hcWritebackNorm(h, h, pss, dummy, inj, y, wts, scale, norm, eps, rs, n, dims, streams, 2, s.slots - 1, s.slots, 1);
}

/// A native read-out over rows [r0, r0 + n) whose normed rows are in: down, act and gates, up, the mix.
Expand Down
5 changes: 2 additions & 3 deletions zig/src/families/flashnext/cuda_batch_head.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const HcKeep = batch.HcKeep;
const dims = wt.dims;
const wide = wt.wide;
const streams = wt.streams;
const slots = wt.slots;
const vocab = wt.vocab;
const pss_n = batch.pss_n;
pub fn head(self: anytype, logits_host: ?[]u8) !void {
Expand Down Expand Up @@ -59,8 +58,8 @@ pub fn head(self: anytype, logits_host: ?[]u8) !void {
try self.copy(s.h, s.h + r * wide * 2, wide * 2);
try self.copy(s.pss, s.pss + r * pss_n * 4, pss_n * 4);
try self.copy(s.inj, s.inj + r * streams * 2, streams * 2);
try self.copy(s.y, s.y + r * slots * dims * 4, slots * dims * 4);
try self.copy(s.wts, s.wts + r * slots * 4, slots * 4);
try self.copy(s.y, s.y + r * s.slots * dims * 4, s.slots * dims * 4);
try self.copy(s.wts, s.wts + r * s.slots * 4, s.slots * 4);
if (self.pending_f32) try self.copy(s.moe_f32, s.moe_f32 + r * dims * 4, dims * 4);
}
if (self.hooks.streams == 0) try self.writeback(0, 1);
Expand Down
7 changes: 3 additions & 4 deletions zig/src/families/flashnext/cuda_batch_moe.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const wt = @import("cuda_prompt_wt.zig");
const batch = @import("cuda_batch.zig");
const LayerKeep = batch.LayerKeep;
const dims = wt.dims;
const slots = wt.slots;

/// After a layer's experts, when something adds to or keeps its output: the slots' fp32 sum, adds, kept branch.
pub fn moeAdds(self: anytype, layer: usize, keep: LayerKeep) !void {
Expand All @@ -23,10 +22,10 @@ pub fn moeAdds(self: anytype, layer: usize, keep: LayerKeep) !void {
pub fn sumSlots(self: anytype, r0: usize, rows: usize) !void {
const s = self.s;
var a: cuda.Args = .{};
a.add(s.y + r0 * slots * dims * 4);
a.add(s.wts + r0 * slots * 4);
a.add(s.y + r0 * s.slots * dims * 4);
a.add(s.wts + r0 * s.slots * 4);
a.add(s.moe_f32 + r0 * dims * 4);
a.add(batch.cint(dims));
a.add(batch.cint(slots));
a.add(batch.cint(s.slots));
try cuda.launch.launch(self.k.moe_sum, .{ .grid = .{ .x = @intCast(rows), .y = dims / 256 }, .block = .{ .x = 256 } }, self.stream, &a);
}
12 changes: 5 additions & 7 deletions zig/src/families/flashnext/cuda_batch_side.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const cuda = @import("cuda");
const wt = @import("cuda_prompt_wt.zig");

const dims = wt.dims;
const slots = wt.slots;
const moe_w = wt.moe_w;

/// On by default: its slot rows of y are apart from the routed experts', so the bits match; =0 runs it after them.
pub fn on() bool {
Expand Down Expand Up @@ -40,19 +38,19 @@ pub fn run(pass: anytype, st: cuda.Stream, native: anytype, n: usize) !void {
const glue = pass.tri.own orelse return error.NativeGlueRequired;
try pass.k.nativeMatmul(st, s.mixed, dims, n, native.shared[0].native.?, s.shared_gate, s.native_part, .bf16);
try pass.k.nativeMatmul(st, s.mixed, dims, n, native.shared[1].native.?, s.shared_up, s.native_part, .bf16);
try glue.sharedSwiGLU(st, s.shared_gate, s.shared_up, s.shared_act, n, moe_w);
try glue.sharedSwiGLU(st, s.shared_gate, s.shared_up, s.shared_act, n, native.shared_width);
try down(pass, st, s.shared_act, native, n);
}

/// The whole shared down into y's slot at any eligible rows (faster than the prompt tile and scatter); else scatter.
pub fn down(pass: anytype, st: cuda.Stream, input: u64, native: anytype, n: usize) !void {
const s = pass.s;
const w = native.shared[2].native.?;
if (w.n == dims and cuda.qmmf_shared.eligible(pass.k.native_lane, n, w, moe_w, slots * dims))
return pass.k.native_lane.matmulF32Strided(st, input, moe_w, n, w, s.y + (slots - 1) * dims * 4, slots * dims);
if (w.n == dims and cuda.qmmf_shared.eligible(pass.k.native_lane, n, w, native.shared_width, s.slots * dims))
return pass.k.native_lane.matmulF32Strided(st, input, native.shared_width, n, w, s.y + (s.slots - 1) * dims * 4, s.slots * dims);
const glue = pass.tri.own orelse return error.NativeGlueRequired;
try pass.k.nativeMatmul(st, input, moe_w, n, w, s.shared_out, s.native_part, .f32);
try glue.sharedScatter(st, s.shared_out, s.y, n, dims, slots);
try pass.k.nativeMatmul(st, input, native.shared_width, n, w, s.shared_out, s.native_part, .f32);
try glue.sharedScatter(st, s.shared_out, s.y, n, dims, s.slots);
}

/// After y is cleared: the shared expert forked onto the side stream (one GPU, not a prompt fill), or null.
Expand Down
22 changes: 13 additions & 9 deletions zig/src/families/flashnext/cuda_batch_state.zig
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ const partRows = sizes.partRows;
/// Device scratch for chunks of up to `cap` rows.
pub const Scratch = struct {
cap: usize,
/// A row's expert slots: the checkpoint's top-k and the shared one.
slots: usize = slots,
/// The shared expert's stream (cuda_batch_side.zig); null: it runs after the routed experts on the pass's stream.
side: ?shared.Side = null,
bufs: std.ArrayList(cuda.DeviceBuffer) = .empty,
Expand Down Expand Up @@ -301,6 +303,7 @@ pub const Scratch = struct {
shared_up: u64 = 0,
shared_act: u64 = 0,
shared_out: u64 = 0,
tail: u64 = 0, // a block-FP8 projection's BF16 tail rows [cap, tail_n] before they are copied into place
y: u64 = 0,
branch: u64 = 0,
/// A layer's MoE output in fp32 [cap + 1, dims] when something adds to it, and the pocket's activations [cap, 128].
Expand Down Expand Up @@ -379,7 +382,7 @@ pub const Scratch = struct {
/// Bytes `alloc(cap)` takes on the device.
pub fn bytes(cap: usize, z: Sizes) usize {
const head_rows = z.head_rows;
const pairs = cap * slots;
const pairs = cap * z.slots;
const nblk = cdiv(pairs, plan_small);
const items = cdiv(pairs, tile) + experts_n + 1;
var n: usize = 0;
Expand All @@ -393,8 +396,8 @@ pub const Scratch = struct {
n += att_rows * nch * q_heads * (head_dim + 2) * 4 + cap * out_k * 2 * 2 + cap * (out_k / 32) * 4;
n += fused_rows * (idw + 2) * 4 + lanes_bytes;
n += cap * (experts_n + 1) * 4 + pairs * 4 * 2 + pairs * 4 + items * 3 * 4 + 8 + pairs * 4 + nblk * (experts_n + 1) * 4 + pairs * 4;
n += cap * moe_w * 6 + cap * dims * 4;
n += pairs * moe_w * 2 + (pairs + slots) * dims * 4 + cap * dims * 2;
n += cap * z.shared * 6 + cap * dims * 4 + cap * wt.tail_n * 2;
n += pairs * moe_w * 2 + (pairs + z.slots) * dims * 4 + cap * dims * 2;
n += cap * dims * 2 + cap * (dims / 32) * 4 + cap * wide * 2 + cap * dims * 2 + 4 * cap * dims * 4;
n += cap * wide * 2 * 2 + cap * streams * 4 + vocab * 2 + head_rows * vocab + half * 4 + 4096;
n += (cap + 1) * dims * 4 + cap * pocket_stride * 4;
Expand All @@ -404,12 +407,12 @@ pub const Scratch = struct {

pub fn alloc(gpa: std.mem.Allocator, d: *const cuda.Driver, stream: cuda.Stream, cap: usize, z: Sizes) !Scratch {
const head_rows = z.head_rows;
var s: Scratch = .{ .cap = cap, .head_rows = head_rows };
var s: Scratch = .{ .cap = cap, .head_rows = head_rows, .slots = z.slots };
errdefer {
cuda.completion.drain(d, stream, "Flash Next scratch work did not drain before rollback");
s.free(gpa);
}
const pairs = cap * slots;
const pairs = cap * z.slots;
const nblk = cdiv(pairs, plan_small);
const items = cdiv(pairs, tile) + experts_n + 1;
s.ids = try s.add(gpa, d, cap * 4);
Expand Down Expand Up @@ -465,12 +468,13 @@ pub const Scratch = struct {
s.counts = try s.add(gpa, d, 8);
s.rank = try s.add(gpa, d, pairs * 4);
s.hist = try s.add(gpa, d, nblk * (experts_n + 1) * 4);
s.shared_gate = try s.add(gpa, d, cap * moe_w * 2);
s.shared_up = try s.add(gpa, d, cap * moe_w * 2);
s.shared_act = try s.add(gpa, d, cap * moe_w * 2);
s.shared_gate = try s.add(gpa, d, cap * z.shared * 2);
s.shared_up = try s.add(gpa, d, cap * z.shared * 2);
s.shared_act = try s.add(gpa, d, cap * z.shared * 2);
s.shared_out = try s.add(gpa, d, cap * dims * 4);
s.tail = try s.add(gpa, d, cap * wt.tail_n * 2);
s.moe_act = try s.add(gpa, d, pairs * moe_w * 2);
s.y = try s.add(gpa, d, (pairs + slots) * dims * 4); // a spare row: the exchange's odd-row pad
s.y = try s.add(gpa, d, (pairs + z.slots) * dims * 4); // a spare row: the exchange's odd-row pad
s.branch = try s.add(gpa, d, cap * dims * 2);
s.moe_f32 = try s.add(gpa, d, (cap + 1) * dims * 4);
s.pocket_act = try s.add(gpa, d, cap * pocket_stride * 4);
Expand Down
Loading
Loading