Emit position_ids from the SFT collator when sequence parallelism is enabled - #6812
Emit position_ids from the SFT collator when sequence parallelism is enabled#6812qgallouedec wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d13784a7ac
ℹ️ 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".
|
|
||
| # Sequence parallelism (Ulysses/ALST) shards batches along the sequence dimension and requires | ||
| # `position_ids` in every batch to preserve each token's global position. | ||
| parallelism_config = self.accelerator.parallelism_config |
There was a problem hiding this comment.
Guard access to the new Accelerate property
When TRL is installed with a supported Accelerate version below 1.10.1, constructing any SFTTrainer now raises AttributeError here because those releases predate Accelerator.parallelism_config; the project still declares accelerate>=1.4.0, and _compat.py explicitly handles the absence of parallelism support in these versions. Gate this access with an explicit Accelerate version check (or raise the minimum dependency) so ordinary non-parallel SFT remains usable.
AGENTS.md reference: AGENTS.md:L47-L48
Useful? React with 👍 / 👎.
|
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.
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 d13784a. Configure here.
| and parallelism_config.sp_enabled | ||
| and isinstance(self.data_collator, DataCollatorForLanguageModeling) | ||
| ): | ||
| self.data_collator.return_position_ids = True |
There was a problem hiding this comment.
SP check crashes on older Accelerate
High Severity
SFTTrainer now reads self.accelerator.parallelism_config and sp_enabled on every init. Those attributes do not exist on the supported Accelerate range: parallelism_config arrived in 1.10 and sp_enabled in 1.12, while TRL still requires and CI-tests accelerate>=1.4.0. That makes ordinary (non-SP) SFTTrainer construction raise AttributeError.
Reviewed by Cursor Bugbot for commit d13784a. Configure here.


Running SFT with the documented ALST/Ulysses setup (
distributed_type: DEEPSPEED+parallelism_config_sp_backend: deepspeed, e.g.examples/accelerate_configs/alst_ulysses_4gpu.yaml) fails on the first batch:DataCollatorForLanguageModelingonly emitsposition_idsin padding-free mode; the standard padded path never includes them, so the documented Ulysses configuration doesn't work out ofthe box.
Fix
DataCollatorForLanguageModelinggainsreturn_position_ids(defaultFalse): in padded mode it then returnsposition_idsalongsideattention_mask; per-samplearange, or boundary-resetting ids for packed examples (reusing the existingget_position_ids_from_packed_seq_lengths).SFTTrainerflips it on automatically whenaccelerator.parallelism_config.sp_enabled.No behavior change for any existing configuration (flag defaults off; SP configs previously crashed).
Repro
ulysses2.yaml(2 GPUs):repro.py:Before (main): the
ValueErrorabove at the first batch. After (this branch):training OK.Tested
End-to-end on 8×H100 (
sp_size=8, ZeRO-3, Qwen3-0.6B, 1,048,576-token sequences): before → theValueErrorabove; after → trains at 234 s/step, 24.3 GB/GPU (loss/metrics consistent with the ring-attention CP run of the same setup).Note
Low Risk
Default-off flag and narrow auto-enable only when SP is active; fixes a crash path without changing standard SFT behavior.
Overview
Fixes Ulysses/ALST SFT setups that failed on the first batch because padded batches never included
position_ids(only padding-free mode did).DataCollatorForLanguageModelingadds optionalreturn_position_ids(defaultFalse). In standard padded mode it can now returnposition_idswithattention_mask, using per-sequencearangeor packedseq_lengths(same helper as padding-free).SFTTrainersetsreturn_position_ids=Truewhenaccelerator.parallelism_config.sp_enabled.Tests cover padded and packed
return_position_idsbatches. Default configs are unchanged.Reviewed by Cursor Bugbot for commit d13784a. Bugbot is set up for automated code reviews on this repo. Configure here.