Repository navigation
Native FSDP2: keep accelerate's FSDP2 for UNSLOTH_FSDP2=1 on XPU / NPU / other non-CUDA installs - #14760
Conversation
… installs Before the native backend, UNSLOTH_FSDP2=1 exported accelerate's FSDP2 env on every device. Native FSDP2 refuses XPU / NPU / HPU / ..., so without an explicit backend those installs now go back to accelerate's FSDP2, decided from the torch build and vendor packages without importing torch.
|
@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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c3fb81b25f
ℹ️ 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".
| for name in ("cuda", "hip", "xpu"): | ||
| match = re.search(rf"^{name}\s*(?::[^=\n]*)?=\s*(.+?)\s*$", source, re.M) |
There was a problem hiding this comment.
Detect XPU wheels whose runtime marker is null
When an Intel wheel identifies itself through an __version__ suffix such as +xpu but has torch.version.xpu = None—a supported case already documented in the repository—this parser records no accelerator, so non_cuda_install() returns None. Consequently, UNSLOTH_FSDP2=1 still selects the native backend and is later refused when the live XPU device is detected, leaving the XPU launch regression this change intends to fix. Parse __version__ as a fallback (and apply the equivalent fallback when torch is already imported).
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, fixed. The repo already treats torch.version.xpu = None on a working +xpu wheel as a real case (studio hardware.py and install_python_stack.py both fall back to the +xpu label), and torch older than 2.7 has no xpu attribute in torch/version.py at all. _torch_build() now takes the +xpu suffix of __version__ as the XPU marker when xpu is empty, both when reading torch/version.py from disk and when torch is already imported, so the launch step and Trainer setup still agree. I kept it to the version label and did not use torch.xpu._is_compiled(), because the launch step cannot see that before torch is imported and the two calls would disagree. Tests: test_xpu_version_label_with_a_null_runtime_marker, test_xpu_version_label_read_without_importing_torch, and test_cpu_wheel_label_is_not_xpu for the negative case.
| if backend == "" and non_cuda_install() is not None: | ||
| # Native FSDP2 runs on CUDA / ROCm or the CPU. On an XPU / NPU / HPU / ... install, | ||
| # UNSLOTH_FSDP2=1 keeps accelerate's FSDP2 wrap, as it did before the native backend existed; | ||
| # an explicit UNSLOTH_FSDP2_BACKEND=native is still refused (refuse_native_backend_conflicts). | ||
| return False |
There was a problem hiding this comment.
Honor UNSLOTH_FSDP2=native on non-CUDA installs
When the documented UNSLOTH_FSDP2=native form is used on an XPU/NPU/HPU install without UNSLOTH_FSDP2_BACKEND, this condition treats it like the generic UNSLOTH_FSDP2=1 request and silently routes to Accelerate. That contradicts unsloth_fsdp2_requested(), the function's own docstring, and the native backend documentation, all of which define the native value as selecting Unsloth's native wrapper; it should remain native and reach the existing unsupported-device refusal just like UNSLOTH_FSDP2_BACKEND=native.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, fixed. UNSLOTH_FSDP2=native is an explicit pick just like UNSLOTH_FSDP2_BACKEND=native, so only the generic UNSLOTH_FSDP2=1 falls back to accelerate's FSDP2 on a non-CUDA install now. The explicit native request stays native. Native FSDP2 still cannot run on an XPU / NPU / HPU device, so Trainer setup refuses it before touching the model (refuse_native_backend_conflicts), the same as with UNSLOTH_FSDP2_BACKEND=native. The refusal message now also says to use UNSLOTH_FSDP2=1 or UNSLOTH_FSDP2_BACKEND=accelerate for accelerate's FSDP2 on that device. A CPU Trainer with the explicit native pick still runs natively on gloo. Test: test_unsloth_fsdp2_native_on_xpu_stays_native_and_is_refused.
| if _env_flag("ACCELERATE_USE_CPU"): | ||
| return None |
There was a problem hiding this comment.
Preserve TrainingArguments CPU launches on accelerator installs
When an XPU/NPU/HPU installation is launched with TrainingArguments(use_cpu=True) or the legacy no_cuda=True, ACCELERATE_USE_CPU is not set during this import-time check—the Trainer later passes the flag directly to PartialState. The code therefore routes to Accelerate FSDP before the arguments are available; Accelerate classifies the run as MULTI_CPU, for which it does not activate its FSDP plugin, so the requested FSDP2 sharding is silently lost. Keep this path native until Trainer setup can honor _trainer_uses_cpu(), or otherwise propagate the argument into the launch decision.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Valid, fixed. TrainingArguments(use_cpu=True) / no_cuda=True never reaches the env, and accelerate's AcceleratorState only turns on FSDP for MULTI_GPU / MLU / SDAA / MUSA / NPU / XPU / HPU / NEURON (accelerate 1.15 state.py, the distributed_type in [...] block before the ACCELERATE_USE_FSDP check). A MULTI_CPU run would lose the sharding without any error. The launch step can't see the TrainingArguments, so it still exports accelerate's FSDP2 env on these installs, and now records exactly which keys it wrote. In the Trainer init wrapper, a use_cpu / no_cuda Trainer calls reclaim_native_fsdp2_for_cpu_trainer, which puts those keys back before the Accelerator is built (keeping any FSDP_* value the user set) and hands the run to native FSDP2 on the gloo CPU mesh. That is the same path a CPU Trainer takes on a CUDA install, and what this install did before this PR. Tests: test_cpu_trainer_on_xpu_install_takes_native_back[use_cpu|no_cuda], test_cpu_trainer_reclaim_needs_the_non_cuda_route, plus an end-to-end gloo run, test_xpu_install_cpu_trainer_shards_natively: a world of 2 with a spoofed XPU torch build and the real _unsloth_trainer_init. Its model comes out natively sharded, with accelerate on MULTI_CPU and ACCELERATE_USE_FSDP unset. It fails on the previous head.
…Trainer, test env restore - A +xpu wheel with torch.version.xpu = None (or no xpu attribute) is detected from the version label, with and without torch imported. - UNSLOTH_FSDP2=native is an explicit native pick like UNSLOTH_FSDP2_BACKEND=native: it stays native and is refused at Trainer setup on a live non-CUDA device, now naming the accelerate alternative. - TrainingArguments(use_cpu / no_cuda) on a non-CUDA install undoes the launch step's accelerate FSDP2 env before the Accelerator exists and shards natively on gloo: accelerate's MULTI_CPU never builds its FSDP plugin. - The routing tests restore os.environ, so the exported FSDP env no longer leaks into later files. [skip ci]
# Conflicts: # unsloth/models/_utils.py
…nv after the main merge [skip ci]
|
Codex Review: Didn't find any major issues. Hooray! 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". |
…still exercise the refusals
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? 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". |
…wrapped the Trainer (#15427) ## 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 - No real XPU / NPU / MLU hardware: the non-CUDA install is simulated through `torch.version` build markers, as the existing #14760 test does. - `import unsloth` itself cannot run on a CPU-only host, so the test executes the real `fsdp2_launch` functions in the order `unsloth/__init__.py` calls them, with the real `_unsloth_trainer_init` source.
Problem
Before #14273,
UNSLOTH_FSDP2=1undertorchrunalways exported accelerate's FSDP2 launch env, so it worked wherever accelerate's FSDP2 does, Intel XPU included. #14273 made the native backend the default forUNSLOTH_FSDP2=1and refuses every non-CUDA accelerator (NATIVE_BACKEND_TABLEdevice rows: XPU, NPU, HPU, MLU, MUSA, SDAA, Neuron) at Trainer setup. The same launch command that trained on XPU before now raisesRuntimeError: ... cannot run under Intel XPU. The only way back is knowing to setUNSLOTH_FSDP2_BACKEND=accelerate.Change
fsdp2_launch.non_cuda_install()returns the non-CUDA accelerator accelerate would pick on this install, from the torch build (torch.version.cuda / hip / xpu, read fromtorch/version.pywhen torch is not imported yet) and the installed vendor packages. It never reports one on a CUDA or ROCm torch build, or for a CPU launch (ACCELERATE_USE_CPU).unsloth_fsdp2_native_requested()returns False in that case when no backend is set explicitly, so the launch step exports accelerate's FSDP2 env exactly as before Native FSDP2: fully_shard per block and root for every model, LoRA, QLoRA and full finetuning (UNSLOTH_FSDP2=1) #14273 (and before the process group exists, so accelerate still picks itsxpu:xccl,cpu:gloobackend for FULL_STATE_DICT saves). Since the answer does not depend on importing torch, the launch step and Trainer setup agree.UNSLOTH_FSDP2_BACKEND=nativestill selects native and is still refused on those devices. CUDA and ROCm launches do not change.Tests
tests/test_fsdp2_non_cuda_accelerate_route.py(7 cases): XPU build routes to accelerate (ACCELERATE_USE_FSDP=true,FSDP_VERSION=2), a vendor package on a non-CUDA build routes to accelerate, CUDA / ROCm builds stay native, explicit native and CPU launches stay native, and the torch build is read fromversion.pywithout importing torch. On main 3 of them fail (the routing cases); on this branch all 7 pass.tests/test_fsdp2_native_backend_table.pyandtests/test_fsdp2_detection.pypass on this branch. When both files run in one pytest process,test_loader_utils_versionsandtest_a_built_accelerator_is_authoritativefail the same way on main, because module state leaks from the backend table file. Run on their own, they pass.Not covered