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

+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",