Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

MoE decode gate/up: share each unit's k loop over three blocks when the units fit one per SM

YanissAmz659ed624abff
diff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_coop.cu b/exllamav3/exllamav3_ext/quant/exl3_moe_coop.cu
index ae78cd6..1099eb6 100644
--- a/exllamav3/exllamav3_ext/quant/exl3_moe_coop.cu
+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_coop.cu
@@ -80,6 +80,22 @@ static int moe_coop_sk_mode()
return mode;
}
+// Gate/up k-split for batches whose units already fit one per SM (decode: 48 units for 82 SMs on Flint): every
+// unit's k-slice loop is a latency chain of one block, so it is shared by this many blocks (as long as the grid
+// stays resident), the last arrival summing the fp32 partial rows (d_out scratch) in fixed order. The sum is not
+// rounded to fp16 before the output rotation as the unsplit unit's output is, so results differ from it by that
+// rounding. EXL3_MOE_COOP_KSPLIT_A = 1 restores the unsplit unit, bit-identical to the previous kernel
+static int moe_coop_ksplit_a_mode()
+{
+ static int mode = -2;
+ if (mode == -2)
+ {
+ const char* env = std::getenv("EXL3_MOE_COOP_KSPLIT_A");
+ mode = env ? atoi(env) : 3;
+ }
+ return mode;
+}
+
// Grid (resident blocks x SMs) and SM count, per (kernel, smem, device), queried once
static int moe_coop_sk_grid(const MoeCoopKernel& k, int device, int& sms)
{
@@ -159,13 +175,23 @@ void exl3_moe_coop_launch(const MoeCoopParams& p_in, int K_gu, int K_d, int cb,
{
int sms;
const int grid_sk = moe_coop_sk_grid(ka, device, sms);
- // Units that fit one per SM are whole-unit blocks either way (same sums, same results): keep the
- // per-unit grid rather than launching a grid of resident blocks that mostly exit at once
if (sk == 2 || base_a > sms)
{
grid_a = grid_sk;
p.ksplit_a = -((sk == 2 || grid_a <= sms ? 0 : sms) + 1);
}
+ else if (p.ksplit_a == 1 && p.Hi / 16 >= 96)
+ {
+ // Units fit one per SM, so the hybrid would run whole units anyway (and launch a grid of resident
+ // blocks that mostly exit at once): per-unit grid, k-split while the grid stays resident
+ const int ks = std::min({ moe_coop_ksplit_a_mode(), max_split, grid_sk / std::max(base_a, 1),
+ exl3_moe_coop_ns::sk_rows(p.slots_max, slots, nproj, p.I, p.Ho) });
+ if (ks > 1)
+ {
+ p.ksplit_a = ks;
+ grid_a = base_a * ks;
+ }
+ }
}
void* args[] = { (void*) &p };
diff --git a/exllamav3/exllamav3_ext/quant/exl3_moe_coop_kernel.cuh b/exllamav3/exllamav3_ext/quant/exl3_moe_coop_kernel.cuh
index 3a7a097..e340301 100644
--- a/exllamav3/exllamav3_ext/quant/exl3_moe_coop_kernel.cuh
+++ b/exllamav3/exllamav3_ext/quant/exl3_moe_coop_kernel.cuh
@@ -867,9 +867,9 @@ __device__ __forceinline__ void a_unit
const int slots = p.bsz * p.topk;
void* C;
bool c_f32 = p.gu_f32;
- if (p.ksplit_a < 0 && seg[is_gate ? SEG_NPG : SEG_NPU] > 1)
+ if ((p.ksplit_a < 0 && seg[is_gate ? SEG_NPG : SEG_NPU] > 1) || p.ksplit_a > 1)
{
- // Stream-K segment of a shared unit: fp32 partial row in the d_out scratch
+ // Stream-K segment of a shared unit, or a split-k block: fp32 partial row in the d_out scratch
const int nproj = p.gated ? 2 : 1;
const int pi = is_gate ? 0 : nproj - 1;
C = (void*) (p.d_out + (size_t) (pi * sk_rows(p.slots_max, slots, nproj, p.I, p.Ho) + seg[SEG_Q]) * slots * p.I);
@@ -902,7 +902,7 @@ __device__ __forceinline__ void a_unit
auto load_sum = [&] (const void* buf, int np, int pi, float& v0, float& v1, float& v2, float& v3)
{
v0 = v1 = v2 = v3 = 0.0f;
- if (p.ksplit_a < 0 && np > 1)
+ if ((p.ksplit_a < 0 || p.ksplit_a > 1) && np > 1)
{
const int nproj = p.gated ? 2 : 1;
const int slots = p.bsz * p.topk;