libref, libavx: had_quant_act_i8g, the activation of the i8a8h GEMMs

had_quant_act_fp8 at int8 codes (top 127), one body shared by both in each library, so a
rotated int8 trunk runs on the host kernels too. gemm_nt_q needs no change: both libraries pick
the int8 product from the operands' dtypes, and i8a8h's rotation is in the values. libref's
operand description takes i8a8h, and rad-avx-check gains cases for the op and the dtype.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
eddie168168andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-19 10:42:13 +02:00
1 parent 037427e67e
commit 61afe50b96
10 files changed
+84 -11

No files matched your search

+1 -1
View File
@@ -78,7 +78,7 @@ Every `RAD_E_UNSUPPORTED` a libavx kernel returns, beside libref's answer to the
| `rope` | YaRN with `axial` or a position scale | the same |
| `rope` | Llama 3 scaling beside YaRN, with `axial`, or over multi-component positions | the same |
| `rope` | a rotary half-width over 1024 | the same |
| `had_quant_act_i8`, `had_quant_act_fp8`, `rmsnorm_had_quant_i8`, `gated_had_quant_i8`, `hc_read` | a Hadamard group over 4096 | the same (`REF_HAD_MAX`) |
| `had_quant_act_i8`, `had_quant_act_fp8`, `had_quant_act_i8g`, `rmsnorm_had_quant_i8`, `gated_had_quant_i8`, `hc_read` | a Hadamard group over 4096 | the same (`REF_HAD_MAX`) |
| `gdn_conv_prep`, `gdn_conv_update` | a convolution history over 16 | the same |
| `gdn_recurrent_update` | a `state_form` other than `per_candidate` or `anchor` | the same |
| `ple_conv`, `causal_conv1d` | a history over 64 | the same |
+9 -2
View File
@@ -153,6 +153,8 @@ std::vector<Case> build_cases() {
HS("dtype", "bf16") }, EB(5, 256, 2, 1.5));
add("had_quant_act_fp8", { HI("M", 5), HI("n", 256), HI("group", group),
HS("dtype", "bf16") });
add("had_quant_act_i8g", { HI("M", 5), HI("n", 256), HI("group", group),
HS("dtype", "bf16") });
add("had_quant_act_i8", { HI("M", 5), HI("n", 256), HI("group", group),
HS("dtype", "bf16") });
add("dequant", { HI("M", 5), HI("n", 256), HI("group", group),
@@ -221,11 +223,14 @@ std::vector<Case> build_cases() {
add("gemm_nt_q_bias", { HI("M", M), HI("N", 96), HI("K", 256), HI("group", 128),
HS("dtype", dt) }, 0, fl);
}
/* The int8 trunk (row-major codes, a scale per row per 128 of K on both sides) and OCP-MXFP4
* (e2m1 codes with an E8M0 exponent per 32 of K against the E4M3 activation). */
/* The int8 trunk (row-major codes, a scale per row per 128 of K on both sides; i8a8h is the
* same operands over a rotated weight) and OCP-MXFP4 (e2m1 codes with an E8M0 exponent per 32
* of K against the E4M3 activation). */
for (int M : { 1, 65 }) {
add("gemm_nt_q", { HI("M", M), HI("N", 96), HI("K", 256), HI("group", 128),
HS("dtype", "i8a8") }, 0, 2.0 * M * 96 * 256);
add("gemm_nt_q", { HI("M", M), HI("N", 96), HI("K", 256), HI("group", 128),
HS("dtype", "i8a8h") }, 0, 2.0 * M * 96 * 256);
add("gemm_nt_q", { HI("M", M), HI("N", 96), HI("K", 256), HI("group", 32),
HS("dtype", "mxfp4a8") }, 0, 2.0 * M * 96 * 256);
}
@@ -961,6 +966,8 @@ std::vector<Case> build_cases() {
HS("dtype", "bf16") }, (double)M * 2048 * 3, 0);
speed("quant_act_i8g", { HI("M", M), HI("n", 2048), HI("group", 128),
HS("dtype", "bf16") }, (double)M * 2048 * 3, 0);
speed("had_quant_act_i8g", { HI("M", M), HI("n", 2048), HI("group", 128),
HS("dtype", "bf16") }, (double)M * 2048 * 3, 0);
speed("had_quant_act_i8", { HI("M", M), HI("n", 2048), HI("group", 128),
HS("dtype", "bf16") }, (double)M * 2048 * 3, 0);
}
+1 -1
View File
@@ -83,7 +83,7 @@ int avx_par_force(int threads);
/* ---- quantisation and rotation */ \
X(quant_act_i8) X(quant_act_i8g) X(had_quant_act_i8) X(quant_act_fp8) \
X(had_quant_act_fp8) X(gram_accum) X(rmsnorm_had_quant_i8) X(gated_had_quant_i8) \
X(dequant) X(quant_cols_fp8) \
X(dequant) X(quant_cols_fp8) X(had_quant_act_i8g) \
X(rmsnorm_quant_fp8) X(gated_quant_fp8) X(givens_rotate) X(givens_dense_quant) \
/* ---- gemm and the vocabulary edges */ \
X(gemm_nt) X(gemm_nt_bias) X(gemm_nt_q) X(gemm_nt_q_bias) \
+13 -3
View File
@@ -537,8 +537,10 @@ AVX_KERNEL(gated_quant_fp8) {
* is scaled by.
*
* Unlike had_quant_act_i8, 1/sqrt(g) is not applied here. <Hw, Ha> = g <w, a>, and the whole g is
* divided out of the weight scale instead, where dividing a bf16 by a power of two is exact. */
AVX_KERNEL(had_quant_act_fp8) {
* divided out of the weight scale instead, where dividing a bf16 by a power of two is exact.
*
* had_quant_act_i8g is the same at int8 codes (`top` 127), for the i8a8h weight. */
static int had_quant_q8(const RadArgs* a, float top) {
if (!rad_args_have(a, 3)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *q = &a->t[1], *s = &a->t[2];
const int64_t n = p_int(a, "n", 0);
@@ -561,7 +563,7 @@ AVX_KERNEL(had_quant_act_fp8) {
fwht_row(buf, n, g);
for (int64_t gi = 0; gi < ngrp; ++gi) {
float sc;
fp8_group(buf, out, gi * g, gi * g + g, &sc);
fp8_group(buf, out, gi * g, gi * g + g, &sc, top);
stt(s, rowoff(s, r, ngrp) + gi * laststride(s), sc);
}
out_row(q, rowoff(q, r, n), n, out);
@@ -569,6 +571,14 @@ AVX_KERNEL(had_quant_act_fp8) {
return RAD_OK;
}
AVX_KERNEL(had_quant_act_fp8) {
return had_quant_q8(a, 448.0f);
}
AVX_KERNEL(had_quant_act_i8g) {
return had_quant_q8(a, 127.0f);
}
/* ------------------------------------------------------------- givens_rotate */
/* ParoQuant's activation side for a 16-bit GEMM: each row is divided by its channel scales and
* rotated by `stages` rounds of disjoint Givens pairs, once per part, parts side by side in y.
+7
View File
@@ -772,6 +772,9 @@ static const RadOpSchema kSchemas[] = {
{ "had_quant_act_fp8", ARR(pQuant), ARR(oQuantFp8),
"width-`group` Hadamard, then the same symmetric block-scaled E4M3 quantiser. No normalisation: "
"the 1/group is folded into the stored weight scale at convert (docs/OPS.md)." },
{ "had_quant_act_i8g", ARR(pQuant), ARR(oQuantFp8),
"width-`group` Hadamard, then quant_act_i8g's symmetric int8 per group. No normalisation: the "
"1/group is the i8a8h weight's stored scale (docs/OPS.md)." },
{ "gram_accum", ARR(pQuant), ARR(oGram),
"h += x^T x over a dequantised E4M3 activation. `h` is [n, n] and INOUT." },
{ "givens_dense_quant", ARR(pGivensDense), ARR(oGivensDense),
@@ -1244,6 +1247,10 @@ SIMPLE("avx_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",
L(had_quant_act_fp8)),
SIMPLE("avx_had_quant_act_i8g", "had_quant_act_i8g", "quant",
"width-`group` Hadamard, then the symmetric int8 quantiser, one scale per group",
"float in, i8 out, f32 scale per group; n a multiple of group",
L(had_quant_act_i8g)),
SIMPLE("avx_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", L(givens_rotate)),
+2
View File
@@ -103,6 +103,7 @@ int ref_had_quant_act_i8 (const RadArgs*, RadStream);
int ref_quant_act_fp8 (const RadArgs*, RadStream);
int ref_quant_cols_fp8 (const RadArgs*, RadStream);
int ref_had_quant_act_fp8 (const RadArgs*, RadStream);
int ref_had_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);
@@ -309,6 +310,7 @@ REF_SHAPE_FN(ref_had_quant_act_i8);
REF_SHAPE_FN(ref_quant_act_fp8);
REF_SHAPE_FN(ref_quant_cols_fp8);
REF_SHAPE_FN(ref_had_quant_act_fp8);
REF_SHAPE_FN(ref_had_quant_act_i8g);
REF_SHAPE_FN(ref_givens_dense_quant);
REF_SHAPE_FN(ref_givens_rotate);
REF_SHAPE_FN(ref_rmsnorm_quant_fp8);
+14 -3
View File
@@ -360,8 +360,11 @@ int ref_quant_cols_fp8(const RadArgs* a, RadStream) {
* ~0.2% error per group (a fifth of the int4 grid's own error).
*
* The output is not interchangeable with quant_act_fp8's: only a weight plane stored as w4a8h
* carries the matching rotation and /g. */
int ref_had_quant_act_fp8(const RadArgs* a, RadStream) {
* carries the matching rotation and /g.
*
* had_quant_act_i8g is the same at int8 codes (`top` 127), for the i8a8h weight, whose scale
* carries the /g the same way. */
static int had_quant_act_q8(const RadArgs* a, float top) {
if (!have(a, 3)) return RAD_E_INVAL;
const RadTensor *x = &a->t[0], *q = &a->t[1], *s = &a->t[2];
const int64_t n = p_int(a, "n", 0);
@@ -383,7 +386,7 @@ int ref_had_quant_act_fp8(const RadArgs* a, RadStream) {
const int64_t k0 = gi * g;
for (int64_t i = 0; i < g; ++i) buf[i] = ldt(x, xb + (k0 + i) * xs);
fwht(buf, g);
const float sc = q8_group([&](int64_t i) { return buf[i]; }, g, 448.0f, q,
const float sc = q8_group([&](int64_t i) { return buf[i]; }, g, top, q,
qb + k0 * qs, qs);
stt(s, rowoff(s, r, ngrp) + gi * laststride(s), sc);
}
@@ -391,6 +394,14 @@ int ref_had_quant_act_fp8(const RadArgs* a, RadStream) {
return RAD_OK;
}
int ref_had_quant_act_fp8(const RadArgs* a, RadStream) {
return had_quant_act_q8(a, 448.0f);
}
int ref_had_quant_act_i8g(const RadArgs* a, RadStream) {
return had_quant_act_q8(a, 127.0f);
}
/* ------------------------------------------------------------- givens_rotate */
/* ParoQuant's activation side: each row divided 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
+10
View File
@@ -1062,6 +1062,12 @@ static const RadOpSchema kSchemas[] = {
"amax(rotated) / 448, shaped [M, n/group] in f32. n must be a multiple of `group` -- a rotation "
"block cannot straddle the end of a row. NOT interchangeable with quant_act_fp8 in either "
"direction: the consumer must be a weight that was rotated the same way." },
/* Its int8 twin, the activation of the i8a8h GEMMs; the factor of `group` is the weight's here
* too. */
{ "had_quant_act_i8g", ARR(pQuant), ARR(oQuantFp8),
"quant_act_i8g with a Walsh-Hadamard rotation of width `group` along n first: scale = "
"amax(rotated) / 127 per group, [M, n/group] in f32, n a multiple of `group`. Pairs with an i8 "
"weight rotated the same way, its scale carrying the factor of `group` (dtype i8a8h)." },
/* The calibration op, the only one no forward pass issues: it runs under a calibration flag and
* its output is read at convert time, in another process, through a file. `dtype` is the input's:
* fp8a8 where the tap is a quantiser's output, bf16 without `a_scale` for a plain activation. */
@@ -1788,6 +1794,10 @@ 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",
ref_had_quant_act_fp8),
SIMPLE("ref_had_quant_act_i8g", "had_quant_act_i8g", "quant",
"width-`group` Hadamard, then the symmetric int8 quantiser, one scale per group",
"float in, i8 out, f32 scale per group; n a multiple of group",
ref_had_quant_act_i8g),
SIMPLE("ref_gram_accum", "gram_accum", "quant",
"h += x^T x over a dequantised E4M3 activation, or a plain bf16 one -- the Gram matrix "
"GPTQ calibrates on",
+14 -1
View File
@@ -863,6 +863,18 @@ int ref_had_quant_act_fp8_shape(const RadParam* p, int n_p, int operand, RadOpdD
return o.finish("had_quant_act_fp8", operand, out);
}
/* quant_act_i8g's extents: the rotation changes the values, not the shapes. */
int ref_had_quant_act_i8g_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);
const int64_t ng = scale_groups(n, gi(p, n_p, "group", 0));
Opd o;
o.t(D, { M, n }); /* x */
o.t((uint32_t)RAD_I8, { M, n }); /* q */
o.t((uint32_t)RAD_F32, { M, ng }); /* scale (amax / 127) */
return o.finish("had_quant_act_i8g", operand, out);
}
/* The int8 rotated forms: `group` is the Walsh-Hadamard rotation width (libr4d's `had`) and the
* scale is one per row, carrying the 1/sqrt(group) normalisation (ref_quant.cpp). Describing it as
* [M, n/group] would leave the tail of every scale buffer unwritten by both sides, comparing two
@@ -960,7 +972,8 @@ static int gemm_q_shape(const RadParam* p, int n_p, int operand, bool bias, RadO
const char* q = gs(p, n_p, "dtype");
auto is = [&](const char* s) { return q && std::strcmp(q, s) == 0; };
const bool a8 = is("fp8a8"), a16 = is("fp8a16"), w4 = is("w4a8"), w8 = is("w8a8");
const bool i8 = is("i8a8"), mx = is("mxfp4a8");
/* i8a8h is i8a8's operands; its rotation is in the values. */
const bool i8 = is("i8a8") || is("i8a8h"), mx = is("mxfp4a8");
if ((!a8 && !a16 && !w4 && !w8 && !i8 && !mx) || N <= 0 || K <= 0 || group <= 0)
return RAD_E_UNSUPPORTED;
/* Neither has a bias form in any architecture. */
+13
View File
@@ -264,6 +264,7 @@ TEST(op_vocabulary) {
"scale_rows",
"softmax",
"quant_act_i8", "quant_act_i8g", "quant_act_fp8", "quant_cols_fp8", "had_quant_act_fp8", "givens_dense_quant", "givens_rotate", "had_quant_act_i8",
"had_quant_act_i8g",
"gram_accum",
/* The fused fp8 forms. A host run needs every op covered, so these keep a host-backend run
* from failing with "site 'host' is unavailable for this band". */
@@ -6435,6 +6436,17 @@ TEST(had_quant_act_fp8_rotates_before_the_absmax) {
CHECK_EQ(q.at(2), 0.0f); CHECK_EQ(q.at(3), 448.0f);
}
/* had_quant_act_i8g is the same at int8 codes: amax / 127, nothing folded either. */
TEST(had_quant_act_i8g_rotates_before_the_absmax) {
T x(RAD_F32, {1, 4}); x.f({1, 1, 1, -1});
T q(RAD_I8, {1, 4}), s(RAD_F32, {1, 2});
A a; a.t(x).t(q).t(s).i("M", 1).i("n", 4).i("group", 2).s("dtype", "f32");
CHECK_OK(run("had_quant_act_i8g", a));
CHECK_EQ(s.at(0), 2.0f / 127.0f); CHECK_EQ(s.at(1), 2.0f / 127.0f);
CHECK_EQ(q.at(0), 127.0f); CHECK_EQ(q.at(1), 0.0f);
CHECK_EQ(q.at(2), 0.0f); CHECK_EQ(q.at(3), 127.0f);
}
namespace {
void put_pair(T& t, int64_t i, int32_t id, float v) {
std::memcpy((float*)t.t.data + 2 * i, &id, 4);
@@ -6898,6 +6910,7 @@ std::vector<Geo> geometries() {
G("quant_act_fp8", "M", 3, a.i("M", R).i("n", 32).i("group", 16).s("dtype", "f32")),
G("quant_cols_fp8", "M", 3, a.i("M", R).i("n", 32).s("dtype", "f32")),
G("had_quant_act_fp8", "M", 3, a.i("M", R).i("n", 32).i("group", 16).s("dtype", "f32")),
G("had_quant_act_i8g", "M", 3, a.i("M", R).i("n", 32).i("group", 16).s("dtype", "f32")),
G("had_quant_act_i8", "M", 3, a.i("M", R).i("n", 32).i("group", 16).s("dtype", "f32")),
G("rmsnorm_had_quant_i8", "M", 3, a.i("M", R).i("n", 32).d("eps", 1e-6).i("group", 16)
.s("dtype", "f32")),