Skip to content

Tensor parallel x FSDP2: average trainable TP parameters left out of FSDP2 over the data parallel ranks - #15748

Merged
danielhanchen merged 3 commits into
mainfrom
dh/tp-ignored-params-dp-avg
Oct 28, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/tp-ignored-params-dp-avg

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Under tensor parallel x native FSDP2, a trainable tensor parallel (DTensor) parameter passed in FSDP2Config(ignored_params=...) is left out of FSDP2, so FSDP2 never reduces its gradient. reduce_unsharded_trainable_grads is the path that averages such unmanaged parameters over the data parallel ranks, but it skipped every DTensor parameter (assuming FSDP2 owned it). Each data parallel replica then stepped with its own gradient and the replicas drifted apart from the first step.

Change

unsloth/models/fsdp2_save.py:

  • reduce_unsharded_trainable_grads now tells FSDP2's own parameters apart from other DTensors by reading the FSDP2 param groups (sharded and unsharded parameters). A trainable DTensor that FSDP2 does not manage, and whose mesh meets the data parallel mesh only at this rank (a tensor parallel slice replicated over dp), is registered for averaging. Anything else keeps today's behaviour (skipped).
  • _sync_replicated_grads averages the local shard of such a DTensor gradient over the dp mesh only, in the same flat float32 all-reduce, the same once-per-step autograd callback and the same grad accumulation deferral as the plain replicated parameters, so it runs before clipping. A missing gradient is recreated as a DTensor with the parameter's mesh and placements.
  • Plain (non DTensor) parameters take exactly the same path as before.

Tests

New tests/test_fsdp2_tp_ignored_params_dp_gloo.py (gloo CPU, 4 ranks, dp 2 x tp 2, tiny Llama LoRA, the layer 0 q_proj / down_proj adapters passed as ignored_params, 3 SGD steps clipped to 0.05 by native_clip_grad_norm_):

main this branch
dp replicas of the ignored tp parameters identical every step fails at step 0 (dp replicas differ at step 0: ...q_proj.lora_A.default.weight) pass (bitwise)
grad norms and final weights vs one process on the full batch not reached pass (rel 1e-5 / 1e-4)

Pure data parallel (2 ranks, no tensor parallel, LoRA plus a trainable final norm in ignored_params, 3 clipped AdamW steps): grad norms and every trainable parameter are bitwise identical between main and this branch.

Neighbouring suites (tensor parallel gloo, tensor parallel x FSDP2, native FSDP2 clip, replicated grad sync, save, guards, units) pass.

…anks

reduce_unsharded_trainable_grads skipped every DTensor parameter, so a trainable
tensor parallel parameter passed in FSDP2Config(ignored_params=...) was never
averaged over the dp ranks and the replicas drifted. Average its local shard over
the dp mesh in the same once-per-step callback as plain replicated parameters.
@danielhanchen danielhanchen self-assigned this Oct 28, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 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-28T11:31:21.343709Z de010dc 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. Swish!

Reviewed commit: de010dc80a

ℹ️ 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 f27ae8a into main Oct 28, 2026
@danielhanchen
danielhanchen deleted the dh/tp-ignored-params-dp-avg branch October 28, 2026 14:10
danielhanchen added a commit that referenced this pull request Oct 28, 2026
`Repo tests (CPU, fsdp2)` on #15833's staging run passed every test and
still went red:

```
364 passed, 28 skipped, 80 warnings in 2092.29s (0:34:52)
##[error]Process completed with exit code 124.
```

The shard's pytest step runs under `timeout --signal=INT --kill-after=60
2100`. pytest finished 8 s under that cap, and the cap then fired while
it was exiting.

## Cause

#15291 moved the FSDP2 gloo tests into their own shard at about ten
minutes of runner time. They have grown since, with most of a dozen
FSDP2 PRs merged today adding tests there (#15756, #15748, #15720,
#15709, #15708, #15352, ...). Over the last twenty-odd staging runs this
job took 21 to 38 minutes, so it now sits at its cap on an ordinary
runner. Nothing failed; the shard is simply too big for its timeout.

## Fix

Split the shard in two by file name, balanced on measured time. Per-test
durations on main, summed per file across all 76 files:

| shard | selection | files | tests | measured time |
|---|---|---|---|---|
| `fsdp2` | `tests/test_fsdp2_[!n]*.py tests/test_fsdp2_n[!a]*.py
tests/test_fsdp2_native_[abc]*.py` | 34 | 237 | 1156 s (45%) |
| `fsdp2-native` (new) | `tests/test_fsdp2_native_[!abc]*.py` | 42 | 276
| 1406 s (55%) |
| both together, unchanged total | `tests/test_fsdp2*.py` | 76 | 513 |
2562 s |

`fsdp2` keeps its name, so its check name is unchanged. The step timeout
and job cap are left alone, so each half gets the whole budget.

## Guards

- `tests/test_ci_repo_cpu_shards.py` already fails when a file lands in
no shard or in two. It caught my first draft, which dropped
`test_fsdp2_non_cuda_accelerate_route.py`; hence the `n[!a]` glob.
- A new check, `TestEveryGlobRootMatchesAFile`, fails if a shard glob
matches no test file. bash passes an unmatched glob through literally
and pytest then fails the whole shard on a missing path. Sabotage:
adding `tests/test_fsdp2_zz*.py` to a selection fails it with
`[('fsdp2', 'tests/test_fsdp2_zz*.py')]`.
- `tests/studio/test_backend_ci_parallel_isolation.py` pins the number
of parallel runs, now nine (six repo-root shards).

## Verification

- `tests/test_ci_repo_cpu_shards.py` and
`tests/studio/test_backend_ci_parallel_isolation.py`: 56 passed.
- With `tests/studio/test_cache_budget_discipline.py` added: everything
passes except the guard on my first draft, fixed above.
- `--collect-only` on the two selections gives 237 + 276 = 513, the same
as the old single glob.

The per-file times are from a local `-n 8` run, so the split ratio is
approximate. The first runs of the two new shards will show the actual
runner times.
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