Skip to content

Refuse accelerate config cp_size under Unsloth SFT, not only TrainingArguments - #15212

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s6-longctx-cp
Oct 25, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s6-longctx-cp

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

TRL's long context recipe turns on context parallelism through the accelerate config:

parallelism_config:
  parallelism_config_cp_size: 4

accelerate launch exports ACCELERATE_USE_PARALLELISM_CONFIG=true and PARALLELISM_CONFIG_CP_SIZE, and the Accelerator builds its ParallelismConfig from them. TrainingArguments.parallelism_config stays None, so the existing check in the SFTTrainer patch (which only reads args.parallelism_config) let it through. transformers then wraps every step in accelerator.maybe_context_parallel and shards the inputs over Unsloth's model, which Unsloth does not support.

This change also checks trainer.accelerator.parallelism_config.cp_size and raises the same error, pointing at SFTConfig(context_parallel_size = N).

Behaviour

Setup Before After
TrainingArguments(parallelism_config = ParallelismConfig(cp_size = 2)) refused refused (unchanged)
accelerate config parallelism_config_cp_size: 2 (env) not refused, accelerate's ring runs inside the step refused at SFTTrainer.__init__
UNSLOTH_FSDP2=1 (native) with either of the above refused by native FSDP2 unchanged
SFTConfig(context_parallel_size = 2) Unsloth CP unchanged

Tests

  • New test_accelerate_config_cp_size_is_refused[1, 2] in tests/test_context_parallel.py: fails on main for cp_size 2, passes here; cp_size 1 still builds.
  • CPU gloo, 2 ranks, tiny Qwen3 LoRA through the real SFTTrainer with the accelerate env set: main reaches the training step and fails inside torch's CP dispatcher (aten._scaled_dot_product_flash_attention_for_cpu has no sharding rule), which shows the ring was engaged; with this change it stops at __init__ with the message above.
  • tests/test_context_parallel*.py, tests/test_ulysses*.py: 142 passed on main, 144 passed here (the 2 new cases).

…Arguments

accelerate launch with parallelism_config_cp_size builds the Accelerator's
ParallelismConfig from the environment, so TrainingArguments.parallelism_config
stays None and the existing cp_size check passed. accelerate's ring then ran
over Unsloth's model. Check the accelerator's config as well and point to
SFTConfig(context_parallel_size = N).
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 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 ✅ Completed 2026-10-25T07:38:33.957517Z 78ab9b4 Manual request
ℹ️ 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 78ab9b42d5

ℹ️ 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 added a commit that referenced this pull request Oct 25, 2026
…5239)

## Summary

`Source lint` is red on main, and so on every PR (for example #15212 and
#15217). The step that fails is `Duplicate definitions (whole tree)`:

```
tests/test_fp8_torchao_import_route.py:13: has_real_cuda is imported twice from the same module (first at line 11)
```

#15211 and c993bf3 (skip the QAT MX and FP8 block GEMM GPU cases on the
recorded real CUDA) landed the same `from real_accelerator import
has_real_cuda` fix 18 minutes apart. Their import lines sit at different
spots, so git merged both without a conflict and left the name bound
twice. `scripts/lint_duplicate_definitions.py` exists to catch exactly
this merge damage.

## Change

- Drop the second import. Nothing else in the file changes.

## Verification

- `python scripts/lint_duplicate_definitions.py
tests/test_fp8_torchao_import_route.py`: on main `duplicate definitions
(1)` with exit 1. With this change: `clean: 1 file(s) checked` and exit
0.
- `tests/test_fp8_torchao_import_route.py` and
`tests/python/test_accelerator_skip_guards.py`: `52 passed, 1 skipped`.
danielhanchen added a commit that referenced this pull request Oct 25, 2026
…ranks (#15250)

## Summary


`tests/test_float8_training_fsdp2_gloo.py::test_float8_linear_trains_under_native_fsdp2`
fails in the `Repo tests (CPU, rest)` shard on unrelated PRs (#15212,
#15217, #15218) and passes when run on its own:

```
Mismatched elements: 1 / 4:  Max relative difference: 6.84e-05
Index | Obtained          | Expected
3     | 5.603824138641357 | 5.603440761566162 ± 5.6e-05
```

The sharded run is the correct one. Training the same unsharded fp8
model in a clean process gives `5.603824139` at step 4, and the result
is identical with 1, 2, 4 and 8 threads. The value that moved is the
reference. The test trained it inside the pytest worker, so it inherited
whatever earlier tests in the shard had left in that process, while the
two gloo ranks start fresh under `spawn`. The tolerance is fine. The two
sides were simply not run under the same conditions.

## Change

- The unsharded reference trains in its own spawned process
(`_reference_worker`, `nprocs = 1`), so both sides of the comparison
start from a clean interpreter. The tolerance is unchanged (`rel = 1e-5,
abs = 1e-6`).
- The JSON reads and both writes name `encoding = "utf-8"`.

## Verification

- `tests/test_float8_training_fsdp2_gloo.py`: passes, and the clean
reference matches the sharded losses exactly.
- Sabotage 1 (the CI failure): a pytest plugin that changes only the
parent process (scales `F.cross_entropy` by `1 + 1e-4`, which `spawn`
children do not inherit). The old test fails with `Max relative
difference: 2.0e-04`. The new test passes.
- Sabotage 2 (the guard still bites): scaling the sharded ranks' losses
by `1 + 1e-4` makes the new test fail.

I did not pin down which earlier test leaves the state behind. With this
change, the comparison no longer depends on it.
@danielhanchen
danielhanchen merged commit af99ed7 into main Oct 25, 2026
32 of 37 checks passed
@danielhanchen
danielhanchen deleted the dh/s6-longctx-cp branch October 25, 2026 12:08
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.

1 participant