Raise when packing is combined with context parallelism - #6843
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d748ed2b6e
ℹ️ 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".
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit cb37381. Configure here.
# Conflicts: # trl/trainer/sft_trainer.py
|
merging without review, feel free to review later if you think I missed something |

docs/source/distributing_training.mdcurrently recommends packing as a best practice for context parallelism:Sequence boundaries are not preserved under context parallelism. Packed documents are kept apart by a block-diagonal attention mask, and context parallelism can only express full causal attention, accelerate's CP hook replaces every layer's mask with
is_causal=True. So each packed document attends to every document before it in the same sequence, silently.Repro
Weight-independent probe: perturb one token in the first packed document and count how many output positions change. Only the first document should be affected. The model uses plain full attention, so packing is the only thing under test.
repro_cp_packing.pyThe fix
Raise in
SFTTrainer.__init__whenargs.packingandparallelism_config.cp_enabled, and correct the doc section. This matches how the other trainers handle context parallelism:GRPOConfig,RLOOConfigandDistillationConfigalready raise forcp_size > 1.Note that padding-free / flash-attention packing is unaffected — this only concerns the context-parallel path, which requires SDPA.
Context
The root cause is in accelerate (its CP hook drops the mask without checking it); that side is addressed in huggingface/accelerate#4177, which fixes the model-level (sliding-window) case. Packing is data-level, so it can only be caught here.
Note
Medium Risk
Changes only misconfigured CP+packing setups from silent wrong training to a hard error; valid configs are unchanged, but anyone relying on the old doc guidance must update their SFT config.
Overview
Prevents silent cross-document attention when Ring Attention / context parallelism (
cp_size > 1) is used with SFT packing.SFTTrainernow raises a clearValueErrorat init if Accelerate reportscp_enabledand eitherpackingoreval_packingis on. The check usesself.accelerator.parallelism_config(not training args) so it still applies when CP is set only in an Accelerate YAML, and is gated on Accelerate ≥ 1.10.1.The distributing training docs are updated to match: the Ring Attention example drops
packing=True, and best practices now say not to use packing with CP because CP forces full causal attention and cannot preserve packed sequences’ block-diagonal masks.Reviewed by Cursor Bugbot for commit 8cc16d9. Bugbot is set up for automated code reviews on this repo. Configure here.