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 }) {
|
||||
|
||||
@@ -299,6 +299,45 @@ static int gemm_q_drive(const Mat& ma, const Mat& mb, const Scales* SA, const Sc
|
||||
return gemm_drive(ma, mb, SA, SB, Y, bias, M, N, K);
|
||||
}
|
||||
|
||||
/* gemm_nt_q at dtype m2xfp4, the m2xfp4 quantiser's own scheme (libquant): `b` is the weight's
|
||||
* codes and meta planes concatenated as stored (avx_planes.h's m2 arm -- the codes rows, then the
|
||||
* meta rows), `b_scale` the E8M0 exponent plane [N, K/32] and `b_ref` the same plane, as the
|
||||
* mxfp4 grid's. The exact element is e2m1 code . 2^(e-127) . (1 + k_s/4), the subgroup multiplier
|
||||
* k_s the meta byte's two-bit field [2s, 2s+2) over subgroups s of eight columns: the product
|
||||
* runs with the weight's group at that eight and the meta folded into the scale stream
|
||||
* (Scales::m2), the same law ref states element by element (ref_gemm.cpp's gemm_m2) and
|
||||
* libr4d's staging table applies on the card. */
|
||||
static int gemm_m2(const RadTensor* A, const RadTensor* as_, const RadTensor* B,
|
||||
const RadTensor* bs_, const RadTensor* Y, int64_t M, int64_t N, int64_t K) {
|
||||
if (A->dtype != RAD_F8E4M3 || B->dtype != RAD_U8 || B->rank != 1) return RAD_E_DTYPE;
|
||||
if (!as_ || !bs_ || bs_->rank < 2) return RAD_E_DTYPE;
|
||||
if (K <= 0 || K % 32 || N <= 0) return RAD_E_SHAPE;
|
||||
if (numel(B) != N * (K / 2 + K / 32)) return RAD_E_SHAPE;
|
||||
|
||||
Scales SB = make_scales(bs_, N, K, true);
|
||||
if (!SB.p || SB.group != 32 || SB.rblk != 1) return RAD_E_SHAPE;
|
||||
SB.group = 8;
|
||||
SB.e8m0 = true;
|
||||
SB.m2 = true;
|
||||
SB.m2_meta = (const uint8_t*)B->data + N * (K / 2);
|
||||
SB.m2_mrs = K / 32;
|
||||
|
||||
/* The codes as their own [N, K] operand over the concatenation's head: nibble elements, a
|
||||
* row K/2 bytes, dense. */
|
||||
RadTensor Bc{};
|
||||
Bc.data = B->data;
|
||||
Bc.dtype = RAD_FP4E2M1;
|
||||
Bc.rank = 2;
|
||||
Bc.shape[0] = N;
|
||||
Bc.shape[1] = K;
|
||||
Bc.stride[0] = K;
|
||||
Bc.stride[1] = 1;
|
||||
|
||||
Scales SA = make_scales(as_, M, K, false);
|
||||
const Mat ma = as_mat(A, K), mb = as_mat(&Bc, K);
|
||||
return gemm_q_drive(ma, mb, &SA, &SB, Y, nullptr, M, N, K);
|
||||
}
|
||||
|
||||
/* gemm_nt_bias's epilogue (docs/OPS.md): activation of the stored product, then the residual add,
|
||||
* each step read from and stored to the output's dtype (the unfused chain, as in libref). */
|
||||
static void gemm_epilogue(const RadTensor* Y, const RadTensor* res, int act, int64_t M, int64_t N) {
|
||||
@@ -975,6 +1014,20 @@ static int gemm_ntq_impl(const RadArgs* a, bool bias_operand) {
|
||||
* to the grids that remove a zero through it, `b_ref` to an e2m1 weight. */
|
||||
const avxp::ZpGrid* zg = avxp::zp_grid(p_str(a, "dtype", nullptr));
|
||||
if (zg && bias_operand) return RAD_E_UNSUPPORTED;
|
||||
/* M2XFP4: the quantiser's own scheme, decoded by gemm_m2 from its concatenated planes (whose
|
||||
* byte count is its own, not N x K). Parts (pb1/pb2) are the rotated grids'; the m2 declare
|
||||
* makes none, and a case carrying them is declined rather than answered unsplit, as libref
|
||||
* declines them. Its `b_ref` is the mxfp4 grid's, accepted and ignored beside the scheme. */
|
||||
if (!bias_operand && p_str(a, "dtype", nullptr) &&
|
||||
std::strcmp(p_str(a, "dtype", nullptr), "m2xfp4") == 0) {
|
||||
if (rad_arg_in(a, 5)) return RAD_E_UNSUPPORTED; /* a_sum belongs to the asym grids */
|
||||
if (p_int(a, "pb1", 0) > 0 || p_int(a, "pb2", 0) > 0) return RAD_E_UNSUPPORTED;
|
||||
const int64_t K = p_int(a, "K", 0), N = p_int(a, "N", 0);
|
||||
if (K <= 0 || N <= 0) return RAD_E_SHAPE;
|
||||
const int64_t M = numel(Y) / N;
|
||||
if (M <= 0) return RAD_E_SHAPE;
|
||||
return gemm_m2(A, as_, B, bs_, Y, M, N, K);
|
||||
}
|
||||
if (!bias_operand) {
|
||||
if (rad_arg_in(a, 5) && (!zg || zg->act != avxp::ZP_I8_ROW)) return RAD_E_UNSUPPORTED;
|
||||
if (rad_arg_in(a, 6) && B->dtype != RAD_FP4E2M1) return RAD_E_UNSUPPORTED;
|
||||
@@ -988,6 +1041,7 @@ static int gemm_ntq_impl(const RadArgs* a, bool bias_operand) {
|
||||
if (numel(A) < M * K || numel(B) < N * K) return RAD_E_SHAPE;
|
||||
if (bias && numel(bias) < N) return RAD_E_SHAPE;
|
||||
|
||||
|
||||
/* quant_act_i8's codes arrive with the middle two dwords of every 16-byte step exchanged;
|
||||
* exchange them back into a contiguous copy. */
|
||||
RadTensor Ac;
|
||||
|
||||
+25
-2
@@ -84,7 +84,20 @@ struct Scales {
|
||||
uint32_t dt;
|
||||
int64_t rs, cs, group, rblk;
|
||||
bool e8m0;
|
||||
/* M2XFP4 (libquant's `m2xfp4`, lq_m2xfp.cpp): `group` is 8, so `g` names a subgroup of
|
||||
* eight columns. The E8M0 byte is one per four subgroups and the meta plane's two-bit field
|
||||
* refines it, the scale of subgroup g being e8m0(scale[g >> 2]) . (1 + k/4) with k the
|
||||
* field's value -- the quantiser's own decode, stated where this folds it into the product. */
|
||||
const uint8_t* m2_meta = nullptr; /* [rows][K/32] meta bytes, canonical rows */
|
||||
int64_t m2_mrs = 0; /* bytes between meta rows */
|
||||
bool m2 = false;
|
||||
inline float grp(int64_t row, int64_t g) const {
|
||||
if (m2) {
|
||||
const int64_t gi = g >> 2;
|
||||
const int64_t i = (rblk > 1 ? row / rblk : row) * rs + gi * cs;
|
||||
const int k = (m2_meta[row * m2_mrs + gi] >> (2 * (g & 3))) & 3;
|
||||
return rad_e8m0_to_f32(((const uint8_t*)p)[i]) * (1.0f + 0.25f * (float)k);
|
||||
}
|
||||
const int64_t i = (rblk > 1 ? row / rblk : row) * rs + g * cs;
|
||||
if (e8m0) {
|
||||
const uint8_t e = ((const uint8_t*)p)[i];
|
||||
@@ -95,6 +108,17 @@ struct Scales {
|
||||
/* Groups [g0, g0 + n) of `row` into dst, the same values grp() returns, with the dtype
|
||||
* resolved once rather than per scale. */
|
||||
inline void row_groups(int64_t row, int64_t g0, int64_t n, float* dst) const {
|
||||
if (m2) {
|
||||
const int64_t rr = rblk > 1 ? row / rblk : row;
|
||||
const uint8_t* b = (const uint8_t*)p;
|
||||
for (int64_t g = 0; g < n; ++g) {
|
||||
const int64_t gi = g0 + g;
|
||||
const int k = (m2_meta[row * m2_mrs + (gi >> 2)] >> (2 * (gi & 3))) & 3;
|
||||
dst[g] = rad_e8m0_to_f32(b[rr * rs + (gi >> 2) * cs]) *
|
||||
(1.0f + 0.25f * (float)k);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const int64_t base = (rblk > 1 ? row / rblk : row) * rs + g0 * cs;
|
||||
const uint8_t* b = (const uint8_t*)p;
|
||||
if (cs == 1) {
|
||||
@@ -112,8 +136,7 @@ struct Scales {
|
||||
for (int64_t g = 0; g < n; ++g)
|
||||
dst[g] = rad_bf16_to_f32(((const uint16_t*)p)[base + g * cs]);
|
||||
} else if (dt == RAD_F16) {
|
||||
for (int64_t g = 0; g < n; ++g)
|
||||
dst[g] = rad_f16_to_f32(((const uint16_t*)p)[base + g * cs]);
|
||||
for (int64_t g = 0; g < n; ++g) dst[g] = rad_f16_to_f32(((const uint16_t*)p)[base + g * cs]);
|
||||
} else if (dt == RAD_F32) {
|
||||
for (int64_t g = 0; g < n; ++g) dst[g] = ((const float*)p)[base + g * cs];
|
||||
} else {
|
||||
|
||||
+101
-2
@@ -159,12 +159,67 @@ static inline int avx_layout_mtp(const RadParam*, int, int operand, const RadEnc
|
||||
return layout_scaled(operand, 4, 5, enc, sel, planes, n, out);
|
||||
}
|
||||
|
||||
/* gemm_nt_q: `b_scale` (operand 3) as the scale and zero of the grid `dtype` names. */
|
||||
/* M2XFP4, the `m2xfp4` quantiser's own scheme (libquant): three TILED planes over 32-column
|
||||
* groups, and gemm_nt_q's weight operand takes the codes and the meta. Their stored form is the
|
||||
* two planes' canonical bytes concatenated -- the codes' rows, then the meta's -- the form the
|
||||
* core's selection reader hands a kernel that decodes the scheme itself beside the scale stream
|
||||
* another operand carries (core/format/encoding.cpp), and the one both libraries' m2 arms read.
|
||||
* libref/ref_planes.h states the same rules. */
|
||||
static inline bool m2_enc_planes(const RadEncoding* enc) {
|
||||
if (!rad_enc_is(enc, "m2xfp4")) return false;
|
||||
/* A rotated weight (ParoQuant) carries the same three planes in the basis its "givens"
|
||||
* transform names, with the rotation's tables beside them; the rotation is the activation's,
|
||||
* so the planes are read as they are. */
|
||||
if (enc->transform[0] && std::strncmp(enc->transform, "givens", RAD_ENC_STR) != 0) return false;
|
||||
const RadEncPlane* sc = rad_enc_plane(enc, "scale");
|
||||
const RadEncPlane* mt = rad_enc_plane(enc, "meta");
|
||||
const bool planes = enc->plane[0].kind == RAD_PLANE_TILED &&
|
||||
enc->plane[0].dtype == RAD_FP4E2M1 && enc->plane[0].block[0] == 1 &&
|
||||
enc->plane[0].block[1] == 1 && sc && mt && sc->kind == RAD_PLANE_TILED &&
|
||||
sc->dtype == RAD_E8M0 && sc->block[0] == 1 && sc->block[1] == 32 &&
|
||||
mt->kind == RAD_PLANE_TILED && mt->dtype == RAD_U8 && mt->block[0] == 1 &&
|
||||
mt->block[1] == 32;
|
||||
if (!planes) return false;
|
||||
if (!enc->transform[0]) return enc->n_planes == 3;
|
||||
return enc->n_planes >= 6 && rad_enc_plane(enc, "t.pairs") &&
|
||||
rad_enc_plane(enc, "t.angle") && rad_enc_plane(enc, "t.scale");
|
||||
}
|
||||
|
||||
/* gemm_nt_q: `b_scale` (operand 3) as the scale and zero of the grid `dtype` names. At `m2xfp4`
|
||||
* the weight operand takes codes and meta concatenated (above); the scale and reference operands
|
||||
* are single planes, stored as they are. */
|
||||
static inline int avx_layout_gemm_q(const RadParam* p, int n_p, int operand,
|
||||
const RadEncoding* enc, const int* sel,
|
||||
const RadTensor* planes, int n, RadLayout* out) {
|
||||
const char* dt = rad_param_gets(p, n_p, "dtype", nullptr);
|
||||
if (dt && std::strcmp(dt, "m2xfp4") == 0) {
|
||||
const long long group = rad_param_geti(p, n_p, "group", 0);
|
||||
const long long N = rad_param_geti(p, n_p, "N", 0), K = rad_param_geti(p, n_p, "K", 0);
|
||||
if (!m2_enc_planes(enc) || group != 32 || N <= 0 || K <= 0 || K % 32)
|
||||
return RAD_E_DTYPE;
|
||||
if (operand == 2) {
|
||||
if (!sel_roles(enc, sel, n, "codes", "meta")) return RAD_E_DTYPE;
|
||||
if (planes[0].shape[0] != N || planes[0].shape[1] != K ||
|
||||
planes[1].shape[0] != N || planes[1].shape[1] != K / 32)
|
||||
return RAD_E_SHAPE;
|
||||
out->tag = "m2.codes.meta";
|
||||
out->dtype = RAD_U8;
|
||||
out->rank = 1;
|
||||
out->shape[0] = N * (K / 2 + K / 32);
|
||||
out->align = 0;
|
||||
out->bytes = out->shape[0];
|
||||
return RAD_OK;
|
||||
}
|
||||
if (operand == 3 || operand == 6) {
|
||||
if (n != 1 || sel[0] < 0 || sel[0] >= enc->n_planes ||
|
||||
std::strncmp(enc->plane[sel[0]].role, "scale", RAD_ENC_STR) != 0)
|
||||
return RAD_E_DTYPE;
|
||||
return RAD_E_UNSUPPORTED; /* one plane, stored as it is */
|
||||
}
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
if (n < 2 || operand != 3) return RAD_E_UNSUPPORTED;
|
||||
const ZpGrid* zg = zp_grid(rad_param_gets(p, n_p, "dtype", nullptr));
|
||||
const ZpGrid* zg = zp_grid(dt);
|
||||
const long long group = rad_param_geti(p, n_p, "group", 0);
|
||||
if (!zg || group <= 0 || !rad_enc_is(enc, "affine")) return RAD_E_DTYPE;
|
||||
if (enc->transform[0] && std::strncmp(enc->transform, "givens", RAD_ENC_STR) != 0)
|
||||
@@ -182,6 +237,50 @@ static inline int avx_layout_gemm_q(const RadParam* p, int n_p, int operand,
|
||||
return rad_planes_layout(enc, sel, planes, n, out);
|
||||
}
|
||||
|
||||
/* gemm_nt_q's own relayout: the m2 arm's concatenated form beside the zero-point grids' joined
|
||||
* one, each as its layout hook laid it out. */
|
||||
template <RadLayoutFn L>
|
||||
static int avx_relayout_gemm_q(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
|
||||
const int* sel, const RadTensor* planes, int n, void* dst,
|
||||
int64_t dst_bytes) {
|
||||
RadLayout lay{};
|
||||
const int st = L(p, n_p, operand, enc, sel, planes, n, &lay);
|
||||
if (st != RAD_OK) return st;
|
||||
if (lay.tag && std::strcmp(lay.tag, "m2.codes.meta") == 0) {
|
||||
if (dst_bytes != lay.bytes) return RAD_E_SHAPE;
|
||||
uint8_t* d = (uint8_t*)dst;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
const int64_t rb = rad_enc_row_bytes(planes[i].dtype, planes[i].shape[1]);
|
||||
const int64_t b = planes[i].shape[0] * rb;
|
||||
std::memcpy(d, planes[i].data, (size_t)b);
|
||||
d += b;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
return rad_planes_relayout(enc, sel, planes, n, dst, dst_bytes);
|
||||
}
|
||||
|
||||
template <RadLayoutFn L>
|
||||
static int avx_unrelayout_gemm_q(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
|
||||
const int* sel, const void* src, int64_t src_bytes,
|
||||
const RadTensor* planes, int n) {
|
||||
RadLayout lay{};
|
||||
const int st = L(p, n_p, operand, enc, sel, planes, n, &lay);
|
||||
if (st != RAD_OK) return st;
|
||||
if (lay.tag && std::strcmp(lay.tag, "m2.codes.meta") == 0) {
|
||||
if (src_bytes != lay.bytes) return RAD_E_SHAPE;
|
||||
const uint8_t* s = (const uint8_t*)src;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
const int64_t rb = rad_enc_row_bytes(planes[i].dtype, planes[i].shape[1]);
|
||||
const int64_t b = planes[i].shape[0] * rb;
|
||||
std::memcpy(planes[i].data, s, (size_t)b);
|
||||
s += b;
|
||||
}
|
||||
return RAD_OK;
|
||||
}
|
||||
return rad_planes_unrelayout(enc, sel, src, src_bytes, planes, n);
|
||||
}
|
||||
|
||||
template <RadLayoutFn L>
|
||||
static int avx_relayout_planes(const RadParam* p, int n_p, int operand, const RadEncoding* enc,
|
||||
const int* sel, const RadTensor* planes, int n, void* dst,
|
||||
|
||||
+11
-3
@@ -1385,6 +1385,14 @@ static int avx_moe_cb_unrelayout(const RadParam* p, int n_p, int operand, const
|
||||
avxp::avx_relayout_planes<avxp::lay>, nullptr, avxp::avx_unrelayout_planes<avxp::lay>, \
|
||||
nullptr }
|
||||
|
||||
/* gemm_nt_q: its zero-point grids join scale and zero (avx_planes.h) while its m2xfp4 arm
|
||||
* concatenates codes and meta, so the row carries the hook's own relayout pair. */
|
||||
#define ROWL_GQ(nm, opname, fam, what, dts, fn, lay) \
|
||||
{ nm, opname, fam, what, SHAPE_ANY, dts, RAD_DOMAIN_HOST, 0, \
|
||||
nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr, fn, nullptr, avxp::lay, \
|
||||
avxp::avx_relayout_gemm_q<avxp::lay>, nullptr, avxp::avx_unrelayout_gemm_q<avxp::lay>, \
|
||||
nullptr }
|
||||
|
||||
#define DT_FLOAT "bf16 f16 f32 fp8_e4m3 fp8_e5m2 i8 i32, chosen per operand from its own dtype"
|
||||
#define DT_ANY "any dtype rad_types.h names, per operand"
|
||||
#define DT_FLOAT_CB DT_FLOAT "; or u4 / u5 codes into an f32 table, joined (w4nl* / w5nl*)"
|
||||
@@ -1547,9 +1555,9 @@ SIMPLE("avx_gated_quant_fp8", "gated_quant_fp8", "quant",
|
||||
SIMPLE("avx_gemm_nt", "gemm_nt", "gemm", "C = A B^T, f32 accumulate, cache-blocked", DT_FLOAT,
|
||||
L(gemm_nt)),
|
||||
SIMPLE("avx_gemm_nt_bias", "gemm_nt_bias", "gemm", "C = A B^T + bias", DT_FLOAT, L(gemm_nt_bias)),
|
||||
ROWL("avx_gemm_nt_q", "gemm_nt_q", "gemm", "C = A B^T with group-scaled quantised operands",
|
||||
"i8 / fp8 / float A; w4, w2, mxfp4, i8 or float B; u2 / u4 / u5 codes with an f16 scale "
|
||||
"and integer zero per group, joined; f32 accumulate", L(gemm_nt_q), avx_layout_gemm_q),
|
||||
ROWL_GQ("avx_gemm_nt_q", "gemm_nt_q", "gemm", "C = A B^T with group-scaled quantised operands",
|
||||
"i8 / fp8 / float A; w4, w2, mxfp4, m2xfp4, i8 or float B; u2 / u4 / u5 codes with an f16 "
|
||||
"scale and integer zero per group, joined; f32 accumulate", L(gemm_nt_q), avx_layout_gemm_q),
|
||||
SIMPLE("avx_gemm_nt_q_bias", "gemm_nt_q_bias", "gemm", "quantised C = A B^T + bias",
|
||||
"i8 / fp8 / float A; w4, w2, mxfp4, i8 or float B; f32 accumulate", L(gemm_nt_q_bias)),
|
||||
SIMPLE("avx_lora_shrink", "lora_shrink", "gemm", "t = x A[slot]^T per row, base rows untouched",
|
||||
|
||||
Reference in new issue
Block a user