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 prefill: add the gated shared expert in one pass

YanissAmzfc0e7ef9e0e6
diff --git a/exllamav3/modules/block_sparse_mlp.py b/exllamav3/modules/block_sparse_mlp.py
index 74a7858..f824cde 100644
--- a/exllamav3/modules/block_sparse_mlp.py
+++ b/exllamav3/modules/block_sparse_mlp.py
@@ -2072,7 +2072,22 @@ class BlockSparseMLP(BlockSparseMLP_CPU, Module):
final_hidden_states = self.routed_post_norm.forward(final_hidden_states, params)
# Shared experts
- if self.shared_experts and not bc_sh_exp:
+ # EXL3_SHGATE_FUSE (default 1, read per call): prefill rows with a shared gate take the shared expert's fp16 down output
+ # and add float(y) * sigmoid(gate) into the routed sum in one pass (gr_pf.shgate_add), replacing the fp32 widen copy, the
+ # torch multiply and the torch add of add_sigmoid_gate's fallback. Same arithmetic, bit-identical. 0 goes back.
+ if (self.shared_experts and not bc_sh_exp and self.shared_gate and bsz > 32 and not self.tp_reduce
+ and not self.shared_experts_post_norm and final_hidden_states.dtype == torch.float
+ and final_hidden_states.is_contiguous() and os.environ.get("EXL3_SHGATE_FUSE", "1") != "0"):
+ sh_params = {**params, "moe_valu": True} if _MOE_VALU else dict(params)
+ sh_params["nc2_down_half"] = True
+ yh = self.shared_experts.forward(x, sh_params)
+ z = self.shared_gate.forward(x, params)
+ if yh.dtype == torch.half and yh.is_contiguous() and z.dtype == torch.float and z.is_contiguous():
+ from .gated_delta_net_fn.gr_pf import shgate_add
+ shgate_add(final_hidden_states.view(-1, self.hidden_size), yh.view(-1, self.hidden_size), torch.sigmoid(z))
+ else:
+ ext.add_sigmoid_gate(yh.float(), z.float(), final_hidden_states)
+ elif self.shared_experts and not bc_sh_exp:
y = self.shared_experts.forward(x, {**params, "moe_valu": True} if _MOE_VALU else params)
if pre_norm_reduce:
params["backend"].all_reduce(y, True)
diff --git a/exllamav3/modules/gated_delta_net_fn/gr_pf.py b/exllamav3/modules/gated_delta_net_fn/gr_pf.py
index ab15dc0..d7f42df 100644
--- a/exllamav3/modules/gated_delta_net_fn/gr_pf.py
+++ b/exllamav3/modules/gated_delta_net_fn/gr_pf.py
@@ -107,3 +107,26 @@ def gr_apply_norm(x: torch.Tensor, y: torch.Tensor, post: torch.Tensor, w_h: tor
# fma=True is bit-identical to ext.hc_apply (hipcc contracts post * y + x; checked on GPU, R=1..2048)
_gr_apply_norm_kernel[(rows,)](x, y, post, w_h, normed, float(eps), y_pitch, D, H, triton.next_power_of_2(D), fma,
num_warps = 8, enable_fp_fusion = False)
+
+
+def _shgate_add_kernel(f_ptr, y_ptr, s_ptr, D: tl.constexpr, BD: tl.constexpr):
+ # EXL3_SHGATE_FUSE: f[r] += float(y[r]) * s[r], the arithmetic of the shared expert's fp32 widen + add_sigmoid_gate's
+ # torch fallback (z.add_(x * sigmoid(y))): exact widen, one fp32 multiply, one fp32 add (no fma: enable_fp_fusion = False)
+ r = tl.program_id(0).to(tl.int64)
+ o = tl.program_id(1) * BD + tl.arange(0, BD)
+ m = o < D
+ s = tl.load(s_ptr + r)
+ y = tl.load(y_ptr + r * D + o, mask = m, other = 0.0).to(tl.float32)
+ f = tl.load(f_ptr + r * D + o, mask = m, other = 0.0)
+ tl.store(f_ptr + r * D + o, f + y * s, mask = m)
+
+
+def shgate_add(f: torch.Tensor, y: torch.Tensor, s: torch.Tensor):
+ """f (R, D) fp32 contiguous, updated in place; y (R, D) fp16 contiguous (the shared expert's down output); s (R,) or (R, 1)
+ fp32 = torch.sigmoid(gate). Bit-identical to f.add_(y.float() * s)."""
+ R, D = f.shape
+ assert f.dtype == torch.float32 and f.is_contiguous() and y.dtype == torch.float16 and y.is_contiguous() and y.shape == f.shape
+ assert s.dtype == torch.float32 and s.is_contiguous() and s.numel() == R
+ BD = 1024
+ _shgate_add_kernel[(R, triton.cdiv(D, BD))](f, y, s, D, BD, num_warps = 4, enable_fp_fusion = False)