ParoQuant: drop the sequential rotate-and-quantise ops (givens_quant_act_fp8 / givens_quant_act_i8g): givens_dense_quant replaces them; the rotate-only f16 kernel keeps its own file

This commit is contained in:
Malic Zilyas committed 2026-10-08 23:35:43 +02:00
1 parent c17f5fdc89
commit ec65b8cbeb
18 files changed
+318 -486

No files matched your search

-4
View File
@@ -100,8 +100,6 @@ int ref_quant_act_i8 (const RadArgs*, RadStream);
int ref_had_quant_act_i8 (const RadArgs*, RadStream);
int ref_quant_act_fp8 (const RadArgs*, RadStream);
int ref_had_quant_act_fp8 (const RadArgs*, RadStream);
int ref_givens_quant_act_fp8(const RadArgs*, RadStream);
int ref_givens_quant_act_i8g(const RadArgs*, RadStream);
int ref_givens_dense_quant(const RadArgs*, RadStream);
int ref_givens_rotate(const RadArgs*, RadStream);
int ref_rmsnorm_quant_fp8 (const RadArgs*, RadStream);
@@ -232,8 +230,6 @@ REF_SHAPE_FN(ref_quant_act_i8);
REF_SHAPE_FN(ref_had_quant_act_i8);
REF_SHAPE_FN(ref_quant_act_fp8);
REF_SHAPE_FN(ref_had_quant_act_fp8);
REF_SHAPE_FN(ref_givens_quant_act_fp8);
REF_SHAPE_FN(ref_givens_quant_act_i8g);
REF_SHAPE_FN(ref_givens_dense_quant);
REF_SHAPE_FN(ref_givens_rotate);
REF_SHAPE_FN(ref_rmsnorm_quant_fp8);
+18 -50
View File
@@ -410,35 +410,31 @@ int ref_had_quant_act_fp8(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------- givens_quant_act_fp8 */
/* ParoQuant's activation side: every row scaled by its channel factors, rotated by `stages`
* rounds of disjoint Givens pairs, then the same symmetric block-scaled E4M3 quantiser. A weight
* stored as v = w . D . G_1 ... G_S (abi/rad_encoding.h, "givens") meets the activation
* x . D^-1 . G_1 ... G_S, because <v, x D^-1 G_1..G_S> = <w, x> and each G is orthogonal.
/* ------------------------------------------------------------- givens_rotate */
/* ParoQuant's activation side: every row scaled by its channel factors and rotated by `stages` rounds of
* disjoint Givens pairs, written in y's dtype. A weight stored as v = w . D . G_1 ... G_S
* (abi/rad_encoding.h, "givens") meets the activation x . D^-1 . G_1 ... G_S, because
* <v, x D^-1 G_1..G_S> = <w, x> and each G is orthogonal. For a weight whose GEMM reads a 16-bit
* activation (w5a16); the quantising form is givens_dense_quant, over the composed matrices.
*
* PARTS. A fused projection (gate|up, q|k|v) was rotated per piece, so a row has `parts` rotated
* copies, written side by side: q is [rows, parts * n] and scale [rows, parts * n/group], part p at
* columns [p*n, (p+1)*n). The tables stack the parts: pairs and angle `stages` rows each, scale one
* row, per part. A part's pairs address columns of the row, and a pair must stay inside one scale
* group (the checkpoint's are within groups of 128): the quantiser reads each group's rotated
* values after the stages and the device kernel rotates a group at a time.
* copies, written side by side: y is [rows, parts * n], part p at columns [p*n, (p+1)*n). The tables
* stack the parts: pairs and angle `stages` rows each, scale one row, per part. A part's pairs address
* columns of the row and stay inside one group of `group` (the checkpoint's are within groups of 128).
*
* The arithmetic is the device kernel's, restated: the divide by the channel scale, then for each
* stage a' = a cos t + b sin t, b' = b cos t - a sin t in f32, then amax/448 per group. Not
* bit-identical to the device on the angle: cos and sin of an f32 differ by an ulp between libm
* and the GPU's. */
static int givens_body(const RadArgs* a, bool quant, bool i8 = false) {
if (!have(a, quant ? 6 : 5)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *pr = &a->t[1], *an = &a->t[2], *sc = &a->t[3];
const RadTensor *q = &a->t[4], *s = quant ? &a->t[5] : nullptr;
* stage a' = a cos t + b sin t, b' = b cos t - a sin t in f32. Not bit-identical to the device on the
* angle: cos and sin of an f32 differ by an ulp between libm and the GPU's. */
int ref_givens_rotate(const RadArgs* a, RadStream) {
if (!have(a, 5)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *pr = &a->t[1], *an = &a->t[2], *sc = &a->t[3], *q = &a->t[4];
const int64_t n = p_int(a, "n", 0), S = p_int(a, "stages", 0), P = p_int(a, "parts", 1);
int64_t g = p_int(a, "group", 0);
const int64_t g = p_int(a, "group", 0);
if (n <= 0 || S <= 0 || P <= 0 || (n & 1)) return RAD_E_SHAPE;
if (g <= 0 || g > n || n % g != 0) return RAD_E_SHAPE;
if (numel(pr) < P * S * n || numel(an) < P * S * (n / 2) || numel(sc) < P * n) return RAD_E_SHAPE;
const int64_t rows = numel(x) / n;
const int64_t ngrp = n / g;
if (rows <= 0 || numel(q) < rows * P * n || (quant && numel(s) < rows * P * ngrp)) return RAD_E_SHAPE;
if (rows <= 0 || numel(q) < rows * P * n) return RAD_E_SHAPE;
// validated before the loop, because an OpenMP body cannot return
for (int64_t i = 0; i < P * S * n; ++i) {
@@ -464,38 +460,13 @@ static int givens_body(const RadArgs* a, bool quant, bool i8 = false) {
buf[(size_t)ib] = vb * c - va * sn;
}
}
if (!quant) { // givens_rotate: the rotated row, in q's dtype
for (int64_t k = 0; k < n; ++k) st_dt(q->dtype, q->data, qb + (p * n + k) * qs, buf[(size_t)k]);
continue;
}
for (int64_t gi = 0; gi < ngrp; ++gi) {
const int64_t k0 = gi * g;
float amax = 0.0f;
for (int64_t i = 0; i < g; ++i) {
const float m = std::fabs(buf[(size_t)(k0 + i)]);
if (m > amax) amax = m;
}
const float qmax = i8 ? 127.0f : 448.0f;
const float scl = amax > 0.0f ? amax * (1.0f / qmax) : 1.0f;
const float inv = 1.0f / scl;
for (int64_t i = 0; i < g; ++i) {
float v = buf[(size_t)(k0 + i)] * inv;
if (v > qmax) v = qmax;
if (v < -qmax) v = -qmax;
if (i8) v = std::nearbyint(v);
st_dt(q->dtype, q->data, qb + (p * n + k0 + i) * qs, v);
}
stt(s, rowoff(s, r, P * ngrp) + (p * ngrp + gi) * laststride(s), scl);
}
for (int64_t k = 0; k < n; ++k) st_dt(q->dtype, q->data, qb + (p * n + k) * qs, buf[(size_t)k]);
}
}
return RAD_OK;
}
int ref_givens_quant_act_fp8(const RadArgs* a, RadStream) { return givens_body(a, true); }
int ref_givens_quant_act_i8g(const RadArgs* a, RadStream) { return givens_body(a, true, true); }
/* The same activation with the rotation applied as the dense matrix: y[j] = sum_i T[j][i] * x[i] / D[i] over
/* ParoQuant's activation quantiser: the rotation applied as the dense matrix, y[j] = sum_i T[j][i] * x[i] / D[i] over
* each (part, 128-column group), then the quantiser `q`'s dtype names. T is the weight, f16, one [128, 128]
* matrix a (part, group) -- what r4d_rad_relayout_gdense composes from the givens tables. */
int ref_givens_dense_quant(const RadArgs* a, RadStream) {
@@ -541,9 +512,6 @@ int ref_givens_dense_quant(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* The same channel scale and rotations with NO quantiser: y = the rotated row in y's dtype, `parts`
* copies side by side. For a weight whose GEMM reads a 16-bit activation (w5a16). */
int ref_givens_rotate(const RadArgs* a, RadStream) { return givens_body(a, false); }
/* ---------------------------------------------------------------- the Gram accumulator
*
+5 -28
View File
@@ -201,10 +201,10 @@ static const RadOperandSpec oHadQuant[] = { OPD("x"), OUT("q"), OUT("scale") };
/* No `asum`: that output exists for the asymmetric integer grids and an fp8 grid is symmetric by
* construction (libr4d/r4d_rows.cpp says the same). */
static const RadOperandSpec oQuantFp8[] = { OPD("x"), OUT("q"), OUT("scale") };
/* ParoQuant's activation quantiser: a rotation per part in front of quant_act_fp8's. The three
/* ParoQuant's rotation: a rotation per part, to f16 for the 16-bit GEMMs. The three
* tables are the "givens" encoding's (abi/rad_encoding.h) and are WEIGHT operands, so they are
* resolved, moved and placed with the weight they belong to. */
static const RadParamSpec pGivensQuant[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_STR("dtype"),
static const RadParamSpec pGivensRotate[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_STR("dtype"),
P_INT("stages"), P_INT("parts") };
static const RadParamSpec pGivensDense[] = { P_INT("M"), P_INT("n"), P_INT("group"), P_STR("dtype"),
P_INT("parts"), P_STR("out") };
@@ -212,8 +212,6 @@ static const RadOperandSpec oGivensDense[] = { OPD("x"), WGT("tdense"), WGT("rsc
OUT("scale") };
static const RadOperandSpec oGivensRotate[] = { OPD("x"), WGT("pairs"), WGT("angle"), WGT("rscale"),
OUT("y") };
static const RadOperandSpec oGivensQuant[] = { OPD("x"), WGT("pairs"), WGT("angle"), WGT("rscale"),
OUT("q"), OUT("scale") };
/* ---- the fp8 fusions ----------------------------------------------------------------------
* A fused op's operands are the unfused pair's minus the tensor between them, and `out_bf16`
@@ -806,28 +804,15 @@ static const RadOpSchema kSchemas[] = {
"previous pick. `hp` absent is a plane of ones, which drops the form to a plain low-rank "
"bigram. Serial in l by construction -- the predecessor is not known until the previous step "
"has been decided." },
{ "givens_quant_act_fp8", ARR(pGivensQuant), ARR(oGivensQuant),
"ParoQuant's activation quantiser: x is divided by the channel scale `rscale`, rotated by "
"`stages` rounds of disjoint Givens pairs (a' = a cos t + b sin t, b' = b cos t - a sin t, pairs "
"as column indices of the row, each inside one scale group), then quantised as quant_act_fp8 "
"does: scale = amax / 448 per `group`. `parts` rotated copies are written side by side -- q is "
"[M, parts * n], scale [M, parts * n / group] -- part p from table rows [p * stages, (p + 1) * "
"stages) of `pairs` and `angle` and row p of `rscale`: a fused projection's pieces were each "
"rotated on their own. Only a weight stored with the \"givens\" transform reads the result; "
"it is NOT quant_act_fp8's output in either direction." },
{ "givens_dense_quant", ARR(pGivensDense), ARR(oGivensDense),
"ParoQuant's activation quantiser with the rotation as a dense per-group matrix: y = T (x / "
"rscale) for each of `parts` rotated copies, T the [128, 128] f16 matrix a (part, 128-column "
"group) that its Givens stages compose to, then the block-scaled quantiser `out` names "
"(\"i8\": scale amax / 127, round to nearest; \"fp8\": E4M3, amax / 448). q is [M, parts * n] "
"and scale [M, parts * n / group]; tdense is [parts * n, 128]. The same activation as "
"givens_quant_act_i8g / _fp8 up to the f16 rounding of T." },
{ "givens_quant_act_i8g", ARR(pGivensQuant), ARR(oGivensQuant),
"givens_quant_act_fp8 with an int8 output: the same channel scale and rotations, then "
"quant_act_i8g's quantiser (scale = amax / 127 per `group`, round to nearest). For the int8 "
"prefill GEMMs of a weight stored with the \"givens\" transform." },
{ "givens_rotate", ARR(pGivensQuant), ARR(oGivensRotate),
"givens_quant_act_fp8 without the quantiser: y = x / rscale rotated by `stages` rounds of Givens "
"the sequential rotation, up to the f16 rounding of T." },
{ "givens_rotate", ARR(pGivensRotate), ARR(oGivensRotate),
"ParoQuant's rotation without a quantiser: y = x / rscale rotated by `stages` rounds of Givens "
"pairs, `parts` copies side by side ([M, parts * n]) in y's own dtype. For a weight whose GEMM "
"reads a 16-bit activation. `group` is the width a pair stays inside." },
{ "had_quant_act_fp8", ARR(pQuant), ARR(oQuantFp8),
@@ -1195,18 +1180,10 @@ SIMPLE("ref_dflash_select", "dflash_select", "draft",
SIMPLE("ref_givens_rotate", "givens_rotate", "quant",
"channel scale and Givens rotations per part, in the output's dtype",
"float in, float out; int32 pairs, f32 angles and channel scales", ref_givens_rotate),
SIMPLE("ref_givens_quant_act_fp8", "givens_quant_act_fp8", "quant",
"channel scale and Givens rotations per part, then the block-scaled E4M3 quantiser",
"float in, fp8_e4m3 out, f32 scale per group; int32 pairs, f32 angles and channel scales",
ref_givens_quant_act_fp8),
SIMPLE("ref_givens_dense_quant", "givens_dense_quant", "quant",
"the rotation as a dense per-group matrix, then the block-scaled int8 or E4M3 quantiser",
"float in, int8 or fp8_e4m3 out, f32 scale per group; f16 matrices and f32 channel scales",
ref_givens_dense_quant),
SIMPLE("ref_givens_quant_act_i8g", "givens_quant_act_i8g", "quant",
"channel scale and Givens rotations per part, then the block-scaled int8 quantiser",
"float in, int8 out, f32 scale per group; int32 pairs, f32 angles and channel scales",
ref_givens_quant_act_i8g),
SIMPLE("ref_had_quant_act_fp8", "had_quant_act_fp8", "quant",
"width-`group` Hadamard, then the same symmetric block-scaled E4M3 quantiser",
"float in, fp8_e4m3 out, f32 scale per group; n a multiple of group",
+2 -10
View File
@@ -832,20 +832,12 @@ int ref_gram_accum_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* ou
* stage, the partner inside the group), and a described operand can only be drawn, not constructed
* -- an index draw has repeats, and a Givens stage over a repeated column is not a rotation. A case
* built from one would compare two implementations of nothing. r4d_selftest builds real pairings. */
int ref_givens_quant_act_fp8_shape(const RadParam*, int, int, RadOpdDesc*) {
return RAD_E_UNSUPPORTED;
}
int ref_givens_quant_act_i8g_shape(const RadParam*, int, int, RadOpdDesc*) {
return RAD_E_UNSUPPORTED; /* a pairing cannot be drawn: see givens_quant_act_fp8 */
}
int ref_givens_dense_quant_shape(const RadParam*, int, int, RadOpdDesc*) {
return RAD_E_UNSUPPORTED; /* a composed rotation cannot be drawn: see givens_quant_act_fp8 */
return RAD_E_UNSUPPORTED; /* a composed rotation cannot be drawn: it is a weight, built from real pairings */
}
int ref_givens_rotate_shape(const RadParam*, int, int, RadOpdDesc*) {
return RAD_E_UNSUPPORTED; /* a pairing cannot be drawn: see givens_quant_act_fp8 */
return RAD_E_UNSUPPORTED; /* a pairing cannot be drawn: a described operand can only be drawn, and an index draw has repeats */
}
int ref_had_quant_act_fp8_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {