libref: givens_rotate (the rotation without the quantiser, f16 out)

This commit is contained in:
Malic Zilyas committed 2026-10-08 23:35:43 +02:00
1 parent e45bb596f4
commit ffa235bf0f
5 files changed
+30 -5

No files matched your search

+2
View File
@@ -101,6 +101,7 @@ 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_rotate(const RadArgs*, RadStream);
int ref_rmsnorm_quant_fp8 (const RadArgs*, RadStream);
int ref_gated_quant_fp8 (const RadArgs*, RadStream);
int ref_gemm_nt_q_gated (const RadArgs*, RadStream);
@@ -230,6 +231,7 @@ 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_rotate);
REF_SHAPE_FN(ref_rmsnorm_quant_fp8);
REF_SHAPE_FN(ref_gated_quant_fp8);
REF_SHAPE_FN(ref_gemm_nt_q_gated);
+14 -4
View File
@@ -427,10 +427,10 @@ int ref_had_quant_act_fp8(const RadArgs* a, RadStream) {
* 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. */
int ref_givens_quant_act_fp8(const RadArgs* a, RadStream) {
if (!have(a, 6)) return RAD_E_INVAL;
static int givens_body(const RadArgs* a, bool quant) {
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 = &a->t[5];
const RadTensor *q = &a->t[4], *s = quant ? &a->t[5] : nullptr;
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);
if (n <= 0 || S <= 0 || P <= 0 || (n & 1)) return RAD_E_SHAPE;
@@ -438,7 +438,7 @@ int ref_givens_quant_act_fp8(const RadArgs* a, RadStream) {
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 || numel(s) < rows * P * ngrp) return RAD_E_SHAPE;
if (rows <= 0 || numel(q) < rows * P * n || (quant && numel(s) < rows * P * ngrp)) 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,6 +464,10 @@ int ref_givens_quant_act_fp8(const RadArgs* a, RadStream) {
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;
@@ -486,6 +490,12 @@ int ref_givens_quant_act_fp8(const RadArgs* a, RadStream) {
return RAD_OK;
}
int ref_givens_quant_act_fp8(const RadArgs* a, RadStream) { return givens_body(a, true); }
/* 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
*
* h[k1][k2] += sum over rows of x[m][k1] * x[m][k2], where x is the DEQUANTISED E4M3 input, or
+9
View File
@@ -206,6 +206,8 @@ static const RadOperandSpec oQuantFp8[] = { OPD("x"), OUT("q"), OUT("scale") };
* 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"),
P_INT("stages"), P_INT("parts") };
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") };
@@ -809,6 +811,10 @@ static const RadOpSchema kSchemas[] = {
"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_rotate", ARR(pGivensQuant), ARR(oGivensRotate),
"givens_quant_act_fp8 without the 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),
"A Walsh-Hadamard rotation of width `group` along n, then symmetric E4M3 per group: scale = "
"amax(rotated) / 448, shaped [M, n/group] in f32. n must be a multiple of `group` -- a rotation "
@@ -1171,6 +1177,9 @@ SIMPLE("ref_gemm_nt_q_gated", "gemm_nt_q_gated", "gemm",
SIMPLE("ref_dflash_select", "dflash_select", "draft",
"DFlash2's greedy walk over the candidate lattice",
"i32 candidates, bf16 codebooks, i32 tokens out", ref_dflash_select),
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",
+4
View File
@@ -836,6 +836,10 @@ int ref_givens_quant_act_fp8_shape(const RadParam*, int, int, RadOpdDesc*) {
return RAD_E_UNSUPPORTED;
}
int ref_givens_rotate_shape(const RadParam*, int, int, RadOpdDesc*) {
return RAD_E_UNSUPPORTED; /* a pairing cannot be drawn: see givens_quant_act_fp8 */
}
int ref_had_quant_act_fp8_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
const uint32_t D = act_dt(p, n_p);
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
+1 -1
View File
@@ -261,7 +261,7 @@ TEST(op_vocabulary) {
"embed_lookup_q", "cast",
"scale_rows",
"softmax",
"quant_act_i8", "quant_act_fp8", "had_quant_act_fp8", "givens_quant_act_fp8", "had_quant_act_i8",
"quant_act_i8", "quant_act_fp8", "had_quant_act_fp8", "givens_quant_act_fp8", "givens_rotate", "had_quant_act_i8",
"gram_accum",
/* The fused fp8 forms. A host run needs EVERY op covered, so these are what stands
* between a host-backend run and "site 'host' is unavailable for this band". */