From ab736ca3e3ead2e3289c26b67b1c98f8b0d08a83 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Sat, 31 Oct 2026 09:51:23 +0000 Subject: [PATCH] libr4d: mla_attn_paged splits by heads alone, so a verify row matches its single-row decode The history split was sized by rows * heads and clamped by the step's longest table: at eight heads one and two rows split 16 ways and three rows 11, so the k + 1 rows of a speculative verify summed their partials in another order than the single-row decodes at the same positions, and Ling-3.0's greedy text left the run without speculation from MTP depth 2 on. The count now comes from the head count only; a scratch hook sizes the partials for up to 32 rows a launch, and a larger batch takes as many splits as its scratch holds. r4d_emu's mla test holds verify launches of 2 to 32 rows to their single-row decodes bit for bit. --- docs/OPS.md | 6 ++- libr4d/r4d_mla.hip | 27 +++++++++++-- libr4d/r4d_plugin.h | 1 + libr4d/r4d_rows.cpp | 2 +- tests/r4d_emu/test_mla.cpp | 80 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 6 deletions(-) diff --git a/docs/OPS.md b/docs/OPS.md index 2599c162..64508ecd 100644 --- a/docs/OPS.md +++ b/docs/OPS.md @@ -3330,7 +3330,11 @@ of the expansion. The softmax is f32 and `out` rounded once before the gate. libr4d's `mla_attn_paged_bf16` serves `latent_dim` 512 and `rope_dim` 64, up to 64 heads, pages of up to 256 tokens: a wave a (query row, head), its lanes holding 16 latent and 2 rotary columns each, so a score is one wave sum. A short batch splits each row's history over waves and joins the f32 -partials. +partials. The split count comes from the head count alone, never from the rows of the launch or +its longest table, so a row's bytes do not depend on its batch: a speculative verify of `k + 1` +rows must give, bit for bit, the single-row decodes at the same positions, or greedy output +differs from the run without speculation. That holds up to 32 rows a launch, the rows the scratch +hook sizes for; a larger batch takes as many splits as its scratch holds. `mla_project` moves a query into the latent and a latent result out of it, through the bf16 weight `b` `[heads*2*head_dim, latent_dim]` that stores each head's key rows, then its value rows. diff --git a/libr4d/r4d_mla.hip b/libr4d/r4d_mla.hip index 2c6da49a..75d9e20f 100644 --- a/libr4d/r4d_mla.hip +++ b/libr4d/r4d_mla.hip @@ -326,6 +326,18 @@ __global__ __launch_bounds__(128) void merge_paged(const float* scratch, const u } paged_store(out, gate, m, r, head, lane, value, den); } +/* Splits of a (row, head) job's history. A row's bytes must not depend on the batch it is in: a + * speculative verify scores the k + 1 rows of a sequence in one launch, and greedy output equals + * the run without speculation only if each row gives the single-row decode at its position bit for + * bit. So the count is a function of the head count alone, not of the launch's rows or its longest + * table. A history shorter than the count leaves splits empty; they exit at once and merge_paged + * skips them. */ +/* The rows the scratch serves at full split: a verify at depth 31. */ +constexpr long kPagedRows = 32; +int paged_splits(long heads) { return (int)std::min(16l, (256 + heads - 1) / heads); } +int64_t paged_scratch_bytes(long rows, long heads) { + return std::min(rows, kPagedRows) * heads * paged_splits(heads) * PART * 4; +} /* Operands: q [M, heads, 512], q_rope [M, heads, 64], kv [pages, 1, bs, >= 576], block_table * [seqs, blocks], seqused [seqs], cu_seqlens [seqs + 1], gate [M, >= heads] (optional), out * [M, heads, 512]. */ @@ -371,10 +383,11 @@ int paged_body(const RadArgs* a, RadStream stream) { (gate && (gate->rank != 2 || gate->shape[0] != q.shape[0] || gate->shape[1] < heads || gate->stride[1] != 1))) return RAD_E_SHAPE; - const long jobs = q.shape[0] * heads; - int splits = (int)std::min(16l, (256 + jobs - 1) / jobs); - splits = (int)std::max(1l, std::min((long)splits, (bt.shape[1] * bs + 63) / 64)); - if (!a->scratch || a->scratch_bytes < scratch_bytes) splits = 1; + /* The split comes from the head count alone (paged_splits). Only a batch whose partials the + * scratch cannot hold takes fewer, as many as fit. */ + const long rows = q.shape[0], slots = a->scratch ? a->scratch_bytes / (PART * 4) : 0; + int splits = paged_splits(heads); + if (rows * heads * splits > slots) splits = (int)std::max(1l, slots / (rows * heads)); const PagedMeta m{ q.stride[0], q.stride[1], qr.stride[0], qr.stride[1], kv.stride[0], kv.stride[2], bt.stride[0], bt.stride[1], o.stride[0], o.stride[1], gate ? (long)gate->stride[0] : 0L, @@ -1071,6 +1084,12 @@ extern "C" int r4d_rad_mla_attn_paged(const RadArgs* a, RadStream stream) { return paged_body(a, stream); } extern "C" int64_t r4d_rad_mla_attn_scratch(const RadArgs*) { return scratch_bytes; } +/* Sized at the band's top row count, at most kPagedRows (paged_splits). */ +extern "C" int64_t r4d_rad_mla_attn_paged_scratch(const RadArgs* a) { + const long heads = r4d_geti_or(a, "heads", 0), rows = r4d_geti_or(a, "M", kPagedRows); + if (heads < 1 || heads > 64 || rows < 1) return RAD_E_SHAPE; + return paged_scratch_bytes(rows, heads); +} extern "C" int64_t r4d_rad_mla_attn_matrix_scratch(const RadArgs*) { return matrix_scratch_bytes; } extern "C" void r4d_mla_matrix_pin_form(int form) { g_mla_matrix_form = form; } extern "C" void r4d_mla_rows_pin_form(int form) { g_mla_rows_form = form; } diff --git a/libr4d/r4d_plugin.h b/libr4d/r4d_plugin.h index d64a748a..116258dc 100644 --- a/libr4d/r4d_plugin.h +++ b/libr4d/r4d_plugin.h @@ -832,6 +832,7 @@ int r4d_rad_mla_attn(const RadArgs*, RadStream); int r4d_rad_mla_attn_paged(const RadArgs*, RadStream); int r4d_shape_mla_attn_paged(const RadParam*, int, int, RadOpdDesc*); int64_t r4d_rad_mla_attn_scratch(const RadArgs*); +int64_t r4d_rad_mla_attn_paged_scratch(const RadArgs*); int r4d_rad_mla_attn_matrix(const RadArgs*, RadStream); int64_t r4d_rad_mla_attn_matrix_scratch(const RadArgs*); int r4d_rad_mla_attn_rows(const RadArgs*, RadStream); diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index 7125a9c7..737b906a 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -6314,7 +6314,7 @@ static const RadKernelInfo kKernels[] = { "latent_dim=512, rope_dim=64, heads<=64, block_size<=256; one page table a sequence", "bf16 Q/rotary/cache/gate/output, f32 softmax and partials", RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, ROW(cMlaAttnPaged), NOTUNE, - nullptr, nullptr, r4d_rad_mla_attn_paged, r4d_rad_mla_attn_scratch, nullptr, nullptr, + nullptr, nullptr, r4d_rad_mla_attn_paged, r4d_rad_mla_attn_paged_scratch, nullptr, nullptr, r4d_shape_mla_attn_paged }, { "mla_project_bf16", "mla_project", "attn", "per-head absorbed MLA projections", "head_dim 128 or 256, latent_dim=512; pitched tensors", "bf16 input, weights and output; fp32 arithmetic", diff --git a/tests/r4d_emu/test_mla.cpp b/tests/r4d_emu/test_mla.cpp index 1b660578..d0e74d43 100644 --- a/tests/r4d_emu/test_mla.cpp +++ b/tests/r4d_emu/test_mla.cpp @@ -714,6 +714,79 @@ int periodic_report() { return ok ? 0 : 1; } +/* mla_attn_paged (Ling-3.0's MLA) under speculation: the k + 1 rows of a verify, launched + * together, against k + 1 single-row decodes at the same positions, each with the table its own + * step carries and scratch as the hook sizes it. Greedy output equals the run without + * speculation only if they agree bit for bit; a split taken from the launch's rows (16, 16 and + * 11 ways at eight heads for one, two and three rows) or from its longest table does not. */ +int run_paged_verify(int heads, int context, std::mt19937& rng) { + constexpr int PB = 16, RD = 64, W = D + RD, kMaxRows = 32; + const int pages = (context + kMaxRows + PB - 1) / PB; + std::normal_distribution nrm(0.f, 1.f); + std::vector kvb((size_t)pages * PB * W), qb((size_t)kMaxRows * heads * D), + rb((size_t)kMaxRows * heads * RD), gb((size_t)kMaxRows * heads); + for (auto& v : kvb) v = rad_f32_to_bf16(nrm(rng)); + for (auto& v : qb) v = rad_f32_to_bf16(0.25f * nrm(rng)); + for (auto& v : rb) v = rad_f32_to_bf16(0.25f * nrm(rng)); + for (auto& v : gb) v = rad_f32_to_bf16(nrm(rng)); + std::vector page(pages); + for (int i = 0; i < pages; ++i) page[i] = i; + std::shuffle(page.begin(), page.end(), rng); + /* Rows [row0, row0 + rows) of one sequence whose context after the step is `used`. */ + auto launch = [&](int row0, int rows, int used, std::vector& out) { + const int blocks = (used + PB - 1) / PB; + std::vector bt(page.begin(), page.begin() + blocks), su = { used }, cu = { 0, rows }; + RadTensor t[8] = { tens(qb.data() + (size_t)row0 * heads * D, RAD_BF16, { rows, heads, D }), + tens(rb.data() + (size_t)row0 * heads * RD, RAD_BF16, + { rows, heads, RD }), + tens(kvb.data(), RAD_BF16, { pages, 1, PB, W }), + tens(bt.data(), RAD_I32, { 1, blocks }), + tens(su.data(), RAD_I32, { 1 }), + tens(cu.data(), RAD_I32, { 2 }), + tens(gb.data() + (size_t)row0 * heads, RAD_BF16, { rows, heads }), + tens(out.data() + (size_t)row0 * heads * D, RAD_BF16, + { rows, heads, D }) }; + RadParam p[6]{}; + const char* keys[5] = { "M", "heads", "latent_dim", "rope_dim", "block_size" }; + const long long vals[5] = { rows, heads, D, RD, PB }; + for (int i = 0; i < 5; ++i) { + p[i].key = keys[i]; + p[i].kind = RAD_P_INT; + p[i].ival = vals[i]; + } + p[5].key = "scale"; + p[5].kind = RAD_P_F64; + p[5].dval = 1.0 / std::sqrt(192.0); + RadArgs a{}; + a.t = t; + a.n_t = 8; + a.p = p; + a.n_p = 6; + const int64_t need = r4d_rad_mla_attn_paged_scratch(&a); + if (need < 0) return (int)need; + std::vector scratch((size_t)need + 16, 0); + a.scratch = scratch.data() + ((16 - ((uintptr_t)scratch.data() & 15)) & 15); + a.scratch_bytes = need; + return r4d_rad_mla_attn_paged(&a, nullptr); + }; + int fails = 0; + for (int rows : { 2, 3, 4, 5, 8, kMaxRows }) { + std::vector verify((size_t)rows * heads * D), single(verify.size()); + int rc = launch(0, rows, context + rows, verify); + for (int i = 0; i < rows && rc == RAD_OK; ++i) rc = launch(i, 1, context + i + 1, single); + int same = 0; + for (int i = 0; i < rows; ++i) + same += !std::memcmp(&verify[(size_t)i * heads * D], &single[(size_t)i * heads * D], + (size_t)heads * D * 2); + const bool ok = rc == RAD_OK && same == rows; + std::printf("paged verify: %2d heads, context %4d, %2d rows: %2d/%2d rows equal the " + "single-row decode bit for bit %s\n", + heads, context, rows, same, rows, ok ? "PASS" : "FAIL"); + fails += !ok; + } + return fails; +} + } // namespace int main(int argc, char** argv) { @@ -783,6 +856,13 @@ int main(int argc, char** argv) { std::mt19937 rng(20261021u + (unsigned)i); fails += run_split(splits[i].name, splits[i].world, splits[i].fresh, rng); } + /* Eight heads is a TP4 rank of Ling-3.0, 32 one card; a context of 40 is under the old + * clamp by table length, 700 past it. */ + if (!filter || std::strstr("paged verify", filter)) { + std::mt19937 rng(20261031u); + for (int heads : { 8, 32 }) + for (int context : { 40, 700 }) fails += run_paged_verify(heads, context, rng); + } if (!filter) fails += periodic_report(); std::printf("%d failure(s)\n", fails); return fails ? 1 : 0;