Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 2 additions & 2 deletions zig/build/hip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn steps(b: *std.Build, target: std.Build.ResolvedTarget, test_step: *std.Bu
}

/// The model-free kernels' source groups, in zig/src/hip/kernels.zig's Group order.
const groups = [_][]const u8{ "ops/ops.hip", "ops/act.hip", "attention/attention.hip", "recurrence/gated_delta.hip", "attention/prefill.hip", "recurrence/gdn_prefill.hip", "decode/decode.hip", "decode/plan.hip", "tiles/dot2_tiles.hip", "tiles/dot2.hip" };
const groups = [_][]const u8{ "ops/ops.hip", "ops/act.hip", "attention/attention.hip", "recurrence/gated_delta.hip", "attention/prefill.hip", "decode/decode.hip", "decode/plan.hip", "tiles/dot2_tiles.hip", "tiles/dot2.hip" };

/// What the group sources include, so an edit to one rebuilds them.
const headers = [_][]const u8{
Expand All @@ -102,7 +102,7 @@ const headers = [_][]const u8{
/// The flags of the kernels' first build: no contraction, wave32 on RDNA, C++20.
const flags = [_][]const u8{ "-D__HIP_PLATFORM_AMD__=1", "-DUSE_ROCM=1", "-DHIPBLAS_V2", "-fPIC", "-DCUDA_HAS_FP16=1", "-DHIP_ENABLE_WARP_SYNC_BUILTINS=1", "-std=c++20", "-fno-gpu-rdc", "-mno-wavefrontsize64", "-ffp-contract=off" };

const stub = "pub const arch = \"\";\npub const images: [10][]align(8) const u8 = @splat(&.{});\n";
const stub = "pub const arch = \"\";\npub const images: [9][]align(8) const u8 = @splat(&.{});\n";

/// One code object a group for `arch`, built with the caps table's instruction switches, as the `hip_kernels` module.
fn kernelObjects(b: *std.Build, hipcc: []const u8, hipcc_resolved: []const u8, device_lib: ?[]const u8, arch: []const u8) *std.Build.Module {
Expand Down
471 changes: 0 additions & 471 deletions zig/kernels/hip/recurrence/gdn_prefill.hip

This file was deleted.

76 changes: 60 additions & 16 deletions zig/src/hip/kernel_tests/gdn.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! The DeltaNet recurrence, token-serial and chunked, against a float64 recurrence on random inputs.
//! The DeltaNet recurrence against a float64 recurrence on random inputs, and the same bits however a prompt is cut.
const std = @import("std");
const rig = @import("rig.zig");
const Rig = rig.Rig;
const gpa = rig.gpa;
const DeviceBuffer = @import("../memory.zig").DeviceBuffer;

const d = 128;
const Shape = struct { length: usize, key_heads: usize, value_heads: usize };
Expand Down Expand Up @@ -105,7 +106,15 @@ fn relative(got: []const f32, want: []const f64) f64 {
return max / scale;
}

test "the serial and chunked DeltaNet stay within 5e-3 of the float64 recurrence at four lengths" {
/// One call of `length` rows from row `from`, state in place; q, k, v, gate and beta are offset to that row.
fn run(t: *Rig, sh: Shape, q: DeviceBuffer, k: DeviceBuffer, v: DeviceBuffer, gate: DeviceBuffer, beta: DeviceBuffer, state: DeviceBuffer, y: DeviceBuffer, from: usize, length: usize) !void {
const qk = from * sh.key_heads * d * 4;
const vy = from * sh.value_heads * d * 4;
const gb = from * sh.value_heads * 4;
try t.ops(&t.on).gatedDelta(rig.at(q) + qk, rig.at(k) + qk, rig.at(v) + vy, rig.at(gate) + gb, rig.at(beta) + gb, rig.at(state), rig.at(y) + vy, length, sh.key_heads, sh.value_heads, d, d, null);
}

test "the DeltaNet stays within 5e-3 of the float64 recurrence at four lengths" {
const t = try Rig.open(1 << 30);
defer t.close();
for ([_]Shape{
Expand Down Expand Up @@ -135,19 +144,54 @@ test "the serial and chunked DeltaNet stay within 5e-3 of the float64 recurrence
defer y.free();
var state = try t.upload(in.state);
defer state.free();
// `off` runs the token-serial kernel, `on` the chunked one for these lengths
for ([_]*const @import("../launches.zig").Launcher{ &t.off, &t.on }) |l| {
try state.upload(0, std.mem.sliceAsBytes(in.state));
try y.fill8(0);
t.arena.reset();
try t.ops(l).gatedDelta(rig.at(q), rig.at(k), rig.at(v), rig.at(gate), rig.at(beta), rig.at(state), rig.at(y), sh.length, sh.key_heads, sh.value_heads, d, d, null);
try t.stream.synchronize();
const gy = try rig.download(f32, y);
defer gpa.free(gy);
const gs = try rig.download(f32, state);
defer gpa.free(gs);
try std.testing.expect(relative(gy, ry) < 5e-3);
try std.testing.expect(relative(gs, rs) < 5e-3);
}
try run(t, sh, q, k, v, gate, beta, state, y, 0, sh.length);
try t.stream.synchronize();
const gy = try rig.download(f32, y);
defer gpa.free(gy);
const gs = try rig.download(f32, state);
defer gpa.free(gs);
try std.testing.expect(relative(gy, ry) < 5e-3);
try std.testing.expect(relative(gs, rs) < 5e-3);
}
}

test "a prompt cut on either side of 64 rows gives the same rows and state as one call" {
const t = try Rig.open(1 << 30);
defer t.close();
const sh: Shape = .{ .length = 200, .key_heads = 2, .value_heads = 4 };
const in = try Inputs.make(sh, 0x51ed);
defer in.free();
var q = try t.upload(in.q);
defer q.free();
var k = try t.upload(in.k);
defer k.free();
var v = try t.upload(in.v);
defer v.free();
var gate = try t.upload(in.gate);
defer gate.free();
var beta = try t.upload(in.beta);
defer beta.free();
var y = try t.alloc(sh.length * sh.value_heads * d * 4);
defer y.free();
var state = try t.upload(in.state);
defer state.free();
try run(t, sh, q, k, v, gate, beta, state, y, 0, sh.length);
try t.stream.synchronize();
const wy = try rig.download(f32, y);
defer gpa.free(wy);
const ws = try rig.download(f32, state);
defer gpa.free(ws);
for ([_]usize{ 1, 63, 64, 65, 136 }) |c| {
try state.upload(0, std.mem.sliceAsBytes(in.state));
try y.fill8(0);
try run(t, sh, q, k, v, gate, beta, state, y, 0, c);
try run(t, sh, q, k, v, gate, beta, state, y, c, sh.length - c);
try t.stream.synchronize();
const gy = try rig.download(f32, y);
defer gpa.free(gy);
const gs = try rig.download(f32, state);
defer gpa.free(gs);
try std.testing.expectEqualSlices(u32, @ptrCast(wy), @ptrCast(gy));
try std.testing.expectEqualSlices(u32, @ptrCast(ws), @ptrCast(gs));
}
}
4 changes: 2 additions & 2 deletions zig/src/hip/kernel_tests/rig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub const Rig = struct {
stream: Stream,
/// Every kernel choice on, as a run takes them.
on: Launcher,
/// Merged decode launches and the chunked DeltaNet off: the kernels they replace.
/// Merged decode launches off: the kernels they replace.
off: Launcher,
arena: Arena,
/// The activation type: bf16 where the GPU has a bf16 dot2, fp16 on RDNA2.
Expand All @@ -62,7 +62,7 @@ pub const Rig = struct {
errdefer t.stream.deinit();
t.on = try Launcher.load(&t.r, .{}, kernels.images);
errdefer t.on.unload();
t.off = try Launcher.load(&t.r, .{ .fuse = false, .chunked = false }, kernels.images);
t.off = try Launcher.load(&t.r, .{ .fuse = false }, kernels.images);
errdefer t.off.unload();
t.arena = try Arena.init(&t.r, arena_bytes);
t.bf16 = caps.Caps.of(kernels.arch).?.act == .bf16;
Expand Down
2 changes: 1 addition & 1 deletion zig/src/hip/kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const objects = @import("hip_kernels");

/// The source groups of zig/kernels/hip, in the order the build embeds them.
pub const Group = enum { ops, act, attention, gated_delta, prefill, gdn_prefill, decode, plan, affine_tiles, affine_dot2 };
pub const Group = enum { ops, act, attention, gated_delta, prefill, decode, plan, affine_tiles, affine_dot2 };
pub const group_count = @typeInfo(Group).@"enum".field_names.len;

/// The architecture the objects were built for; the loader takes them only on that device.
Expand Down
11 changes: 1 addition & 10 deletions zig/src/hip/launches.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ pub const PlanArgs = plan.PlanArgs;
pub const PlanRef = plan.PlanRef;
pub const PlanKeep = plan.Keep;
pub const gdn_chunk = recurrence.gdn_chunk;
pub const GdnScratch = recurrence.GdnScratch;
pub const gdnScratch = recurrence.gdnScratch;

pub const Launcher = struct {
r: *const runtime.Runtime,
Expand All @@ -66,7 +64,6 @@ pub const Launcher = struct {
fa_prefill: Triple,
fa_wide: [2]Function, // the 64-row prefill tile, fp16 and bf16 caches
fa_paged: [2]Function, // the same tile over a stream's pages
gdn_chunked: [5]Function, // prep, kt, wy, h, o
softmax_stats: Function,
sum_partials: Function,
op: Ops,
Expand All @@ -78,8 +75,6 @@ pub const Launcher = struct {
fuse: bool,
/// The 64-row prefill attention tile is on.
wide: bool,
/// The DeltaNet prefill runs chunked (off keeps the one-token recurrence).
chunked: bool,
/// The MLX affine products and the registry that picks their tiles.
affine: affine.Kernels,

Expand Down Expand Up @@ -130,7 +125,7 @@ pub const Launcher = struct {
};

/// Which kernels a run takes where two give the same bits; every one defaults on.
pub const Choices = struct { fuse: bool = true, wide: bool = true, chunked: bool = true, products: affine.Choices = .{} };
pub const Choices = struct { fuse: bool = true, wide: bool = true, products: affine.Choices = .{} };

/// Loads the code objects on the current device and resolves every kernel the launchers use.
pub fn load(r: *const runtime.Runtime, choices: Choices, images: [kernels.group_count][]align(8) const u8) Error!Launcher {
Expand All @@ -151,7 +146,6 @@ pub const Launcher = struct {
const pl = l.mods[@backingInt(kernels.Group.plan)];
l.fuse = choices.fuse;
l.wide = choices.wide;
l.chunked = choices.chunked;
l.dec = .{ .router = .{ try dec.function("tf_router_decode_f16"), try dec.function("tf_router_decode_bf16") }, .tail = try dec.function("tf_tail"), .conv_split = try dec.function("tf_conv_split"), .rms2 = try dec.function("tf_rms2"), .gnorm_out = try dec.function("tf_gnorm_out"), .select = try dec.function("tf_select_decode") };
l.plan = .{
.kv_write = try pl.function("tf_plan_kv_write"),
Expand All @@ -166,8 +160,6 @@ pub const Launcher = struct {
};
l.fa_wide = .{ try pre.function("tf_fa_wide_f16"), try pre.function("tf_fa_wide_bf16") };
l.fa_paged = .{ try pre.function("tf_fa_paged_f16"), try pre.function("tf_fa_paged_bf16") };
const gp = l.mods[@backingInt(kernels.Group.gdn_prefill)];
l.gdn_chunked = .{ try gp.function("tf_gdn_prep"), try gp.function("tf_gdn_kt"), try gp.function("tf_gdn_wy"), try gp.function("tf_gdn_h"), try gp.function("tf_gdn_o") };
l.op = .{
.embed_rows = try ops.function("tf_embed_rows"),
.embed_dense = try ops.function("tf_embed_dense"),
Expand Down Expand Up @@ -297,7 +289,6 @@ pub const Launcher = struct {
pub const pagesWrite = plan.pagesWrite;
pub const pagesGather = plan.pagesGather;
pub const planGather = plan.planGather;
pub const gdnChunked = recurrence.gdnChunked;
pub const tf_gdn_gate_prefill = recurrence.tf_gdn_gate_prefill;
pub const tf_gnorm_silu = recurrence.tf_gnorm_silu;
pub const tf_embed_rows = elementwise.tf_embed_rows;
Expand Down
90 changes: 1 addition & 89 deletions zig/src/hip/launches/recurrence.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! DeltaNet launches: gate, serial and chunked recurrence, and the gated norm.
//! DeltaNet launches: gate, recurrence and the gated norm.

const std = @import("std");
const Launcher = @import("../launches.zig").Launcher;
const util = @import("util.zig");
const S = util.S;
Expand All @@ -16,22 +15,6 @@ const cdiv = util.cdiv;
const tri = util.tri;
const invalid = util.invalid;

/// The chunked DeltaNet's chunk, in rows.
pub const gdn_chunk = 64;

/// Byte sizes of the chunked DeltaNet's scratch, rows padded to whole chunks.
pub const GdnScratch = struct { qk: usize, gc: usize, wu: usize, h: usize, total: usize };

pub fn gdnScratch(length: usize, key_heads: usize, value_heads: usize) GdnScratch {
const chunks = cdiv(length, gdn_chunk);
const rows = chunks * gdn_chunk;
const qk = std.mem.alignForward(usize, rows * key_heads * 128 * 2, 256);
const gc = std.mem.alignForward(usize, length * value_heads * 4, 256);
const wu = std.mem.alignForward(usize, rows * value_heads * 128 * 2, 256);
const h = chunks * value_heads * 128 * 128 * 2;
return .{ .qk = qk, .gc = gc, .wu = wu, .h = h, .total = 3 * qk + gc + 4 * wu + h };
}

pub fn tf_gdn_gate(l: *const Launcher, av: C, bv: C, kind: c_int, a_log: CF, dt_bias: CF, gate: F, beta: F, count: c_int, heads: c_int, s: S) Error!void {
var a: Args = .{};
try a.add(ad(av));
Expand Down Expand Up @@ -71,77 +54,6 @@ pub fn tf_gated_delta(l: *const Launcher, q: CF, k: CF, v: CF, gate: CF, beta: C
}
}

/// The chunked DeltaNet prefill of `length` rows (dk = dv = 128, batch 1), `scratch` of `gdnScratch` bytes.
pub fn gdnChunked(l: *const Launcher, q: CF, k: CF, v: CF, gate: CF, beta: CF, state: F, y: F, length: usize, key_heads: usize, value_heads: usize, scratch: u64, s: S) Error!void {
if (length < 1 or key_heads < 1 or @rem(value_heads, key_heads) != 0) return invalid("gdn chunked");
const sc = gdnScratch(length, key_heads, value_heads);
const qh = scratch;
const kh = qh + sc.qk;
const kt = kh + sc.qk;
const vh = kt + sc.qk;
const gc = vh + sc.wu;
const w = gc + sc.gc;
const u = w + sc.wu;
const vt = u + sc.wu;
const hb = vt + sc.wu;
const chunks = cdiv(length, gdn_chunk);
const len: c_int = @intCast(length);
const hk: c_int = @intCast(key_heads);
const hv: c_int = @intCast(value_heads);
var a: Args = .{};
try a.add(ad(q));
try a.add(ad(k));
try a.add(ad(v));
try a.add(qh);
try a.add(kh);
try a.add(vh);
try a.add(len);
try a.add(hk);
try a.add(hv);
try l.go(l.gdn_chunked[0], dim(cdiv(length * (key_heads + value_heads) * 32, 256), 1, 1), dim(256, 1, 1), 0, s, &a);
var t: Args = .{};
try t.add(kh);
try t.add(kt);
try t.add(len);
try t.add(hk);
try l.go(l.gdn_chunked[1], dim(chunks, key_heads, 1), dim(256, 1, 1), 0, s, &t);
var b: Args = .{};
try b.add(kh);
try b.add(vh);
try b.add(ad(beta));
try b.add(ad(gate));
try b.add(gc);
try b.add(w);
try b.add(u);
try b.add(len);
try b.add(hk);
try b.add(hv);
try l.go(l.gdn_chunked[2], dim(chunks, value_heads, 1), dim(256, 1, 1), 0, s, &b);
var c: Args = .{};
try c.add(kt);
try c.add(w);
try c.add(u);
try c.add(vt);
try c.add(gc);
try c.add(ad(state));
try c.add(hb);
try c.add(len);
try c.add(hk);
try c.add(hv);
try l.go(l.gdn_chunked[3], dim(8, value_heads, 1), dim(256, 1, 1), 0, s, &c);
var d: Args = .{};
try d.add(qh);
try d.add(kh);
try d.add(vt);
try d.add(hb);
try d.add(gc);
try d.add(ad(y));
try d.add(len);
try d.add(hk);
try d.add(hv);
try l.go(l.gdn_chunked[4], dim(chunks, value_heads, 1), dim(256, 1, 1), 0, s, &d);
}

pub fn tf_gdn_gate_prefill(l: *const Launcher, av: C, bv: C, kind: c_int, a_log: CF, dt_bias: CF, gate: F, beta: F, count: c_int, heads: c_int, s: S) Error!void {
var a: Args = .{};
try a.add(ad(av));
Expand Down
2 changes: 1 addition & 1 deletion zig/src/hip/ops/ops.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub const Ops = struct {
bf16: bool,
stream: abi.Stream,
arena: *Arena,
/// A prompt's span: products, router and recurrence take prefill's kernel, so rows have the same bits however cut.
/// A prompt's span: products and router take prefill's kernel, so rows have the same bits however cut.
prefill: bool = false,
/// A lane round's forward: decode tiles at any row count, so a row's bits ignore the rows it shares a round with.
window: bool = false,
Expand Down
25 changes: 1 addition & 24 deletions zig/src/hip/ops/recurrence.zig
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
//! The linear attention: the conv, the gates and the DeltaNet recurrence.

const t = @import("types.zig");
const launches = @import("../launches.zig");
const Ops = @import("ops.zig").Ops;
const Error = t.Error;
const Tensor = t.Tensor;
const p = t.p;
const f = t.f;
const int = t.int;

/// Rows from which the DeltaNet prefill runs chunked, and the rows of one chunked segment (a multiple of the chunk).
const gdn_min_rows = 64;
const gdn_segment = 4096;

pub fn convPrefill(o: Ops, x: Tensor, weight: u64, state: ?u64, out: u64, new_state: u64, len: usize, channels: usize, kernel: usize) Error!void {
if (kernel < 1 or kernel > 8) return error.BadShape;
try o.l.tf_conv_prefill(p(x.ptr), @backingInt(x.kind), f(weight), if (state) |s| f(s) else null, f(out), f(new_state), int(len), int(channels), int(kernel), o.stream);
Expand All @@ -35,25 +30,7 @@ pub fn gdnGate(o: Ops, a: Tensor, b: Tensor, a_log: u64, dt_bias: u64, gate: u64
try o.l.tf_gdn_gate(p(a.ptr), p(b.ptr), @backingInt(a.kind), f(a_log), f(dt_bias), f(gate), f(beta), int(elements), int(heads), o.stream);
}

/// The chunked recurrence in segments of `gdn_segment` rows, whose scratch the arena hands back after each.
fn gatedDeltaChunked(o: Ops, z: *const launches.Launcher, q: u64, k: u64, v: u64, gate: u64, beta: u64, state: u64, y: u64, length: usize, key_heads: usize, value_heads: usize) Error!void {
var start: usize = 0;
while (start < length) : (start += gdn_segment) {
const rows = @min(gdn_segment, length - start);
const mark = o.arena.mark();
defer o.arena.release(mark);
const scratch = try o.arena.take(launches.gdnScratch(rows, key_heads, value_heads).total);
const qk = start * key_heads * 128 * 4;
const vy = start * value_heads * 128 * 4;
const gb = start * value_heads * 4;
try z.gdnChunked(f(q + qk), f(k + qk), f(v + vy), f(gate + gb), f(beta + gb), f(state), f(y + vy), rows, key_heads, value_heads, scratch, o.stream);
}
}

/// The DeltaNet recurrence, state in place; a prefill of `gdn_min_rows` rows or more runs chunked unless reference.
/// The DeltaNet recurrence in fp32, state in place, the same walk at every length.
pub fn gatedDelta(o: Ops, q: u64, k: u64, v: u64, gate: u64, beta: u64, state: u64, y: u64, length: usize, key_heads: usize, value_heads: usize, dk: usize, dv: usize, states: ?u64) Error!void {
if (states == null and (length >= gdn_min_rows or o.prefill) and dk == 128 and dv == 128 and value_heads % key_heads == 0) {
if (o.l.chunked) return gatedDeltaChunked(o, o.l, q, k, v, gate, beta, state, y, length, key_heads, value_heads);
}
try o.l.tf_gated_delta(f(q), f(k), f(v), f(gate), f(beta), f(state), f(y), 1, int(length), int(key_heads), int(value_heads), int(dk), int(dv), o.stream, if (states) |s| f(s) else null);
}
Loading