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
5 changes: 2 additions & 3 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: GLM-5.3-Flash's latent cache and indexer accept full GLM-5.3's shapes (slice (a) of #3063) (#3949). Thanks to @bjornmonnens.

- Qwen3.8-27B on Metal (M1 to M4): `--mtp-drafts N` loads the checkpoint's MTP head from `mtp-4bit.safetensors` and drafts chains of up to N tokens for every stream, alone or in shared rounds, and `--mtp-confidence` stops a chain below its floor; before, the Metal build refused both flags (#2464, refs #2368). Thanks to @jschmied.

- Flash Next on CUDA: `TF_FLASHNEXT_GATHER_EARLY=1` (default `0`) gathers a shared decode pass's n-gram rows for every lane in one pooled call instead of one call a stream, for passes of at most 64 lanes (wider passes keep the per-stream call); which rows are read and how a row is dequantized do not change, so replies stay main's, and the measured mean decode gain on one GB10 rises to +2.10% at eight streams (#4009). Thanks to @BadAd84.
Expand All @@ -20,13 +22,10 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

- CUDA: Enable dense MTP drafting for Qwen3.5-family targets with compatible checkpoint tensors (#3582). Thanks to @tacosta.



- CUDA on unified-memory Linux (GB10): the start-up memory budget is at most the free memory CUDA reports, less the same reserve, instead of `/proc/meminfo` alone, and the server first drops the checkpoint's own shards from the page cache, so a checkpoint that was just read is not refused for its own cached pages; discrete cards, Windows, Metal and HIP are unchanged (#3192, part of #1857).
- Docs: the first gfx1201 receipt in the repository, Flash Next oQ2-MTP at `--tp 2` on two Radeon AI PRO R9700s against #2401's 1.2.3 numbers on the same card class, and `docs/recipes/flash-next-hip.md` as the home for Flash Next on HIP (#3608, refs #2401). Thanks to @timmyit.



## 1.3.3

- Kernel contract: the HIP relu² stream tile is checked at 5, 6 and 8 bits in groups of 64 as well as 4, with gates 1 to 4, a planted control for each wider width, and gate-5 records on the four RDNA chips (#3163, part of #3061). Thanks to @gilby.
Expand Down
15 changes: 10 additions & 5 deletions docs/kernel-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ sparse). Its prompt indexer (`tf_glm_index_scores`, `tf_glm_index_pools`) is bit
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.
Full GLM-5.3's indexer values run on the same kernels (`contract_glm_full.zig`): `tf_glm_scores` at pools of 1 with
64 roped query dims against float64 (allowing the roped queries' bf16 rounding), `tf_glm_index_scores` bit-equal to
it, `tf_glm_dsa_tokens` at pools of 1, and `tf_glm_index_write`'s LayerNormed keys roped on 64 dims, with four
controls: queries or keys left unroped, keys read as pools of 4, and token lists one position late.
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
their slots from host pages or the layer's row cache, the token lists' slots, the marks cleared, the dense rows through
Expand Down Expand Up @@ -568,11 +572,12 @@ squares in bf16.

CUDA also checks the mla image (`mla.cu`, MLA over a 512-wide latent with 64 roped columns and 128-wide heads, on
GLM's chunk bodies): the head-wise gate, the rope (plain, and with YaRN's attention factor at 900,000 positions), the
128-wide absorb_q and expand_v, and the chunk tiles (`tf_mla_chunks_r16`, 576-wide q and keys over a linear cache,
through `tf_glm_merge`; the split form with `tf_mla_merge_part`), each against float64 (eight controls, real launches
each given one planted fault: the gate reading the next head's column, the plain rope one position late, the YaRN
rope without its factor, absorb_q and expand_v reading the next head's weights, the chunks the next head's queries,
the split chunks reading a rank's page z as chunk z, and the per-rank merge one chunk short).
128-wide absorb_q and expand_v, full GLM-5.3's 192-wide absorb_q (`tf_mla_absorb_q_192`, 21 and 22 heads), and the
chunk tiles (`tf_mla_chunks_r16`, 576-wide q and keys over a linear cache, through `tf_glm_merge`; the split form with
`tf_mla_merge_part`), each against float64 (nine controls, real launches each given one planted fault: the gate
reading the next head's column, the plain rope one position late, the YaRN rope without its factor, both absorb_q
forms and expand_v reading the next head's weights, the chunks the next head's queries, the split chunks reading a
rank's page z as chunk z, and the per-rank merge one chunk short).

## CUDA records per nvcc release

Expand Down
7 changes: 7 additions & 0 deletions tools/glm_index_emulate/cuda_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ inline int __float_as_int(float f) { int i; std::memcpy(&i, &f, 4); return i; }
inline float __fadd_rn(float a, float b) { return a + b; }
inline float __fmul_rn(float a, float b) { return a * b; }
inline float __fmaf_rn(float a, float b, float c) { return std::fma(a, b, c); }
#ifdef __APPLE__
// glibc's sincosf, which Apple's libm does not declare (model and kernel both turn through it, so they stay equal)
inline void sincosf(float x, float* s, float* c) {
*s = std::sin(x);
*c = std::cos(x);
}
#endif
using std::fmaxf;
using std::max;
using std::min;
79 changes: 53 additions & 26 deletions tools/glm_index_emulate/emulate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static int fails = 0;
static uint32_t bits(float f) { uint32_t u; std::memcpy(&u, &f, 4); return u; }
static float bf(__nv_bfloat16 v) { return __bfloat162float(v); }

struct Case { int64_t pos, rows, np; int loop, heads; bool paged; };
struct Case { int64_t pos, rows, np; int loop, heads; bool paged; int pool = 4, rope = 0; };

// tf_glm_scores' result for one (row, pool), the model of the mma: a chain of double sums per k16 step, then its rules
static float model_score(const std::vector<__nv_bfloat16>& q, const std::vector<__nv_bfloat16>& w, int64_t w_stride,
Expand Down Expand Up @@ -103,34 +103,48 @@ static float model_score(const std::vector<__nv_bfloat16>& q, const std::vector<

static void scores_case(std::mt19937_64& g, const Case& c) {
const int D = 128, W = 40; // head weights in a row of W columns
const int64_t visible = (c.pos + c.rows) / 4;
const int64_t visible = (c.pos + c.rows) / c.pool, pp = 512 / c.pool; // pp: a page's pools
const int64_t nb = (std::min(c.np, std::max<int64_t>(512, visible)) + 63) / 64;
std::normal_distribution<float> nd(0.0f, 1.0f);
std::vector<__nv_bfloat16> q(c.rows * c.heads * D), w(c.rows * W);
for (auto& x : q) x = __float2bfloat16_rn(nd(g));
for (auto& x : w) x = __float2bfloat16_rn(nd(g));
const int64_t pool_rows = (c.pos + c.rows) / 4 + 1;
const int64_t pool_rows = (c.pos + c.rows) / c.pool + 1;
std::vector<__nv_bfloat16> keys(std::max<int64_t>(c.np, pool_rows + 200) * D);
for (auto& x : keys) x = __float2bfloat16_rn(nd(g) * 2);
// pages of 128 pools, shuffled, when paged: the table maps a logical page to its place
const int64_t npages = (keys.size() / D + 127) / 128, page_elems = 128 * D;
// pages of pp pools, shuffled, when paged: the table maps a logical page to its place
const int64_t npages = (keys.size() / D + pp - 1) / pp, page_elems = pp * D;
std::vector<int> pt(npages), order(npages);
for (int i = 0; i < npages; i++) order[i] = i;
std::shuffle(order.begin(), order.end(), g);
std::vector<__nv_bfloat16> store(npages * page_elems);
for (int64_t p = 0; p < (int64_t)(keys.size() / D); p++) {
pt[p / 128] = order[p / 128];
std::memcpy(&store[(int64_t)order[p / 128] * page_elems + (p % 128) * D], &keys[p * D], D * 2);
pt[p / pp] = order[p / pp];
std::memcpy(&store[(int64_t)order[p / pp] * page_elems + (p % pp) * D], &keys[p * D], D * 2);
}
const __nv_bfloat16* pk = c.paged ? store.data() : keys.data();
const float scale = 0.0883883461f, wscale = 0.176776692f;
const int pos = (int)c.pos;
// rope: the model turns q's pairs below c.rope itself (row r at pos + r), the kernel its fragments
std::vector<float> inv(64);
for (int i = 0; i < 64; i++) inv[i] = (float)std::pow(8.0e6, -2.0 * i / 64);
std::vector<__nv_bfloat16> qm = q;
for (int64_t r = 0; r < c.rows; r++)
for (int h = 0; h < c.heads; h++)
for (int i = 0; i < c.rope / 2; i++) {
__nv_bfloat16* x = &qm[(r * c.heads + h) * D + 2 * i];
const float x0 = bf(x[0]), x1 = bf(x[1]);
float sn, cs;
sincosf((float)(c.pos + r) * inv[i], &sn, &cs);
x[0] = __float2bfloat16_rn(std::fma(x0, cs, -(x1 * sn)));
x[1] = __float2bfloat16_rn(std::fma(x1, cs, x0 * sn));
}
std::vector<float> out(c.rows * c.np);
std::vector<uint32_t> raw(out.size(), 0x7FC0DEAD);
std::memcpy(out.data(), raw.data(), out.size() * 4);
run_grid((c.rows + 7) / 8, (nb + c.loop - 1) / c.loop, 256, [&] {
tf_glm_index_scores(q.data(), w.data(), W, pk, out.data(), &pos, c.rows, c.np, nb, scale, wscale, c.heads, c.loop,
c.paged ? pt.data() : nullptr, c.paged ? page_elems * 2 : 0);
c.paged ? pt.data() : nullptr, c.paged ? page_elems * 2 : 0, c.pool, c.rope, inv.data());
});
int64_t bad = 0, unread = 0;
for (int64_t r = 0; r < c.rows; r++)
Expand All @@ -140,12 +154,12 @@ static void scores_case(std::mt19937_64& g, const Case& c) {
unread += have != 0x7FC0DEAD; // never written
continue;
}
const int64_t npool = (c.pos + r + 1) / 4;
const float want = p < npool ? model_score(q, w, W, &keys[p * D], r, c.heads, scale, wscale) : -INFINITY;
const int64_t npool = (c.pos + r + 1) / c.pool;
const float want = p < npool ? model_score(qm, w, W, &keys[p * D], r, c.heads, scale, wscale) : -INFINITY;
bad += have != bits(want);
}
printf("scores pos %lld rows %lld np %lld loop %d heads %d %s: %lld differ, %lld untouched columns written\n",
(long long)c.pos, (long long)c.rows, (long long)c.np, c.loop, c.heads, c.paged ? "paged" : "plain",
printf("scores pos %lld rows %lld np %lld loop %d heads %d pool %d rope %d %s: %lld differ, %lld untouched columns written\n",
(long long)c.pos, (long long)c.rows, (long long)c.np, c.loop, c.heads, c.pool, c.rope, c.paged ? "paged" : "plain",
(long long)bad, (long long)unread);
fails += bad != 0 || unread != 0;
}
Expand Down Expand Up @@ -188,34 +202,43 @@ int main() {
std::mt19937_64 g(0x101);
for (const Case& c : {Case{0, 1, 1024, 2, 32, false}, Case{1000, 19, 1024, 4, 32, false}, Case{3000, 24, 1024, 16, 32, true},
Case{3900, 17, 1000, 3, 32, false}, Case{5000, 9, 2048, 5, 20, true},
Case{6000, 24, 2048, 8, 32, false}, Case{12000, 8, 4096, 3, 32, true}})
Case{6000, 24, 2048, 8, 32, false}, Case{12000, 8, 4096, 3, 32, true},
// full GLM-5.3's: every key scored (pools of 1, 512 a page), queries roped on 64 dims
Case{3000, 9, 4096, 4, 32, true, 1, 64}, Case{2100, 17, 2560, 3, 32, false, 1, 64},
Case{5000, 8, 8192, 16, 32, true, 1, 0}})
scores_case(g, c);

const int K = 512, rows = 2;
const int rows = 2;
int paths[3] = {0, 0, 0};
for (int np : {1024, 4097, 12289, 32769, 70001})
// GLM-5.3-Flash's 512 of pools of 4 at every bucket, then full GLM-5.3's 2,048 of single keys
struct Pick { int np, pool, K; };
std::vector<Pick> picks;
for (int np : {1024, 4097, 12289, 32769, 70001}) picks.push_back({np, 4, 512});
for (int np : {4097, 12289}) picks.push_back({np, 1, 2048});
for (const Pick& pk : picks)
for (int mode = 0; mode < 3; mode++)
for (Kind kind : {normal, ties, equal, zeros, skewed, decoy}) {
const int np = pk.np, pool = pk.pool, K = pk.K;
// the first row's position: few visible pools (limit K), about half, or nearly all
const int pos = mode == 0 ? 300 : mode == 1 ? 2 * np : 4 * np - rows - 5;
const int pos = mode == 0 ? 300 : mode == 1 ? pool * np / 2 : pool * np - rows - 5;
std::vector<float> s((size_t)rows * np + 4);
float* base = s.data() + (np % 4 ? 1 : 0); // an unaligned row now and then: the scalar loads
std::vector<int> limit(rows);
for (int r = 0; r < rows; r++) {
limit[r] = (int)std::min<int64_t>(np, std::max<int64_t>((pos + r + 1) / 4, K));
limit[r] = (int)std::min<int64_t>(np, std::max<int64_t>((pos + r + 1) / pool, K));
fill(g, base + (size_t)r * np, np, limit[r], kind);
}
std::vector<int64_t> out((size_t)rows * K, -7);
std::vector<int> path(rows, -1);
run_grid(rows, 1, glm_index::T, [&] {
tf_glm_index_pools(base, out.data(), np, &pos, K, tile_cap(np), tile_target(np), path.data());
tf_glm_index_pools(base, out.data(), np, &pos, K, tile_cap(np), tile_target(np), path.data(), pool);
});
for (int r = 0; r < rows; r++) {
paths[path[r]]++;
const auto want = reference(base + (size_t)r * np, limit[r], K);
if (!std::equal(want.begin(), want.end(), out.begin() + (size_t)r * K)) {
fails++;
printf("pools np %d mode %d kind %d row %d (path %d) differs\n", np, mode, (int)kind, r, path[r]);
printf("pools np %d pool %d mode %d kind %d row %d (path %d) differs\n", np, pool, mode, (int)kind, r, path[r]);
}
}
}
Expand All @@ -225,26 +248,30 @@ int main() {
// glm_index_decode.cuh: a decode window's rows marked on many CTAs, then merged, against the same plain top-K
int dpaths[4] = {0, 0, 0, 0};
const int drows = 3;
for (int np : {4097, 12289, 70001})
std::vector<Pick> dpicks;
for (int np : {4097, 12289, 70001}) dpicks.push_back({np, 4, 512});
dpicks.push_back({12289, 1, 2048});
for (const Pick& pk : dpicks)
for (int mode = 0; mode < 3; mode++)
for (Kind kind : {normal, ties, equal, zeros, skewed, decoy}) {
const int pos = mode == 0 ? 300 : mode == 1 ? 2 * np : 4 * np - drows - 5;
const int np = pk.np, pool = pk.pool, K = pk.K;
const int pos = mode == 0 ? 300 : mode == 1 ? pool * np / 2 : pool * np - drows - 5;
std::vector<float> s((size_t)drows * np + 4);
float* base = s.data() + (np % 4 ? 1 : 0);
std::vector<int> limit(drows);
for (int r = 0; r < drows; r++) {
limit[r] = (int)std::min<int64_t>(np, std::max<int64_t>((pos + r + 1) / 4, K));
limit[r] = (int)std::min<int64_t>(np, std::max<int64_t>((pos + r + 1) / pool, K));
fill(g, base + (size_t)r * np, np, limit[r], kind);
}
const int64_t words = (np + 31) / 32;
std::vector<uint32_t> bitw((size_t)drows * 2 * words, 0xA5A5A5A5u); // words a scan does not write stay unread
const int last = (int)std::min<int64_t>(np, std::max<int64_t>((pos + drows) / 4, K));
const int last = (int)std::min<int64_t>(np, std::max<int64_t>((pos + drows) / pool, K));
const unsigned ctas = last <= glm_dsel::CAP ? 0 : (unsigned)((last + glm_dsel::SPAN - 1) / glm_dsel::SPAN);
if (ctas > 0)
run_grid(ctas, drows, glm_dsel::T1, [&] { tf_glm_dsel_scan(base, np, &pos, K, 2048, bitw.data(), words); });
run_grid(ctas, drows, glm_dsel::T1, [&] { tf_glm_dsel_scan(base, np, &pos, K, 2048, bitw.data(), words, pool); });
std::vector<int64_t> out((size_t)drows * K, -7);
std::vector<int> path(drows, -1);
run_grid(drows, 1, glm_dsel::T2, [&] { tf_glm_dsel_merge(base, out.data(), np, &pos, K, bitw.data(), words, path.data()); });
run_grid(drows, 1, glm_dsel::T2, [&] { tf_glm_dsel_merge(base, out.data(), np, &pos, K, bitw.data(), words, path.data(), pool); });
for (int r = 0; r < drows; r++) {
if (path[r] < 0 || path[r] > 3) {
fails++;
Expand All @@ -255,7 +282,7 @@ int main() {
const auto want = reference(base + (size_t)r * np, limit[r], K);
if (!std::equal(want.begin(), want.end(), out.begin() + (size_t)r * K)) {
fails++;
printf("dsel np %d mode %d kind %d row %d (path %d) differs\n", np, mode, (int)kind, r, path[r]);
printf("dsel np %d pool %d mode %d kind %d row %d (path %d) differs\n", np, pool, mode, (int)kind, r, path[r]);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions zig/exl3_prompt_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn run(rows: usize, prompt: bool, shared: bool) !void {
Capture.reset();
var d: cuda.Driver = undefined;
d.api.cuLaunchKernel = Capture.launch;
d.api.cuLaunchKernelEx = glm.pdl.forwardEx(Capture.launch);
d.api.cuMemcpy2DAsync_v2 = Capture.copy;
const s: cuda.Stream = .{ .d = &d, .handle = null };
var k: ex.Experts = .{ .mod = undefined, .group = function(&d, 1), .rot_in = function(&d, 2), .grouped = @splat(function(&d, 3)), .gateup = function(&d, 4), .down_combine = function(&d, 5) };
Expand Down Expand Up @@ -266,6 +267,7 @@ test "exl3 prompt: GLM admission facts through shared lanes keep drafted plain a
test "exl3 prompt: each projection group launches the kernel of its own verified width, with the same arguments" {
var d: cuda.Driver = undefined;
d.api.cuLaunchKernel = Capture.launch;
d.api.cuLaunchKernelEx = glm.pdl.forwardEx(Capture.launch);
const s: cuda.Stream = .{ .d = &d, .handle = null };
var k: ex.Experts = .{ .mod = undefined, .group = function(&d, 1), .rot_in = function(&d, 2), .grouped = @splat(function(&d, 3)), .gateup = function(&d, 4), .down_combine = function(&d, 5) };
k.prompt = .{ .rotate = function(&d, 11), .gateup = function(&d, 12), .down = function(&d, 13), .gateup_k6 = function(&d, 14), .down_k6 = function(&d, 15) };
Expand Down
1 change: 1 addition & 0 deletions zig/glm_exl3_prompt_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ fn run(rows: usize, prompt: bool, shared: bool, count: usize) !void {
Record.reset();
var d: cuda.Driver = undefined;
d.api.cuLaunchKernel = Record.launch;
d.api.cuLaunchKernelEx = glm.pdl.forwardEx(Record.launch);
d.api.cuMemcpy2DAsync_v2 = Record.copy;
var k: glm.exl3.Kernels = undefined;
inline for (.{ "group", "rot_in", "gateup", "down_combine" }, .{ 9, 5, 13, 14 }) |field, id| @field(k, field) = Record.function(&d, id);
Expand Down
1 change: 1 addition & 0 deletions zig/glm_ring_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fn check(boundary: usize, resumed: bool, rewind: usize) !void {
st.capacity = boundary + 8192;
st.ring_rows = keys.len;
st.ring_row = 1;
st.pool = 4;
st.rings = &rings;
st.caches = &.{};
st.cur = &.{};
Expand Down
Loading