Repository navigation
Conversation
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.
Fixes #111
This PR fixes a loss-scaling issue when
micro_acc_steps > 1, where the accumulated gradients and logged metrics (fvu,auxk,multi_topk) were scaled down bymicro_acc_steps.Background
In PR #138 (commit
f19b274), micro-batching was restored by chunking activations and normalizing each chunk using thetotal_varianceof the full batch.Since all chunks use the full-batch variance, their losses already sum up to the loss of the full batch. However, in
sparsify/trainer.py,acc_stepswas still calculated as:Each chunk was then divided by
acc_stepsbefore backward:This caused the loss to be divided by
micro_acc_stepsan extra time.On the unpatched
main, I observed the following gradient norms compared to an unchunked batch:micro_acc_steps=2: 0.50xmicro_acc_steps=3: 0.33xmicro_acc_steps=8: 0.125xThe logged FVU was also under-reported by a factor of
micro_acc_steps.The existing
test_micro_acc_steps_matches_unchunked_updatedid not catch this because it compared parameter weights after an Adam step. Adam's first update is largely insensitive to gradient scaling.Changes Made
sparsify/trainer.py: Changedacc_stepsto divide only bygrad_acc_steps.tests/test_micro_acc_steps.py:multi_topk, andauxk.config.pyandREADME.mdto clarify thatmicro_acc_stepsonly applies toloss_fn="fvu", sinceceandklrequire full-sequence reconstruction.Testing
main, the new tests reproduced the issue: 7 failed, 4 passed.pre-commitandruff checkboth pass.Related: #131, #138