perf(deepseek_v4): use mx.fast.rms_norm in HyperConnection and HyperHead#16
Open
0xClandestine wants to merge 1 commit intoBlaizzy:pc/add-deepseekv4flash-modelfrom
Conversation
Replace manual rsqrt(mean(x²)+eps) + scaled matmul with mx.fast.rms_norm throughout the HyperConnection/HyperHead compute paths: - Remove _rms_rsqrt helper (subsumed by _hc_mixes) - _hc_mixes: (flat @ fn_T) * rsqrt → rms_norm(flat) @ fn_T - _hyper_head_op: same rewrite - HyperConnection.compute_weights training path: unified with inference path - HyperHead.__call__ training path: same rewrite mx.fast.rms_norm dispatches a fused Metal kernel; the rewrite is algebraically exact ((x@W)*s = rms_norm(x)@W for per-row scalar s) and verified ~1.2× faster on DeepSeek-V4 geometry (HC=4, D=7168, flat_dim=28672).
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
Replaces the manual
rsqrt(mean(x²) + eps)+ scaled matmul pattern inHyperConnectionandHyperHeadwithmx.fast.rms_norm, which dispatches a fused Metal kernel._rms_rsqrthelper (subsumed)_hc_mixes:(flat @ fn_T) * rsqrt→mx.fast.rms_norm(flat, None, eps) @ fn_T_hyper_head_op: same rewriteHyperConnection.compute_weightstraining path: unified with inference path (both now go through_hc_mixes)HyperHead.__call__training path: same rewriteThe identity
(x @ W) * s = rms_norm(x) @ Wholds becauses = 1/rms(x)is a per-row scalar, so it distributes over matrix multiplication.Perf
Measured on M-series (DeepSeek-V4 geometry: HC=4, D=7168, flat_dim=28672):
_hc_mixes(B=2 L=256)_hyper_head(B=2 L=256)Verified numerically with phew-mlx equivalence checker (
SubstitutionClass.normed_matmul, atol=1e-3).