Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- CUDA kernels: ERNIE 5.5's DSA key lists (`tf_ernie55_window_merge`, in a new `ernie5_5_index` image): each row's indexer top-k picks, then its 128-position sliding window, with the row's count, as the sparse MLA reads them; under the kernel contract against the host's lists; nothing launches it yet, so served output is unchanged (#5211, part of #4926).

- CUDA: Qwen with `--kv-cache fp8` gives the same tokens with the default `TF_QWEN_PDL` on as with it off; the FP8 RoPE kernel no longer starts before its head norms finish (#PR, fixes #5308). Thanks to @jojomondag for the report.

- Tools: a device check that drafted output equals plain output under seeded sampling, on Metal and CUDA, for releases and PR qualification (#5249).
Expand Down
9 changes: 9 additions & 0 deletions docs/kernel-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,15 @@ once in the vendor's order with nothing fused. Each output must be the host's re
TP=2 rank's half; gate 4 runs whole logits with whole ranges and power-of-two weights, where every step is exact
(`ernie5_5-head`; two controls: each term's multiply and add fused into an fma, and the last term's square dropped).

CUDA also checks ERNIE 5.5's DSA key lists (`ernie5_5/index.cu`, `tf_ernie55_window_merge`): each row's indexer
picks (`topk_rows`' out, `picked` of them held to [0, k] and ending at the first -1), then its sliding window's
min(seq, 128) positions up to seq - 1, then -1, as vLLM-ERNIE's `merge_swa_topk` lays them out for the sparse MLA,
with the count of entries a row. The lists must be the host's (`ernie5_5/window.zig`) to the byte, and every row's
entries, rows told a negative count or one past k included, are rows below seq that ascend to seq - 1 with its picks
below the window, from a decode row at the first position and deep in context, across seq = 128 and seq = k + 128,
to prompt chunks and the 1M context's last rows (`ernie5_5-index`; two controls: every row taking all k picks, and
the window's oldest position dropped).

CPU: Qwen3.5-2B's affine projection (4 bits, groups of 64, `cpu_kernels.zig`) at the widths the 2B calls it, one row
to a 128-row prompt chunk, registered once for each chain count of its projection classes (16; 32 for the delta rule's
a and b; 8 for the head), and its RMS norm over 2048-wide rows; each registration's runs take 1 to 8 threads in turn,
Expand Down
1 change: 1 addition & 0 deletions zig/build/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub fn targets(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.bu
runner.addImport("hy4_ihc_fused_cuda", b.createModule(.{ .root_source_file = b.path("zig/src/families/hy4_flash/ihc_fused_cuda.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "cuda", .module = cuda }} }));
runner.addImport("ernie5_5_cuda", b.createModule(.{ .root_source_file = b.path("zig/src/families/ernie5_5/cuda_head.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "cuda", .module = cuda }} }));
runner.addImport("hy4_route_rows_cuda", b.createModule(.{ .root_source_file = b.path("zig/src/families/hy4_flash/rows_cuda.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "cuda", .module = cuda }} }));
runner.addImport("ernie5_5_index_cuda", b.createModule(.{ .root_source_file = b.path("zig/src/families/ernie5_5/cuda_index.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "cuda", .module = cuda }} }));
runner.addImport("gemma4_cuda", @import("gemma4_cuda.zig").module(b, target, optimize, cuda, mods.core, mods.lanes));
runner.addImport("spark25", @import("spark25.zig").module(b, target, optimize, cuda, mods.core, mods.lanes));
// the served family and its engine, for the dots runner
Expand Down
1 change: 1 addition & 0 deletions zig/build/cuda_kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ pub const kernels = [_]Kernel{
.{ .name = "hy4_route", .src = "hy4_flash/route", .flags = glue },
.{ .name = "hy4_rows", .src = "hy4_flash/rows", .flags = &.{"-O3"} },
.{ .name = "hy4_dfly2_glue", .src = "hy4_flash/dfly2_glue", .flags = glue },
.{ .name = "ernie5_5_index", .src = "ernie5_5/index", .flags = &.{"-O3"} }, // ours: ERNIE 5.5's DSA key lists
.{ .name = "minimax_m3", .flags = glue }, // ours: MiniMax-M3.1-Flash's glue (norms, RoPE prep, routing, DSpark, tower RoPE)
.{ .name = "minimax_m3_attn", .flags = glue }, // ours: MiniMax-M3.1-Flash's indexer and block-sparse attention
.{ .name = "idol", .flags = glue }, // ours: Idol's denoiser glue and its fp16 head-64 attention
Expand Down
55 changes: 55 additions & 0 deletions zig/kernels/cuda/ernie5_5/index.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// ERNIE 5.5's sparse-attention key lists: each row's indexer picks, then its sliding window, for the sparse MLA.

namespace ernie55i {

enum Fault { none, all_picks, short_window };

// Row blockIdx.x at pos[0] + row: picks to picked[row] in [0, k] and the first -1, the window to seq - 1, -1, a count.
template <Fault F>
__device__ __forceinline__ void merge(const int* __restrict__ picks, const int* __restrict__ picked,
const int* __restrict__ pos, int* __restrict__ tok, int* __restrict__ cnt, int k,
int window, long long w) {
__shared__ int valid;
const long long row = blockIdx.x, seq = static_cast<long long>(pos[0]) + row + 1;
const int p = picked[row];
const long long held = F == all_picks ? k : p < 0 ? 0 : p > k ? k : p;
const long long sw = (seq < window ? seq : window) - (F == short_window ? 1 : 0), first = seq - sw;
const int* in = picks + row * k;
if (threadIdx.x == 0) valid = static_cast<int>(held);
__syncthreads();
// a negative pick inside the held count ends the picks (the control keeps every pick it is told)
if (F != all_picks)
for (long long c = threadIdx.x; c < held; c += blockDim.x)
if (in[c] < 0) atomicMin(&valid, static_cast<int>(c));
__syncthreads();
const long long n = valid;
int* out = tok + row * w;
for (long long c = threadIdx.x; c < w; c += blockDim.x)
out[c] = c < n ? in[c] : c < n + sw ? static_cast<int>(first + (c - n)) : -1;
if (threadIdx.x == 0) cnt[row] = static_cast<int>(n + sw);
}

} // namespace ernie55i

// One row a block of 256 threads; lists w wide (at least k + window).
extern "C" __global__ void __launch_bounds__(256)
tf_ernie55_window_merge(const int* __restrict__ picks, const int* __restrict__ picked, const int* __restrict__ pos,
int* __restrict__ tok, int* __restrict__ cnt, int k, int window, long long w) {
ernie55i::merge<ernie55i::none>(picks, picked, pos, tok, cnt, k, window, w);
}

// The contract's planted control: every row takes all k picks, its -1s past picked among them.
extern "C" __global__ void __launch_bounds__(256)
tf_ctl_ernie55_window_merge_all_picks(const int* __restrict__ picks, const int* __restrict__ picked,
const int* __restrict__ pos, int* __restrict__ tok, int* __restrict__ cnt,
int k, int window, long long w) {
ernie55i::merge<ernie55i::all_picks>(picks, picked, pos, tok, cnt, k, window, w);
}

// The contract's planted control: the window's oldest position dropped.
extern "C" __global__ void __launch_bounds__(256)
tf_ctl_ernie55_window_merge_short_window(const int* __restrict__ picks, const int* __restrict__ picked,
const int* __restrict__ pos, int* __restrict__ tok,
int* __restrict__ cnt, int k, int window, long long w) {
ernie55i::merge<ernie55i::short_window>(picks, picked, pos, tok, cnt, k, window, w);
}
3 changes: 2 additions & 1 deletion zig/src/core/contract/ernie5_5_records.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! ERNIE 5.5's CUDA launches (ernie5_5/head.cu): MultiMax on the head's fp32 logits.
//! ERNIE 5.5's CUDA launches: MultiMax on the head's fp32 logits (ernie5_5/head.cu), the DSA key lists (index.cu).
const registry = @import("registry.zig");

/// nvcc 13.0's usage (nvcc -cubin per SM at the image's flags), the compiler of the release fatbins.
pub const cuda = [_]registry.CudaRecord{
.{ .launch = .{ .name = "ernie5_5 head tf_ernie55_multimax", .symbol = "tf_ernie55_multimax", .image = "ernie5_5_head", .threads = 256 }, .usage = .{ .{ .regs = 12, .static = 0, .bound = 256 }, .{ .regs = 12, .static = 0, .bound = 256 }, .{ .regs = 12, .static = 0, .bound = 256 }, .{ .regs = 12, .static = 0, .bound = 256 }, .{ .regs = 12, .static = 0, .bound = 256 } } },
.{ .launch = .{ .name = "ernie5_5 index tf_ernie55_window_merge", .symbol = "tf_ernie55_window_merge", .image = "ernie5_5_index", .threads = 256 }, .usage = .{ .{ .regs = 24, .static = 4, .bound = 256 }, .{ .regs = 24, .static = 4, .bound = 256 }, .{ .regs = 24, .static = 4, .bound = 256 }, .{ .regs = 40, .static = 4, .bound = 256 }, .{ .regs = 40, .static = 4, .bound = 256 } } },
};

test "ERNIE 5.5 CUDA: every launch is in the live registry and fits every SM" {
Expand Down
1 change: 1 addition & 0 deletions zig/src/cuda/kernels.zig
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub const inkling2_attn: []const u8 = if (available) &Blob("fatbin_inkling2_attn
pub const inkling2_pattn: []const u8 = if (available) &Blob("fatbin_inkling2_pattn").bytes else &.{};
pub const inkling2_ops: []const u8 = if (available) &Blob("fatbin_inkling2_ops").bytes else &.{};
pub const ernie5_5_head: []const u8 = if (available) &Blob("fatbin_ernie5_5_head").bytes else &.{};
pub const ernie5_5_index: []const u8 = if (available) &Blob("fatbin_ernie5_5_index").bytes else &.{};
pub const inkling2_rows: []const u8 = if (available) &Blob("fatbin_inkling2_rows").bytes else &.{};
pub const inkling2_dspark: []const u8 = if (available) &Blob("fatbin_inkling2_dspark").bytes else &.{};
pub const ricursive: []const u8 = if (available) &Blob("fatbin_ricursive").bytes else &.{};
Expand Down
37 changes: 37 additions & 0 deletions zig/src/families/ernie5_5/cuda_index.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! ERNIE 5.5's indexer kernels (ernie5_5/index.cu): the sparse MLA's key lists, each row's picks then its window.
const cuda = @import("cuda");
pub const window = @import("window.zig");

pub const threads = 256;

pub const Index = struct {
mod: cuda.Module,
merge_fn: cuda.Function,

pub fn load(d: *const cuda.Driver) !Index {
var mod = try cuda.Module.load(d, cuda.kernels.ernie5_5_index);
errdefer mod.unload();
return .{ .mod = mod, .merge_fn = try mod.function("tf_ernie55_window_merge") };
}

pub fn unload(x: *Index) void {
x.mod.unload();
}

/// topk_rows' out and picked, pos (row r at pos[0] + r) into tok i32 [rows, w] and cnt i32 [rows], as window.merge.
pub fn merge(x: *const Index, s: cuda.Stream, picks: u64, picked: u64, pos: u64, tok: u64, cnt: u64, sh: window.Shape) !void {
try launch(x.merge_fn, s, picks, picked, pos, tok, cnt, sh);
}
};

/// `f`, tf_ernie55_window_merge or an entry of the image with its arguments, one row a block.
pub fn launch(f: cuda.Function, s: cuda.Stream, picks: u64, picked: u64, pos: u64, tok: u64, cnt: u64, sh: window.Shape) !void {
try window.check(sh);
if (sh.rows == 0) return;
var a: cuda.Args = .{};
inline for (.{ picks, picked, pos, tok, cnt }) |v| a.add(v);
a.add(@as(c_int, @intCast(sh.k)));
a.add(@as(c_int, @intCast(sh.window)));
a.add(@as(i64, @intCast(sh.w)));
try cuda.launch.launch(f, .{ .grid = .{ .x = @intCast(sh.rows) }, .block = .{ .x = threads } }, s, &a);
}
3 changes: 2 additions & 1 deletion zig/src/families/ernie5_5/host_test.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Host-only ERNIE 5.5 family package: config, weight map, layer plan, chat encoding, tokenizer, MultiMax. No GPU.
//! Host-only ERNIE 5.5 family: config, weight map, layer plan, chat encoding, tokenizer, MultiMax, key lists. No GPU.
test {
_ = @import("config.zig");
_ = @import("config_test.zig");
Expand All @@ -9,4 +9,5 @@ test {
_ = @import("template_test.zig");
_ = @import("tokenizer_test.zig");
_ = @import("multimax_test.zig");
_ = @import("window_test.zig");
}
33 changes: 33 additions & 0 deletions zig/src/families/ernie5_5/window.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! ERNIE 5.5's sparse-attention key lists: each row's indexer picks, then its sliding window (vLLM's merge_swa_topk).
const std = @import("std");

/// The released config's sliding_window and index_topk.
pub const window = 128;
pub const topk = 2048;

/// rows rows, row r at position pos0 + r; k picks a row in, lists w wide out (at least k + window).
pub const Shape = struct { rows: usize, k: usize, w: usize, window: usize = window };

/// Sizes the kernel holds in i32, and lists wide enough for every pick and the window.
pub fn check(sh: Shape) error{WindowShape}!void {
const most = std.math.maxInt(i32);
if (sh.window == 0 or sh.k > most or sh.window > most or sh.w < sh.k + sh.window or sh.rows > most / sh.w)
return error.WindowShape;
}

/// Row r: picks to picked[r] (in [0, k]) and the first negative, seq - min(seq, window) .. seq - 1, -1, its count.
pub fn merge(sh: Shape, picks: []const i32, picked: []const i32, pos0: i64, tok: []i32, cnt: []i32) void {
for (0..sh.rows) |r| {
const seq = pos0 + @as(i64, @intCast(r)) + 1;
const held: usize = @intCast(std.math.clamp(picked[r], 0, @as(i32, @intCast(sh.k))));
const n = for (picks[r * sh.k ..][0..held], 0..) |v, i| {
if (v < 0) break i; // a negative pick inside the count ends the picks
} else held;
const sw: usize = @intCast(@min(seq, @as(i64, @intCast(sh.window))));
const row = tok[r * sh.w ..][0..sh.w];
@memcpy(row[0..n], picks[r * sh.k ..][0..n]);
for (row[n..][0..sw], 0..) |*t, i| t.* = @intCast(seq - @as(i64, @intCast(sw)) + @as(i64, @intCast(i)));
@memset(row[n + sw ..], -1);
cnt[r] = @intCast(n + sw);
}
}
99 changes: 99 additions & 0 deletions zig/src/families/ernie5_5/window_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! window.zig against vLLM-ERNIE's merge_swa_topk, and its refusals.
const std = @import("std");
const win = @import("window.zig");

/// merge_swa_topk as the vendor's Triton kernel writes a column: its n from the position, not from picked.
fn vendor(topk: []const i32, k: usize, window: usize, seq: i64, col: usize) i32 {
const n: i64 = @min(@max(seq - @as(i64, @intCast(window)), 0), @as(i64, @intCast(k)));
const sw: i64 = @min(seq, @as(i64, @intCast(window)));
const c: i64 = @intCast(col);
if (c < n) return topk[col];
return if (c < n + sw) @intCast(seq - sw + c - n) else -1;
}

fn run(a: std.mem.Allocator, sh: win.Shape, pos0: i64, picks: []const i32, picked: []const i32) !struct { []i32, []i32 } {
const tok = try a.alloc(i32, sh.rows * sh.w);
const cnt = try a.alloc(i32, sh.rows);
win.merge(sh, picks, picked, pos0, tok, cnt);
return .{ tok, cnt };
}

test "with picked as top-k gives it, each list is the vendor's, across the window's and the picks' edges" {
const a = std.testing.allocator;
inline for (.{ .{ 4, 3, 9 }, .{ win.topk, win.window, win.topk + win.window + 64 } }) |g| {
const k: usize = g[0];
const sh: win.Shape = .{ .rows = 2 * (k + g[1]) + 3, .k = k, .w = g[2], .window = g[1] };
const picks = try a.alloc(i32, sh.rows * k);
defer a.free(picks);
const picked = try a.alloc(i32, sh.rows);
defer a.free(picked);
for (0..sh.rows) |r| {
const seq: i64 = @intCast(r + 1);
const n: usize = @intCast(@min(@max(seq - @as(i64, @intCast(sh.window)), 0), @as(i64, @intCast(k))));
picked[r] = @intCast(n);
// n ascending picks spread below seq - window, as topk_rows leaves them, then -1
const older = seq - @as(i64, @intCast(sh.window));
for (picks[r * k ..][0..k], 0..) |*p, i| p.* = if (i < n) @intCast(@divFloor(@as(i64, @intCast(i)) * older, @as(i64, @intCast(n)))) else -1;
}
const tok, const cnt = try run(a, sh, 0, picks, picked);
defer a.free(tok);
defer a.free(cnt);
for (0..sh.rows) |r| {
const seq: i64 = @intCast(r + 1);
var valid: i32 = 0;
for (0..sh.w) |c| {
const want = vendor(picks[r * k ..][0..k], k, sh.window, seq, c);
try std.testing.expectEqual(want, tok[r * sh.w + c]);
valid += @intFromBool(want != -1);
}
try std.testing.expectEqual(valid, cnt[r]);
try std.testing.expectEqual(@as(i32, @intCast(seq - 1)), tok[r * sh.w + @as(usize, @intCast(cnt[r])) - 1]);
}
}
}

test "rows from a later position: the window ends at each row's own position" {
const a = std.testing.allocator;
const sh: win.Shape = .{ .rows = 3, .k = 2, .w = 6, .window = 3 };
const picks = [_]i32{ 5, 90, 7, 91, 8, 92 };
const tok, const cnt = try run(a, sh, 99, &picks, &.{ 2, 2, 2 });
defer a.free(tok);
defer a.free(cnt);
try std.testing.expectEqualSlices(i32, &.{ 5, 90, 97, 98, 99, -1, 7, 91, 98, 99, 100, -1, 8, 92, 99, 100, 101, -1 }, tok);
try std.testing.expectEqualSlices(i32, &.{ 5, 5, 5 }, cnt);
}

test "picked is held to [0, k]: a negative one lists the window alone, one past k lists k picks" {
const a = std.testing.allocator;
const sh: win.Shape = .{ .rows = 2, .k = 2, .w = 5, .window = 2 };
const tok, const cnt = try run(a, sh, 9, &.{ 1, 2, 3, 4 }, &.{ -3, 7 });
defer a.free(tok);
defer a.free(cnt);
try std.testing.expectEqualSlices(i32, &.{ 8, 9, -1, -1, -1, 3, 4, 9, 10, -1 }, tok);
try std.testing.expectEqualSlices(i32, &.{ 2, 4 }, cnt);
}

test "a count past the picks' nonnegative prefix stops at the first -1: no -1 before a list's count" {
const a = std.testing.allocator;
const sh: win.Shape = .{ .rows = 2, .k = 2, .w = 5, .window = 3 };
// picks [5, -1] told 7 at seq 10 (the review's case), then a row whose first pick is -1
const tok, const cnt = try run(a, sh, 9, &.{ 5, -1, -1, 4 }, &.{ 7, 2 });
defer a.free(tok);
defer a.free(cnt);
try std.testing.expectEqualSlices(i32, &.{ 5, 7, 8, 9, -1, 8, 9, 10, -1, -1 }, tok);
try std.testing.expectEqualSlices(i32, &.{ 4, 3 }, cnt);
for (0..sh.rows) |r| for (tok[r * sh.w ..][0..@intCast(cnt[r])]) |v| try std.testing.expect(v >= 0);
}

test "check refuses lists narrower than k + window, no window, and sizes past i32" {
try win.check(.{ .rows = 16, .k = win.topk, .w = win.topk + win.window });
try std.testing.expectError(error.WindowShape, win.check(.{ .rows = 1, .k = 4, .w = 6, .window = 3 }));
try std.testing.expectError(error.WindowShape, win.check(.{ .rows = 1, .k = 4, .w = 6, .window = 0 }));
try std.testing.expectError(error.WindowShape, win.check(.{ .rows = 1 << 20, .k = win.topk, .w = 4096 }));
}

test "check refuses a row count whose rows * w would wrap, before multiplying" {
// 2^63 + 1 rows of 2 on a 64-bit host (the review's case), 2^31 + 1 on a 32-bit one
const rows = (@as(usize, 1) << (@bitSizeOf(usize) - 1)) | 1;
try std.testing.expectError(error.WindowShape, win.check(.{ .rows = rows, .k = 0, .w = 2, .window = 1 }));
}
Loading
Loading