Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
b731206
feat(hip): the model-free kernels on #463's runtime: attention, Delta…
BlivionIaG Oct 10, 2026
67d9479
feat(core): weight formats behind one interface, MLX affine 2- to 8-b…
BlivionIaG Oct 10, 2026
48abd99
feat(core): a kernel registry that picks a product's tile by tuned co…
BlivionIaG Oct 10, 2026
6254228
feat(hip): the MLX affine products: dot2 tiles, the gfx11 matrix-core…
BlivionIaG Oct 10, 2026
9b6b260
docs(hip): the device bitcode of Ubuntu's packaged HIP, and no rocWMM…
BlivionIaG Oct 11, 2026
1152c67
Merge branch 'hip-kernels-ops' into hip-kernels-affine
BlivionIaG Oct 11, 2026
df83c6f
Merge branch 'hip-kernels-ops' into hip-kernels-affine
BlivionIaG Oct 11, 2026
5d05894
chore(hip): gdn_prefill.hip drops the vLLM SPDX lines it kept from th…
BlivionIaG Oct 11, 2026
a579284
Merge branch 'hip-kernels-affine' into fam-new
BlivionIaG Oct 11, 2026
e6c11fb
feat(qwen3_5): the GPU backends' checkpoint loader, on the host
BlivionIaG Oct 10, 2026
eb9bd0f
fix(qwen3_5): un-center an MTP head shipped zero-centered
BlivionIaG Oct 11, 2026
4c231bf
feat(qwen3_5): the HIP config refuses what the kernels do not compute…
BlivionIaG Oct 11, 2026
02c1dd6
Merge remote-tracking branch 'upstream/main' into fam-r2
BlivionIaG Oct 12, 2026
57fadd1
feat(hip): the runtime pieces an engine uses beyond #463's model-free…
BlivionIaG Oct 10, 2026
211ea81
feat(hip): the kernels a tensor-parallel rank and the MTP head add
BlivionIaG Oct 10, 2026
42664f9
feat(qwen3_5): the Qwen3.5 / Qwen3.6 engine on HIP, its native server…
BlivionIaG Oct 10, 2026
3d9bd2e
feat(hip): one rank source: --speed-up is refused, the HIP engine's r…
BlivionIaG Oct 10, 2026
a25c8e2
feat(qwen3_5): the HIP prompt cache serves serial requests too
BlivionIaG Oct 10, 2026
3092774
test(qwen3_5): chat with the prompt cache equals chat without it thro…
BlivionIaG Oct 10, 2026
9a4e10f
feat(qwen3_5): a prompt runs 512-row chunks while other streams decode
BlivionIaG Oct 11, 2026
3f396fc
feat(hip): RDNA4 runs on the dot2 tiles; matrix=on is refused where n…
BlivionIaG Oct 11, 2026
fc795cb
test(qwen3_5): serve_rewind.py's docstring on one line, as the lean c…
BlivionIaG Oct 11, 2026
10f67d9
refactor(qwen3_5): the engine reaches the other ranks through hip/col…
BlivionIaG Oct 11, 2026
7f5fd5b
build(hip): the HIP server's CLI module takes EXLR8's import, as main…
BlivionIaG Oct 12, 2026
13961db
feat(hip): tensor parallelism over RCCL, behind hip/collective.zig
BlivionIaG Oct 11, 2026
ba0bc3c
feat(hip): the graph cache by shape in the hip module, for every HIP …
BlivionIaG Oct 12, 2026
905d9e9
feat(hip): a step recorded as graphs cut at its collectives, the coll…
BlivionIaG Oct 12, 2026
5480055
feat(hip): the graph cache over any replayable step, a caller's own k…
BlivionIaG Oct 12, 2026
ebe69eb
feat(qwen3_5): HIP rounds record split graphs: the collectives run be…
BlivionIaG Oct 12, 2026
10926cf
feat(qwen3_5): HIP rounds replay split graphs at any world size, RCCL…
BlivionIaG Oct 12, 2026
66d8a19
fix(qwen3_5): every rank sizes its streams for rank 0's window, so a …
BlivionIaG Oct 12, 2026
d47b0f8
perf(qwen3_5): the HIP loader drops each part's mapped pages once it …
BlivionIaG Oct 13, 2026
1ae1078
style(hip): the ops headers end without a blank line, as git diff --c…
BlivionIaG Oct 14, 2026
47612d0
Merge branch 'hip-kernels-ops' into qwen35-family (ops headers end wi…
BlivionIaG Oct 14, 2026
3850523
Merge branch 'qwen35-family' into qwen35-rccl (ops headers end withou…
BlivionIaG Oct 14, 2026
5537d6a
fix(hip): the paged prefill tile calls expf, as the flat tile does
BlivionIaG Oct 14, 2026
9178585
Merge branch 'hip-kernels-ops' into qwen35-family (the paged prefill …
BlivionIaG Oct 14, 2026
f5281d5
Merge branch 'qwen35-family' into qwen35-rccl (the paged prefill tile…
BlivionIaG Oct 14, 2026
d91b074
fix(hip): one fp32 DeltaNet recurrence at every length; the chunked p…
BlivionIaG Oct 14, 2026
1da7042
Merge branch 'hip-kernels-ops' into qwen35-family (one fp32 DeltaNet …
BlivionIaG Oct 14, 2026
205b800
Merge branch 'qwen35-family' into qwen35-rccl (one fp32 DeltaNet recu…
BlivionIaG Oct 14, 2026
1bdd859
refactor(hip): the policy's gdn switch goes with the chunked recurren…
BlivionIaG Oct 14, 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
feat(qwen3_5): the GPU backends' checkpoint loader, on the host
Qwen3.5 / Qwen3.6 text checkpoints for the HIP engine, host side only: the geometry (dense and MoE, MTP layer
count, rotary, layer types) from config.json, the MLX affine quantization table per module, one tensor index
over the safetensors shards, the projections, the routed experts stacked, and the tensor-parallel cuts on
core/quant. `zig build test` runs its tests; TF_QWEN_DIR indexes a real checkpoint.

Draft: the geometry is a third Qwen text parser beside qwen27/config.zig and the Metal 2B's admission. #491
proposes one shared config and weight path instead; this is refit to that once agreed.
  • Loading branch information
BlivionIaG committed Oct 11, 2026
commit e6c11fb95c8391ee69bb222c8a510168367d626f
9 changes: 9 additions & 0 deletions zig/build/hip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ pub fn steps(b: *std.Build, target: std.Build.ResolvedTarget, test_step: *std.Bu
const run_hip_tests = b.addRunArtifact(hip_tests);
test_step.dependOn(&run_hip_tests.step);
b.step("hip-host-test", "HIP admission tests without GPU work").dependOn(&run_hip_tests.step);
const loader = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("zig/src/families/qwen3_5/loader.zig"),
.target = b.graph.host,
.link_libc = true,
.imports = &.{.{ .name = "core", .module = coreModule(b, b.graph.host) }},
}) }));
loader.has_side_effects = true; // TF_QWEN_DIR is not a cached input
test_step.dependOn(&loader.step);
b.step("test-qwen35", "Qwen3.5 / 3.6 checkpoint loader tests; TF_QWEN_DIR indexes a real checkpoint").dependOn(&loader.step);
const hipcc = b.option([]const u8, "hipcc", "HIP compiler for model-free tests") orelse "hipcc";
const hipcc_resolved = b.findProgram(.{ .names = &.{hipcc} }) orelse hipcc;
const hip_include = b.option([]const u8, "hip-include", "HIP header directory") orelse
Expand Down
259 changes: 259 additions & 0 deletions zig/src/families/qwen3_5/config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,262 @@ test "admit untied heads and refuse a root and language tiedness disagreement" {
defer gpa.free(mismatch);
try std.testing.expectError(error.BadQwenConfig, parse(gpa, mismatch));
}

// The geometry any backend reads, with its defaults and checks; Metal's admission above is stricter (one geometry).

pub const Error = error{
InvalidConfig,
MissingField,
UnsupportedModel,
InvalidMoe,
InvalidRotary,
InvalidLayerTypes,
} || std.mem.Allocator.Error || std.json.ParseError(std.json.Scanner);

/// The Python `Spec`, plus the shared expert's width and the MTP layer count the config declares.
pub const Spec = struct {
hidden: usize,
intermediate: usize,
n_layers: usize,
heads: usize,
kv_heads: usize,
head_dim: usize,
key_heads: usize,
value_heads: usize,
key_dim: usize,
value_dim: usize,
conv: usize,
vocab: usize,
eps: f64,
rope_theta: f64,
rotary_dim: usize,
full_every: usize,
bits: u8,
group: u16,
experts: usize,
top_k: usize,
moe_width: usize,
shared_width: usize,
mtp_layers: usize,

/// Whether layer `index` is full attention (the others are gated delta-net).
pub fn full(s: Spec, index: usize) bool {
return (index + 1) % s.full_every == 0;
}

pub fn keyWidth(s: Spec) usize {
return s.key_heads * s.key_dim;
}

pub fn valueWidth(s: Spec) usize {
return s.value_heads * s.value_dim;
}
};

/// The config's root object, when its model_type is a Qwen3.5 text model, dense or sparse.
pub fn declared(a: std.mem.Allocator, bytes: []const u8) Error!std.json.ObjectMap {
const doc = try objectOf(try std.json.parseFromSliceLeaky(std.json.Value, a, bytes, .{}));
const kind = doc.get("model_type") orelse return error.UnsupportedModel;
if (kind != .string or !(eql(kind.string, "qwen3_5") or eql(kind.string, "qwen3_5_moe"))) return error.UnsupportedModel;
return doc;
}

/// The text tower's object: `text_config` when the config has one, else the root itself.
pub fn textTower(doc: std.json.ObjectMap) Error!std.json.ObjectMap {
return if (truthy(doc.get("text_config"))) try objectOf(doc.get("text_config").?) else doc;
}

/// `cfg.get(key) or text.get(key) or {}`, an object.
pub fn pickObject(doc: std.json.ObjectMap, tower: std.json.ObjectMap, key: []const u8) Error!std.json.ObjectMap {
for ([_]std.json.ObjectMap{ doc, tower }) |source| if (truthy(source.get(key))) return objectOf(source.get(key).?);
return std.json.ObjectMap.empty;
}

/// `tie_word_embeddings`: the output head is the embedding (the default).
pub fn isTied(doc: std.json.ObjectMap, tower: std.json.ObjectMap) bool {
return if (doc.get("tie_word_embeddings")) |v| truthyValue(v) else if (tower.get("tie_word_embeddings")) |v| truthyValue(v) else true;
}

/// The Spec of a text tower whose weights are `bits` wide in groups of `group_size`.
pub fn geometry(tower: std.json.ObjectMap, bits: u8, group_size: u16) Error!Spec {
const experts = try optionalInt(tower, "num_experts", 0);
const top_k = try optionalInt(tower, "num_experts_per_tok", 0);
const moe_width = try optionalInt(tower, "moe_intermediate_size", 0);
if (experts != 0) {
if (top_k == 0 or top_k > experts or moe_width == 0) return error.InvalidMoe;
if (tower.get("norm_topk_prob")) |v| if (!truthyValue(v)) return error.InvalidMoe;
}
const model_width = try requiredInt(tower, "hidden_size");
const attn_heads = try requiredInt(tower, "num_attention_heads");
const per_head = if (truthy(tower.get("head_dim"))) try toInt(tower.get("head_dim").?) else model_width / attn_heads;
const rope = if (truthy(tower.get("rope_parameters"))) try objectOf(tower.get("rope_parameters").?) else std.json.ObjectMap.empty;
const partial = try numberOf(rope.get("partial_rotary_factor") orelse tower.get("partial_rotary_factor") orelse .{ .float = 0.25 });
const product = @as(f64, @floatFromInt(per_head)) * partial;
if (!(product >= 0 and product <= @as(f64, @floatFromInt(per_head)))) return error.InvalidRotary;
const rotary: usize = @intFromFloat(product);
const base = try numberOf(rope.get("rope_theta") orelse if (truthy(tower.get("rope_theta"))) tower.get("rope_theta").? else .{ .integer = 10_000_000 });
const spec: Spec = .{
.hidden = model_width,
.intermediate = try optionalInt(tower, "intermediate_size", 0),
.n_layers = try requiredInt(tower, "num_hidden_layers"),
.heads = attn_heads,
.kv_heads = try requiredInt(tower, "num_key_value_heads"),
.head_dim = per_head,
.key_heads = try requiredInt(tower, "linear_num_key_heads"),
.value_heads = try requiredInt(tower, "linear_num_value_heads"),
.key_dim = try requiredInt(tower, "linear_key_head_dim"),
.value_dim = try requiredInt(tower, "linear_value_head_dim"),
.conv = try requiredInt(tower, "linear_conv_kernel_dim"),
.vocab = try requiredInt(tower, "vocab_size"),
.eps = try numberOf(tower.get("rms_norm_eps") orelse .{ .float = 1e-6 }),
.rope_theta = base,
.rotary_dim = rotary,
.full_every = try optionalInt(tower, "full_attention_interval", 4),
.bits = bits,
.group = group_size,
.experts = experts,
.top_k = top_k,
.moe_width = moe_width,
.shared_width = try optionalInt(tower, "shared_expert_intermediate_size", 0),
.mtp_layers = try optionalInt(tower, "mtp_num_hidden_layers", 0),
};
if (rotary % 2 != 0 or rotary == 0 or rotary > per_head) return error.InvalidRotary;
if (spec.full_every == 0 or attn_heads == 0) return error.InvalidConfig;
if (tower.get("layer_types")) |kinds| if (kinds != .null) {
if (kinds != .array) return error.InvalidLayerTypes;
for (kinds.array.items, 0..) |item, i| {
const want = if (spec.full(i)) "full_attention" else "linear_attention";
if (item != .string or !eql(item.string, want)) return error.InvalidLayerTypes;
}
};
return spec;
}

fn eql(a: []const u8, b: []const u8) bool {
return std.mem.eql(u8, a, b);
}

fn objectOf(v: std.json.Value) Error!std.json.ObjectMap {
return if (v == .object) v.object else error.InvalidConfig;
}

/// Python truthiness of an optional JSON value (`x.get(k) or default`).
fn truthy(v: ?std.json.Value) bool {
return if (v) |value| truthyValue(value) else false;
}

fn truthyValue(v: std.json.Value) bool {
return switch (v) {
.null => false,
.bool => |b| b,
.integer => |i| i != 0,
.float => |f| f != 0,
.string => |s| s.len > 0,
.array => |x| x.items.len > 0,
.object => |o| o.count() > 0,
else => true,
};
}

/// Python `int(x)` of a JSON number.
fn toInt(v: std.json.Value) Error!usize {
return switch (v) {
.integer => |i| std.math.cast(usize, i) orelse error.InvalidConfig,
.float => |f| if (f >= 0 and f < 1e15) @intFromFloat(f) else error.InvalidConfig,
else => error.InvalidConfig,
};
}

fn numberOf(v: std.json.Value) Error!f64 {
return switch (v) {
.integer => |i| @floatFromInt(i),
.float => |f| f,
else => error.InvalidConfig,
};
}

fn requiredInt(o: std.json.ObjectMap, name: []const u8) Error!usize {
return toInt(o.get(name) orelse return error.MissingField);
}

/// `int(o.get(name, default) or 0)`: absent is `default`, null or zero is 0.
fn optionalInt(o: std.json.ObjectMap, name: []const u8, default: usize) Error!usize {
const v = o.get(name) orelse return default;
return if (truthyValue(v)) toInt(v) else 0;
}

const sample_text =
\\{"model_type": "qwen3_5", "tie_word_embeddings": false,
\\ "text_config": {"hidden_size": 4096, "intermediate_size": 12288, "num_hidden_layers": 8,
\\ "num_attention_heads": 16, "num_key_value_heads": 4, "head_dim": 256,
\\ "linear_num_key_heads": 16, "linear_num_value_heads": 32, "linear_key_head_dim": 128,
\\ "linear_value_head_dim": 128, "linear_conv_kernel_dim": 4, "vocab_size": 248320,
\\ "rms_norm_eps": 1e-06, "full_attention_interval": 4, "mtp_num_hidden_layers": 1,
\\ "layer_types": ["linear_attention", "linear_attention", "linear_attention", "full_attention",
\\ "linear_attention", "linear_attention", "linear_attention", "full_attention"],
\\ "rope_parameters": {"rope_theta": 10000000, "partial_rotary_factor": 0.25}}}
;

test "text_config parses into the Python Spec" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const doc = try declared(arena.allocator(), sample_text);
const tower = try textTower(doc);
const s = try geometry(tower, 4, 64);
try std.testing.expectEqual(@as(usize, 4096), s.hidden);
try std.testing.expectEqual(@as(usize, 12288), s.intermediate);
try std.testing.expectEqual(@as(usize, 64), s.rotary_dim);
try std.testing.expectEqual(@as(usize, 256), s.head_dim);
try std.testing.expectEqual(@as(usize, 2048), s.keyWidth());
try std.testing.expectEqual(@as(usize, 4096), s.valueWidth());
try std.testing.expectEqual(@as(f64, 10_000_000), s.rope_theta);
try std.testing.expectEqual(@as(f64, 1e-6), s.eps);
try std.testing.expectEqual(@as(u8, 4), s.bits);
try std.testing.expectEqual(@as(u16, 64), s.group);
try std.testing.expectEqual(@as(usize, 0), s.experts);
try std.testing.expectEqual(@as(usize, 1), s.mtp_layers);
try std.testing.expect(!isTied(doc, tower));
try std.testing.expect(s.full(3) and s.full(7) and !s.full(0) and !s.full(4));
}

test "a top-level config, the partial rotary default and the MoE fields" {
const text =
\\{"model_type": "qwen3_5_moe",
\\ "hidden_size": 2048, "num_hidden_layers": 4, "num_attention_heads": 16, "num_key_value_heads": 2,
\\ "linear_num_key_heads": 16, "linear_num_value_heads": 32, "linear_key_head_dim": 128,
\\ "linear_value_head_dim": 128, "linear_conv_kernel_dim": 4, "vocab_size": 1000, "rope_theta": 5000000,
\\ "partial_rotary_factor": 0.5, "num_experts": 256, "num_experts_per_tok": 8,
\\ "moe_intermediate_size": 512, "shared_expert_intermediate_size": 512}
;
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const doc = try declared(arena.allocator(), text);
const tower = try textTower(doc);
const s = try geometry(tower, 8, 64);
try std.testing.expect(isTied(doc, tower));
try std.testing.expectEqual(@as(usize, 128), s.head_dim);
try std.testing.expectEqual(@as(usize, 64), s.rotary_dim);
try std.testing.expectEqual(@as(f64, 5_000_000), s.rope_theta);
try std.testing.expectEqual(@as(usize, 256), s.experts);
try std.testing.expectEqual(@as(usize, 8), s.top_k);
try std.testing.expectEqual(@as(usize, 512), s.moe_width);
try std.testing.expectEqual(@as(usize, 512), s.shared_width);
try std.testing.expectEqual(@as(usize, 0), s.intermediate);
try std.testing.expectEqual(@as(usize, 4), s.full_every);
}

test "the Python loader's refusals" {
const a = std.testing.allocator;
var arena: std.heap.ArenaAllocator = .init(a);
defer arena.deinit();
const wrong = try std.mem.replaceOwned(u8, a, sample_text, "\"qwen3_5\"", "\"llama\"");
defer a.free(wrong);
try std.testing.expectError(error.UnsupportedModel, declared(arena.allocator(), wrong));
const types = try std.mem.replaceOwned(u8, a, sample_text, "\"full_attention\"],", "\"linear_attention\"],");
defer a.free(types);
const tower = try textTower(try declared(arena.allocator(), types));
try std.testing.expectError(error.InvalidLayerTypes, geometry(tower, 4, 64));
const moe = try std.mem.replaceOwned(u8, a, sample_text, "\"vocab_size\"", "\"num_experts\": 8, \"num_experts_per_tok\": 9, \"vocab_size\"");
defer a.free(moe);
try std.testing.expectError(error.InvalidMoe, geometry(try textTower(try declared(arena.allocator(), moe)), 4, 64));
}
21 changes: 21 additions & 0 deletions zig/src/families/qwen3_5/loader.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Qwen3.5 / Qwen3.6 text checkpoints on the host for the GPU backends: configuration, tensor index, projections, cuts.

pub const config = @import("weights/config.zig");
pub const table = @import("weights/table.zig");
pub const host = @import("weights/host.zig");
pub const checkpoint = @import("weights/checkpoint.zig");
pub const slicing = @import("weights/slicing.zig");
pub const Config = config.Config;
pub const Spec = config.Spec;
pub const Checkpoint = checkpoint.Checkpoint;

test {
_ = config;
_ = table;
_ = @import("weights/shard.zig");
_ = @import("weights/projection.zig");
_ = @import("weights/checkpoint_test.zig");
_ = @import("weights/real_test.zig");
@import("std").testing.refAllDecls(slicing);
_ = @import("config.zig");
}
Loading