diff --git a/CHANGELOG.md b/CHANGELOG.md index 45c2050a752..a80cbea9cae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/docs/environment.md b/docs/environment.md index 93a4317e5fc..fa5c91a2fe2 100644 --- a/docs/environment.md +++ b/docs/environment.md @@ -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 | diff --git a/zig/dense_serving_test.zig b/zig/dense_serving_test.zig index 57f1499bc9f..6bfdfd2b9cf 100644 --- a/zig/dense_serving_test.zig +++ b/zig/dense_serving_test.zig @@ -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 diff --git a/zig/kernels/cuda/qwen35.cu b/zig/kernels/cuda/qwen35.cu index fa65456fcea..e309a302fdd 100644 --- a/zig/kernels/cuda/qwen35.cu +++ b/zig/kernels/cuda/qwen35.cu @@ -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, @@ -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); } diff --git a/zig/kernels/cuda/qwen35_attn_tc.cuh b/zig/kernels/cuda/qwen35_attn_tc.cuh index e5dfbaa4b4c..ca3c5995438 100644 --- a/zig/kernels/cuda/qwen35_attn_tc.cuh +++ b/zig/kernels/cuda/qwen35_attn_tc.cuh @@ -225,7 +225,7 @@ template // 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]; @@ -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); diff --git a/zig/src/families/qwen3_5/cuda_attention.zig b/zig/src/families/qwen3_5/cuda_attention.zig index eee13a817f1..27dc699b906 100644 --- a/zig/src/families/qwen3_5/cuda_attention.zig +++ b/zig/src/families/qwen3_5/cuda_attention.zig @@ -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; @@ -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| { diff --git a/zig/src/families/qwen3_5/cuda_engine.zig b/zig/src/families/qwen3_5/cuda_engine.zig index 7ff380f43b6..30dbe7b7619 100644 --- a/zig/src/families/qwen3_5/cuda_engine.zig +++ b/zig/src/families/qwen3_5/cuda_engine.zig @@ -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, @@ -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; @@ -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. diff --git a/zig/src/families/qwen3_5/cuda_forward.zig b/zig/src/families/qwen3_5/cuda_forward.zig index 56ab6e16352..ca29f16b215 100644 --- a/zig/src/families/qwen3_5/cuda_forward.zig +++ b/zig/src/families/qwen3_5/cuda_forward.zig @@ -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"); @@ -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 }); } diff --git a/zig/src/families/qwen3_5/cuda_signatures.zig b/zig/src/families/qwen3_5/cuda_signatures.zig index 66087358d20..1631cceffe3 100644 --- a/zig/src/families/qwen3_5/cuda_signatures.zig +++ b/zig/src/families/qwen3_5/cuda_signatures.zig @@ -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 { @@ -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 } }; @@ -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. @@ -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; diff --git a/zig/src/platform/environment.zig b/zig/src/platform/environment.zig index e8affea3d65..80aadeae85c 100644 --- a/zig/src/platform/environment.zig +++ b/zig/src/platform/environment.zig @@ -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)" }, diff --git a/zig/src/platform/environment_test.zig b/zig/src/platform/environment_test.zig index 35b9e66dc42..cd1a7a4fde1 100644 --- a/zig/src/platform/environment_test.zig +++ b/zig/src/platform/environment_test.zig @@ -45,6 +45,8 @@ const valid = [_]Case{ .{ .name = "TF_CUDA_POOL_AFTER_WARMUP", .raw = "1", .n = 1 }, .{ .name = "TF_FLASHNEXT_READOUT_FUSED", .raw = "0", .n = 0 }, .{ .name = "TF_FLASHNEXT_READOUT_FUSED", .raw = "1", .n = 1 }, + .{ .name = "TF_QWEN35_ATTN_ALL_ROWS", .raw = "0", .n = 0 }, + .{ .name = "TF_QWEN35_ATTN_ALL_ROWS", .raw = "1", .n = 1 }, .{ .name = "TENSORFOLD_VIDEO_TOKENS", .raw = "0", .n = 0 }, .{ .name = "TENSORFOLD_VIDEO_TOKENS", .raw = "18446744073709551615", .n = std.math.maxInt(u64) }, .{ .name = "TENSORFOLD_MAX_PDF_PAGES", .raw = "4294967295", .n = std.math.maxInt(u32) }, diff --git a/zig/tests/cuda/contract_q35_attn.zig b/zig/tests/cuda/contract_q35_attn.zig index 9ede8ed9f86..bf423e80642 100644 --- a/zig/tests/cuda/contract_q35_attn.zig +++ b/zig/tests/cuda/contract_q35_attn.zig @@ -29,9 +29,9 @@ const Ancestor = extern struct { bits: u64, depth: u32, chain: u32 }; pub const Cache = enum { bf16, fp8 }; -/// A registered form: its cache, and for the control a launch that drops each row's last chunk. -pub const Form = struct { cache: Cache, control: bool = false, mt2: bool = false, whole: bool = false }; -pub const forms = [_]Form{ .{ .cache = .bf16 }, .{ .cache = .fp8 }, .{ .cache = .bf16, .control = true }, .{ .cache = .fp8, .mt2 = true }, .{ .cache = .fp8, .mt2 = true, .whole = true } }; +/// A registered form: cache/layout controls, optional wrong-output control, and the exact all-length BF16 opt-in. +pub const Form = struct { cache: Cache, control: bool = false, mt2: bool = false, whole: bool = false, all_rows: bool = false }; +pub const forms = [_]Form{ .{ .cache = .bf16 }, .{ .cache = .fp8 }, .{ .cache = .bf16, .control = true }, .{ .cache = .fp8, .mt2 = true }, .{ .cache = .fp8, .mt2 = true, .whole = true }, .{ .cache = .bf16, .all_rows = true } }; /// A row's merged output sums every key it sees, then the chunks' partials (at most 64). fn keyChain(s: c.Shape) usize { @@ -45,12 +45,17 @@ const stress_keys = 2048; const nan_key = "a NaN key in one of a row's several chunks leaves the row finite: tf_q35_attn_merge skips that chunk"; pub fn spec(f: Form) c.Spec { - const name = if (f.mt2) (if (f.whole) "cuda qwen35 MT2 fp8 whole attention" else "cuda qwen35 MT2 fp8 paged attention") else switch (f.cache) { + const name = if (f.all_rows) "cuda qwen35 opt-in all-length BF16 rows attention" else if (f.mt2) (if (f.whole) "cuda qwen35 MT2 fp8 whole attention" else "cuda qwen35 MT2 fp8 paged attention") else switch (f.cache) { .bf16 => if (f.control) "control qwen35 attention dropping each row's last chunk" else "cuda qwen35 decode attention, bf16 cache (tf_q35_attn_partial, tf_q35_attn_merge)", .fp8 => "cuda qwen35 decode attention, fp8 cache (tf_q35_attn_partial8, tf_q35_attn_merge)", }; - // a DFlash2 window past 2,048 keys, four streams' windows, a 128-row prompt chunk, 256-key chunks past 8,192 - const served: []const c.Shape = if (f.mt2) &.{ .{ .rows = 1, .n = qh, .k = 512 }, .{ .rows = 1, .n = qh, .k = 8191 }, .{ .rows = 1, .n = qh, .k = 8192 }, .{ .rows = 3, .n = qh, .k = 32768 }, .{ .rows = 5, .n = qh, .k = 9000 }, .{ .rows = 6, .n = qh, .k = 9000 }, .{ .rows = 16, .n = qh, .k = 65535 }, .{ .rows = 1, .n = qh, .k = 122779 } } else &.{ .{ .rows = 16, .n = qh, .k = 2100 }, .{ .rows = 64, .n = qh, .k = 700 }, .{ .rows = 128, .n = qh, .k = 300 }, .{ .rows = 16, .n = qh, .k = 9000 }, .{ .rows = 64, .n = qh, .k = 9000 } }; + // Required opt-in cells plus the established short/long, multi-window, prompt-chain and MT2 coverage. + const served: []const c.Shape = if (f.all_rows) &.{ + .{ .rows = 1, .n = qh, .k = 512 }, .{ .rows = 4, .n = qh, .k = 512 }, .{ .rows = 8, .n = qh, .k = 512 }, .{ .rows = 16, .n = qh, .k = 512 }, + .{ .rows = 1, .n = qh, .k = 2048 }, .{ .rows = 4, .n = qh, .k = 2048 }, .{ .rows = 8, .n = qh, .k = 2048 }, .{ .rows = 16, .n = qh, .k = 2048 }, + .{ .rows = 1, .n = qh, .k = 7168 }, .{ .rows = 4, .n = qh, .k = 7168 }, .{ .rows = 8, .n = qh, .k = 7168 }, .{ .rows = 16, .n = qh, .k = 7168 }, + .{ .rows = 1, .n = qh, .k = 16384 }, .{ .rows = 4, .n = qh, .k = 16384 }, .{ .rows = 8, .n = qh, .k = 16384 }, .{ .rows = 16, .n = qh, .k = 16384 }, + } else if (f.mt2) &.{ .{ .rows = 1, .n = qh, .k = 512 }, .{ .rows = 1, .n = qh, .k = 8191 }, .{ .rows = 1, .n = qh, .k = 8192 }, .{ .rows = 3, .n = qh, .k = 32768 }, .{ .rows = 5, .n = qh, .k = 9000 }, .{ .rows = 6, .n = qh, .k = 9000 }, .{ .rows = 16, .n = qh, .k = 65535 }, .{ .rows = 1, .n = qh, .k = 122779 } } else &.{ .{ .rows = 16, .n = qh, .k = 2100 }, .{ .rows = 64, .n = qh, .k = 700 }, .{ .rows = 128, .n = qh, .k = 300 }, .{ .rows = 16, .n = qh, .k = 9000 }, .{ .rows = 64, .n = qh, .k = 9000 } }; return .{ .name = name, .accum = .f32, .depth = keyChain, .ops = 3, .out = .bf16, .replaces = if (f.mt2) "the four-MMA FP8 row tile" else "the chunk-major grid (each row alone)", .tiles = .{ .{ .size = if (f.mt2) 32 / group else width }, .{}, .{ .size = 128 } }, .served = served, .sums = .{ .why = attn.why }, .known = .{ .gate = .nonfinite, .finding = nan_key } }; } @@ -316,7 +321,9 @@ pub const Q35 = struct { rows_args.add(ptr); } const shared: u32 = if (self.form.cache == .bf16) attn_tiles else 0; - try cuda.launch.launch(self.partial, .{ .grid = .{ .x = @intCast(n), .y = @intCast(z), .z = kvh }, .block = .{ .x = 128 }, .shared = shared }, self.rig.s, &args); + const rows_only = self.form.all_rows and n > 1; + if (self.form.cache == .bf16) rows_args.add(rows_only); + if (!rows_only) try cuda.launch.launch(self.partial, .{ .grid = .{ .x = @intCast(n), .y = @intCast(z), .z = kvh }, .block = .{ .x = 128 }, .shared = shared }, self.rig.s, &args); const tile: usize = if (self.form.mt2) 32 / group else 64 / group; const groups = (n + tile - 1) / tile; try cuda.launch.launch(self.long_rows, .{ .grid = .{ .x = @intCast(groups), .y = @intCast(z), .z = kvh }, .block = .{ .x = if (self.form.mt2) @as(u32, 128) else 256 }, .shared = shared }, self.rig.s, &rows_args); diff --git a/zig/tests/cuda/contract_q35_attn_host.zig b/zig/tests/cuda/contract_q35_attn_host.zig index 75286faf321..a107b4a07dd 100644 --- a/zig/tests/cuda/contract_q35_attn_host.zig +++ b/zig/tests/cuda/contract_q35_attn_host.zig @@ -10,7 +10,7 @@ const Call = struct { block: [3]u32, shared: u32, geo: Geo, - args: [12]u64 = @splat(0), + args: [13]u64 = @splat(0), }; const Trace = struct { var calls: std.ArrayList(Call) = .empty; @@ -60,6 +60,7 @@ const Trace = struct { call.args[7] = @as(u32, @bitCast(read(f32, a, 7))); if (long) call.args[8] = @intCast(read(c_int, a, 8)); for (8 + @as(usize, @intFromBool(long))..11 + @as(usize, @intFromBool(long))) |i| call.args[i] = read(u64, a, i); + if (id == 2) call.args[12] = @intFromBool(read(bool, a, 12)); } calls.append(T.allocator, call) catch return 2; return 0; @@ -113,6 +114,7 @@ fn partial(call: Call, form: adapter.Form, long: bool, rows: usize, first: usize if (long) try T.expectEqual(rows, call.args[8]); const at: usize = if (long) 9 else 8; try T.expectEqualSlices(u64, &.{ p[0] + first * 24 * 64 * 4, p[1] + first * 24 * 64 * 4, p[2] + first * 24 * 64 * 256 * 4 }, call.args[at..][0..3]); + if (form.cache == .bf16) try T.expectEqual(form.all_rows and rows > 1, call.args[12] != 0); } fn merge(call: Call, rows: usize, p: [3]u64, out: u64) !void { try T.expectEqual(@as(usize, 3), call.id); @@ -125,18 +127,18 @@ fn merge(call: Call, rows: usize, p: [3]u64, out: u64) !void { test "q35 contract host: actual adapter launches short and long TC paths then merge for both caches" { defer Trace.deinit(); var d = driver(); - for (adapter.forms) |form| for ([_]usize{ 1, 9, 10, 11, 16, 64, 128 }) |rows| { - // Both entries are needed even for mixed streams: length-based selection happens on the device. + for (adapter.forms) |form| for ([_]usize{ 1, 4, 8, 9, 10, 11, 16, 17, 64, 128 }) |rows| { for ([_]usize{ 700, 8191, 8192, 9000 }) |keys| { Trace.reset(); var q = try make(&d, form, rows, keys); defer q.deinit(); try q.run(); - try T.expectEqual(@as(usize, 3), Trace.calls.items.len); + const rows_only = form.all_rows and rows > 1; + try T.expectEqual(@as(usize, if (rows_only) 2 else 3), Trace.calls.items.len); const z: u32 = if (form.control) 9 else 64; - try partial(Trace.calls.items[0], form, false, rows, 0, z, q.part); - try partial(Trace.calls.items[1], form, true, rows, 0, z, q.part); - try merge(Trace.calls.items[2], rows, q.part, q.out); + if (!rows_only) try partial(Trace.calls.items[0], form, false, rows, 0, z, q.part); + try partial(Trace.calls.items[@intFromBool(!rows_only)], form, true, rows, 0, z, q.part); + try merge(Trace.calls.items[if (rows_only) 1 else 2], rows, q.part, q.out); try T.expect(!Trace.bad_attribute); try T.expectEqualSlices(usize, if (form.cache == .bf16) &.{ 1, 2 } else &.{}, Trace.attrs.items); } diff --git a/zig/tests/cuda/contract_q35_launch_args_host.zig b/zig/tests/cuda/contract_q35_launch_args_host.zig index 7976cb3af94..0c4219cc73b 100644 --- a/zig/tests/cuda/contract_q35_launch_args_host.zig +++ b/zig/tests/cuda/contract_q35_launch_args_host.zig @@ -25,7 +25,7 @@ fn word(text: []const u8, at: usize) []const u8 { return text[at..]; } -/// A parameter's kind: the geometry struct, a pointer, a count or a scale. +/// A parameter's kind: geometry struct, pointer, count, scale or Boolean mode. fn classify(param: []const u8) ?sig.Kind { const text = std.mem.trim(u8, param, " \t\r\n"); if (text.len == 0) return null; @@ -35,6 +35,7 @@ fn classify(param: []const u8) ?sig.Kind { if (std.mem.indexOfScalar(u8, text, '*') != null) return .ptr; if (std.mem.eql(u8, first, "int")) return .i32; if (std.mem.eql(u8, first, "float")) return .f32; + if (std.mem.eql(u8, first, "bool")) return .boolean; return null; } @@ -127,11 +128,12 @@ fn tupleElements(a: std.mem.Allocator, tokens: []const Tok, text: []const u8, op return out.items; } -/// The kind a launch site's argument carries: the geometry struct, a pointer, a count or a scale. +/// The kind a launch site's argument carries: geometry struct, pointer, count, scale or Boolean mode. fn argKind(text: []const u8) sig.Kind { if (std.mem.startsWith(u8, text, "@as(f32")) return .f32; if (std.mem.startsWith(u8, text, "@as(c_int")) return .i32; if (std.mem.eql(u8, text, "geo")) return .geo; + if (std.mem.eql(u8, text, "rows_only")) return .boolean; return .ptr; } diff --git a/zig/tests/cuda/contract_qwen_pages.zig b/zig/tests/cuda/contract_qwen_pages.zig index ce296ef5aad..44839b638e6 100644 --- a/zig/tests/cuda/contract_qwen_pages.zig +++ b/zig/tests/cuda/contract_qwen_pages.zig @@ -259,6 +259,7 @@ fn launch(gpu: check.Gpu, s: cuda.Stream, fns: Fns, f: Fixture, tables: ?[2][]co try cuda.launch.launch(t.partial, .{ .grid = .{ .x = @intCast(m), .y = max_chunks, .z = kv_heads }, .block = .{ .x = 128 }, .shared = t.shared }, s, &args); var rows_args: cuda.Args = .{}; inline for (.{ geo, qn, segs_d, rowseg_d, rowanc_d, layer, none, @as(f32, 1.0 / 16.0), @as(c_int, @intCast(m)), pm, pl, po }) |x| rows_args.add(x); + if (f.format == .bf16) rows_args.add(false); const groups = (m + 64 / group - 1) / (64 / group); try cuda.launch.launch(t.rows, .{ .grid = .{ .x = @intCast(groups), .y = max_chunks, .z = kv_heads }, .block = .{ .x = 256 }, .shared = t.shared }, s, &rows_args); } else try cuda.launch.launch(fns.partial, .{ .grid = .{ .x = max_chunks, .y = kv_heads, .z = @intCast(m) }, .block = .{ .x = 32 * group } }, s, &args); diff --git a/zig/tests/cuda/contract_qwen_shared_prefix.zig b/zig/tests/cuda/contract_qwen_shared_prefix.zig index de52f91cf00..e9af24c2eaf 100644 --- a/zig/tests/cuda/contract_qwen_shared_prefix.zig +++ b/zig/tests/cuda/contract_qwen_shared_prefix.zig @@ -169,6 +169,7 @@ fn rows(k: Kernel, f: cuda.Function, s: cuda.Stream, x: Staged) !void { const none: u64 = 0; var args: cuda.Args = .{}; inline for (.{ geo, x.q, x.segs, x.rowseg, x.rowanc, layer, none, scale, @as(c_int, @intCast(x.m)), x.pm, x.pl, x.po }) |v| args.add(v); + if (k.format == .bf16) args.add(false); const groups = (x.m + k.tile_rows - 1) / k.tile_rows; try cuda.launch.launch(f, .{ .grid = .{ .x = @intCast(groups), .y = max_chunks, .z = kv_heads }, .block = .{ .x = k.threads }, .shared = k.shared }, s, &args); }