Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
51c9ccf
Gemma 4 dense: the 31B's config admission and a token oracle from gil…
BobClawblaw Oct 14, 2026
269e56f
Gemma 4 dense: kernel fixtures from gilby's RowDecode, and the core l…
BobClawblaw Oct 14, 2026
bf4418b
Gemma 4 dense: the decode glue on Metal, bit-equal to gilby's RowDeco…
BobClawblaw Oct 14, 2026
bcf358f
Gemma 4 dense: native greedy decode on Metal, token for token and log…
BobClawblaw Oct 14, 2026
7cf144c
Gemma 4 dense: prompts prefilled in decode windows, the same bits at …
BobClawblaw Oct 14, 2026
f7197de
Gemma 4 dense on the lane core: shared rounds and copy drafts, every …
BobClawblaw Oct 14, 2026
e96fa2d
Gemma 4 31B served natively on Metal: registered as gemma4, served gr…
BobClawblaw Oct 14, 2026
d980d8e
Gemma 4 dense: the reg lane kernel for every projection, the same bit…
BobClawblaw Oct 14, 2026
1832cb0
Gemma 4 dense: prompts in 128-row windows, keys written before attent…
BobClawblaw Oct 14, 2026
3bd3467
tf-gemma4-bench: prompt-window and long-context forwards, and core af…
BobClawblaw Oct 14, 2026
1eacc91
Gemma 4 dense: prompt chunks of 512 rows on core affine_mm, from alig…
BobClawblaw Oct 14, 2026
2efafcc
Gemma 4 dense: prompt attention on the tensor units, the head dim in …
BobClawblaw Oct 14, 2026
41809bf
Gemma 4 dense: the server prefills in 512-row chunks on the tensor-un…
BobClawblaw Oct 14, 2026
42e7e61
tf-gemma4-bench: projections with their group sums given, against the…
BobClawblaw Oct 14, 2026
4e59002
Gemma 4 dense: global layers' decode attention on the tensor units, a…
BobClawblaw Oct 14, 2026
d2bf7bf
Gemma 4 dense: decode scratch sized by 256-key chunks, and long-conte…
BobClawblaw Oct 14, 2026
3e983d9
Gemma 4 dense: decode glue writes the projections' group sums and the…
BobClawblaw Oct 14, 2026
09d40af
Lane core: a target may hand a drafter each prompt chunk's tapped row…
BobClawblaw Oct 15, 2026
819618d
Gemma 4 dense drafts with z-lab's DFlash: 150-190 tokens a second for…
BobClawblaw Oct 15, 2026
abb860c
Gemma 4 drafter: a round's streams in one block forward and one absor…
BobClawblaw Oct 15, 2026
ae4cfdd
Gemma 4: the greedy draw's host fallback switch goes (a getenv a row …
BobClawblaw Oct 15, 2026
0acea2f
Gemma 4 gate: the mlx oracle prefills in 2048-token chunks, and the l…
BobClawblaw Oct 15, 2026
c05ce72
Gemma 4 prompt attention: a simdgroup scores 64 keys over the whole h…
BobClawblaw Oct 15, 2026
9a06f96
Core affine_mm: tile fragments in vector loads, and a step's codes, s…
BobClawblaw Oct 15, 2026
80d1aba
Gemma 4 prompt attention: 32 query rows a threadgroup, a simdgroup sc…
BobClawblaw Oct 15, 2026
127be33
Core affine_mm dequantizes each step's weights to bf16 in its tile, a…
BobClawblaw Oct 15, 2026
b775230
Gemma 4 decode: each shape's fastest K split, and the glue as the pro…
BobClawblaw Oct 15, 2026
31c0410
Gemma 4 decode: a sliding layer's attention kernel preps its own head…
BobClawblaw Oct 15, 2026
64f1895
Core lane kernel: pipelines without a prologue keep main's tile index…
BobClawblaw Oct 15, 2026
9e2afcb
Merge branch 'native-vision' into gemma4-native
BobClawblaw Oct 15, 2026
6d701ac
Gemma 4 startup: a refused drafter, or a failed allocation on the way…
BobClawblaw Oct 15, 2026
0fea59e
The server's image path: an engine names its template's part type, an…
BobClawblaw Oct 15, 2026
8965bbb
Gemma 4 reads images: its 27-layer tower on Metal, bidirectional imag…
BobClawblaw Oct 15, 2026
c530403
Third-party notices: Gemma 4's sources, gilby's RowDecode (#360), MLX…
BobClawblaw Oct 16, 2026
7daa48d
Structured output with DFlash drafts: the Drafted wrapper hands the d…
BobClawblaw Oct 16, 2026
7322cde
Gemma 4's long-lived buffers in one residency set the idle keepalive …
BobClawblaw Oct 16, 2026
d48368c
Gemma 4 docs: structured output with drafts, forced tool calls refuse…
BobClawblaw Oct 16, 2026
c048539
A grammar's read of held fillers leaves them for the verify, so both …
BobClawblaw Oct 16, 2026
0fbb3e2
Gemma 4's image projection is admitted only as u32 words with bf16 sc…
BobClawblaw Oct 16, 2026
5154a1b
Gemma 4's RMS kernel names its source: MLX 0.32's rms_looped (rms_nor…
BobClawblaw Oct 16, 2026
3338988
Merge main (TensorFold 1.0.12) into gemma4-native
BobClawblaw Oct 16, 2026
e8b6054
The kernel contract and tile-check read core affine_mm as it now comp…
BobClawblaw Oct 16, 2026
21e8040
Gemma 4 recipe on 1.0.12: the idle first token held by the residency …
BobClawblaw Oct 16, 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
Gemma 4 drafter: a round's streams in one block forward and one absor…
…b pass; wide forwards' costs timed at load

holdFn concatenates the streams' blocks (up to 128 rows) and runs the drafter once, attention a block at its rows:
eight streams' blocks take 9 ms instead of 27. absorbFn packs the streams' rows into passes the same way, fc a piece
and the rest together. The drafter reports batched head costs. The backend times forwards at 32, 48, 64, 96 and 128
rows as shared_costs, since the rule's extension of the window table priced them at twice the truth; GEMMA_DRAFT_TRACE
also prints each verify's rows and time and the test each case's engine steps. A two-blocks-a-threadgroup variant of
the reg lane kernel was measured and dropped: wide forwards are bound by tensor-op compute (0.8 ms a row), not weight
reads. Eight streams: 203-208 tok/s (from 180-195); drafted replies still equal plain, the served gate passes.
  • Loading branch information
BobClawblaw committed Oct 15, 2026
commit abb860c5585cbdd8d4bdf2597efd050fcac0a304
11 changes: 7 additions & 4 deletions docs/recipes/gemma-4-native.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ groups of 64 by MLX's affine rule and run on the same lane projection; the conte
live in a 2,047-slot ring for the four sliding layers and a growing buffer for the full one, as in z-lab's MLX model.
It sits in front of the engine as the lane core's `Drafted` wrapper (chains, no trees): the target hands it each
prompt chunk's and each verify's tapped rows, and the depth rule prices its drafts against the forward's cost by rows
timed at load. A block costs about 3.4 ms at any depth and an absorb about 2.5 ms.
timed at load. A round's streams draft in one block forward (3.4 ms for one stream, 9 for eight) and absorb in one
pass, whose command buffer the block settles.

The context defaults to 32,768 tokens; `--context` raises it to the checkpoint's 262,144, at the cost of attention
scratch that grows with it. There is no cross-request prompt reuse yet.
Expand Down Expand Up @@ -81,6 +82,8 @@ zig-out/bin/tf-gemma4-run M O R 128 512 && python tools/zig/gemma4_gate.py serve
and four at once; thinking replies keep their reasoning apart, and tool calls parse.

With the drafter, one stream decodes at 150-190 tokens a second (6.4, 6.4, 2.1 and 6.7 tokens a round on the four
prompts; 10.2 and 7.8 on copying replies) and eight streams at 180-195 together. Plain decode is 43 tokens a second
for one stream. The drafts match z-lab's own MLX drafter's acceptance; shared rounds draft shallow because the lane
projection reads its weights once per 16 rows, so a 128-row forward costs 128 ms against 25 for 16.
prompts; 10.2 and 7.8 on copying replies) and eight streams at about 210 together. Plain decode is 43 tokens a second
for one stream. The drafts match z-lab's own MLX drafter's acceptance. Shared rounds draft shallow on purpose: past 16
rows the forward is bound by tensor-op compute at about 0.8 ms a row (40 ms at 32 rows, 71 at 64, 130 at 128, the
same rate as the prompt path and mlx-lm's), so the depth rule, given those costs, finds deeper shared drafts do not
pay; the drafter's own cost is 9 ms a round for eight streams.
24 changes: 19 additions & 5 deletions zig/src/families/gemma4/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,16 @@ pub const Metal = struct {
/// A window's forward ms by rows, timed by measure() (the depth rule prices drafts with them); none until then.
costs: [st.max_rows]lanes.config.Cost = undefined,
timed: usize = 0,
/// Wider forwards' ms by total rows (several streams' windows), timed at load; the rule interpolates between.
shared: [shared_widths.len]lanes.config.Cost = undefined,
shared_timed: usize = 0,
io: std.Io,
trace: bool = false, // GEMMA_DRAFT_TRACE: each verify's rows and wall time on stderr

pub fn init(gpa: std.mem.Allocator, model: *Model, options: Options) !*Metal {
pub fn init(gpa: std.mem.Allocator, io: std.Io, model: *Model, options: Options) !*Metal {
const out = try gpa.create(Metal);
errdefer gpa.destroy(out);
out.* = .{ .gpa = gpa, .model = model, .options = options, .scratch = try st.Scratch.init(model.device, options.capacity), .prompt_scratch = try prompt.Scratch.init(model.device, options.capacity) };
out.* = .{ .gpa = gpa, .io = io, .trace = std.c.getenv("GEMMA_DRAFT_TRACE") != null, .model = model, .options = options, .scratch = try st.Scratch.init(model.device, options.capacity), .prompt_scratch = try prompt.Scratch.init(model.device, options.capacity) };
return out;
}

Expand All @@ -47,11 +52,12 @@ pub const Metal = struct {

/// Every kernel keeps a row's bits at any window width and beside any other stream's rows (tf-gemma4-glue).
pub fn facts(self: *const Metal) lanes.Model {
return .{ .exact_width = st.max_rows, .hidden_rows = true, .batch_rows = st.prompt_rows, .max_streams = self.options.streams, .streams_exact = true, .window_costs = self.costs[0..self.timed] };
return .{ .exact_width = st.max_rows, .hidden_rows = true, .batch_rows = st.prompt_rows, .max_streams = self.options.streams, .streams_exact = true, .window_costs = self.costs[0..self.timed], .shared_costs = self.shared[0..self.shared_timed] };
}

/// A window of each width 1..16 at position 0 on a throwaway cache, the second of two runs timed.
pub fn measure(self: *Metal, io: std.Io) !void {
pub fn measure(self: *Metal) !void {
const io = self.io;
var cache = try st.Cache.init(self.model.device, 256);
defer cache.deinit();
var ids: [st.max_rows]u32 = @splat(2);
Expand All @@ -66,11 +72,14 @@ pub const Metal = struct {
self.costs[rows - 1] = .{ .width = @intCast(rows), .ms = ms };
}
self.timed = st.max_rows;
for (&self.shared, shared_widths) |*cost, rows| cost.* = .{ .width = @intCast(rows), .ms = try self.timeRows(rows) };
self.shared_timed = shared_widths.len;
self.gen += 1;
}

/// The forward's ms at `rows` rows (up to 128) of one stream, the second of two runs.
pub fn timeRows(self: *Metal, io: std.Io, rows: usize) !f64 {
pub fn timeRows(self: *Metal, rows: usize) !f64 {
const io = self.io;
var cache = try st.Cache.init(self.model.device, 256);
defer cache.deinit();
var ids: [st.prompt_rows]u32 = @splat(2);
Expand Down Expand Up @@ -219,7 +228,9 @@ pub const Metal = struct {
@memcpy(ids[total + 1 ..][0..w.tokens.len], w.tokens);
total += rows;
}
const t0 = std.Io.Clock.awake.now(self.io);
try fwd.run(self.model, &self.scratch, segments[0..windows.len], ids[0..total]);
if (self.trace) std.debug.print("verify of {d} streams, {d} rows: {d:.2} ms\n", .{ windows.len, total, @as(f64, @floatFromInt(t0.durationTo(std.Io.Clock.awake.now(self.io)).toNanoseconds())) / 1e6 });
self.gen += 1;
if (self.scratch.taps) |taps| {
var row: usize = 0;
Expand Down Expand Up @@ -273,6 +284,9 @@ pub const Metal = struct {
}
};

/// Total rows a shared round is timed at beyond one window (the forward's cost grows past 16 rows: tensor-op compute).
const shared_widths = [_]usize{ 32, 48, 64, 96, 128 };

/// A row's allowed-token bits (structured output): one bit a logit.
const mask_words = (cfg.vocab + 31) / 32;

Expand Down
Loading