fix(perf): prevent fp32 promotion in model hot paths#20
Merged
Conversation
inureyes
added a commit
that referenced
this pull request
May 18, 2026
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Generalises the bf16/f16 dtype-preservation pattern that landed for
gpt-ossin #17 to every MoE / hot-path model in the tree. The same FP32 promotion that crushed gpt-oss decode (19 → 113 tok/s) was hiding in 25+ other models — anywhere the expert MoE combine, the activation helper, or the router cast back through aneinsum/softmaxboundary.Cherry-picked from the internal repo (
mlxcel-internalcommit616c4704); the internaldocs/model_tests_m5max.mdupdate from the original commit was intentionally excluded (it referenced an internal-only baseline doc that was never in the public repo).What changed
Shared helper (
src/models/switch_layers.rs, +70):moe_weighted_sum(expert_out, scores, output_dtype)— replaces the oldnkh,nk->nheinsum contraction (which promoted to FP32 on M5 for bf16/f16 activations) with mlx-lm'sy * scores[..., None]+sum(axis=-2)pattern, with scores cast to the expert output dtype and the final result restored to the hidden/residual dtype.FFI helpers (
mlxcel-core/cpp/mlx_cxx_bridge.cpp+ffi_tests.rs+180):utils (
mlxcel-core/src/utils.rs+25): supporting dtype helpers.Model call-site updates (25 model files): each MoE/router path now routes its combine through
moe_weighted_sumand casts router/expert scores back to the input dtype before residual add. Models touched:deepseek, deepseek_v3, deepseek_v32, ernie4_5_moe, exaone_moe, glm4_moe,
glm4_moe_lite, gpt_oss, hunyuan_moe, kimi_linear, minimax, mistral4,
mixtral, moondream3, olmoe, phimoe, qwen2_moe, qwen3_5, qwen3_moe,
qwen3_next, qwen3_vl_moe, solar_open, step3p5
Verification
make verify-fmt— cleanmake verify-clippy(CI-faithful:--all-targets --features metal,accelerate -- -D warnings) — clean in 2m04smake verify-testskipped here (15-30 min release-mode run); the underlying commit is already validated inmlxcel-internalagainst the M5 Max benchmark sweep.Why a single commit
The 25-model sweep is one logical change — same pattern, same root cause, same fix shape per file. Splitting it would multiply review cost without adding signal.