Mixture of experts
×Each position runs 8 of 384 routed experts, weighted by the router, plus one shared expert that every position runs.
Why this design
Many small experts. With few large experts, the tokens sent to one expert are more likely to cover very different kinds of knowledge, and one expert has to hold all of it. The authors of DeepSeekMoE split each expert into smaller ones and activate more of them at the same parameters and compute, so that knowledge can be divided among experts; the number of possible combinations of active experts grows enormously. DeepSeekMoE §3.1
Shared experts. Tokens routed to different experts may still need some common knowledge, and several routed experts may then each learn it. Experts that run for every token are meant to hold that common part, which the authors of the design say reduces redundancy among the routed experts and leaves them more specialised. This model has one such expert. DeepSeekMoE §3.2
384 experts. At a fixed number of active parameters, Kimi K2’s authors found that more experts in all, a higher sparsity, steadily lowered training and validation loss. More sparsity also makes the infrastructure harder, so they settle on 8 active experts of 384, a sparsity of 48, against DeepSeek-V3’s 8 of 256. Kimi K2.5 keeps Kimi K2’s choice. Kimi K2 report §2.3, pp. 6–7
All 19 steps
- in
- [B batch, T positions, 7168 d]
- out
- [B batch, T positions, 7168 d]
Parameters16,958,226,816 × 60 layers
16,958,226,816 parameters per instance, across its learned components
| Component | Calculation | Parameters |
|---|---|---|
| Router | 2,752,896 | |
| Routed expert | 44,040,192 × 384 | 16,911,433,728 |
| Shared expert | 44,040,192 | |
| Per-instance total | 16,958,226,816 | |
One in each of the 60 MoE layers, each with its own weights.
Weights · one MoE layer
A token uses 8 of the 384 routed experts, the shared expert and the router: 399,114,624 parameters (2.4%).
INT4 routed experts, their bfloat16 scales (one per 32 weights) left out; the shared expert in bfloat16; the router’s matrix and bias in bfloat16. The checkpoint stores the bias in float32; loaded, it takes the type the loader is given, and the atlas counts it in the configuration’s bfloat16.
config.json 39, 95–129modeling_deepseek.py 429–433, 530–541, 544–609Explanation
The implementation flattens batch and positions into one token axis for the experts and restores the shape (modeling_deepseek.py 534, 537–538).
Sources
In codeself.mlp
Direct evidence
The MoE layer holds the routed experts, the router and one shared expert MLP of n_shared_experts × moe_intermediate_size features.
modeling_deepseek.py 503–528 ↗ · moonshotai/Kimi-K2.5 @ 4d01dfe0332d
Excerpt
if hasattr(config, "ep_size") and config.ep_size > 1:
assert config.ep_size == dist.get_world_size()
self.ep_size = config.ep_size
self.experts_per_rank = config.n_routed_experts // config.ep_size
self.ep_rank = dist.get_rank()
self.experts = nn.ModuleList([
(DeepseekV3MLP(config,
intermediate_size=config.moe_intermediate_size)
if i >= self.ep_rank * self.experts_per_rank
and i < (self.ep_rank + 1) * self.experts_per_rank else None)
for i in range(config.n_routed_experts)
])
else:
self.ep_size = 1
self.experts_per_rank = config.n_routed_experts
self.ep_rank = 0
self.experts = nn.ModuleList([
DeepseekV3MLP(config,
intermediate_size=config.moe_intermediate_size)
for i in range(config.n_routed_experts)
])
self.gate = MoEGate(config)
if config.n_shared_experts is not None:
intermediate_size = config.moe_intermediate_size * config.n_shared_experts
self.shared_experts = DeepseekV3MLP(
config=config, intermediate_size=intermediate_size)Each token’s output is the router-weighted sum of its selected experts plus the shared expert applied to every token; tokens are sorted by expert, each expert runs on its tokens, and outputs are scattered back.
modeling_deepseek.py 530–609 ↗ · moonshotai/Kimi-K2.5 @ 4d01dfe0332d
Excerpt
def forward(self, hidden_states):
identity = hidden_states
orig_shape = hidden_states.shape
topk_idx, topk_weight = self.gate(hidden_states)
hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
flat_topk_idx = topk_idx.view(-1)
if not self.training:
y = self.moe_infer(hidden_states, topk_idx,
topk_weight).view(*orig_shape)
if self.config.n_shared_experts is not None:
y = y + self.shared_experts(identity)
return y… lines 542–543 omitted …
def moe_infer(self, x, topk_ids, topk_weight):
cnts = topk_ids.new_zeros((topk_ids.shape[0], len(self.experts)))
cnts.scatter_(1, topk_ids, 1)
tokens_per_expert = cnts.sum(dim=0)
idxs = topk_ids.view(-1).argsort()
sorted_tokens = x[idxs // topk_ids.shape[1]]
sorted_tokens_shape = sorted_tokens.shape
if self.ep_size > 1:
tokens_per_ep_rank = tokens_per_expert.view(self.ep_size,
-1).sum(dim=1)
tokens_per_expert_group = tokens_per_expert.new_empty(
tokens_per_expert.shape[0])
dist.all_to_all_single(tokens_per_expert_group, tokens_per_expert)
output_splits = (tokens_per_expert_group.view(
self.ep_size, -1).sum(1).cpu().numpy().tolist())
gathered_tokens = sorted_tokens.new_empty(
tokens_per_expert_group.sum(dim=0).cpu().item(),
sorted_tokens.shape[1])
input_split_sizes = tokens_per_ep_rank.cpu().numpy().tolist()
dist.all_to_all(
list(gathered_tokens.split(output_splits)),
list(sorted_tokens.split(input_split_sizes)),
)
tokens_per_expert_post_gather = tokens_per_expert_group.view(
self.ep_size, self.experts_per_rank).sum(dim=0)
gatherd_idxs = np.zeros(shape=(gathered_tokens.shape[0], ),
dtype=np.int32)
s = 0
for i, k in enumerate(tokens_per_expert_group.cpu().numpy()):
gatherd_idxs[s:s + k] = i % self.experts_per_rank
s += k
gatherd_idxs = gatherd_idxs.argsort()
sorted_tokens = gathered_tokens[gatherd_idxs]
tokens_per_expert = tokens_per_expert_post_gather
tokens_per_expert = tokens_per_expert.cpu().numpy()
outputs = []
start_idx = 0
for i, num_tokens in enumerate(tokens_per_expert):
end_idx = start_idx + num_tokens
if num_tokens == 0:
continue
expert = self.experts[i + self.ep_rank * self.experts_per_rank]
tokens_for_this_expert = sorted_tokens[start_idx:end_idx]
expert_out = expert(tokens_for_this_expert)
outputs.append(expert_out)
start_idx = end_idx
outs = torch.cat(outputs,
dim=0) if len(outputs) else sorted_tokens.new_empty(0)
if self.ep_size > 1:
new_x = torch.empty_like(outs)
new_x[gatherd_idxs] = outs
gathered_tokens = new_x.new_empty(*sorted_tokens_shape)
dist.all_to_all(
list(gathered_tokens.split(input_split_sizes)),
list(new_x.split(output_splits)),
)
outs = gathered_tokens
new_x = torch.empty_like(outs)
new_x[idxs] = outs
final_out = (new_x.view(
*topk_ids.shape, -1).type(topk_weight.dtype).mul_(
topk_weight.unsqueeze(dim=-1)).sum(dim=1).type(new_x.dtype))
return final_out