Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
"""The Windows video-memory readers, against scripted stand-ins for the gdi32 kernel thunks and for PDH.
The stand-ins are driven through the same ctypes structures the module passes, so struct layout, the two-call adapter
enumeration, the PCI match, handle cleanup and the counter-instance naming are exercised; only the OS behind them is fake.
"""
import ctypes
import functools
import os
import sys
from types import SimpleNamespace
import pytest
import torch
from invokeai.backend.util import wddm
from invokeai.backend.util.devices import TorchDevice
GIB = 1024**3
DEVICE = torch.device("cuda", 0)
OUR_BUS, OUR_DEVICE = 3, 0
def _luid_low(handle: int) -> int:
return 0x22A00 + handle
class _FakePdh:
"""A PDH that reports the given ``GPU Process Memory`` instances' shared usage."""
def __init__(
self,
instances: dict[str, int],
unavailable: tuple[str, ...] = (),
invalid: tuple[str, ...] = (),
):
self.instances = instances
self.unavailable = unavailable
self.invalid = invalid
self.counter_paths: list[str] = []
self.closed = 0
self._alive: list[object] = []
def PdhOpenQueryW(self, source, user_data, query_ref):
query_ref._obj.value = 1
return 0
def PdhAddEnglishCounterW(self, query, path, user_data, counter_ref):
if path in self.unavailable:
return -1073738824 # PDH_CSTATUS_NO_COUNTER
self.counter_paths.append(path)
handle = 2 + len(self.counter_paths)
counter_ref._obj.value = handle
return 0
def PdhCollectQueryData(self, query):
return 0
def PdhGetFormattedCounterArrayW(self, counter, fmt, size_ref, count_ref, buffer):
instances = self.instances
count = len(instances)
if buffer is None:
size_ref._obj.value = ctypes.sizeof(wddm._PdhCounterValueItem) * count
count_ref._obj.value = count
return wddm._PDH_MORE_DATA
items = (wddm._PdhCounterValueItem * count).from_buffer(buffer)
for item, (name, value) in zip(items, instances.items(), strict=True):
item.szName = name
item.FmtValue.largeValue = value
if name in self.invalid:
item.FmtValue.CStatus = 0xC0000BB8 # PDH_CSTATUS_INVALID_DATA
self._alive.append(items) # the name buffers live as long as the array object
count_ref._obj.value = count
return 0
def PdhCloseQuery(self, query):
self.closed += 1
return 0
class _FakeGdi32:
"""Adapters as (handle, bus, device); every thunk returns NTSTATUS 0 unless told otherwise."""
def __init__(self, adapters, budget=15 * GIB, usage=0, query_status=0):
self.adapters = adapters
self.budget = budget
self.usage = usage
self.query_status = query_status
self.enum_calls = 0
self.closed: list[int] = []
def D3DKMTEnumAdapters2(self, ref):
self.enum_calls += 1
enum = ref._obj
if not enum.pAdapters:
enum.NumAdapters = len(self.adapters)
return 0
for i, (handle, _bus, _device) in enumerate(self.adapters):
enum.pAdapters[i].hAdapter = handle
enum.pAdapters[i].AdapterLuid.LowPart = _luid_low(handle)
enum.pAdapters[i].AdapterLuid.HighPart = 0
return 0
def D3DKMTQueryAdapterInfo(self, ref):
query = ref._obj
address = wddm._AdapterAddress.from_address(query.pPrivateDriverData)
_handle, bus, device = next(a for a in self.adapters if a[0] == query.hAdapter)
address.BusNumber, address.DeviceNumber = bus, device
return 0
def D3DKMTQueryVideoMemoryInfo(self, ref):
info = ref._obj
info.Budget, info.CurrentUsage = self.budget, self.usage
return self.query_status
def D3DKMTCloseAdapter(self, ref):
self.closed.append(ref._obj.hAdapter)
return 0
@pytest.fixture
def windows_rocm(monkeypatch: pytest.MonkeyPatch):
"""A Windows ROCm build whose device 0 sits at PCI bus 3 with 16 GiB. Returns an installer for a fake gdi32."""
monkeypatch.setattr(sys, "platform", "win32")
monkeypatch.setattr(torch.version, "hip", "7.14.0")
props = SimpleNamespace(pci_domain_id=0, pci_bus_id=OUR_BUS, pci_device_id=OUR_DEVICE, total_memory=16 * GIB)
monkeypatch.setattr(torch.cuda, "get_device_properties", lambda index: props)
wddm.reset_cache()
def install(fake: _FakeGdi32) -> _FakeGdi32:
monkeypatch.setattr(wddm, "_load_gdi32", functools.lru_cache(maxsize=1)(lambda: fake))
return fake
yield install
wddm.reset_cache()
def test_reads_the_budget_of_the_adapter_at_the_devices_pci_location(windows_rocm):
gdi32 = windows_rocm(_FakeGdi32([(10, 7, 0), (20, OUR_BUS, OUR_DEVICE), (30, 0xFFFFFFFF, 0xFFFF)], budget=15 * GIB))
assert wddm.video_memory_budget(DEVICE) == 15 * GIB
assert sorted(gdi32.closed) == [10, 30], "the other adapters' handles are closed, ours stays open"
def test_resolves_the_adapter_once(windows_rocm):
gdi32 = windows_rocm(_FakeGdi32([(20, OUR_BUS, OUR_DEVICE)]))
wddm.video_memory_budget(DEVICE)
gdi32.budget = 12 * GIB # another GPU process started
assert wddm.video_memory_budget(DEVICE) == 12 * GIB
assert gdi32.enum_calls == 2, "one count call and one fill call, for the first query only"
def test_usage_beyond_the_card_is_unknown_and_keeps_the_budget(windows_rocm):
"""`CurrentUsage` counts bytes Windows has already paged out, so it can exceed the adapter's own size when the
process over-commits. It says nothing about residency then, but the budget must survive: the model cache's
free-VRAM cap needs it most in exactly that state."""
windows_rocm(_FakeGdi32([(20, OUR_BUS, OUR_DEVICE)], budget=15 * GIB, usage=17 * GIB))
assert wddm.local_video_memory(DEVICE) == (15 * GIB, None)
assert wddm.video_memory_budget(DEVICE) == 15 * GIB
@pytest.mark.parametrize(
("adapters", "kwargs"),
[
([(10, 7, 0)], {}),
([(20, OUR_BUS, OUR_DEVICE), (21, OUR_BUS, OUR_DEVICE)], {}),
([(20, OUR_BUS, OUR_DEVICE)], {"query_status": -1073741823}),
([(20, OUR_BUS, OUR_DEVICE)], {"budget": 0}),
([(20, OUR_BUS, OUR_DEVICE)], {"budget": 17 * GIB}),
],
ids=[
"no-adapter-at-the-location",
"two-adapters-at-the-location",
"query-fails",
"zero-budget",
"budget-over-total",
],
)
def test_unknown_rather_than_guessed(windows_rocm, adapters, kwargs):
gdi32 = windows_rocm(_FakeGdi32(adapters, **kwargs))
assert wddm.video_memory_budget(DEVICE) is None
if len(adapters) == 2:
assert sorted(gdi32.closed) == [20, 21], "an ambiguous match must not leak either handle"
@pytest.mark.parametrize(
("platform", "hip", "device"),
[("linux", "7.2.0", DEVICE), ("win32", None, DEVICE), ("win32", "7.14.0", torch.device("cpu"))],
ids=["linux-rocm", "windows-cuda", "cpu-device"],
)
def test_elsewhere_gdi32_is_never_touched(windows_rocm, monkeypatch, platform, hip, device):
def loader():
raise AssertionError("gdi32 must not be loaded outside Windows ROCm")
monkeypatch.setattr(wddm, "_load_gdi32", functools.lru_cache(maxsize=1)(loader))
monkeypatch.setattr(sys, "platform", platform)
monkeypatch.setattr(torch.version, "hip", hip)
assert wddm.video_memory_budget(device) is None
def test_reset_closes_the_cached_handle(windows_rocm):
gdi32 = windows_rocm(_FakeGdi32([(20, OUR_BUS, OUR_DEVICE)]))
wddm.video_memory_budget(DEVICE)
wddm.reset_cache()
assert gdi32.closed == [20]
@pytest.mark.parametrize("hip", ["7.14.0", None], ids=["rocm", "cuda"])
def test_process_usage_is_read_on_either_windows_build(windows_rocm, monkeypatch, hip):
"""The working-memory probe measures on CUDA too; the adapter is found by PCI location there just the same."""
gdi32 = windows_rocm(_FakeGdi32([(10, 7, 0), (20, OUR_BUS, OUR_DEVICE)], usage=5 * GIB))
monkeypatch.setattr(torch.version, "hip", hip)
assert wddm.process_local_usage(DEVICE) == 5 * GIB
assert gdi32.closed == [10]
@pytest.mark.parametrize(
"kwargs", [{"usage": 17 * GIB}, {"query_status": -1073741823}], ids=["usage-beyond-the-card", "query-fails"]
)
def test_process_usage_is_unknown_rather_than_guessed(windows_rocm, monkeypatch, kwargs):
windows_rocm(_FakeGdi32([(20, OUR_BUS, OUR_DEVICE)], **kwargs))
monkeypatch.setattr(torch.version, "hip", None)
assert wddm.process_local_usage(DEVICE) is None
def test_the_ungated_readers_elsewhere_never_touch_the_os(monkeypatch):
def loader():
raise AssertionError("no Windows library may be loaded off Windows")
monkeypatch.setattr(wddm, "_load_gdi32", functools.lru_cache(maxsize=1)(loader))
monkeypatch.setattr(wddm, "_load_pdh", functools.lru_cache(maxsize=1)(loader))
monkeypatch.setattr(sys, "platform", "linux")
assert wddm.process_local_usage(DEVICE) is None
assert wddm.process_paged_bytes(DEVICE) is None
monkeypatch.setattr(sys, "platform", "win32")
assert wddm.process_local_usage(torch.device("cpu")) is None
def _instance(pid: int, handle: int) -> str:
return f"pid_{pid}_luid_0x00000000_0x{_luid_low(handle):08X}_phys_0"
def test_paged_bytes_reads_this_process_on_the_devices_adapter(windows_rocm, monkeypatch):
windows_rocm(_FakeGdi32([(10, 7, 0), (20, OUR_BUS, OUR_DEVICE)]))
me = os.getpid()
pdh = _FakePdh(
{
_instance(me, 10): 5 * GIB, # this process, other adapter
_instance(me + 1, 20): 7 * GIB, # another process, our adapter
_instance(me, 20): 3 * GIB, # this process, our adapter (PDH may report upper-case hex)
}
)
monkeypatch.setattr(wddm, "_load_pdh", functools.lru_cache(maxsize=1)(lambda: pdh))
assert wddm.paged_bytes(DEVICE) == 3 * GIB
assert wddm.paged_bytes(DEVICE) == 3 * GIB
assert pdh.counter_paths == [r"\GPU Process Memory(*)\Shared Usage"], (
"only the counter this answer needs, opened once"
)
wddm.reset_cache()
assert pdh.closed == 1
def test_a_missing_counter_answers_unknown(windows_rocm, monkeypatch):
"""A Windows without the ``GPU Process Memory`` counters makes the paged amount unknown instead of failing."""
windows_rocm(_FakeGdi32([(20, OUR_BUS, OUR_DEVICE)]))
pdh = _FakePdh({_instance(os.getpid(), 20): 3 * GIB}, unavailable=(r"\GPU Process Memory(*)\Shared Usage",))
monkeypatch.setattr(wddm, "_load_pdh", functools.lru_cache(maxsize=1)(lambda: pdh))
assert wddm.paged_bytes(DEVICE) is None
def test_an_instance_with_invalid_data_is_skipped(windows_rocm, monkeypatch):
"""PDH leaves `largeValue` undefined unless the item's status says the data is good."""
windows_rocm(_FakeGdi32([(20, OUR_BUS, OUR_DEVICE)]))
me = _instance(os.getpid(), 20)
pdh = _FakePdh({me: 7 * GIB}, invalid=(me,))
monkeypatch.setattr(wddm, "_load_pdh", functools.lru_cache(maxsize=1)(lambda: pdh))
assert wddm.paged_bytes(DEVICE) is None
def test_paged_bytes_is_unknown_before_this_process_used_the_adapter(windows_rocm, monkeypatch):
windows_rocm(_FakeGdi32([(20, OUR_BUS, OUR_DEVICE)]))
pdh = _FakePdh({_instance(os.getpid() + 1, 20): GIB})
monkeypatch.setattr(wddm, "_load_pdh", functools.lru_cache(maxsize=1)(lambda: pdh))
assert wddm.paged_bytes(DEVICE) is None
def test_on_windows_cuda_only_the_ungated_reader_answers_the_paged_amount(windows_rocm, monkeypatch):
"""The model cache's spill warning stays a ROCm affair; the probe's spill check needs the figure on CUDA too."""
windows_rocm(_FakeGdi32([(20, OUR_BUS, OUR_DEVICE)]))
monkeypatch.setattr(torch.version, "hip", None)
pdh = _FakePdh({_instance(os.getpid(), 20): 3 * GIB})
monkeypatch.setattr(wddm, "_load_pdh", functools.lru_cache(maxsize=1)(lambda: pdh))
assert wddm.process_paged_bytes(DEVICE) == 3 * GIB
assert wddm.paged_bytes(DEVICE) is None
def test_paged_bytes_elsewhere_never_touches_pdh(monkeypatch):
def loader():
raise AssertionError("pdh must not be loaded outside Windows ROCm")
monkeypatch.setattr(wddm, "_load_pdh", functools.lru_cache(maxsize=1)(loader))
monkeypatch.setattr(sys, "platform", "linux")
assert wddm.paged_bytes(DEVICE) is None
def test_structures_match_the_d3dkmt_layout():
"""The kernel reads these by offset: a shifted field would read a neighbour's bytes, not fail."""
assert ctypes.sizeof(wddm._AdapterInfo) == 20
assert wddm._QueryVideoMemoryInfo.Budget.offset == 16
assert ctypes.sizeof(wddm._QueryVideoMemoryInfo) == 56
# PDH_FMT_COUNTERVALUE_ITEM_W on 64-bit: a name pointer, then the value whose 8-byte union follows a DWORD status.
assert wddm._PdhCounterValueItem.FmtValue.offset == 8
assert wddm._PdhCounterValue.largeValue.offset == 8
assert ctypes.sizeof(wddm._PdhCounterValueItem) == 24
needs_windows_rocm = pytest.mark.skipif(
sys.platform != "win32" or torch.version.hip is None or not torch.cuda.is_available(),
reason="needs a ROCm GPU under Windows",
)
@pytest.mark.slow
@needs_windows_rocm
def test_on_hardware_freed_memory_counts_as_headroom_again():
"""HIP keeps memory after a free and hands it to the next allocation; WDDM still counts it as this process's usage.
The capped free figure must recover anyway, or an offload the model cache just made would look like no progress."""
wddm.reset_cache()
device = torch.device("cuda", 0)
torch.empty(1, device=device)
torch.cuda.empty_cache()
assert wddm.video_memory_budget(device) is not None
before, _ = TorchDevice.cuda_mem_get_info(device)
block = torch.empty(2 * GIB, dtype=torch.uint8, device=device).fill_(1)
torch.cuda.synchronize()
held, _ = TorchDevice.cuda_mem_get_info(device)
del block
torch.cuda.empty_cache()
after, _ = TorchDevice.cuda_mem_get_info(device)
assert before - held >= 1.9 * GIB
assert after >= before - 64 * 2**20
@pytest.mark.slow
@needs_windows_rocm
def test_on_hardware_the_paged_amount_of_this_process_is_found():
"""The warning depends on finding this process's instance under the name the code builds; if that is wrong,
`paged_bytes` quietly answers None forever."""
wddm.reset_cache()
device = torch.device("cuda", 0)
block = torch.empty(64 * 2**20, dtype=torch.uint8, device=device).fill_(1)
torch.cuda.synchronize()
paged = wddm.paged_bytes(device)
assert paged is not None and paged >= 0
del block