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

- Qwen 3.5 on CUDA: the mixers' and the dense MLP's scratch rows share one region, freeing 0.46 GiB on the 27B with the same tokens and speed (#1476). Thanks to @diwakergupta.

- The kernel contract checks Qwen3.8-27B's attention_io pack, append and keep at the other two head shapes the family admits, GQA16 (32 query heads over 2) and the 9B's 16 over 4 (#2063). Thanks to @cshintov.

- `kernel-contract` frees each kernel's device buffers once its gates are done, so the full suite fits on a 16 GiB card instead of running out of memory in the MoE backward suite (#2042). Thanks to @BlivionIaG.
Expand Down
14 changes: 14 additions & 0 deletions zig/qwen35_formats_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,17 @@ test "format coexistence: EXL3 BF16 prompt: existing rows and partial final slic
test {
_ = @import("src/families/qwen3_5/cuda_host_embed.zig"); // where an EXL3 checkpoint's embedding rows live
}

test "format coexistence: disjoint mixer and dense MLP scratch regions share one arena union" {
const c = try config(exl3, false, false);
defer t.allocator.free(c);
var e: Engine = undefined;
e.g = try geo.parse(t.allocator, c);
e.arena = .{ .d = undefined, .ptr = 0x100000, .len = 1 << 30 };
e.arena_at = 0;
const measured = try e.scratch(.measure);
e.arena_at = 0;
try t.expectEqual(measured, try e.scratch(.allocate));
try t.expectEqual(e.y, e.gate);
try t.expectEqual(e.z, e.qraw);
}
43 changes: 4 additions & 39 deletions zig/src/families/qwen3_5/cuda_engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ pub const max_segs = Round.max_segs;
/// Rows of a verify forward at most: each row's logits are kept and drawn.
pub const most_verify = Round.most_verify;
/// tf_q35_argmax_slices' blocks a row (ARGMAX_SLICE entries each).
fn argmaxSlices(vocab: usize) usize {
pub fn argmaxSlices(vocab: usize) usize {
return (vocab + 4095) / 4096;
}
/// A window's rows at most (exact: one-row steps' bits; qwen35.cuh's MAXW, the rows a stream's log holds).
pub const most_window = Round.most_window;
/// Partial attention chunks a row at most (qwen35.cuh's MAXCH): each row's chunk width grows with its own keys.
const max_chunks = 64;
pub const max_chunks = 64;

/// Device allocations are 256-byte aligned, as the driver's are.
pub fn aligned(n: usize) usize {
Expand Down Expand Up @@ -336,43 +336,8 @@ pub const Engine = struct {
return e.arena.ptr + at;
}

/// The scratch rows' bytes (`measure`: summed, nothing taken), or the rows themselves from the arena.
pub fn scratch(e: *Engine, mode: enum { measure, allocate }) !usize {
var total: usize = 0;
const Take = struct {
e: *Engine,
mode: @TypeOf(mode),
total: *usize,
pub fn alloc(t: @This(), bytes: usize) !u64 {
t.total.* += aligned(bytes);
return if (t.mode == .measure) 0 else try t.e.alloc(bytes);
}
};
const t = Take{ .e = e, .mode = mode, .total = &total };
const g = e.g;
const R = max_rows;
const wide = @max(g.valueWidth(), g.attnWidth(), g.hidden); // y and mix: either mixer's rows
inline for (.{ "h", "x", "r" }) |name| @field(e, name) = try t.alloc(R * g.hidden * 2);
inline for (.{ "y", "mix", "z", "v" }) |name| @field(e, name) = try t.alloc(R * wide * 2);
inline for (.{ "q", "k" }) |name| @field(e, name) = try t.alloc(R * g.k_heads * geometry.dk * 2);
e.qkv = try t.alloc(R * g.convWidth() * 2);
inline for (.{ "gate", "up", "act" }) |name| @field(e, name) = try t.alloc(R * g.intermediate * 2);
e.xs = try t.alloc(R * (@max(g.intermediate, wide) / 64) * 4);
inline for (.{ "a", "b", "beta" }) |name| @field(e, name) = try t.alloc(R * g.v_heads * 2);
e.g_ = try t.alloc(R * g.v_heads * 4);
e.qraw = try t.alloc(R * 2 * g.attnWidth() * 2);
e.qn = try t.alloc(R * g.attnWidth() * 2);
inline for (.{ "kraw", "vraw", "kn" }) |name| @field(e, name) = try t.alloc(R * g.kv_heads * head_dim * 2);
// the decode partials: verify rows only (a prompt chunk's attention runs cuda_prompt_attention's tiles)
e.pm = try t.alloc(most_verify * g.q_heads * max_chunks * 4);
e.pl = try t.alloc(most_verify * g.q_heads * max_chunks * 4);
e.po = try t.alloc(most_verify * g.q_heads * max_chunks * head_dim * 4);
e.logits = try t.alloc(most_verify * g.vocab * 2);
if (g.moe()) e.routed = try routed.Scratch.carve(t, g, R);
e.amax = try t.alloc(most_verify * argmaxSlices(g.vocab) * 8);
e.picks = try t.alloc(most_verify * cuda.sample.Draws.row_scratch);
return total;
}
/// The scratch rows' bytes, or the rows themselves from the arena (cuda_scratch.zig).
pub const scratch = @import("cuda_scratch.zig").scratch;

pub fn run(e: *Engine, f: cuda.Function, grid: [3]usize, block: [3]usize, args: anytype) !void {
var a: cuda.Args = .{};
Expand Down
76 changes: 76 additions & 0 deletions zig/src/families/qwen3_5/cuda_scratch.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! The engine's scratch rows from its arena: the two mixers' rows and the dense MLP's share one region.
const E = @import("cuda_engine.zig");
const cuda = @import("cuda");
const geometry = @import("cuda_geometry.zig");
const routed = @import("cuda_routed.zig");

pub const Mode = enum { measure, allocate };

/// What a carve has taken: bytes counted (`measure`) or arena allocations (`allocate`).
const Take = struct {
e: *E.Engine,
mode: Mode,
total: usize = 0,

const Mark = struct { total: usize, at: usize };

pub fn alloc(t: *Take, bytes: usize) !u64 {
t.total += E.aligned(bytes);
return if (t.mode == .measure) 0 else try t.e.alloc(bytes);
}

fn mark(t: *const Take) Mark {
return .{ .total = t.total, .at = t.e.arena_at };
}

/// Back to `start` for a region that never runs at once with the last; returns where the last ended.
fn rewind(t: *Take, start: Mark) Mark {
const end = t.mark();
t.total = start.total;
t.e.arena_at = start.at;
return end;
}

/// Past the furthest of the shared regions' ends.
fn past(t: *Take, ends: []const Mark) void {
for (ends) |m| {
t.total = @max(t.total, m.total);
t.e.arena_at = @max(t.e.arena_at, m.at);
}
}
};

/// The scratch rows' bytes (`measure`: summed, nothing taken), or the rows themselves from the arena.
pub fn scratch(e: *E.Engine, mode: Mode) !usize {
var t: Take = .{ .e = e, .mode = mode };
const g = e.g;
const R = E.max_rows;
const wide = @max(g.valueWidth(), g.attnWidth(), g.hidden); // y and mix: either mixer's rows
inline for (.{ "h", "x", "r" }) |name| @field(e, name) = try t.alloc(R * g.hidden * 2);
e.xs = try t.alloc(R * (@max(g.intermediate, wide) / 64) * 4);
// a layer's mixer, then its MLP, pass only h, x, xs and r (outside it): their rows share one start
const shared = t.mark();
inline for (.{ "y", "mix", "z", "v" }) |name| @field(e, name) = try t.alloc(R * wide * 2);
inline for (.{ "q", "k" }) |name| @field(e, name) = try t.alloc(R * g.k_heads * geometry.dk * 2);
e.qkv = try t.alloc(R * g.convWidth() * 2);
inline for (.{ "a", "b", "beta" }) |name| @field(e, name) = try t.alloc(R * g.v_heads * 2);
e.g_ = try t.alloc(R * g.v_heads * 4);
const delta = t.rewind(shared);
inline for (.{ "y", "mix" }) |_| _ = try t.alloc(R * wide * 2); // the attention mixer's y and mix: the same rows
e.qraw = try t.alloc(R * 2 * g.attnWidth() * 2);
e.qn = try t.alloc(R * g.attnWidth() * 2);
inline for (.{ "kraw", "vraw", "kn" }) |name| @field(e, name) = try t.alloc(R * g.kv_heads * geometry.head_dim * 2);
const attention = t.rewind(shared);
inline for (.{ "gate", "up", "act" }) |name| @field(e, name) = try t.alloc(R * g.intermediate * 2);
t.past(&.{ delta, attention });
// the decode partials: verify rows only (a prompt chunk's attention runs cuda_prompt_attention's tiles)
const partials = E.most_verify * g.q_heads * E.max_chunks;
e.pm = try t.alloc(partials * 4);
e.pl = try t.alloc(partials * 4);
e.po = try t.alloc(partials * geometry.head_dim * 4);
e.logits = try t.alloc(E.most_verify * g.vocab * 2);
if (g.moe()) e.routed = try routed.Scratch.carve(&t, g, R);
e.amax = try t.alloc(E.most_verify * E.argmaxSlices(g.vocab) * 8);
e.picks = try t.alloc(E.most_verify * cuda.sample.Draws.row_scratch);
return t.total;
}
Loading