Files
VoiceStudio/tests/test_device_cache_release.py
T
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

174 lines
6.5 KiB
Python

"""Device-cache release reaches every accelerator an engine can select.
The engine sidecars resolve their device through ``torch.accelerator``, so an
Ascend NPU host synthesizes on ``npu`` and an Intel Arc host on ``xpu`` (see
``tests/test_moss_tts_v15.py``). The dubbing and generation recovery paths
open-coded the CUDA/MPS pair instead, which made the flush a silent no-op on
those hosts: the allocator kept the blocks it had just been asked to hand back,
and the next allocation failed with the memory still counted as "in use".
These tests pin the shared primitive to all four backends, to the accelerator
the engines actually resolve (``torch.accelerator``, and the probe chain on
builds too old to have it), and to the two properties the call sites depend on —
the direct recovery calls never raise, and ``free_vram()`` still surfaces a
failed flush to its unload callers.
"""
from __future__ import annotations
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
BACKENDS = ("cuda", "mps", "xpu", "npu")
def _fake_torch(active: str, *, npu_present: bool = True, engine_accelerator: str | None = None):
"""A torch stub whose only available accelerator is ``active``.
``engine_accelerator`` mirrors what ``torch.accelerator.current_accelerator``
answers — i.e. the device the engine sidecars would synthesize on. Left
``None`` the stub has no ``torch.accelerator`` at all, like a pre-2.6 build.
"""
backends = {
name: SimpleNamespace(
is_available=lambda name=name: active == name,
empty_cache=Mock(),
)
for name in BACKENDS
}
torch = SimpleNamespace(
cuda=backends["cuda"],
mps=backends["mps"],
xpu=backends["xpu"],
backends=SimpleNamespace(mps=backends["mps"]),
)
if npu_present:
torch.npu = backends["npu"]
if engine_accelerator is not None:
accel = SimpleNamespace(type=engine_accelerator)
torch.accelerator = SimpleNamespace(current_accelerator=lambda **_kw: accel)
return torch, backends
@pytest.mark.parametrize("active", ["cpu", *BACKENDS])
def test_release_device_cache_flushes_the_active_accelerator(monkeypatch, active):
from services import model_manager as mm
torch, backends = _fake_torch(active)
monkeypatch.setattr(mm, "_lazy_torch", lambda: torch)
mm.release_device_cache()
for name, backend in backends.items():
assert backend.empty_cache.call_count == int(active == name), name
def test_release_device_cache_skips_a_backend_this_torch_does_not_ship(monkeypatch):
"""A torch build without torch_npu has no ``npu`` attribute to probe."""
from services import model_manager as mm
torch, backends = _fake_torch("npu", npu_present=False)
monkeypatch.setattr(mm, "_lazy_torch", lambda: torch)
mm.release_device_cache()
assert all(backend.empty_cache.call_count == 0 for backend in backends.values())
def test_release_device_cache_follows_the_engines_accelerator(monkeypatch):
"""Hybrid host: CUDA probes as available, but the engines run on npu.
``torch.accelerator.current_accelerator`` is the resolution the engine
sidecars use (``engines/moss_tts_v15``), so the flush has to follow it
instead of stopping at the first backend that merely probes as available —
otherwise the active allocator keeps the blocks and the recovery retry hits
the same OOM.
"""
from services import model_manager as mm
torch, backends = _fake_torch("cuda", engine_accelerator="npu")
monkeypatch.setattr(mm, "_lazy_torch", lambda: torch)
mm.release_device_cache()
assert backends["npu"].empty_cache.call_count == 1
assert backends["cuda"].empty_cache.call_count == 0
def test_release_device_cache_uses_explicit_caller_device(monkeypatch):
"""NLLB may run on CUDA while the default engine accelerator is NPU."""
from services import model_manager as mm
torch, backends = _fake_torch("npu", engine_accelerator="npu")
monkeypatch.setattr(mm, "_lazy_torch", lambda: torch)
mm.release_device_cache(device="cuda:0")
assert backends["cuda"].empty_cache.call_count == 1
assert backends["npu"].empty_cache.call_count == 0
mm.release_device_cache(device="cpu")
assert backends["npu"].empty_cache.call_count == 0
def test_release_device_cache_ignores_a_cpu_answer(monkeypatch):
"""A cpu answer must not end the search — probe the shipped backends."""
from services import model_manager as mm
torch, backends = _fake_torch("npu", engine_accelerator="cpu")
monkeypatch.setattr(mm, "_lazy_torch", lambda: torch)
mm.release_device_cache()
assert backends["npu"].empty_cache.call_count == 1
def test_release_device_cache_falls_back_without_torch_accelerator(monkeypatch):
"""A pre-2.6 build has no ``torch.accelerator`` — the probe chain still runs."""
from services import model_manager as mm
torch, backends = _fake_torch("npu") # no accelerator attribute at all
monkeypatch.setattr(mm, "_lazy_torch", lambda: torch)
mm.release_device_cache()
assert backends["npu"].empty_cache.call_count == 1
def test_release_device_cache_never_raises(monkeypatch):
"""Freeing memory is best-effort — a broken backend must not fail a request."""
from services import model_manager as mm
torch, backends = _fake_torch("npu")
backends["npu"].empty_cache.side_effect = RuntimeError("driver went away")
monkeypatch.setattr(mm, "_lazy_torch", lambda: torch)
mm.release_device_cache() # must not raise
def test_free_vram_still_propagates_a_flush_failure(monkeypatch):
"""``free_vram()`` keeps its original contract: unload callers report a
failed flush (``model_lifecycle`` records ``success: False, reason: …``)."""
from services import model_manager as mm
torch, backends = _fake_torch("cuda")
backends["cuda"].empty_cache.side_effect = RuntimeError("driver went away")
monkeypatch.setattr(mm, "_lazy_torch", lambda: torch)
monkeypatch.setattr(mm, "_clear_cublas_workspaces", lambda torch: None)
with pytest.raises(RuntimeError):
mm.free_vram()
def test_free_vram_still_collects_cublas_and_flushes(monkeypatch):
"""``free_vram()`` keeps its gc + cuBLAS clear and now shares the flush."""
from services import model_manager as mm
torch, backends = _fake_torch("cuda")
cleared = Mock()
monkeypatch.setattr(mm, "_lazy_torch", lambda: torch)
monkeypatch.setattr(mm, "_clear_cublas_workspaces", cleared)
mm.free_vram()
assert cleared.call_count == 1
assert backends["cuda"].empty_cache.call_count == 1