Skip to content

Warn once when gradient accumulation falls back to a mean of micro-batch means - #17921

Merged
danielhanchen merged 2 commits into
mainfrom
ga-micro-mean-warning
Nov 16, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
ga-micro-mean-warning

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Gradient accumulation is token-weighted for every native transformers family, but a model whose head computes its own mean (remote code that returns a CrossEntropyLoss mean and accepts no num_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_samples dispatch. 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 ignores num_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.

…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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T07:10:52.580975Z fcf08d1 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: fcf08d111b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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
danielhanchen merged commit 2d213db into main Nov 16, 2026
@danielhanchen
danielhanchen deleted the ga-micro-mean-warning branch November 16, 2026 08:31
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant