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

+4
View File
@@ -688,6 +688,10 @@ void r4d_hc_read_pin_prefill(int form);
// wherever the cache rows are aligned to them), 1 the element-wise load the rule falls back to.
// Bit-identical; r4d_selftest compares them and --perfmla times both.
void r4d_mla_matrix_pin_form(int form);
// Test hook: pins the MLA rows row's form. 0 is the launcher's rule (64 heads a workgroup, vector
// key loads where aligned; the matrix row's kernel under 32 rows), 1 the element-wise key load
// (bit-identical to 3), 2 32 heads a workgroup, 3 the rows kernel of the rule at any row count.
void r4d_mla_rows_pin_form(int form);
// Test hook: records the function each launch dispatches into `fns[0..cap)`, in order, until the
// next call, and returns how many launches the previous recording saw (which may exceed its cap).
// Null `fns` stops recording. r4d_selftest uses it to show that a pinned form dispatches a kernel
+240
View File
@@ -537,6 +537,178 @@ __global__ __launch_bounds__(128) void attend_matrix(const uint16_t* q, const ui
atomicAdd(&g_mla_stats[2], (unsigned long long)(raster + 15) / 16);
}
}
/* ROWS gives a workgroup one query row and HG groups of sixteen heads, four waves a group, so a
* key chunk is loaded once for 16*HG heads instead of the head-tiled row's sixteen. GLM's prefill
* splits attention by rows, one rank holding all 64 heads of its rows; that is what this serves.
*
* Scores are S^T = K Q^T: tokens are the matrix rows and heads the columns, so a head is a lane
* and its running maximum and denominator stay in registers. Each wave of a group takes a quarter
* of the latent dimension; the four partial dot products meet in LDS and every wave of the group
* adds them in the same order, so all four hold identical scores and statistics. The
* probabilities become the second product's B operand with one permlanex16 swap, and each wave
* accumulates O^T for its quarter of the latent columns, the keys read transposed from the tile.
* The next chunk's keys are fetched into registers while the current one is multiplied, so a
* chunk costs two barriers. */
constexpr int kRowsPitch = 520;
template <int HG, bool WIDE>
__global__ __launch_bounds__(HG * 128) void attend_rows(const uint16_t* q, const uint16_t* kv,
const int* bt, const int* len,
uint16_t* out, float* scratch, Meta m) {
constexpr int NT = HG * 128, LOADS = 16 * 64 / NT;
static_assert(LOADS * NT == 16 * 64 && LOADS <= 32, "a chunk's 16-byte pieces over the group");
__shared__ __align__(16) uint16_t keys[2][16 * kRowsPitch];
__shared__ __align__(32) float partial[HG * 4][32 * 8];
__shared__ int visible[2][16];
const int tid = threadIdx.x, lane = tid & 31, wave = tid >> 5, quarter = wave & 3,
group = wave >> 2, r = blockIdx.x, split = blockIdx.z;
const int head = blockIdx.y * 16 * HG + group * 16 + (lane & 15);
const int live = min(max(len[(long)r * m.lp], 0), m.blocks * m.bs);
const int begin = (int)((long)live * split / m.splits),
end = (int)((long)live * (split + 1) / m.splits);
v8bf query[8];
{
const uint16_t* qp =
q + (long)r * m.qr + (long)head * m.qh + quarter * 128 + 4 * (lane >> 4);
#pragma unroll
for (int s = 0; s < 8; ++s) {
union {
uint16_t h[8];
v8bf f;
} u;
#pragma unroll
for (int e = 0; e < 4; ++e) {
u.h[e] = qp[s * 16 + e];
u.h[4 + e] = qp[s * 16 + 8 + e];
}
query[s] = u.f;
}
}
uint4 staged[LOADS];
unsigned staged_ok = 0;
auto fetch = [&](int start) {
#pragma unroll
for (int j = 0; j < LOADS; ++j) {
const int flat = tid + j * NT, token = start + (flat >> 6), piece = flat & 63;
const int page = token < end ? bt[(long)r * m.bp + (token / m.bs) * m.bc] : -1;
const bool ok = page >= 0 && page < m.pages;
const long at = ok ? (long)page * m.kp + (token % m.bs) * m.kt + piece * 8 : 0;
if constexpr (WIDE) {
staged[j] = *(const uint4*)(kv + at);
if (!ok) staged[j] = make_uint4(0, 0, 0, 0);
} else {
uint32_t w[4];
#pragma unroll
for (int i = 0; i < 4; ++i)
w[i] = ok ? (uint32_t)kv[at + 2 * i] | (uint32_t)kv[at + 2 * i + 1] << 16 : 0u;
staged[j] = make_uint4(w[0], w[1], w[2], w[3]);
}
staged_ok = ok ? staged_ok | 1u << j : staged_ok & ~(1u << j);
}
};
auto stash = [&](int b) {
#pragma unroll
for (int j = 0; j < LOADS; ++j) {
const int flat = tid + j * NT, token = flat >> 6, piece = flat & 63;
*(uint4*)(keys[b] + token * kRowsPitch + piece * 8) = staged[j];
if (!piece) visible[b][token] = (int)(staged_ok >> j & 1u);
}
};
v8f value[8];
#pragma unroll
for (int mt = 0; mt < 8; ++mt) value[mt] = (v8f){ 0 };
float run_max = -INFINITY, run_den = 0;
fetch(begin);
stash(0);
__syncthreads();
int buf = 0;
for (int start = begin; start < end; start += 16) {
const bool more = start + 16 < end;
if (more) fetch(start + 16);
const uint16_t* tile = keys[buf];
v8f dot = { 0 };
#pragma unroll
for (int s = 0; s < 8; ++s)
dot = mma(frag(tile, kRowsPitch, 0, quarter * 128 + s * 16, lane), query[s], dot);
*(v8f*)&partial[wave][lane * 8] = dot;
__syncthreads();
v8f score = *(const v8f*)&partial[group * 4][lane * 8];
#pragma unroll
for (int k = 1; k < 4; ++k) score += *(const v8f*)&partial[group * 4 + k][lane * 8];
float chunk_max = -INFINITY;
#pragma unroll
for (int e = 0; e < 8; ++e) {
score[e] = visible[buf][8 * (lane >> 4) + e] ? score[e] * m.scale : -INFINITY;
chunk_max = fmaxf(chunk_max, score[e]);
}
chunk_max = fmaxf(chunk_max, swap16(chunk_max));
const float next_max = fmaxf(run_max, chunk_max);
const float alpha = isfinite(next_max) ? expf(run_max - next_max) : 1;
float p[8], sum = 0;
#pragma unroll
for (int e = 0; e < 8; ++e) {
p[e] = isfinite(score[e]) ? expf(score[e] - next_max) : 0;
sum += p[e];
}
/* Both halves add tokens 0-7 before 8-15, so the sixteen lanes of a head agree. */
const float other = swap16(sum);
run_den = run_den * alpha + (lane < 16 ? sum + other : other + sum);
run_max = next_max;
/* A B fragment's lane holds its head's tokens 0-3 and 8-11 (lanes 0-15) or 4-7 and 12-15
* (16-31); a score lane holds 0-7 or 8-15. Each half sends the other the four it needs. */
uint32_t hi[4], lo[4];
#pragma unroll
for (int i = 0; i < 4; ++i) {
const uint16_t h0 = f32_to_bf16(p[2 * i]), h1 = f32_to_bf16(p[2 * i + 1]);
hi[i] = (uint32_t)h0 | (uint32_t)h1 << 16;
lo[i] = (uint32_t)f32_to_bf16(p[2 * i] - bf16_to_f32(h0)) |
(uint32_t)f32_to_bf16(p[2 * i + 1] - bf16_to_f32(h1)) << 16;
}
const bool low = lane < 16;
const uint32_t hx0 = r4d_xor_lane_u32<16>(low ? hi[2] : hi[0]),
hx1 = r4d_xor_lane_u32<16>(low ? hi[3] : hi[1]),
lx0 = r4d_xor_lane_u32<16>(low ? lo[2] : lo[0]),
lx1 = r4d_xor_lane_u32<16>(low ? lo[3] : lo[1]);
union {
uint32_t w[4];
v8bf f;
} ph, pl;
ph.w[0] = low ? hi[0] : hx0;
ph.w[1] = low ? hi[1] : hx1;
ph.w[2] = low ? hx0 : hi[2];
ph.w[3] = low ? hx1 : hi[3];
pl.w[0] = low ? lo[0] : lx0;
pl.w[1] = low ? lo[1] : lx1;
pl.w[2] = low ? lx0 : lo[2];
pl.w[3] = low ? lx1 : lo[3];
#pragma unroll
for (int mt = 0; mt < 8; ++mt) {
const auto v = fragT(tile, kRowsPitch, quarter * 128 + mt * 16, 0, lane);
value[mt] *= alpha;
value[mt] = mma(v, ph.f, value[mt]);
value[mt] = mma(v, pl.f, value[mt]);
}
if (more) stash(buf ^ 1);
__syncthreads();
buf ^= 1;
}
#pragma unroll
for (int mt = 0; mt < 8; ++mt)
#pragma unroll
for (int e = 0; e < 8; ++e) {
const int col = quarter * 128 + mt * 16 + 8 * (lane >> 4) + e;
if (m.splits == 1)
out[(long)r * m.orr + (long)head * m.oh + col] =
f32_to_bf16(run_den > 0 ? value[mt][e] / run_den : 0);
else
scratch[(((long)r * m.heads + head) * m.splits + split) * PART + col] =
value[mt][e];
}
if (m.splits > 1 && !quarter && lane < 16) {
float* dst = scratch + (((long)r * m.heads + head) * m.splits + split) * PART;
dst[D] = run_max;
dst[D + 1] = run_den;
}
}
bool env_on(const char* name) {
const char* e = std::getenv(name);
return e && *e && *e != '0';
@@ -643,6 +815,70 @@ int body(const RadArgs* a, RadStream stream) {
if (rc == RAD_OK && stats) report_stats(stream);
return rc;
}
/* r4d_mla_rows_pin_form: 0 is the launcher's rule (64 heads a workgroup, vector key loads where
* the cache is aligned, the matrix row's kernel under kRowsMin rows), 1 the element-wise key load,
* 2 thirty-two heads a workgroup, 3 the rule's rows kernel at any row count, 4 the matrix row's
* kernel at every row count. The pin is a test hook (r4d_selftest, r4d_emu); nothing else sets
* it. */
int g_mla_rows_form = 0;
/* Under it, a decode step's rows at one rank, a workgroup a row leaves the card mostly idle, and
* the matrix row's four tiles a row and its splits are faster (r4d_selftest --case
* mla_attn_rows_bf16, M 7: 0.5x). */
constexpr long kRowsMin = 32;
int rows_body(const RadArgs* a, RadStream stream) {
if (!a || a->n_t < 5) return RAD_E_INVAL;
const auto &q = a->t[0], &kv = a->t[1], &bt = a->t[2], &len = a->t[3], &o = a->t[4];
if (q.dtype != RAD_BF16 || kv.dtype != RAD_BF16 || o.dtype != RAD_BF16 || bt.dtype != RAD_I32 ||
len.dtype != RAD_I32)
return RAD_E_DTYPE;
for (int i = 0; i < 5; ++i)
if (!a->t[i].data) return RAD_E_INVAL;
const long heads = r4d_geti_or(a, "heads", 0), bs = r4d_geti_or(a, "block_size", 0);
const float scale = (float)r4d_getf(a, "scale", 0);
if (r4d_geti_or(a, "latent_dim", 0) != D || heads < 1 || heads > 64 || bs < 1 || bs > 256 ||
!std::isfinite(scale) || scale <= 0 || q.rank != 3 || q.shape[0] < 1 ||
q.shape[0] > 65535 || q.shape[1] != heads || q.shape[2] != D || q.stride[2] != 1 ||
q.stride[1] < D || q.stride[0] < heads * q.stride[1] || kv.rank != 4 || kv.shape[0] < 1 ||
kv.shape[0] > INT_MAX || kv.shape[1] != 1 || kv.shape[2] != bs || kv.shape[3] != D ||
kv.stride[3] != 1 || kv.stride[2] < D || kv.stride[0] < bs * kv.stride[2] || bt.rank != 2 ||
bt.shape[0] != q.shape[0] || bt.shape[1] < 1 || bt.shape[1] > INT_MAX / bs ||
bt.stride[1] < 1 || bt.stride[0] < bt.shape[1] * bt.stride[1] || len.rank != 1 ||
len.shape[0] != q.shape[0] || len.stride[0] < 1 || o.rank != 3 ||
o.shape[0] != q.shape[0] || o.shape[1] != heads || o.shape[2] != D || o.stride[2] != 1 ||
o.stride[1] < D || o.stride[0] < heads * o.stride[1])
return RAD_E_SHAPE;
if (heads % 32) return RAD_E_UNSUPPORTED;
const int form = g_mla_rows_form;
if (form == 4 || (!form && q.shape[0] < kRowsMin)) return body<true>(a, stream);
const int group_heads = heads % 64 || form == 2 ? 32 : 64;
const long rows = q.shape[0], groups = heads / group_heads, jobs = rows * groups;
/* Short batches split the history until 64 workgroups run, two to each of the R9700's 32
* WGPs, as far as 16-token chunks and the scratch's 2048 partials allow. */
int splits = (int)std::min(64l, (64 + jobs - 1) / jobs);
splits = (int)std::max(1l, std::min((long)splits, (bt.shape[1] * bs + 15) / 16));
splits = (int)std::max(1l, std::min((long)splits, 2048 / (rows * heads)));
if (!a->scratch || a->scratch_bytes < matrix_scratch_bytes) splits = 1;
const bool wide = form != 1 && !((uintptr_t)kv.data & 15) && !(kv.stride[0] % 8) &&
!(kv.stride[2] % 8);
Meta m{ q.stride[0], q.stride[1], kv.stride[0], kv.stride[2],
bt.stride[0], bt.stride[1], len.stride[0], o.stride[0],
o.stride[1], (int)q.shape[0], (int)heads, (int)bt.shape[1],
(int)bs, (int)kv.shape[0], splits, scale };
#define ROWS_EMIT(HG, W) \
R4D_EMIT(stream, (attend_rows<HG, W>), dim3(m.rows, (unsigned)groups, m.splits), \
dim3(HG * 128), 0, (const uint16_t*)q.data, (const uint16_t*)kv.data, \
(const int*)bt.data, (const int*)len.data, (uint16_t*)o.data, (float*)a->scratch, m)
int rc;
if (group_heads == 64)
rc = wide ? ROWS_EMIT(4, true) : ROWS_EMIT(4, false);
else
rc = wide ? ROWS_EMIT(2, true) : ROWS_EMIT(2, false);
#undef ROWS_EMIT
if (rc == RAD_OK && splits > 1)
rc = R4D_EMIT(stream, merge, dim3(m.rows, (m.heads + 3) / 4), dim3(128), 0,
(const float*)a->scratch, (uint16_t*)o.data, m);
return rc;
}
}
extern "C" int r4d_rad_mla_attn(const RadArgs* a, RadStream stream) {
return body<false>(a, stream);
@@ -650,12 +886,16 @@ extern "C" int r4d_rad_mla_attn(const RadArgs* a, RadStream stream) {
extern "C" int r4d_rad_mla_attn_matrix(const RadArgs* a, RadStream stream) {
return body<true>(a, stream);
}
extern "C" int r4d_rad_mla_attn_rows(const RadArgs* a, RadStream stream) {
return rows_body(a, stream);
}
extern "C" int r4d_rad_mla_project(const RadArgs* a, RadStream stream) {
return project_body(a, stream);
}
extern "C" int64_t r4d_rad_mla_attn_scratch(const RadArgs*) { return scratch_bytes; }
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; }
/* Test hook: the current device's RADIANCE_MLA_CONFLICT_STATS counters since the last call (passes,
* second passes, raster chunks), then zeroed. */
extern "C" void r4d_mla_matrix_stats(unsigned long long* out) {
+1
View File
@@ -698,6 +698,7 @@ int r4d_rad_mla_attn(const RadArgs*, RadStream);
int64_t r4d_rad_mla_attn_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);
int r4d_shape_mla_attn(const RadParam*, int, int, RadOpdDesc*);
int r4d_rad_router_sigmoid_topk(const RadArgs*, RadStream);
int r4d_shape_router_sigmoid_topk(const RadParam*, int, int, RadOpdDesc*);
+16
View File
@@ -4266,6 +4266,16 @@ static const RadConstraint cMlaMatrix[] = { C_GE("M", 1),
C_GE("blocks", 1),
C_EQ("block_size", 4),
C_IN("dtype", "bf16") };
/* GLM's prefill splits MLA by rows, a rank holding every head of its rows: the rows row loads a
* key chunk once for a workgroup of 64 heads and outranks the matrix row there. Measured on gfx1201
* (r4d_selftest --perfmla 512 513 64 20 topk:16:16000), a rank's run of 512 rows takes 3.9 ms
* against 12.3 ms on the matrix row, and the 4096 rows x 8 heads it replaces 14.0 ms on the paired
* matrix row. Under 32 rows its launcher runs the matrix row's kernel (r4d_mla.hip); a bound on M
* here would cut every mla_attn band at 32, the eight-head ones too. */
static const RadConstraint cMlaRows[] = { C_GE("M", 1), C_LE("M", 65535),
C_EQ("latent_dim", 512), C_EQ("heads", 64),
C_GE("blocks", 1), C_EQ("block_size", 4),
C_IN("dtype", "bf16") };
/* `n` is bounded by LDS: nh (hc*n) and ne (n) are both bf16 and live through the second phase;
* 28672 elements is that budget at hc 4. */
static const RadConstraint cMtpEnter[] = {
@@ -5541,6 +5551,12 @@ static const RadKernelInfo kKernels[] = {
nullptr, nullptr, r4d_rad_scale, nullptr, nullptr, nullptr,
r4d_shape_scale },
{ "mla_attn_rows_bf16", "mla_attn", "attn", "row-tiled WMMA latent attention, one key load for 64 heads",
"latent_dim=512, heads=64, block_size=4",
"bf16 operands; FP32 probabilities via high/low BF16",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY + 2, ROW(cMlaRows), NOTUNE,
nullptr, nullptr, r4d_rad_mla_attn_rows, r4d_rad_mla_attn_matrix_scratch, nullptr, nullptr,
r4d_shape_mla_attn },
{ "mla_attn_matrix_bf16", "mla_attn", "attn", "head-tiled WMMA latent attention with FP32 softmax",
"latent_dim=512, heads a multiple of 8 (heads%16==8 pairs two requests a tile), block_size=4",
"bf16 operands; FP32 probabilities via high/low BF16",
+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");