Skip to content

DDP: upload to the Hub from rank 0 only - #17868

Merged
danielhanchen merged 3 commits into
mainfrom
ddp-push-rank0
Nov 15, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
ddp-push-rank0

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Under plain DDP, model.push_to_hub and save_pretrained(push_to_hub = True) uploaded from every rank, creating duplicate Hub commits. zoo's save_lora(push_to_hub = True) goes through the same path.

These calls now go through the existing tp_world_push: rank 0 uploads, and the other ranks do not wait on a rendezvous. TP tokenizers already use this path.

  • Unchanged: local saves still run on every rank. FSDP2-sharded models are unchanged, since their collective save already writes on rank 0 only.
  • Flip test tests/test_ddp_push_rank0_gloo.py (2-rank gloo CPU): on main, rank 1 also runs both uploads; with this PR only rank 0 does.
  • Related tests: test_tp_world_save_dirs_gloo.py and saving/test_peft_base_export_guard.py pass.
  • Not covered here: push_to_hub_merged and push_to_hub_gguf under DDP still upload from every rank, because they sit behind different decorators.

Under plain DDP every rank ran model.push_to_hub, and save_pretrained / zoo save_lora with
push_to_hub = True, so each rank made its own commits to the repo. Every rank holds the same
weights, so rank 0 alone uploads (no rendezvous, as tokenizer pushes under tensor parallel);
local saves still run on every rank and FSDP2's collective save is unchanged.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 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-15T20:06:06.949913Z 2586f35 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4be016e9de

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

