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
@@ -250,6 +250,17 @@ std::vector<Case> build_cases() {
|
||||
add("moe_gemm_q", { HI("M", 9), HI("N", 64), HI("K", 256), HI("K_odd", 128),
|
||||
HI("n_expert", 8), HI("top_k", 2), HI("group", 128),
|
||||
HS("a_order", "sorted"), HS("dtype", "w8a8") });
|
||||
/* Four classes at three widths -- a four-rank slice of a five-block expert is two blocks
|
||||
* of one class and one of each other -- over ten experts, so classes 2 and 3 hold one
|
||||
* fewer than 0 and 1: the gate/up pair and the down projection. */
|
||||
add("moe_gemm_q_mod4", { HI("M", 9), HI("N", 128), HI("K", 128), HI("N_1", 256),
|
||||
HI("N_2", 128), HI("N_3", 128), HI("parts", 2),
|
||||
HI("n_expert", 10), HI("top_k", 2), HI("group", 128),
|
||||
HS("a_order", "token"), HS("dtype", "w8a8") });
|
||||
add("moe_gemm_q_mod4", { HI("M", 9), HI("N", 64), HI("K", 128), HI("K_1", 128),
|
||||
HI("K_2", 256), HI("K_3", 128), HI("n_expert", 10),
|
||||
HI("top_k", 2), HI("group", 128), HS("a_order", "sorted"),
|
||||
HS("dtype", "w8a8") });
|
||||
}
|
||||
add("moe_scatter", { HI("M", 9), HI("n_expert", 8), HI("top_k", 2), HI("expert_base", 0) });
|
||||
|
||||
|
||||
@@ -104,6 +104,7 @@ int avx_isa_force(int level);
|
||||
/* ---- mixture of experts */ \
|
||||
X(router_topk) X(moe_scatter) X(moe_gemm) X(moe_gemm_q) \
|
||||
X(moe_gather) X(row_topk) X(row_topk_merge) X(logit_rerank) \
|
||||
X(moe_gemm_q_mod4) \
|
||||
/* ---- gated delta net */ \
|
||||
X(gdn_conv_prep) X(gdn_conv_update) X(gdn_kkt_solve) X(gdn_chunk_scan) \
|
||||
X(gdn_recurrent_update) X(gdn_conv_recurrent_update) X(gdn_gated_rmsnorm) \
|
||||
|
||||
+37
-21
@@ -294,8 +294,8 @@ AVX_KERNEL(moe_scatter) {
|
||||
* whose meaning changes with its extent is exactly the shape a checker cannot see: if both
|
||||
* implementations gather they agree with each other, and a harness that sizes the operand the way
|
||||
* the gather wants never exercises the other reading. */
|
||||
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 (!rad_args_have(a, n_opd)) return RAD_E_INVAL;
|
||||
const RadTensor* A = &a->t[0];
|
||||
const RadTensor* as_ = quant ? rad_arg_in(a, 1) : nullptr;
|
||||
@@ -303,12 +303,18 @@ static int moe_gemm_impl(const RadArgs* a, bool quant) {
|
||||
const RadTensor* ws_ = quant ? rad_arg_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];
|
||||
const RadTensor* Y = &a->t[quant ? (ncls == 4 ? 12 : 8) : 4];
|
||||
/* THE QUANTISED ARM'S EXPERTS ARE TWO TABLES BY PARITY, expert e at entry e / 2 of class
|
||||
* e % 2; the plain arm's are one. */
|
||||
* e % 2 -- or for `moe_gemm_q_mod4` four, entry e / 4 of class e % 4; the plain arm's are
|
||||
* one. */
|
||||
const bool two = quant;
|
||||
const int nc = quant ? ncls : 1;
|
||||
const RadTensor* W1 = two ? &a->t[6] : W;
|
||||
const RadTensor* ws1_ = two ? rad_arg_in(a, 7) : nullptr;
|
||||
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 ? rad_arg_in(a, 9) : nullptr,
|
||||
quant && nc == 4 ? rad_arg_in(a, 11) : nullptr };
|
||||
|
||||
const char* ord = p_str(a, "a_order", "token");
|
||||
const bool sorted_a = ord && std::strcmp(ord, "sorted") == 0;
|
||||
@@ -316,19 +322,28 @@ 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, 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;
|
||||
|
||||
@@ -371,11 +386,11 @@ static int moe_gemm_impl(const RadArgs* a, bool quant) {
|
||||
* be read as a table at all, which is refused BEFORE any parallel region because an OpenMP
|
||||
* body cannot return. */
|
||||
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];
|
||||
we[e] = expert_entry(tw, ix);
|
||||
if (!we[e].data) return RAD_E_STRIDE;
|
||||
if (we[e].rank < 2 || we[e].shape[0] < n || we[e].shape[1] < k) return RAD_E_SHAPE;
|
||||
@@ -427,8 +442,8 @@ static int moe_gemm_impl(const RadArgs* a, bool quant) {
|
||||
for (int64_t i = 0; i < T; ++i) {
|
||||
if (atok[i] < 0) { stt(Y, i * y_rs + n * y_cs, 0.0f); continue; }
|
||||
const int64_t e = aexp[i];
|
||||
const bool odd = two && (e & 1);
|
||||
const int64_t fc = (odd ? N1 : N0) / parts, ke = odd ? K1 : K0;
|
||||
const int q = (int)(e % nc);
|
||||
const int64_t fc = Nq[q] / parts, ke = Kq[q];
|
||||
if (j >= fc) { stt(Y, i * y_rs + n * y_cs, 0.0f); continue; }
|
||||
const int64_t wr = pp * fc + j;
|
||||
/* The expert's weight row is packed when the expert CHANGES, which -- because the
|
||||
@@ -454,6 +469,7 @@ static int moe_gemm_impl(const RadArgs* a, bool quant) {
|
||||
|
||||
AVX_KERNEL(moe_gemm) { return moe_gemm_impl(a, false); }
|
||||
AVX_KERNEL(moe_gemm_q) { return moe_gemm_impl(a, true); }
|
||||
AVX_KERNEL(moe_gemm_q_mod4) { 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)].
|
||||
|
||||
@@ -263,6 +263,18 @@ static const RadParamSpec pMoeGemmQ[] = { P_INT("M"), P_INT("N"), P_INT("K"),
|
||||
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 over four classes by the expert's number mod 4, `w_q` class q at [N_q, K_q]. Mirrors
|
||||
* libref's and libr4d's schema exactly. */
|
||||
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` + `shared_gate` present = the shared arm folded in: y = narrow(shared *
|
||||
* act(gate)) + narrow(gathered), the unfused pair's two narrows carried in the stores. `act` is
|
||||
@@ -500,6 +512,8 @@ static const RadOpSchema kSchemas[] = {
|
||||
{ "moe_gemm", ARR(pMoeGemm), ARR(oMoeGemm), "grouped GEMM over the expert-sorted rows." },
|
||||
{ "moe_gemm_q", ARR(pMoeGemmQ), ARR(oMoeGemmQ),
|
||||
"grouped GEMM over the expert-sorted rows, quantised operands." },
|
||||
{ "moe_gemm_q_mod4", ARR(pMoeGemmQMod4), ARR(oMoeGemmQMod4),
|
||||
"grouped GEMM over the expert-sorted rows, quantised operands, four classes by number mod 4." },
|
||||
{ "moe_gather", ARR(pMoeGather), ARR(oMoeGather), "weighted scatter back to token order." },
|
||||
{ "row_topk", ARR(pRowTopk), ARR(oRowTopk), "exact per-row top-R." },
|
||||
{ "row_topk_merge", ARR(pRowTopkMerge), ARR(oRowTopkMerge),
|
||||
@@ -685,6 +699,9 @@ SIMPLE("avx_moe_gemm", "moe_gemm", "moe", "grouped GEMM over the expert-sorted r
|
||||
L(moe_gemm)),
|
||||
SIMPLE("avx_moe_gemm_q", "moe_gemm_q", "moe",
|
||||
"grouped GEMM over the expert-sorted rows, quantised operands", DT_FLOAT, L(moe_gemm_q)),
|
||||
SIMPLE("avx_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, L(moe_gemm_q_mod4)),
|
||||
SIMPLE("avx_moe_gather", "moe_gather", "moe", "weighted scatter back to token order", DT_FLOAT,
|
||||
L(moe_gather)),
|
||||
SIMPLE("avx_row_topk", "row_topk", "moe", "exact per-row top-R", DT_FLOAT, L(row_topk)),
|
||||
|
||||
Reference in new issue
Block a user