From 83856506e114060fb3a37e861a37fb0494e4f320 Mon Sep 17 00:00:00 2001 From: eddie168168 Date: Sat, 17 Oct 2026 15:42:07 +0800 Subject: [PATCH] libr4d: i8a8h -- the int8 trunk GEMMs read a weight rotated by fwht128, against had_quant_act_i8g gemm_i8a8_nt_m16 and gemm_i8a8_tiled take dtype i8a8h: the same launch over i8 codes rotated by the unnormalised 128-wide Hadamard along K, the 1/128 in the bf16 scale (libquant's transform=fwht128). The layout hook accepts only the transform its dtype names and tags the rotated codes r4d.i8h.w.frag64, since the bytes cannot tell the two forms apart. r4d_selftest's i8a8h_rot measures the pair against the exact f32 product: rel_l2 0.009-0.012 rotated, 0.012-0.014 plain, ~1.0 for the rotated weight against the plain quantiser. The had_quant_act_i8g row now says the factor of 128 is the weight's, not sqrt(128). Co-Authored-By: Claude Opus 5.5 --- libr4d/r4d_fp8_layout.cpp | 20 ++++- libr4d/r4d_rows.cpp | 18 ++-- libr4d/r4d_selftest.cpp | 170 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 199 insertions(+), 9 deletions(-) diff --git a/libr4d/r4d_fp8_layout.cpp b/libr4d/r4d_fp8_layout.cpp index b74b6efa..f102ae17 100644 --- a/libr4d/r4d_fp8_layout.cpp +++ b/libr4d/r4d_fp8_layout.cpp @@ -228,8 +228,18 @@ extern "C" int r4d_rad_unrelayout_fp8_block(const RadParam* p, int n_p, int oper * side for one 128-K block. On the narrow path a lane's eight accumulator rows are eight * consecutive rows of one tile, so their scales are one 16-byte load. A row-major plane needs * eight scattered loads, which if-converted into every k step cost the decode kernel 70% at - * M = 4. The prefill tile reads one row's scale per lane and works with either form. */ + * M = 4. The prefill tile reads one row's scale per lane and works with either form. + * + * `i8a8h` is the same planes holding a weight rotated by the unnormalised 128-wide Hadamard along + * K, the 1/128 in its scale (libquant's `transform=fwht128`), to be met by had_quant_act_i8g's + * activation. The bytes cannot tell the two apart, so each dtype accepts only its own transform + * and the rotated codes carry a tag of their own. */ namespace { +bool i8_rotated(const RadParam* p, int n_p) { + const char* d = r4d_pgets(p, n_p, "dtype"); + return d && std::strcmp(d, "i8a8h") == 0; +} + int i8_enc_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; @@ -237,7 +247,10 @@ int i8_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* en const int rc = fgeom(p, n_p, &g); if (rc != RAD_OK) return rc; if (operand != FL_B && operand != FL_BSCALE) return RAD_E_UNSUPPORTED; - if (!r4d_enc::affine(enc, RAD_I8, RAD_BF16, 1, R4D_FP8_BLOCK)) return RAD_E_DTYPE; + const bool rot = i8_rotated(p, n_p); + if (!r4d_enc::affine(enc, RAD_I8, RAD_BF16, 1, R4D_FP8_BLOCK, RAD_DT_INVALID, + rot ? "fwht128" : "")) + return RAD_E_DTYPE; if (operand == FL_BSCALE) { if (!r4d_enc::selects(enc, sel, n, "scale") || !r4d_enc::dims(pl[0], g.N, g.sk)) return RAD_E_SHAPE; @@ -248,7 +261,8 @@ int i8_enc_layout(const RadParam* p, int n_p, int operand, const RadEncoding* en if (!r4d_enc::selects(enc, sel, n, "codes") || !r4d_enc::dims(pl[0], g.N, g.K)) return RAD_E_SHAPE; if (g.K % R4D_FP8_SUPER_K || g.N % r4d_fp8::kFragRows) return RAD_E_SHAPE; - ffill(out, "r4d.i8.w.frag64", RAD_I8, g.N, g.K, r4d_fp8::frag_bytes(g.N, g.K)); + ffill(out, rot ? "r4d.i8h.w.frag64" : "r4d.i8.w.frag64", RAD_I8, g.N, g.K, + r4d_fp8::frag_bytes(g.N, g.K)); return RAD_OK; } } /* namespace */ diff --git a/libr4d/r4d_rows.cpp b/libr4d/r4d_rows.cpp index a244d920..ef5fd83b 100644 --- a/libr4d/r4d_rows.cpp +++ b/libr4d/r4d_rows.cpp @@ -3746,14 +3746,16 @@ static const RadConstraint cGemmNtQBiasFp8A8M16[] = { C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16), C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "fp8a8"), }; -/* The int8 twins: the same bands at dtype i8a8. */ +/* The int8 twins: the same bands at dtype i8a8. `i8a8h` is the same launch over a weight rotated + * by a 128-wide Hadamard, the 1/128 in its scale, as `w4a8h` is beside `w4a8`; only its layout + * tag and its activation (had_quant_act_i8g) differ. */ static const RadConstraint cGemmNtI8A8M16[] = { C_LE("M", 64), C_DIV("K", 128), C_DIV("N", 16), C_EQ("group", R4D_FP8_BLOCK), - C_IN("dtype", "i8a8"), + C_IN("dtype", "i8a8 i8a8h"), }; static const RadConstraint cGemmNtI8A8Tiled[] = { C_DIV("K", R4D_FP8_BLOCK), C_DIV("N", R4D_FP8_BLOCK), C_EQ("group", R4D_FP8_BLOCK), - C_IN("dtype", "i8a8"), + C_IN("dtype", "i8a8 i8a8h"), }; /* libr4d's `K DIV 4`, renamed to docs/OPS.md's `n` as in every quantiser row here. */ static const RadConstraint cQuantActFp8[] = { @@ -5967,7 +5969,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, "for the fp8 one and the weight's scale per row", "the fp8 row's: M <= 64; K a multiple of 128; N a multiple of 16", "i8 A with an f32 scale per (row, 128 K), i8 W with a bf16 scale per ROW per 128 K, bf16 C; " - "int32 accumulation within a 128-K block, folded to f32 at its end", + "int32 accumulation within a 128-K block, folded to f32 at its end. dtype i8a8h: the weight " + "rotated by a 128-wide Hadamard, the 1/128 in its scale, against had_quant_act_i8g's activation", RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_FP8_M16, ROW(cGemmNtI8A8M16), NOTUNE, nullptr, nullptr, r4d_rad_gemm_i8a8_m16, r4d_rad_gemm_i8a8_m16_scratch, @@ -5978,7 +5981,9 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, "gemm_fp8a8_tiled at INT8 codes: the same 128x128 prefill tile, the iu8 WMMA for the fp8 one and " "the weight's scale per row, eight a lane a tile in the fold", "the fp8 row's: any M; K and N multiples of 128", - "i8 A with an f32 scale per (row, 128 K), i8 W with a bf16 scale per ROW per 128 K, bf16 C", + "i8 A with an f32 scale per (row, 128 K), i8 W with a bf16 scale per ROW per 128 K, bf16 C. " + "dtype i8a8h: the weight rotated by a 128-wide Hadamard, the 1/128 in its scale, against " + "had_quant_act_i8g's activation", RAD_DOMAIN_DEVICE, R4D_PRIO_GEMM_FP8_TILED, ROW(cGemmNtI8A8Tiled), NOTUNE, nullptr, nullptr, r4d_rad_gemm_i8a8_tiled, nullptr, @@ -6337,7 +6342,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512, "a 128-wide Hadamard along K, then per-(row, 128-column) symmetric int8 quantisation", "had_quant_act_fp8's: n a multiple of 128 and group 128", "bf16 in, int8 out in PLAIN ROW-MAJOR order with an f32 scale of amax/127 per (row, 128 " - "columns); the sqrt(128) of the unnormalised transform is left to the weight's stored scale", + "columns); the factor of 128 of the unnormalised transform is left to the weight's stored " + "scale (dtype i8a8h)", RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY, ROW(cHadQuantActFp8), NOTUNE, nullptr, nullptr, r4d_rad_had_quant_act_i8g }, diff --git a/libr4d/r4d_selftest.cpp b/libr4d/r4d_selftest.cpp index 36c7e482..9a49f50f 100644 --- a/libr4d/r4d_selftest.cpp +++ b/libr4d/r4d_selftest.cpp @@ -4650,6 +4650,173 @@ static void i8a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_t N ok(nm, rel < 5e-3, d); } +/* The rotated int8 trunk against the f32 product it approximates. i8a8_numeric's reference uses + * the quantiser's own codes, so it would pass with the weight and activation rotated by different + * transforms; this one measures meaning. The weight is quantised on the host as libquant writes + * `codes=i8 group=128 scale=bf16 transform=fwht128` (each 128 of K rotated by h_fwht, the 1/128 in + * the stored scale) and the activation by had_quant_act_i8g on the device, so the pair holds only + * if h_fwht and r4d_fwht_wave are one transform and the 1/128 is divided out exactly once. + * + * Three pairings against the exact product of the sources: plain weight with quant_act_i8g + * (i8a8), rotated weight with had_quant_act_i8g (i8a8h), and the rotated weight with the plain + * quantiser, the mismatch a container served under the wrong dtype would produce, which must + * measure far off. Outlier columns make the rotated grid the better one. */ +static void i8a8h_rot(const Plugin& pl, const char* row, int64_t M, int64_t N, int64_t K) { + if (!sel(row) && !sel("had_quant_act_i8g")) return; + char nm[160]; + std::snprintf(nm, sizeof nm, "%s i8a8h + had_quant_act_i8g vs the f32 it approximates, " + "M=%lld N=%lld K=%lld", row, (long long)M, (long long)N, (long long)K); + const RadKernelInfo* r = find(pl, row); + const RadKernelInfo* qp = find(pl, "quant_act_i8g"); + const RadKernelInfo* qh = find(pl, "had_quant_act_i8g"); + if (!r || !qp || !qh || !r->layout || !r->relayout) { ok(nm, false, "rows or hooks absent"); return; } + const int64_t nkb = K / 128; + + /* Outliers every 37th weight column and 23rd activation column, six times the body. */ + std::vector wsrc((size_t)(N * K)), xsrc((size_t)(M * K)); + std::vector xbf((size_t)(M * K)); + for (int64_t n = 0; n < N; ++n) + for (int64_t k = 0; k < K; ++k) { + float v = (float)(((n * 31 + k * 17) % 61) - 30) / 37.0f * 0.02f; + if (k % 37 == 0) v *= 6.0f; + wsrc[(size_t)(n * K + k)] = v; + } + for (int64_t m = 0; m < M; ++m) + for (int64_t k = 0; k < K; ++k) { + float v = (float)(((m * 29 + k * 11) % 53) - 26) / 41.0f; + if (k % 23 == 0) v *= 6.0f; + /* Rounded through bf16 here, so the reference reads the number the device does. */ + const uint16_t h = h_to_bf16(v); + xbf [(size_t)(m * K + k)] = h; + xsrc[(size_t)(m * K + k)] = h_bf16(h); + } + std::vector want((size_t)(M * N), 0.0); + for (int64_t m = 0; m < M; ++m) + for (int64_t n = 0; n < N; ++n) { + double acc = 0.0; + for (int64_t k = 0; k < K; ++k) + acc += (double)wsrc[(size_t)(n * K + k)] * (double)xsrc[(size_t)(m * K + k)]; + want[(size_t)(m * N + n)] = acc; + } + + /* The weight as libquant's rtn writes it: codes against the rounded scale, and the rotated + * form then stores that scale over 128, exact in bf16. */ + auto planes = [&](bool rot) { + Planes p; + p.enc = rad_enc_affine(RAD_I8, RAD_BF16, 1, 128); + if (rot) rad_enc_copy_str(p.enc.transform, "fwht128"); + p.size(N, K); + std::vector v(128); + for (int64_t n = 0; n < N; ++n) + for (int64_t g = 0; g < nkb; ++g) { + for (int64_t j = 0; j < 128; ++j) v[(size_t)j] = wsrc[(size_t)(n * K + g * 128 + j)]; + if (rot) h_fwht(v.data(), 128); + float amax = 0.0f; + for (float x : v) amax = std::max(amax, std::fabs(x)); + rad_store_f32(p.data[1].data(), RAD_BF16, n * nkb + g, amax > 0 ? amax / 127.0f : 1.0f); + const float s = rad_load_f32(p.data[1].data(), RAD_BF16, n * nkb + g); + for (int64_t j = 0; j < 128; ++j) + p.data[0][(size_t)(n * K + g * 128 + j)] = + (uint8_t)(int8_t)std::max(-127.0f, std::min(127.0f, std::nearbyint(v[(size_t)j] / s))); + if (rot) rad_store_f32(p.data[1].data(), RAD_BF16, n * nkb + g, s / 128.0f); + } + return p; + }; + const Planes plain = planes(false), rotated = planes(true); + + /* The bytes, extents and arrangement of the two forms are identical, so the encoding is the + * only guard: each dtype takes its own and refuses the other, under tags of their own. */ + { + const std::vector lp8 = { PI("N", N), PI("K", K), PI("group", 128), PS("dtype", "i8a8") }; + const std::vector lp8h = { PI("N", N), PI("K", K), PI("group", 128), PS("dtype", "i8a8h") }; + std::vector scratch; + RadLayout lplain{}, lrot{}; + const int own_plain = stored(r, lp8, 2, plain, { "codes" }, &scratch, &lplain); + const int own_rot = stored(r, lp8h, 2, rotated, { "codes" }, &scratch, &lrot); + const int cross_a = stored(r, lp8, 2, rotated, { "codes" }, &scratch); + const int cross_b = stored(r, lp8h, 2, plain, { "codes" }, &scratch); + const bool tags = lplain.tag && lrot.tag && std::strcmp(lplain.tag, lrot.tag) != 0; + const std::string what = std::string(row) + ": i8a8 and i8a8h each take only their own encoding"; + ok(what.c_str(), own_plain == RAD_OK && own_rot == RAD_OK && cross_a == RAD_E_DTYPE && + cross_b == RAD_E_DTYPE && tags, + "own " + std::to_string(own_plain) + "/" + std::to_string(own_rot) + ", crossed " + + std::to_string(cross_a) + "/" + std::to_string(cross_b) + ", tags " + + (lplain.tag ? lplain.tag : "(none)") + " / " + (lrot.tag ? lrot.tag : "(none)")); + } + + auto launch = [&](const RadKernelInfo* k, std::vector ts, std::vector ps) { + RadArgs a; + std::memset(&a, 0, sizeof(a)); + a.t = ts.data(); a.n_t = (int)ts.size(); + a.p = ps.data(); a.n_p = (int)ps.size(); + a.world_size = 1; + if (k->scratch) { + const int64_t need = k->scratch(&a); + if (need > 0) { a.scratch = dalloc((size_t)need); a.scratch_bytes = need; } + } + const int rc = k->launch(&a, (RadStream) nullptr); + return rc == RAD_OK && st_stream_sync(nullptr) == hipSuccess ? RAD_OK : (rc ? rc : -1); + }; + + void* d_x = dalloc(xbf.size() * 2); + void* d_q = dalloc((size_t)(M * K)); + void* d_qs = dalloc((size_t)(M * nkb) * 4); + void* d_y = dalloc((size_t)(M * N) * 2); + if (!d_x || !d_q || !d_qs || !d_y) { ok(nm, false, "hipMalloc failed"); return; } + st_memcpy(d_x, xbf.data(), xbf.size() * 2, hipMemcpyHostToDevice); + + /* rel_l2 of one pairing against the reference, or -1 when something refused to run. */ + auto measure = [&](const Planes& p, const char* dt, const RadKernelInfo* quant) -> double { + const std::vector lp = { PI("N", N), PI("K", K), PI("group", 128), PS("dtype", dt) }; + std::vector wq, wsc; + if (stored(r, lp, 2, p, { "codes" }, &wq) != RAD_OK || + stored(r, lp, 3, p, { "scale" }, &wsc) != RAD_OK) + return -1.0; + void* d_w = dalloc(wq.size()); + void* d_s = dalloc(wsc.size()); + if (!d_w || !d_s) return -1.0; + st_memcpy(d_w, wq.data(), wq.size(), hipMemcpyHostToDevice); + st_memcpy(d_s, wsc.data(), wsc.size(), hipMemcpyHostToDevice); + if (launch(quant, { T(d_x, RAD_BF16, { M, K }), T(d_q, RAD_I8, { M, K }), + T(d_qs, RAD_F32, { M, nkb }) }, + { PI("M", M), PI("n", K), PI("group", 128), PS("dtype", "bf16") }) != RAD_OK) + return -1.0; + std::vector ts = { + T(d_q, RAD_I8, { M, K }), T(d_qs, RAD_F32, { M, nkb }), T(d_w, RAD_I8, { N, K }), + T(d_s, RAD_BF16, { N, nkb }), T(d_y, RAD_BF16, { M, N }), T(nullptr, RAD_F32, { 1 }), + T(nullptr, RAD_U8, { 1 }), + }; + absent(ts[5]); absent(ts[6]); + if (launch(r, ts, { PI("M", M), PI("N", N), PI("K", K), PI("group", 128), PS("dtype", dt) }) + != RAD_OK) + return -1.0; + std::vector got((size_t)(M * N)); + st_memcpy(got.data(), d_y, got.size() * 2, hipMemcpyDeviceToHost); + double num = 0.0, den = 0.0; + for (int64_t i = 0; i < M * N; ++i) { + const double d = (double)h_bf16(got[(size_t)i]) - want[(size_t)i]; + num += d * d; + den += want[(size_t)i] * want[(size_t)i]; + } + return den > 0.0 ? std::sqrt(num / den) : -1.0; + }; + const double e_plain = measure(plain, "i8a8", qp); + const double e_rot = measure(rotated, "i8a8h", qh); + const double e_cross = measure(rotated, "i8a8h", qp); + + /* Printed either way: the pass is about the plumbing, the numbers about the format. An int8 + * pair lands at a few parts in a thousand; the failures the bounds catch are far off. An + * output off by a constant c measures |1 - c|, so a 1/128 never divided out measures ~127, + * and a 1/sqrt(128) folded on the activation side too (the per-row had_*_i8 convention) + * ~0.91. */ + std::printf(" rel_l2 vs f32: i8a8 %.4f, i8a8h %.4f, rotated weight with the plain " + "quantiser %.4f\n", e_plain, e_rot, e_cross); + ok(nm, e_plain >= 0.0 && e_rot >= 0.0 && e_cross >= 0.0 && e_plain < 0.05 && e_rot < 0.05 && + e_rot < e_plain && e_cross > 0.5, + "rel_l2 i8a8 " + std::to_string(e_plain) + ", i8a8h " + std::to_string(e_rot) + + ", crossed " + std::to_string(e_cross)); +} + /* The tiled bf16 GEMM against the skinny one, byte for byte, at every tile variant. An op over * M 1..max_tok is served by the skinny row at M <= 64 and the tiled one above, so they must agree @@ -19406,6 +19573,9 @@ static void device_phase(const Plugin& pl) { bias_gemm_numeric(pl, 520, 1040, 1104, 0, true, true); bias_gemm_numeric(pl, 2050, 600, 40, 2, false, true); bias_gemm_numeric(pl, 200, 300, 136, 2, true, false); + i8a8h_rot(pl, "gemm_i8a8_nt_m16", 1, 256, 1024); + i8a8h_rot(pl, "gemm_i8a8_nt_m16", 16, 256, 1024); + i8a8h_rot(pl, "gemm_i8a8_tiled", 200, 256, 1024); w8a16_numeric(pl, "gemm_w8a16_nt_m64", 5, 272, 384, RAD_F16); w8a16_numeric(pl, "gemm_w8a16_nt_m64", 64, 2560, 6144, RAD_F16); gemm_case(pl, "gemm_w4a8_nt_m64", "w4a8", M, N, K, RAD_I8, 1, 0, 0, N*K/2, N*(K/128)*4);