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.

Fused MoE decode kernels: shared experts above 8 rows go through the regular tail

YanissAmz8a9371cb6421
diff --git a/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp.cpp b/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp.cpp
index c4b5f44..eedd0f6 100644
--- a/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp.cpp
+++ b/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp.cpp
@@ -68,7 +68,8 @@ void BC_BlockSparseMLP::run_bszN
(
const at::Tensor& y,
at::Tensor& selected_experts,
- at::Tensor& routing_weights
+ at::Tensor& routing_weights,
+ bool with_shared
)
{
// Two fused launches per layer cover every (token, expert) slot of the batch (see
@@ -86,18 +87,13 @@ void BC_BlockSparseMLP::run_bszN
// Shared experts run through their own multi-row graph first (same stream); the kernel adds
// the result into the routed sum, through the sigmoid gate when there is one
c10::optional<at::Tensor> sh_o;
- if (shared_experts)
+ if (shared_experts && with_shared)
{
- // The shared-expert graphs cover MAX_BSZN rows each: larger batches run as consecutive slices
- // (rows are independent), the first one included so that <= MAX_BSZN rows are unchanged
+ // The shared-expert graphs cover MAX_BSZN rows: larger batches take their shared experts from the caller
+ TORCH_CHECK(num_tokens <= MAX_BSZN, "run_bszN: shared experts above MAX_BSZN rows are the caller's");
+ at::Tensor x_dense = y.unsqueeze(0);
at::Tensor out_d_sh_n = out_d_sh.value().slice(1, 0, num_tokens);
- for (int a = 0; a < num_tokens; a += MAX_BSZN)
- {
- int n = std::min(MAX_BSZN, num_tokens - a);
- at::Tensor x_dense = y.slice(0, a, a + n).unsqueeze(0);
- at::Tensor d_n = out_d_sh_n.slice(1, a, a + n);
- shared_experts->run_bszN(x_dense, d_n);
- }
+ shared_experts->run_bszN(x_dense, out_d_sh_n);
sh_o = out_d_sh_n;
}
exl3_moe_coop_run(coop_p, coop_K_gu, coop_K_d, coop_cb, y, selected_experts, routing_weights, sh_o);
diff --git a/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp.h b/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp.h
index 9bc7d45..2f4f111 100644
--- a/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp.h
+++ b/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp.h
@@ -155,7 +155,8 @@ struct BC_BlockSparseMLP
(
const at::Tensor& y,
at::Tensor& selected_experts,
- at::Tensor& routing_weights
+ at::Tensor& routing_weights,
+ bool with_shared = true
);
void run_single_expert_gr
diff --git a/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp_bc.h b/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp_bc.h
index ba2accd..1c06f68 100644
--- a/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp_bc.h
+++ b/exllamav3/exllamav3_ext/libtorch/blocksparse_mlp_bc.h
@@ -105,6 +105,6 @@ py::class_<BC_BlockSparseMLP, std::shared_ptr<BC_BlockSparseMLP>>(m, "BC_BlockSp
py::arg("down_bias_ptrs") = py::none(),
py::arg("act_relu2") = false
)
-.def("run_bszN", &BC_BlockSparseMLP::run_bszN)
+.def("run_bszN", &BC_BlockSparseMLP::run_bszN, py::arg("y"), py::arg("selected_experts"), py::arg("routing_weights"), py::arg("with_shared") = true)
.def("run_single_expert", &BC_BlockSparseMLP::run_single_expert)
.def("run_single_expert_dq", &BC_BlockSparseMLP::run_single_expert_dq);
diff --git a/exllamav3/modules/block_sparse_mlp.py b/exllamav3/modules/block_sparse_mlp.py
index 589a766..50e7897 100644
--- a/exllamav3/modules/block_sparse_mlp.py
+++ b/exllamav3/modules/block_sparse_mlp.py
@@ -2862,9 +2862,12 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
# quantized paths apply), so this is the last tier
else:
assert bszn_eligible
- self.bc.run_bszN(y, selected_experts, routing_weights)
+ # Shared experts: merged in the kernel from their 8-row graph, or above MAX_BSZN rows by the regular tail below
+ # (their R-row kernels; measured 1.2-2 ms/step faster at 10-32 rows than 8-row slices of the graph)
+ sh_in_bc = self.bc_sh_exp and bsz <= MAX_BSZN
+ self.bc.run_bszN(y, selected_experts, routing_weights, sh_in_bc)
final_hidden_states = self.experts_cfg.out_bszn[:bsz].view(eshape)
- bc_sh_exp = self.bc_sh_exp
+ bc_sh_exp = sh_in_bc
# CPU tail partial folds in before the post norms (nonlinear: they must see the
# complete routed sum)