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

## Unreleased

- Qwen3.5 on CUDA: EXL3 checkpoints with the mcg codebook marker are admitted alongside mul1 ones, and unknown markers are still refused as an unsupported quantization (#3412). Thanks to @Ch0mpaa.

- CUDA on unified-memory Linux (GB10): the start-up memory budget is at most the free memory CUDA reports, less the same reserve, instead of `/proc/meminfo` alone, and the server first drops the checkpoint's own shards from the page cache, so a checkpoint that was just read is not refused for its own cached pages; discrete cards, Windows, Metal and HIP are unchanged (#3192, part of #1857).

- Qwen 3.5/3.6/3.8 on CUDA: a prompt chunk before the last that nothing reads (no last-layer tap, learning, MLP or pocket reader, prompt int8 path or image) stops its last layer after its keys, values and RoPE, skipping that layer's query projection, attention, output projection, MLP and head, with the same reply tokens; the final chunk is unchanged (#3060, fixes #2941). Thanks to @javierbits.



- LFM3 on Metal: a new prompt is filled a chunk at a time between other streams' decode rounds instead of stalling them for its whole prefill, and a pass can be cancelled keeping the state at the last chunk's end; chunks are cut exactly as before, so replies are unchanged (#3088, part of #3021).

- LFM3 on Metal: in a shared round, every stream's qk now runs in one dispatch instead of once per stream per attention layer, with the same arithmetic per row, so replies are unchanged (#3079).
Expand Down
26 changes: 20 additions & 6 deletions zig/src/families/qwen3_5/cuda_exl3_admit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,47 @@ const std = @import("std");
const core = @import("core");
const cuda = @import("cuda");
const mul1_magic: u32 = 0x83DCD12D;
const mcg_magic: u32 = 0xCBAC1FED;
fn name(buf: []u8, comptime fmt: []const u8, args: anytype) ![]const u8 {
return std.fmt.bufPrint(buf, fmt, args);
}

/// One EXL3 group's tensors, checked: trellis [k/16][n/16][16 * bits] i16, the mul1 marker, fp16 suh [k] and svh [n].
pub const Group = struct { trellis: core.checkpoint.Tensor, suh: core.checkpoint.Tensor, svh: core.checkpoint.Tensor, halves: u32 };
/// One EXL3 group's tensors: trellis [k/16][n/16][16*bits] i16, a mul1/mcg marker, fp16 suh [k] and svh [n].
pub const Group = struct { trellis: core.checkpoint.Tensor, suh: core.checkpoint.Tensor, svh: core.checkpoint.Tensor, halves: u32, codebook: cuda.exl3.Codebook };

pub fn group(ck: *core.Checkpoint, base: []const u8, n: usize, k: usize) !Group {
if (n == 0 or k == 0 or n % 128 != 0 or k % 128 != 0 or n > std.math.maxInt(u32) or k > std.math.maxInt(u32)) return error.UnexpectedTensor;
var buf: [240]u8 = undefined;
var mcg_buf: [240]u8 = undefined;
const t = try ck.get(try name(&buf, "{s}.trellis", .{base}));
if (t.dtype != .i16 or t.rank != 3 or t.dim(0) != k / 16 or t.dim(1) != n / 16 or t.dim(2) % 8 != 0) {
return error.UnexpectedTensor;
}
const halves = t.dim(2) / 8;
if (halves < 2 or halves > 16) return error.UnsupportedQwenQuantization;
const marker = try ck.get(try name(&buf, "{s}.mul1", .{base}));
if ((marker.dtype != .i32 and marker.dtype != .u32) or marker.numel() != 1 or marker.bytes.len != 4 or std.mem.readInt(u32, marker.bytes[0..4], .little) != mul1_magic) return error.UnsupportedQwenQuantization;
// Accept .mul1 (magic 0x83DCD12D) or .mcg (magic 0xCBAC1FED); exactly one marker tensor per group.
const mul1_name = try name(&buf, "{s}.mul1", .{base});
const mcg_name = try name(&mcg_buf, "{s}.mcg", .{base});
if (ck.has(mul1_name) and ck.has(mcg_name)) return error.UnsupportedQwenQuantization;
const cb: cuda.exl3.Codebook = if (ck.has(mul1_name)) blk: {
const marker = try ck.get(mul1_name);
if ((marker.dtype != .i32 and marker.dtype != .u32) or marker.numel() != 1 or marker.bytes.len != 4 or std.mem.readInt(u32, marker.bytes[0..4], .little) != mul1_magic) return error.UnsupportedQwenQuantization;
break :blk .mul1;
} else blk: {
if (!ck.has(mcg_name)) return error.UnsupportedQwenQuantization;
const marker = try ck.get(mcg_name);
if ((marker.dtype != .i32 and marker.dtype != .u32) or marker.numel() != 1 or marker.bytes.len != 4 or std.mem.readInt(u32, marker.bytes[0..4], .little) != mcg_magic) return error.UnsupportedQwenQuantization;
break :blk .mcg;
};
const suh = try ck.get(try name(&buf, "{s}.suh", .{base}));
const svh = try ck.get(try name(&buf, "{s}.svh", .{base}));
if (!suh.is(.f16, &.{k}) or !svh.is(.f16, &.{n})) return error.UnexpectedTensor;
return .{ .trellis = t, .suh = suh, .svh = svh, .halves = @intCast(halves) };
return .{ .trellis = t, .suh = suh, .svh = svh, .halves = @intCast(halves), .codebook = cb };
}

/// Dense projections must also have a compiled linear variant.
pub fn denseGroup(ck: *core.Checkpoint, base: []const u8, n: usize, k: usize) !Group {
const g = try group(ck, base, n, k);
if (cuda.exl3.variantIndex(g.halves, .mul1, 4) == null) return error.UnsupportedQwenQuantization;
if (cuda.exl3.variantIndex(g.halves, g.codebook, 4) == null) return error.UnsupportedQwenQuantization;
return g;
}
76 changes: 76 additions & 0 deletions zig/src/families/qwen3_5/cuda_exl3_loader_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,79 @@ test "EXL3 admission: mixed supported widths and allocator failures stay host ow
try write(tmp.dir, .{ .scale = "F32" });
try t.expectError(error.UnexpectedTensor, read(t.allocator, path));
}

// --- Stage 6c: mcg codebook admission tests ---

const cuda_exl3 = @import("cuda").exl3;

fn writeMcg(dir: std.Io.Dir, halves: usize, magic: u32) !void {
const tn = 8 * 8 * halves * 8 * 2;
const sn: usize = 256;
const header = try std.fmt.allocPrint(t.allocator, "{{\"p.trellis\":{{\"dtype\":\"I16\",\"shape\":[8,8,{d}],\"data_offsets\":[0,{d}]}}," ++
"\"p.mcg\":{{\"dtype\":\"I32\",\"shape\":[],\"data_offsets\":[{d},{d}]}}," ++
"\"p.suh\":{{\"dtype\":\"F16\",\"shape\":[128],\"data_offsets\":[{d},{d}]}}," ++
"\"p.svh\":{{\"dtype\":\"F16\",\"shape\":[128],\"data_offsets\":[{d},{d}]}}}}", .{ halves * 8, tn, tn, tn + 4, tn + 4, tn + 4 + sn, tn + 4 + sn, tn + 4 + sn + 256 });
defer t.allocator.free(header);
const data = try t.allocator.alloc(u8, 8 + header.len + tn + 4 + sn + 256);
defer t.allocator.free(data);
@memset(data, 0);
std.mem.writeInt(u64, data[0..8], header.len, .little);
@memcpy(data[8..][0..header.len], header);
std.mem.writeInt(u32, data[8 + header.len + tn ..][0..4], magic, .little);
try dir.writeFile(t.io, .{ .sub_path = "model.safetensors", .data = data });
}

test "EXL3 admission: an mcg-codebook group admits and reports codebook .mcg" {
var tmp = t.tmpDir(.{});
defer tmp.cleanup();
const path = try pathOf(tmp);
defer t.allocator.free(path);
try writeMcg(tmp.dir, 16, 0xCBAC1FED);
var ck = try core.Checkpoint.openModel(t.allocator, t.io, path);
defer ck.close();
const g = try admit.group(&ck, "p", 128, 128);
try t.expectEqual(cuda_exl3.Codebook.mcg, g.codebook);
try t.expectEqual(@as(u32, 16), g.halves);
_ = try admit.denseGroup(&ck, "p", 128, 128);
}

test "EXL3 admission: an mcg marker with the wrong magic is refused" {
var tmp = t.tmpDir(.{});
defer tmp.cleanup();
const path = try pathOf(tmp);
defer t.allocator.free(path);
try writeMcg(tmp.dir, 16, 0xDEADBEEF);
var ck = try core.Checkpoint.openModel(t.allocator, t.io, path);
defer ck.close();
try t.expectError(error.UnsupportedQwenQuantization, admit.group(&ck, "p", 128, 128));
}

fn writeBoth(dir: std.Io.Dir, halves: usize) !void {
const tn = 8 * 8 * halves * 8 * 2;
const sn: usize = 256;
const header = try std.fmt.allocPrint(t.allocator, "{{\"p.trellis\":{{\"dtype\":\"I16\",\"shape\":[8,8,{d}],\"data_offsets\":[0,{d}]}}," ++
"\"p.mul1\":{{\"dtype\":\"I32\",\"shape\":[],\"data_offsets\":[{d},{d}]}}," ++
"\"p.mcg\":{{\"dtype\":\"I32\",\"shape\":[],\"data_offsets\":[{d},{d}]}}," ++
"\"p.suh\":{{\"dtype\":\"F16\",\"shape\":[128],\"data_offsets\":[{d},{d}]}}," ++
"\"p.svh\":{{\"dtype\":\"F16\",\"shape\":[128],\"data_offsets\":[{d},{d}]}}}}", .{ halves * 8, tn, tn, tn + 4, tn + 4, tn + 8, tn + 8, tn + 8 + sn, tn + 8 + sn, tn + 8 + sn + 256 });
defer t.allocator.free(header);
const data = try t.allocator.alloc(u8, 8 + header.len + tn + 8 + sn + 256);
defer t.allocator.free(data);
@memset(data, 0);
std.mem.writeInt(u64, data[0..8], header.len, .little);
@memcpy(data[8..][0..header.len], header);
std.mem.writeInt(u32, data[8 + header.len + tn ..][0..4], 0x83DCD12D, .little);
std.mem.writeInt(u32, data[8 + header.len + tn + 4 ..][0..4], 0xCBAC1FED, .little);
try dir.writeFile(t.io, .{ .sub_path = "model.safetensors", .data = data });
}

test "EXL3 admission: a group carrying both mul1 and mcg markers is refused" {
var tmp = t.tmpDir(.{});
defer tmp.cleanup();
const path = try pathOf(tmp);
defer t.allocator.free(path);
try writeBoth(tmp.dir, 16);
var ck = try core.Checkpoint.openModel(t.allocator, t.io, path);
defer ck.close();
try t.expectError(error.UnsupportedQwenQuantization, admit.group(&ck, "p", 128, 128));
}
10 changes: 8 additions & 2 deletions zig/src/families/qwen3_5/cuda_exl3_weights.zig
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn dense(s: *Sink, kit: ?*Kit, ck: *core.Checkpoint, prefix: []const u8, mod: []
.k = @intCast(k),
.n = @intCast(n),
.halves = gr.halves,
.codebook = .mul1,
.codebook = gr.codebook,
.stride_k = st.k,
.stride_nb = st.nb,
};
Expand Down Expand Up @@ -180,6 +180,7 @@ fn experts(s: *Sink, ck: *core.Checkpoint, p: []const u8) !cuda.experts.Layer {
var suh: [3]u64 = undefined;
var svh: [3]u64 = undefined;
var buf: [200]u8 = undefined;
var expert_cb: ?cuda.exl3.Codebook = null;
for (mods, 0..) |mod, m| {
const k = if (m == 2) g.intermediate else g.hidden;
const n = if (m == 2) g.hidden else g.intermediate;
Expand All @@ -194,6 +195,11 @@ fn experts(s: *Sink, ck: *core.Checkpoint, p: []const u8) !cuda.experts.Layer {
for (0..count) |e| {
const base = if (e < g.experts) try name(&buf, "{s}mlp.experts.{d}.{s}", .{ p, e, mod }) else try name(&buf, "{s}mlp.shared_expert.{s}", .{ p, mod });
const gr = try group(ck, base, n, k);
if (expert_cb) |ecb| {
if (ecb != gr.codebook) return error.UnsupportedQwenQuantization;
} else {
expert_cb = gr.codebook;
}
tp[e] = try s.put(gr.trellis.bytes);
widths[e] = @intCast(gr.halves);
@memcpy(su[e * k * 2 ..][0 .. k * 2], gr.suh.bytes);
Expand All @@ -220,7 +226,7 @@ fn experts(s: *Sink, ck: *core.Checkpoint, p: []const u8) !cuda.experts.Layer {
.experts = count,
.dims = g.hidden,
.width = g.intermediate,
.codebook = .mul1,
.codebook = expert_cb orelse return error.UnsupportedQwenQuantization,
.limit = 0, // Qwen's SwiGLU is unclamped
.act_mode = 1, // fp32 silu(gate) * up
};
Expand Down