Archived
moe_gemm_q_mod4: the quantised grouped GEMM over four classes of experts
moe_gemm_q over four tables instead of two: expert e is entry e / 4 of class e % 4. A separate op, because the declaration divides a table op's weights over its table operands by count. The arguments are repacked to fit four classes in 256 bytes; moe_gemm_q's arithmetic is unchanged. Where weights do not divide evenly over the tables, the first tables take one more each (510 experts: 128/128/127/127). libref and libavx serve the op.
This commit is contained in:
1 parent
bfae23e368
commit
bc2e6ba614
17 files changed
+634
-137
No files matched your search
+38
-21
@@ -173,8 +173,8 @@ int ref_moe_scatter(const RadArgs* a, RadStream) {
|
||||
* here. Only the WEIGHT scale grid is block-shaped -- one scale per 128x128 tile of an expert --
|
||||
* while the activation's is per row per group of K, which is `rblk = 1`, which is what
|
||||
* make_scales derives on its own from the two extents. */
|
||||
static int moe_gemm_impl(const RadArgs* a, bool quant) {
|
||||
const int n_opd = quant ? 9 : 5;
|
||||
static int moe_gemm_impl(const RadArgs* a, bool quant, int ncls = 2) {
|
||||
const int n_opd = quant ? (ncls == 4 ? 13 : 9) : 5;
|
||||
if (!have(a, n_opd)) return RAD_E_INVAL;
|
||||
const RadTensor* A = &a->t[0];
|
||||
const RadTensor* as_ = quant ? t_in(a, 1) : nullptr;
|
||||
@@ -182,12 +182,18 @@ static int moe_gemm_impl(const RadArgs* a, bool quant) {
|
||||
const RadTensor* ws_ = quant ? t_in(a, 3) : nullptr;
|
||||
const RadTensor* sorted = &a->t[quant ? 4 : 2];
|
||||
const RadTensor* off = &a->t[quant ? 5 : 3];
|
||||
const RadTensor* Y = &a->t[quant ? 8 : 4];
|
||||
/* THE QUANTISED ARM'S EXPERTS ARE TWO TABLES BY PARITY -- see ref_registry.cpp -- and the
|
||||
* plain arm's are one, which is the same walk with every expert in the first class. */
|
||||
const RadTensor* Y = &a->t[quant ? (ncls == 4 ? 12 : 8) : 4];
|
||||
/* THE QUANTISED ARM'S EXPERTS ARE TWO TABLES BY PARITY -- see ref_registry.cpp -- or, for
|
||||
* `moe_gemm_q_mod4`, four by the expert's number mod 4; the plain arm's are one, which is the
|
||||
* same walk with every expert in the first class. */
|
||||
const RadTensor* W1 = quant ? &a->t[6] : W;
|
||||
const RadTensor* ws1_ = quant ? t_in(a, 7) : nullptr;
|
||||
const bool two = quant;
|
||||
const int nc = quant ? ncls : 1;
|
||||
const RadTensor* Wq[4] = { W, W1, quant && nc == 4 ? &a->t[8] : W,
|
||||
quant && nc == 4 ? &a->t[10] : W };
|
||||
const RadTensor* wsq[4] = { ws_, ws1_, quant && nc == 4 ? t_in(a, 9) : nullptr,
|
||||
quant && nc == 4 ? t_in(a, 11) : nullptr };
|
||||
|
||||
/* "sorted" means `a` already has one row per (token, slot) in the scatter's order, which is
|
||||
* what the DOWN projection of a routed block reads -- its input is the first projection's
|
||||
@@ -199,19 +205,29 @@ static int moe_gemm_impl(const RadArgs* a, bool quant) {
|
||||
|
||||
const int64_t N0 = p_int(a, "N", W->rank >= 3 ? W->shape[1] : 0);
|
||||
const int64_t K0 = p_int(a, "K", W->rank >= 3 ? W->shape[2] : 0);
|
||||
const int64_t N1 = two ? p_int(a, "N_odd", N0) : N0;
|
||||
const int64_t K1 = two ? p_int(a, "K_odd", K0) : K0;
|
||||
/* Each class's geometry, defaulting to class 0's: N_odd/K_odd at two classes, N_q/K_q at
|
||||
* four. */
|
||||
static const char* const kN4[4] = { "N", "N_1", "N_2", "N_3" };
|
||||
static const char* const kK4[4] = { "K", "K_1", "K_2", "K_3" };
|
||||
int64_t Nq[4] = { N0, N0, N0, N0 }, Kq[4] = { K0, K0, K0, K0 };
|
||||
for (int q = 1; q < nc; ++q) {
|
||||
Nq[q] = p_int(a, nc == 4 ? kN4[q] : "N_odd", N0);
|
||||
Kq[q] = p_int(a, nc == 4 ? kK4[q] : "K_odd", K0);
|
||||
}
|
||||
const int64_t parts = two ? p_int(a, "parts", 1) : 1;
|
||||
const int64_t n_expert = p_int(a, "n_expert", W->rank >= 3 ? W->shape[0] : 0);
|
||||
const int64_t top_k = p_int(a, "top_k", 1);
|
||||
if (N0 <= 0 || K0 <= 0 || N1 <= 0 || K1 <= 0 || parts <= 0 || n_expert <= 0 || top_k <= 0)
|
||||
return RAD_E_SHAPE;
|
||||
if (N0 % parts || N1 % parts) return RAD_E_SHAPE;
|
||||
/* The output is the wider class's width, the activation the longer class's K. */
|
||||
const int64_t N = N0 > N1 ? N0 : N1, K = K0 > K1 ? K0 : K1;
|
||||
const int64_t ne0 = two ? (n_expert + 1) / 2 : n_expert, ne1 = two ? n_expert / 2 : 0;
|
||||
if (W->rank < 3 || W->shape[0] < ne0) return RAD_E_SHAPE;
|
||||
if (two && (W1->rank < 3 || W1->shape[0] < ne1)) return RAD_E_SHAPE;
|
||||
if (parts <= 0 || n_expert <= 0 || top_k <= 0) return RAD_E_SHAPE;
|
||||
/* The output is the widest class's width, the activation the longest class's K. Class q
|
||||
* holds ceil((n - q) / nc) experts. */
|
||||
int64_t N = 0, K = 0;
|
||||
for (int q = 0; q < nc; ++q) {
|
||||
if (Nq[q] <= 0 || Kq[q] <= 0 || Nq[q] % parts) return RAD_E_SHAPE;
|
||||
if (Nq[q] > N) N = Nq[q];
|
||||
if (Kq[q] > K) K = Kq[q];
|
||||
const int64_t neq = (n_expert + nc - 1 - q) / nc;
|
||||
if (neq > 0 && (Wq[q]->rank < 3 || Wq[q]->shape[0] < neq)) return RAD_E_SHAPE;
|
||||
}
|
||||
const int64_t T = numel(Y) / N;
|
||||
if (T <= 0 || numel(sorted) < T || numel(off) < n_expert + 1) return RAD_E_SHAPE;
|
||||
|
||||
@@ -235,17 +251,17 @@ static int moe_gemm_impl(const RadArgs* a, bool quant) {
|
||||
/* Per-expert readings, built once outside the row loop: a table lookup and a Scales are cheap,
|
||||
* and doing them per row would be T of them against n_expert. A null `data` means the operand
|
||||
* could not be read as a table at all, which is refused before the loop because an OpenMP body
|
||||
* cannot return. Expert e is entry e / 2 of class e % 2 when there are two classes. */
|
||||
* cannot return. Expert e is entry e / nc of class e % nc. */
|
||||
std::vector<RadTensor> we((size_t)n_expert), wse((size_t)n_expert);
|
||||
std::vector<Mat> mw((size_t)n_expert);
|
||||
std::vector<Scales> SW((size_t)n_expert);
|
||||
std::vector<int64_t> en((size_t)n_expert), ek((size_t)n_expert);
|
||||
for (int64_t e = 0; e < n_expert; ++e) {
|
||||
const bool odd = two && (e & 1);
|
||||
const RadTensor* tw = odd ? W1 : W;
|
||||
const RadTensor* ts = odd ? ws1_ : ws_;
|
||||
const int64_t ix = two ? e / 2 : e;
|
||||
const int64_t n = odd ? N1 : N0, k = odd ? K1 : K0;
|
||||
const int q = (int)(e % nc);
|
||||
const RadTensor* tw = Wq[q];
|
||||
const RadTensor* ts = wsq[q];
|
||||
const int64_t ix = e / nc;
|
||||
const int64_t n = Nq[q], k = Kq[q];
|
||||
en[(size_t)e] = n;
|
||||
ek[(size_t)e] = k;
|
||||
we[(size_t)e] = expert_entry(tw, ix);
|
||||
@@ -301,6 +317,7 @@ static int moe_gemm_impl(const RadArgs* a, bool quant) {
|
||||
|
||||
int ref_moe_gemm(const RadArgs* a, RadStream) { return moe_gemm_impl(a, false); }
|
||||
int ref_moe_gemm_q(const RadArgs* a, RadStream) { return moe_gemm_impl(a, true); }
|
||||
int ref_moe_gemm_q_mod4(const RadArgs* a, RadStream) { return moe_gemm_impl(a, true, 4); }
|
||||
|
||||
/* ------------------------------------------------------------------ moe_gather */
|
||||
/* y[t] = sum over the token's slots of expert_w[t, j] * y_expert[position of (t, j)].
|
||||
|
||||
@@ -120,6 +120,7 @@ int ref_router_topk (const RadArgs*, RadStream);
|
||||
int ref_moe_scatter (const RadArgs*, RadStream);
|
||||
int ref_moe_gemm (const RadArgs*, RadStream);
|
||||
int ref_moe_gemm_q (const RadArgs*, RadStream);
|
||||
int ref_moe_gemm_q_mod4 (const RadArgs*, RadStream);
|
||||
int ref_scale_rows (const RadArgs*, RadStream);
|
||||
int ref_moe_gather (const RadArgs*, RadStream);
|
||||
int ref_row_topk (const RadArgs*, RadStream);
|
||||
@@ -231,6 +232,7 @@ REF_SHAPE_FN(ref_router_topk);
|
||||
REF_SHAPE_FN(ref_moe_scatter);
|
||||
REF_SHAPE_FN(ref_moe_gemm);
|
||||
REF_SHAPE_FN(ref_moe_gemm_q);
|
||||
REF_SHAPE_FN(ref_moe_gemm_q_mod4);
|
||||
REF_SHAPE_FN(ref_scale_rows);
|
||||
REF_SHAPE_FN(ref_moe_gather);
|
||||
REF_SHAPE_FN(ref_row_topk);
|
||||
|
||||
@@ -344,6 +344,19 @@ static const RadParamSpec pMoeGemmQ[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_
|
||||
static const RadOperandSpec oMoeGemmQ[] = { OPD("a"), OPD_O("a_scale"), WTAB("w"), WTAB("w_scale"),
|
||||
OPD("sorted_tok"), OPD("expert_offset"),
|
||||
WTAB("w_odd"), WTAB("w_odd_scale"), OUT("y") };
|
||||
/* THE SAME OP OVER FOUR CLASSES, by the expert's number mod 4: `w`/`w_scale` class 0 at [N, K],
|
||||
* `w_q`/`w_q_scale` class q at [N_q, K_q], expert e at entry e / 4 of class e % 4 -- how four
|
||||
* ranks take turns with the extra block of a five-block expert. libr4d's schema states the same. */
|
||||
static const RadParamSpec pMoeGemmQMod4[] = { P_INT("M"), P_INT("N"), P_INT("K"), P_INT("n_expert"),
|
||||
P_INT("top_k"), P_INT("group"), P_STR("a_order"),
|
||||
P_STR("dtype"), P_INTO("N_1"), P_INTO("K_1"),
|
||||
P_INTO("N_2"), P_INTO("K_2"), P_INTO("N_3"),
|
||||
P_INTO("K_3"), P_INTO("parts") };
|
||||
static const RadOperandSpec oMoeGemmQMod4[] = { OPD("a"), OPD_O("a_scale"), WTAB("w"),
|
||||
WTAB("w_scale"), OPD("sorted_tok"),
|
||||
OPD("expert_offset"), WTAB("w_1"),
|
||||
WTAB("w_1_scale"), WTAB("w_2"), WTAB("w_2_scale"),
|
||||
WTAB("w_3"), WTAB("w_3_scale"), OUT("y") };
|
||||
|
||||
/* `shared` and `shared_gate` PRESENT = the shared arm folded in, `y = bf16(shared * act(gate)) +
|
||||
* bf16(gathered)`, which is scale_rows' fused gate-and-add taken one op further back. `act` is
|
||||
@@ -770,6 +783,11 @@ static const RadOpSchema kSchemas[] = {
|
||||
"two tables by parity: `w` the even ones at [N, K], `w_odd` the odd ones at [N_odd, K_odd], "
|
||||
"expert e at entry e/2. The output is max(N, N_odd) wide in `parts` stacked parts, and a "
|
||||
"narrower class fills each part's leading columns and zeros the rest." },
|
||||
{ "moe_gemm_q_mod4", ARR(pMoeGemmQMod4), ARR(oMoeGemmQMod4),
|
||||
"moe_gemm_q over FOUR weight-table classes by the expert's number mod 4: `w` class 0 at [N, K], "
|
||||
"`w_q` class q at [N_q, K_q], expert e at entry e/4 of class e%4. The output is as wide as the "
|
||||
"widest class in `parts` stacked parts, and a narrower class fills each part's leading columns "
|
||||
"and zeros the rest." },
|
||||
{ "moe_gather", ARR(pMoeGather), ARR(oMoeGather),
|
||||
"Scatters the sorted expert output back and weights it. The sum over a token's slots runs in "
|
||||
"ASCENDING SLOT order, which is what makes it reproducible." },
|
||||
@@ -1048,6 +1066,9 @@ SIMPLE("ref_moe_gemm", "moe_gemm", "moe", "grouped GEMM over the expert-sorted r
|
||||
ref_moe_gemm),
|
||||
SIMPLE("ref_moe_gemm_q", "moe_gemm_q", "moe",
|
||||
"grouped GEMM over the expert-sorted rows, quantised operands", DT_FLOAT, ref_moe_gemm_q),
|
||||
SIMPLE("ref_moe_gemm_q_mod4", "moe_gemm_q_mod4", "moe",
|
||||
"grouped GEMM over the expert-sorted rows, quantised operands, four classes by number mod 4",
|
||||
DT_FLOAT, ref_moe_gemm_q_mod4),
|
||||
SIMPLE("ref_moe_gather", "moe_gather", "moe", "weighted scatter back to token order", DT_FLOAT,
|
||||
ref_moe_gather),
|
||||
SIMPLE("ref_rope_table", "rope_table", "attn", "cos/sin at each position's own row", DT_FLOAT, ref_rope_table),
|
||||
|
||||
@@ -1145,6 +1145,44 @@ int ref_moe_gemm_q_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* ou
|
||||
return o.finish("moe_gemm_q", operand, out);
|
||||
}
|
||||
|
||||
/* The four-class form: class q at [N_q, K_q], defaulting to class 0's, holds the experts e with
|
||||
* e % 4 == q -- ceil((n - q) / 4) of them -- and `y` follows the last class. */
|
||||
int ref_moe_gemm_q_mod4_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0);
|
||||
const int64_t N0 = gi(p, n_p, "N", 0), K0 = gi(p, n_p, "K", 0);
|
||||
const int64_t Nq[4] = { N0, gi(p, n_p, "N_1", N0), gi(p, n_p, "N_2", N0), gi(p, n_p, "N_3", N0) };
|
||||
const int64_t Kq[4] = { K0, gi(p, n_p, "K_1", K0), gi(p, n_p, "K_2", K0), gi(p, n_p, "K_3", K0) };
|
||||
const int64_t ne = gi(p, n_p, "n_expert", 0);
|
||||
const int64_t k = gi(p, n_p, "top_k", 0);
|
||||
int64_t g = gi(p, n_p, "group", 0);
|
||||
if (M <= 0 || ne <= 0 || k <= 0) return RAD_E_UNSUPPORTED;
|
||||
int64_t N = 0, K = 0;
|
||||
for (int q = 0; q < 4; ++q) {
|
||||
if (Nq[q] <= 0 || Kq[q] <= 0) return RAD_E_UNSUPPORTED;
|
||||
if (Nq[q] > N) N = Nq[q];
|
||||
if (Kq[q] > K) K = Kq[q];
|
||||
}
|
||||
if (g <= 0) g = K;
|
||||
const uint32_t D = act_dt(p, n_p);
|
||||
Opd o;
|
||||
const int64_t AR = sorted_a_order(p, n_p) ? M * k : M;
|
||||
auto cls = [&](int q) {
|
||||
const int64_t nq = (ne + 3 - q) / 4;
|
||||
o.t((uint32_t)RAD_F8E4M3, { nq, Nq[q], Kq[q] });
|
||||
o.t(D, { nq, (Nq[q] + g - 1) / g, (Kq[q] + g - 1) / g });
|
||||
};
|
||||
o.t((uint32_t)RAD_F8E4M3, { AR, K }); /* a -- E4M3 codes, see `a_order` */
|
||||
o.t((uint32_t)RAD_F32, { AR, (K + g - 1) / g }); /* a_scale */
|
||||
cls(0); /* w, w_scale -- class 0 */
|
||||
o.t_idx({ M * k }, M * k, -1, 0); /* sorted_tok */
|
||||
o.t_idx({ ne + 1 }, M * k + 1, -1, 0); /* expert_offset */
|
||||
cls(1);
|
||||
cls(2);
|
||||
cls(3);
|
||||
o.t(D, { M * k, N }); /* y -- in SORTED order */
|
||||
return o.finish("moe_gemm_q_mod4", operand, out);
|
||||
}
|
||||
|
||||
int ref_moe_gather_shape(const RadParam* p, int n_p, int operand, RadOpdDesc* out) {
|
||||
const int64_t M = gi(p, n_p, "M", 0), n = gi(p, n_p, "n", 0);
|
||||
const int64_t k = gi(p, n_p, "top_k", 0);
|
||||
|
||||
Reference in new issue
Block a user