ParoQuant dense: review fixes -- the matrices composed exactly, out checked, the stored form pinned

* r4d_rad_relayout_gdense composes in f64 (angles widened, cos/sin and products in double) and rounds to
  f16: the stored matrix is a derivation of the planes, the same bytes whichever libm computed it, as
  abi/rad_abi.h asks of a relayout.
* givens_dense_quant (libr4d and libref) refuses an `out` that does not name q's dtype instead of reading
  the quantiser off q alone.
* relayout_parity_test pins the dense stored form by hash, checks each matrix is orthogonal to f16
  accuracy and that a broken pairing is refused.
* r4d_givens_dense: static_assert that a thread stages one channel scale of the group; the selftest's
  givens_numeric takes one mode (rotate / dense fp8 / dense i8) instead of three flags; cGivensDense
  sits after the Hadamard constraint with its own comment.
This commit is contained in:
Malic Zilyas committed 2026-10-08 23:35:43 +02:00
1 parent d6b506fe4f
commit 9137948b95
6 files changed
+100 -30

No files matched your search

+6 -1
View File
@@ -33,13 +33,15 @@
#include <hip/hip_fp16.h>
#include <algorithm>
#include <cstdint>
#include <cstring>
namespace {
constexpr uint32_t kGroup = 128;
constexpr uint32_t kThreads = 128;
constexpr uint32_t kWaves = kThreads / 32;
constexpr uint32_t kTStride = kGroup + 8; // f16 per LDS row of T: 16 B pad, conflict-free b128 reads
constexpr uint32_t kTStride = kGroup + 8;
static_assert(kThreads == kGroup, "a thread stages one channel scale of the group (invd[tid])"); // f16 per LDS row of T: 16 B pad, conflict-free b128 reads
typedef _Float16 v8h_t __attribute__((ext_vector_type(8)));
@@ -141,8 +143,11 @@ static int givens_dense_body(const RadArgs* a, const R4dSink& sk) {
if (x->rank < 2 || q->rank != 2 || sc->rank != 2 || t->rank != 2 || rs->rank != 2)
return R4D_NO(RAD_E_SHAPE);
if (t->dtype != RAD_F16 || rs->dtype != RAD_F32) return R4D_NO(RAD_E_DTYPE);
// `out` names the quantiser and q's dtype must be it: a mismatch is refused, not read from q
const char* out = r4d_gets(a, "out");
const bool i8 = q->dtype == RAD_I8;
if (!i8 && q->dtype != RAD_F8E4M3) return R4D_NO(RAD_E_DTYPE);
if (!out || std::strcmp(out, i8 ? "i8" : "fp8") != 0) return R4D_NO(RAD_E_DTYPE);
const long long M = x->shape[0], K = r4d_numel(x) / (x->shape[0] > 0 ? x->shape[0] : 1);
const long long parts = r4d_geti_or(a, "parts", 1), nkb = K / kGroup;
if (r4d_geti_or(a, "group", kGroup) != kGroup || K % kGroup || parts < 1) return R4D_NO(RAD_E_SHAPE);
+15 -10
View File
@@ -641,9 +641,14 @@ extern "C" int r4d_rad_unrelayout_givens_pairs(const RadParam* p, int n_p, int o
* A "givens" weight stores its rotation as pairs (i32 [P*S, K]) and angles (f32 [P*S, K/2]); the dense
* kernel (r4d_givens_dense.hip) applies it as T (x / D), T being the composition of the group's S
* stages. The blob is [P * K/128][128 (j)][128 (i)] f16, T[j][i] = output j of the network fed the unit
* vector e_i, so a (part, group) is 32 KiB the kernel stages whole. Composed in f32 in the order the
* stages are applied (stage 0 first), a' = a c + b s, b' = b c - a s -- libref's loop -- and rounded to
* f16 once. Operand 1 of givens_dense_quant; everything else stays as stored. */
* vector e_i, so a (part, group) is 32 KiB the kernel stages whole. Composed in the order the stages are
* applied (stage 0 first), a' = a c + b s, b' = b c - a s -- libref's loop.
*
* A DERIVATION, NOT A CHOICE (abi/rad_abi.h: a relayout derives nothing a reader of the planes could not):
* the composition is done in f64 -- the f32 angles widened, cos and sin and every product in double --
* so its error (~1e-15) is far below an f16 step and the stored matrix is the exact composition rounded
* to f16 (by way of f32), the same bytes whichever libm or summation order computed it. Operand 1 of
* givens_dense_quant; everything else stays as stored. */
namespace {
constexpr int GD_OPERAND = 1;
constexpr long long GD_GROUP = 128;
@@ -690,8 +695,8 @@ extern "C" int r4d_rad_relayout_gdense(const RadParam* p, int n_p, int operand,
uint16_t* t = (uint16_t*)dst;
const long long nkb = K / GD_GROUP, half = GD_GROUP / 2;
std::vector<int> ia((size_t)(S * half)), ib((size_t)(S * half));
std::vector<float> cs((size_t)(S * half)), sn((size_t)(S * half));
std::vector<float> v((size_t)GD_GROUP);
std::vector<double> cs((size_t)(S * half)), sn((size_t)(S * half));
std::vector<double> v((size_t)GD_GROUP);
for (long long pt = 0; pt < P; ++pt)
for (long long g = 0; g < nkb; ++g) {
for (long long st = 0; st < S; ++st)
@@ -699,20 +704,20 @@ extern "C" int r4d_rad_relayout_gdense(const RadParam* p, int n_p, int operand,
const long long row = pt * S + st, o = (size_t)(st * half + j);
ia[(size_t)o] = (int)(pairs[row * K + g * GD_GROUP + 2 * j] - g * GD_GROUP);
ib[(size_t)o] = (int)(pairs[row * K + g * GD_GROUP + 2 * j + 1] - g * GD_GROUP);
const float th = ang[row * (K / 2) + g * half + j];
const double th = (double)ang[row * (K / 2) + g * half + j];
cs[(size_t)o] = std::cos(th);
sn[(size_t)o] = std::sin(th);
}
uint16_t* tg = t + (size_t)(pt * nkb + g) * GD_GROUP * GD_GROUP;
for (long long i = 0; i < GD_GROUP; ++i) {
std::fill(v.begin(), v.end(), 0.0f);
v[(size_t)i] = 1.0f;
std::fill(v.begin(), v.end(), 0.0);
v[(size_t)i] = 1.0;
for (long long o = 0; o < S * half; ++o) {
const float va = v[(size_t)ia[(size_t)o]], vb = v[(size_t)ib[(size_t)o]];
const double va = v[(size_t)ia[(size_t)o]], vb = v[(size_t)ib[(size_t)o]];
v[(size_t)ia[(size_t)o]] = va * cs[(size_t)o] + vb * sn[(size_t)o];
v[(size_t)ib[(size_t)o]] = vb * cs[(size_t)o] - va * sn[(size_t)o];
}
for (long long j = 0; j < GD_GROUP; ++j) tg[j * GD_GROUP + i] = f32_to_f16(v[(size_t)j]);
for (long long j = 0; j < GD_GROUP; ++j) tg[j * GD_GROUP + i] = f32_to_f16((float)v[(size_t)j]);
}
}
return RAD_OK;
+5 -3
View File
@@ -3020,12 +3020,14 @@ static const RadConstraint cQuantActFp8[] = {
* straddle the end of a row: the ragged tail the unrotated kernel writes zero-padded would mix
* real values with padding inside one butterfly, and there is no weight block on the other side
* that was rotated the same way. */
static const RadConstraint cGivensDense[] = {
C_DIV("n", R4D_FP8_BLOCK), C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "bf16"), C_IN("out", "i8 fp8"),
};
static const RadConstraint cHadQuantActFp8[] = {
C_DIV("n", R4D_FP8_BLOCK), C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "bf16"),
};
/* ParoQuant's dense rotation: the same whole-group rule (a rotation group cannot straddle a row's end),
* and `out` one of the two quantisers its epilogue has. */
static const RadConstraint cGivensDense[] = {
C_DIV("n", R4D_FP8_BLOCK), C_EQ("group", R4D_FP8_BLOCK), C_IN("dtype", "bf16"), C_IN("out", "i8 fp8"),
};
/* The dtype is the INPUT's, and the input is already quantised -- so it is `fp8a8` and not `bf16`
* the way every other row in this family reads it. A four-multiple `n` is the staging slot (one
+19 -16
View File
@@ -3774,8 +3774,11 @@ static void mxfp4a8_numeric(const Plugin& pl, const char* row, int64_t M, int64_
* kernel's scale must match it to the rounding of its f32 sincos, and every dequantised element
* must sit within the E4M3 grid of the reference (half a step at the top of the range is 1/16 of
* the group's maximum, so 7% of it is a bound a wrong pair or a wrong angle cannot meet). */
static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, int stages,
bool rot_only = false, bool i8 = false, bool dense = false) {
/* What givens_numeric runs: the sequential rotation to f16, or the dense rotation with either quantiser. */
enum class GvCase { Rotate, DenseFp8, DenseI8 };
static void givens_numeric(const Plugin& pl, int64_t M, int64_t K, int parts, int stages, GvCase mode) {
const bool rot_only = mode == GvCase::Rotate, i8 = mode == GvCase::DenseI8, dense = !rot_only;
const char* row = dense ? "givens_dense_quant" : "givens_rotate";
if (!sel(row)) return;
char nm[160];
@@ -13176,24 +13179,24 @@ static void device_phase(const Plugin& pl) {
if (sel("givens_rotate")) {
if (!g_case) std::printf("\nParoQuant's rotation to f16, against an f64 rotation\n");
givens_numeric(pl, 3, 5120, 1, 8, true);
givens_numeric(pl, 5, 5120, 2, 8, true);
givens_numeric(pl, 300, 6144, 2, 8, true);
givens_numeric(pl, 3, 5120, 1, 8, GvCase::Rotate);
givens_numeric(pl, 5, 5120, 2, 8, GvCase::Rotate);
givens_numeric(pl, 300, 6144, 2, 8, GvCase::Rotate);
}
if (sel("givens_dense_quant")) {
if (!g_case) std::printf("\nParoQuant's rotation as dense per-group matrices, int8 and E4M3 output, against libm's rotation\n");
for (int i8 = 0; i8 < 2; ++i8) {
givens_numeric(pl, 1, 128, 1, 8, false, i8, true);
givens_numeric(pl, 5, 5120, 2, 8, false, i8, true);
givens_numeric(pl, 8, 5120, 2, 8, false, i8, true);
givens_numeric(pl, 16, 5120, 1, 8, false, i8, true);
givens_numeric(pl, 300, 6144, 2, 8, false, i8, true);
givens_numeric(pl, 2048, 5120, 1, 8, false, i8, true);
givens_numeric(pl, 2048, 5120, 2, 8, false, i8, true);
givens_numeric(pl, 64, 17408, 1, 8, false, i8, true);
givens_numeric(pl, 2048, 17408, 1, 8, false, i8, true);
givens_numeric(pl, 7, 5120, 3, 2, false, i8, true);
for (const GvCase mode : { GvCase::DenseFp8, GvCase::DenseI8 }) {
givens_numeric(pl, 1, 128, 1, 8, mode);
givens_numeric(pl, 5, 5120, 2, 8, mode);
givens_numeric(pl, 8, 5120, 2, 8, mode);
givens_numeric(pl, 16, 5120, 1, 8, mode);
givens_numeric(pl, 300, 6144, 2, 8, mode);
givens_numeric(pl, 2048, 5120, 1, 8, mode);
givens_numeric(pl, 2048, 5120, 2, 8, mode);
givens_numeric(pl, 64, 17408, 1, 8, mode);
givens_numeric(pl, 2048, 17408, 1, 8, mode);
givens_numeric(pl, 7, 5120, 3, 2, mode);
}
}
+1
View File
@@ -476,6 +476,7 @@ int ref_givens_dense_quant(const RadArgs* a, RadStream) {
if (n <= 0 || P <= 0 || g != 128 || n % g != 0) return RAD_E_SHAPE;
const bool i8 = q->dtype == RAD_I8;
if (!i8 && q->dtype != RAD_F8E4M3) return RAD_E_DTYPE;
if (std::strcmp(p_str(a, "out", ""), i8 ? "i8" : "fp8") != 0) return RAD_E_DTYPE; // `out` names q's dtype
const int64_t rows = numel(x) / n, ngrp = n / g;
if (rows <= 0 || numel(tm) < P * n * g || numel(sc) < P * n || numel(q) < rows * P * n ||
numel(s) < rows * P * ngrp) return RAD_E_SHAPE;
+54
View File
@@ -835,3 +835,57 @@ TEST(givens_pairs_are_stored_as_they_are_and_checked) {
RAD_E_FORMAT);
}
}
/* ParoQuant's dense rotation matrices: composed at load from the pairs and angles, exactly (f64), and
* rounded to f16 -- a derivation of the planes, pinned by hash so that a change in how they are composed
* shows here. Each stored matrix is orthogonal to f16 accuracy, and a broken pairing is refused. */
TEST(givens_dense_matrices_are_the_exact_composition) {
const int64_t K = 256, P = 2, S = 3, PS = P * S, G = 128;
RadEncoding e = rad_enc_affine(RAD_FP4E2M1, RAD_E8M0, 1, 32);
rad_enc_add_table(&e, "t.pairs", RAD_I32, PS, K);
rad_enc_add_table(&e, "t.angle", RAD_F32, PS, K / 2);
const int sel[2] = { rad_enc_find(&e, "t.pairs"), rad_enc_find(&e, "t.angle") };
std::vector<int32_t> pairs((size_t)(PS * K));
std::vector<float> ang((size_t)(PS * K / 2));
for (int64_t r = 0; r < PS; ++r) {
const int64_t mul = 2 * r + 3, add = 17 * r + 5; /* odd multiplier: a permutation */
for (int64_t c = 0; c < K; ++c)
pairs[(size_t)(r * K + c)] = (int32_t)((c / G) * G + (c % G * mul + add) % G);
for (int64_t j = 0; j < K / 2; ++j) ang[(size_t)(r * K / 2 + j)] = 0.37f * (float)(r + 1) - 0.011f * (float)j;
}
auto tensor = [](void* d, uint32_t dt, int64_t r, int64_t c) {
RadTensor t{};
t.data = d;
t.dtype = dt;
t.rank = 2;
t.shape[0] = r;
t.shape[1] = c;
t.stride[0] = c;
t.stride[1] = 1;
return t;
};
RadTensor t[2] = { tensor(pairs.data(), RAD_I32, PS, K), tensor(ang.data(), RAD_F32, PS, K / 2) };
Opts kp;
kp.i("n", K).i("parts", P);
RadLayout L{};
REQUIRE_EQ(r4d_rad_layout_gdense(kp.data(), kp.n(), 1, &e, sel, t, 2, &L), RAD_OK);
REQUIRE_EQ(L.bytes, (int64_t)(P * K * G * 2));
std::vector<uint8_t> dst((size_t)L.bytes);
REQUIRE_EQ(r4d_rad_relayout_gdense(kp.data(), kp.n(), 1, &e, sel, t, 2, dst.data(), L.bytes), RAD_OK);
golden("givens dense", fnv(dst), 0x8e72bd2bb541c6e6ull);
/* T's columns are unit vectors (the composition of rotations is orthogonal), to f16 accuracy */
const uint16_t* tm = (const uint16_t*)dst.data();
double worst = 0;
for (int64_t pg = 0; pg < P * K / G; ++pg)
for (int64_t i = 0; i < G; ++i) {
double nrm = 0;
for (int64_t j = 0; j < G; ++j) {
const double v = rad_f16_to_f32(tm[(size_t)((pg * G + j) * G + i)]);
nrm += v * v;
}
worst = std::max(worst, std::fabs(nrm - 1.0));
}
CHECK(worst < 2e-3);
pairs[5] = pairs[6]; /* a column twice in a stage */
CHECK_EQ(r4d_rad_relayout_gdense(kp.data(), kp.n(), 1, &e, sel, t, 2, dst.data(), L.bytes), RAD_E_FORMAT);
}