Commit Graph
1 Commits
Author SHA1 Message Date
li-lizheandPalash Debnath 59f37e0db0 fix(memory): release device cache across supported accelerators (#2317)
* fix(memory): release the device cache on every accelerator the engines can use

The dubbing and generation recovery paths open-coded the CUDA/MPS pair when
flushing the device cache. Engines pick their device through torch.accelerator,
so on an Ascend NPU or Intel XPU host those paths flushed nothing at all: the
allocator kept the blocks the offload had just freed, and the next allocation
failed with that memory still counted as in use.

Extract the narrow primitive free_vram() already used (no gc.collect, no cuBLAS
clear, covers CUDA/MPS/XPU/NPU, never raises) as release_device_cache() and call
it from the five recovery paths. free_vram() keeps its gc + cuBLAS behaviour.

Verified on an Ascend 910B4 (torch 2.15.0.dev + torch_npu 2.15.0.dev):
reserved npu memory stayed at 134.0 MiB with the old pair and dropped to 0.0 MiB
with the shared helper; the new tests fail 9/9 on the pre-change tree.

* docs(changelog): credit the cache-flush fix with its PR ref (#2317)

* fix(memory): follow the engines' accelerator and keep free_vram raising

Two review findings on the cache-release helper:

1. On a hybrid host (CUDA probes as available, inference runs elsewhere) the
   elif chain flushed CUDA and never reached the active allocator. Ask the same
   question the engine sidecars ask -- torch.accelerator.current_accelerator(
   check_available=True) -- and flush that backend, falling back to the
   CUDA/MPS/XPU/NPU probe chain only when the build cannot answer.

2. free_vram() propagated empty_cache() failures before, and model_lifecycle
   unload callers report a failed flush to the user, so the helper takes
   raise_on_failure and free_vram() passes True. The direct recovery calls stay
   best-effort.

Tests pin both, plus the cpu-answer and pre-2.6 fallbacks.

* fix(memory): flush selected allocator and offload SSE cleanup

* test(memory): remove source-text assertion

* test(memory): trim obsolete test tail

* fix(dub): flush MPS allocator after NLLB CPU fallback

---------

Co-authored-by: Palash Debnath <4178343+debpalash@users.noreply.github.com>
2026-09-24 17:17:42 +05:30