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
8 changes: 2 additions & 6 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

- Inkling 2 Mini on Metal: the router's and loop gates' dots run a thread per output with main's serial arithmetic (one row keeps main's kernels), so prompts reach the first token about 42% sooner and four streams decode about 64% faster on an M5 Ultra, with identical tokens (#4912).

- LFM3-VL on Metal: a text-only load leaves the vision tower and projector on disk, so it holds only the text weights; replies are unchanged (#3420).

- Kolibri-1 on CUDA: decode rounds of two or more rows replay a CUDA graph captured once a shape, making the eager round's launches, so tokens are unchanged; `TENSORFOLD_KOLIBRI_GRAPHS=0` keeps them eager (#4884).
Expand All @@ -20,20 +22,14 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

- Flash Next on CUDA: `TF_FLASHNEXT_WINDOW_CHECK=1` (default off) runs a boot self-check that measures the widest verify window whose every row's scores equal its one-token step on that machine, and a lone stream's copy window then grows past the shipped 32 rows to at most the 48 the pending-row log holds; a check that cannot run, fails or proves nothing past 32 leaves the window at 32, and with the switch off every buffer the window sizes is main's (#4075). Thanks to @BadAd84.



- Inkling 2 Mini on Metal: the routed experts and the combine run one thread per output with the same serial arithmetic, so one-stream decode is about 16x faster on an M5 Ultra and 17x on an M3 Ultra, with identical tokens (#4894). Thanks @sethforprivacy for the grid idea.

- Qwen 3.8-27B on XPU: Arc Pro B60 cards open through device detection when no B70 is found; explicit overrides and the B70 default are unchanged (#4802).



- GLM on Metal: `GLM_POOL_TOKENS` sizes the shared latent pool apart from each stream's `--context` window, so one Mac's streams can share a 2M-token pool with zero output-token differences (#3906). Thanks to @gilby.

- DeepSeek-V4.1 on Metal: `TF_DSV41_ROUND_PROF=1` times every decode round (host prepare, encode, wait and choose, each command buffer's GPU span and the gaps between them, and the drafter's ingest, propose and finish) and logs mean, p50 and p95 by slots, rows and drafted or plain every 4096 rounds and at close; it is off by default and changes no token (#2951, refs #2984). Thanks to @gilby.



- Qwen3.8 on CUDA: `TF_QWEN_PDL` is now on by default on sm_12x GPUs (GB10, RTX 50 series), so the ModelOpt NVFP4 lanes and the qwen35 glue kernels use programmatic dependent launch with tokens unchanged; other GPUs keep plain launches, `TF_QWEN_PDL=1` still turns it on from sm_90 and `TF_QWEN_PDL=0` turns it off (#4881).

- Tests: the prose benchmark's two concurrency cases hold their mock streams long enough that a busy CI host can no longer fail them (#4930).
Expand Down
4 changes: 2 additions & 2 deletions zig/kernels/metal/inkling2/glue_sources.zig
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//! Inkling 2 Mini's embedding, residual, convolution, routing and output glue.
pub const glue = @embedFile("glue.metal");
//! Inkling 2 Mini's glue, then the split router (glue_split.metal), which the contract compares bit for bit.
pub const glue = @embedFile("glue.metal") ++ "\n" ++ @embedFile("glue_split.metal");
44 changes: 44 additions & 0 deletions zig/kernels/metal/inkling2/glue_split.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Main's router bits in two launches, after glue.metal: a thread a score with main's serial dot, then picks.

kernel void tf_inkling2_route_scores(
const device bfloat* x [[buffer(0)]], const device bfloat* weight [[buffer(1)]], device float* scores [[buffer(2)]],
constant InkRouteArgs& a [[buffer(3)]], uint2 tid [[thread_position_in_threadgroup]],
uint2 group [[threadgroup_position_in_grid]]) {
const uint row = group.y, e = group.x * 32 + tid.x;
if (row >= a.rows || e >= 128) return;
const device bfloat* w = weight + size_t(e) * 2304;
const device bfloat* v = x + size_t(row) * 2304;
float dot = 0.0f;
for (uint d = 0; d < 2304; ++d) dot += float(w[d]) * float(v[d]);
scores[size_t(row) * 128 + e] = ink_sigmoid(dot);
}

// One thread a row: main's top-8 and ascending-index combine over the stored scores.
kernel void tf_inkling2_route_select(
const device float* scores [[buffer(0)]], const device float* bias [[buffer(1)]], device uint* ids [[buffer(2)]],
device float* weights [[buffer(3)]], constant InkRouteArgs& a [[buffer(4)]], uint row [[thread_position_in_grid]]) {
if (row >= a.rows) return;
const device float* s = scores + size_t(row) * 128;
float chosen[8];
uint picked[8];
bool used[128] = {};
for (uint j = 0; j < 8; ++j) {
uint best = 128;
for (uint e = 0; e < 128; ++e) if (!used[e]) {
if (best == 128 || s[e] + bias[e] > s[best] + bias[best]) best = e;
}
used[best] = true; picked[j] = best; chosen[j] = s[best];
}
// Combining, including the denominator, is in ascending expert index.
for (uint j = 1; j < 8; ++j) {
uint id = picked[j]; float score = chosen[j]; uint k = j;
while (k > 0 && picked[k - 1] > id) { picked[k] = picked[k - 1]; chosen[k] = chosen[k - 1]; --k; }
picked[k] = id; chosen[k] = score;
}
float den = 1e-20f;
for (uint j = 0; j < 8; ++j) den += chosen[j];
for (uint j = 0; j < 8; ++j) {
ids[row * 8 + j] = picked[j];
weights[row * 8 + j] = precise::divide(chosen[j], den) * 8.0f;
}
}
4 changes: 2 additions & 2 deletions zig/kernels/metal/inkling2/loop_sources.zig
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
//! Inkling 2 Mini's cross-pass message and rank-64 gate shaders.
pub const loop = @embedFile("loop.metal");
//! Inkling 2 Mini's message and rank-64 gate shaders, then the split gates the contract compares bit for bit.
pub const loop = @embedFile("loop.metal") ++ "\n" ++ @embedFile("loop_split.metal");
72 changes: 72 additions & 0 deletions zig/kernels/metal/inkling2/loop_split.metal
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Main's loop gate bits in three launches over every row (m's rows a.m_stride floats apart), after loop.metal.

struct InkLoopSplitArgs { uint rows, m_stride; };

kernel void tf_inkling2_loop_norm(
const device float* h [[buffer(0)]], const device float* m [[buffer(1)]], device bfloat* normalized [[buffer(2)]],
constant InkLoopSplitArgs& a [[buffer(3)]], uint t [[thread_position_in_threadgroup]],
uint row [[threadgroup_position_in_grid]], uint lane [[thread_index_in_simdgroup]]) {
if (row >= a.rows) return;
const device float* inputs[2] = { h + size_t(row) * INKLING_LOOP_H,
m + size_t(row) * a.m_stride };
threadgroup float sums[2 * 8], inverse[2];
for (uint s = 0; s < 2; ++s) {
float ss = 0.0f;
for (uint d = t; d < INKLING_LOOP_H; d += 256) {
const float x = inputs[s][d];
ss += x * x;
}
const float reduced = simd_sum(ss);
if (lane == 0) sums[s * 8 + t / 32] = reduced;
}
threadgroup_barrier(mem_flags::mem_threadgroup);
if (t == 0) for (uint s = 0; s < 2; ++s) {
float ss = 0.0f;
for (uint j = 0; j < 8; ++j) ss += sums[s * 8 + j];
inverse[s] = precise::rsqrt(precise::divide(ss, 2304.0f) + INKLING_LOOP_EPS);
}
threadgroup_barrier(mem_flags::mem_threadgroup);
device bfloat* out = normalized + size_t(row) * (2 * INKLING_LOOP_H);
for (uint d = t; d < INKLING_LOOP_H; d += 256) {
out[d] = bfloat(h[size_t(row) * INKLING_LOOP_H + d] * inverse[0]);
out[INKLING_LOOP_H + d] = bfloat(m[size_t(row) * a.m_stride + d] * inverse[1]);
}
}

// One thread a (rank, row), main's serial ascending dot over the stored BF16 norms.
kernel void tf_inkling2_loop_down(
const device bfloat* normalized [[buffer(0)]], const device bfloat* down [[buffer(1)]],
device float* zeta [[buffer(2)]],
constant InkLoopSplitArgs& a [[buffer(3)]], uint2 tid [[thread_position_in_threadgroup]],
uint2 group [[threadgroup_position_in_grid]]) {
const uint row = group.y, r = group.x * 32 + tid.x;
if (row >= a.rows || r >= INKLING_LOOP_RANK) return;
const device bfloat* x = normalized + size_t(row) * (2 * INKLING_LOOP_H);
const device bfloat* w = down + size_t(r) * (2 * INKLING_LOOP_H);
float dot = 0.0f;
for (uint d = 0; d < 2 * INKLING_LOOP_H; ++d) dot += float(w[d]) * float(x[d]);
zeta[size_t(row) * INKLING_LOOP_RANK + r] = float(bfloat(dot));
}

kernel void tf_inkling2_loop_apply(
const device float* h [[buffer(0)]], const device float* m [[buffer(1)]], const device float* zeta [[buffer(2)]],
const device bfloat* forget [[buffer(3)]], const device bfloat* gain [[buffer(4)]], device float* out [[buffer(5)]],
constant InkLoopSplitArgs& a [[buffer(6)]], uint t [[thread_position_in_threadgroup]],
uint row [[threadgroup_position_in_grid]]) {
if (row >= a.rows) return;
const device float* z = zeta + size_t(row) * INKLING_LOOP_RANK;
for (uint d = t; d < INKLING_LOOP_H; d += 256) {
float fdot = 0.0f, gdot = 0.0f;
for (uint r = 0; r < INKLING_LOOP_RANK; ++r) {
fdot += float(forget[size_t(d) * INKLING_LOOP_RANK + r]) * z[r];
gdot += float(gain[size_t(d) * INKLING_LOOP_RANK + r]) * z[r];
}
const float f = precise::tanh(fdot);
const float g = precise::tanh(gdot);
// Four separately rounded binary32 steps; do not fold these into one expression.
const float keep = 1.0f - f;
const float left = keep * h[size_t(row) * INKLING_LOOP_H + d];
const float right = g * m[size_t(row) * a.m_stride + d];
out[size_t(row) * INKLING_LOOP_H + d] = left + right;
}
}
2 changes: 2 additions & 0 deletions zig/src/core/contract/inkling2_glue_metal_records.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ pub const records = [_]M{
.{ .name = "metal inkling2 glue logits tf_inkling2_logits", .threads = 32, .tg_memory = 0, .reserved = 32 },
.{ .name = "metal inkling2 glue expert_pick tf_inkling2_expert_pick", .threads = 32, .tg_memory = 0, .reserved = 32 },
.{ .name = "metal inkling2 glue expert_combine tf_inkling2_expert_combine", .threads = 32, .tg_memory = 0, .reserved = 32 },
.{ .name = "metal inkling2 glue route_scores tf_inkling2_route_scores", .threads = 32, .tg_memory = 0, .reserved = 32 },
.{ .name = "metal inkling2 glue route_select tf_inkling2_route_select", .threads = 32, .tg_memory = 0, .reserved = 32 },
};
3 changes: 3 additions & 0 deletions zig/src/core/contract/inkling2_loop_metal_records.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ const M = @import("limits.zig").MetalLaunch;
pub const records = [_]M{
.{ .name = "metal inkling2 loop messages tf_inkling2_loop_messages", .threads = 256, .tg_memory = 144, .reserved = 256 },
.{ .name = "metal inkling2 loop gates tf_inkling2_loop_gates", .threads = 256, .tg_memory = 9552, .reserved = 256 },
.{ .name = "metal inkling2 loop norm tf_inkling2_loop_norm", .threads = 256, .tg_memory = 80, .reserved = 256 },
.{ .name = "metal inkling2 loop down tf_inkling2_loop_down", .threads = 32, .tg_memory = 0, .reserved = 32 },
.{ .name = "metal inkling2 loop apply tf_inkling2_loop_apply", .threads = 256, .tg_memory = 0, .reserved = 256 },
};
24 changes: 19 additions & 5 deletions zig/src/families/inkling2/metal/forward.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ fn messageRow(s: *state.Scratch, core: usize, role: usize, i: usize) Ref {
return at(s.messages, ((i * state.message_entries + core * 4 + role) * state.hidden) * 4);
}

/// One controller over every row: m's rows are a token's message entries apart; the bits equal per-row `loop.gates`.
fn gates(m: *const Model, e: mtl.ComputeEncoder, s: *state.Scratch, h: Ref, msg: Ref, c: model_file.Controller, out: Ref, rows: usize) void {
// under two rows (decode's one), main's call: one launch a row
if (!loop.plan.splitRows(rows)) {
for (0..rows) |_| loop.gates(&m.loop, e, l(h), l(msg), l(r(c.down)), l(r(c.forget)), l(r(c.gain)), l(out), 1);
return;
}
const stride: u32 = state.message_entries * state.hidden;
loop.gatesSplit(&m.loop, e, l(h), l(msg), stride, l(r(c.down)), l(r(c.forget)), l(r(c.gain)), l(out), l(at(s.gate_norm, 0)), l(at(s.gate_zeta, 0)), @intCast(rows));
}

fn runtime(m: *const Model, e: mtl.ComputeEncoder, op: model_file.RuntimeOp, refs: []const Ref, count: usize, boundary: usize) void {
e.setPipeline(m.runtime[@backingInt(op)]);
for (refs, 0..) |r_, i| e.setBuffer(r_.buf, r_.off, i);
Expand Down Expand Up @@ -148,11 +159,11 @@ fn prepareAttentionInputs(m: *const Model, e: mtl.ComputeEncoder, s: *state.Scra
if (v.hasMessages()) {
const controllers = w.controllers.?;
const core = v.layer - 6;
for (0..3) |role| for (0..rows) |i| {
for (0..3) |role| {
const c = controllers[role];
const source_role = if (lp.kind == .local) 0 else role;
loop.gates(&m.loop, e, l(arRow(s, source_role, rows, i, false)), l(messageRow(s, core, role, i)), l(r(c.down)), l(r(c.forget)), l(r(c.gain)), l(arRow(s, role, rows, i, true)), 1);
};
gates(m, e, s, arRow(s, source_role, rows, 0, false), messageRow(s, core, role, 0), c, arRow(s, role, rows, 0, true), rows);
}
}
const gated = v.hasMessages();
// An ungated local visit has exactly one AR result and one norm result; Q/K/V alias.
Expand Down Expand Up @@ -270,10 +281,13 @@ fn visit(m: *const Model, e: mtl.ComputeEncoder, s: *state.Scratch, v: *const la
if (gated) {
const c = w.controllers.?[3];
const core = v.layer - 6;
for (0..rows) |i| loop.gates(&m.loop, e, l(row(s.mlp_ar, i, state.hidden * 4)), l(messageRow(s, core, 3, i)), l(r(c.down)), l(r(c.forget)), l(r(c.gain)), l(row(s.mlp_gated, i, state.hidden * 4)), 1);
gates(m, e, s, at(s.mlp_ar, 0), messageRow(s, core, 3, 0), c, at(s.mlp_gated, 0), rows);
norm(m, e, at(s.mlp_gated, 0), w.mlp_norm, at(s.mlp_normed, 0), rows);
} else norm(m, e, at(s.mlp_ar, 0), w.mlp_norm, at(s.mlp_normed, 0), rows);
glue.router(&m.glue, e, at(s.mlp_normed, 0), r(w.router_weight), r(w.router_bias), at(s.route_ids, 0), at(s.route_weights, 0), @intCast(rows));
if (loop.plan.splitRows(rows))
glue.routerSplit(&m.glue, e, at(s.mlp_normed, 0), r(w.router_weight), r(w.router_bias), at(s.route_scores, 0), at(s.route_ids, 0), at(s.route_weights, 0), @intCast(rows))
else
glue.router(&m.glue, e, at(s.mlp_normed, 0), r(w.router_weight), r(w.router_bias), at(s.route_ids, 0), at(s.route_weights, 0), @intCast(rows));
try routedExperts(m, e, s, v.layer, rows);
base = 0;
for (segments) |seg| {
Expand Down
21 changes: 20 additions & 1 deletion zig/src/families/inkling2/metal/glue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub const Pipelines = struct {
conv_static: mtl.Pipeline,
conv_dynamic: mtl.Pipeline,
router: mtl.Pipeline,
route_scores: mtl.Pipeline,
route_select: mtl.Pipeline,
swiglu: mtl.Pipeline,
logits: mtl.Pipeline,
expert_pick: mtl.Pipeline,
Expand All @@ -28,7 +30,7 @@ pub const Pipelines = struct {
pub fn loadSource(device: mtl.Device, text: []const u8) !Pipelines {
const lib = try mtl.Library.fromSource(device, text, mtl.CompileOptions.mlx());
defer lib.deinit();
var pipes: [9]mtl.Pipeline = undefined;
var pipes: [11]mtl.Pipeline = undefined;
var made: usize = 0;
errdefer for (pipes[0..made]) |pipe| pipe.deinit();
for ([_][]const u8{
Expand All @@ -41,6 +43,8 @@ pub const Pipelines = struct {
"tf_inkling2_logits",
"tf_inkling2_expert_pick",
"tf_inkling2_expert_combine",
"tf_inkling2_route_scores",
"tf_inkling2_route_select",
}, &pipes) |name, *pipe| {
pipe.* = try mtl.Pipeline.init(device, lib, name, false);
made += 1;
Expand All @@ -55,6 +59,8 @@ pub const Pipelines = struct {
.logits = pipes[6],
.expert_pick = pipes[7],
.expert_combine = pipes[8],
.route_scores = pipes[9],
.route_select = pipes[10],
};
}
pub fn deinit(p: *Pipelines) void {
Expand Down Expand Up @@ -110,6 +116,19 @@ pub fn router(p: *const Pipelines, e: mtl.ComputeEncoder, x: Ref, weight: Ref, b
e.dispatchGroups(mtl.Size.of(rows, 1, 1), mtl.Size.of(256, 1, 1));
}

/// `router`'s bits in two launches, a thread an (expert, row) score then a thread a row; scores: [rows][128] F32.
pub fn routerSplit(p: *const Pipelines, e: mtl.ComputeEncoder, x: Ref, weight: Ref, bias: Ref, scores: Ref, ids: Ref, weights: Ref, rows: u32) void {
std.debug.assert(rows > 0);
e.setPipeline(p.route_scores);
inline for (.{ x, weight, scores }, 0..) |r, i| e.setBuffer(r.buf, r.off, i);
e.setValue(RouteArgs{ .rows = rows }, 3);
e.dispatchGroups(mtl.Size.of(experts / 32, rows, 1), unit);
e.setPipeline(p.route_select);
inline for (.{ scores, bias, ids, weights }, 0..) |r, i| e.setBuffer(r.buf, r.off, i);
e.setValue(RouteArgs{ .rows = rows }, 4);
e.dispatchGroups(mtl.Size.of((rows + 31) / 32, 1, 1), unit);
}

const ActArgs = extern struct { rows: u32, width: u32 };
pub fn swiglu(p: *const Pipelines, e: mtl.ComputeEncoder, gu: Ref, out: Ref, rows: u32, width: u32) void {
std.debug.assert(rows > 0 and width > 0);
Expand Down
14 changes: 13 additions & 1 deletion zig/src/families/inkling2/metal/glue_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ fn near(want: f64, got: f64) !void {
}

test "glue entries have one shader, symbol, unique launch record and honest scalar reservation" {
for (std.enums.values(p.Kind), records) |kind, record| {
const kinds = std.enums.values(p.Kind);
for (kinds, records[0..kinds.len]) |kind, record| {
try t.expectEqualStrings(kind.label(), record.name);
try t.expectEqual(@as(usize, 1), std.mem.count(u8, shader, kind.symbol()));
try t.expectEqual(record.threads, record.reserved);
Expand All @@ -22,6 +23,17 @@ test "glue entries have one shader, symbol, unique launch record and honest scal
try t.expectEqual(@as(u32, 128), records[@backingInt(p.Kind.conv_dynamic)].tg_memory);
}

test "the split router's two launches follow the kinds' records, one symbol and one scalar record each" {
const split = records[std.enums.values(p.Kind).len..];
try t.expectEqual(@as(usize, 2), split.len);
for (split, [_][]const u8{ "tf_inkling2_route_scores", "tf_inkling2_route_select" }) |record, symbol| {
try t.expect(std.mem.endsWith(u8, record.name, symbol));
try t.expectEqual(@as(usize, 1), std.mem.count(u8, shader, symbol));
try t.expectEqual(@as(u32, 32), record.threads);
try t.expectEqual(record.threads, record.reserved);
}
}

test "shape admission fixes Mini's hidden and routed dimensions" {
try t.expect(p.valid(.embed_norm, .{ .rows = 4, .n = p.hidden, .k = p.hidden }));
try t.expect(!p.valid(.ar, .{ .rows = 6, .n = p.hidden, .k = p.hidden }));
Expand Down
Loading
Loading