ParoQuant: check the givens pairs at load; one re-tile slab for every part; one bounds helper; one mxfp4 GEMM declaration

* libr4d: givens_quant_act_fp8 gets layout/relayout/unrelayout hooks for `pairs` -- stored as is,
  refused (RAD_E_FORMAT) when a stage is not a pairing of its own group of 128, which the kernel would
  index out of bounds. relayout_parity_test covers it.
* gemm_mxfp4a8_tiled: the parts run in order on one stream, so they share one re-tiled activation;
  the scratch is one slab again.
* mx_part_bounds: the pb1/pb2 clamping in one place (mx_parts, the decode scratch hook).
* LinearFP8: one gemm_nt_q declaration and one issue path for the plain and the rotated MXFP4 weight;
  pb1/pb2 still only on a weight with parts, so a one-part op keeps its tune-cache geometry.
This commit is contained in:
Malic Zilyas committed 2026-10-08 23:35:43 +02:00
1 parent 5681da471c
commit 9af15f3a6e
9 files changed
+169 -63

No files matched your search

+19 -33
View File
@@ -1090,27 +1090,16 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
RAD_ARCH_TRY(rad_weight_shard_span(b, ws, lo, hi));
RAD_ARCH_TRY(rad_weight_shard_span(b, wref, lo, hi));
}
/* ONE GEMM for both: a rotated weight reads its own rotated pair, a plain one the
* producer's. `pb1`/`pb2` (the last two params) go only to a weight with parts, so a one-part
* op keeps the plain op's geometry -- the key the tune cache is read by. */
const int64_t hi = max_rows > 0 ? max_rows : g.max_tok;
if (paro) {
op = rot_parts > 1
? rw(b, RAD_OP(b, "gemm_nt_q",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8"),
RAD_INT("pb1", pb1), RAD_INT("pb2", pb2)),
RAD_WEIGHTS(w, ws, wref)),
{ rot_q, rot_s }, { y })
: rw(b, RAD_OP(b, "gemm_nt_q",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8")),
RAD_WEIGHTS(w, ws, wref)),
{ rot_q, rot_s }, { y });
return op ? RAD_OK : RAD_E_INVAL;
}
op = rw(b, RAD_OP(b, "gemm_nt_q",
RAD_PARAMS(RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8")),
RAD_WEIGHTS(w, ws, wref)),
{ a.q, a.s }, { y });
const RadParam ps[] = { RAD_RANGE("M", 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_MX_GROUP), RAD_STR("dtype", "mxfp4a8"),
RAD_INT("pb1", pb1), RAD_INT("pb2", pb2) };
const int n_ps = (int)(sizeof ps / sizeof ps[0]) - (rot_parts > 1 ? 0 : 2);
op = rw(b, rad_decl_op(b, "gemm_nt_q", ps, n_ps, RAD_WEIGHTS(w, ws, wref)),
{ paro ? rot_q : a.q, paro ? rot_s : a.s }, { y });
return op ? RAD_OK : RAD_E_INVAL;
}
@@ -1212,20 +1201,17 @@ inline void LinearFP8::step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, in
RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_NONE);
return;
}
if (paro) {
/* The rotation first: this linear's own codes, `rot_parts` copies side by side. */
RAD_ISSUE_N(c, op_rot, rows, brow_slice(a.x, r0, rows, K), RAD_W(rot_pairs),
RAD_W(rot_angle), RAD_W(rot_scale),
brow_slice(rot_q, r0, rows, rot_parts * K),
brow_slice(rot_s, r0, rows, rot_parts * (K / RAD_FP8_BLOCK)));
RAD_ISSUE_N(c, op, rows, brow_slice(rot_q, r0, rows, rot_parts * K),
brow_slice(rot_s, r0, rows, rot_parts * (K / RAD_FP8_BLOCK)),
RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_W(wref));
return;
}
if (mx) {
RAD_ISSUE_N(c, op, rows, brow_slice(a.q, r0, rows, K),
brow_slice(a.s, r0, rows, K / RAD_FP8_BLOCK),
/* A rotated weight rotates its input first, into its own codes, `rot_parts` copies side
* by side (one for a plain weight, which reads the producer's). */
const rad_buf aq = paro ? rot_q : a.q, as = paro ? rot_s : a.s;
const int64_t ak = rot_parts * K;
if (paro)
RAD_ISSUE_N(c, op_rot, rows, brow_slice(a.x, r0, rows, K), RAD_W(rot_pairs),
RAD_W(rot_angle), RAD_W(rot_scale), brow_slice(aq, r0, rows, ak),
brow_slice(as, r0, rows, ak / RAD_FP8_BLOCK));
RAD_ISSUE_N(c, op, rows, brow_slice(aq, r0, rows, ak),
brow_slice(as, r0, rows, ak / RAD_FP8_BLOCK),
RAD_W(w), RAD_W(ws), brow_slice(y, r0, rows, N), RAD_NONE, RAD_W(wref));
return;
}
+17 -8
View File
@@ -368,21 +368,30 @@ struct MxParts {
long long lda = 0; /* 0: the plane's own K */
};
/* The bounds `pb1`/`pb2` name over N rows, as every reader takes them: { 0, e1, e2, N }, part i
* being [bound[i], bound[i+1]). Returns how many copies of the activation they imply, 1..3. */
static inline int mx_part_bounds(long long N, long long pb1, long long pb2, long long bound[4]) {
const long long e1 = pb1 > 0 && pb1 < N ? pb1 : N;
long long e2 = pb2 > 0 && pb2 < N ? pb2 : N;
if (e2 < e1) e2 = e1;
bound[0] = 0;
bound[1] = e1;
bound[2] = e2;
bound[3] = N;
return e2 < N ? 3 : (e1 < N ? 2 : 1);
}
static inline int mx_parts(const RadArgs* a, GemmOps* g, MxParts* p) {
const long long pb1 = r4d_geti_or(a, "pb1", 0), pb2 = r4d_geti_or(a, "pb2", 0);
p->n = 1;
p->n0[0] = 0;
p->n1[0] = g->N;
p->lda = 0;
if (pb1 <= 0 && pb2 <= 0) return RAD_OK;
long long bound[4];
const int segs = mx_part_bounds(g->N, r4d_geti_or(a, "pb1", 0), r4d_geti_or(a, "pb2", 0), bound);
if (segs == 1) return RAD_OK;
const long long K = r4d_geti_or(a, "K", 0);
if (K <= 0) return R4D_NO(RAD_E_INVAL);
const long long e1 = pb1 > 0 ? (pb1 < g->N ? pb1 : g->N) : g->N;
long long e2 = pb2 > 0 ? (pb2 < g->N ? pb2 : g->N) : g->N;
if (e2 < e1) e2 = e1;
const long long bound[4] = { 0, e1, e2, g->N };
const int segs = e2 < g->N ? 3 : (e1 < g->N ? 2 : 1);
if (g->K != (long long)segs * K || (e1 % 16) || (e2 % 16)) return R4D_NO(RAD_E_SHAPE);
if (g->K != (long long)segs * K || (bound[1] % 16) || (bound[2] % 16)) return R4D_NO(RAD_E_SHAPE);
p->n = 0;
for (int i = 0; i < 3; ++i)
if (bound[i + 1] > bound[i]) {
+2 -5
View File
@@ -621,11 +621,8 @@ extern "C" int64_t r4d_rad_gemm_mxfp4a8_decode_scratch(const RadArgs* a) {
if (M < 1 || N < 16 || K < 64 || M > r4d_gemm_mxfp4a8_decode_max_m()) return 0;
R4dMxDecCfg t;
r4d_cfg_mxfp4_decode(a, &t);
const long long pb1 = r4d_geti_or(a, "pb1", 0), pb2 = r4d_geti_or(a, "pb2", 0);
const long long e1 = pb1 > 0 ? (pb1 < N ? pb1 : N) : N;
long long e2 = pb2 > 0 ? (pb2 < N ? pb2 : N) : N;
if (e2 < e1) e2 = e1;
const long long bound[4] = { 0, e1, e2, N };
long long bound[4];
mx_part_bounds(N, r4d_geti_or(a, "pb1", 0), r4d_geti_or(a, "pb2", 0), bound);
int64_t worst = 0;
for (int i = 0; i < 3; ++i) {
const long long np = bound[i + 1] - bound[i];
+7 -12
View File
@@ -367,8 +367,8 @@ extern "C" void r4d_gemm_mxfp4a8_tiled(long at, long ascale, long wq, long ws, l
// ============================================================== the plugin ABI entry point
// The row-major activation is re-tiled into the op's scratch, then the GEMM reads it from there.
// A partitioned weight re-tiles each part's copy of the activation into its own slab of the
// scratch and runs the GEMM over that part's rows (mx_parts).
// A partitioned weight re-tiles each part's copy of the activation into the scratch in turn and
// runs the GEMM over that part's rows (mx_parts).
extern "C" int r4d_rad_gemm_mxfp4a8_tiled(const RadArgs* a, RadStream s) {
GemmOps g;
int rc = gemm_ops(a, &g, 1);
@@ -386,13 +386,13 @@ extern "C" int r4d_rad_gemm_mxfp4a8_tiled(const RadArgs* a, RadStream s) {
rc = mx_ascale_grid(g, &as_group, &as_rs, mp.n);
if (rc != RAD_OK) return rc;
const long long slab = r4d_mxfp4a8_tile_a_bytes((int)g.M, (int)g.K);
if (!a->scratch || a->scratch_bytes < slab * mp.n) return R4D_NO(RAD_E_INVAL);
if (!a->scratch || a->scratch_bytes < slab) return R4D_NO(RAD_E_INVAL);
R4dMxTileCfg t;
r4d_cfg_mxfp4_tiled(a, &t);
const long kb = (long)((g.K + 127) / 128);
const long at = (long)(uintptr_t)a->scratch;
for (int p = 0; p < mp.n; ++p) {
const long long n0 = mp.n0[p], np = mp.n1[p] - mp.n0[p];
const long at = (long)(uintptr_t)a->scratch + (long)(p * slab);
R4D_TRY("gemm_mxfp4a8_tiled",
r4d_mxfp4a8_tile_a(r4d_addr(g.a) + p * g.K, at, (int)g.M, (int)g.K,
(long)(uintptr_t)s, (int)(mp.lda ? mp.lda : g.K));
@@ -405,14 +405,9 @@ extern "C" int r4d_rad_gemm_mxfp4a8_tiled(const RadArgs* a, RadStream s) {
}
/* The core sizes the op's scratch from this, at the band's widest M: the fragment-tiled copy of
* the activation, whole 16-row fragments -- one a part. */
* the activation, whole 16-row fragments. One copy for every part: the parts run one after another
* on the op's stream, so a part's re-tile cannot overtake the previous part's GEMM. */
extern "C" int64_t r4d_rad_gemm_mxfp4a8_tiled_scratch(const RadArgs* a) {
const long long M = r4d_geti_or(a, "M", 0), K = r4d_geti_or(a, "K", 0);
const long long N = r4d_geti_or(a, "N", 0);
const long long pb1 = r4d_geti_or(a, "pb1", 0), pb2 = r4d_geti_or(a, "pb2", 0);
const long long e1 = pb1 > 0 ? (pb1 < N ? pb1 : N) : N;
long long e2 = pb2 > 0 ? (pb2 < N ? pb2 : N) : N;
if (e2 < e1) e2 = e1;
const int parts = e2 < N ? 3 : (e1 < N ? 2 : 1);
return (int64_t)r4d_mxfp4a8_tile_a_bytes((int)M, (int)K) * parts;
return (int64_t)r4d_mxfp4a8_tile_a_bytes((int)M, (int)K);
}
+2 -2
View File
@@ -5,8 +5,8 @@
// the weight is read by the plain MXFP4 GEMM unchanged.
//
// WHY THE ROTATION IS A GROUP AT A TIME. ParoQuant's pairs never leave a group of 128 columns --
// that is how the checkpoint is trained, and the converter refuses one that does -- so a group is a
// closed system: its 128 values are rotated by S stages of 64 disjoint pairs and then quantised
// that is how the checkpoint is trained, and the converter and the loader (r4d_rad_relayout_givens_pairs)
// refuse one that does -- so a group is a closed system: its 128 values are rotated by S stages of 64 disjoint pairs and then quantised
// with ONE scale. A wave owns a group. The values sit in LDS (128 floats a wave, no barrier
// needed: a wave's LDS accesses are in order, and the pairs of a stage are disjoint so no two lanes
// touch one address), each lane takes two pairs a stage, and the absmax is a wave reduction. The
+65
View File
@@ -611,6 +611,71 @@ extern "C" int r4d_rad_unrelayout_w2a8(const RadParam* p, int n_p, int operand,
return RAD_OK;
}
/* ---- givens pairs: ParoQuant's rotation pairs (i32 [P*S, K]), stored as they are but CHECKED.
* The rotate kernels index a group's 128 LDS slots with them unchecked, so a stage that is not a
* pairing of its own group -- an index outside the group, or one column twice -- is refused here,
* at load, rather than read out of bounds. Operand 1 of the givens rows; the rest is as stored. */
namespace {
constexpr int GV_PAIRS_OPERAND = 1;
constexpr long long GV_GROUP = 128;
int gv_pairs_layout(const RadParam* p, int n_p, int operand, const RadEncoding* enc, const int* sel,
const RadTensor* pl, int n, RadLayout* out) {
if (!out || !pl) return RAD_E_INVAL;
if (operand != GV_PAIRS_OPERAND) return RAD_E_UNSUPPORTED;
const long long K = r4d_pgeti_or(p, n_p, "n", 0);
if (K <= 0 || K % GV_GROUP || !selects(enc, sel, n, "t.pairs") || pl[0].rank != 2 ||
pl[0].dtype != RAD_I32 || pl[0].shape[1] != K) return RAD_E_SHAPE;
fill(out, "r4d.givens.pairs", RAD_I32, pl[0].shape[0], K, pl[0].shape[0] * K * 4);
return RAD_OK;
}
/* Every row (a stage of a part) is, group by group, a perfect matching of the group's columns. */
bool gv_pairs_ok(const int32_t* pairs, long long rows, long long K) {
for (long long r = 0; r < rows; ++r)
for (long long g = 0; g < K; g += GV_GROUP) {
bool seen[GV_GROUP] = {};
for (long long i = 0; i < GV_GROUP; ++i) {
const long long c = (long long)pairs[r * K + g + i] - g;
if (c < 0 || c >= GV_GROUP || seen[c]) return false;
seen[c] = true;
}
}
return true;
}
} /* namespace */
extern "C" int r4d_rad_layout_givens_pairs(const RadParam* p, int n_p, int operand,
const RadEncoding* enc, const int* sel,
const RadTensor* pl, int n, RadLayout* out) {
return gv_pairs_layout(p, n_p, operand, enc, sel, pl, n, out);
}
extern "C" int r4d_rad_relayout_givens_pairs(const RadParam* p, int n_p, int operand,
const RadEncoding* enc, const int* sel,
const RadTensor* pl, int n, void* dst,
int64_t dst_bytes) {
RadLayout L;
const int rc = gv_pairs_layout(p, n_p, operand, enc, sel, pl, n, &L);
if (rc != RAD_OK) return rc;
if (dst_bytes != L.bytes || !pl[0].data) return RAD_E_SHAPE;
if (!gv_pairs_ok((const int32_t*)pl[0].data, L.shape[0], L.shape[1])) return RAD_E_FORMAT;
std::memcpy(dst, pl[0].data, (size_t)L.bytes);
return RAD_OK;
}
extern "C" int r4d_rad_unrelayout_givens_pairs(const RadParam* p, int n_p, int operand,
const RadEncoding* enc, const int* sel,
const void* src, int64_t src_bytes,
const RadTensor* pl, int n) {
RadLayout L;
const int rc = gv_pairs_layout(p, n_p, operand, enc, sel, pl, n, &L);
if (rc != RAD_OK) return rc;
if (src_bytes != L.bytes || !pl[0].data) return RAD_E_SHAPE;
std::memcpy(pl[0].data, src, (size_t)L.bytes);
return RAD_OK;
}
/* ---- MXFP4: fp4 e2m1 codes with an E8M0 exponent a 32. The codes go to fragment order, the
* exponents to [K/32][N], and b_ref is each row's largest exponent -- a reduction over the plane,
* which is a derivation any reader of the plane could make and so not a choice. */
+6
View File
@@ -391,6 +391,12 @@ int r4d_rad_layout_mxfp4(const RadParam*, int, int, const RadEncoding*, const in
const RadTensor*, int, RadLayout*);
int r4d_rad_relayout_mxfp4(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, void*, int64_t);
int r4d_rad_layout_givens_pairs(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, RadLayout*);
int r4d_rad_relayout_givens_pairs(const RadParam*, int, int, const RadEncoding*, const int*,
const RadTensor*, int, void*, int64_t);
int r4d_rad_unrelayout_givens_pairs(const RadParam*, int, int, const RadEncoding*, const int*,
const void*, int64_t, const RadTensor*, int);
/* -- the small bf16 model ops (r4d_model_bf16.hip) --
*
+5 -3
View File
@@ -5161,11 +5161,13 @@ static const RadKernelInfo kKernels[] = {
"n a multiple of 128 -- a rotation group cannot straddle the end of a row -- and group 128, "
"which is both the rotation group and the weight scale's activation block",
"bf16 in, E4M3 out in PLAIN ROW-MAJOR order, parts side by side, with an f32 scale per (row, part, "
"128 columns); the tables are the weight's int32 pairs, f32 angles and f32 channel scales",
"128 columns); the tables are the weight's int32 pairs (checked at load to pair each group's own "
"columns), f32 angles and f32 channel scales",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cHadQuantActFp8), NOTUNE,
nullptr, nullptr, r4d_rad_givens_quant_act_fp8, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, r4d_rad_givens_quant_act_fp8_describe },
nullptr, nullptr, r4d_rad_givens_quant_act_fp8, nullptr, r4d_rad_layout_givens_pairs,
r4d_rad_relayout_givens_pairs, nullptr, r4d_rad_unrelayout_givens_pairs, nullptr,
r4d_rad_givens_quant_act_fp8_describe },
{ "quant_act_i8g", "quant_act_i8g", "quant",
"per-(row, 128-column) symmetric int8 quantisation of a bf16 activation",
+46
View File
@@ -760,3 +760,49 @@ TEST(a_kernel_refuses_an_encoding_it_does_not_read) {
}
RAD_TEST_MAIN()
/* ParoQuant's rotation pairs are stored as they are -- and refused at load when a stage is not a
* pairing of its own group's 128 columns, which the rotate kernels would index out of bounds. */
TEST(givens_pairs_are_stored_as_they_are_and_checked) {
const int64_t K = 256, R = 2; /* two groups, two stages */
RadEncoding e = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
rad_enc_add_table(&e, "t.pairs", RAD_I32, R, K);
const int sel[1] = { rad_enc_find(&e, "t.pairs") };
std::vector<int32_t> pairs((size_t)(R * K));
for (int64_t r = 0; r < R; ++r)
for (int64_t c = 0; c < K; ++c) /* stage 0 reverses each group, stage 1 not */
pairs[(size_t)(r * K + c)] = (int32_t)((c / 128) * 128 + (r ? c % 128 : 127 - c % 128));
RadTensor t{};
t.data = pairs.data();
t.dtype = RAD_I32;
t.rank = 2;
t.shape[0] = R;
t.shape[1] = K;
t.stride[0] = K;
t.stride[1] = 1;
Opts kp;
kp.i("n", K).i("stages", 1).i("parts", 2);
RadLayout L{};
CHECK_EQ(r4d_rad_layout_givens_pairs(kp.data(), kp.n(), 2, &e, sel, &t, 1, &L), RAD_E_UNSUPPORTED);
REQUIRE_EQ(r4d_rad_layout_givens_pairs(kp.data(), kp.n(), 1, &e, sel, &t, 1, &L), RAD_OK);
REQUIRE_EQ(L.bytes, (int64_t)(R * K * 4));
std::vector<uint8_t> dst((size_t)L.bytes);
REQUIRE_EQ(r4d_rad_relayout_givens_pairs(kp.data(), kp.n(), 1, &e, sel, &t, 1, dst.data(), L.bytes),
RAD_OK);
CHECK_EQ(std::memcmp(dst.data(), pairs.data(), dst.size()), 0);
std::vector<int32_t> back((size_t)(R * K), -1);
RadTensor tb = t;
tb.data = back.data();
REQUIRE_EQ(r4d_rad_unrelayout_givens_pairs(kp.data(), kp.n(), 1, &e, sel, dst.data(), L.bytes, &tb, 1),
RAD_OK);
CHECK(back == pairs);
/* a pair leaving its group, and a column paired twice in a stage */
std::vector<int32_t> out = pairs, dup = pairs;
out[(size_t)(K + 5)] = 130;
dup[3] = dup[4];
for (std::vector<int32_t>* bad : { &out, &dup }) {
t.data = bad->data();
CHECK_EQ(r4d_rad_relayout_givens_pairs(kp.data(), kp.n(), 1, &e, sel, &t, 1, dst.data(), L.bytes),
RAD_E_FORMAT);
}
}