Make the gradient-accumulation rewrite emit group references - #10818
Open
vineethsaivs wants to merge 2 commits into
Open
Make the gradient-accumulation rewrite emit group references#10818vineethsaivs wants to merge 2 commits into
vineethsaivs wants to merge 2 commits into
Conversation
patch_gradient_accumulation_fix moves accelerator.backward below the division so
the gradients carry the divided loss. Its regex is raw but its replacement is
not, so "\1", "\2" and "\3" are octal escapes that Python turns into 0x01, 0x02
and 0x03 long before re.sub can read them as group references.
The three captured indents therefore came back as control characters:
'else:\n\x02if num_items_in_batch is None:\n\x03loss = loss / ...'
and the exec two lines down raises SyntaxError instead of installing the patched
training_step.
Use a raw template with \g<1>, \g<2> and \g<3>. The rewrite then produces what it
always meant to.
Not reachable today: the shape it matches predates
huggingface/transformers#35808, and 4.51.3, 4.56.0 and 5.5.0 all fail the match,
which is the whole pinned range. So this is a landmine rather than a live crash,
and deleting the block instead is a fair call. Say the word and I will.
Test: tests/test_gradient_accumulation_rewrite.py reads the pattern and the
replacement out of _utils.py with ast and runs them against the old
training_step. One test pins the ordering, the other that the result carries no
control characters, parses, and keeps backward one indent level outside the
division. The second fails on main.
for more information, see https://pre-commit.ci
Member
|
Confirmed the replacement in unsloth/models/_utils.py still uses plain backslash-digit strings, so the captured indents come back as control bytes if that pattern ever matches again. Will get this reviewed, and I may take you up on the delete-the-block variant instead. |
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 breaks
patch_gradient_accumulation_fixrewritestraining_stepsoaccelerator.backwardruns on the divided loss instead of the full one. The regex is raw. The replacement is not:"\1","\2"and"\3"are octal escapes. Python turns them into0x01,0x02and0x03long beforere.subgets a chance to read them as group references, so the three captured indents come back as control characters:The
exec(function, globals())two lines below then raisesSyntaxError: unexpected indentrather than installing the patchedtraining_step.What changed
A raw template with
\g<1>,\g<2>,\g<3>. The rewrite now produces what it was written to produce:How live is it
Not live. The shape this matches is what
training_steplooked like before huggingface/transformers#35808 reordered the two. I checked the whole pinned range inpyproject.toml(transformers>=4.51.3,<=5.5.0):So today the branch is a silent no-op, and this is a landmine rather than a crash anyone is hitting. Deleting the block is a fair call instead and I am happy to send that version if you prefer it. I did not assume it, because the other three replacements in the same function are also inert on these versions and I did not want to decide that for you.
Tests
tests/test_gradient_accumulation_rewrite.pyreads the pattern and the replacement out of_utils.pywithastand runs them against the oldtraining_stepshape, since_utils.pycannot be imported without an accelerator.backwardbelow the divisionbackwardone indent level outside theifThe second fails on main: