Skip to content

Make the gradient-accumulation rewrite emit group references - #10818

Closed
vineethsaivs wants to merge 3 commits into
unslothai:mainfrom
vineethsaivs:fix/grad-accum-rewrite-backrefs
Closed

vineethsaivs wants to merge 3 commits into
unslothai:mainfrom
vineethsaivs:fix/grad-accum-rewrite-backrefs

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

What breaks

patch_gradient_accumulation_fix rewrites training_step so accelerator.backward runs on the divided loss instead of the full one. The regex is raw. The replacement is not:

"else:\n"
"\2if num_items_in_batch is None:\n"
"\3loss = loss / self.args.gradient_accumulation_steps\n"
"\1self.accelerator.backward(loss, **kwargs)",

"\1", "\2" and "\3" are octal escapes. Python turns them into 0x01, 0x02 and 0x03 long before re.sub gets a chance to read them as group references, so the three captured indents come back as control characters:

'else:\n\x02if num_items_in_batch is None:\n\x03loss = loss / self.args.gradient_accumulation_steps\n\x01self.accelerator.backward(loss, **kwargs)'

The exec(function, globals()) two lines below then raises SyntaxError: unexpected indent rather than installing the patched training_step.

What changed

A raw template with \g<1>, \g<2>, \g<3>. The rewrite now produces what it was written to produce:

    else:
        if num_items_in_batch is None:
            loss = loss / self.args.gradient_accumulation_steps
        self.accelerator.backward(loss, **kwargs)
    return loss.detach()

How live is it

Not live. The shape this matches is what training_step looked like before huggingface/transformers#35808 reordered the two. I checked the whole pinned range in pyproject.toml (transformers>=4.51.3,<=5.5.0):

version pattern matches
4.51.3 (minimum pin) no
4.56.0 no
5.5.0 (maximum pin) no

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.py reads the pattern and the replacement out of _utils.py with ast and runs them against the old training_step shape, since _utils.py cannot be imported without an accelerator.

  • the rewrite moves backward below the division
  • the result carries no control characters, parses, and keeps backward one indent level outside the if
2 passed in 0.20s

The second fails on main:

assert not any(character in rewritten for character in "\x01\x02\x03")
E  assert not True

@danielhanchen

Copy link
Copy Markdown
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.

Use backreference syntax so the captured indents come back as text rather than control bytes.
@danielhanchen

Copy link
Copy Markdown
Member

Thank you for the careful write-up and the test, @vineethsaivs. You are right that the replacement used plain "\1", "\2", "\3" strings, which become control bytes instead of group references.

I checked whether that line can ever run. The pattern does not match Trainer.training_step in any transformers release from 4.45.2 through 5.18.0, including our pinned floor (4.51.3) and ceiling (5.5.0), so re.sub returns the source unchanged and the replacement is never used. A base vs head SFT and inference run showed no difference, as expected.

Since the branch is unreachable on every supported version, the cleaner fix is to remove the dead rewrite rather than repair it, as you suggested in the description. Closing this one in favour of that cleanup. Thanks again for spotting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants