Repository navigation
Keep the padding mask for left-padded label-less training forwards - #13154
Conversation
Unsloth's training forward drops the attention mask because it assumes right padding. Label-less forwards (custom losses such as ms-swift's generative rerankers, which left-pad and read the last token) also zero the pad embeddings, so every real token attended to them and gradients went NaN on the first step. Keep the mask for those batches only; right padded and labelled forwards take the same path as before. Fixes #3705
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 |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 48c669d334
ℹ️ 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".
| mask = attention_mask.bool() | ||
| return bool((mask[:, 1:] & ~mask[:, :-1]).any()) |
There was a problem hiding this comment.
Avoid a data-dependent Python branch in compiled forwards
When a label-less training forward is run under torch.compile(..., fullgraph=True) or CUDA graph capture, converting the device result of .any() to a Python bool requires a data-dependent scalar read: Dynamo cannot retain this in a full graph, and CUDA capture cannot perform the host synchronization. This path runs for every non-None mask, including all-ones and right-padded masks, so compiled custom-loss and reranker training can now fail before attention; guard tracing/capture and conservatively retain the mask there, or otherwise avoid the Python scalar conversion.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not a regression: torch.compile(model, fullgraph=True) on this training forward already fails on main with the same Unsupported error (tiny Qwen3, LoRA, label-less, measured on both trees), and the default torch.compile (fullgraph=False) runs on both with finite logits. The fast forward is not CUDA graph captured in training.
|
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". |
A left-padded row always starts with a pad, so one column answers the question with a single small kernel instead of a full-mask scan; the host sync stays, but only on label-less training forwards.
|
Codex Review: Didn't find any major issues. Chef's kiss. 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". |
|
pre-commit.ci run |
1 similar comment
|
pre-commit.ci run |
|
Codex Review: Didn't find any major issues. 🚀 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". |
torch.cuda.is_available() is patched to True by the CUDA spoof the version_compat suites install, so the GPU test un-skipped on CPU-only runners sharing their pytest session.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 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 #3705
Problem
LlamaModel_fast_forwarddrops the 2D attention mask in training mode because it assumes right padding (causal attention never reaches trailing pads). When the forward has no labels it also zeroes the pad embeddings (the DPO block). A left-padded, label-less batch therefore has every real token attending to zeroed pad positions, and the gradients go NaN.That is exactly the ms-swift generative reranker flow from the issue: ms-swift pads reranker batches on the left by default (
--padding_side left, "only support --padding_side left" in its listwise example at the version the reporter used), pops the per-sequence labels before calling the model, and reads the last token's yes/no logits. Plain transformers + PEFT keeps the mask and trains fine.Fix
Keep the mask in training when the forward has no labels and some row starts with a pad (left padding). Everything else is unchanged:
left_pack_padding) and DPO flushes left, so their masks never trigger it. PPO keeps its existing opt-in flag.Cost
The check is a host sync, so it runs only for label-less training forwards (
_has_no_labelsshort-circuits it for SFT) and reads only the first mask column. B200, exclusive GPU, per call:torch.cuda.synchronize()Behind queued GPU work all three read ~360 us, which is the queued work finishing, not added GPU time.
End to end, DPO on unsloth/Qwen3-0.6B (every forward label-less and right padded, so the check runs and the mask is still dropped), 40 steps, 8 runs ABBA on one exclusive B200, fresh process and compile cache each: median step time base 276 ms (runs 210 to 326 ms), head 236 ms (223 to 316 ms), difference 95% CI [-90, +61] ms, a tie inside the host noise. Losses are bit-identical across all 8 runs.
Results
Qwen3-Reranker, LoRA r=64 alpha=128, bf16,
use_gradient_checkpointing="unsloth", GA 16, lr 2e-4, 8 sequences per group (1 positive + 7 negatives frommteb/scidocs-reranking), ms-swift's listwise generative reranker loss andRerankerTrainer.compute_lossreproduced verbatim, B200.The constant ~16x between the Unsloth and transformers columns is gradient accumulation, not this bug: the batch counter returns
num_items_in_batch = Nonefor per-sequence labels, and ms-swift'scompute_lossonly divides by GA when it is not None. Withmax_grad_norm=1.0and AdamW the updates match; the per-step loss ratio is 14x to 18x across the run.Tiny Qwen3, bf16, training mode: the left-padded row's logits vs the same row alone differ by up to 5.5e-2 on main (logits are ~4e-2) and 3.7e-4 with this PR.
Tests
tests/test_llama_left_padding_training_mask.py: the mask predicate over long, int, bool and float masks, plus a GPU test that the left-padded label-less training forward matches the unpadded row and gives finite LoRA gradients. The GPU test fails on main (Greatest absolute difference: 0.0549 (up to 0.005 allowed)) and passes here.tests/test_ppo_trainer_patches.py: the source check for the training-mode mask branch now matches the new condition.