Skip to content
Merged
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- GLM-5.3-Flash on CUDA: drafted decode after a 1,040,384-token prompt takes its DSA index scores from `tf_glm_index_scores`, 13.8% faster at TP=4 (172.39 against 151.51 tok/s median) with byte-identical scores and the same tokens; shorter windows keep `tf_glm_scores` (#3689). Thanks to @Bizuayeu.

- GLM-5.3-Flash on CUDA: `TF_GLM_DRAFT_SPLIT=1` splits the DFlash2 drafter's MLP across three ranks or more where each share is 256-aligned, without `TF_ROCE=1`; the ranks must set it alike, two ranks split as before, and unset changes nothing. On three GB10s whose gathers stay on NCCL, decode with `--drafter` is about 5% faster at one stream (four streams within their start-to-start spread), with the same replies and the same tokens a round on the measured prompts. As with `TF_ROCE=1`, the split drafter adds its down projection's rank shares in another order, so its proposals can differ at near-ties (#3616, for #3581).



- Flash Next on HIP: on an integrated GPU that shares the host's pages (no firmware carve-out), the default kept-prompt cache takes no more than the memory the streams leave, at most 16 GiB and none when nothing is left; an explicit `--prompt-cache-gib` still sets it (#3645).

- Tests: the Web loopback startup test sets its fixture ready before probing, so its health, model-list and relay checks no longer depend on warm-up timing (#3753, fixes #3752). Thanks to @AIFlowML.
Expand Down
1 change: 1 addition & 0 deletions docs/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Diagnostics quote and escape the supplied text so it cannot create another termi
| `TF_GLM_PROMPT_STEPS` | Exactly `0` or `1`; default on (GLM prompt passes in chunks); read on rank 0 |
| `TF_GLM_SHARED` | Exactly `0` or `1`; default on (GLM windows share a forward); read on rank 0 |
| `TF_GLM_DSEL` | Exactly `0` or `1`; default on (a GLM decode window's DSA select runs on many CTAs; 0 keeps one CTA a row) |
| `TF_GLM_DSCORES` | Exactly `0` or `1`; default on (a GLM decode window's DSA index scores run on glm_index_scores; 0 keeps tf_glm_scores) |
| `TF_GLM_DFLASH2_LOG` | Whole unsigned 64-bit count of DFlash2 rounds whose drafts are logged; default 0 |
| `TF_GLM_DRAFT_CONFIDENCE` | Finite number 0 through 1 where a DFlash2 chain's chance product stops it; default 0.7 on CUDA, 0.5 on Metal |
| `TF_GLM_CONTEXT_CEILING` | GLM-5.3-Flash on CUDA: whole token count 1 through 2147483616, the window `--context` may ask for and `--context 0` serves, in place of config.json's 1048576; read at GLM's open. Past that length the model's quality is the operator's to check. Unset keeps config.json's window |
Expand Down
5 changes: 3 additions & 2 deletions docs/kernel-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,9 @@ CUDA also checks GLM-5.3-Flash's KV page kernels on a page table out of order: t
(`tf_glm_lat_write`, `tf_glm_pool_keys`), the index scores and the selected tokens' rows (`tf_glm_scores`,
`tf_glm_dsa_tokens`), and the MLA chunks through their merge (`tf_glm_chunks_16` and `tf_glm_chunks_32`, dense and
sparse). Its prompt indexer (`tf_glm_index_scores`, `tf_glm_index_pools`) is bit-equal to `tf_glm_scores` and
`tf_glm_select_rows`, and its decode select at a 1,024-thread block a row (`tf_glm_select_rows_wide`) and in spans
over many blocks (`tf_glm_dsel_scan`, then `tf_glm_dsel_merge` a block a row) to `tf_glm_select_rows`; both stated
`tf_glm_select_rows`, its decode windows' scores (`tf_glm_index_scores` at the decode tiling) to `tf_glm_scores`,
and its decode select at a 1,024-thread block a row (`tf_glm_select_rows_wide`) and in spans over many blocks
(`tf_glm_dsel_scan`, then `tf_glm_dsel_merge` a block a row) to `tf_glm_select_rows`; both stated
rules rank any NaN above +inf, as the GPU's `s + 0` makes every NaN one positive NaN.
CUDA also checks `TF_GLM_KV_HOST`'s gather (`tf_glm_kvh_*` in `glm_ops.cu`, launched by `cuda_kv_host.zig`): the
marks (a slot for each distinct pick; past its slots, the call flagged and no row left marked), the picked rows into
Expand Down
1 change: 1 addition & 0 deletions zig/src/families/glm/cuda_env_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const switches = [_]Name{
.{ .name = "TF_GLM_PROMPT_STEPS", .default = true },
.{ .name = "TF_GLM_SHARED", .default = true },
.{ .name = "TF_GLM_DSEL", .default = true },
.{ .name = "TF_GLM_DSCORES", .default = true },
};
const counts = [_][:0]const u8{"TF_GLM_DFLASH2_LOG"};
const reals = [_][:0]const u8{"TF_GLM_DRAFT_CONFIDENCE"};
Expand Down
12 changes: 11 additions & 1 deletion zig/src/families/glm/cuda_forward.zig
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub const Modules = struct {
mm: mm_mod.Kernels, // glm_mm: the bf16 linears on tiled weights
index: ?index_mod.Kernels, // TF_GLM_INDEX_CUDA: a prompt chunk's indexer scores and pools (glm_index)
dsel: bool, // TF_GLM_DSEL (default on): a decode window's DSA select on many CTAs (cuda_sparse.decodeSelect)
dscore: ?index_mod.Kernels, // TF_GLM_DSCORES (default on): a decode window's index scores on glm_index_scores
tiled: bool = false, // eligible bf16 linears are tiled; KDA inputs with uneven strides retain their row-major path
packs: ?*const mm_mod.Packed = null, // the linears packed to 4, 6 or 8 bits (TF_GLM_DENSE_BITS, _HEAD_BITS)
copy_rows: cuda.Function,
Expand Down Expand Up @@ -91,6 +92,14 @@ pub const Modules = struct {
};
m.dsel = try @import("sys").environment.read(bool, "TF_GLM_DSEL", true);
if (!m.dsel) std.log.warn("TF_GLM_DSEL=0: a decode window's DSA select keeps one 1,024-thread CTA a row", .{});
m.dscore = null;
errdefer if (m.dscore) |*x| x.unload();
if (try @import("sys").environment.read(bool, "TF_GLM_DSCORES", true)) {
m.dscore = index_mod.Kernels.load(d, k.glm_index, .scores) catch |err| blk: {
std.log.warn("TF_GLM_DSCORES: glm_index is not loaded ({t}); decode windows keep tf_glm_scores", .{err});
break :blk null;
};
} else std.log.warn("TF_GLM_DSCORES=0: a decode window's index scores keep tf_glm_scores", .{});
m.tiled = false;
m.packs = null; // `m` starts undefined: the field default does not apply
return m;
Expand All @@ -107,6 +116,7 @@ pub const Modules = struct {

pub fn deinit(m: *Modules) void {
if (m.index) |*x| x.unload();
if (m.dscore) |*x| x.unload();
m.exl3.unload();
for (&m.mods) |*x| x.unload();
m.lane.unload();
Expand Down Expand Up @@ -165,7 +175,7 @@ pub const Forward = struct {
}

pub fn set(f: *const Forward) glue.Set {
return .{ .glue = &f.m.glue, .index = if (f.m.index) |*x| x else null, .mm = if (f.m.tiled) &f.m.mm else null, .packs = f.m.packs, .prompt = f.ranks[0].b.prefill, .linear = .{ .lane = &f.m.lane }, .copy_rows = f.m.copy_rows, .dsel = f.m.dsel };
return .{ .glue = &f.m.glue, .index = if (f.m.index) |*x| x else null, .mm = if (f.m.tiled) &f.m.mm else null, .packs = f.m.packs, .prompt = f.ranks[0].b.prefill, .linear = .{ .lane = &f.m.lane }, .copy_rows = f.m.copy_rows, .dsel = f.m.dsel, .dscore = if (f.m.dscore) |*x| x else null };
}

/// The layer sums' boundary (cuda_boundary.zig): every rank's fp32 partial into every gathered buffer.
Expand Down
2 changes: 2 additions & 0 deletions zig/src/families/glm/cuda_glue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub const Set = struct {
copy_rows: ?cuda.Function = null,
/// A decode window's DSA select on many CTAs (cuda_sparse.decodeSelect; TF_GLM_DSEL, default on).
dsel: bool = false,
/// A decode window's index scores on glm_index_scores (TF_GLM_DSCORES; null keeps tf_glm_scores).
dscore: ?*const index_mod.Kernels = null,
};

/// A hidden-size norm's weight: bf16, or float32 (FP4-Local's) read as float32 by the _f32w kernels.
Expand Down
48 changes: 46 additions & 2 deletions zig/src/families/glm/cuda_index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ pub const Mode = enum {
}
};

/// A decode window's 64-pool tiles a CTA by its rows and blocks, fit to a sweep on GB10 (speed only).
pub fn decodeLoop(rows: usize, nb: usize) usize {
const want: usize = if (rows <= 4) 1 else if (rows <= 8) 2 else rows / 4 * ((nb + 1023) / 1024);
return @max(1, @min(@min(score_tiles, want), @max(1, nb / 32)));
}

/// glm_index_scores' grid: rows in CTAs of 8, the `nb` pool blocks in runs of score_tiles.
pub fn scoresGrid(rows: usize, nb: usize) [2]u32 {
return .{ @intCast(cdiv(rows, score_rows)), @intCast(@max(1, cdiv(nb, score_tiles))) };
Expand Down Expand Up @@ -77,11 +83,23 @@ pub const Kernels = struct {
return rows >= rows_from and np >= topk and pk % 16 == 0 and ps % 16 == 0;
}

/// Whether a decode window takes the scores kernel: under rows_from rows, 8 from 64 blocks (4 from 2,048), aligned.
pub fn takesDecode(k: *const Kernels, rows: usize, np: usize, nb: usize, pk: u64, ps: u64) bool {
_ = k;
const wide = (rows >= 8 and nb >= 64) or (rows >= 4 and nb >= 2048);
return wide and rows < rows_from and np >= topk and pk % 16 == 0 and ps % 16 == 0;
}

/// tf_glm_scores' launch for `rows` rows at `at` into out [rows][np]; the first `nb` 64-pool blocks are written.
pub fn scores(k: *const Kernels, s: cuda.Stream, qi: u64, wts: u64, w_stride: usize, pk: u64, out: u64, at: u64, rows: usize, np: usize, nb: usize, scale: f32, wscale: f32, heads: usize, pt: u64, ps: u64) !void {
const g = scoresGrid(rows, nb);
return k.scoresTiles(s, qi, wts, w_stride, pk, out, at, rows, np, nb, scale, wscale, heads, pt, ps, score_tiles);
}

/// As scores, `loop` 64-pool tiles a CTA (speed only: every tiling writes the same scores).
pub fn scoresTiles(k: *const Kernels, s: cuda.Stream, qi: u64, wts: u64, w_stride: usize, pk: u64, out: u64, at: u64, rows: usize, np: usize, nb: usize, scale: f32, wscale: f32, heads: usize, pt: u64, ps: u64, loop: usize) !void {
const g: [2]u32 = .{ @intCast(cdiv(rows, score_rows)), @intCast(@max(1, cdiv(nb, loop))) };
var a: cuda.Args = .{};
inline for (.{ qi, wts, @as(i64, @intCast(w_stride)), pk, out, at, @as(i64, @intCast(rows)), @as(i64, @intCast(np)), @as(i64, @intCast(nb)), scale, wscale, @as(i64, @intCast(heads)), @as(c_int, score_tiles), pt, @as(i64, @intCast(ps)) }) |v| a.add(v);
inline for (.{ qi, wts, @as(i64, @intCast(w_stride)), pk, out, at, @as(i64, @intCast(rows)), @as(i64, @intCast(np)), @as(i64, @intCast(nb)), scale, wscale, @as(i64, @intCast(heads)), @as(c_int, @intCast(loop)), pt, @as(i64, @intCast(ps)) }) |v| a.add(v);
try cuda_pdl.launch(k.scores_fn, .{ .grid = .{ .x = g[0], .y = g[1] }, .block = .{ .x = threads } }, s, &a);
}

Expand Down Expand Up @@ -125,6 +143,32 @@ test "GLM index: the candidate cap follows the pool count and stays in shared me
try std.testing.expect(tile(1 << 30).cap <= cap_max);
}

test "GLM index: a decode window's tiles a CTA follow its rows and blocks" {
try std.testing.expectEqual(@as(usize, 1), decodeLoop(4, 4064)); // 4 rows at 1M: a tile a CTA
try std.testing.expectEqual(@as(usize, 2), decodeLoop(8, 4064));
try std.testing.expectEqual(@as(usize, 16), decodeLoop(16, 4064)); // 1M: 1,024 blocks round up
try std.testing.expectEqual(@as(usize, 8), decodeLoop(16, 2048)); // 512K
try std.testing.expectEqual(@as(usize, score_tiles), decodeLoop(32, 4064));
try std.testing.expectEqual(@as(usize, 4), decodeLoop(16, 1024)); // 256K
try std.testing.expectEqual(@as(usize, 2), decodeLoop(32, 64)); // 16K: nb / 32 keeps the CTAs
try std.testing.expectEqual(@as(usize, 1), decodeLoop(8, 8));
try std.testing.expectEqual(@as(usize, score_tiles), decodeLoop(48, 1 << 20));
}

test "GLM index: decode windows on aligned key caches take the scores kernel" {
const k: Kernels = undefined;
try std.testing.expect(k.takesDecode(8, 4096, 64, 0x1000, 32768)); // 16K: the contract's 8 rows over 4,096 pools
try std.testing.expect(!k.takesDecode(8, 4096, 63, 0x1000, 32768)); // under 64: tf_glm_scores, no slower on GB10
try std.testing.expect(!k.takesDecode(rows_from - 1, 4096, 63, 0x1000, 32768));
try std.testing.expect(k.takesDecode(rows_from - 1, 216080, 3376, 0x1000, 32768));
try std.testing.expect(k.takesDecode(4, 216080, 2048, 0x1000, 32768)); // 4 rows from ~512K
try std.testing.expect(!k.takesDecode(4, 65536, 800, 0x1000, 32768));
try std.testing.expect(!k.takesDecode(1, 216080, 3376, 0x1000, 32768)); // one row: tf_glm_scores
try std.testing.expect(!k.takesDecode(rows_from, 4096, 64, 0x1000, 32768)); // prompt-sized: Kernels.takes
try std.testing.expect(!k.takesDecode(16, 4096, 64, 0x1008, 32768));
try std.testing.expect(!k.takesDecode(16, topk - 1, 64, 0x1000, 32768));
}

test "GLM index: only prompt-sized launches on aligned key caches take the kernels" {
const k: Kernels = undefined;
try std.testing.expect(k.takes(rows_from, 1024, 0x1000, 32768));
Expand Down
5 changes: 4 additions & 1 deletion zig/src/families/glm/cuda_sparse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const cuda = @import("cuda");
const cuda_pdl = @import("cuda_pdl.zig");
const glue = @import("cuda_glue.zig");
const latent = @import("cuda_latent.zig");
const index_mod = @import("cuda_index.zig");

/// sparse.POOL, TOPK_POOLS, SELECT_LIST, SCORE_LOOP_FROM, SCORE_LOOP; geometry.MLA_SELECT_ROWS.
pub const pool = 4;
Expand Down Expand Up @@ -153,8 +154,10 @@ pub fn selectTokensProfile(set: glue.Set, ops: cuda.Module, stream: cuda.Stream,
inline for (.{ qi + a * heads * d * 2, wts + a * w_stride * 2, @as(i64, @intCast(w_stride)), pk, s.scores, at, @as(i64, @intCast(np_max)), @as(i64, @intCast(nb)), scale, headScale(heads), @as(i64, @intCast(heads)), @as(c_int, @intCast(loop)), t.pt, @as(i64, @intCast(t.ps)) }) |v| sa.add(v);
if (d != 128 or heads > 32) return error.IndexShape;
const quick = if (set.index) |k| if (k.takes(rows, np_max, pk, t.ps)) k else null else null;
// a decode window's rows: the same scores from glm_index_scores (8 rows a CTA, key tiles staged once)
const dec = if (set.dscore) |k| if (k.takesDecode(rows, np_max, nb, pk, t.ps)) k else null else null;
var stamp = prof.span(profile, stream, .index_score);
if (quick) |k| try k.scores(stream, qi + a * heads * d * 2, wts + a * w_stride * 2, w_stride, pk, s.scores, at, n, np_max, nb, scale, headScale(heads), heads, t.pt, t.ps) else try cuda_pdl.launch(set.glue.scores, .{ .grid = .{ .x = @intCast(n), .y = @intCast(cdiv(nb, loop)) }, .block = .{ .x = 128 } }, stream, &sa);
if (quick) |k| try k.scores(stream, qi + a * heads * d * 2, wts + a * w_stride * 2, w_stride, pk, s.scores, at, n, np_max, nb, scale, headScale(heads), heads, t.pt, t.ps) else if (dec) |k| try k.scoresTiles(stream, qi + a * heads * d * 2, wts + a * w_stride * 2, w_stride, pk, s.scores, at, n, np_max, nb, scale, headScale(heads), heads, t.pt, t.ps, index_mod.decodeLoop(n, nb)) else try cuda_pdl.launch(set.glue.scores, .{ .grid = .{ .x = @intCast(n), .y = @intCast(cdiv(nb, loop)) }, .block = .{ .x = 128 } }, stream, &sa);
stamp.end();
stamp = prof.span(profile, stream, .index_select);
if (quick) |k| if (k.select) {
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 @@ -138,6 +138,7 @@ pub const rules = [_]Rule{
.{ .name = "TF_GLM_PROMPT_STEPS", .scope = .glm, .kind = .flag, .forms = "0 or 1" },
.{ .name = "TF_GLM_SHARED", .scope = .glm, .kind = .flag, .forms = "0 or 1" },
.{ .name = "TF_GLM_DSEL", .scope = .glm, .kind = .flag, .forms = "0 or 1" },
.{ .name = "TF_GLM_DSCORES", .scope = .glm, .kind = .flag, .forms = "0 or 1" },
.{ .name = "TF_GLM_DFLASH2_LOG", .scope = .glm, .kind = .u64, .forms = "a whole unsigned 64-bit round count (0 logs no drafts)" },
.{ .name = "TF_GLM_DRAFT_CONFIDENCE", .scope = .glm, .kind = .real, .max = 1, .forms = "a finite chain-chance cut from 0 to 1 (default 0.7 on CUDA, 0.5 on Metal)" },
// 2^31 - 32: GLM's slots, the window and its 32 verify rows (glm/cuda_native.zig's capacity), keep i32 positions
Expand Down
23 changes: 18 additions & 5 deletions zig/tests/cuda/contract_glm_index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ fn scoreDepth(_: c.Shape) usize {

const nan_relu = "relu is fmaxf(x, 0), as Triton's tl.maximum: a NaN dot scores 0 where float64 keeps the NaN";

/// A decode window's served shapes: decodeLoop's 2, 4 and 1 tiles a CTA, as cuda_sparse launches Set.dscore.
const decode_served = [_]c.Shape{ .{ .rows = 8, .n = 4096, .k = dk }, .{ .rows = 16, .n = 51200, .k = dk }, .{ .rows = 4, .n = 131072, .k = dk } };

/// scores: rows x pool columns (k: an index key's width); tokens: rows x K pools a row.
pub fn spec(comptime which: enum { scores, tokens }) c.Spec {
return switch (which) {
Expand All @@ -48,6 +51,8 @@ pub const Scores = struct {
fast: ?cuda.Function = null,
base: u64 = 0,
wrong_table: bool = false,
decode: bool = false, // a decode window's tiles a CTA (glm.index.decodeLoop), as cuda_sparse launches Set.dscore
short: bool = false, // the control: the decode tiling is told it has one 64-pool tile fewer
shape: c.Shape = undefined,
q: []u16 = &.{},
w: []u16 = &.{},
Expand Down Expand Up @@ -83,8 +88,13 @@ pub const Scores = struct {
declared.name = "cuda glm tf_glm_index_scores on pages out of order";
declared.replaces = "tf_glm_scores";
}
if (self.decode) {
declared.name = "cuda glm decode scores tf_glm_index_scores on pages out of order";
declared.served = &decode_served;
}
if (self.short) declared.name = "control: GLM decode scores write one tile short";
if (self.wrong_table) declared.name = "control: GLM paged scores use a swapped table";
return .{ .spec = declared, .ctx = self, .vt = &vt, .control = if (self.wrong_table) .shapes else null };
return .{ .spec = declared, .ctx = self, .vt = &vt, .control = if (self.wrong_table or self.short) .shapes else null };
}

const vt = g.hooks(Scores);
Expand All @@ -102,7 +112,8 @@ pub const Scores = struct {
const a = self.rig.gpu.gpa;
var prng = std.Random.DefaultPrng.init(seed);
const rng = prng.random();
self.at = if (shape.rows <= 8) 4100 else 2000;
// decode: up to 8,192 visible pools (64 pages)
self.at = if (self.decode) @as(usize, @min(shape.n, 8192)) * sparse.pool - shape.rows - 4 else if (shape.rows <= 8) 4100 else 2000;
self.scale = if (fill == .stress) 0.125 else @floatCast(std.math.pow(f64, dk, -0.5));
self.wscale = if (fill == .stress) 0.25 else sparse.headScale(heads);
self.seen = self.visible(shape.rows - 1);
Expand Down Expand Up @@ -146,12 +157,14 @@ pub const Scores = struct {

fn launch(self: *Scores, f: cuda.Function, fast: bool, out: u64) !void {
const rows = self.shape.rows;
const nb = self.shape.n / 64;
const loop: usize = if (fast) glm.index.score_tiles else if (rows >= sparse.score_loop_from) sparse.score_loop else 1;
const nb = if (fast and self.short) self.shape.n / 64 - 1 else self.shape.n / 64;
const loop: usize = if (fast and self.decode) glm.index.decodeLoop(rows, self.shape.n / 64) else if (fast) glm.index.score_tiles else if (rows >= sparse.score_loop_from) sparse.score_loop else 1;
var a: cuda.Args = .{};
if (fast) {
inline for (.{ self.in[0], self.in[1], @as(i64, w_stride), self.in[2], out, self.pos, @as(i64, @intCast(rows)), @as(i64, @intCast(self.shape.n)), @as(i64, @intCast(nb)), self.scale, self.wscale, @as(i64, heads), @as(c_int, @intCast(loop)), self.pt, @as(i64, pages.page_bytes) }) |v| a.add(v);
const g2 = glm.index.scoresGrid(rows, nb);
var g2 = glm.index.scoresGrid(rows, nb);
// cuda_sparse's decode grid: nb tiles in runs of loop
if (self.decode) g2[1] = @intCast(@max(1, (nb + loop - 1) / loop));
try cuda.launch.launch(f, .{ .grid = .{ .x = g2[0], .y = g2[1] }, .block = .{ .x = glm.index.threads } }, self.rig.s, &a);
} else {
inline for (.{ self.in[0], self.in[1], @as(i64, w_stride), self.in[2], out, self.pos, @as(i64, @intCast(self.shape.n)), @as(i64, @intCast(nb)), self.scale, self.wscale, @as(i64, heads), @as(c_int, @intCast(loop)), self.pt, @as(i64, pages.page_bytes) }) |v| a.add(v);
Expand Down
Loading
Loading