Archived
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:
1 parent
d6b506fe4f
commit
9137948b95
6 files changed
+100
-30
No files matched your search
@@ -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
@@ -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
@@ -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
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user