libr4d: mla_attn_rows_bf16 -- one key load for all 64 heads of a query row

A third mla_attn row, at 64 heads. A workgroup takes one query row and all its heads, four waves
to each group of sixteen, and loads each 16-token key chunk once for all of them, where the
matrix row loads it once a tile of sixteen. The scores are K Q^T, tokens as the tile's rows and
heads as its columns, so a head's running maximum and denominator stay in its lane; the waves of
a group each take a quarter of the latent's dot products and add the four partials in one order,
and the probabilities reach the value product through a permlanex16 swap instead of LDS. The
next chunk's keys are fetched into registers while the current one is multiplied, two barriers a
chunk. A row's bytes do not depend on the other rows of the batch. Under 32 rows the launcher
runs the matrix row's kernel; r4d_mla_rows_pin_form, a test hook, pins the element-wise key load,
32 heads a workgroup, the rows kernel at any row count, or the matrix row's kernel at every row
count (the form a KL measurement needs to tell this kernel's share of a row split's difference).

Measured on gfx1201 (r4d_selftest --perfmla, one sequence past the DSA budget at 16k of
context, 513 blocks): 512 rows x 64 heads take 3.9 ms, 35 TFLOP/s, against 12.3 ms on the
matrix row, and the 4096 rows x 8 heads they stand for in a TP8 prefill chunk 14.0 ms on the
paired union tile. Against the wave row the output stays within the matrix row's tolerance
(max abs 2.4e-4 at 513 blocks).

r4d_selftest checks it against libref at 1 to 33 rows, 32 and 64 heads, every fill and both key
loads, and times it against the matrix row (--case mla_attn_rows_bf16, --perfmla at 64 heads).
r4d_emu_mla runs it on the host against an f64 softmax, its forms bit for bit against each
other, a run of rows against the same rows of the whole batch, and a mixed TP step (decodes, a
verify window, a prefill past the budget, a fresh chunk) split by heads over the matrix row and
by rows over this one, at runs that do and do not divide the step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
NunzeandClaude Opus 5.5 committed 2026-10-22 09:58:07 +00:00
1 parent 123c439684
commit 87701cb6b8
8 files changed
+645 -66

No files matched your search

