Skip to content

Tensor parallel DPO with PEFT: no DTensor copy into TRL's ref adapter - #17886

Merged
danielhanchen merged 9 commits into
mainfrom
fix-tp-dpo-peft-ref-adapter
Nov 16, 2026
Merged

danielhanchen merged 9 commits into
mainfrom
fix-tp-dpo-peft-ref-adapter

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Problem

DPO with a PEFT model under tensor parallel fails in DPOTrainer.__init__ with aten.copy_.default got mixed torch.Tensor and DTensor. TRL (1.15, dpo_trainer.py) adds a "ref" adapter when ref_model is 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)::

  • Off tensor parallel: returns False, nothing changes.
  • Under TP with a fresh adapter (every lora_B zero, agreed across ranks): skips the copy, and TRL computes reference log-probs with adapters disabled, which equals the fresh adapter (TRL's own fallback).
  • Under TP with an already-trained adapter: raises a clear ValueError before 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 generated UnslothDPOTrainer): 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:

arm result losses
main, TP=2 aten.copy_.default got mixed torch.Tensor and DTensor in DPOTrainer.__init__ none
this PR, TP=2 trains on both GPUs, adapters ['default'] 0.6931, 0.5819, 0.4338, 0.3147, 0.2046, 0.0969
this PR, 1 GPU TRL's own "ref" adapter path 0.6931, 0.5793, 0.4327, 0.3127, 0.2037, 0.0960

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.

…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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T04:03:27.001084Z dff9a44 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 Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-16T01:49:09.040139Z
We couldn’t complete this review. Comment @codex review to try again.
cf30ccf PR opened
ℹ️ 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[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 23802e0523

ℹ️ 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".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 16, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 16, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 16, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 16, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Nov 16, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: dff9a449d0

ℹ️ 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
danielhanchen merged commit b835ccc into main Nov 16, 2026
@danielhanchen
danielhanchen deleted the fix-tp-dpo-peft-ref-adapter branch November 16, 2026 06:05
danielhanchen added a commit that referenced this pull request Nov 16, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant