Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
b523c90
Flash Next: opt-in continuous batching on native Metal
cwschroeder Oct 10, 2026
fd272ef
Preserve alternating benchmark receipts on the current upstream base
cwschroeder Oct 10, 2026
0c8d4d5
Apply Zig 0.17 formatting required by current CI
cwschroeder Oct 10, 2026
6dbf170
Flash Next: chain batch MTP drafts in one Metal command buffer
cwschroeder Oct 10, 2026
39824c6
Qualify experimental batch MTP chaining on M3 Ultra
cwschroeder Oct 10, 2026
77ebc3f
Attach final current-base host validation receipt
cwschroeder Oct 10, 2026
a5e409e
Flash Next shared sessions: FLASHNEXT_LOAD_LIMIT_GB lets the slots ta…
BobClawblaw Oct 11, 2026
1874ffe
Flash Next shared sessions sample: a request's temperature draws each…
BobClawblaw Oct 12, 2026
b7372ac
Flash Next's host sampler: an unlimited or wide top-k by a counting s…
BobClawblaw Oct 12, 2026
03cc874
FLASHNEXT_LOAD_LIMIT_GB covers the prompt cache too: the automatic bu…
BobClawblaw Oct 12, 2026
d519c08
Flash Next's wide top-k draw keeps a uniform that rounds to 1: its +i…
BobClawblaw Oct 12, 2026
5d59c76
Merge #8 from BobClawblaw: Flash Next shared sessions: FLASHNEXT_LOAD…
cwschroeder Oct 13, 2026
c930303
Merge #7 from BobClawblaw: Flash Next shared sessions sample: a reque…
cwschroeder Oct 13, 2026
04c8571
Merge upstream main (TensorFold 1.0.8) into Flash Next continuous bat…
cwschroeder Oct 13, 2026
3f24b00
Flash Next shared rounds priced from forwards timed at load; every st…
sethforprivacy Oct 12, 2026
5551aba
Flash Next shared sessions sized to the traffic: FLASHNEXT_SLOT_CONTE…
sethforprivacy Oct 12, 2026
a3085be
Flash Next: a lone request on the engine's GPU-side rounds, handed to…
sethforprivacy Oct 12, 2026
ba31b91
Flash Next sized sessions: grow on the reply's remaining work, never …
Oct 13, 2026
dd86083
Flash Next shared-round prices: time only on a session that holds the…
Oct 13, 2026
09c7262
Flash Next lone driver: read the prompt after the first token's commi…
Oct 13, 2026
3cb70f0
Merge #9 from sethforprivacy: Flash Next shared rounds priced from fo…
cwschroeder Oct 14, 2026
59df001
Merge feat/flashnext-continuous-batching (#9) into flashnext-batch-lone
Oct 14, 2026
0f679e2
Flash Next lone driver: lone's doc comment above lone again (commitFi…
Oct 14, 2026
198b3c5
Merge feat/flashnext-continuous-batching (#9) into flashnext-batch-se…
Oct 14, 2026
b1a4204
Flash Next: facts' priced-lone flag renamed from `lone`, which the me…
Oct 14, 2026
069ef4d
Flash Next sized sessions: a paused stream whose move fails ends with…
Oct 14, 2026
f70705a
Merge Flash Next batching onto current upstream, preserve both head c…
cwschroeder Oct 16, 2026
2f69e55
Merge reviewed lone-driver fixes alongside measured batching costs
cwschroeder Oct 16, 2026
15e424e
Merge sized sessions and migration failure handling with lone handoff
cwschroeder Oct 16, 2026
72d817c
Finish migration-failed peers after cancellation before freeing their…
cwschroeder Oct 16, 2026
1d0f0df
Port shared Flash Next session bindings to current upstream
cwschroeder Oct 16, 2026
dd1b741
Serve default Flash Next requests on memory-sized shared sessions
cwschroeder Oct 16, 2026
4e184b2
Retain shared-family tests after extracting the first-token lifetime …
cwschroeder Oct 16, 2026
fac5563
Describe the default shared route consistently in the README and recipe
cwschroeder Oct 16, 2026
f36482a
Gate default Flash Next serving with observed shared GPU rounds
cwschroeder Oct 16, 2026
b038a3d
Preserve Flash Next residency and lifecycle after the upstream rebase
cwschroeder Oct 16, 2026
8cee8ef
Draw shared Flash Next picks with the upstream GPU sampler
cwschroeder Oct 16, 2026
d0f6138
Avoid a second warmup when auto falls back to one Flash Next session
cwschroeder Oct 16, 2026
ef85c8b
Honor validated model load limits when keeping buffers resident
cwschroeder Oct 16, 2026
5164380
Refuse --parallel above one with --learn as with --speed-up; restore …
cwschroeder Oct 16, 2026
1bf016d
A warm pass its cache keeps nothing of is skipped: Flash Next's short…
BobClawblaw Oct 17, 2026
b2595ed
Flash Next reads images on one Mac with --vision: the shared Qwen-VL …
BobClawblaw Oct 17, 2026
c7d9f69
Flash Next's next-turn pass skips an image prompt on one Mac: its sta…
BobClawblaw Oct 17, 2026
557f5b8
Flash Next reads images on shared sessions: each stream's prompt pass…
BobClawblaw Oct 17, 2026
abab848
Flash Next reads videos with --vision: Qwen3-VL's frame pairs at two …
BobClawblaw Oct 17, 2026
4af9b08
Flash Next images and video documented; the video offer's Qwen layout…
BobClawblaw Oct 17, 2026
8a509ab
Flash Next image and video chats resume from the prompt cache: a kept…
BobClawblaw Oct 17, 2026
8e4ba6d
Flash Next shared sessions enforce structured output: verify rows mas…
BobClawblaw Oct 17, 2026
440ca2b
Flash Next's grammar mask under the kernel contract: fz_mask_rows in …
BobClawblaw Oct 17, 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
Prev Previous commit
Next Next commit
Flash Next shared sessions sample: a request's temperature draws each…
… token on the host from the head's logits

The first token from the prompt pass's last row, each verify row from the window's logits, with the lane core's keyed sampler (top-k in one pass over bf16 keys, equal to qwen3_5's draw; families/flashnext/sample.zig, the same file as #719). Sampled replies are the same drafted and undrafted, alone and beside other streams; greedy replies are unchanged.
  • Loading branch information
BobClawblaw committed Oct 12, 2026
commit 1874ffe0b29f849909fd143e7a38983ca2b2d014
4 changes: 3 additions & 1 deletion docs/recipes/qwen3.8-flash-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ New requests join between rounds, and cancelling a request releases its slot for
Additional slots allocate caches for the selected context. Startup reserves their metadata and the prompt-cache
budget, then checks the physical footprint before accepting requests. A fixed slot count that does not fit is
refused; lower the count, context or prompt-cache budget. `--prompt-cache-over-cap` overrides only the prompt-cache
budget. Greedy decoding remains required, so clients must send temperature 0.
budget. Shared sessions sample: a request with a temperature draws each token on the host from the head's logits
with the server's keyed sampler (top-k, top-p, min-p, a `seed` or one derived from the prompt), so a sampled reply is
the same with drafting on and off and alone or beside other streams. `--parallel 1` still decodes greedily only.

The checkpoint gate compares shared replies with fresh legacy replies, with drafting off and on, then checks
prompt admission, cancellation, slot reuse and a restored prefix while another request remains active:
Expand Down
1 change: 1 addition & 0 deletions zig/flashnext_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
test {
_ = @import("src/families/flashnext/host_test.zig");
_ = @import("src/families/flashnext/pack.zig");
_ = @import("src/families/flashnext/sample.zig");
_ = @import("src/families/flashnext/batch_meta_test.zig");
}
21 changes: 20 additions & 1 deletion zig/src/families/flashnext/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ const Stream = lanes.Stream;
const Buf = fz.Buf;
const mtl = @import("metal");
const segments = @import("../../core/segments.zig");
const sample = @import("sample.zig");

/// A stream's sampling when it samples (a temperature above 0); null: the GPU's argmax stands.
fn sampling(stream: *const Stream) ?lanes.Sampling {
const p = stream.sampling orelse return null;
return if (p.temperature > 0) p else null;
}

/// Row `row` of a [rows, VOCAB] bf16 logits buffer, drawn with `p` keyed at `position`.
fn drawRow(gpa: std.mem.Allocator, logits: Buf, row: usize, p: lanes.Sampling, position: u64) !u32 {
const bytes = logits.b.contents()[logits.off + row * fz.VOCAB * 2 ..][0 .. fz.VOCAB * 2];
return sample.draw(gpa, @as([*]const u16, @ptrCast(@alignCast(bytes.ptr)))[0..fz.VOCAB], p, position);
}

pub const Backend = struct {
gpa: std.mem.Allocator,
Expand Down Expand Up @@ -120,7 +133,6 @@ pub const Backend = struct {
const pool = mtl.objc.Pool.push();
defer pool.pop();
const b = self(ptr);
if (stream.sampling) |p| if (p.temperature > 0) return error.GreedyOnly;
const prompt = stream.prompt();
if (prompt.len == 0) return error.EmptyPrompt;
try b.settle();
Expand Down Expand Up @@ -162,6 +174,10 @@ pub const Backend = struct {
end = at + call.rows;
const ps = [2]*fz.Prompt{ b.e.pr, b.e.pr2 };
s.first = try fz.Prompt.chunkN(ps[0..call.parts], &s.m, b.gpa, prompt[at..end]);
// the pass's last row's logits are in the model's head buffer until the next forward: a sampled first token now
if (end == prompt.len) if (sampling(stream)) |p| {
s.first = try drawRow(b.gpa, s.m.t.logits, 0, p, prompt.len);
};
for (ps[0..call.parts], 0..) |p, k| {
const start = at + segments.start(call.rows, call.parts, k);
const rows = segments.rows(call.rows, call.parts, k);
Expand Down Expand Up @@ -243,6 +259,9 @@ pub const Backend = struct {
try b.primary.finish(cb);
Session.copy(b.primary.last, b.hidden, plan.total * fz.WIDE * 2);
for (out, 0..) |o, i| @memcpy(o.sampled, t.picks.b.slice(u32, plan.total)[plan.starts[i]..][0..o.sampled.len]);
for (windows, out, 0..) |w, o, i| if (sampling(w.stream)) |p| {
for (o.sampled, w.positions, 0..) |*tok, pos, j| tok.* = try drawRow(b.gpa, t.logits, plan.starts[i] + j, p, pos);
};
b.window_count += 1;
if (windows.len > 1) b.shared_count += 1;
if (std.c.getenv("FZ_BATCH_LOG") != null)
Expand Down
107 changes: 107 additions & 0 deletions zig/src/families/flashnext/sample.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//! The keyed host sampler over a bf16 logits row, in one pass: the top-k candidates by an order-preserving integer key
//! of each bf16 word, then lanes.sampling.choose. The candidates, their order and the draw equal `reference`, which is
//! qwen3_5/backend.zig's draw without its mask (two passes in f64), kept here for wide top-k and the tests.
const std = @import("std");
const lanes = @import("lanes");

/// k at most this takes the one-pass path; a wider (or unlimited) top-k takes the reference's.
pub const MAX_K = 256;

/// A bf16 word's order as an unsigned key: larger floats give larger keys, -0 and +0 the same.
fn key(word: u16) u16 {
const w = if (word == 0x8000) 0 else word;
return if (w & 0x8000 != 0) ~w else w | 0x8000;
}

fn value(word: u16) f64 {
return @as(f32, @bitCast(@as(u32, word) << 16));
}

/// qwen3_5/backend.zig's draw without a mask: the unchanged top-k candidates for the keyed sampler.
pub fn reference(gpa: std.mem.Allocator, logits: []const u16, p: lanes.Sampling, position: u64) !u32 {
var best: ?usize = null;
for (logits, 0..) |word, id| {
const x = value(word);
if (!std.math.isFinite(x)) return error.NonfiniteQwenLogits;
if (best == null or x > value(logits[best.?])) best = id;
}
if (p.temperature <= 0) return @intCast(best.?);
const allowed = logits.len;
const k = if (p.top_k == 0) allowed else @min(allowed, p.top_k);
const values = try gpa.alloc(f64, k);
defer gpa.free(values);
const ids = try gpa.alloc(u64, k);
defer gpa.free(ids);
var filled: usize = 0;
for (logits, 0..) |word, id| {
const x = value(word);
if (k == allowed) {
values[filled] = x;
ids[filled] = id;
filled += 1;
continue;
}
if (filled == k and x <= values[k - 1]) continue;
var at = @min(filled, k - 1);
while (at > 0 and x > values[at - 1]) : (at -= 1) {
values[at] = values[at - 1];
ids[at] = ids[at - 1];
}
values[at] = x;
ids[at] = id;
filled = @min(filled + 1, k);
}
return @intCast(try lanes.sampling.choose(gpa, values, ids, position, p));
}

/// Row `row` of a [rows, vocab] bf16 logits buffer whose bytes start at `base`.
pub fn row(base: [*]const u8, vocab: usize, at: usize) []const u16 {
return @as([*]const u16, @ptrCast(@alignCast(base + at * vocab * 2)))[0..vocab];
}

/// The draw for `logits` with `p` keyed at `position` (p.temperature above 0).
pub fn draw(gpa: std.mem.Allocator, logits: []const u16, p: lanes.Sampling, position: u64) !u32 {
const k: usize = p.top_k;
if (k == 0 or k > MAX_K or k >= logits.len) return reference(gpa, logits, p, position);
var keys: [MAX_K]u16 = undefined;
var ids: [MAX_K]u64 = undefined;
var filled: usize = 0;
for (logits, 0..) |word, id| {
if (word & 0x7f80 == 0x7f80) return error.NonfiniteQwenLogits; // exponent all ones: inf or nan
const x = key(word);
if (filled == k and x <= keys[k - 1]) continue;
var at = @min(filled, k - 1);
while (at > 0 and x > keys[at - 1]) : (at -= 1) {
keys[at] = keys[at - 1];
ids[at] = ids[at - 1];
}
keys[at] = x;
ids[at] = id;
filled = @min(filled + 1, k);
}
var values: [MAX_K]f64 = undefined;
for (ids[0..filled], values[0..filled]) |id, *v| v.* = value(logits[id]);
return @intCast(try lanes.sampling.choose(gpa, values[0..filled], ids[0..filled], position, p));
}

test "the one-pass draw equals the reference on random rows with ties, signed zeros and every top-k" {
const gpa = std.testing.allocator;
var rng = std.Random.DefaultPrng.init(7);
const r = rng.random();
const words = try gpa.alloc(u16, 4096);
defer gpa.free(words);
for (0..40) |trial| {
for (words) |*w| {
const f: f32 = @floatCast(r.floatNorm(f64) * 3.0);
w.* = @intCast(@as(u32, @bitCast(f)) >> 16);
}
for (0..64) |j| words[r.uintLessThan(usize, words.len)] = words[j]; // ties
words[r.uintLessThan(usize, words.len)] = 0x8000;
words[r.uintLessThan(usize, words.len)] = 0;
for ([_]u32{ 1, 2, 20, 64, 256 }) |k| for ([_]f64{ 0.6, 1.0, 1.5 }) |t| {
const p: lanes.Sampling = .{ .seed = trial * 31 + k, .temperature = t, .top_k = k, .top_p = 0.95 };
const pos: u64 = trial * 7 + k;
try std.testing.expectEqual(try reference(gpa, words, p, pos), try draw(gpa, words, p, pos));
};
}
}
3 changes: 1 addition & 2 deletions zig/src/native/flashnext_batch_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, dir: []const u8, dump: ?[]const
errdefer h.core.deinit();
const plan = try fit.budget(cache_gib, over, 0, a, why, "");
eng.snap_pool.max = plan.budget_bytes;
h.host = api.LaneHost.init(gpa, io, &h.core, .{ .name = "flashnext-zig", .lanes = slots, .greedy_only = true, .context_window = @intCast(window), .prefill_step = @intCast(eng.pr.step), .prompt_cache_plan = plan });
h.host = api.LaneHost.init(gpa, io, &h.core, .{ .name = "flashnext-zig", .lanes = slots, .context_window = @intCast(window), .prefill_step = @intCast(eng.pr.step), .prompt_cache_plan = plan });
h.cache = null;
if (plan.budget_bytes > 0) {
h.cache = api.prompt_cache.Store.init(gpa, .{ .ptr = &h.back, .vtable = &.{ .bytes = Backend.snapBytes, .save = Backend.snapSave, .restore = Backend.snapRestore, .drop = Backend.snapDrop, .charged = Backend.snapCharged, .spare = Backend.snapSpare, .trim = Backend.snapTrim, .reuses = Backend.snapReuses } }, .{ .lookahead = 1, .planned = true }, plan.budget_bytes);
Expand All @@ -72,7 +72,6 @@ pub fn open(gpa: std.mem.Allocator, io: std.Io, dir: []const u8, dump: ?[]const

fn words(_: ?*anyopaque, err: anyerror) ?[]const u8 {
return switch (err) {
error.GreedyOnly => "the native Flash Next engine decodes greedily only: send temperature 0",
error.ContextFull => "the prompt and max_tokens do not fit this server's --context",
else => null,
};
Expand Down
1 change: 1 addition & 0 deletions zig/src/tensorfold.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub const segments = @import("core/segments.zig");
pub const row_projection = @import("core/row_projection.zig");
pub const nemotron = @import("families/nemotron/nemotron.zig");
pub const flashnext_replay = @import("families/flashnext/replay.zig");
pub const flashnext_sample = @import("families/flashnext/sample.zig");
pub const flashnext_engine = @import("families/flashnext/engine.zig");
pub const flashnext_snapshot = @import("families/flashnext/snapshot.zig");
pub const flashnext_session = @import("families/flashnext/session.zig");
Expand Down