Repository navigation
Fixes to XPU offloading during training - #9534
Conversation
|
Confirmed both image trainers still pick device and weight dtype with a CUDA-only rule (diffusion_dit_trainer.py:1874, diffusion_h3_trainer.py:522), so the 4-bit transformer lands off-device on XPU; mark it ready for review when you are happy with it and we will take a look. |
|
hey @danielhanchen, thanks for looking into this for me! |
|
hey @danielhanchen, sorry for taking so long with this, things have been busy on my end. I would say that the PR is just about ready for a merge whenever you're available to look it over. Thanks again! |
|
Confirmed the CUDA-only device and dtype rule is still there in diffusion_dit_trainer.py and diffusion_h3_trainer.py, and this puts the 4-bit transformer on the right device via device_map, which matches the failure in #9524. Will get this reviewed. |
Follow-up to the XPU device selection in this PR. The selection is right, and a differential over both trainers confirms an XPU box now binds device="xpu" with bf16 where it previously bound "cpu" while the bitsandbytes quantizer had already placed the 4-bit transformer on xpu:0. Four things around it needed fixing. The bf16 guard was not a guard. torch.xpu.is_bf16_supported() takes including_emulation and defaults it to True, then short-circuits before reading has_bfloat16_conversions, so the bare call answers True for every available XPU. That is the same emulation trap native_bf16_supported() exists to avoid on the CUDA side, so an emulation-only XPU was accepted into a bf16-throughout loop. native_bf16_supported_xpu() now asks for the native answer explicitly and falls back to the no-argument form on a torch that predates the parameter. The DiT trainer never got the empty_cache mirror the H3 trainer got. Both sites sit immediately before the multi-GB transformer load, one after the text encoders are freed and one after the VAE, so on an XPU box that memory stayed resident for the load that follows. Both boundaries now clear the selected accelerator. An uninitialised XPU driver killed the run. torch.xpu.is_available() raising propagated out of the trainer, where the same probe in dit_accelerator_missing_reason is guarded per accelerator. The selection moves into a shared resolve_train_device() that guards each probe and keeps CUDA first, so a box carrying both trains where it always did. A bf16 XPU run recorded its precision as "no". effective_mixed_precision still keyed the flow families on torch.cuda.is_available() while the loops bind bf16 on any accelerator, so identity_for_config mislabelled every XPU checkpoint and a later resume could be refused as a precision mismatch. It now resolves the device the same way the loops do. tests/test_diffusion_xpu_training.py covers the decision table with a bare fake torch.xpu that carries only the attributes each case names, so a missing-API case cannot pass on an auto-created attribute. Reverting any one of the four fixes fails it. CPU-only, no Intel hardware required. Verified on an NVIDIA host: a real bnb-nf4 load plus LoRA attach plus one optimizer step is unchanged against this PR's parent on parameter and quant-state placement, dtypes, load and step peak memory, and loss to the last digit. The XPU kernels themselves remain unverified here; no Intel device is reachable from CI or this host.
Comment-only. Deletes the four docstrings that restated their own code or their test's name, drops the section banners, and collapses the remaining multi-line rationales to one line each, keeping every load-bearing fact: the is_bf16_supported emulation short-circuit, why the fake torch.xpu must stay a bare namespace, why the decision is read at the _assert_trusted_base_model cut, and the identity_for_config resume invariant. test_diffusion_xpu_training.py 55 comment lines to 31 (26.7 to 16.8 percent). AST-verified comments, docstrings and whitespace only; 19 tests still pass.
…preflight Two gaps the XPU device selection opened, neither reachable before it. capture_rng_state and restore_rng_state handled the CPU and CUDA generators only, but both flow loops draw their per-step noise with randn_like on the TRAINING device (diffusion_dit_trainer.py:2248), so an XPU run's noise stream lives in the XPU generator. A resumed run silently continued from a fresh seed. Mirrors the CUDA block, including its all-or-nothing capture and its per-device restore; the XPU generator API is a 1:1 mapping of the CUDA one. bf16_unsupported_reason checked CUDA alone while dit_accelerator_missing_reason accepts any available XPU, so the start route admitted an emulation-only XPU, evicted the user's resident models, and only then hit the trainer's own guard. That ordering is the whole reason this preflight exists. The probe is tri-state rather than boolean. get_device_properties() raises when no device is really there, and collapsing that to unsupported made the preflight refuse nf4 on any host whose XPU cannot be interrogated: a probe failing CLOSED, caught by test_training_precision_preflight_error. Only a definite no rejects before eviction; an undeterminable capability is left to the child, which runs on the device and can ask properly. Full diffusion selection: 33 failures, all 33 present on main with the same selection, zero introduced. Reverting either fix or the fail-open rule fails the new tests.
Recording bf16 for an XPU flow run, two commits ago, removed the accidental barrier that used to stop a CUDA checkpoint resuming on an XPU box: the two precisions now match and nothing else in CheckpointIdentity distinguishes the backends. The restore then looks for torch_xpu_* keys in a bundle holding only torch_cuda_* ones, finds none, and returns quietly, so the destination device generator stays freshly seeded and every later latent and noise draw differs while the resume reports success. Measured: zero set_rng_state calls, no error. capture_rng_state now records which accelerator its device tensors came from, and _assert_required_state refuses a known mismatch before teardown, alongside the missing-stream checks already there for the same reason one level up. Deliberately not a CheckpointIdentity field: adding one changes the identity of every checkpoint ever written and would break resumes that work today. A bundle predating this carries no accelerator key and still resumes, since an unknown backend is not a mismatch. Diffusion plus checkpoint selection: 39 failures, all 39 present on main with the same selection, zero introduced.
Comment-only, the closing pass. Collapses the cross-backend resume note and the tri-state probe docstring, keeping both load-bearing facts: that nothing else distinguishes the backends because the identity records bf16 for each, and that an unprobeable XPU must not fail closed. AST-verified comments only.
|
Review record for #9534, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#55. Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found. Round 1 — reviewed mirror
|
#11450 landed on main as one squashed commit, so GitHub retargeted this PR to main and the merge no longer recognised the base commits this branch already carried. Three conflicts, all of the same shape: this branch adds a block where main has none, so HEAD wins in each. The rest of import_fixes.py auto merged, which matters because main moved on separately, and the resolution keeps that work: nothing in main's copy of any of the three files is missing here. The part the merge got wrong on its own: main carries the quant_state check at _gpu_init.py:110 with its `del` at 123, and this branch moved the call below the repair with its own `del`. Auto merge kept both, which is not merely the duplicate warning #11452 already fixed once, it is a NameError on `import unsloth`, since the second `del` deletes a name the first already removed. `test_the_check_is_called_once_and_after_the_repair` catches the duplicate call that causes it. Resolved to one call, after the repair, and one `del`. Verified after resolving: no duplicate top-level definitions, 106 passed and 4 skipped across the three suites, and 24 passed with 1 skipped in a venv holding only pytest and packaging. test_formatter_fixed_point reports studio/backend/tests/test_diffusion_xpu_training.py and tests/test_omni_auto_class_resolution.py as not fixed points. Both arrived from main today, in #9534 and #11467, and the same test fails on main itself at b7aca55, so they are pre-existing and not touched here.
* Fixes to XPU offloading during training * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed related issue of cache not being cleared on XPU * Studio: harden the XPU training path for the DiT and H3 trainers Follow-up to the XPU device selection in this PR. The selection is right, and a differential over both trainers confirms an XPU box now binds device="xpu" with bf16 where it previously bound "cpu" while the bitsandbytes quantizer had already placed the 4-bit transformer on xpu:0. Four things around it needed fixing. The bf16 guard was not a guard. torch.xpu.is_bf16_supported() takes including_emulation and defaults it to True, then short-circuits before reading has_bfloat16_conversions, so the bare call answers True for every available XPU. That is the same emulation trap native_bf16_supported() exists to avoid on the CUDA side, so an emulation-only XPU was accepted into a bf16-throughout loop. native_bf16_supported_xpu() now asks for the native answer explicitly and falls back to the no-argument form on a torch that predates the parameter. The DiT trainer never got the empty_cache mirror the H3 trainer got. Both sites sit immediately before the multi-GB transformer load, one after the text encoders are freed and one after the VAE, so on an XPU box that memory stayed resident for the load that follows. Both boundaries now clear the selected accelerator. An uninitialised XPU driver killed the run. torch.xpu.is_available() raising propagated out of the trainer, where the same probe in dit_accelerator_missing_reason is guarded per accelerator. The selection moves into a shared resolve_train_device() that guards each probe and keeps CUDA first, so a box carrying both trains where it always did. A bf16 XPU run recorded its precision as "no". effective_mixed_precision still keyed the flow families on torch.cuda.is_available() while the loops bind bf16 on any accelerator, so identity_for_config mislabelled every XPU checkpoint and a later resume could be refused as a precision mismatch. It now resolves the device the same way the loops do. tests/test_diffusion_xpu_training.py covers the decision table with a bare fake torch.xpu that carries only the attributes each case names, so a missing-API case cannot pass on an auto-created attribute. Reverting any one of the four fixes fails it. CPU-only, no Intel hardware required. Verified on an NVIDIA host: a real bnb-nf4 load plus LoRA attach plus one optimizer step is unchanged against this PR's parent on parameter and quant-state placement, dtypes, load and step peak memory, and loss to the last digit. The XPU kernels themselves remain unverified here; no Intel device is reachable from CI or this host. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Studio: tighten the comments added by the XPU training work Comment-only. Deletes the four docstrings that restated their own code or their test's name, drops the section banners, and collapses the remaining multi-line rationales to one line each, keeping every load-bearing fact: the is_bf16_supported emulation short-circuit, why the fake torch.xpu must stay a bare namespace, why the decision is read at the _assert_trusted_base_model cut, and the identity_for_config resume invariant. test_diffusion_xpu_training.py 55 comment lines to 31 (26.7 to 16.8 percent). AST-verified comments, docstrings and whitespace only; 19 tests still pass. * Studio: carry the XPU training path into resume and the pre-eviction preflight Two gaps the XPU device selection opened, neither reachable before it. capture_rng_state and restore_rng_state handled the CPU and CUDA generators only, but both flow loops draw their per-step noise with randn_like on the TRAINING device (diffusion_dit_trainer.py:2248), so an XPU run's noise stream lives in the XPU generator. A resumed run silently continued from a fresh seed. Mirrors the CUDA block, including its all-or-nothing capture and its per-device restore; the XPU generator API is a 1:1 mapping of the CUDA one. bf16_unsupported_reason checked CUDA alone while dit_accelerator_missing_reason accepts any available XPU, so the start route admitted an emulation-only XPU, evicted the user's resident models, and only then hit the trainer's own guard. That ordering is the whole reason this preflight exists. The probe is tri-state rather than boolean. get_device_properties() raises when no device is really there, and collapsing that to unsupported made the preflight refuse nf4 on any host whose XPU cannot be interrogated: a probe failing CLOSED, caught by test_training_precision_preflight_error. Only a definite no rejects before eviction; an undeterminable capability is left to the child, which runs on the device and can ask properly. Full diffusion selection: 33 failures, all 33 present on main with the same selection, zero introduced. Reverting either fix or the fail-open rule fails the new tests. * Studio: refuse a resume whose RNG state belongs to another accelerator Recording bf16 for an XPU flow run, two commits ago, removed the accidental barrier that used to stop a CUDA checkpoint resuming on an XPU box: the two precisions now match and nothing else in CheckpointIdentity distinguishes the backends. The restore then looks for torch_xpu_* keys in a bundle holding only torch_cuda_* ones, finds none, and returns quietly, so the destination device generator stays freshly seeded and every later latent and noise draw differs while the resume reports success. Measured: zero set_rng_state calls, no error. capture_rng_state now records which accelerator its device tensors came from, and _assert_required_state refuses a known mismatch before teardown, alongside the missing-stream checks already there for the same reason one level up. Deliberately not a CheckpointIdentity field: adding one changes the identity of every checkpoint ever written and would break resumes that work today. A bundle predating this carries no accelerator key and still resumes, since an unknown backend is not a mismatch. Diffusion plus checkpoint selection: 39 failures, all 39 present on main with the same selection, zero introduced. * Studio: tighten the comments added during review Comment-only, the closing pass. Collapses the cross-backend resume note and the tri-state probe docstring, keeping both load-bearing facts: that nothing else distinguishes the backends because the identity records bf16 for each, and that an unprobeable XPU must not fail closed. AST-verified comments only. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han <[email protected]>
Closes #9524