[Bugfix][NPU] Fix/Refactor routed scaling factor application in MoE routing - #31449
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the NPU MoE top-k logic to scale topk_weights by routed_scaling_factor when renormalize is false, and adjusts the condition for applying this scaling factor on the output. The review feedback highlights potential runtime TypeError issues if topk_config.routed_scaling_factor is None, suggesting safety guards to handle None values during multiplication and when passing the factor to the custom NPU operator.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| else: | ||
| topk_weights = topk_weights * topk_config.routed_scaling_factor |
There was a problem hiding this comment.
If topk_config.routed_scaling_factor is None (since it is an Optional[float]), multiplying topk_weights by it directly will raise a TypeError at runtime. We should guard this multiplication by checking if routed_scaling_factor is not None.
| else: | |
| topk_weights = topk_weights * topk_config.routed_scaling_factor | |
| elif topk_config.routed_scaling_factor is not None: | |
| topk_weights = topk_weights * topk_config.routed_scaling_factor |
| topk_config.routed_scaling_factor | ||
| if topk_config.apply_routed_scaling_factor_on_output | ||
| if (not renormalize or topk_config.apply_routed_scaling_factor_on_output) | ||
| else 1 |
There was a problem hiding this comment.
If topk_config.routed_scaling_factor is None, passing it directly to the custom NPU op npu_moe_gating_top_k might cause a TypeError or unexpected behavior in the PyTorch C++ binding. It is safer to guard against None and default to 1.0.
topk_config.routed_scaling_factor
if (not renormalize or topk_config.apply_routed_scaling_factor_on_output)
and topk_config.routed_scaling_factor is not None
else 1.0859f69b to
a4c6f63
Compare
|
/tag-and-rerun-ci |
Motivation
Currently, the
routed_scaling_factoris applied inconsistently depending on whetherrenormalizeis enabled. Whenrenormalize=True, the scaling factor is not applied after weight normalization, leading to incorrect router weights and potentially degraded model performance. This PR unifies the application logic to ensure the scaling factor is always correctly applied, regardless of the renormalization setting.Modifications
topk_weightshandlingrenormalize=True, callapply_routed_scaling_after_renorm(topk_weights, topk_config)after renormalization to apply the scaling factor.renormalize=False, multiplytopk_weightsbytopk_config.routed_scaling_factordirectly.routed_scaling_factorto the expert implementation constructor.These changes eliminate the conditional omission of the scaling factor and align the code with the design expectation that routed_scaling_factor should always affect the router weights/output.
Accuracy Tests
Verified on Ascend910 NPU:
test_bs_1_speedtest_gsm8kSpeed Tests and Profiling
No speed regression. Throughput restored from 65.54 to 77.65 token/s (matching the pre-regression baseline of ~78 token/s). The fix only restores a multiplication that was incorrectly removed, adding negligible overhead.
Checklist
test_npu_llada2_mini.pycovers this regressionCI States
Latest PR Test (Base): ❌ Run #29574948888
Latest PR Test (Extra): ❌ Run #29574948768