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

## Unreleased

- Vulkan: a dispatch records a barrier only when it conflicts with an earlier one (#2038). Thanks @joshuaswarren.
- The Vulkan engine records its dispatches into one command buffer and submits when 128 are waiting, or when the next upload, download or fill needs the queue, instead of one submission and fence wait per dispatch. Each pipeline holds a ring of 256 descriptor sets. No kernel changes, so the logits are byte-identical. On a 13-inch M1 with Qwen3.5-2B 4-bit and tiled projections, a run took 56.0% less time on a 24-token prompt, 42.0% less on a 256-token document and 30.1% less on a 1,024-token document. On a 16-inch M1 Max, it took 84.1%, 82.5% and 61.8% less time on those inputs; the 256-token cell was noisy. Receipts: [docs/receipts/vulkan-submit-batch-m1](docs/receipts/vulkan-submit-batch-m1/README.md) (#2038). Thanks @joshuaswarren.

- Vulkan: the 4-bit tile kernel computes four rows and two columns of a prompt chunk per thread, cutting prompt run time on Apple M1 (Honeykrisp) with identical logits; it falls back to the previous tile when a grid exceeds the device limit (#2017).

- Vulkan: qmm_q4_tile reads the 4-bit projection from 32-column tiles, 29% to 67% less run time on an M1 with the same logits (stacked on #1990) (#2015). Thanks to @joshuaswarren.

- Vulkan: Qwen3.5 prefills in 64-row chunks, cutting first-token time by 12.7% to 16.2% at 512 to 4,096 keys over five paired runs a size with identical token IDs (#3401, refs #1420). Thanks to @joshuaswarren.

- XPU: the FlashNext standalone runner builds again as well: it also gets its `device_id` import (#5685, thanks @supernovae).

- CI: pull requests run the Linux jobs; the Windows, macOS and second Linux test jobs run on main, nightly and on demand, where landing trains already cover those platforms.
Expand Down
13 changes: 9 additions & 4 deletions docs/receipts/vulkan-qmm-tile-m1/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# qmm_q4_tile receipts, Apple M1 (13-inch)

Qwen3.5-2B 4-bit, 64 greedy steps, two runs per process. Old build is #1990's head (the vec4 build, ELF `46571f11`), new build is #2015's head (ELF `6e6f47f2`). Both include the float32 fix. The ELFs were built before the comment and name commits that sit above them; those commits change no code.
Each case ran three old and three new processes, interleaved in both orders (old,new,new,old,old,new). Every process ran under `taskset` on the four P cores (CPU 4-7). Run 2 is the prefill plus 63 decode steps plus the per-step logits readback. Linux 7.3.0-2-20261110.1-lab-ARCH, started 2026-11-12 16:16Z, GPU timeouts 0. All 18 processes returned 0 with the pinned ids and the pinned logits digest.
The "old" arm is #1990 built from source commit `46571f111b2f0e92963561702b7eaab41868f823`, ELF SHA-256
`2a1d481c692900c1cefb08fa308d5852d2ec748ee7bc24c89ff013c26ac36d72` (6,089,352 bytes). The "new" arm is #2015 built
from source commit `6e6f47f228a662cb26dedf5b24bf7c50c780fe43`, ELF SHA-256
`b3b7f5e17752b31370490a7d4fe3f4ff92cf3593ed3bed447759f52c5561de41` (6,128,512 bytes). Both include the float32
fix. These two ELFs produced every time and digest in this file. The ELFs predate comment- and name-only commits.
The retained receipt does not record the shell build commands. The branch has been restacked since; no table here
comes from any later head.

| Case | Old run 2 (median) | New run 2 (median) | Gain: median (pair range) | Logits digest, all six processes |
|---|---:|---:|---|---|
Expand Down Expand Up @@ -39,10 +44,10 @@ An earlier measurement, on the build before the last tile was padded (`a2dcaa1d`

Files:

- `pinned-heads-summary.json` holds every process row, the pairs, the ELF hashes and the setup of the pinned run in the first table. It is generated from the raw run output.
- `pinned-heads-summary.json` holds every process row, the pairs, the source commits and ELF hashes and the setup of the pinned run in the first table. It is generated from the raw run output.
- `fixed-heads-summary.json` is the same for the unpinned run of 2026-11-11 in the second table.
- `summary.json` is the first version's summary (earlier builds, earlier environment, both tile builds). It stays as the record of those runs.
- `runners-2a5a06c2/` holds the output of the ten contract and stage runners at #2015's head (`2a5a06c2`, 2026-11-12, GPU timeouts 0), one file per runner, with `log.txt` listing the exit codes. `tf-vulkan-kernel-contract.txt` includes `qmm_q4_tile_f32`: four kernels pass gates 1 to 4, eight controls caught. `tf-stage1-gpu.txt` holds the projection cases with `tile=identical` (the tiled image against the default image, byte for byte, on the bf16 projections). No byte-level comparison of the f32 tile image against `qmm_f32` exists; the equal logits digests of the two heads are the identity evidence for it.
- `runners-2a5a06c2/` holds the output of the ten contract and stage runners at #2015's head (`2a5a06c2`, 2026-11-12, GPU timeouts 0), one file per runner, with `log.txt` listing the exit codes. `tf-vulkan-kernel-contract.txt` includes `qmm_q4_tile_f32`: four kernels pass gates 1 to 4, eight controls caught. That runner output predates `qmm_q4_tile_rows` joining the contract on the sibling #2017; the current contract covers five kernels. `tf-stage1-gpu.txt` holds the projection cases with `tile=identical` (the tiled image against the default image, byte for byte, on the bf16 projections). No byte-level comparison of the f32 tile image against `qmm_f32` exists; the equal logits digests of the two heads are the identity evidence for it.
- `PREREG.md` was written before the first GPU run. Its addendum, written after the final pairs, says what changed and why.
- `raw-files-sha256.txt` lists the SHA-256 of every raw run file the summary was generated from (`final/` and `interim/`) for `summary.json`.

Expand Down
6 changes: 3 additions & 3 deletions docs/receipts/vulkan-qmm-tile-m1/pinned-heads-summary.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kernel_release": "7.3.0-2-20261110.1-lab-ARCH",
"settings": "free-running greedy, 64 steps, two runs per process, fresh process per run, one model process at a time, every process under taskset on the four P cores (CPU 4-7)",
"start_utc": "2026-11-12T16:16:03Z",
"old_build": "#1990 head (vec4 build), ELF sha256 prefix 46571f11",
"new_build": "#2015 head (tile build), ELF sha256 prefix 6e6f47f2",
"old_build": "#1990 measured build: source commit 46571f111b2f0e92963561702b7eaab41868f823; tf-qwen35-model ELF SHA-256 2a1d481c692900c1cefb08fa308d5852d2ec748ee7bc24c89ff013c26ac36d72 (6089352 bytes)",
"new_build": "#2015 measured build: source commit 6e6f47f228a662cb26dedf5b24bf7c50c780fe43; tf-qwen35-model ELF SHA-256 b3b7f5e17752b31370490a7d4fe3f4ff92cf3593ed3bed447759f52c5561de41 (6128512 bytes)",
"note": "Both include the float32 fix. The ELFs were built before the comment and name only commits that sit above them; those commits change no code.",
"gpu_timeouts": 0
},
Expand Down Expand Up @@ -207,4 +207,4 @@
]
}
]
}
}
8 changes: 4 additions & 4 deletions zig/src/families/qwen3_5/backend/vulkan/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const pinned = @import("../../config.zig");
const Engine = engine_mod.Engine;
const be = lanes.backend;

// Each prompt pass uses chunks of at most 32 rows.
pub const max_prompt_rows: usize = 32;
// Each prompt pass uses chunks of at most 64 rows: the served step is the engine's own chunk.
pub const max_prompt_rows: usize = engine_mod.max_prefill_rows;

/// The SDPA arm's visible-key limit, the state's capacity ceiling (state.zig).
pub const max_capacity = @import("state.zig").max_capacity;
Expand Down Expand Up @@ -91,12 +91,12 @@ pub const Vulkan = struct {
// steps floor 1: a prompt-only warm pass (max_new 0) still reads one row past the prompt
try engine_mod.planFor(self.engine.capacity(), 0, ids.len, @max(stream.max_new, 1));
try self.engine.reset(); // a new prompt starts from a really zeroed state (also clears a poisoned one)
try self.engine.prefill(ids); // the same canonical 32-row schedule the stepped path runs
try self.engine.prefill(ids); // the same canonical 64-row schedule the stepped path runs
stream.fill_at.store(@intCast(ids.len), .monotonic);
self.owner = stream;
}

// Each 32-row call checks ids and capacity before reset; visible() tracks progress.
// Each 64-row call checks ids and capacity before reset; visible() tracks progress.
fn prefillStepFn(ptr: *anyopaque, stream: *lanes.Stream, max_rows: u32) !bool {
const self = cast(ptr);
if (self.owner) |o| {
Expand Down
24 changes: 12 additions & 12 deletions zig/src/families/qwen3_5/backend/vulkan/engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ const View = vk.tensor.View;

pub const max_prefill_rows = fwd.max_prefill_rows;

// Chunk cut = min(done+32,total), pure, host-tested; totals 32/33/63/64/65 cut at 32 / 32,33 / 32,63 / 32,64 / 32,64,65
// Chunk cut = min(done+64,total), pure; the host tests cover totals 64, 65, 127, 128 and 129.
pub fn chunkEnd(done: usize, total: usize) usize {
return @min(done +| max_prefill_rows, total);
}

// The staged-transfer buffer: 8 MiB so the load's ~27k tensors stream in large chunks instead of one 64 KiB fence each.
pub const staging_bytes: u64 = 8 << 20;
// The 8 MiB scratch arena holds persistent windows and worst-layer prefill transients (~5.6 MiB), with headroom.
pub const arena_bytes: u64 = 8 << 20;
// The 16 MiB scratch arena holds persistent windows (~2.6 MB) and worst-layer prefill transients (~11.1 MB at 64 rows).
pub const arena_bytes: u64 = 16 << 20;

// Step errors include dispatch.Error; Timeout exits 124 and InFlight exits 1 inside `vk.orExit`, before teardown.
pub const Error = vk.dispatch.Error ||
Expand Down Expand Up @@ -190,7 +190,7 @@ pub const Engine = struct {
e.poisoned = false;
}

// Check ids/bounds, commit 32-row chunks. Failure poisons (partial RNN state never reused); refusals touch nothing.
// Check ids/bounds, commit 64-row chunks. Failure poisons (partial RNN state never reused); refusals touch nothing.
pub fn prefill(e: *Engine, tokens: []const u32) Error!void {
if (e.poisoned) return error.Poisoned;
if (tokens.len == 0) return error.UnsupportedPrompt;
Expand All @@ -204,7 +204,7 @@ pub const Engine = struct {
}
}

// One forward chunk has 1..32 checked rows; its logits give the next-token distribution after the chunk.
// One forward chunk has 1..64 checked rows; its logits give the next-token distribution after the chunk.
pub fn prefillChunk(e: *Engine, tokens: []const u32) Error!void {
if (e.poisoned) return error.Poisoned;
if (tokens.len == 0 or tokens.len > max_prefill_rows) return error.UnsupportedPrompt;
Expand Down Expand Up @@ -355,15 +355,15 @@ test "the plan refuses empty, stepless and overflowing sequences before any GPU
try testing.expectError(error.ContextTooLong, planFor(cap, std.math.maxInt(u64) - 1, 1, 3));
}

test "the canonical cut ends every whole-request length at 32-row boundaries" {
test "the canonical cut ends every whole-request length at 64-row boundaries" {
for ([_]struct { len: usize, cuts: []const usize }{
.{ .len = 1, .cuts = &.{1} },
.{ .len = 31, .cuts = &.{31} },
.{ .len = 32, .cuts = &.{32} },
.{ .len = 33, .cuts = &.{ 32, 33 } },
.{ .len = 63, .cuts = &.{ 32, 63 } },
.{ .len = 64, .cuts = &.{ 32, 64 } },
.{ .len = 65, .cuts = &.{ 32, 64, 65 } },
.{ .len = 63, .cuts = &.{63} },
.{ .len = 64, .cuts = &.{64} },
.{ .len = 65, .cuts = &.{ 64, 65 } },
.{ .len = 127, .cuts = &.{ 64, 127 } },
.{ .len = 128, .cuts = &.{ 64, 128 } },
.{ .len = 129, .cuts = &.{ 64, 128, 129 } },
}) |case| {
var done: usize = 0;
var at: usize = 0;
Expand Down
8 changes: 5 additions & 3 deletions zig/src/families/qwen3_5/backend/vulkan/forward.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Arena = vk.arena.Arena;
const View = vk.tensor.View;

// Whole-prompt rows are split into dispatch chunks capped here; persistent windows use this cap.
pub const max_prefill_rows: usize = 32;
pub const max_prefill_rows: usize = 64;

// Layers 0-3 keep every activation store in f32; 4+ round stores to bf16 (the validated store policy).
pub const wide_layers: usize = state.wide_layers;
Expand All @@ -19,7 +19,7 @@ pub fn actKind(layer: usize) vk.tensor.Kind {
return if (layer < wide_layers) .f32 else .bf16;
}
/// The GDN prefill scan's chunk cap (chunks of 1 when a prefill fits one).
pub const max_gdn_chunk: u64 = 32;
pub const max_gdn_chunk: u64 = max_prefill_rows;

comptime {
if (2 * pinned.query_heads * pinned.head_dim != 4096)
Expand Down Expand Up @@ -189,7 +189,9 @@ test "extractQPlan derives the interleaved geometry and refuses torn views" {
try testing.expectEqual(@as(u32, 24), plan.heads_rows);
try testing.expectEqual(@as(u32, 512), plan.in_head_stride);
try testing.expectEqual(@as(?ExtractQPlan, null), extractQPlan(0, 0, 0));
try testing.expectEqual(@as(?ExtractQPlan, null), extractQPlan(33, 33 * 2048, 33 * 4096)); // past the prefill cap
const past = max_prefill_rows + 1;
// Refuse a request past the prefill cap.
try testing.expectEqual(@as(?ExtractQPlan, null), extractQPlan(past, past * 2048, past * 4096));
try testing.expectEqual(@as(?ExtractQPlan, null), extractQPlan(2, 2 * 2048, 2 * 4095));
try testing.expectEqual(@as(?ExtractQPlan, null), extractQPlan(2, 2 * 2047, 2 * 4096));
}
7 changes: 5 additions & 2 deletions zig/src/vulkan/ops.zig
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,17 @@ pub const Ops = struct {

const testing = std.testing;

test "deinit destroys every pipeline init builds" {
test "deinit destroys every pipeline init builds exactly once" {
const src = @embedFile("ops.zig");
const start = std.mem.indexOf(u8, src, "pub fn deinit(").?;
const body = src[start..std.mem.indexOfPos(u8, src, start, "\n }\n").?];
const info = @typeInfo(Ops).@"struct";
inline for (info.field_names, info.field_types) |name, ty| {
if (ty == dispatch.Pipeline) {
try testing.expect(std.mem.indexOf(u8, body, "o." ++ name ++ ".deinit();") != null);
const call = "o." ++ name ++ ".deinit();";
const first = std.mem.indexOf(u8, body, call) orelse
return testing.expect(false);
try testing.expect(std.mem.indexOf(u8, body[first + call.len ..], call) == null);
}
}
}
Expand Down
26 changes: 25 additions & 1 deletion zig/src/vulkan/ops_q4.zig
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ pub fn tileImage(act_f32: bool, m: u64) TileImage {
return if (m >= tile_rows) .tile_rows else .tile;
}

/// Rows image X groups for an m x n grid; past max_x_groups, the previous tile image must serve.
pub fn rowsGroups(m: u64, n: u64, local: u64) u64 {
const threads = (m + tile_rows - 1) / tile_rows * ((n + 31) / 32 * 16);
return (threads + local - 1) / local;
}

/// `tileImage`, but the previous tile image serves any rows grid that would not fit the device's X group limit.
pub fn tileImageFits(act_f32: bool, m: u64, n: u64, local: u64, max_x_groups: u64) TileImage {
const image = tileImage(act_f32, m);
if (image == .tile_rows and rowsGroups(m, n, local) > max_x_groups) return .tile;
return image;
}

/// `project` over weights repacked by `tile`: one dispatch, so w must fit one binding and start 16-byte aligned.
pub fn projectTiled(o: *Ops, cmds: *Commands, out: View, x: View, w: View, scales: View, biases: View, k: u64, timeout_ns: u64) Error!void {
try tiledActivation(out, x);
Expand All @@ -122,7 +135,7 @@ pub fn projectTiled(o: *Ops, cmds: *Commands, out: View, x: View, w: View, scale
const range = o.dev.properties.limits.maxStorageBufferRange;
if (x.byteLen() > range or out.byteLen() > range or w.byteLen() > range) return error.TooLarge;
if (qmmVariant(x, w) != .vec4) return error.Misaligned;
const image = tileImage(out.kind == .f32, g.m);
const image = tileImageFits(out.kind == .f32, g.m, g.n, o.qmm_q4_tile_rows.local[0], o.dev.properties.limits.maxComputeWorkGroupCount[0]);
const pipe = switch (image) {
.tile_f32 => &o.qmm_q4_tile_f32,
.tile_rows => &o.qmm_q4_tile_rows,
Expand Down Expand Up @@ -319,6 +332,17 @@ test "the tile image follows the activation kind and the chunk rows" {
try testing.expectEqual(TileImage.tile_rows, tileImage(false, 33));
}

test "the rows image yields to the tile when its grid cannot fit the X limit" {
// BF16 aligned m=4194304, n=1, k=64: rows needs 65,536 groups; tile's 16,384 fit.
try testing.expectEqual(@as(u64, 65536), rowsGroups(4194304, 1, 256));
try testing.expectEqual(TileImage.tile, tileImageFits(false, 4194304, 1, 256, 65535));
try testing.expectEqual(TileImage.tile_rows, tileImageFits(false, 4194304, 1, 256, 65536));
// Small grids and f32 activations are untouched by the limit.
try testing.expectEqual(TileImage.tile_rows, tileImageFits(false, 33, 64, 256, 65535));
try testing.expectEqual(TileImage.tile, tileImageFits(false, 3, 64, 256, 65535));
try testing.expectEqual(TileImage.tile_f32, tileImageFits(true, 4194304, 1, 256, 65535));
}

test "the vec4 qmm image is chosen only when every vector load is 16-byte aligned" {
const dev: @import("device.zig").Device = undefined;
const buf = @import("memory.zig").Buffer{ .dev = &dev, .handle = null, .memory = null, .len = 1 << 20, .kind = .device, .mapped = null, .coherent = false };
Expand Down
8 changes: 4 additions & 4 deletions zig/tests/vulkan/contract_attention.zig
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn visibleKeys(case: SdpaCase, r: u64) u64 {
return case.prefix + r + 1;
}

// Cases test n=1,15/16/17,255/256/257,1023/1024/1025,7167/7168 and rows 17/32 at 1024/7168 limits.
// Cases test n=1,15/16/17,255/256/257,1023/1024/1025,7167/7168 and rows 17/64 at 1024/7168 limits.
pub const served_cases = [_]SdpaCase{
.{ .name = "decode n=1", .rows = 1, .prefix = 0 },
.{ .name = "decode n=16 tile", .rows = 1, .prefix = 15 },
Expand All @@ -88,9 +88,9 @@ pub const served_cases = [_]SdpaCase{
.{ .name = "decode n=1025 stride+1", .rows = 1, .prefix = 1024 },
.{ .name = "decode n=7167 stream-1", .rows = 1, .prefix = 7166 },
.{ .name = "decode n=7168 stream", .rows = 1, .prefix = 7167 },
.{ .name = "prefill rows32 n1..32", .rows = 32, .prefix = 0 },
.{ .name = "prefill rows64 n1..64", .rows = 64, .prefix = 0 },
.{ .name = "prefill rows17 across 1024", .rows = 17, .prefix = 1007 },
.{ .name = "prefill rows32 to 7168", .rows = 32, .prefix = 7136 },
.{ .name = "prefill rows64 to 7168", .rows = 64, .prefix = 7104 },
};

// Attend f64 dots/max/softmax/output for n=prefix+r+1 in (kv_head,capacity,head_dim); matches fx.Attend.init order.
Expand Down Expand Up @@ -343,7 +343,7 @@ test "generalized reference equals the fixture's Attend element for element" {

test "served cases stay inside the engine's shapes and hit every reduction edge" {
for (served_cases) |case| {
try testing.expect(case.rows == 1 or case.rows <= 32); // decode rows 1, prefill <= 32
try testing.expect(case.rows == 1 or case.rows <= 64); // decode rows 1, prefill <= max_prefill_rows (64)
const capacity = capacityOf(case);
try testing.expect(capacity <= max_visible_keys); // kStreamKeys
try testing.expectEqual(visibleKeys(case, case.rows - 1), capacity); // exact fit
Expand Down
2 changes: 1 addition & 1 deletion zig/tests/vulkan/contract_attention_main.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// The attention gate checks rope_bf16, sdpa_rows_bf16 and sdpa_rows_f32q: decode=1, prefill<=32, keys=1..7168.
// The attention gate checks rope_bf16, sdpa_rows_bf16 and sdpa_rows_f32q: decode=1, prefill<=64, keys=1..7168.
const std = @import("std");
const vk = @import("vulkan");
const shaders = @import("stage3_shaders");
Expand Down
4 changes: 2 additions & 2 deletions zig/tests/vulkan/contract_pointwise.zig
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ pub const GatedCase = struct {
at: GatedOffsets = .{},
};

// Served tokens: one decode row and a prefill chunk (rows = 1, max_prefill_rows = 32), plus controls on three tokens.
// Served tokens: one decode row and a prefill chunk (rows = 1, max_prefill_rows = 64), plus controls on three tokens.
pub const gated_cases = [_]GatedCase{
.{ .name = "1x16x128", .tokens = 1, .at = .{ .x = 128, .weight = 0, .gate = 256, .out = 384 } },
.{ .name = "32x16x128 dynamic zero token", .tokens = 32, .fill = .dynamic, .zero_token = true, .at = .{ .x = 128, .weight = 0, .gate = 256, .out = 384 } },
.{ .name = "64x16x128 dynamic zero token", .tokens = 64, .fill = .dynamic, .zero_token = true, .at = .{ .x = 128, .weight = 0, .gate = 256, .out = 384 } },
.{ .name = "controls 3x16x128", .tokens = 3, .controls = true, .at = .{ .x = 128, .weight = 0, .gate = 256, .out = 384 } },
};

Expand Down
Loading
Loading