Repository navigation
Tensor parallel DPO with PEFT: no DTensor copy into TRL's ref adapter - #17886
Conversation
…h adapter TRL >= 1.0 DPOTrainer(ref_model = None) on a PeftModel adds a "ref" adapter and copies every .default. parameter into it. Under Unsloth tensor parallel the default LoRA factors are DTensor shards and the new ref ones plain tensors, so init died with "aten.copy_.default got mixed torch.Tensor and DTensor". Guard that block in the generated DPOTrainer source: under tensor parallel, a fresh adapter (every default lora_B zero, agreed across ranks) skips the copy and TRL falls back to its disabled-adapter reference, which gives identical reference logits. An already trained adapter is refused with a ValueError before training. Off tensor parallel nothing changes.
Codex Review Summary: ✅ CompletedThis 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 Summary: ❌ FailedThis 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. |
… adapter [skip ci]
[skip ci]
… tied saved-module originals [skip ci]
…magnitudes, trainable tokens) [skip ci]
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
…["default"] spelling) [skip ci]
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
…he gloo shard (2100 s cap) (#17969) ## Summary `Repo tests (CPU, gloo)` is timing out again: the shard's pytest runs under `timeout --signal=INT 2100` and recent runs were cut off there with nothing failing: | run | result | |---|---| | #17934 (upstream) | 257 passed, 3 skipped in 2089.55 s, exit 124 | | staging run of #17904 | 258 passed, 3 skipped in 2095.11 s, exit 124 | | staging run of #17957 | 251 passed, 1 failed in 2096.44 s, exit 124 | For comparison the same shard ran 241 tests in 1292 s when #17892 merged this morning. Since then #17886 (`test_tp_dpo_peft_ref_adapter_gloo.py`), #17930 (`test_cp_ulysses_media_towers_gloo.py`), `test_cp_ulysses_dynamic_len_gloo.py` and the DeepSeek-V4.1 additions to `test_parallel_deepseek_v41_gloo.py` landed in it, and a slower runner now goes over the cap. Every one of those tests passes; the shard just no longer fits. ## Change - `.github/workflows/studio-backend-ci.yml`: `tests/test_tp_[!n]*gloo*.py` (the non-native tensor parallel files: `tp_env`, `tp_eval`, `tp_full_finetune`, `tp_replica`, `tp_kv_replicated`, `tp_moe_dropout`, `tp_dpo_peft_ref_adapter`, ...; 12 files) moves out of `gloo` into a new `gloo-tp-env` shard. `gloo` keeps its other three globs (19 files). Comment added with the numbers above. - `tests/studio/test_backend_ci_parallel_isolation.py`: the command scan's expected counts go from 15 parallel runs / 10 repo-root shards to 16 / 11, as its message asks when a shard is added. ## Why that split Measured locally on main with the shard's exact command (`-n 4 --dist loadgroup`): 275 passed in 1302.83 s. Of the 40 slowest tests (2815 worker-seconds), 2716 s are in `tests/test_tp_[!n]*gloo*.py`; out of about 5200 worker-seconds in total, so these 12 files are well over half of the shard. Each half now has room under the cap on a slower runner. ## Testing - `tests/test_ci_repo_cpu_shards.py` (every repo test file collected by exactly one shard, every glob matches a file): 15 passed. - All 15 test files that read `studio-backend-ci.yml`: 341 passed, 3 skipped. - The new shard's first run on this PR is the timing check.
Problem
DPO with a PEFT model under tensor parallel fails in
DPOTrainer.__init__withaten.copy_.default got mixed torch.Tensor and DTensor. TRL (1.15,dpo_trainer.py) adds a"ref"adapter whenref_modelis None and copies the policy adapter into it; Unsloth shards the LoRA under TP, so the new adapter's plain tensors meet DTensors. The rl.py rewrite that drops this block only matches the GRPO / RLOO form.Fix
For the DPO trainer only, the ref-adapter block runs under
if not _unsloth_tp_skip_dpo_ref_adapter(model)::lora_Bzero, agreed across ranks): skips the copy, and TRL computes reference log-probs with adapters disabled, which equals the fresh adapter (TRL's own fallback).ValueErrorbefore training instead of silently changing the reference model.Tests
tests/test_tp_dpo_peft_ref_adapter_gloo.py(2 CPU gloo ranks, tiny Llama, the generatedUnslothDPOTrainer): main fails both cases with the DTensor copy error; here both pass, and one training step gives loss 0.6931 (log 2, policy equals reference at the start). Existing DPO tests: 26 passed, 89 skipped.Not covered: a 2xT4 NCCL run of a real DPO notebook, and TRL versions other than 1.15 end to end (the rewrite was checked against 1.8, 1.14.2, 1.18 and main source).
Kaggle 2x T4
unsloth/Qwen3-0.6B, 16-bit LoRA r16, fp16, beta 0.1, 6 steps,
ref_model = None, TRL 1.15:aten.copy_.default got mixed torch.Tensor and DTensorinDPOTrainer.__init__['default']Step 1 is log 2 on both (fresh adapter: policy == reference); TP stays within 0.0026 of 1 GPU. AMD gfx1151 Linux + Windows: 1-GPU DPO losses unchanged vs main.