Repository navigation
FSDP2: keep the use_cpu reclaim reachable when only import unsloth wrapped the Trainer - #15427
Merged
Merged
Conversation
…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.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On a non-CUDA install (Intel XPU, Ascend NPU, MLU, ...),
UNSLOTH_FSDP2=1is routed to accelerate's FSDP2 at import time, andunsloth_fsdp2_native_requested()is False. #14760 addedreclaim_native_fsdp2_for_cpu_trainerso that aTrainingArguments(use_cpu=True)Trainer still shards natively (accelerate runs it as MULTI_CPU, which never builds its FSDP plugin). That reclaim lives inside theTrainer.__init__wrapper, but the import-time installerinstall_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
Trainerafterimport 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_initalso 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 ofunsloth/__init__.pyrun, then a plain Trainer on a tiny Llama LoRA withuse_cpu=True.ACCELERATE_USE_FSDPleft exportedtest_xpu_install_cpu_trainer_shards_natively_without_a_loaderHead also passes
tests/test_fsdp2_native_trainer_args_gloo.py,tests/test_fsdp2_native_trainer_init_any_loader_gloo.pyandtests/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
torch.versionbuild markers, as the existing Native FSDP2: keep accelerate's FSDP2 for UNSLOTH_FSDP2=1 on XPU / NPU / other non-CUDA installs #14760 test does.import unslothitself cannot run on a CPU-only host, so the test executes the realfsdp2_launchfunctions in the orderunsloth/__init__.pycalls them, with the real_unsloth_trainer_initsource.