Skip to content

[BUGFIX] Respect main-process gating during PEFT exports - #18686

Merged
danielhanchen merged 9 commits into
unslothai:mainfrom
DimitriChrysafis:fix-peft-export-main-process
Nov 23, 2026
Merged

danielhanchen merged 9 commits into
unslothai:mainfrom
DimitriChrysafis:fix-peft-export-main-process

Conversation

@DimitriChrysafis

@DimitriChrysafis DimitriChrysafis commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem: Under plain DDP, PEFT merged saves (unsloth_generic_save) and GGUF saves (unsloth_save_pretrained_gguf) ignored is_main_process=False, and merged/GGUF Hub pushes (push_to_hub_gguf, push_to_hub_merged, save_pretrained_merged(push_to_hub=True)) ran merging, conversion, and Hub uploads on every rank.

Fix: Added is_main_process gating (with an fsdp2_gather_active() escape so FSDP2 sub-mesh writers are never vetoed) to unsloth_save_pretrained_gguf, unsloth_push_to_hub_gguf, _push_merged_to_hub_revision, and the PEFT merged branch of unsloth_generic_save. Added _ddp_push_on_rank0 using _plain_data_parallel_push + tp_world_push on unsloth_push_to_hub_gguf, unsloth_generic_push_to_hub_merged, and unsloth_generic_save_pretrained_merged so plain DDP uploads run on rank 0 without intercepting FSDP1/FSDP2 collectives or local saves.

Tests: Added tests/test_peft_export_main_process.py (11 CPU tests with mocked torch.distributed) and ran test_hub_push_rank0_ddp_gloo.py, test_local_save_tokenizer_ddp_gloo.py, test_fsdp2_save_coverage.py, test_save_method_lora_routing.py, test_merged_hub_destination.py, test_public_api_surface.py, test_st_save_merged_signature.py, test_fsdp2_save_args.py, test_export_api_surface.py, and test_ci_repo_cpu_shards.py (245 passed, 1 skipped). Multi-GPU NCCL DDP/FSDP2 runs were not executed locally.

Follow-ups on this branch:

  • One helper, _defer_to_main_process, gates every export, so the GGUF path no longer forwards a stray local to unsloth_generic_save (TypeError inside an FSDP2 gather).
  • The staged save behind a merged Hub push (revision / create_pr) runs as the elected writer, so a sub-mesh writer uploads the model files, not only the README.
  • merged_4bit / merged_4bit_forced still run on every DDP replica, since they merge into the live model; every other method exports from rank 0 alone.
  • On an FSDP1 model the new GGUF and PEFT merge gates keep every rank in the call, since its full state dict all-gathers on every rank.
  • Verified with a real two-rank NCCL run on 2x T4 (base: rank 1 merged, pushed and converted; head: rank 1 does none of it, rank 0 unchanged) and a two-rank gloo run on CPU.

@chatgpt-codex-connector

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

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-23T21:36:49.413027Z
We couldn’t complete this review. Comment @codex review to try again.
3e835d3 Draft marked ready
ℹ️ 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.

… full-model saves honour the FSDP2 gather

[skip ci]
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. Breezy!

Reviewed commit: e84dcfc02c

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

Copy link
Copy Markdown
Member

pre-commit.ci run

chatgpt-codex-connector[bot]

This comment was marked as resolved.

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

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 3e835d3ad9

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

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.

2 participants