Repository navigation
Fix AdafactorBigVision dropping the factored row normalization for tall matrices - #2749
Merged
rwightman merged 1 commit intoAug 26, 2026
Conversation
…ll matrices _single_tensor_adafactor computes the row factor as exp_avg_sq_r / row_col_mean, where row_col_mean is meant to be the overall mean of the row accumulator. It was reduced over 'reduce_dc = dc - 1 if dc > dr else dc', a shift ported from the big_vision/optax source where the row accumulator is stored with the reduced axis removed. timm instead keeps that axis via keepdim=True, so exp_avg_sq_r retains full rank and the shift points at an already-size-1 axis whenever dc > dr (i.e. the larger dimension comes first, e.g. an [out, in] weight with out > in). In that case row_col_mean == exp_avg_sq_r, row_factor collapses to all ones, and the Adafactor row normalization is silently dropped, making the update depend on the orientation of the weight matrix. Reduce over dc directly, which is the mean over the retained axis. The factored update is now transpose-equivariant again. Adds a regression test asserting update(W.T) == update(W).T for a factored shape.
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
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.
What
AdafactorBigVisionsilently drops the factored row normalization for any 2D parameter whose larger dimension comes first (e.g. an[out, in]weight without > in, such as an MLP up-projection). The update then depends on the orientation of the weight matrix.Why
In
_single_tensor_adafactor, the row factor isexp_avg_sq_r / row_col_mean, whererow_col_meanis meant to be the overall mean of the row accumulator:The
dc - 1 if dc > drshift is ported from the big_vision / optax source, where the row accumulator is stored with the reduced axis removed (so a later index has to be shifted down by one). timm keeps that axis viakeepdim=True, soexp_avg_sq_rretains full rank and the shift is wrong: whendc > drit points at an axis that is already size 1. Thenrow_col_mean == exp_avg_sq_r,row_factor = (exp_avg_sq_r / row_col_mean).rsqrt()is identically 1, and the row normalization vanishes.Verified by running the optimizer: for grad shape
(30, 20)the update equalsgrad * col_factorwithrow_factor == 1exactly (diff0.0), deviates from the correctly-factored update by max abs0.771, and breaks transpose-equivariance by0.771— while the(20, 30)orientation (which hits thedc < drno-shift branch) matches the reference exactly. No test caught it becausetest_adafactoronly checks generic convergence, never the factored value.Fix
exp_avg_sq_rkeeps full ndim, so reduce overdcdirectly (the retained axis) — no optax axis-removal shift. This restores a nonzero row factor and makes the factored update transpose-equivariant again. Thedc < drorientation is unchanged (therereduce_dcalready equaleddc).Tests
Adds
test_adafactor_bv_factored_row_normalization, assertingupdate(W.T) == update(W).Tfor a factored shape (both orientations). It fails onmainand passes with the fix. Fulltests/test_optim.pyhas no new failures (the two pre-existing failures —test_kron[kron]torch.compile/inductor on this platform, and thecsgdpfull-run convergence flake — reproduce identically on cleanmain).The multi-tensor (
foreach=True) Adafactor path is an unimplemented stub, so this single-tensor path is the only affected site.