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.
This commit is contained in:
keptn42 authored and StillDeadcode committed 2026-11-03 19:07:34 +01:00
1 parent 303060f210
commit ab736ca3e3
5 files changed
+110 -6

No files matched your search

+23 -4
View File
@@ -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; }
+1
View File
@@ -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);
+1 -1
View File
@@ -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",