ParoQuant int5/int4: the review fixes of PR 1 carried to the 5-/4-bit path

* givens_rotate and givens_quant_act_i8g take the same checked `pairs` relayout as givens_quant_act_fp8.
* gemm_w4a8i8/w5a8i8_tiled: one re-tile slab for every part, as gemm_mxfp4a8_tiled (they share its
  scratch hook, which is one slab now).
* LinearFP8::decl_gemm_q: the one gemm_nt_q declaration, pb1/pb2 only with parts, for the MXFP4, the
  skinny 5-/4-bit and the int8 prefill GEMMs.
This commit is contained in:
Malic Zilyas committed 2026-10-08 23:35:43 +02:00
1 parent 6091039926
commit 3491aca6fe
3 files changed
+33 -44

No files matched your search

+24 -37
View File
@@ -764,11 +764,16 @@ struct LinearFP8 {
/* THE ROTATION OF A ROTATED WEIGHT, declared: its tables as views of the weight's own planes, its
* code planes (`givens_quant_act_fp8`: E4M3 and a scale) or, with `f16`, its single f16 plane
* (`givens_rotate`, for a GEMM that reads a 16-bit activation), and the op in front of the
* GEMM. `cut` is the rows of each rotated part; *pb1/*pb2 come back as the GEMM's part bounds. */
* GEMM. `cut` is the rows of each rotated part; the GEMM's part bounds come back in pb1 and pb2. */
int declare_rotation(RadBuilder* b, Names& nm, const Geom& g, const char* base, const char* wn,
const RadEncoding& enc, int64_t N, int64_t K, RadWeightGroup grp,
const ActFP8& a, int64_t max_rows, const std::vector<int64_t>& cut,
bool f16, int64_t* pb1, int64_t* pb2);
/* gemm_nt_q over this weight, M in [lo, hi]. `pb1`/`pb2` go only to a weight with rotated
* parts, so a one-part op keeps the plain op's geometry -- the key the tune cache is read by. */
rad_op decl_gemm_q(RadBuilder* b, int64_t lo, int64_t hi, int64_t group, const char* dtype,
int64_t pb1, int64_t pb2, std::initializer_list<rad_weight> wts,
std::initializer_list<rad_buf> reads, rad_buf y) const;
void step(RadCtx* c, const ActFP8& a, rad_buf y, int64_t T, int64_t r0 = 0,
int64_t rows = 0) const;
/* A SPAN PER STACKED ROW PART (rad_weight_shard_span_parts), in the weight's rows; the scale
@@ -875,6 +880,17 @@ inline int LinearFP8::probe(RadBuilder* b, Names& nm, const Geom& g, const char*
return RAD_OK;
}
inline rad_op LinearFP8::decl_gemm_q(RadBuilder* b, int64_t lo, int64_t hi, int64_t group,
const char* dtype, int64_t pb1, int64_t pb2,
std::initializer_list<rad_weight> wts,
std::initializer_list<rad_buf> reads, rad_buf y) const {
const RadParam ps[] = { RAD_RANGE("M", lo, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", group), RAD_STR("dtype", dtype),
RAD_INT("pb1", pb1), RAD_INT("pb2", pb2) };
const int n_ps = (int)(sizeof ps / sizeof ps[0]) - (rot_parts > 1 ? 0 : 2);
return rw(b, rad_decl_op(b, "gemm_nt_q", ps, n_ps, wts.begin(), (int)wts.size()), reads, { y });
}
inline int LinearFP8::declare_rotation(RadBuilder* b, Names& nm, const Geom& g, const char* base,
const char* wn, const RadEncoding& enc, int64_t N, int64_t K,
RadWeightGroup grp, const ActFP8& a, int64_t max_rows,
@@ -1093,20 +1109,8 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
ws = decl_view(b, nm.f("%s.scale", base), wn, "scale,zero", RAD_F16, { N, K },
RAD_ACCESS_PER_TOKEN, shard, grp);
if (!w || !ws) return RAD_E_INVAL;
op = rot_parts > 1
? rw(b, RAD_OP(b, "gemm_nt_q",
RAD_PARAMS(RAD_RANGE("M", 1, band ? kSplit : hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_FP8_BLOCK),
RAD_STR("dtype", bits5 ? "w5a16" : "w4a16_asym"),
RAD_INT("pb1", pb1), RAD_INT("pb2", pb2)),
RAD_WEIGHTS(w, ws)),
{ rot_q }, { y })
: rw(b, RAD_OP(b, "gemm_nt_q",
RAD_PARAMS(RAD_RANGE("M", 1, band ? kSplit : hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_FP8_BLOCK),
RAD_STR("dtype", bits5 ? "w5a16" : "w4a16_asym")),
RAD_WEIGHTS(w, ws)),
{ rot_q }, { y });
op = decl_gemm_q(b, 1, band ? kSplit : hi, RAD_FP8_BLOCK, bits5 ? "w5a16" : "w4a16_asym",
pb1, pb2, { w, ws }, { rot_q }, y);
if (!op) return RAD_E_INVAL;
if (band) {
/* THE PREFILL BAND: rows above the skinny kernel's ceiling rotate to int8 and meet the int8
@@ -1124,18 +1128,8 @@ inline int LinearFP8::declare(RadBuilder* b, Names& nm, const Geom& g, const cha
RAD_INT("stages", S), RAD_INT("parts", P)),
RAD_WEIGHTS(rot_pairs, rot_angle, rot_scale)),
{ a.x }, { rot_q2, rot_s2 });
op2 = rot_parts > 1
? rw(b, RAD_OP(b, "gemm_nt_q",
RAD_PARAMS(RAD_RANGE("M", kSplit + 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", bits5 ? "w5a8i8" : "w4a8i8_asym"),
RAD_INT("pb1", pb1), RAD_INT("pb2", pb2)),
RAD_WEIGHTS(w, ws)),
{ rot_q2, rot_s2 }, { y })
: rw(b, RAD_OP(b, "gemm_nt_q",
RAD_PARAMS(RAD_RANGE("M", kSplit + 1, hi), RAD_INT("N", N), RAD_INT("K", K),
RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", bits5 ? "w5a8i8" : "w4a8i8_asym")),
RAD_WEIGHTS(w, ws)),
{ rot_q2, rot_s2 }, { y });
op2 = decl_gemm_q(b, kSplit + 1, hi, RAD_FP8_BLOCK, bits5 ? "w5a8i8" : "w4a8i8_asym",
pb1, pb2, { w, ws }, { rot_q2, rot_s2 }, y);
if (!op_rot2 || !op2) return RAD_E_INVAL;
}
return RAD_OK;
@@ -1224,16 +1218,9 @@ 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;
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 });
/* ONE GEMM for both: a rotated weight reads its own rotated pair, a plain one the producer's. */
op = decl_gemm_q(b, 1, max_rows > 0 ? max_rows : g.max_tok, RAD_MX_GROUP, "mxfp4a8", pb1, pb2,
{ w, ws, wref }, { paro ? rot_q : a.q, paro ? rot_s : a.s }, y);
return op ? RAD_OK : RAD_E_INVAL;
}
+3 -3
View File
@@ -239,7 +239,7 @@ static void wpa8i8_launch(long at, long ascale, long wq, long whi, long wsz, lon
}
// ============================================================== the plugin ABI entry points
// The row-major int8 activation is re-tiled into the op's scratch (one slab a part), then the GEMM
// The row-major int8 activation is re-tiled into the op's scratch (one slab, the parts in turn), then the GEMM
// reads it from there -- mxfp4a8_tiled's arrangement exactly, including a partitioned weight as
// one launch per part over windows of the planes.
template <int BITS>
@@ -258,11 +258,11 @@ static int wpa8i8_dispatch(const RadArgs* a, RadStream s) {
if (rc != RAD_OK) return rc;
if (as_group != 128) return R4D_NO(RAD_E_SHAPE); // one scale a (row, 128): the slab
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);
const long long kb = g.K / 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_wpa8i8_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));
+6 -4
View File
@@ -5254,8 +5254,9 @@ static const RadKernelInfo kKernels[] = {
"scales",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cHadQuantActFp8), NOTUNE,
nullptr, nullptr, r4d_rad_givens_quant_act_i8g, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, r4d_rad_givens_quant_act_i8g_describe },
nullptr, nullptr, r4d_rad_givens_quant_act_i8g, nullptr, r4d_rad_layout_givens_pairs,
r4d_rad_relayout_givens_pairs, nullptr, r4d_rad_unrelayout_givens_pairs, nullptr,
r4d_rad_givens_quant_act_i8g_describe },
{ "givens_rotate", "givens_rotate", "quant",
"the channel scale and ParoQuant's Givens rotations, once for each rotated part, to f16",
@@ -5264,8 +5265,9 @@ static const RadKernelInfo kKernels[] = {
"f32 channel scales",
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
ROW(cHadQuantActFp8), NOTUNE,
nullptr, nullptr, r4d_rad_givens_rotate, nullptr, nullptr, nullptr,
nullptr, nullptr, nullptr, r4d_rad_givens_rotate_describe },
nullptr, nullptr, r4d_rad_givens_rotate, nullptr, r4d_rad_layout_givens_pairs,
r4d_rad_relayout_givens_pairs, nullptr, r4d_rad_unrelayout_givens_pairs, nullptr,
r4d_rad_givens_rotate_describe },
{ "quant_act_i8g", "quant_act_i8g", "quant",
"per-(row, 128-column) symmetric int8 quantisation of a bf16 activation",