Skip to content

Fix evaluate() under auto padding-free and keep compute_metrics rows per example - #13136

Merged
danielhanchen merged 3 commits into
mainfrom
fix-padding-free-eval-cache
Oct 9, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix-padding-free-eval-cache

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Fixes #3470

Two problems with evaluation when Unsloth auto-enables padding-free batching for SFTTrainer.

1. trainer.evaluate() outside train() reports a wrong loss. After train() returns (and before it starts), for_inference restores use_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 inside train() are fine because use_cache is off there. Packed eval batches now run with use_cache=False.

2. compute_metrics gets one packed row per batch. compute_metrics / preprocess_logits_for_metrics are meant to block padding-free (the UNSLOTH_RETURN_LOGITS=1 check), but the generated UnslothSFTTrainer.__init__ sets that flag after the padding-free wrapper already decided. So eval_pred.predictions held all examples of a batch concatenated in one row, unlike the per-example rows stock Trainer gives. The wrapper now reads both arguments directly.

Measured on one B200, LoRA, 20 steps, loss-only eval (eval_loss):

Model When main, padding-free this PR, padding-free padding_free=False
unsloth/gemma-3-270m-it evaluate() before train 10.424 8.897 8.898
unsloth/gemma-3-270m-it eval inside train 2.327 2.327 2.369
unsloth/gemma-3-270m-it evaluate() after train 3.816 2.327 2.369
unsloth/Qwen3-0.6B evaluate() before train 9.812 9.386 9.384
unsloth/Qwen3-0.6B eval inside train 1.099 1.099 1.098
unsloth/Qwen3-0.6B evaluate() after train 4.352 1.099 1.098

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, and trainer.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), plus tests/test_eval_step_restores_return_logits.py and tests/test_padding_free_signature_gate.py.

…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
@danielhanchen danielhanchen self-assigned this Oct 9, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-09T14:14:36.602138Z 75072bb New commits
ℹ️ 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread unsloth/models/rl.py
Comment on lines +283 to +284
if "packed_seq_lengths" in inputs and "use_cache" not in inputs:
inputs = {**inputs, "use_cache": False}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

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".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: db80934a8f

ℹ️ 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".

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.

[Feature] Compute WER/CER metrics with Gemma3

1 participant