Repository navigation
fix(device_type): flush and fence through the shared device helpers - #12284
Conversation
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]>
|
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. |
|
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
|
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
model and its replacement are resident at once and a large VLM OOMs. The flush
asked
torch.cuda.is_available()and thentorch.xpu.is_available(), so on anNPU host neither ran and the retry reallocated on top of the previous attempt's
blocks.
Both now call
clean_gpu_cache(), which dispatches to whichever backenddevice_typeselected.unsloth/optimizers/q_galore_adamw.py
step()fences withif 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 thecross-platform equivalent, already imported this way by unsloth/models/rl.py.
tests/test_device_helpers.py
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 theold guard flushed nothing; it left 100.0 MiB reserved on npu:0 that
clean_gpu_cache()released, anddevice_synchronize()returned on the device.pytest tests/test_device_helpers.pypasses on the change (15 passed) and fails onthe 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.