Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
103 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
badbb6f
feat(qwen3_5): structured output on HIP: the lane core's grammar mask…
BlivionIaG Oct 11, 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
a9bab75
Merge 1.0.9 (c36b536d) into hip-kernels-affine
BlivionIaG Oct 13, 2026
fa4d769
Merge branch 'qwen35-structured' into fn-base3
BlivionIaG Oct 13, 2026
f76877c
Merge branch 'hip-kernels-affine' (1.0.9) into fn-base3
BlivionIaG Oct 13, 2026
996c098
fix(core): MLX affine dequant reads any code width from 2 to 8 bits, …
BlivionIaG Oct 12, 2026
7aa8722
refactor(hip): the memory budget in admission.zig, where every HIP fa…
BlivionIaG Oct 11, 2026
9c1a841
refactor(flashnext): the affine spec in a file of its own, so a backe…
BlivionIaG Oct 11, 2026
1356ecb
refactor(hip): the routed MoE and the tensor-parallel reduce in the h…
BlivionIaG Oct 11, 2026
a3f02b3
feat(flashnext): native HIP routing for qwen4_exp behind admission: f…
BlivionIaG Oct 11, 2026
6be6733
refactor(hip): the token draw from logits rows in hip_serve, a module…
BlivionIaG Oct 11, 2026
fd3baad
perf(hip): the MoE route's expert starts and tile ends by a block sca…
BlivionIaG Oct 12, 2026
6c8c96d
feat(hip): Flash Next's kernels: residual streams and their tp sums, …
BlivionIaG Oct 12, 2026
2e2a03f
feat(flashnext): a window's n-gram rows on the host, where the HIP en…
BlivionIaG Oct 11, 2026
a56342b
feat(flashnext): the HIP forward on one device: weights, caches, host…
BlivionIaG Oct 12, 2026
3fa8861
refactor(core): a MoE layer's stacked experts and router rows in core…
BlivionIaG Oct 11, 2026
07ff3aa
feat(hip): a MoE plan sized by the rows' own pairs, for a family whos…
BlivionIaG Oct 12, 2026
c28aad2
feat(hip): the merged MoE pick over 512 threads, one expert a thread …
BlivionIaG Oct 12, 2026
0e4b374
feat(hip): the gated norm's gate is SiLU or sigmoid
BlivionIaG Oct 11, 2026
8d2c611
feat(flashnext): the HIP admission's refusal names the card's grant a…
BlivionIaG Oct 11, 2026
ca575f7
feat(flashnext): serve on HIP: the lane backend, MTP drafts, a round'…
BlivionIaG Oct 12, 2026
f8a5f74
feat(flashnext): tensor parallelism on HIP: weights cut by rank, shar…
BlivionIaG Oct 12, 2026
e3ee03b
perf(flashnext): HIP rounds replay split graphs by shape, host inputs…
BlivionIaG Oct 12, 2026
1ae1078
style(hip): the ops headers end without a blank line, as git diff --c…
BlivionIaG Oct 14, 2026
fbc626d
Merge branch 'hip-kernels-ops' into hip-kernels-affine (ops headers e…
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
deec0a9
Merge branch 'qwen35-family' into qwen35-structured (ops headers end …
BlivionIaG Oct 14, 2026
3f4cc24
Merge the ops headers' whitespace fix (#617, through #621, #697 and #…
BlivionIaG Oct 14, 2026
21dbcbe
Merge the ops headers' whitespace fix (through #811)
BlivionIaG Oct 14, 2026
e10cd5e
Merge the ops headers' whitespace fix (through #812)
BlivionIaG Oct 14, 2026
38ed32e
Merge the ops headers' whitespace fix (through #813)
BlivionIaG Oct 14, 2026
61507de
Merge the ops headers' whitespace fix (through #814)
BlivionIaG Oct 14, 2026
ad93407
Merge the ops headers' whitespace fix (through #815)
BlivionIaG Oct 14, 2026
932f5ca
Merge the ops headers' whitespace fix (through #816)
BlivionIaG Oct 14, 2026
294e01c
Merge the ops headers' whitespace fix (through #817)
BlivionIaG Oct 14, 2026
5537d6a
fix(hip): the paged prefill tile calls expf, as the flat tile does
BlivionIaG Oct 14, 2026
bbf83cd
Merge branch 'hip-kernels-ops' into hip-kernels-affine (the paged pre…
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
28c3160
Merge branch 'qwen35-family' into qwen35-structured (the paged prefil…
BlivionIaG Oct 14, 2026
4a91921
Merge the paged prefill tile's expf (#617, through #621, #697 and #655)
BlivionIaG Oct 14, 2026
4c26705
Merge the paged prefill tile's expf (through #811)
BlivionIaG Oct 14, 2026
4e61d5f
Merge the paged prefill tile's expf (through #812)
BlivionIaG Oct 14, 2026
3ae9475
Merge the paged prefill tile's expf (through #813)
BlivionIaG Oct 14, 2026
1b76637
Merge the paged prefill tile's expf (through #814)
BlivionIaG Oct 14, 2026
a4272df
Merge the paged prefill tile's expf (through #815)
BlivionIaG Oct 14, 2026
fef121c
Merge the paged prefill tile's expf (through #816)
BlivionIaG Oct 14, 2026
01e8edf
Merge the paged prefill tile's expf (through #817)
BlivionIaG Oct 14, 2026
d91b074
fix(hip): one fp32 DeltaNet recurrence at every length; the chunked p…
BlivionIaG Oct 14, 2026
da96b90
Merge branch 'hip-kernels-ops' into hip-kernels-affine (one fp32 Delt…
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
81d6529
Merge branch 'qwen35-family' into qwen35-structured (one fp32 DeltaNe…
BlivionIaG Oct 14, 2026
1bdd859
refactor(hip): the policy's gdn switch goes with the chunked recurren…
BlivionIaG Oct 14, 2026
edc5b3f
refactor(hip): the policy's gdn switch goes with the chunked recurren…
BlivionIaG Oct 14, 2026
02c3ff0
Merge the one fp32 DeltaNet recurrence (#617, through #655)
BlivionIaG Oct 14, 2026
4d5f7a9
Merge the one fp32 DeltaNet recurrence (through #697)
BlivionIaG Oct 14, 2026
060c30e
Merge the one fp32 DeltaNet recurrence (through #621)
BlivionIaG Oct 14, 2026
0ff2cb5
Merge the one fp32 DeltaNet recurrence (through #811)
BlivionIaG Oct 14, 2026
80f45fe
Merge the one fp32 DeltaNet recurrence (through #812)
BlivionIaG Oct 14, 2026
a20af02
Merge the one fp32 DeltaNet recurrence (through #813)
BlivionIaG Oct 14, 2026
0d41fbd
Merge the one fp32 DeltaNet recurrence (through #814)
BlivionIaG Oct 14, 2026
b611071
Merge the one fp32 DeltaNet recurrence (through #815)
BlivionIaG Oct 14, 2026
3d54151
Merge the one fp32 DeltaNet recurrence (through #816)
BlivionIaG Oct 14, 2026
5f390c5
Merge the one fp32 DeltaNet recurrence (through #817)
BlivionIaG Oct 14, 2026
a1aec28
Merge TensorFold 1.0.11 into fn3/f-flashnext-admission (Spec stays in…
BlivionIaG Oct 14, 2026
a5f8646
refactor(flashnext): config.zig and index.zig import no core; widths.…
BlivionIaG Oct 14, 2026
69a4cfd
Merge fn3/f-flashnext-admission (TensorFold 1.0.11) into fn3/g-flashn…
BlivionIaG Oct 14, 2026
0652d0a
fix(flashnext): the HIP n-gram tables read a shard's declared width t…
BlivionIaG Oct 14, 2026
d4f9757
Merge fn3/g-flashnext-engine (TensorFold 1.0.11) into fn3/h-flashnext-tp
BlivionIaG Oct 14, 2026
b473b7c
Merge fn3/h-flashnext-tp (TensorFold 1.0.11) into fn3/i-flashnext-graphs
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(flashnext): the HIP forward on one device: weights, caches, host…
… n-gram tables, every layer and the MTP head
  • Loading branch information
BlivionIaG committed Oct 13, 2026
commit a56342b3c9eaf4fe2df2a6dd914770e704e01f46
10 changes: 9 additions & 1 deletion zig/build/hip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ pub fn steps(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.buil
test_step.dependOn(&server(b, b.graph.host, host, build_options).step);
const gpu = engineModules(b, target, optimize, objects, hip_include);
b.step("tf-qwen35-test", "Qwen3.5 / 3.6 model checks on the GPU (-Dhip-arch)").dependOn(&b.addInstallArtifact(checksExe(b, target, optimize, gpu), .{}).step);
const fx_test = b.addExecutable(.{ .name = "tf-flashnext-hip-test", .root_module = b.createModule(.{
.root_source_file = b.path("zig/tests/flashnext_hip/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.imports = &.{ .{ .name = "hip", .module = gpu.hip }, .{ .name = "flashnext", .module = gpu.flashnext }, .{ .name = "tokenizer", .module = gpu.hip.import_table.get("core").?.import_table.get("tokenizer").? } },
}) });
b.step("tf-flashnext-hip-test", "Flash Next model checks on the GPU (-Dhip-arch)").dependOn(&b.addInstallArtifact(fx_test, .{}).step);
const native = server(b, target, gpu, build_options);
b.step("native-hip", "tensorfold-native with the HIP engines into zig-out/native-hip/bin (-Dhip-arch)").dependOn(&b.addInstallArtifact(native, .{ .dest_dir = .{ .override = .{ .custom = "native-hip/bin" } } }).step);
}
Expand Down Expand Up @@ -146,7 +154,7 @@ fn engineModules(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
.target = target,
.optimize = optimize,
.link_libc = true,
.imports = &.{ .{ .name = "hip", .module = hip }, .{ .name = "lanes", .module = lanes } },
.imports = &.{ .{ .name = "hip", .module = hip }, .{ .name = "lanes", .module = lanes }, .{ .name = "core", .module = core }, .{ .name = "engine_api", .module = api }, .{ .name = "hip_serve", .module = serve } },
});
const native = b.createModule(.{
.root_source_file = b.path("zig/src/native/hip.zig"),
Expand Down
16 changes: 16 additions & 0 deletions zig/src/families/flashnext/hip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ pub const index = @import("index.zig");
pub const Spec = @import("spec.zig").Spec;
pub const plan = @import("hip/plan.zig");
pub const ngram = @import("hip/ngram.zig");
pub const model = @import("hip/model.zig");
pub const load = @import("hip/load.zig");
pub const cache = @import("hip/cache.zig");
pub const tables = @import("hip/tables.zig");
pub const forward = @import("hip/forward.zig");
pub const mixers = @import("hip/mixers.zig");
pub const engine = @import("hip/engine.zig");
pub const head = @import("hip/head.zig");
/// The registry entry of the native server.
pub const native = @import("hip/native.zig");

Expand All @@ -13,5 +21,13 @@ test {
_ = index;
_ = plan;
_ = ngram;
_ = model;
_ = cache;
@import("std").testing.refAllDecls(engine.Engine);
@import("std").testing.refAllDecls(load);
@import("std").testing.refAllDecls(forward);
@import("std").testing.refAllDecls(mixers);
@import("std").testing.refAllDecls(tables.Tables);
@import("std").testing.refAllDecls(head);
_ = native;
}
174 changes: 174 additions & 0 deletions zig/src/families/flashnext/hip/cache.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//! A stream's state on a rank: sparse layers' keys, DeltaNet states, the n-gram tail; a window's per-row snapshots.
const std = @import("std");
const hip = @import("hip");
const model = @import("model.zig");

const Dims = model.Dims;

/// Most rows a verified window holds: the pending token and up to three drafts.
pub const max_window = 4;

/// Where the bytes of one sparse layer's caches start, and their strides.
pub const SparseView = struct { k: u64, v: u64, raw: u64, pooled: u64 };

/// Where one DeltaNet layer's conv tail (fp32 (taps - 1, channels)) and recurrent state (fp32) live.
pub const LinearView = struct { conv: u64, state: u64 };

pub const Layout = struct {
sparse: usize,
linear: usize,
act: usize,
cap: usize,
kv_bytes: usize,
raw_bytes: usize,
pooled_bytes: usize,
conv_bytes: usize,
state_bytes: usize,
tail_rows: usize,
tail_bytes: usize,
kv_heads: usize,
head_dim: usize,
index_dim: usize,
ratio: usize,

pub fn of(d: Dims, kinds: []const bool, cap: usize, act: model.Kind) Layout {
var sparse: usize = 0;
for (kinds) |linear| sparse += @intFromBool(!linear);
const a = act.size();
const blocks = cap / d.index_ratio + 1;
return .{
.sparse = sparse,
.linear = kinds.len - sparse,
.act = a,
.cap = cap,
.kv_bytes = d.kv_heads * cap * d.head_dim * a,
.raw_bytes = cap * d.index_dim * a,
.pooled_bytes = blocks * d.index_dim * a,
.conv_bytes = (d.conv - 1) * d.convWidth() * 4,
.state_bytes = d.value_heads * d.key_dim * d.value_dim * 4,
.tail_rows = d.ngram * (d.ple_taps - 1),
.tail_bytes = d.ngram * (d.ple_taps - 1) * d.wide() * a,
.kv_heads = d.kv_heads,
.head_dim = d.head_dim,
.index_dim = d.index_dim,
.ratio = d.index_ratio,
};
}

fn sparseBytes(l: Layout) usize {
return 2 * l.kv_bytes + l.raw_bytes + l.pooled_bytes;
}

fn linearBytes(l: Layout) usize {
return l.conv_bytes + l.state_bytes;
}

/// Device bytes of a stream's caches at `cap` positions.
pub fn bytes(l: Layout) usize {
return l.sparse * l.sparseBytes() + l.linear * l.linearBytes() + l.tail_bytes;
}

/// Bytes of the state after `at` positions: the sparse layers' first `at` rows, the rest whole.
pub fn stateBytes(l: Layout, at: usize) usize {
const sparse_rows = 2 * l.kv_heads * at * l.head_dim * l.act + at * l.index_dim * l.act + at / l.ratio * l.index_dim * l.act;
return l.sparse * sparse_rows + l.linear * l.linearBytes() + l.tail_bytes;
}

/// Device bytes of a window's snapshots: every DeltaNet layer's conv tail and state after each row.
pub fn windowBytes(l: Layout) usize {
return max_window * l.linear * l.linearBytes();
}

/// Device bytes of the n-gram conv's input over a window: the tail, then the window's rows.
pub fn cinBytes(l: Layout) usize {
return l.tail_bytes + max_window * (l.tail_bytes / l.tail_rows);
}
};

pub const Caches = struct {
layout: Layout,
/// Each layer's place among its kind (sparse or linear).
slot: []usize,
linear: []bool,
mem: hip.DeviceBuffer,
/// Snapshots of the last window: every linear layer's conv tails (a row each), then their states (a row each).
window: hip.DeviceBuffer,
/// The n-gram conv's input over the last window (the tail, then its rows): a keep takes its new tail from it.
cin: hip.DeviceBuffer,
/// Positions kept, and complete blocks pooled in every sparse layer.
len: usize = 0,
pooled: usize = 0,
/// The n-gram history: the last tokens before the next row (eos before any).
history: [2]i64,

pub fn init(gpa: std.mem.Allocator, d: *const hip.Runtime, dims: Dims, kinds: []const bool, cap: usize, act: model.Kind, eos: i64) !Caches {
const layout = Layout.of(dims, kinds, cap, act);
const slot = try gpa.alloc(usize, kinds.len);
errdefer gpa.free(slot);
var counts: [2]usize = .{ 0, 0 };
for (kinds, slot) |linear, *s| {
s.* = counts[@intFromBool(linear)];
counts[@intFromBool(linear)] += 1;
}
const linear = try gpa.dupe(bool, kinds);
errdefer gpa.free(linear);
var mem = try hip.DeviceBuffer.alloc(d, layout.bytes());
errdefer mem.free();
try mem.fill8(0);
var window = try hip.DeviceBuffer.alloc(d, @max(layout.windowBytes(), 4));
errdefer window.free();
var cin = try hip.DeviceBuffer.alloc(d, layout.cinBytes());
errdefer cin.free();
return .{ .layout = layout, .slot = slot, .linear = linear, .mem = mem, .window = window, .cin = cin, .history = .{ eos, eos } };
}

pub fn deinit(c: *Caches, gpa: std.mem.Allocator) void {
c.cin.free();
c.window.free();
c.mem.free();
gpa.free(c.slot);
gpa.free(c.linear);
c.* = undefined;
}

pub fn sparse(c: *const Caches, layer: usize) SparseView {
const l = c.layout;
const at = c.mem.base() + c.slot[layer] * l.sparseBytes();
return .{ .k = at, .v = at + l.kv_bytes, .raw = at + 2 * l.kv_bytes, .pooled = at + 2 * l.kv_bytes + l.raw_bytes };
}

pub fn linearAt(c: *const Caches, layer: usize) LinearView {
const l = c.layout;
const at = c.mem.base() + l.sparse * l.sparseBytes() + c.slot[layer] * l.linearBytes();
return .{ .conv = at, .state = at + l.conv_bytes };
}

/// The n-gram conv's tail rows (act dtype, (tail_rows, W)).
pub fn tail(c: *const Caches) u64 {
const l = c.layout;
return c.mem.base() + l.sparse * l.sparseBytes() + l.linear * l.linearBytes();
}

/// Row `row` of the window's snapshots of linear layer `layer`; a layer's rows consecutive, as the kernels write.
pub fn snapshot(c: *const Caches, layer: usize, row: usize) LinearView {
const l = c.layout;
const n = c.slot[layer] * max_window + row;
const states = c.window.base() + l.linear * max_window * l.conv_bytes;
return .{ .conv = c.window.base() + n * l.conv_bytes, .state = states + n * l.state_bytes };
}
};

test "a stream's bytes at tp 4: 12 sparse layers' caches, 36 DeltaNet states and the n-gram tail" {
const config = @import("../config.zig");
var c = try config.parse(std.testing.allocator, @embedFile("../fixtures/config.json"));
defer c.deinit();
const d = try Dims.of(&c, 4);
var kinds: [48]bool = undefined;
for (&kinds, 0..) |*k, i| k.* = (try c.kind(i)) == .linear_attention;
const l = Layout.of(d, &kinds, 4096, .f16);
try std.testing.expectEqual(@as(usize, 12), l.sparse);
try std.testing.expectEqual(@as(usize, 36), l.linear);
const per_layer = 2 * 4096 * 256 * 2 + 4096 * 128 * 2 + 1025 * 128 * 2;
const linear = 3 * 2560 * 4 + 12 * 128 * 128 * 4;
try std.testing.expectEqual(@as(usize, 12 * per_layer + 36 * linear + 9 * 10240 * 2), l.bytes());
}
47 changes: 47 additions & 0 deletions zig/src/families/flashnext/hip/checkpoint_tables.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! The checkpoint as core/quant reads it for the HIP loader: tensors by name, code widths from the config.
const std = @import("std");
const Config = @import("../config.zig").Config;

/// Each side passes its own core's checkpoint and width types (one by path, one as a module).
pub fn Tables(comptime Checkpoint: type, comptime Width: type) type {
const Got = @typeInfo(@TypeOf(Checkpoint.get)).@"fn".return_type.?;
return struct {
ck: *Checkpoint,
cfg: *const Config,

const Self = @This();

pub fn has(t: *const Self, key: []const u8) bool {
return t.ck.has(key);
}

pub fn get(t: *const Self, key: []const u8) Got {
return t.ck.get(key);
}

/// The width core/quant reads `key`'s words and group tables with.
pub fn width(t: *const Self, key: []const u8) !Width {
const s = (try t.cfg.quantization(key)) orelse return error.UnsupportedQuantization;
return .{ .bits = s.bits, .group = @intCast(s.group) };
}
};
}

test "a projection's width comes from the config's table" {
var c = try @import("../config.zig").parse(std.testing.allocator, @embedFile("../fixtures/config.json"));
defer c.deinit();
const Fake = struct {
pub fn has(_: *@This(), _: []const u8) bool {
return false;
}
pub fn get(_: *@This(), _: []const u8) !u8 {
return error.NotFound;
}
};
var f: Fake = .{};
const t: Tables(Fake, struct { bits: u8, group: u16 }) = .{ .ck = &f, .cfg = &c };
const w = try t.width("language_model.model.layers.0.linear_attn.in_proj_qkv");
try std.testing.expectEqual(@as(u8, 6), w.bits);
try std.testing.expectEqual(@as(u16, 32), w.group);
try std.testing.expect(!t.has("x"));
}
Loading