File size: 866 Bytes
3afd6d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from pathlib import Path
import sys
import types

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from utils import model_memory


def test_cleanup_model_memory_skips_mps_cache_when_backend_is_unavailable(monkeypatch):
    calls = []

    class FakeCuda:
        @staticmethod
        def is_available():
            return False

    class FakeMpsBackend:
        @staticmethod
        def is_available():
            return False

    class FakeBackends:
        mps = FakeMpsBackend()

    class FakeMps:
        @staticmethod
        def empty_cache():
            calls.append("mps_empty_cache")

    fake_torch = types.SimpleNamespace(
        cuda=FakeCuda(),
        backends=FakeBackends(),
        mps=FakeMps(),
    )
    monkeypatch.setitem(sys.modules, "torch", fake_torch)

    model_memory.cleanup_model_memory()

    assert calls == []