RMSNormVJP backward writes a full {n_rows, D} gw_temp intermediate - #4293
Open
JasonHonKL wants to merge 3 commits into
Open
RMSNormVJP backward writes a full {n_rows, D} gw_temp intermediate#4293JasonHonKL wants to merge 3 commits into
RMSNormVJP backward writes a full {n_rows, D} gw_temp intermediate#4293JasonHonKL wants to merge 3 commits into
Conversation
Contributor
Author
|
@zcbenz await verification I guess haha. Quite some changes mostly related to calculate before merge like divide and conquer. |
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.
Proposed changes
The GPU backward pass of
mx.fast.rms_normcomputes the weight gradientgwin two full-tensor passes:
vjp_rms_single_row/vjp_rms_looped(one threadgroup per row) write eachrow's
gwcontribution into a temporary of shape{n_rows, D}— the samesize as the whole input (
mlx/backend/metal/normalization.cpp:142-152).gw_tempand sums the rowsinto the final
{D}output (normalization.cpp:200-205).For each element of
gwthis moves2 · n_rows · D · itemsizebytes throughDRAM (write + re-read) that contain no information beyond partial sums waiting
to be added. On a GPU this is pure bandwidth waste — the arithmetic to combine
rows inside the kernel is free by comparison. It also forces a transient
allocation the size of the input.
Result
POC
(AI generated)
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes