rms_norm: fuse a residual add into RmsNorm - #2585
Closed
czoli1976 wants to merge 1 commit into
Closed
Conversation
A residual add feeding an RmsNorm over the trailing axis is two passes over the tensor where one would do. Add an AddRmsNorm op that evaluates both, exposing the sum as a second output because a transformer reads it again in the next block, and a declutter rule on RmsNorm that collapses the pair onto it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Draft — and after further measurement I recommend closing this rather than merging it. Leaving it up for the record and for the harness; the analysis below is the useful part.
Ported from vLLM #48757 ("Fuse Transformers Residual Add + RMSNorm").
AddRmsNormevaluates a residual add and anRmsNormover the trailing axis together, exposing the sum as a second output because a transformer reads it again in the next block;detect_add_rms_normonRmsNorm's declutter collapses the pair and rewires both consumer sets. It is correct —fuses_and_keeps_both_valuescompares output bytes against the undecluttered graph for f32 and f16 — and it is a wash:Why, in two layers
The implementation does not actually fuse anything at the memory level. It adds whole-tensor, clones, then normalises whole-tensor: read a, read b, write sum, read sum, write normed — exactly the unfused sequence, just inside one node. The read-back it was supposed to remove is still there. That is a flaw in this PR, not in the idea.
But the idea does not pay here either. Row-blocking it properly would remove one of eight tensor passes, 12.5% of traffic. That only converts if the pair is bandwidth-bound, and it is not:
At 8-28% of peak the bottleneck is compute and dependency latency, not traffic. For f16 in particular the cost is the whole-tensor widening inside
RmsNorm, which #2580 removes — a much larger effect than anything this fusion could reach.And the reason it cannot be made to pay the way vLLM's does: their kernel updates the residual in place in a preallocated activation buffer, so the sum genuinely costs nothing. tract's graph has no in-place contract, so the sum must be materialised, and the traffic is what it is.
If someone wants to revisit it, the only interesting target is a machine where this pair is bandwidth-bound — much lower bandwidth per unit of compute than an M1 Pro. I have not found one to test on, and I would not merge this on speculation.
Two traps worth recording
The first implementation used a hand-written scalar
*s = *s + *rfor the add and was 20% slower than unfused, because tract'sAddlowers to a vectorisedOptBinUnicast; it needstract_linalg::bin_unicast(dt, BinOp::Add). Separately, a criterion baseline compared across processes reported this change as -14% f16 / -6% f32; interleaving the arms showed the wash above. Cross-run criterion baselines are not reliable on this machine.Tests
tract-core 270 green.
cargo fmt --allclean;cargo clippyclean (the two remaining warnings are pre-existing on main).🍍