Repository navigation
Fix evaluate() under auto padding-free and keep compute_metrics rows per example - #13136
Conversation
…per example Outside train(), for_inference restores use_cache, so a packed eval batch builds a KV cache and transformers skips its packed-sequence mask: packed examples attend to each other and trainer.evaluate() reports a wrong loss (Gemma 3 270M 3.82 vs 2.37, Qwen3 0.6B 4.35 vs 1.10). Run packed eval batches with use_cache=False. compute_metrics / preprocess_logits_for_metrics were meant to block padding-free, but the generated trainer sets UNSLOTH_RETURN_LOGITS only after the padding-free decision, so compute_metrics got one packed row per batch instead of one row per example. Read them in the wrapper too. Fixes #3470
Codex Review SummaryThis 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 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 027eff9fac
ℹ️ 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".
| if "packed_seq_lengths" in inputs and "use_cache" not in inputs: | ||
| inputs = {**inputs, "use_cache": False} |
There was a problem hiding this comment.
Avoid passing
use_cache to fixed packing signatures
When a model's forward explicitly accepts packed_seq_lengths but has no **kwargs or use_cache parameter, _forward_signature_accepts_packing treats it as padding-free compatible, yet this branch adds use_cache and compute_loss forwards it to the model, causing evaluate() to fail with an unexpected-keyword TypeError. This supported signature shape is already represented by _NamesPacking in tests/test_padding_free_signature_gate.py; either gate this injection on use_cache support or disable caching without introducing an unsupported forward argument.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
No real forward has that shape: every model that reaches padding-free here takes **kwargs (transformers forwards, Unsloth's CausalLM fast forward), and _NamesPacking is a test stub for the signature gate only. Keeping the change unconditional.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. 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". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! 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". |
Fixes #3470
Two problems with evaluation when Unsloth auto-enables padding-free batching for
SFTTrainer.1.
trainer.evaluate()outsidetrain()reports a wrong loss. Aftertrain()returns (and before it starts),for_inferencerestoresuse_cache, so a packed eval batch builds a KV cache. transformers only applies its packed-sequence mask when there is no cache (masking_utils._preprocess_mask_arguments), so every packed example attends to the ones before it. Evals insidetrain()are fine becauseuse_cacheis off there. Packed eval batches now run withuse_cache=False.2.
compute_metricsgets one packed row per batch.compute_metrics/preprocess_logits_for_metricsare meant to block padding-free (theUNSLOTH_RETURN_LOGITS=1check), but the generatedUnslothSFTTrainer.__init__sets that flag after the padding-free wrapper already decided. Soeval_pred.predictionsheld all examples of a batch concatenated in one row, unlike the per-example rows stockTrainergives. The wrapper now reads both arguments directly.Measured on one B200, LoRA, 20 steps, loss-only eval (
eval_loss):With the issue's Gemma 3 270M setup and a
compute_metrics, 150 steps: main enabled padding-free, so the predictions came as one packed row per batch, andtrainer.evaluate()after training gave eval_loss 3.873 and token accuracy 0.399. With this PR, padding-free stays off, the rows are per example, and eval_loss is 0.822 with token accuracy 0.773. Plain transformers + PEFT on the same data gives eval_loss 0.819, token accuracy 0.770 and CER 0.209 (this PR also gets CER 0.209).Tests:
tests/test_padding_free_eval.py(5 of its 8 cases fail on main, all pass here), plustests/test_eval_step_restores_return_logits.pyandtests/test_padding_free_signature_gate.py.