Archived
M2XFP4 (MXFP4v2): quantiser, gfx1201 WMMA kernels, host rows, df2 wiring, .rad packer (Import::ParoM2), stability fixes (counters-in-scratch x3, aql publish fences), kbench rows, runbook (REMAINING.md)
This commit is contained in:
1 parent
9eaf10da6e
commit
ea0908596b
60 files changed
+4619
-193
No files matched your search
@@ -3607,6 +3607,84 @@ void planes_cases(const Check& ck, std::vector<Hand>& hs, Broken& broken) {
|
||||
hs.push_back(std::move(h));
|
||||
}
|
||||
|
||||
/* ---- M2XFP4, the m2xfp4 quantiser's own scheme over the E4M3 pair ------------------------- *
|
||||
* Codes across their whole range, E8M0 exponents over a few binades with a zero group among
|
||||
* them, meta bytes across their whole range so every subgroup multiplier appears. The weight
|
||||
* operand joins codes and meta as their concatenated bytes -- the codes rows, then the meta
|
||||
* rows -- while `b_scale` and `b_ref` are the exponent plane as it lies. The oracle is the
|
||||
* quantiser's own decode (libquant's lq_m2xfp.cpp): e2m1 . 2^(e-127) . (1 + k/4) against the
|
||||
* activation's codes and per-128 scales, summed in double. N = 40 and 41 are not multiples of
|
||||
* the drivers' row blocks; K = 384 is three activation scale blocks and a twelfth group, and
|
||||
* M = 13, 17 the small-M paths' 8 + 4 + 1 rows and one past them. */
|
||||
{
|
||||
const float kE2M1[8] = { 0.0f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f, 4.0f, 6.0f };
|
||||
for (int64_t N : { 40, 41 })
|
||||
for (int64_t M : { 1, 13, 17 }) {
|
||||
const int64_t K = 384, kb = K / 32, akb = K / 128;
|
||||
RadEncoding e;
|
||||
rad_enc_clear(&e);
|
||||
rad_enc_copy_str(e.scheme, "m2xfp4");
|
||||
rad_enc_add_plane(&e, "codes", RAD_FP4E2M1, 1, 1);
|
||||
rad_enc_add_plane(&e, "scale", RAD_E8M0, 1, 32);
|
||||
rad_enc_add_plane(&e, "meta", RAD_U8, 1, 32);
|
||||
Planes w(e, N, K);
|
||||
w.fill(0, [](int64_t r, int64_t c) {
|
||||
return (float)hint(257, r * 61 + c, 0, 15);
|
||||
});
|
||||
auto ebyte = [](int64_t r, int64_t g) {
|
||||
return (uint8_t)(g % 7 == 3 ? 0 : 118 + (r + g) % 6); /* a zero group among them */
|
||||
};
|
||||
auto mbyte = [kb](int64_t r, int64_t g) {
|
||||
return (uint8_t)hint(259, r * kb + g, 0, 255);
|
||||
};
|
||||
for (int64_t r = 0; r < N; ++r)
|
||||
for (int64_t g = 0; g < kb; ++g) {
|
||||
w.row_at(1, r)[g] = ebyte(r, g);
|
||||
w.row_at(2, r)[g] = mbyte(r, g);
|
||||
}
|
||||
const std::vector<RadParam> p =
|
||||
rp({ { "M", M }, { "N", N }, { "K", K }, { "group", 32 } }, { { "dtype", "m2xfp4" } });
|
||||
std::vector<uint8_t> j;
|
||||
uint32_t jdt = 0;
|
||||
std::string why;
|
||||
if (!join_both(ck, "gemm_nt_q", p, 2, w, { 0, 2 }, &j, &jdt, &why)) {
|
||||
broken.why.push_back(std::string("gemm_nt_q m2xfp4: ") + why);
|
||||
continue;
|
||||
}
|
||||
/* The activation's exact e4m3 codes (integers the dtype holds) and f32 scales. */
|
||||
auto acode = [](int64_t t) { return (float)hint(252, t, -13, 13); };
|
||||
auto ascal = [akb](int64_t t) { return 0.003f * (float)(1 + (t / akb) % 5); };
|
||||
std::vector<float> af((size_t)(M * K));
|
||||
for (int64_t m = 0; m < M; ++m)
|
||||
for (int64_t k = 0; k < K; ++k)
|
||||
af[(size_t)(m * K + k)] = acode(m * K + k) * ascal(m * akb + k / 128);
|
||||
const std::string nm = std::string("m2xfp4, M=") + std::to_string(M) + ", N=" +
|
||||
std::to_string(N);
|
||||
Hand h(nm.c_str(), "gemm_nt_q", 8e-3);
|
||||
h.dense(RAD_F8E4M3, { M, K }, acode)
|
||||
.dense(RAD_F32, { M, akb }, ascal);
|
||||
h.raw(RAD_U8, { N * (K / 2 + kb) }, j.data());
|
||||
h.raw(RAD_E8M0, { N, kb }, w.data[1].data());
|
||||
h.dense(RAD_BF16, { M, N }, zero, true)
|
||||
.none()
|
||||
.raw(RAD_E8M0, { N, kb }, w.data[1].data());
|
||||
h.pi("M", M).pi("N", N).pi("K", K).pi("group", 32).ps("dtype", "m2xfp4");
|
||||
std::vector<float> W((size_t)(N * K));
|
||||
for (int64_t r = 0; r < N; ++r)
|
||||
for (int64_t k = 0; k < K; ++k) {
|
||||
const int64_t c = hint(257, r * 61 + k, 0, 15);
|
||||
const uint8_t mb = mbyte(r, k >> 5);
|
||||
const float km = 1.0f + 0.25f * (float)((mb >> (2 * ((k & 31) >> 3))) & 3);
|
||||
W[(size_t)(r * K + k)] = (c & 8 ? -kE2M1[c & 7] : kE2M1[c & 7]) *
|
||||
std::ldexpf(1.0f, (int)ebyte(r, k >> 5) - 127) * km;
|
||||
}
|
||||
h.oracle_opd = 4;
|
||||
h.oracle_tol = 8e-3;
|
||||
h.oracle = oracle_gemm(af, K, W, M, N, K, [](int64_t) { return 0; });
|
||||
hs.push_back(std::move(h));
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- refusals ------------------------------------------------------------------------ *
|
||||
* A zero-point grid's `b_scale` that is not joined has no zero to read: both refuse. */
|
||||
{
|
||||
@@ -3666,6 +3744,26 @@ bool planes_refusals(const Check& ck, int* run) {
|
||||
zp(RAD_U4, RAD_U8, 128, nullptr), { 2, 1 }, RAD_E_DTYPE });
|
||||
rs.push_back({ "scale and zero at fp8a8", "gemm_nt_q", 3, gq("fp8a8"),
|
||||
zp(RAD_U4, RAD_U8, 128, nullptr), { 1, 2 }, RAD_E_DTYPE });
|
||||
const auto gq32 = [&](const char* dt) {
|
||||
return P({ { "M", 4 }, { "N", 256 }, { "K", 256 }, { "group", 32 } }, dt);
|
||||
};
|
||||
const auto m2enc = [] {
|
||||
RadEncoding e;
|
||||
rad_enc_clear(&e);
|
||||
rad_enc_copy_str(e.scheme, "m2xfp4");
|
||||
rad_enc_add_plane(&e, "codes", RAD_FP4E2M1, 1, 1);
|
||||
rad_enc_add_plane(&e, "scale", RAD_E8M0, 1, 32);
|
||||
rad_enc_add_plane(&e, "meta", RAD_U8, 1, 32);
|
||||
return e;
|
||||
};
|
||||
rs.push_back({ "m2xfp4 over affine planes", "gemm_nt_q", 2, gq32("m2xfp4"),
|
||||
zp(RAD_U4, RAD_U8, 32, nullptr), { 0, 2 }, RAD_E_DTYPE });
|
||||
rs.push_back({ "m2xfp4 with codes but no meta", "gemm_nt_q", 2, gq32("m2xfp4"), m2enc(), { 0 },
|
||||
RAD_E_DTYPE });
|
||||
rs.push_back({ "m2xfp4's scale plane alone", "gemm_nt_q", 3, gq32("m2xfp4"), m2enc(), { 1 },
|
||||
RAD_E_UNSUPPORTED });
|
||||
rs.push_back({ "m2xfp4's reference plane alone", "gemm_nt_q", 6, gq32("m2xfp4"), m2enc(), { 1 },
|
||||
RAD_E_UNSUPPORTED });
|
||||
bool ok = true;
|
||||
for (R& r : rs) {
|
||||
for (const Lib* lib : { ck.ref, ck.lib }) {
|
||||
|
||||
Reference in new issue
Block a user