Skip to content
Merged
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

- Qwen3.5/3.8 on CUDA: `TF_QWEN35_ATTN_ALL_ROWS=1` sends multi-row BF16 decode windows to the existing rows attention kernel at every context length (it already matches the one-row kernel's score and reduction order), removing the dead second launch below 8K; off by default (#N).

- Tests: a GB10 check compares Qwen CUDA prompts run in one pass against 512-row capped cuts with a second stream live, byte for byte and over the next 64 greedy tokens (#N).

- Flash Next on Metal: `TF_FLASHNEXT_BATCH_GDN_WIDE=1` runs 8- and 16-row batch segments on the existing non-replay GDN kernel instead of the recorded pipe; off by default until the M3 and M5 checks (#N).
Expand Down
1 change: 1 addition & 0 deletions docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Diagnostics quote and escape the supplied text so it cannot create another termi
| `TENSORFOLD_UNIFIED_HOST_COUNTS` | Exactly `0` or `1`, checked when the CUDA backend starts. An integrated GPU on Linux admits against `/proc/meminfo` with `1` and against the CUDA pool with `0`; unset, it reads the pool under WSL2 (`WSL_DISTRO_NAME` set) and `/proc/meminfo` otherwise ([Windows](WINDOWS.md)) |
| `TF_HEAD_GEOMETRY` | Whole native-size nonnegative geometry count in the head benchmark |
| `TF_ROCE_GID_INDEX` | Whole index 0 through 2147483647; unset searches for a RoCE v2 IPv4 GID |
| `TF_QWEN35_ATTN_ALL_ROWS` | Qwen3.8 on CUDA: exactly `0` or `1`; unset, empty or `0` keeps the current BF16 decode attention launches. `1` uses the existing rows kernel at every context length and skips the separate partial launch for decode windows wider than one row. The kernel contract compares output bytes against the original dual-launch path |
| `TF_QWEN35_CHUNK_SOLO` | Qwen 3.5/3.6/3.8 on CUDA: 4096 or 8192 rows a foreground prompt pass while no other stream decodes (a background warm pass keeps 4096); unset, empty or 4096 is off, with main's scratch. Checked when the model loads, with the family's own message |
| `TF_GEMMA4_CHUNK_SOLO` | Gemma 4 on CUDA: a multiple of 512 from 512 through 4096; unset, empty or 512 is off. Checked when the model loads, with the family's own message |
| `TF_GEMMA4_ARGMAX_GRAPH` | Gemma 4 on CUDA: exactly `0` or `1`; default off. `1` captures a greedy verify round's argmax (the same `tf_round_bf16_argmax` over the same logits) in the target forward's graph, skips the drawn-token download no greedy entry reads, and stages each forward's ids, positions and slots in pinned memory; target tokens keep their bits |
Expand Down
4 changes: 2 additions & 2 deletions zig/dense_serving_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ test "dense serving: owned CUDA images retain their reviewed lane and decode byt
try std.testing.expectEqualStrings("8780fd710be3ce2f04e53fa1e69eb062809d3bec6391d9bd49983d0588e374d0", &hash(group));
// #2825 hoists copy addresses before the K loop; copy order and math match prefill_kernel.
try std.testing.expectEqualStrings("e3724114b87cae6f6f761b2f86357f05b1693d84a0233e4d58ce46349fa853f6", &hash(@embedFile("kernels/cuda/qmm_prefill.cu")));
// join variant's tf_q35_attn_rows_join for shared pages keeps SASS; #4706 adds TF_QWEN_PDL prologues (default off)
try std.testing.expectEqualStrings("12cb39dc09ba9e00fec8837739091cc2f305092043b0c92e23e0300c987c9ad3", &hash(@embedFile("kernels/cuda/qwen35.cu")));
// KIA14 adds all_rows (false keeps selection/arithmetic); #4706 adds default-off TF_QWEN_PDL prologues
try std.testing.expectEqualStrings("79e97b0992f82e891ff8a80cdad68b534e07606b98feef3dffe25101477c99e1", &hash(@embedFile("kernels/cuda/qwen35.cu")));
// #1879's bytes: a comment names cuda_scratch.zig, where max_chunks moved; the code is unchanged
try std.testing.expectEqualStrings("f9e4a040cf219aac8db211df25d264dba390fce4a7e8da45557652c6ff0f28ba", &hash(@embedFile("kernels/cuda/qwen35.cuh")));
// 67711a6aa7's bytes: the opt-in DSpine draft kernels (#2633); default serving unchanged
Expand Down
12 changes: 6 additions & 6 deletions zig/kernels/cuda/qwen35.cu
Original file line number Diff line number Diff line change
Expand Up @@ -554,18 +554,18 @@ extern "C" __global__ void __launch_bounds__(128) tf_q35_attn_partial(Geo geo, c
extern "C" __global__ void __launch_bounds__(256) tf_q35_attn_rows(Geo geo, const __nv_bfloat16* __restrict__ Q, const Seg* __restrict__ segs,
const int* __restrict__ rowseg, const Ancestor* __restrict__ rowanc, int layer,
const int* __restrict__ P, float scale, int total,
float* __restrict__ PM, float* __restrict__ PL, float* __restrict__ PO) {
float* __restrict__ PM, float* __restrict__ PL, float* __restrict__ PO, bool all_rows) {
extern __shared__ __align__(16) uint4 attn_tiles[];
tf_q35_tc::attn_rows<4, tf_q35_tc::Bf16Tiles<256>>({attn_tiles, nullptr, nullptr, nullptr}, geo, Q, segs, rowseg, rowanc, layer, P, scale, total, PM, PL, PO);
tf_q35_tc::attn_rows<4, tf_q35_tc::Bf16Tiles<256>>({attn_tiles, nullptr, nullptr, nullptr}, geo, Q, segs, rowseg, rowanc, layer, P, scale, total, PM, PL, PO, all_rows);
}

// tf_q35_attn_rows for a forward whose streams share pages (cuda_cache.anyShares): rows on one page run join.
extern "C" __global__ void __launch_bounds__(256) tf_q35_attn_rows_join(Geo geo, const __nv_bfloat16* __restrict__ Q, const Seg* __restrict__ segs,
const int* __restrict__ rowseg, const Ancestor* __restrict__ rowanc, int layer,
const int* __restrict__ P, float scale, int total,
float* __restrict__ PM, float* __restrict__ PL, float* __restrict__ PO) {
float* __restrict__ PM, float* __restrict__ PL, float* __restrict__ PO, bool all_rows) {
extern __shared__ __align__(16) uint4 attn_tiles[];
tf_q35_tc::attn_rows<4, tf_q35_tc::Bf16Tiles<256>, true>({attn_tiles, nullptr, nullptr, nullptr}, geo, Q, segs, rowseg, rowanc, layer, P, scale, total, PM, PL, PO);
tf_q35_tc::attn_rows<4, tf_q35_tc::Bf16Tiles<256>, true>({attn_tiles, nullptr, nullptr, nullptr}, geo, Q, segs, rowseg, rowanc, layer, P, scale, total, PM, PL, PO, all_rows);
}

extern "C" __global__ void __launch_bounds__(128) tf_q35_attn_partial_whole(Geo geo, const __nv_bfloat16* __restrict__ Q, const Seg* __restrict__ segs,
Expand All @@ -579,7 +579,7 @@ extern "C" __global__ void __launch_bounds__(128) tf_q35_attn_partial_whole(Geo
extern "C" __global__ void __launch_bounds__(256) tf_q35_attn_rows_whole(Geo geo, const __nv_bfloat16* __restrict__ Q, const Seg* __restrict__ segs,
const int* __restrict__ rowseg, const Ancestor* __restrict__ rowanc, int layer,
const int* __restrict__ P, float scale, int total,
float* __restrict__ PM, float* __restrict__ PL, float* __restrict__ PO) {
float* __restrict__ PM, float* __restrict__ PL, float* __restrict__ PO, bool all_rows) {
extern __shared__ __align__(16) uint4 attn_tiles[];
tf_q35_tc::attn_rows<4, tf_q35_tc::WholeBf16Tiles<256>>({attn_tiles, nullptr, nullptr, nullptr}, geo, Q, segs, rowseg, rowanc, layer, P, scale, total, PM, PL, PO);
tf_q35_tc::attn_rows<4, tf_q35_tc::WholeBf16Tiles<256>>({attn_tiles, nullptr, nullptr, nullptr}, geo, Q, segs, rowseg, rowanc, layer, P, scale, total, PM, PL, PO, all_rows);
}
6 changes: 3 additions & 3 deletions zig/kernels/cuda/qwen35_attn_tc.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ template <int MT, class Tiles, bool JOIN = false> // JOIN: another stream's row
__device__ __forceinline__ void attn_rows(TileStore store, Geo geo, const __nv_bfloat16* __restrict__ Q, const Seg* __restrict__ segs,
const int* __restrict__ rowseg, const Ancestor* __restrict__ rowanc, int layer,
const int* __restrict__ P, float scale, int total,
float* __restrict__ PM, float* __restrict__ PL, float* __restrict__ PO) {
float* __restrict__ PM, float* __restrict__ PL, float* __restrict__ PO, bool all_rows = false) {
constexpr int LINES = 16 * MT, WARPS = 2 * MT, NT = HD / 8 / WARPS;
__shared__ __align__(16) __nv_bfloat16 pt[LINES][TK + 8];
__shared__ float red[2][LINES], alpha[LINES];
Expand All @@ -237,12 +237,12 @@ __device__ __forceinline__ void attn_rows(TileStore store, Geo geo, const __nv_b
const int tid = threadIdx.x, warp = tid >> 5, lane = tid & 31, g = lane >> 2, qd = lane & 3;
const int c = blockIdx.y, h = blockIdx.z, m0 = blockIdx.x * RB;
if (c >= MAXCH) return;
// the group's rows of long streams (a short stream's rows are attn_one's)
// Long streams, or every stream under the exact all-length opt-in.
if (tid < RB) {
const int m = m0 + tid;
r_seg[tid] = -1;
if constexpr (JOIN) r_p0[tid] = -1;
if (m < total && !past_rows(P, m) && segs[rowseg[m]].len >= GROUP_LEN) {
if (m < total && !past_rows(P, m) && (all_rows || segs[rowseg[m]].len >= GROUP_LEN)) {
const Seg sg = segs[rowseg[m]];
const Ancestor anc = rowanc[m];
const int keys = sg.len + rows_seen(anc), ck = chunk_keys(keys);
Expand Down
30 changes: 30 additions & 0 deletions zig/src/families/qwen3_5/cuda_attention.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
//! A batch with a paged stream keys its captured graph apart; the kernels read each stream through its own layout.
const std = @import("std");
const environment = @import("sys").environment;

const all_rows_name = "TF_QWEN35_ATTN_ALL_ROWS";

pub fn parseAllRows(raw: ?[]const u8) !bool {
return environment.value(bool, all_rows_name, raw, false);
}

pub fn allRows() !bool {
return environment.read(bool, all_rows_name, false);
}

/// Only a BF16 decode window wider than one row takes the single all-length rows launch.
pub fn rowsOnly(enabled: bool, prompt: bool, total: usize) bool {
return enabled and !prompt and total > 1;
}

pub fn hasPages(segs: anytype) bool {
for (segs) |sg| if (sg.pt != 0) return true;
Expand All @@ -14,6 +30,20 @@ test "Qwen attention path: unpaged, paged and mixed segment tables" {
try std.testing.expect(hasPages(@as([]const Seg, &.{ .{ .pt = 0 }, .{ .pt = 0x4000 } })));
}

test "Qwen attention path: all-rows flag is strict and defaults off" {
for ([_]?[]const u8{ null, "", " ", "\t", " \t\r\n" }) |raw| try std.testing.expect(!try parseAllRows(raw));
try std.testing.expect(!try parseAllRows("0"));
try std.testing.expect(try parseAllRows("1"));
for ([_][]const u8{ "2", "yes", "on", "true", "00", "1 " }) |raw| try std.testing.expectError(error.InvalidEnvironment, parseAllRows(raw));
}

test "Qwen attention path: all-rows opt-in is limited to multi-row decode windows" {
for ([_]bool{ false, true }) |prompt| for ([_]usize{ 1, 4, 8, 16 }) |total| {
try std.testing.expect(!rowsOnly(false, prompt, total));
try std.testing.expectEqual(!prompt and total > 1, rowsOnly(true, prompt, total));
};
}

test "Qwen attention path: layout is part of every captured shape key" {
const Shape = @import("cuda_shapes.zig").Shape;
for (1..129) |rows| for (1..rows + 1) |segments| for ([_]u8{ 1, 2, 4, 8, 16 }) |slots| {
Expand Down
4 changes: 2 additions & 2 deletions zig/src/families/qwen3_5/cuda_engine.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub const Engine = struct {
stream: cuda.Stream,
mods: [19]cuda.Module,
kv8: bool, // attention caches as e4m3 rows with a scale each
attention_all_rows: bool = false, // exact opt-in all-length BF16 rows for multi-row decode windows
rope_table: u64 = 0, // YaRN's attention factor and 32 frequencies on the device (cuda_rope.zig); 0: plain rope
rope_buf: ?cuda.DeviceBuffer = null, // the table's buffer (cuda_rope_table.zig)
fp8: Kv8,
Expand Down Expand Up @@ -205,6 +206,7 @@ pub const Engine = struct {
e.geo = .{ .hid = @intCast(g.hidden), .nk = @intCast(g.k_heads), .nv = @intCast(g.v_heads), .qh = @intCast(g.q_heads), .kvh = @intCast(g.kv_heads), .nl = @intCast(g.deltas()) };
e.verify_rows = most_verify;
e.kv8 = kv8;
e.attention_all_rows = try @import("cuda_attention.zig").allRows();
e.rope_table = 0; // plain rope until cuda_rope.zig uploads a YaRN table (the engine starts undefined)
e.rope_buf = null;
e.window_rows = most_window;
Expand Down Expand Up @@ -292,12 +294,10 @@ pub const Engine = struct {
errdefer recurrent.shutdown(e);
e.startup.add(try profile.withCache(e.gpa, io, e, Cache));
}

/// Reserve prompt INT8 workspace before serving; prompt forwards are not graph-captured.
pub fn warmPromptInt8(e: *Engine) !void {
try e.int8.ensureRows(e, e.scratch_rows);
}

/// Every path a request takes, run once now: WDDM stalls a process's first forwards for seconds (a 20 s launch).
pub fn warmCache(e: *Engine, warm_cache: *Cache) !void {
// Commit the configured prompt and verify ceiling before admission; warmup then reuses it.
Expand Down
7 changes: 5 additions & 2 deletions zig/src/families/qwen3_5/cuda_forward.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const E = @import("cuda_engine.zig");
const tap_window = @import("cuda_cache.zig").tap_window;
const cuda = @import("cuda");
const sig = @import("cuda_signatures.zig");
const attention = @import("cuda_attention.zig");
const hooks = @import("cuda_learn_hooks.zig");
const geometry = @import("cuda_geometry.zig");
const modelopt = @import("cuda_modelopt.zig");
Expand Down Expand Up @@ -129,8 +130,10 @@ pub fn body(e: *E.Engine, shape: anytype) !void {
const rows_fn = if (shape.mt2) (if (shape.paged) e.mt2.paged else e.mt2.whole) else (if (shape.join) e.fp8.attn_rows_join else if (shape.paged) e.fp8.attn_rows else e.fp8.attn_rows8_whole);
try launch(e, rows_fn, sig.attn_rows8, .{ groups, max_chunks, g.kv_heads }, .{ if (shape.mt2) @as(usize, 128) else 256, 1, 1 }, rows_args);
} else {
try shared(e, if (shape.paged) e.fns.attn_partial else e.fns.attn_partial_whole, sig.attn_partial, .{ total, max_chunks, g.kv_heads }, 128, args);
try shared(e, if (shape.join) e.fns.attn_rows_join else if (shape.paged) e.fns.attn_rows else e.fns.attn_rows_whole, sig.attn_rows, .{ groups, max_chunks, g.kv_heads }, 256, rows_args);
const rows_only = attention.rowsOnly(e.attention_all_rows, prompt, total);
const bf16_rows_args = .{ geo, e.qn, t_segs, t_rows, t_anc, @as(c_int, ai), t_params, @as(f32, 1.0 / 16.0), @as(c_int, @intCast(total)), e.pm, e.pl, e.po, rows_only };
if (!rows_only) try shared(e, if (shape.paged) e.fns.attn_partial else e.fns.attn_partial_whole, sig.attn_partial, .{ total, max_chunks, g.kv_heads }, 128, args);
try shared(e, if (shape.join) e.fns.attn_rows_join else if (shape.paged) e.fns.attn_rows else e.fns.attn_rows_whole, sig.attn_rows, .{ groups, max_chunks, g.kv_heads }, 256, bf16_rows_args);
}
try launch(e, e.fns.attn_merge, sig.attn_merge, .{ g.q_heads, total, 1 }, .{ 32, 1, 1 }, .{ geo, e.pm, e.pl, e.po, t_segs, t_rows, t_anc, t_params, e.y });
}
Expand Down
9 changes: 5 additions & 4 deletions zig/src/families/qwen3_5/cuda_signatures.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! The q35 attention kernels' parameter lists, as their .cu sources declare them.
const std = @import("std");

/// A parameter's kind: the geometry struct, a device pointer, a 32-bit count or a float scale.
pub const Kind = enum { geo, ptr, i32, f32 };
/// A parameter's kind: geometry struct, device pointer, 32-bit count, float scale or Boolean mode.
pub const Kind = enum { geo, ptr, i32, f32, boolean };

/// One kernel's parameter list, in the .cu source's order.
pub const Sig = struct {
Expand All @@ -23,7 +23,7 @@ pub const attn_partial = Sig{ .name = "attn_partial", .handle = "e.fns.attn_part
pub const attn_partial8 = Sig{ .name = "attn_partial8", .handle = "e.fp8.attn_partial", .symbol = "tf_q35_attn_partial8", .params = &.{ .geo, .ptr, .ptr, .ptr, .ptr, .i32, .ptr, .f32, .ptr, .ptr, .ptr } };

/// The bf16 decode attention over a long stream's rows: a block a group of rows.
pub const attn_rows = Sig{ .name = "attn_rows", .handle = "e.fns.attn_rows", .symbol = "tf_q35_attn_rows", .params = &.{ .geo, .ptr, .ptr, .ptr, .ptr, .i32, .ptr, .f32, .i32, .ptr, .ptr, .ptr } };
pub const attn_rows = Sig{ .name = "attn_rows", .handle = "e.fns.attn_rows", .symbol = "tf_q35_attn_rows", .params = &.{ .geo, .ptr, .ptr, .ptr, .ptr, .i32, .ptr, .f32, .i32, .ptr, .ptr, .ptr, .boolean } };

/// The e4m3 decode attention over a long stream's rows.
pub const attn_rows8 = Sig{ .name = "attn_rows8", .handle = "e.fp8.attn_rows", .symbol = "tf_q35_attn_rows8", .params = &.{ .geo, .ptr, .ptr, .ptr, .ptr, .i32, .ptr, .f32, .i32, .ptr, .ptr, .ptr } };
Expand All @@ -35,7 +35,7 @@ pub const attn_rows8_join = Sig{ .name = "attn_rows8_join", .handle = "e.fp8.att
/// Whole-window instances use the same ABI without consulting Seg.pt.
pub const attn_partial_whole = Sig{ .name = "attn_partial_whole", .handle = "e.fns.attn_partial_whole", .symbol = "tf_q35_attn_partial_whole", .params = &.{ .geo, .ptr, .ptr, .ptr, .ptr, .i32, .ptr, .f32, .ptr, .ptr, .ptr } };
pub const attn_partial8_whole = Sig{ .name = "attn_partial8_whole", .handle = "e.fp8.attn_partial8_whole", .symbol = "tf_q35_attn_partial8_whole", .params = &.{ .geo, .ptr, .ptr, .ptr, .ptr, .i32, .ptr, .f32, .ptr, .ptr, .ptr } };
pub const attn_rows_whole = Sig{ .name = "attn_rows_whole", .handle = "e.fns.attn_rows_whole", .symbol = "tf_q35_attn_rows_whole", .params = &.{ .geo, .ptr, .ptr, .ptr, .ptr, .i32, .ptr, .f32, .i32, .ptr, .ptr, .ptr } };
pub const attn_rows_whole = Sig{ .name = "attn_rows_whole", .handle = "e.fns.attn_rows_whole", .symbol = "tf_q35_attn_rows_whole", .params = &.{ .geo, .ptr, .ptr, .ptr, .ptr, .i32, .ptr, .f32, .i32, .ptr, .ptr, .ptr, .boolean } };
pub const attn_rows8_whole = Sig{ .name = "attn_rows8_whole", .handle = "e.fp8.attn_rows8_whole", .symbol = "tf_q35_attn_rows8_whole", .params = &.{ .geo, .ptr, .ptr, .ptr, .ptr, .i32, .ptr, .f32, .i32, .ptr, .ptr, .ptr } };

/// The merge: a row's chunks' partials into each (row, query head)'s output.
Expand Down Expand Up @@ -79,6 +79,7 @@ pub fn kindOf(comptime T: type) ?Kind {
if (T == u64) return .ptr;
if (T == c_int) return .i32;
if (T == f32) return .f32;
if (T == bool) return .boolean;
const info = @typeInfo(T);
if (info == .@"struct" and info.@"struct".layout == .@"extern") return .geo;
return null;
Expand Down
1 change: 1 addition & 0 deletions zig/src/platform/environment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub const rules = [_]Rule{
.{ .name = "TF_QWEN_KV_HOST_GIB", .scope = .cuda, .kind = .u64, .min = 1, .max = std.math.maxInt(u64) >> 30, .forms = "positive whole GiB fitting an unsigned 64-bit byte count; blank or unset: one full KV window" },
.{ .name = "TF_CACHE_EVICT", .scope = .common, .kind = .word, .words = &.{ "main", "decayed_resume" }, .forms = "main or decayed_resume (default main)" },
.{ .name = "TF_FLASHNEXT_BATCH_DRAWS", .scope = .cuda, .kind = .flag, .blank_default = false, .forms = "0 or 1 (0 restores per-stream Flash Next row draws; default 1)" },
.{ .name = "TF_QWEN35_ATTN_ALL_ROWS", .scope = .cuda, .kind = .flag, .forms = "0 or 1 (default 0; 1 runs exact all-length BF16 rows for multi-row Qwen decode windows)" },
.{ .name = "TF_STARTUP_RECEIPT", .scope = .common, .kind = .flag, .forms = "0 or 1 (opt-in startup and first completion receipts)" },
.{ .name = "TF_SHARED_PRICE_LOG", .scope = .common, .kind = .flag, .forms = "0 or 1 (1 logs the shared plain price, a line a round)" },
.{ .name = "TENSORFOLD_DEVICE_CHECK", .scope = .common, .kind = .flag, .forms = "0 or 1 (0 skips serve's device-profile check; default 1)" },
Expand Down
Loading
Loading