Repository navigation
Warn once when gradient accumulation falls back to a mean of micro-batch means - #17921
Merged
Merged
Conversation
…tch means Remote-code heads that return their own CrossEntropyLoss mean cannot take num_items_in_batch, so Trainer averages micro-batch means and every micro-batch weighs the same whatever its token count. This was silent; now get_batch_samples logs one warning per head class when gradient accumulation is on and labels are present.
Member
Author
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
danielhanchen
added a commit
that referenced
this pull request
Nov 16, 2026
…apper above it (rest shard red on main) (#17952) ## Summary `Repo tests (CPU, rest)` fails on current main (seen on #17921, #17934 and the staging run of #17904) in: ``` tests/test_flex_mask_graph_breaks.py::test_compiled_flex_mask_has_no_graph_breaks_and_matches_eager E torch._dynamo.exc.Unsupported: Data-dependent branching E File ".../unsloth_zoo/temporary_patches/misc.py", line 1318, in flex_attention_mask E return original(*bound.args, **bound.kwargs) E File ".../unsloth/import_fixes.py", line 2471, in flex_attention_mask E return original(*args, **kwargs) E File ".../transformers/masking_utils.py", line 685, in flex_attention_mask E if attention_mask is not None and not fast_all(attention_mask): ``` It started with unslothai/unsloth-zoo#2551 (the test passes with zoo at its parent and fails at it). That PR wraps the registered `flex_attention` mask builder to keep the padding `mask_mod` during training, and forwards every call as `original(*bound.args, **bound.kwargs)`, i.e. by position. The wrapper it sits on is Unsloth's `fix_transformers_flex_mask_graph_breaks` patch, which only handled keyword calls: with any positional argument it called transformers directly, so while Dynamo traces the `fast_all(attention_mask)` branch came back and `fullgraph` tracing broke. Both patches are right on their own; the Unsloth one was fragile about how it is called, so this hardens it rather than changing zoo. ## Change - `unsloth/import_fixes.py`: the flex mask patch binds positional arguments to the original's signature and continues with keywords (new helper `_keyword_arguments`, which merges `**kwargs` parameters and returns `None` for anything it cannot express as keywords; then the call goes through unchanged, as before). - `tests/test_flex_mask_graph_breaks.py`: new `test_a_wrapper_forwarding_by_position_keeps_the_compiled_path` registers a wrapper that forwards by position above the patch (as zoo's does) and checks the `fullgraph` compile of a padded batch matches eager. It does not depend on which zoo is installed. ## Testing | unsloth | unsloth_zoo | `test_flex_mask_graph_breaks.py` | |---|---|---| | main | main | 1 failed (the CI failure) | | this PR | main | 4 passed, 1 skipped | | this PR | before #2551 | 4 passed, 1 skipped | | main | before #2551 | 1 failed: the new positional-wrapper test | CPU, torch 2.10, transformers 5.17 (the CI pins for this shard).
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.
Gradient accumulation is token-weighted for every native transformers family, but a model whose head computes its own mean (remote code that returns a
CrossEntropyLossmean and accepts nonum_items_in_batch, for example NemotronH remote or Qwen-1 style heads) silently falls back to a mean of micro-batch means. On our remote-code test ports the padded GA gradient is 0.42 to 0.82 off the token-weighted reference, and nothing said so.This adds a one-time warning per head class in Unsloth's
get_batch_samplesdispatch. It fires only when gradient accumulation is above 1, the trainer cannot pass the token count to the model, there is no custom loss, and the batch has labels, so GRPO / DPO style trainers and every native family stay silent. No behaviour changes.Tests
tests/test_ga_micro_mean_loss_warning.py(CPU, < 1 s): warns once for a head that ignoresnum_items_in_batch, stays silent for one that accepts it. Fails on main, passes here (transformers 5.23 / TRL 1.18 and 4.57.6 / TRL 0.22.2). Through a real SFTTrainer run (GA = 2, remote-code port) it fires exactly once.