Comment thread unsloth/save.py
Comment on lines +7329 to +7331
if fsdp2_units(model):
return False
return method == "push_to_hub" or bool(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Exclude FSDP1 models from the rank-zero-only upload path

When a model contains FSDP1 FullyShardedDataParallel children, this predicate still identifies it as plain DDP because fsdp2_units() only recognizes FSDP2. Calling save_pretrained(push_to_hub=True) without a supplied state dict then skips saving on nonzero ranks, while rank 0 enters the FSDP1 state-dict hooks and waits for their all-gather collectives. An all-rank save that previously completed can therefore hang or time out. Exclude FSDP1-managed models as well before applying the rank-zero-only shortcut.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the latest commit: a model containing an FSDP1 FullyShardedDataParallel module now keeps the all-rank save, since its state dict hooks all-gather. New test test_fsdp1_models_keep_the_all_rank_save checks that plain DDP still takes the rank-0 path and FSDP1 does not.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 2586f35e57

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

[skip ci]
@danielhanchen
danielhanchen merged commit ed4a63c into main Nov 15, 2026
@danielhanchen
danielhanchen deleted the ddp-push-rank0 branch November 15, 2026 23:11
danielhanchen added a commit that referenced this pull request Nov 16, 2026
…oding in test_bind_local_cuda_device (#17892)

## Summary

Two repo-wide reds on `main`, both from test files added in the last few
merges:

1.
`tests/test_ci_repo_cpu_shards.py::TestEveryTestFileLandsInExactlyOneShard::test_no_file_is_dropped_or_run_twice`
(`Repo tests (CPU, rest)`):
- #17862 added `tests/test_tokenizer_save_ddp_gloo.py`. No shard
collects it: `gloo` takes `test_[!fpt]*gloo*`, and `rest` ignores
`test_[!f]*gloo*`. It never runs in CI.
- #17868 added `tests/test_ddp_push_rank0_gloo.py`. Both `gloo`
(`test_[!fpt]*gloo*`) and `rest-2` (`test_[dimsu]*.py`) collect it, so
it runs twice, and in `rest-2` without the gloo shard's setup.
2.
`tests/test_source_read_encoding.py::test_checked_in_file_reads_name_an_encoding`
(`Repo tests (CPU, rest-2)`): #17867's
`tests/test_bind_local_cuda_device.py:47` reads `unsloth/__init__.py`
with `read_text()` and no encoding.

Both new files are correct tests. They only needed a shard home and an
explicit encoding.

## Changes

- `.github/workflows/studio-backend-ci.yml`:
- `gloo` gains `tests/test_t[!p]*gloo*.py`, which covers
`test_tokenizer_*`. `test_tp_*` keeps its existing split between `gloo`
and `gloo-tp`.
- `rest-2` gains `--ignore-glob=tests/test_[dimsu]*gloo*.py`, so a gloo
file starting with d/i/m/s/u runs only in `gloo`.
- The comment that listed `test_t[!p]*` as an unclaimed name is updated.
- `tests/test_bind_local_cuda_device.py`: `read_text(encoding =
"utf-8")`.

## Verification

`tests/test_ci_repo_cpu_shards.py tests/test_source_read_encoding.py
tests/test_bind_local_cuda_device.py`: 38 passed. Removing either
workflow line brings back the matching failure:

```
these files are collected by more than one shard: [('tests/test_ddp_push_rank0_gloo.py', ['gloo', 'rest-2'])]
these files are collected by no shard, so Repo tests (CPU) no longer runs them: ['tests/test_tokenizer_save_ddp_gloo.py']
```
Penguinjanator pushed a commit to Penguinjanator/unsloth that referenced this pull request Nov 16, 2026
…on without Triton, kernel and test gates (unslothai#17882)

Backend CI on main has been red since today's merges. Every failure
traces to one commit; this fixes the unsloth side (the gloo-cp shard
also needs unslothai/unsloth-zoo#2542, since CI installs zoo from main).

| CI shard / test | Cause | Fix |
|---|---|---|
| gloo: `test_qwen4exp_parallel_matches_one_process[pp*]`,
`test_pp_splits_streams_and_runs_the_mixer_last`, all of
`test_frozen_table_pp_gloo.py` | unslothai#17785: transformers 5.17 (the CI pin)
names the Qwen4Exp layer type `qwen_sparse_attention`, 5.22 renamed it
`indexed_attention`; pipeline parallel only accepted the new name, so
every rank raised "hybrid layer stacks" | accept both names in
`_PP_HYBRID_FAMILIES` |
| diffusion (Python 3.13): 13 tests in
`test_diffusion_int8_attention_triton_device.py` | unslothai#17855: `_launch`
read `triton.runtime` while `triton` is `None` on that shard | check
`triton is not None` first |
| rest: `test_every_constexpr_line_has_a_comment` | unslothai#17860: 23
uncommented `tl.constexpr` parameters in `large_head_attention.py` |
comments added; `_tma_tile`'s signature split one parameter per line |
| rest: `test_every_launch_requests_fp_fusion` | unslothai#17860: the `_launch`
helper forwards `enable_fp_fusion` from every caller, but the gate did
not know it is a helper | registered like `layernorm.py`'s |
| rest: `test_no_file_is_dropped_or_run_twice` | unslothai#17868:
`tests/test_ddp_push_rank0_gloo.py` matched both the gloo shard and
rest-2's `test_[dimsu]*.py` | renamed
`tests/test_hub_push_rank0_ddp_gloo.py` |
| rest-2: `test_checked_in_file_reads_name_an_encoding` | unslothai#17867:
`read_text()` without an encoding in
`tests/test_bind_local_cuda_device.py` | `encoding = "utf-8"` |
| rest-2:
`test_sentence_transformer_trainer_checks_the_placement_before_the_deferred_compile[*]`
| unslothai#17859: `patch_train_dataloader` assumed every trainer class has
`get_train_dataloader` | skip the patch when the method is missing |
| rest-2:
`test_router_aux_loss_gradient_survives_unsloth_checkpointing[*]` |
unslothai#17873: the gpu-marked test still runs under CI's `-m 'not server and
not e2e'` | `has_real_accelerator()` skip next to the gpu mark |
| studio: `test_no_skip_guard_reads_a_spoofable_accelerator_probe` |
unslothai#17850: `test_block_cudagraphs_side_grad.py` skipped on
`torch.cuda.is_available()`, which the CUDA spoof fakes | skip on
`has_real_cuda()` |

## Tests

In a venv matching CI (Python 3.12, torch 2.10.0+cpu, transformers
5.17.0, TRL 1.15, PEFT 0.21.3, `UNSLOTH_COMPILE_DISABLE=1`, `-m 'not
server and not e2e'`):

- gloo: `test_qwen4exp_parallel_gloo.py` failed on main, 9 / 9 pass
here; `test_frozen_table_pp_gloo.py` 13 / 13.
- rest / rest-2 / studio: the nine affected files had exactly CI's 9
failures on main; 153 passed / 6 skipped here.
- diffusion: with Triton blocked (as on the Python 3.13 shard), the 13
failures pass here; with Triton present everything passes.

The only non-test code changes are the PP layer-type name, the Triton
`None` check and the Sentence Transformers method check;
`large_head_attention.py` changes are comments only.
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