+45 -13
View File
@@ -144,6 +144,7 @@ struct Plugin {
void (*pin_qsel)(int) = nullptr;
void (*pin_mla)(int) = nullptr;
void (*mla_stats)(unsigned long long*) = nullptr;
void (*pin_mla_rows)(int) = nullptr;
int (*record)(const void**, int) = nullptr;
};
@@ -181,6 +182,7 @@ static bool load(Plugin* pl, const char* path) {
okall &= sym(pl->h, "r4d_qsa_select_pin_form", &pl->pin_qsel);
okall &= sym(pl->h, "r4d_mla_matrix_pin_form", &pl->pin_mla);
okall &= sym(pl->h, "r4d_mla_matrix_stats", &pl->mla_stats);
okall &= sym(pl->h, "r4d_mla_rows_pin_form", &pl->pin_mla_rows);
okall &= sym(pl->h, "r4d_record_dispatches", &pl->record);
return okall;
}
@@ -14008,25 +14010,28 @@ static void mla_check(const Plugin& pl, int64_t rows, int64_t heads, int64_t blo
const int rc = oracle->launch(&args, nullptr);
ok("MLA host oracle", rc == RAD_OK);
args.t = device.data();
/* The head-tiled row serves tiles of 16 heads or of 8 heads from two requests (cMlaMatrix);
* other head counts must be refused rather than computed. */
if (std::strstr(name, "matrix") && heads % 8) {
/* The head-tiled row serves tiles of 16 heads or of 8 heads from two requests (cMlaMatrix),
* the rows row groups of 32 or 64 heads; other head counts must be refused rather than
* computed. */
const bool by_rows = std::strstr(name, "rows") != nullptr;
auto* const pin = by_rows ? pl.pin_mla_rows : pl.pin_mla;
if ((std::strstr(name, "matrix") && heads % 8) || (by_rows && heads % 32)) {
args.scratch = nullptr;
args.scratch_bytes = 0;
const int grc = row->launch(&args, nullptr);
st_stream_sync(nullptr);
ok(fmt("MLA %s refuses heads=%lld (not a multiple of 8) with RAD_E_UNSUPPORTED, no launch",
name, (long long)heads)
ok(fmt("MLA %s refuses heads=%lld (not a multiple of %d) with RAD_E_UNSUPPORTED, no launch",
name, (long long)heads, by_rows ? 32 : 8)
.c_str(),
grc == RAD_E_UNSUPPORTED);
dlclose(ref);
return;
}
if (form && pl.pin_mla) pl.pin_mla(form);
if (form && pin) pin(form);
const int64_t need = row->scratch ? row->scratch(&args) : 0;
void* scratch = need > 0 ? dalloc((size_t)need + 256) : nullptr;
if (need <= 0 || !scratch) {
if (form && pl.pin_mla) pl.pin_mla(0);
if (form && pin) pin(0);
ok("MLA scratch allocation", false);
dlclose(ref);
return;
@@ -14095,7 +14100,7 @@ static void mla_check(const Plugin& pl, int64_t rows, int64_t heads, int64_t blo
versus, base, name, base / us, shape);
}
}
if (form && pl.pin_mla) pl.pin_mla(0);
if (form && pin) pin(0);
dlclose(ref);
}
@@ -14205,17 +14210,19 @@ static void mla_perf(const Plugin& pl, int64_t M, int64_t blocks, int64_t heads,
PS("dtype", "bf16") };
std::vector<uint16_t> base, rule;
auto run = [&](const char* name, int form, const char* label) {
const bool by_rows = std::strstr(name, "rows") != nullptr;
auto* const pin = by_rows ? pl.pin_mla_rows : pl.pin_mla;
const RadKernelInfo* r = find(pl, name);
if (!r) {
std::printf(" %-34s row absent\n", label);
return;
}
if (form >= 0) {
if (!pl.pin_mla) {
if (!pin) {
std::printf(" %-34s no pin hook\n", label);
return;
}
pl.pin_mla(form);
pin(form);
}
RadArgs a;
std::memset(&a, 0, sizeof(a));
@@ -14232,7 +14239,7 @@ static void mla_perf(const Plugin& pl, int64_t M, int64_t blocks, int64_t heads,
const int rc = r->launch(&a, nullptr);
if (rc != RAD_OK || st_stream_sync(nullptr) != hipSuccess) {
std::printf(" %-34s rc=%d (shape not served)\n", label, rc);
if (form >= 0) pl.pin_mla(0);
if (form >= 0) pin(0);
return;
}
std::vector<uint16_t> got(qh.size());
@@ -14253,7 +14260,7 @@ static void mla_perf(const Plugin& pl, int64_t M, int64_t blocks, int64_t heads,
st_ev_destroy(w1);
}
const double us = st_time_us([&](int) { r->launch(&a, nullptr); }, iters, nullptr);
if (form >= 0) pl.pin_mla(0);
if (form >= 0) pin(0);
char diff[192] = "reference";
if (base.empty())
base = got;
@@ -14279,7 +14286,7 @@ static void mla_perf(const Plugin& pl, int64_t M, int64_t blocks, int64_t heads,
}
if (form == 0) rule = got;
std::printf(" %-34s %10.2f us %7.2f TFLOP/s %s\n", label, us, flops / (us * 1e6), diff);
if (form >= 0 && pl.mla_stats) {
if (form >= 0 && !by_rows && pl.mla_stats) {
/* One more launch with the conflict counters on, outside the timing. */
ScopedEnv counting("RADIANCE_MLA_CONFLICT_STATS", "1");
unsigned long long v[3];
@@ -14308,6 +14315,11 @@ static void mla_perf(const Plugin& pl, int64_t M, int64_t blocks, int64_t heads,
run("mla_attn_matrix_bf16", 0, "matrix 16 heads, vector load");
run("mla_attn_matrix_bf16", 1, "matrix 16 heads, element load");
}
if (heads == 64) {
run("mla_attn_rows_bf16", 0, "rows 64 heads, vector load");
run("mla_attn_rows_bf16", 1, "rows 64 heads, element load");
run("mla_attn_rows_bf16", 2, "rows 32 heads, vector load");
}
}
static void kda_check(const Plugin& pl, const char* mode, bool cold, int accepted_count,
@@ -26663,6 +26675,26 @@ static void device_phase(const Plugin& pl) {
for (int64_t heads : { 8, 16 }) mla_project_check(pl, rows, heads, mode, true);
}
}
if (sel("mla_attn_rows_bf16")) {
/* A rank's run of rows with all 64 heads (GLM's prefill split by rows), against the matrix
* row on the same operands. Under 32 rows the rule runs the matrix kernel, so the rows
* kernel is pinned there (form 3). */
for (int requests : { 1, 2, 7, 33 })
for (int64_t blocks : { 17, 513 })
mla_check(pl, requests, 64, blocks, "mla_attn_rows_bf16", MlaFill::ragged,
"mla_attn_matrix_bf16", requests < 32 ? 3 : 0);
mla_check(pl, 7, 64, 513, "mla_attn_rows_bf16", MlaFill::full, "mla_attn_matrix_bf16", 3);
mla_check(pl, 2, 64, 513, "mla_attn_rows_bf16", MlaFill::prefill, "mla_attn_matrix_bf16", 3);
mla_check(pl, 33, 64, 513, "mla_attn_rows_bf16", MlaFill::prefill, "mla_attn_matrix_bf16");
for (int64_t swaps : { 0, 4, 16, 64 })
mla_check(pl, 33, 64, 65, "mla_attn_rows_bf16", MlaFill::topk, "mla_attn_matrix_bf16",
0, swaps);
mla_check(pl, 7, 64, 513, "mla_attn_rows_bf16", MlaFill::prefill, nullptr, 1);
mla_check(pl, 33, 64, 65, "mla_attn_rows_bf16", MlaFill::topk, nullptr, 2, 16);
mla_check(pl, 7, 64, 65, "mla_attn_rows_bf16", MlaFill::topk, "mla_attn_matrix_bf16", 0, 4);
mla_check(pl, 7, 32, 65, "mla_attn_rows_bf16", MlaFill::ragged, nullptr, 3);
mla_check(pl, 4, 40, 17, "mla_attn_rows_bf16");
}
if (sel("mla_attn_matrix_bf16")) {
mla_check(pl, 7, 16, 17, "mla_attn_matrix_bf16");
mla_check(pl, 33, 16, 17, "mla_attn_matrix_bf16");