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
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- Server: a grammar over 512 KiB is refused with HTTP 400 before it is compiled, so one oversized schema can no longer hold the grammar compiler for a minute (#3038).
- Flash Next on Metal (M1 through M4): the scale index is now the default for 4-bit experts in groups of 32, and `TF_FLASHNEXT_SCALE_INDEX=0` turns it off. Prompt passes and grouped decode windows read each group's scale and bias through the index too, and the routed experts' stored scales and biases are released once indexed; every kernel gets the same values, so replies keep their tokens. On an M3 Ultra with the MLX 4-bit checkpoint, memory in use once loaded falls from 129.6 to 122.5 GiB, drafted decode on one stream is 2.0% to 4.2% faster (prose 138.6 to 142.0 tok/s, a copy-heavy edit 282.0 to 293.8), four streams are 2.6% faster (203.1 to 208.3 tok/s) and a cold prompt's first token arrives about 1% sooner; load takes about 0.7 s longer (#2805, part of #2751).

- HIP on gfx1151: the routed prompt tile runs an expert of at most 8 rows in K-parallel passes inside the same launch, with the matrix path's bytes; with Qwen3.6-35B-A3B on a Radeon 8060S, served cold prompts of 64 to 256 tokens are 11% to 22% faster (1.5% at 1,024), and logits and served tokens equal main's. Other cards' tables leave it off (#2878, fixes #2795). Thanks to @AbrahamCete.

- Qwen3.8-27B on XPU: the target's and the DFlash2 drafter's RoPE tables come from core's exact helper, so they hold the reference's fp32 words; 15 of 32 and 18 of 64 values move by 1 ulp (#2000, part of #1852).

- Tests: `zig build test-lane-explore-host` explores the lane host's schedules from a seed (arrivals, cancels at every stage, the backend's room for live lanes and memory pressure) around a fake target, so ordering bugs show up without hand-picked orders (#3233).

- Qwen3.5 on CUDA: `qwen3_5` and `qwen3_5_moe` declare warm turns, so the server prefills a follow-up's prompt in the background and the next turn resumes past the previous reply, with replies unchanged (#2824). Thanks to @devopsfortunato.

- Server: tool-call markup quoted in Markdown inline code spans and fenced code blocks stays prose, in whole replies and in streaming, instead of opening, hiding or running as a live tool call (#3588). Thanks to @gilby.

- Lanes: shared draft pricing prices a chain-cut, unmeasured tail by the rule's rates instead of as a measured zero, and the cache's cut marker survives allocation errors (#2979). Thanks to @devopsfortunato.

- Docs: the Nemotron 3.5 Lightning recipe gains a "Native HIP on a Strix Halo iGPU" section with its configuration, memory footprint, throughput, prefill and exactness checks, and the intro lists HIP beside Metal and CUDA (#3560). Thanks to @gilby.

- Qwen3.5-2B on Metal: the engine declares `warm_turns` from its prompt cache, so a follow-up that re-renders earlier rows resumes the reply instead of prefilling the whole conversation again (#3457). Thanks to @EugeneClaw.

- Kolibri-1: the tree-attention cache offsets are signed, so a server with default flags no longer panics with an integer overflow on its first request when a cache sits below the origin (#3561). Thanks to @jschmied.

- H3 on Metal: `Model.load` reads every transformer shard ahead of its mappings and fails the load on a missing or short shard; `TF_LOAD_PREFETCH` sets the reader count and `0` turns read-ahead off (#2999). Thanks to @plotarmordev.
Expand Down
2 changes: 1 addition & 1 deletion docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Diagnostics quote and escape the supplied text so it cannot create another termi
| `TF_GLM_CONTEXT_CEILING` | GLM-5.3-Flash on CUDA: whole token count 1 through 2147483616, the window `--context` may ask for and `--context 0` serves, in place of config.json's 1048576; read at GLM's open. Past that length the model's quality is the operator's to check. Unset keeps config.json's window |
| `TF_PROMPT_ROWS` | Native serving: a multiple of 16 from 128 through 2048; unset keeps the chip/family default |
| `TF_ANE_COLUMNS` | Whole u32 count; zero disables the experimental share; default zero |
| `TF_FLASHNEXT_SCALE_INDEX` | Flash Next on Metal before tensor units: exactly `0` or `1`; `1` has the 4-bit expert kernels read each group's scale and bias through a 16-bit index into its tensor's table of pairs, the same values with fewer weight bytes; default 0 |
| `TF_FLASHNEXT_SCALE_INDEX` | Flash Next on Metal before tensor units: exactly `0` or `1`; default 1, where the 4-bit expert kernels (decode, grouped and prompt gathers) read each group's scale and bias through a 16-bit index into its tensor's table of pairs, the same values from fewer weight bytes, and the routed experts' stored scales and biases are not kept in memory; `0` keeps and reads the stored arrays; ignored with `--ssd-experts` |
| `TF_FLASHNEXT_GROUPED_ROWS` | Flash Next on Metal without tensor units: `0`, or the rows (`1` through `32`) from which a decode window runs its 4-bit experts grouped by expert, with the same output bits; default 5; ignored with `--ssd-experts` and in speed-up mode |
| `TF_QWEN27_SLOT_RESUME` | Qwen3.8-27B on Metal: exactly `0` or `1`; `0` stops slot 0 resuming a later prompt; unset or blank keeps it on |
| `TF_QWEN27_METAL_KV_PAGES` | Qwen3.8-27B on Metal: exactly `0` or `1`; `1` puts keys and values in core-pool pages (refused on Macs with tensor units); unset or blank is `0` |
Expand Down
9 changes: 6 additions & 3 deletions docs/optimization-ledger.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ last two hold the families with only a Metal, HIP or scoring backend.
| 25. TP ranks | done H[#655] | done C[#1771] H[#817] | done [#855] | yes | done¹ |
| 26. Pipeline | open [#2115] | yes | yes | yes | yes |
| 27. Profiles | done [#1944] | done [#1944] | done [#1959] | done [#1944] | done [#2061] |
| 28. Scale index | ? | open M[#2760] | ? | ? | ? |
| 28. Scale index | ? | done M[#2760], open M[#2805] | ? | ? | ? |
| 29. DSpine | done C[#2633] | no (no drafter) | no (no drafter) | no (no drafter) | no (no drafter) |

| Optimization | kol1 | spk25 | dory | mm3 | dsv41 |
Expand Down Expand Up @@ -469,8 +469,10 @@ below; these are estimates for planning a port, not results.
28. **Scale index.** An affine checkpoint stores a bf16 scale and bias beside every group's codes, a fifth of the
bytes at 4 bits in groups of 32, and a tensor holds few distinct pairs. A 16-bit index a group into the tensor's
table of pairs gives a kernel the same two values from fewer bytes. Flash Next's 4-bit expert kernels read it on
Macs before tensor units behind `TF_FLASHNEXT_SCALE_INDEX` ([#2760], plan in [#2751]); the prompt gathers and the
dense rows still read the stored arrays. `?` cells: the same count has not been run on those checkpoints.
Macs before tensor units ([#2760], plan in [#2751]); its prompt gathers and grouped expert kernels read it too,
the routed experts' stored arrays are released and it is the default, `TF_FLASHNEXT_SCALE_INDEX=0` turning it
off ([#2805]). The dense rows still read the stored arrays. `?` cells: the same count has not been run on those
checkpoints.

29. **DSpine drafter.** Tencent's DSpine-b16 for Qwen3.8-27B, opt-in by `--drafter` with its folder, never the
default. It reuses DFlash2's taps and tap ring and adds a code bridge and an exact-sender table.
Expand Down Expand Up @@ -628,3 +630,4 @@ below; these are estimates for planning a port, not results.
[#2784]: https://github.com/ashhart/TensorFold/pull/2784
[#2764]: https://github.com/ashhart/TensorFold/issues/2764
[#2977]: https://github.com/ashhart/TensorFold/pull/2977
[#2805]: https://github.com/ashhart/TensorFold/pull/2805
11 changes: 6 additions & 5 deletions docs/recipes/qwen3.8-flash-next.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ the lane matmul; M1 through M4 use the per-row projection and hyper-connection k
restore recurrent state, n-gram history and attention state, including incomplete pooled blocks.
The load-time row check disables drafting when windows do not reproduce serial steps.

`TF_FLASHNEXT_SCALE_INDEX=1` (off by default; M1 through M4, 4-bit experts in groups of 32) has the expert kernels read
each group's scale and bias through a 16-bit index into its tensor's table of distinct pairs, built at load. The
kernels get the same two values from 10% fewer weight bytes, so every reply keeps its tokens. Prompt passes still read
the stored scales and biases, so the index is held beside them. The setting does nothing with tensor units,
`--ssd-experts` or speed-up mode, and a tensor with more than 65,536 distinct pairs turns it off with a warning.
On M1 through M4, expert projections stored at 4 bits in groups of 32 are read through a scale index: at load each
tensor's (scale, bias) pairs become a table and a 16-bit index a group, and the decode, grouped and prompt-gather
expert kernels read each group's pair through it. They get the same two values from 10% fewer weight bytes, so every
reply keeps its tokens, and the routed experts' stored scales and biases are released once indexed.
`TF_FLASHNEXT_SCALE_INDEX=0` keeps and reads the stored arrays instead. The index is not built with tensor units or
`--ssd-experts`, nor for 6-bit packs, and a tensor with more than 65,536 distinct pairs turns it off with a warning.

On M1 through M4, a decode window of five rows or more runs its 4-bit experts grouped by expert: rows that picked
the same expert share its launch, so a copy or repeat stretch reads each expert's weights once instead of once a
Expand Down
1 change: 1 addition & 0 deletions zig/flashnext_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ test {
_ = @import("src/families/flashnext/rows_w.zig");
_ = @import("src/families/flashnext/experts4.zig");
_ = @import("src/families/flashnext/scale_pairs.zig");
_ = @import("src/families/flashnext/prompt_index_source.zig");
_ = @import("src/families/flashnext/tree.zig");
_ = @import("src/families/flashnext/gate.zig");
_ = @import("src/families/flashnext/gx.zig");
Expand Down
15 changes: 15 additions & 0 deletions zig/src/core/contract/prompt_index_records.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! The indexed prompt gathers: the 128-thread groups the patched text reserves, and the gathers' dequantized tiles.
const M = @import("limits.zig").MetalLaunch;
const tile = @import("fsr_records.zig").gather_memory;
pub const metal = [_]M{
.{ .name = "flashnext prompt index tf_gather_qmmw_nax_32", .threads = 128, .tg_memory = tile, .reserved = 128 },
.{ .name = "flashnext prompt index tf_gather_qmmw_nax_64", .threads = 128, .tg_memory = tile, .reserved = 128 },
.{ .name = "flashnext prompt index tf_gather_qmmw_nax_128", .threads = 128, .tg_memory = tile, .reserved = 128 },
.{ .name = "flashnext prompt index tf_gather_guw_nax_32", .threads = 128, .tg_memory = 2 * tile, .reserved = 128 },
.{ .name = "flashnext prompt index tf_gather_guw_nax_64", .threads = 128, .tg_memory = 2 * tile, .reserved = 128 },
.{ .name = "flashnext grouped index gx_pick", .threads = 32, .tg_memory = 0, .reserved = 32 },
.{ .name = "flashnext grouped index gx_list", .threads = 512, .tg_memory = 7488, .reserved = 512 },
.{ .name = "flashnext grouped index gx_gateup", .threads = 64, .tg_memory = 0, .reserved = 64 },
.{ .name = "flashnext grouped index gx_down_one", .threads = 64, .tg_memory = 0, .reserved = 64 },
.{ .name = "flashnext grouped index gx_down_tile", .threads = 64, .tg_memory = 1280, .reserved = 64 },
};
1 change: 1 addition & 0 deletions zig/src/core/contract/registry_joins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub const all_metal = @import("lane_q8_records.zig").records ++
@import("glm_rows_records.zig").records ++
@import("weight_only_records.zig").metal ++
@import("grouped_records.zig").metal ++
@import("prompt_index_records.zig").metal ++
@import("hc_activation_records.zig").metal ++
@import("qwen4_records.zig").metal ++
@import("lfm2_records.zig").records ++
Expand Down
13 changes: 10 additions & 3 deletions zig/src/families/flashnext/engine_load.zig
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn finishLoad(context: Construction, r: *Run) !*Engine {
}
const m = try arena.create(Model);
m.* = .{ .r = r, .cap = r.window.capacity, .layers = undefined, .mix = undefined, .head = undefined, .embed = undefined, .ple = undefined, .t = undefined };
r.sb_index = try platform.environment.read(bool, "TF_FLASHNEXT_SCALE_INDEX", false) and r.fsr == null and r.tp == null and !r.device.tensorUnits();
r.sb_index = try platform.environment.read(bool, "TF_FLASHNEXT_SCALE_INDEX", true) and r.fsr == null and !r.device.tensorUnits();
for (0..LAYERS) |i| {
const linear = i % 4 != 3;
var L: Layer = .{ .ahc = try hcOf(r, "L{d}.ahc", .{i}), .mhc = try hcOf(r, "L{d}.mhc", .{i}), .linear = linear, .proj = undefined, .out = undefined, .router = try r.loadf("L{d}.moe.router", .{i}), .ex = undefined };
Expand Down Expand Up @@ -235,7 +235,6 @@ fn finishLoad(context: Construction, r: *Run) !*Engine {
}
}
if (r.xpack) try r.repack(&L.ex);
if (r.sb_index) try @import("scale_index.zig").addAll(r, &L.ex);
m.layers[i] = L;
}
m.mix = try hcOf(r, "mix", .{});
Expand Down Expand Up @@ -333,7 +332,6 @@ fn finishLoad(context: Construction, r: *Run) !*Engine {
}
}
if (r.xpack) try r.repack(&ex);
if (r.sb_index) try @import("scale_index.zig").addAll(r, &ex);
m.mtp = .{
.ahc = try hcOf(r, "mtp.ahc", .{}),
.mhc = try hcOf(r, "mtp.mhc", .{}),
Expand Down Expand Up @@ -386,6 +384,15 @@ fn finishLoad(context: Construction, r: *Run) !*Engine {

e.r = r;
e.m = m;
if (r.sb_index) { // before the prompt kernels compile: they read the routed experts through the index too
var widths = try @import("widths.zig").Widths.init(arena, &cfg);
defer widths.deinit();
const ws = try @import("engine_widths.zig").read(&cfg, &widths, r.ngram);
var sets: [LAYERS + 1]@import("scale_index.zig").Set = undefined;
for (&m.layers, 0..) |*L, i| sets[i] = .{ .ex = &L.ex, .xw = ws.layers[i] };
sets[LAYERS] = .{ .ex = &m.mtp.ex, .xw = ws.mtp };
try @import("scale_index.zig").build(r, &sets);
}
e.pr = try arena.create(Prompt);
e.pr.* = try Prompt.init(r, pack_dir, r.xnew_header);
const uniform = try @import("engine_widths.zig").apply(arena, &cfg, index.object.get("weight_map").?.object, e.pr, m);
Expand Down
33 changes: 33 additions & 0 deletions zig/src/families/flashnext/experts4.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ pub fn indexed(a: std.mem.Allocator, text: []const u8, consts: []const Const) ![
return out;
}

/// The grouped tile kernel's reads (fn_gx.metal): its scale row is the index row, its bias pointer the table.
const tile_reads = [_][2][]const u8{
.{ "const device bfloat* db = (shared ? SDB : DB) + so;", "const device bfloat* db = shared ? SDB : DB;" },
.{ "const float sc = float(ds[c / 2]), bi = float(db[c / 2]);", "const uint ti = uint(((const device ushort*)ds)[c / 2]) * 2u; const float sc = float(db[ti]), bi = float(db[ti + 1]);" },
};

/// fn_gx.metal reading each scale and bias through the index and the table, as `indexed` has the recorded kernels.
pub fn indexedGrouped(a: std.mem.Allocator, text: []const u8) ![]const u8 {
var out = text;
var hits: usize = 0;
for (index_reads) |r| {
hits += std.mem.count(u8, out, r[0]);
out = try std.mem.replaceOwned(u8, a, out, r[0], r[1]);
}
for (tile_reads) |r| {
if (std.mem.count(u8, out, r[0]) != 1) return error.KernelPatch;
out = try std.mem.replaceOwned(u8, a, out, r[0], r[1]);
}
if (hits != 4) return error.KernelPatch;
for ([_][]const u8{ "Sp[at])", "float(S[at])", "float(db[c / 2])" }) |direct| if (std.mem.indexOf(u8, out, direct) != null) return error.KernelPatch;
return out;
}

const calls = [2][2][]const u8{
.{ "down_rows<SWB, SWG, NI>(SDW, SDS, SDB, size_t(d0), x, lane, out)", "down_rows_bal(SDW, SDS, SDB, size_t(d0), x, lane, out)" },
.{ "down_rows<WB, WG, NI>(DW, DS, DB, size_t(PICK[r * TOPK + k]) * D + d0, x, lane, out)", "down_rows_bal(DW, DS, DB, size_t(PICK[r * TOPK + k]) * D + d0, x, lane, out)" },
Expand Down Expand Up @@ -217,3 +240,13 @@ test "the recorded 4/32 expert gate/up takes a row count from buffer 17, once; t
}
try std.testing.expectEqual(@as(usize, 1), seen);
}

test "the grouped kernels read every scale and bias through the index once patched" {
const ks = @import("kernel_sources");
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const out = try indexedGrouped(arena.allocator(), ks.flashnext_gx);
const casts = "(const device ushort*)"; // five index reads beside the casts the kernels already have
try std.testing.expectEqual(std.mem.count(u8, ks.flashnext_gx, casts) + 5, std.mem.count(u8, out, casts));
try std.testing.expectError(error.KernelPatch, indexedGrouped(arena.allocator(), out));
}
Loading
Loading