mstar.model.components.moe#
Mixture-of-Experts blocks.
Top-K router + dispatch path for the standard fused-expert checkpoint
layout (experts.gate_up_proj and experts.down_proj packed as
(num_experts, ...) parameters). Two block flavors:
SparseMoeBlock— Top-K MoE with no shared expert (e.g. the Qwen3-Omni Thinker text backbone).SparseMoeBlockWithSharedExpert— Top-K MoE plus a shared expert with sigmoid gating (e.g. the Qwen3-Omni Talker text backbone). The shared expert is passed in as annn.Moduleso callers can pick any MLP shape they need.
Parallel (TP-aware) variants:
When triton is installed and inputs are on CUDA, dispatch goes through
the Triton fused-MoE kernel in mstar.utils.fused_moe; otherwise it
falls back to the naive per-expert loop in dispatch_experts_fused().
Router contract#
Each block computes its expert assignment with a router. The block keeps
the router in self.gate. The default router is TopKRouter.
A router is an nn.Module. A router does not extend a base class. The
forward method of the router must obey this contract:
router(hidden_states, router_states=None)
-> (routing_weights, selected_experts, router_states_next)
The router reads these inputs:
hidden_statesis the input tensor. The shape is(tokens, hidden_size).router_statesis the state from the previous call. A stateless router ignores this input.
The router returns these outputs:
routing_weightsis the tensor of expert weights. The shape is(tokens, top_k).selected_expertsis the tensor of expert indices. The shape is(tokens, top_k). The type is int64.router_states_nextis the state for the next call. A stateless router returnsNone.
A stateless router ignores router_states. A stateless router returns
None for router_states_next.
A stateful router reads router_states. A stateful router returns a new
state. To thread the state through a block, set the return_router_states
flag on the block.
Functions
|
Naive per-expert dispatch using the fused HF checkpoint layout. |
Classes
|
TP-aware Top-K sparse MoE. |
TP-aware Top-K sparse MoE with a shared expert + sigmoid gating. |
|
|
Top-K sparse MoE with fused expert weights, no shared expert. |
|
Top-K sparse MoE with a shared expert + sigmoid gating. |
|
Softmax top-k router. |
- class mstar.model.components.moe.ParallelSparseMoeBlock(hidden_size, num_experts, num_experts_per_tok, moe_intermediate_size, norm_topk_prob=True, router=None, comm_group=None)[source]#
Bases:
ModuleTP-aware Top-K sparse MoE.
When
tp_size == 1, the forward is identical toSparseMoeBlock(full fused kernel, no communication). Whentp_size > 1, expert weights are sharded along the intermediate dimension and an all-reduce is inserted between the down-projection GEMM and the top-k sum-reduce.- Parameters:
- forward(hidden_states, router_states=None, *, return_router_states=False)[source]#
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
Bases:
ModuleTP-aware Top-K sparse MoE with a shared expert + sigmoid gating.
The shared expert should be a
ParallelGatedMLPconstructed with the samecomm_groupso its all-reduce is handled internally.- Parameters:
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mstar.model.components.moe.SparseMoeBlock(hidden_size, num_experts, num_experts_per_tok, moe_intermediate_size, norm_topk_prob=True, router=None)[source]#
Bases:
ModuleTop-K sparse MoE with fused expert weights, no shared expert.
- Expert weights match the HF fused checkpoint layout:
experts.gate_up_proj:(num_experts, 2 * moe_intermediate_size, hidden_size)experts.down_proj:(num_experts, hidden_size, moe_intermediate_size)
- Parameters:
- forward(hidden_states, router_states=None, *, return_router_states=False)[source]#
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
Bases:
ModuleTop-K sparse MoE with a shared expert + sigmoid gating.
Final output is:
out = routed(x) + sigmoid(shared_gate(x)) * shared_expert(x)
The shared expert is supplied by the caller (any
nn.Modulematching thehidden_size → hidden_sizeinterface). The routed path uses the same fused checkpoint layout asSparseMoeBlock.- Parameters:
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mstar.model.components.moe.TopKRouter(hidden_size, num_experts, num_experts_per_tok, norm_topk_prob=True)[source]#
Bases:
ModuleSoftmax top-k router. This is the default router for all MoE blocks.
See the module docstring for the router contract. This router is stateless. It ignores
router_states. It returnsNoneforrouter_states_next.- Parameters:
- mstar.model.components.moe.dispatch_experts_fused(hidden_states, gate_up_proj, down_proj, num_experts, selected_experts, routing_weights)[source]#
Naive per-expert dispatch using the fused HF checkpoint layout.
Used as a fallback when the Triton fused-MoE kernel isn’t available. Loops over the experts that received any tokens and runs SwiGLU per expert.
- Parameters:
hidden_states (Tensor) –
(tokens, hidden_size).gate_up_proj (Tensor) –
(num_experts, 2 * moe_intermediate_size, hidden_size).down_proj (Tensor) –
(num_experts, hidden_size, moe_intermediate_size).selected_experts (Tensor) –
(tokens, top_k)int64.routing_weights (Tensor) –
(tokens, top_k)float.num_experts (int)
- Return type: