ops: pair_argmax in libref and libavx -- the sharded draft head's top-1, one id a row

A vocab-sharded draft head proposes each position's argmax: every rank takes its slice's top-R
as (id, value) pairs, an all_gather puts them side by side, and something picks the winner.
row_topk_merge does that with an R-wide answer; a block drafter wants ONE id a row, written
contiguously into the token buffer it verifies, and a rank cannot send R = 1 -- a
row-interleaved all_gather moves whole 16-byte words and a pair is 8 bytes. pair_argmax is the
merge at R = 1 over world_size * R pairs: largest value, ties to the lower id, a negative id a
pad, -1 and -inf for a row with nothing valid, `val` optional.

The Gemma 4 DFlash head is its caller (arch/gemma4, later in this series) and gemma4k its device
kernel. libref is the oracle and the schema's first declarer; libavx carries the host row,
because an op the placement puts on the host cannot run on a reference (avx_check holds libavx
to libref's vocabulary). Both scalar: n is sixteen pairs at TP8.

docs/OPS.md: the row and a paragraph beside row_topk_merge's. ref_test: ties to the lower id in
any slot, a pad carrying the row's largest value dropped, an all-pad row, all-negative values,
`idx` at a pitch of two with the slots between untouched, and `val` absent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
AshlynandClaude Opus 5.5 committed 2026-10-12 14:40:04 -05:00
1 parent 29bfe8f00c
commit d0f6f332ff
10 files changed
+154 -2

No files matched your search

+38 -1
View File
@@ -273,7 +273,7 @@ TEST(op_vocabulary) {
"attn_paged", "attn_dense", "kv_store", "rope", "rope_table", "qk_norm_rope", "qsa_block_key",
"router_topk", "moe_scatter", "moe_gemm", "moe_gemm_q", "moe_gemm_q_mod4", "moe_gather",
"row_topk",
"row_topk_merge", "logit_rerank",
"row_topk_merge", "pair_argmax", "logit_rerank",
"gdn_conv_prep", "gdn_conv_update", "gdn_kkt_solve", "gdn_chunk_scan",
"gdn_recurrent_update", "gdn_conv_recurrent_update", "gdn_gated_rmsnorm",
"all_reduce", "all_gather",
@@ -604,6 +604,43 @@ TEST(op_scale) {
CHECK(run("scale", b) != RAD_OK);
}
/* The sharded draft head's merge at R = 1, on paper: three rows of four gathered pairs, each case
* one rule. Row 0 ties on 2.0 between ids 9 and 3 (the lower id wins, whatever slot it sits in)
* beside a pad carrying the row's largest value (dropped: a pad is a negative id, not a value);
* row 1 is all pads (-1 and -inf, nothing invented); row 2 is all negative values (the largest is
* still the answer). `idx` is written at a pitch of two, as a proposal lands in a token buffer laid
* out for something else, and the slots between are left alone. */
TEST(op_pair_argmax) {
struct P { int32_t id; float v; };
const P rows[3][4] = {
{ { 5, 1.0f }, { 9, 2.0f }, { 3, 2.0f }, { -1, 7.0f } },
{ { -1, 9.0f }, { -1, 9.0f }, { -1, 9.0f }, { -1, 9.0f } },
{ { 7, -3.0f }, { 2, -4.0f }, { 11, -1.0f }, { 4, -1.5f } },
};
T pairs(RAD_F32, {3, 4, 2});
float* pf = (float*)pairs.t.data;
for (int m = 0; m < 3; ++m)
for (int j = 0; j < 4; ++j) {
std::memcpy(&pf[(m * 4 + j) * 2], &rows[m][j].id, 4);
pf[(m * 4 + j) * 2 + 1] = rows[m][j].v;
}
T idx(RAD_I32, {3, 2}); idx.n({ 100, 100, 100, 100, 100, 100 });
idx.t.shape[1] = 1; /* [3, 1] at a row pitch of 2 */
T val(RAD_F32, {3});
A a; a.t(pairs).t(idx).t(val).i("M", 3).i("n", 4).s("dtype", "f32");
CHECK_OK(run("pair_argmax", a));
CHECK_EQ(idx.iat(0), 3); CHECK_EQ(val.at(0), 2.0f);
CHECK_EQ(idx.iat(2), -1); CHECK(std::isinf(val.at(1)) && val.at(1) < 0);
CHECK_EQ(idx.iat(4), 11); CHECK_EQ(val.at(2), -1.0f);
CHECK_EQ(idx.iat(1), 100); CHECK_EQ(idx.iat(3), 100);
/* `val` is optional, and the ids do not depend on it. */
T idx2(RAD_I32, {3});
A b; b.t(pairs).t(idx2).tnull().i("M", 3).i("n", 4).s("dtype", "f32");
CHECK_OK(run("pair_argmax", b));
CHECK_EQ(idx2.iat(0), 3); CHECK_EQ(idx2.iat(1), -1); CHECK_EQ(idx2.iat(2), 11);
}
/* The quantised gather. Three things, and the scale is the least interesting of them.
*
* The E4M3 codes are chosen so their VALUES are exact and writable as literals: 0x38 is 1.0,