Skip to content

FSDP2: keep the use_cpu reclaim reachable when only import unsloth wrapped the Trainer - #15427

Merged
danielhanchen merged 2 commits into
mainfrom
dh/s7-rc-fsdp2-reclaim
Oct 27, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
dh/s7-rc-fsdp2-reclaim

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

On a non-CUDA install (Intel XPU, Ascend NPU, MLU, ...), UNSLOTH_FSDP2=1 is routed to accelerate's FSDP2 at import time, and unsloth_fsdp2_native_requested() is False. #14760 added reclaim_native_fsdp2_for_cpu_trainer so that a TrainingArguments(use_cpu=True) Trainer still shards natively (accelerate runs it as MULTI_CPU, which never builds its FSDP plugin). That reclaim lives inside the Trainer.__init__ wrapper, but the import-time installer install_native_fsdp2_trainer_init (#15012) returns early whenever native FSDP2 is not requested, so it never installs the wrapper on this route.

FastLanguageModel / FastModel loads install the wrapper themselves, so they are unaffected. A plain Trainer after import unsloth, or a FastSentenceTransformer encoder that loads without FastModel, trains with the model unsharded and no message, even though the launch step printed that FSDP2 shards it.

Fix

install_native_fsdp2_trainer_init also installs the wrapper while the non-CUDA export is pending (_NON_CUDA_EXPORT is not None). It still returns False there, and a non-CPU Trainer on that install keeps accelerate's FSDP2 exactly as before (the reclaim returns False and changes nothing).

Before / after

2-rank gloo CPU world, XPU build markers (torch.version.xpu), only the two import-time steps of unsloth/__init__.py run, then a plain Trainer on a tiny Llama LoRA with use_cpu=True.

Check main (0ea0473) this branch
Trainer wrapper installed no yes
Model sharded by native FSDP2 no yes
ACCELERATE_USE_FSDP left exported true unset
Accelerator distributed type MULTI_CPU MULTI_CPU
test_xpu_install_cpu_trainer_shards_natively_without_a_loader FAIL PASS

Head also passes tests/test_fsdp2_native_trainer_args_gloo.py, tests/test_fsdp2_native_trainer_init_any_loader_gloo.py and tests/test_fsdp2_non_cuda_accelerate_route.py (29 passed) on torch 2.13 / transformers 5.17, and the two XPU reclaim tests on torch 2.6 CPU / transformers 4.57.6, torch 2.10 and torch 2.14.

Not covered

…nding

On a non-CUDA install UNSLOTH_FSDP2=1 is routed to accelerate's FSDP2 at
import, so install_native_fsdp2_trainer_init returned early and only a
FastLanguageModel / FastModel load wrapped Trainer.__init__. A plain Trainer
or a FastSentenceTransformer encoder with TrainingArguments(use_cpu=True)
then never reached reclaim_native_fsdp2_for_cpu_trainer: accelerate ran it as
MULTI_CPU, which drops its FSDP plugin, and the model trained unsharded.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T19:23:44.009005Z 8a90a08 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. Keep them coming!

Reviewed commit: 8a90a08b8e

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