Skip to content

Commit

Permalink
[Bugfix] Fix deepseekv3 gate bias error (vllm-project#12002)
Browse files Browse the repository at this point in the history
Signed-off-by: mgoin <[email protected]>
Co-authored-by: mgoin <[email protected]>
  • Loading branch information
2 people authored and Ubuntu committed Jan 19, 2025
1 parent a5a061a commit 0e86693
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions vllm/model_executor/layers/fused_moe/fused_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,10 @@ def grouped_topk(hidden_states: torch.Tensor,
raise ValueError(f"Unsupported scoring function: {scoring_func}")

if e_score_correction_bias is not None:
scores.add_(e_score_correction_bias.unsqueeze(0))
# Store original scores before applying correction bias. We use biased
# scores for expert selection but original scores for routing weights
original_scores = scores
scores = scores + e_score_correction_bias.unsqueeze(0)

num_token = scores.shape[0]
group_scores = scores.view(num_token, num_expert_group,
Expand All @@ -510,10 +513,16 @@ def grouped_topk(hidden_states: torch.Tensor,
num_token, num_expert_group,
scores.shape[-1] // num_expert_group).reshape(num_token, -1) # [n, e]
tmp_scores = scores.masked_fill(~score_mask.bool(), 0.0) # [n, e]
topk_weights, topk_ids = torch.topk(tmp_scores,
k=topk,
dim=-1,
sorted=False)

if e_score_correction_bias is not None:
topk_ids = torch.topk(tmp_scores, k=topk, dim=-1, sorted=False)[1]
# Use original unbiased scores for the routing weights
topk_weights = original_scores.gather(1, topk_ids)
else:
topk_weights, topk_ids = torch.topk(tmp_scores,
k=topk,
dim=-1,
sorted=False)

if renormalize:
topk_weights = topk_weights / topk_weights.sum(dim=-1, keepdim=True)
Expand Down

0 comments on commit 0e86693

Please sign in to comment.