Skip to content

fix(device_type): flush and fence through the shared device helpers - #12284

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
li-lizhe:fix/device-agnostic-cache-flush
Oct 1, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
li-lizhe:fix/device-agnostic-cache-flush

Conversation

@li-lizhe

Copy link
Copy Markdown
Contributor

fix(device_type): flush and fence through the shared device helpers

Two remaining call sites keyed their accelerator work off CUDA alone.

unsloth/models/loader_utils.py

  • the retry after a failed load flushes before reallocating, else the half-built
    model and its replacement are resident at once and a large VLM OOMs. The flush
    asked torch.cuda.is_available() and then torch.xpu.is_available(), so on an
    NPU host neither ran and the retry reallocated on top of the previous attempt's
    blocks.
  • the same shape after the torchao conversion drops its copy of the model.
    Both now call clean_gpu_cache(), which dispatches to whichever backend
    device_type selected.

unsloth/optimizers/q_galore_adamw.py

  • step() fences with if torch.cuda.is_available(): torch.cuda.synchronize().
    The quantize kernels are async and the step drops the float weight storage right
    after launching them, so the fence is the only thing ordering that free against
    the device work; on XPU/NPU it was skipped. device_synchronize() is the
    cross-platform equivalent, already imported this way by unsloth/models/rl.py.

tests/test_device_helpers.py

  • test_model_call_sites_use_shared_cache_dispatch already guards llama/vision/
    gemma/gemma2/granite; it now covers loader_utils.py and q_galore_adamw.py too.

Verified on Ascend 910B (torch 2.15.0.dev20260917+cpu, torch_npu 2.15.0.dev20260917,
npu-lerobot container, 8 cards): torch.cuda.is_available() is False there, so the
old guard flushed nothing; it left 100.0 MiB reserved on npu:0 that
clean_gpu_cache() released, and device_synchronize() returned on the device.
pytest tests/test_device_helpers.py passes on the change (15 passed) and fails on
the old call sites with the new assertion.

Not tested: a real multi-card run, and the GGUF/torchao/compressed-tensors paths that
contain these flushes -- those are covered by the source scan above, not by a device run.

Two remaining call sites keyed their accelerator work off CUDA alone.

unsloth/models/loader_utils.py
- the retry after a failed load flushes before reallocating, else the half-built
  model and its replacement are resident at once and a large VLM OOMs. The flush
  asked `torch.cuda.is_available()` and then `torch.xpu.is_available()`, so on an
  NPU host neither ran and the retry reallocated on top of the previous attempt's
  blocks.
- the same shape after the torchao conversion drops its copy of the model.
Both now call `clean_gpu_cache()`, which dispatches to whichever backend
`device_type` selected.

unsloth/optimizers/q_galore_adamw.py
- `step()` fences with `if torch.cuda.is_available(): torch.cuda.synchronize()`.
  The quantize kernels are async and the step drops the float weight storage right
  after launching them, so the fence is the only thing ordering that free against
  the device work; on XPU/NPU it was skipped. `device_synchronize()` is the
  cross-platform equivalent, already imported this way by unsloth/models/rl.py.

tests/test_device_helpers.py
- test_model_call_sites_use_shared_cache_dispatch already guards llama/vision/
  gemma/gemma2/granite; it now covers loader_utils.py and q_galore_adamw.py too.

Verified on Ascend 910B (torch 2.15.0.dev20260917+cpu, torch_npu 2.15.0.dev20260917,
npu-lerobot container, 8 cards): `torch.cuda.is_available()` is False there, so the
old guard flushed nothing; it left 100.0 MiB reserved on npu:0 that
`clean_gpu_cache()` released, and `device_synchronize()` returned on the device.
`pytest tests/test_device_helpers.py` passes on the change (15 passed) and fails on
the old call sites with the new assertion.

Not tested: a real multi-card run, and the GGUF/torchao/compressed-tensors paths that
contain these flushes -- those are covered by the source scan above, not by a device run.

Signed-off-by: li-lizhe <[email protected]>
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the load-retry flush in loader_utils.py and the fence in q_galore_adamw.py step() only ran on CUDA (and XPU for the flush), and both shared helpers exist on main. Will get this reviewed.

@shimmyshimmer

Copy link
Copy Markdown
Member

Review record for #12284, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#259.

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 4a86eb2f83 (this PR's 4a86eb2f8)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 4a86eb2f83

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 4a86eb2f83 (this PR's 4a86eb2f8).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen
danielhanchen merged commit ed7a078 into unslothai:main Oct 1, 2026
31 of 34 checks passed
This was referenced Oct 8, 2026
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.

3 participants