From aa201b39c44b2f2af9a7cacffc62193609f7fbe7 Mon Sep 17 00:00:00 2001 From: laudney <106273+laudney@users.noreply.github.com> Date: Sun, 15 Nov 2026 21:40:31 +0000 Subject: [PATCH] refactor(dsv41): projection forms in the CUDA plan The CUDA plan typed every projection as an EXL3 group, so another weight format would have to thread branches through every reader. GroupPlan and ExpertsPlan now hold their stored form in a tagged union with an EXL3 arm, readers go through it, and the expert width splits on a per-format block (expertShare: 128 for EXL3, 64 for the release format). EXL3 plans, emitted calls and pinned fingerprints are unchanged; block.Options gains the checkpoint format for the release arms to come. --- zig/src/families/deepseek_v41/block.zig | 2 + .../families/deepseek_v41/block_admission.zig | 2 +- zig/src/families/deepseek_v41/block_moe.zig | 2 +- .../families/deepseek_v41/block_prefetch.zig | 2 +- .../deepseek_v41/block_prefill_moe.zig | 2 +- .../families/deepseek_v41/cuda_weights.zig | 6 +- zig/src/families/deepseek_v41/forms.zig | 2 +- .../families/deepseek_v41/gate_forward.zig | 4 +- zig/src/families/deepseek_v41/host_test.zig | 32 ++++---- zig/src/families/deepseek_v41/load_test.zig | 2 +- zig/src/families/deepseek_v41/named.zig | 4 +- zig/src/families/deepseek_v41/partition.zig | 25 +++++- zig/src/families/deepseek_v41/plan.zig | 76 +++++++++++++------ zig/src/families/deepseek_v41/stage.zig | 28 +++---- zig/src/families/deepseek_v41/world3_test.zig | 16 ++-- .../deepseek_v41/world4_plan_test.zig | 28 ++++++- zig/src/families/deepseek_v41/world4_test.zig | 17 +++++ 17 files changed, 170 insertions(+), 80 deletions(-) diff --git a/zig/src/families/deepseek_v41/block.zig b/zig/src/families/deepseek_v41/block.zig index f247daf1906..87552688a22 100644 --- a/zig/src/families/deepseek_v41/block.zig +++ b/zig/src/families/deepseek_v41/block.zig @@ -30,6 +30,8 @@ pub const Widths = struct { /// Run-time sizes fixed at load (scratch and pools), and the L2 prefetch settings. pub const Options = struct { + /// how the checkpoint stores its projections (config.json): the emitter's projection bodies follow it + format: @import("config.zig").Format = .exl3, /// TF_DSV41_IMAGES=native: this prefill segment holds image positions, so Engram fuses with a keep vector image_keep: bool = false, /// TF_DSV41_BIAS_VL: the segment's image rows (MoE call apart, gate.bias_vl); 0: none; < 0: the buffer plan's diff --git a/zig/src/families/deepseek_v41/block_admission.zig b/zig/src/families/deepseek_v41/block_admission.zig index 67b68d823ab..b100574219f 100644 --- a/zig/src/families/deepseek_v41/block_admission.zig +++ b/zig/src/families/deepseek_v41/block_admission.zig @@ -15,7 +15,7 @@ pub fn nativePrefill(c: *const Config, world: u32) !void { pub fn options(c: *const Config, o: anytype, layers: []const u32, n: i64) !void { const rank = if (@hasField(@TypeOf(o), "rank")) o.rank else 0; - const part = @import("partition.zig").share(c.expert_width, rank, o.world) catch return error.InvalidBlock; + const part = @import("partition.zig").expertShare(c, rank, o.world) catch return error.InvalidBlock; if (o.expert_rows < 0 or o.dspark_rows < 0 or o.branch_rows < 0 or o.prefill_rows < 0 or o.image_rows < -1) return error.InvalidBlock; for (layers) |l| { if (l >= c.allLayers()) return error.InvalidBlock; diff --git a/zig/src/families/deepseek_v41/block_moe.zig b/zig/src/families/deepseek_v41/block_moe.zig index a9ff68162fc..0702c061c50 100644 --- a/zig/src/families/deepseek_v41/block_moe.zig +++ b/zig/src/families/deepseek_v41/block_moe.zig @@ -142,7 +142,7 @@ pub fn moe(e: *E, L: u32) !void { const ex = e.cfg.expertsOf(L); const slots: i64 = ex.topk + 1; const Et: i64 = ex.count + 1; - const I: i64 = (try @import("partition.zig").share(e.cfg.expert_width, e.o.rank, e.o.world)).len; + const I: i64 = (try @import("partition.zig").expertShare(e.cfg, e.o.rank, e.o.world)).len; const P = n * slots; const rows: i64 = if (backbone) e.o.expert_rows else e.o.dspark_rows; const sc: []const u8 = if (backbone) "s.ex" else "s.dx"; diff --git a/zig/src/families/deepseek_v41/block_prefetch.zig b/zig/src/families/deepseek_v41/block_prefetch.zig index 3afd452f4a3..485976f0db2 100644 --- a/zig/src/families/deepseek_v41/block_prefetch.zig +++ b/zig/src/families/deepseek_v41/block_prefetch.zig @@ -36,7 +36,7 @@ pub fn l2pfSources(e: *Emitter, L: u32, site: L2Site) ![]const L2Src { }, .f => { const D: i64 = e.cfg.hidden; - const I: i64 = (try @import("partition.zig").share(e.cfg.expert_width, e.o.rank, e.o.world)).len; + const I: i64 = (try @import("partition.zig").expertShare(e.cfg, e.o.rank, e.o.world)).len; const ex = e.cfg.expertsOf(L); const sh = try e.fmt("L{d}.moe.shared.0.w1", .{L}); const k2: u64 = try e.w.k2(sh); diff --git a/zig/src/families/deepseek_v41/block_prefill_moe.zig b/zig/src/families/deepseek_v41/block_prefill_moe.zig index 74812ee90c0..76fb69a4064 100644 --- a/zig/src/families/deepseek_v41/block_prefill_moe.zig +++ b/zig/src/families/deepseek_v41/block_prefill_moe.zig @@ -55,7 +55,7 @@ pub fn moeRows(s: *Seg, L: u32, n: i64, x: Arg, comptime bias: []const u8, part: const topk: i64 = ex.topk; const slots: i64 = topk + 1; const Et: i64 = ex.count; - const I: i64 = (try @import("partition.zig").share(e.cfg.expert_width, e.o.rank, e.o.world)).len; + const I: i64 = (try @import("partition.zig").expertShare(e.cfg, e.o.rank, e.o.world)).len; const P = n * topk; const pick = try e.buf("L.pick", .{}, .i32, &.{ n, slots }); const wts = try e.buf("L.wts", .{}, .f32, &.{ n, slots }); diff --git a/zig/src/families/deepseek_v41/cuda_weights.zig b/zig/src/families/deepseek_v41/cuda_weights.zig index e584f6376e5..4a725aa7fc9 100644 --- a/zig/src/families/deepseek_v41/cuda_weights.zig +++ b/zig/src/families/deepseek_v41/cuda_weights.zig @@ -147,8 +147,8 @@ pub fn load(w: *Weights, d: *const cuda.Driver, io: std.Io, cfg: *const @import( @import("core").checkpoint.cache.files(io, pack.dir, pack.files.items); } fn admitGroup(pack: *const @import("pack.zig").Pack, io: std.Io, group: @import("plan.zig").GroupPlan) !void { - try admitWidth(try pack.codebook(io, group.prefix), group.part.group.k2()); - if (std.mem.indexOfScalar(u32, &cuda.qlinear.block.dense.linear_k2s, group.part.group.k2()) == null) + try admitWidth(try pack.codebook(io, group.prefix), group.form.exl3.part.group.k2()); + if (std.mem.indexOfScalar(u32, &cuda.qlinear.block.dense.linear_k2s, group.form.exl3.part.group.k2()) == null) return error.UnsupportedCudaWidth; } @@ -157,7 +157,7 @@ pub fn admitPlan(io: std.Io, cfg: *const @import("config.zig").Config, pack: *co if (plan.leftovers.len != 0 or pack.missing != 0) return error.UnclaimedTensor; for (plan.layers) |layer| { for (layer.groups) |group| try admitGroup(pack, io, group); - for (layer.experts) |projection| for (projection.prefixes, projection.parts) |prefix, part| { + for (layer.experts) |projection| for (projection.form.exl3.prefixes, projection.form.exl3.parts) |prefix, part| { try admitWidth(try pack.codebook(io, prefix), part.group.k2()); try admitExpertWidths(part.group.k2(), part.group.k2()); }; diff --git a/zig/src/families/deepseek_v41/forms.zig b/zig/src/families/deepseek_v41/forms.zig index 2d0155ce5a7..018b0d1af22 100644 --- a/zig/src/families/deepseek_v41/forms.zig +++ b/zig/src/families/deepseek_v41/forms.zig @@ -67,7 +67,7 @@ fn buildArena(a: std.mem.Allocator, s: *const Source, role: []const u8) !?[]cons const lp = for (s.plan.layers) |*l| { if (l.index == L) break l; } else return error.LayerNotPlanned; - const lay = &lp.experts[proj].layout; + const lay = &lp.experts[proj].form.exl3.layout; const E = lay.words.len; const shared = try std.fmt.allocPrint(a, "{s}.moe.shared.0.{s}", .{ layer, w }); if (std.mem.startsWith(u8, what, "k2")) { diff --git a/zig/src/families/deepseek_v41/gate_forward.zig b/zig/src/families/deepseek_v41/gate_forward.zig index aa75e0ef126..91a4c64c296 100644 --- a/zig/src/families/deepseek_v41/gate_forward.zig +++ b/zig/src/families/deepseek_v41/gate_forward.zig @@ -28,7 +28,7 @@ pub fn widths(a: std.mem.Allocator, w: *const load.Weights, p: *const plan.Plan) for (p.layers) |*l| { var r: [2][2]u32 = .{ .{ 99, 0 }, .{ 99, 0 } }; for ([_]usize{ 0, 2, 1 }, [_]usize{ 0, 0, 1 }) |proj, slot| { - for (l.experts[proj].layout.words) |words| { + for (l.experts[proj].form.exl3.layout.words) |words| { r[slot][0] = @min(r[slot][0], words / 8); r[slot][1] = @max(r[slot][1], words / 8); } @@ -39,7 +39,7 @@ pub fn widths(a: std.mem.Allocator, w: *const load.Weights, p: *const plan.Plan) try out.experts.put(a, l.index, r); // x3gm's widths present: the routed experts' gate (w1) and down (w2) K2s, the shared expert not among them var gm: [2]u32 = .{ 0, 0 }; - for (0..2) |proj| for (l.experts[proj].layout.words) |words| { + for (0..2) |proj| for (l.experts[proj].form.exl3.layout.words) |words| { gm[proj] |= @as(u32, 1) << @intCast(words / 8); }; try out.gm.put(a, l.index, gm); diff --git a/zig/src/families/deepseek_v41/host_test.zig b/zig/src/families/deepseek_v41/host_test.zig index 2de7c9a9272..84c4cc384eb 100644 --- a/zig/src/families/deepseek_v41/host_test.zig +++ b/zig/src/families/deepseek_v41/host_test.zig @@ -172,18 +172,18 @@ test "a synthetic pack: both ranks plan every block, the parts tile the full ten try testing.expectEqual(@as(usize, 10), r0.layers.len); const l2 = &r0.layers[2]; try testing.expectEqual(config.Mode.full, l2.mode); - try testing.expect(l2.experts[1].layout.kind == .ragged and l2.experts[0].layout.kind == .ragged and l2.experts[2].layout.kind == .stack); + try testing.expect(l2.experts[1].form.exl3.layout.kind == .ragged and l2.experts[0].form.exl3.layout.kind == .ragged and l2.experts[2].form.exl3.layout.kind == .stack); try testing.expectEqual(@as(u32, 1), l2.gate_up_mismatch); - try testing.expectEqual(@as(u32, 2), l2.experts[1].layout.widths()); - try testing.expect(r0.layers[3].experts[1].layout.kind == .stack); - try testing.expectEqual(@as(usize, 4), r0.layers[8].experts[0].layout.count()); + try testing.expectEqual(@as(u32, 2), l2.experts[1].form.exl3.layout.widths()); + try testing.expect(r0.layers[3].experts[1].form.exl3.layout.kind == .stack); + try testing.expectEqual(@as(usize, 4), r0.layers[8].experts[0].form.exl3.layout.count()); // every split part on the two ranks adds up to the stored tensor for (r0.layers, r1.layers) |x, y| for (x.groups, y.groups) |g0, g1| { if (g0.split == .whole) { - try testing.expectEqual(g0.full.trellisBytes(), g0.part.trellis.bytes()); - } else try testing.expectEqual(g0.full.trellisBytes(), g0.part.trellis.bytes() + g1.part.trellis.bytes()); + try testing.expectEqual(g0.form.exl3.full.trellisBytes(), g0.form.exl3.part.trellis.bytes()); + } else try testing.expectEqual(g0.form.exl3.full.trellisBytes(), g0.form.exl3.part.trellis.bytes() + g1.form.exl3.part.trellis.bytes()); }; - try testing.expectEqual(r0.head.?.full.trellisBytes(), r0.head.?.part.trellis.bytes() + r1.head.?.part.trellis.bytes()); + try testing.expectEqual(r0.head.?.form.exl3.full.trellisBytes(), r0.head.?.form.exl3.part.trellis.bytes() + r1.head.?.form.exl3.part.trellis.bytes()); try testing.expectEqualStrings("mtp.1.main_proj", r0.dspark.?.main_proj.prefix); try testing.expect(r0.dspark.?.confidence != null); try testing.expectEqual(r0.bytes(), r1.bytes()); @@ -343,8 +343,8 @@ test "real pack headers (TF_DSV41_HEADERS: model.safetensors.index.json and = world or width % 128 != 0 or width / 128 < world) return error.SplitNotOnBlocks; - const blocks: usize = width / 128; + return shareOn(width, 128, rank, world); +} + +/// A rank's whole `unit`-column blocks of `width`, the remainder to the low ranks. +pub fn shareOn(width: u32, unit: u32, rank: usize, world: usize) !Share { + if (world == 0 or unit == 0 or rank >= world or width % unit != 0 or width / unit < world) return error.SplitNotOnBlocks; + const blocks: usize = width / unit; const base = blocks / world; const extra = blocks % world; - return .{ .lo = @intCast((rank * base + @min(rank, extra)) * 128), .len = @intCast((base + @intFromBool(rank < extra)) * 128) }; + return .{ .lo = @intCast((rank * base + @min(rank, extra)) * unit), .len = @intCast((base + @intFromBool(rank < extra)) * unit) }; +} + +/// The expert-width block a format's kernels split on: EXL3 tiles are 128 wide, the release's MMA blocks 64. +pub fn expertUnit(format: @import("config.zig").Format) u32 { + return switch (format) { + .exl3 => 128, + .release_fp8 => 64, + }; +} + +/// A rank's share of the routed and shared experts' width for the checkpoint's format. +pub fn expertShare(c: *const @import("config.zig").Config, rank: usize, world: usize) !Share { + return shareOn(c.expert_width, expertUnit(c.format), rank, world); } /// A rank's whole output groups by canon's rule: heads, `wq_b` columns and `wo_b` rows all follow them. diff --git a/zig/src/families/deepseek_v41/plan.zig b/zig/src/families/deepseek_v41/plan.zig index 00d30899c94..735f25c05ef 100644 --- a/zig/src/families/deepseek_v41/plan.zig +++ b/zig/src/families/deepseek_v41/plan.zig @@ -11,17 +11,32 @@ const Info = @import("pack.zig").Info; const partition = @import("partition.zig"); const testing = std.testing; -/// One EXL3 group of the rank: its stored name and the rank's part. -pub const GroupPlan = struct { - proj: names.Proj, - prefix: []const u8, - split: exl3.Split, +/// An EXL3 group as stored and the rank's part of it. +pub const Exl3Form = struct { full: exl3.Group, part: exl3.Part, /// marker tensor (mul1 / mcg) whose value picks the codebook at load; null: the 3-instruction default marker: ?[]const u8, +}; + +/// How a planned projection stores its weights (the release's forms join in a later change). +pub const Form = union(enum) { exl3: Exl3Form }; + +/// One projection of the rank: its stored name, split and form. +pub const GroupPlan = struct { + proj: names.Proj, + prefix: []const u8, + split: exl3.Split, + form: Form, /// wo_a: the global group index this slice is slice: u32 = 0, + + /// The rank's bytes of this projection as stored. + pub fn bytes(g: *const GroupPlan) u64 { + return switch (g.form) { + .exl3 => |f| f.part.bytes(), + }; + } }; pub const NativePlan = struct { @@ -32,14 +47,22 @@ pub const NativePlan = struct { bytes: u64, }; -/// One routed projection: each expert's group part, their layout on the device, and the stored prefixes. -pub const ExpertsPlan = struct { - proj: names.ExpertProj, +/// Routed EXL3 experts: each expert's group part, their layout on the device, and the stored prefixes. +pub const Exl3Experts = struct { parts: []exl3.Part, prefixes: [][]const u8, layout: exl3.Layout, }; +/// How a routed projection's experts are stored. +pub const ExpertsForm = union(enum) { exl3: Exl3Experts }; + +/// One routed projection of the rank. +pub const ExpertsPlan = struct { + proj: names.ExpertProj, + form: ExpertsForm, +}; + pub const LayerPlan = struct { index: u32, prefix: []const u8, @@ -53,9 +76,9 @@ pub const LayerPlan = struct { /// Routed trellis bytes plus the shared expert's: the quant repo's `rank_bytes`. pub fn expertBytes(l: *const LayerPlan) u64 { var n: u64 = 0; - for (l.experts) |e| n += e.layout.trellisBytes(); + for (l.experts) |e| n += e.form.exl3.layout.trellisBytes(); for (l.groups) |g| switch (g.proj) { - .shared_w1, .shared_w2, .shared_w3 => n += g.part.trellis.bytes(), + .shared_w1, .shared_w2, .shared_w3 => n += g.form.exl3.part.trellis.bytes(), else => {}, }; return n; @@ -64,12 +87,14 @@ pub const LayerPlan = struct { /// Everything the rank holds for the block as stored: trellises, suh/svh, natives. pub fn bytes(l: *const LayerPlan) u64 { var n: u64 = 0; - for (l.groups) |g| n += g.part.bytes(); + for (l.groups) |*g| n += g.bytes(); for (l.natives) |v| n += v.bytes; - for (l.experts) |e| { - n += e.layout.trellisBytes(); - for (e.parts) |p| n += p.suh.bytes() + p.svh.bytes(); - } + for (l.experts) |e| switch (e.form) { + .exl3 => |x| { + n += x.layout.trellisBytes(); + for (x.parts) |p| n += p.suh.bytes() + p.svh.bytes(); + }, + }; return n; } }; @@ -98,11 +123,11 @@ pub const Plan = struct { } pub fn bytes(p: *const Plan) u64 { - var n: u64 = if (p.head) |h| h.part.bytes() else 0; + var n: u64 = if (p.head) |*h| h.bytes() else 0; for (p.top) |v| n += v.bytes; for (p.layers) |*l| n += l.bytes(); if (p.dspark) |d| { - n += d.main_proj.part.bytes(); + n += d.main_proj.bytes(); for (d.natives) |v| n += v.bytes; if (d.confidence) |c| n += c.bytes; } @@ -146,7 +171,8 @@ const Builder = struct { inline for (.{ "trellis", "suh", "svh" }) |part| try b.mark(try std.fmt.bufPrint(&b.buf, "{s}." ++ part, .{prefix})); const marker = try b.pack.marker(prefix, &b.buf); if (marker) |m| try b.mark(m); - return .{ .proj = proj, .prefix = try b.dupe(prefix), .split = split, .full = g, .part = try b.partOf(proj, g, split), .marker = if (marker) |m| try b.dupe(m) else null }; + const form: Exl3Form = .{ .full = g, .part = try b.partOf(proj, g, split), .marker = if (marker) |m| try b.dupe(m) else null }; + return .{ .proj = proj, .prefix = try b.dupe(prefix), .split = split, .form = .{ .exl3 = form } }; } /// The rank's part of a group: `wq_b` and `wo_b` follow the output groups, everything else whole blocks. @@ -189,13 +215,13 @@ const Builder = struct { const name = try names.expertName(block, @intCast(e), proj, &nb); const g = try b.group(.wq_a, name, proj.split(), kn[0], kn[1]); // the codebook is one a projection; here the marker's presence, its value at load - const has = g.marker != null; + const has = g.form.exl3.marker != null; if (codebook_marker) |m| if (m != has) return error.CodebooksDiffer; codebook_marker = has; - p.* = g.part; + p.* = g.form.exl3.part; pre.* = g.prefix; } - return .{ .proj = proj, .parts = parts, .prefixes = prefixes, .layout = try exl3.Layout.of(b.a, parts) }; + return .{ .proj = proj, .form = .{ .exl3 = .{ .parts = parts, .prefixes = prefixes, .layout = try exl3.Layout.of(b.a, parts) } } }; } fn layer(b: *Builder, index: u32) !LayerPlan { @@ -225,7 +251,7 @@ const Builder = struct { } var out: LayerPlan = .{ .index = index, .prefix = block, .mode = b.c.mode(index), .groups = groups, .natives = natives, .experts = undefined, .gate_up_mismatch = 0 }; inline for (.{ .w1, .w2, .w3 }, 0..) |p, i| out.experts[i] = try b.experts(block, index, p); - for (out.experts[0].layout.words, out.experts[2].layout.words) |w1, w3| out.gate_up_mismatch += @intFromBool(w1 != w3); + for (out.experts[0].form.exl3.layout.words, out.experts[2].form.exl3.layout.words) |w1, w3| out.gate_up_mismatch += @intFromBool(w1 != w3); return out; } @@ -278,7 +304,7 @@ pub fn build(gpa: std.mem.Allocator, c: *const Config, pack: *const Pack, o: Opt if (o.world != 3 and (c.heads % o.world != 0 or c.o_groups % o.world != 0)) return error.BadWorld; _ = partition.outputGroups(c.o_groups, o.rank, o.world) catch return error.BadWorld; _ = partition.share(c.vocab, o.rank, o.world) catch return error.BadWorld; - _ = partition.share(c.expert_width, o.rank, o.world) catch return error.BadWorld; + _ = partition.expertShare(c, o.rank, o.world) catch return error.BadWorld; var arena = std.heap.ArenaAllocator.init(gpa); errdefer arena.deinit(); var b: Builder = .{ .a = arena.allocator(), .c = c, .pack = pack, .o = o }; @@ -373,8 +399,8 @@ test "CUDA rank planning selects EXL3 over a kept dense sibling from headers alo defer arena.deinit(); var b: Builder = .{ .a = arena.allocator(), .c = &c, .pack = &p, .o = .{ .rank = rank, .world = 2 } }; const g = try b.group(.wq_a, prefix, .whole, 5120, 512); - try testing.expectEqual(@as(u32, 10), g.full.k2()); - try testing.expectEqual(@as(u64, 1638400), g.part.trellis.bytes()); + try testing.expectEqual(@as(u32, 10), g.form.exl3.full.k2()); + try testing.expectEqual(@as(u64, 1638400), g.form.exl3.part.trellis.bytes()); try testing.expect(!b.used.contains("layers.2.attn.compressor.wkv.weight")); try testing.expect(names.ignored("layers.2.attn.compressor.wkv.weight", &p)); } diff --git a/zig/src/families/deepseek_v41/stage.zig b/zig/src/families/deepseek_v41/stage.zig index 9dcae91143e..7981f6e9212 100644 --- a/zig/src/families/deepseek_v41/stage.zig +++ b/zig/src/families/deepseek_v41/stage.zig @@ -24,12 +24,12 @@ pub fn group(gpa: std.mem.Allocator, io: Io, pack: *const Pack, g: plan.GroupPla var out: GroupImage = .{ .trellis = &.{}, .suh = &.{}, .svh = &.{} }; errdefer out.deinit(gpa); var buf: [256]u8 = undefined; - out.trellis = try gpa.alloc(u8, @intCast(g.part.trellis.bytes())); - try pack.read(io, try pack.need(try std.fmt.bufPrint(&buf, "{s}.trellis", .{g.prefix})), g.part.trellis, out.trellis); - out.suh = try gpa.alloc(u8, @intCast(g.part.suh.bytes())); - try pack.read(io, try pack.need(try std.fmt.bufPrint(&buf, "{s}.suh", .{g.prefix})), g.part.suh, out.suh); - out.svh = try gpa.alloc(u8, @intCast(g.part.svh.bytes())); - try pack.read(io, try pack.need(try std.fmt.bufPrint(&buf, "{s}.svh", .{g.prefix})), g.part.svh, out.svh); + out.trellis = try gpa.alloc(u8, @intCast(g.form.exl3.part.trellis.bytes())); + try pack.read(io, try pack.need(try std.fmt.bufPrint(&buf, "{s}.trellis", .{g.prefix})), g.form.exl3.part.trellis, out.trellis); + out.suh = try gpa.alloc(u8, @intCast(g.form.exl3.part.suh.bytes())); + try pack.read(io, try pack.need(try std.fmt.bufPrint(&buf, "{s}.suh", .{g.prefix})), g.form.exl3.part.suh, out.suh); + out.svh = try gpa.alloc(u8, @intCast(g.form.exl3.part.svh.bytes())); + try pack.read(io, try pack.need(try std.fmt.bufPrint(&buf, "{s}.svh", .{g.prefix})), g.form.exl3.part.svh, out.svh); return out; } @@ -54,19 +54,19 @@ pub const ExpertsImage = struct { }; pub fn experts(gpa: std.mem.Allocator, io: Io, pack: *const Pack, x: *const plan.ExpertsPlan) !ExpertsImage { - const n = x.layout.count(); - const kpart = 2 * @as(u64, x.layout.k_tiles) * 16; - const npart = 2 * @as(u64, x.layout.n_tiles) * 16; + const n = x.form.exl3.layout.count(); + const kpart = 2 * @as(u64, x.form.exl3.layout.k_tiles) * 16; + const npart = 2 * @as(u64, x.form.exl3.layout.n_tiles) * 16; var out: ExpertsImage = .{ .trellis = &.{}, .suh = &.{}, .svh = &.{}, .k2 = &.{}, .offsets = &.{} }; errdefer out.deinit(gpa); - out.trellis = try gpa.alignedAlloc(u8, .@"16", @intCast(x.layout.trellisBytes())); + out.trellis = try gpa.alignedAlloc(u8, .@"16", @intCast(x.form.exl3.layout.trellisBytes())); out.suh = try gpa.alloc(u8, @intCast(n * kpart)); out.svh = try gpa.alloc(u8, @intCast(n * npart)); out.k2 = try gpa.alloc(i32, n); out.offsets = try gpa.alloc(u64, n); var buf: [256]u8 = undefined; - for (x.parts, x.prefixes, 0..) |p, pre, e| { - const at = x.layout.byteOffset(e); + for (x.form.exl3.parts, x.form.exl3.prefixes, 0..) |p, pre, e| { + const at = x.form.exl3.layout.byteOffset(e); out.offsets[e] = at; out.k2[e] = @intCast(p.group.k2()); const t = out.trellis[@intCast(at)..][0..@intCast(p.trellis.bytes())]; @@ -125,7 +125,7 @@ test "staged images: a column part's runs, ragged experts back to back, stacked prefixes[e] = try std.fmt.bufPrint(&nb[e], "layers.0.ffn.experts.{d}.w1", .{e}); parts[e] = try (try pack.group(prefixes[e], &buf)).part(.col, 1, 2); } - const x: plan.ExpertsPlan = .{ .proj = .w1, .parts = &parts, .prefixes = &prefixes, .layout = try exl3.Layout.of(arena.allocator(), &parts) }; + const x: plan.ExpertsPlan = .{ .proj = .w1, .form = .{ .exl3 = .{ .parts = &parts, .prefixes = &prefixes, .layout = try exl3.Layout.of(arena.allocator(), &parts) } } }; var img = try experts(a, io, &pack, &x); defer img.deinit(a); try testing.expectEqualSlices(i32, &.{ 4, 6, 4 }, img.k2); @@ -141,7 +141,7 @@ test "staged images: a column part's runs, ragged experts back to back, stacked try testing.expectEqual(@as(u8, 128), img.svh[256 + 0]); try testing.expectEqual(@as(usize, 3 * 256), img.svh.len); var buf: [128]u8 = undefined; - const gp: plan.GroupPlan = .{ .proj = .wq_a, .prefix = prefixes[1], .split = .row, .full = try pack.group(prefixes[1], &buf), .part = try (try pack.group(prefixes[1], &buf)).part(.whole, 0, 2), .marker = null }; + const gp: plan.GroupPlan = .{ .proj = .wq_a, .prefix = prefixes[1], .split = .row, .form = .{ .exl3 = .{ .full = try pack.group(prefixes[1], &buf), .part = try (try pack.group(prefixes[1], &buf)).part(.whole, 0, 2), .marker = null } } }; var gi = try group(a, io, &pack, gp); defer gi.deinit(a); try testing.expectEqual(@as(u8, 64 + 16 + 15), gi.trellis[(16 + 15) * 96]); diff --git a/zig/src/families/deepseek_v41/world3_test.zig b/zig/src/families/deepseek_v41/world3_test.zig index b2b49eb5517..68155d0ae25 100644 --- a/zig/src/families/deepseek_v41/world3_test.zig +++ b/zig/src/families/deepseek_v41/world3_test.zig @@ -194,8 +194,8 @@ test "world three: the default configuration plans every rank, each with its own try testing.expectEqual(own.lo + seen, g.slice); seen += 1; }, - .wq_b => try testing.expectEqual(own.len * per_group * c.head_dim, g.part.group.n()), - .wo_b => try testing.expectEqual(own.len * c.o_lora, g.part.group.k()), + .wq_b => try testing.expectEqual(own.len * per_group * c.head_dim, g.form.exl3.part.group.n()), + .wo_b => try testing.expectEqual(own.len * c.o_lora, g.form.exl3.part.group.k()), else => {}, }; try testing.expectEqual(own.len, seen); @@ -265,11 +265,11 @@ fn partsTileTheCheckpoint(gpa: std.mem.Allocator, f: *const Fixture) !void { var p = try plan.build(gpa, &f.c, &f.pack, .{ .rank = @intCast(r), .world = 3 }); defer p.deinit(); for (p.layers) |*l| { - for (l.groups) |g| try markPart(a, &seen, &f.pack, g.prefix, g.part); - for (&l.experts) |*x| for (x.parts, x.prefixes) |part, prefix| try markPart(a, &seen, &f.pack, prefix, part); + for (l.groups) |g| try markPart(a, &seen, &f.pack, g.prefix, g.form.exl3.part); + for (&l.experts) |*x| for (x.form.exl3.parts, x.form.exl3.prefixes) |part, prefix| try markPart(a, &seen, &f.pack, prefix, part); } - if (p.head) |h| try markPart(a, &seen, &f.pack, h.prefix, h.part); - if (p.dspark) |d| try markPart(a, &seen, &f.pack, d.main_proj.prefix, d.main_proj.part); + if (p.head) |h| try markPart(a, &seen, &f.pack, h.prefix, h.form.exl3.part); + if (p.dspark) |d| try markPart(a, &seen, &f.pack, d.main_proj.prefix, d.main_proj.form.exl3.part); } var it = seen.iterator(); while (it.next()) |e| { @@ -399,10 +399,10 @@ fn raggedMatchesStored(a: std.mem.Allocator, io: Io, f: *const Fixture) !void { defer b.deinit(); try b.all(&p, f.c.o_groups); for (p.layers) |*l| for (&l.experts, [_][]const u8{ "w1", "w2", "w3" }) |*x, w| { - if (x.layout.kind == .stack) continue; + if (x.form.exl3.layout.kind == .stack) continue; const got = b.find(try std.fmt.bufPrint(&nb, "L{d}.moe.{s}.data", .{ l.index, w })) orelse return error.TestMissingEntry; var at: usize = 0; - for (x.parts, x.prefixes) |part, prefix| { + for (x.form.exl3.parts, x.form.exl3.prefixes) |part, prefix| { const bytes = try a.alloc(u8, @intCast(part.trellis.bytes())); defer a.free(bytes); try f.pack.read(io, try f.pack.need(try std.fmt.bufPrint(&nb, "{s}.trellis", .{prefix})), part.trellis, bytes); diff --git a/zig/src/families/deepseek_v41/world4_plan_test.zig b/zig/src/families/deepseek_v41/world4_plan_test.zig index b30aa0ba529..9fc4a7affc9 100644 --- a/zig/src/families/deepseek_v41/world4_plan_test.zig +++ b/zig/src/families/deepseek_v41/world4_plan_test.zig @@ -13,10 +13,36 @@ fn defaultPack(a: std.mem.Allocator) !Pack { return p; } +/// A group in the field layout main pinned (before the form union), so the digest compares values, not types. +fn legacyGroup(g: plan.GroupPlan) Legacy.Group { + const f = g.form.exl3; + return .{ .proj = g.proj, .prefix = g.prefix, .split = g.split, .full = f.full, .part = f.part, .marker = f.marker, .slice = g.slice }; +} + +const Legacy = struct { + const Group = struct { proj: @import("names.zig").Proj, prefix: []const u8, split: @import("core").exl3_layout.Split, full: @import("core").exl3_layout.Group, part: @import("core").exl3_layout.Part, marker: ?[]const u8, slice: u32 }; + const Experts = struct { proj: @import("names.zig").ExpertProj, parts: []@import("core").exl3_layout.Part, prefixes: [][]const u8, layout: @import("core").exl3_layout.Layout }; + const Layer = struct { index: u32, prefix: []const u8, mode: @import("config.zig").Mode, groups: []Group, natives: []plan.NativePlan, experts: [3]Experts, gate_up_mismatch: u32 }; + const Dspark = struct { main_proj: Group, natives: []plan.NativePlan, confidence: ?plan.NativePlan }; +}; + fn fingerprint(p: *const plan.Plan, hash: *std.crypto.hash.sha2.Sha256) !void { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const a = arena.allocator(); + const layers = try a.alloc(Legacy.Layer, p.layers.len); + for (p.layers, layers) |l, *o| { + const groups = try a.alloc(Legacy.Group, l.groups.len); + for (l.groups, groups) |g, *x| x.* = legacyGroup(g); + var ex: [3]Legacy.Experts = undefined; + for (l.experts, &ex) |e, *x| x.* = .{ .proj = e.proj, .parts = e.form.exl3.parts, .prefixes = e.form.exl3.prefixes, .layout = e.form.exl3.layout }; + o.* = .{ .index = l.index, .prefix = l.prefix, .mode = l.mode, .groups = groups, .natives = l.natives, .experts = ex, .gate_up_mismatch = l.gate_up_mismatch }; + } + const head: ?Legacy.Group = if (p.head) |h| legacyGroup(h) else null; + const dspark: ?Legacy.Dspark = if (p.dspark) |d| .{ .main_proj = legacyGroup(d.main_proj), .natives = d.natives, .confidence = d.confidence } else null; var out: std.Io.Writer.Allocating = .init(std.testing.allocator); defer out.deinit(); - try std.json.Stringify.value(.{ .rank = p.rank, .world = p.world, .layers = p.layers, .top = p.top, .head = p.head, .dspark = p.dspark, .leftovers = p.leftovers }, .{}, &out.writer); + try std.json.Stringify.value(.{ .rank = p.rank, .world = p.world, .layers = layers, .top = p.top, .head = head, .dspark = dspark, .leftovers = p.leftovers }, .{}, &out.writer); hash.update(out.written()); } diff --git a/zig/src/families/deepseek_v41/world4_test.zig b/zig/src/families/deepseek_v41/world4_test.zig index a1c3632570e..1f0e8b832db 100644 --- a/zig/src/families/deepseek_v41/world4_test.zig +++ b/zig/src/families/deepseek_v41/world4_test.zig @@ -100,3 +100,20 @@ test "world four: decode and padded prefill widths are local" { try admission.nativePrefill(&cfg, 4); try std.testing.expectError(error.UnsupportedPrefillHeads, admission.nativePrefill(&cfg, 8)); } + +test "world four: the release format splits the experts on 64-column blocks, 576 a rank" { + var cfg: Config = .{}; + for (0..4) |rank| try std.testing.expectEqual(@as(u32, 640 - 128 * @as(u32, @intFromBool(rank >= 2))), (try partition.expertShare(&cfg, rank, 4)).len); + cfg.format = .release_fp8; + var cursor: u32 = 0; + for (0..4) |rank| { + const p = try partition.expertShare(&cfg, rank, 4); + try std.testing.expectEqual(cursor, p.lo); + try std.testing.expectEqual(@as(u32, 576), p.len); + cursor += p.len; + } + try std.testing.expectEqual(cfg.expert_width, cursor); + try std.testing.expectEqual(@as(u32, 1152), (try partition.expertShare(&cfg, 1, 2)).len); + try std.testing.expectError(error.SplitNotOnBlocks, partition.shareOn(2304, 0, 0, 4)); + try std.testing.expectEqualDeep(try partition.share(2304, 3, 4), try partition.shareOn(2304, 128, 3, 4)); +}