Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Fix Precision Mismatch in MoE Router of DeepSeek V2/V3 Models and Fused Kernels (BF16 -> FP32) #14027

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DaizeDong
Copy link

@DaizeDong DaizeDong commented Feb 28, 2025

Description

  • Updated the computation of router_logits in the MoE gate to use FP32 instead of the default BF16 to enhance numerical stability.
  • Ensured that activation functions (softmax and sigmoid) for router_logits operate in FP32, improving the precision of exponential calculations.
  • Addressed a related TODO in vllm/_custom_ops.py.

Results

The additional computation cost is negligible as the routing operation is lightweight. However, this adjustment results in a consistent performance improvement, with Winogrande accuracy increasing from 71.27 to 71.43 for the DeepSeek-V2-Lite model.

Before Winogrande Accuracy:

image

After Winogrande Accuracy:

image

References

FP32 Computation for router_logits

  • This approach is a new trick in Megatron (Issue 1421) that stabilizes MoE pretraining.
  • The original implementations of DeepSeek V2 and V3 also adopt this method.

FP32 Activation for softmax and sigmoid

  • Similarly, DeepSeek V2 and V3 use FP32 for activation calculations.

Potential Enhancement

Currently, the precision fix for router_logits is implemented by explicitly setting the router weights to FP32. A potentially better approach would be to retain the router weights in FP16 and adjust computation precision dynamically during execution. However, I am not fully familiar with the quant_config in ReplicatedLinear, so I believe someone with more expertise in this area could refine this further.

For now, this implementation effectively ensures stable and correct model behavior.

Copy link

👋 Hi! Thank you for contributing to the vLLM project.

💬 Join our developer Slack at https://slack.vllm.ai to discuss your PR in #pr-reviews, coordinate on features in #feat- channels, or join special interest groups in #sig- channels.

Just a reminder: PRs would not trigger full CI run by default. Instead, it would only run fastcheck CI which starts running only a small and essential subset of CI tests to quickly catch errors. You can run other CI tests on top of those by going to your fastcheck build on Buildkite UI (linked in the PR checks section) and unblock them. If you do not have permission to unblock, ping simon-mo or khluu to add you in our Buildkite org.

Once the PR is approved and ready to go, your PR reviewer(s) can run CI to test the changes comprehensively before merging.

To run CI, PR reviewers can either: Add ready label to the PR or enable auto-merge.

🚀

@@ -114,6 +114,7 @@ def __init__(
self.gate = ReplicatedLinear(config.hidden_size,
config.n_routed_experts,
bias=False,
params_dtype=torch.float32,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel bias should also initiate to fp32.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems not necessary for a change here?
I found that the initialization method for router bias is the same in vllm and HF for DS V3, using nn.Parameter that defaults as FP32. And there is no type conversion before calculation in the original HF implementation.

@mgoin
Copy link
Member

mgoin commented Feb 28, 2025

We should run some performance benchmarks to ensure we aren't regressing cc @tlrmchlsmth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants