Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: find the AMD Vulkan driver through the Windows device registrations (#10564)

oobabooga0f58608a0008
diff --git a/studio/backend/tests/test_install_resolve_prebuilt.py b/studio/backend/tests/test_install_resolve_prebuilt.py
index 2a6e4a11d85..c17f4188bd1 100644
--- a/studio/backend/tests/test_install_resolve_prebuilt.py
+++ b/studio/backend/tests/test_install_resolve_prebuilt.py
@@ -13,13 +13,16 @@ because nothing in-tree mirrors it, and skips when that release is unreachable.
from __future__ import annotations
+import contextlib
+import ctypes
import dataclasses
import importlib
import json
+import ntpath
import os
import sys
from pathlib import Path
-from types import SimpleNamespace
+from types import ModuleType, SimpleNamespace
import pytest
@@ -2826,43 +2829,105 @@ def test_the_icd_search_path_is_built_per_call_not_at_import(monkeypatch):
# ---------------------------------------------------------------------------
+class _FakeKeyHandle:
+ def __init__(self, payload):
+ self.payload = payload
+
+ def __enter__(self):
+ return self.payload
+
+ def __exit__(self, *exc):
+ return False
+
+
+class _FakeDeviceClass(dict):
+ """A device class key: instance-name -> {value name: (data, kind)}."""
+
+
class _FakeIcdWinreg:
HKEY_LOCAL_MACHINE = object()
REG_DWORD = 4
REG_SZ = 1
+ REG_MULTI_SZ = 7
- def __init__(self, by_key):
+ def __init__(
+ self,
+ by_key,
+ devices = None,
+ ):
self._by_key = by_key
+ self._devices = devices or {}
def OpenKey(self, parent, name):
- if name not in self._by_key:
- raise FileNotFoundError(name)
- entries = self._by_key[name]
-
- class _Key:
- def __enter__(_self):
- return entries
-
- def __exit__(_self, *exc):
- return False
-
- return _Key()
-
- def QueryInfoKey(self, entries):
- return (0, len(entries), 0)
+ if isinstance(parent, _FakeDeviceClass):
+ instance = parent.get(name)
+ if instance is None:
+ raise FileNotFoundError(name)
+ if instance == "denied":
+ raise PermissionError(name)
+ return _FakeKeyHandle(instance)
+ if name in self._devices:
+ return _FakeKeyHandle(self._devices[name])
+ if name in self._by_key:
+ return _FakeKeyHandle(self._by_key[name])
+ raise FileNotFoundError(name)
+
+ def QueryInfoKey(self, payload):
+ if isinstance(payload, _FakeDeviceClass):
+ return (len(payload), 0, 0)
+ return (0, len(payload), 0)
+
+ def EnumKey(self, payload, index):
+ return list(payload)[index]
def EnumValue(self, entries, index):
return entries[index]
+ def QueryValueEx(self, instance, value_name):
+ if value_name not in instance:
+ raise FileNotFoundError(value_name)
+ return instance[value_name]
+
-def _icd_paths(monkeypatch, by_key):
+def _icd_paths(
+ monkeypatch,
+ by_key,
+ devices = None,
+ present = None,
+):
+ """Every instance counts as present unless a per-class callback says otherwise."""
monkeypatch.setattr(ilp.sys, "platform", "win32")
for name in ("VK_DRIVER_FILES", "VK_ICD_FILENAMES"):
monkeypatch.delenv(name, raising = False)
- monkeypatch.setitem(sys.modules, "winreg", _FakeIcdWinreg(by_key))
+ monkeypatch.setitem(sys.modules, "winreg", _FakeIcdWinreg(by_key, devices))
+ if present is None:
+
+ def present(class_key_path):
+ return set((devices or {}).get(class_key_path) or ())
+
+ monkeypatch.setattr(ilp, "_windows_present_class_instances", present)
return ilp._amd_vulkan_icd_manifest_paths()
+_DISPLAY_CLASS_KEY = ilp._WINDOWS_DISPLAY_CLASS_KEY
+_SOFTWARE_COMPONENT_CLASS_KEY = ilp._WINDOWS_VULKAN_DEVICE_CLASS_KEYS[1]
+
+
+def _display_class(
+ *,
+ instance = "0000",
+ value,
+ kind = _FakeIcdWinreg.REG_SZ,
+ key = None,
+):
+ """One device class key holding a single VulkanDriverName registration."""
+ return {
+ key or _DISPLAY_CLASS_KEY: _FakeDeviceClass(
+ {instance: {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (value, kind)}}
+ )
+ }
+
+
def test_the_windows_registry_is_not_read_when_the_loader_is_forced(
monkeypatch, _present_manifest, tmp_path
):
@@ -2944,3 +3009,652 @@ def test_a_non_dword_icd_registration_is_ignored(monkeypatch, _present_manifest)
{_DRIVERS_KEY: [(_present_manifest, "0", _FakeIcdWinreg.REG_SZ)]},
)
assert paths == []
+
+
+# ---------------------------------------------------------------------------
+# Device-registered ICDs
+# ---------------------------------------------------------------------------
+
+
+ "class_key", [_DISPLAY_CLASS_KEY, _SOFTWARE_COMPONENT_CLASS_KEY], ids = ["adapter", "component"]
+)
+ "kind, count",
+ [(_FakeIcdWinreg.REG_SZ, 1), (_FakeIcdWinreg.REG_MULTI_SZ, 2)],
+ ids = ["single", "multiple"],
+)
+def test_device_registrations_find_the_amd_driver(monkeypatch, tmp_path, class_key, kind, count):
+ paths = [_icd(tmp_path / str(i) / "amd-vulkan64.json") for i in range(count)]
+ value = paths[0] if kind == _FakeIcdWinreg.REG_SZ else paths
+ devices = _display_class(value = value, kind = kind, key = class_key)
+ assert _icd_paths(monkeypatch, {}, devices) == paths
+ assert _REAL_AMD_VULKAN_ICD_PRESENT() is True
+
+
+def test_an_unexpected_value_type_is_ignored(monkeypatch, _present_manifest):
+ # REG_BINARY is not a supported registration type.
+ binary = _display_class(value = _present_manifest, kind = 3)
+ assert _icd_paths(monkeypatch, {}, binary) == []
+
+
+def test_a_stale_device_registration_is_not_evidence(monkeypatch, tmp_path):
+ # A missing component manifest must not hide a valid adapter registration.
+ missing = str(tmp_path / "B419548" / "amd-vulkan64.json")
+ stale = _display_class(value = missing, key = _SOFTWARE_COMPONENT_CLASS_KEY)
+ assert _icd_paths(monkeypatch, {}, stale) == []
+
+ good = _icd(tmp_path / "store" / "amd-vulkan64.json")
+ both = {**stale, **_display_class(value = good)}
+ assert _icd_paths(monkeypatch, {}, both) == [good]
+
+
+def test_a_manifest_registered_twice_is_listed_once(monkeypatch, _present_manifest):
+ devices = {
+ **_display_class(value = _present_manifest),
+ **_display_class(value = _present_manifest, key = _SOFTWARE_COMPONENT_CLASS_KEY),
+ }
+ legacy = {_DRIVERS_KEY: [(_present_manifest, 0, _FakeIcdWinreg.REG_DWORD)]}
+ assert _icd_paths(monkeypatch, legacy, devices) == [_present_manifest]
+
+
+def test_a_relative_device_registration_is_not_guessed_at(monkeypatch):
+ assert _icd_paths(monkeypatch, {}, _display_class(value = "amd-vulkan64.json")) == []
+
+
+ "name, entry",
+ [
+ ("Properties", "denied"),
+ ("0000", {"DriverDesc": ("Some other adapter", 1)}),
+ # A digit-named instance, so this one reaches OpenKey and raises there;
+ # "Properties" is filtered by isdigit before any key is opened.
+ ("0002", "denied"),
+ ],
+ ids = ["restricted-subkey", "unregistered-adapter", "denied-instance"],
+)
+def test_other_registry_entries_do_not_hide_a_manifest(monkeypatch, _present_manifest, name, entry):
+ devices = {
+ _DISPLAY_CLASS_KEY: _FakeDeviceClass(
+ {name: entry, "0001": {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (_present_manifest, 1)}}
+ )
+ }
+ assert _icd_paths(monkeypatch, {}, devices) == [_present_manifest]
+
+
+def test_the_device_scan_is_not_read_when_the_loader_is_forced(
+ monkeypatch, _present_manifest, tmp_path
+):
+ assert _icd_paths(monkeypatch, {}, _display_class(value = _present_manifest)) == [
+ _present_manifest
+ ]
+ monkeypatch.setenv("VK_DRIVER_FILES", _icd(tmp_path / "intel_icd.json"))
+ assert ilp._amd_vulkan_icd_manifest_paths() == [str(tmp_path / "intel_icd.json")]
+
+
+def test_a_32_bit_device_registration_does_not_answer_for_the_x64_bundle(
+ monkeypatch, _present_manifest
+):
+ devices = {
+ _DISPLAY_CLASS_KEY: _FakeDeviceClass(
+ {"0000": {"VulkanDriverNameWow": (_present_manifest, 1)}}
+ )
+ }
+ assert _icd_paths(monkeypatch, {}, devices) == []
+
+
+def test_the_legacy_key_still_answers_on_its_own(monkeypatch, _present_manifest):
+ assert _icd_paths(
+ monkeypatch, {_DRIVERS_KEY: [(_present_manifest, 0, _FakeIcdWinreg.REG_DWORD)]}, {}
+ ) == [_present_manifest]
+
+
+def test_a_removed_device_registration_is_not_evidence(monkeypatch, tmp_path):
+ left_behind = _icd(tmp_path / "removed" / "amd-vulkan64.json")
+ devices = {
+ _DISPLAY_CLASS_KEY: _FakeDeviceClass(
+ {
+ "0000": {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (left_behind, 1)},
+ "0001": {"DriverDesc": ("The adapter that is actually here", 1)},
+ }
+ )
+ }
+ assert os.path.isfile(left_behind)
+ assert _icd_paths(monkeypatch, {}, devices, present = lambda _key: {"0001"}) == []
+
+ assert _icd_paths(monkeypatch, {}, devices, present = lambda _key: {"0000"}) == [left_behind]
+
+
+def test_a_removed_device_does_not_hide_a_present_one(monkeypatch, tmp_path):
+ removed = _icd(tmp_path / "removed" / "amd-vulkan64.json")
+ live = _icd(tmp_path / "live" / "amd-vulkan64.json")
+ devices = {
+ _DISPLAY_CLASS_KEY: _FakeDeviceClass(
+ {
+ "0000": {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (removed, 1)},
+ "0001": {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (live, 1)},
+ }
+ )
+ }
+ assert _icd_paths(monkeypatch, {}, devices, present = lambda _key: {"0001"}) == [live]
+
+
+def test_presence_is_resolved_per_class_key(monkeypatch, tmp_path):
+ # Instance numbers are local to each class.
+ adapter = _icd(tmp_path / "adapter" / "amd-vulkan64.json")
+ component = _icd(tmp_path / "component" / "amd-vulkan64.json")
+ devices = {
+ **_display_class(value = adapter),
+ **_display_class(value = component, key = _SOFTWARE_COMPONENT_CLASS_KEY),
+ }
+ only_adapter = _icd_paths(
+ monkeypatch,
+ {},
+ devices,
+ present = lambda key: {"0000"} if key == _DISPLAY_CLASS_KEY else set(),
+ )
+ assert only_adapter == [adapter]
+
+
+def test_an_unknowable_presence_answer_keeps_the_legacy_key_alone(
+ monkeypatch, tmp_path, _present_manifest
+):
+ device_only = _display_class(value = _icd(tmp_path / "device" / "amd-vulkan64.json"))
+ assert _icd_paths(monkeypatch, {}, device_only, present = lambda _key: None) == []
+
+ legacy = {_DRIVERS_KEY: [(_present_manifest, 0, _FakeIcdWinreg.REG_DWORD)]}
+ assert _icd_paths(monkeypatch, legacy, device_only, present = lambda _key: None) == [
+ _present_manifest
+ ]
+
+
+def test_no_present_device_of_that_class_is_an_answer(monkeypatch, tmp_path):
+ device_only = _display_class(value = _icd(tmp_path / "device" / "amd-vulkan64.json"))
+ assert _icd_paths(monkeypatch, {}, device_only, present = lambda _key: set()) == []
+
+
+class _FakeCfgMgr:
+ """Fake CfgMgr32 that writes ctypes output parameters on any platform."""
+
+ def __init__(
+ self,
+ by_class,
+ fail_at = None,
+ driver_of = None,
+ # device id -> (CM_Get_DevNode_Status, problem number). Absent means healthy.
+ status_of = None,
+ # Ids that arrive after the list is sized, outgrowing the caller's buffer.
+ arriving = None,
+ ):
+ # class guid -> device ids; device id -> CM_DRP_DRIVER value.
+ self._by_class = by_class
+ self._fail_at = fail_at
+ self._driver_of = driver_of or {}
+ self._status_of = status_of or {}
+ self._arriving = list(arriving or [])
+ self.calls = []
+
+ def _result(self, call):
+ return _CR_FAILURE if self._fail_at == call else ilp._CR_SUCCESS
+
+ def CM_Get_Device_ID_List_SizeW(self, size_ptr, guid, flags):
+ self.calls.append(("size", guid, flags))
+ ids = list(self._by_class.get(guid, []))
+ # Include each ID's terminator and the final NUL.
+ size_ptr[0] = sum(len(entry) + 1 for entry in ids) + 1
+ return self._result("size")
+
+ def CM_Get_Device_ID_ListW(self, guid, buffer, length, flags):
+ self.calls.append(("list", guid, flags))
+ # Queued ids join the class now, after the caller sized its buffer without them.
+ if self._arriving:
+ self._by_class[guid] = list(self._by_class.get(guid, [])) + self._arriving
+ self._arriving = []
+ ids = list(self._by_class.get(guid, []))
+ block = "".join(entry + "\0" for entry in ids) + "\0"
+ if len(block) > length:
+ # The real API refuses an undersized buffer rather than truncating.
+ return ilp._CR_BUFFER_SMALL
+ for index, char in enumerate(block):
+ buffer[index] = char
+ return self._result("list")
+
+ def CM_Get_DevNode_Status(self, status_ptr, problem_ptr, devinst, flags):
+ device_id = list(self._driver_of)[int(devinst.value) - 1]
+ self.calls.append(("status", device_id))
+ if self._fail_at == "status":
+ return _CR_FAILURE
+ status, problem = self._status_of.get(device_id, (0, 0))
+ status_ptr[0] = status
+ problem_ptr[0] = problem
+ return ilp._CR_SUCCESS
+
+ def CM_Locate_DevNodeW(self, devinst_ptr, device_id, flags):
+ self.calls.append(("locate", device_id))
+ if device_id not in self._driver_of:
+ return _CR_FAILURE
+ devinst_ptr[0] = list(self._driver_of).index(device_id) + 1
+ return self._result("locate")
+
+ def CM_Get_DevNode_Registry_PropertyW(self, devinst, prop, kind, buffer, size_ptr, flags):
+ device_id = list(self._driver_of)[int(devinst.value) - 1]
+ driver = self._driver_of[device_id]
+ if buffer is None:
+ # Unbound devices have no driver property.
+ size_ptr[0] = (len(driver) + 1) * ctypes.sizeof(ctypes.c_wchar) if driver else 0
+ return ilp._CR_SUCCESS
+ for index, char in enumerate(driver):
+ buffer[index] = char
+ return self._result("property")
+
+
+_CR_FAILURE = 0x00000013 # cfg.h CR_FAILURE; any non-zero is a failed CM_ call.
+_DISPLAY_GUID = "{4d36e968-e325-11ce-bfc1-08002be10318}"
+
+
+def _with_cfgmgr(monkeypatch, fake):
+ # WinDLL is absent on Linux.
+ monkeypatch.setattr(ctypes, "WinDLL", lambda _name: fake, raising = False)
+
+
+def test_the_presence_probe_reads_the_present_device_list(monkeypatch):
+ fake = _FakeCfgMgr(
+ {_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0", "PCI\\VEN_1002&DEV_7448\\1"]},
+ driver_of = {
+ "PCI\\VEN_1002&DEV_1586\\0": _DISPLAY_GUID + "\\0000",
+ "PCI\\VEN_1002&DEV_7448\\1": _DISPLAY_GUID + "\\0003",
+ },
+ )
+ _with_cfgmgr(monkeypatch, fake)
+ assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == {
+ "0000",
+ "0003",
+ }
+ assert fake.calls[0] == (
+ "size",
+ _DISPLAY_GUID,
+ ilp._CM_GETIDLIST_FILTER_CLASS | ilp._CM_GETIDLIST_FILTER_PRESENT,
+ )
+
+
+def test_a_present_device_with_no_driver_bound_names_no_instance(monkeypatch):
+ fake = _FakeCfgMgr(
+ {_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0", "SWD\\NO_DRIVER\\1"]},
+ driver_of = {
+ "PCI\\VEN_1002&DEV_1586\\0": _DISPLAY_GUID + "\\0000",
+ "SWD\\NO_DRIVER\\1": "",
+ },
+ )
+ _with_cfgmgr(monkeypatch, fake)
+ assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == {"0000"}
+
+
+def test_no_present_device_of_the_class_is_an_empty_answer(monkeypatch):
+ _with_cfgmgr(monkeypatch, _FakeCfgMgr({_DISPLAY_GUID: []}))
+ assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == set()
+
+
[email protected]("failing_call", ["size", "list", "locate"])
+def test_a_failed_cm_call_answers_none(monkeypatch, failing_call):
+ fake = _FakeCfgMgr(
+ {_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0"]},
+ fail_at = failing_call,
+ driver_of = {"PCI\\VEN_1002&DEV_1586\\0": _DISPLAY_GUID + "\\0000"},
+ )
+ _with_cfgmgr(monkeypatch, fake)
+ answer = ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY)
+ # Enumeration failures return None; individual device failures are skipped.
+ assert answer is None if failing_call in ("size", "list") else answer == set()
+
+
+def test_the_presence_probe_answers_none_when_cfgmgr_cannot_be_loaded(monkeypatch):
+ def _no_library(_name):
+ raise OSError("cfgmgr32.dll not found")
+
+ monkeypatch.setattr(ctypes, "WinDLL", _no_library, raising = False)
+ assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) is None
+
+
+def test_the_presence_probe_answers_none_when_the_walk_raises(monkeypatch):
+ class _Exploding:
+ def __getattr__(self, _name):
+ raise RuntimeError("boom")
+
+ _with_cfgmgr(monkeypatch, _Exploding())
+ assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) is None
+
+
+# ---------------------------------------------------------------------------
+# The probe at Windows' own widths
+# ---------------------------------------------------------------------------
+
+
+class _Utf16Buffer:
+ """create_unicode_buffer over Windows' 2-byte code units.
+
+ Indexing matches ctypes: int index is one character, a slice keeps embedded NULs,
+ .value stops at the first NUL.
+ """
+
+ def __init__(self, length):
+ self._units = [0] * length
+
+ def __len__(self):
+ return len(self._units)
+
+ def __setitem__(self, index, char):
+ self._units[index] = ord(char)
+
+ def __getitem__(self, index):
+ if isinstance(index, slice):
+ return "".join(chr(unit) for unit in self._units[index])
+ return chr(self._units[index])
+
+ @property
+ def value(self):
+ out = []
+ for unit in self._units:
+ if unit == 0:
+ break
+ out.append(chr(unit))
+ return "".join(out)
+
+
+class _Windows32Bit:
+ """DWORD, ULONG and DEVINST are all a 32-bit unsigned, pointers are not."""
+
+ def __init__(self, value = 0):
+ self.value = int(value) & 0xFFFFFFFF
+
+
+class _WindowsPointer:
+ def __init__(self, box):
+ self.box = box
+
+ def __getitem__(self, _index):
+ return self.box.value
+
+ def __setitem__(self, _index, value):
+ self.box.value = value
+
+
+class _WindowsWcharT:
+ """Only ever handed to sizeof, which answers 2 on Windows and 4 here."""
+
+
+class _WindowsCfgMgr(_FakeCfgMgr):
+ """_FakeCfgMgr speaking the units the API documents, not this host's.
+
+ CM_Get_DevNode_Registry_PropertyW sizes in BYTES
+ (_Out_writes_bytes_opt_(*pulLength) PVOID Buffer), twice the element count under a
+ 2-byte wchar_t. Assert ``under_allocated`` from OUTSIDE the probe: it catches
+ Exception around the whole walk and would read a failed assert as a clean skip.
+ """
+
+ under_allocated = False
+
+ def CM_Get_DevNode_Registry_PropertyW(self, devinst, prop, kind, buffer, size_ptr, flags):
+ device_id = list(self._driver_of)[int(devinst.value) - 1]
+ driver = self._driver_of[device_id]
+ wanted = (len(driver) + 1) * 2 if driver else 0
+ if buffer is None:
+ size_ptr[0] = wanted
+ return ilp._CR_SUCCESS
+ if len(buffer) * 2 < wanted:
+ self.under_allocated = True
+ return _CR_FAILURE
+ for index, char in enumerate(driver):
+ buffer[index] = char
+ buffer[len(driver)] = "\0"
+ return self._result("property")
+
+
+def _windows_widths(cfgmgr):
+ """Run the probe as Windows runs it: 2-byte wchar_t, 32-bit scalars.
+
+ Native ctypes makes sizeof(c_wchar) 4 here and 2 on Windows, so the byte-to-element
+ conversion in _windows_present_class_instances is only ever exercised at the runner's
+ width and a green suite says nothing about the arithmetic that ships. The probe
+ imports ctypes inside the function, so swapping sys.modules is enough.
+ """
+ shim = ModuleType("ctypes")
+ wintypes = ModuleType("ctypes.wintypes")
+ wintypes.ULONG = _Windows32Bit
+ wintypes.DWORD = _Windows32Bit
+ shim.wintypes = wintypes
+ shim.c_wchar = _WindowsWcharT
+ shim.sizeof = lambda obj: 2 if obj is _WindowsWcharT else ctypes.sizeof(obj)
+ shim.pointer = _WindowsPointer
+ shim.create_unicode_buffer = _Utf16Buffer
+ shim.WinDLL = lambda _name: cfgmgr
+ saved = sys.modules["ctypes"]
+ sys.modules["ctypes"] = shim
+ try:
+ yield
+ finally:
+ sys.modules["ctypes"] = saved
+
+
+def test_the_driver_property_survives_a_two_byte_wchar():
+ # 43 characters, i.e. 88 bytes on Windows. Divided by this host's 4-byte wchar_t it
+ # allocates 23, the callee refuses the undersized buffer, and the instance is lost.
+ driver = _DISPLAY_GUID + "\\0000"
+ fake = _WindowsCfgMgr(
+ {_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0"]},
+ driver_of = {"PCI\\VEN_1002&DEV_1586\\0": driver},
+ )
+ with _windows_widths(fake):
+ answer = ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY)
+ assert fake.under_allocated is False, "the byte length was read as an element count"
+ assert answer == {"0000"}
+
+
+def test_the_device_id_list_is_sized_in_characters_not_bytes():
+ # BufferLen is a CHARACTER count (_Out_writes_(BufferLen) PZZWSTR), so the size
+ # query's answer passes through unscaled.
+ fake = _WindowsCfgMgr(
+ {_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0", "PCI\\VEN_1002&DEV_7448\\1"]},
+ driver_of = {
+ "PCI\\VEN_1002&DEV_1586\\0": _DISPLAY_GUID + "\\0000",
+ "PCI\\VEN_1002&DEV_7448\\1": _DISPLAY_GUID + "\\0003",
+ },
+ )
+ with _windows_widths(fake):
+ answer = ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY)
+ assert answer == {"0000", "0003"}
+
+
+def test_the_forced_loader_list_reads_neither_the_registry_nor_cfgmgr():
+ # The paths alone would be right even if the override ran after a full device walk.
+ class _CountingWinreg(_FakeIcdWinreg):
+ def __init__(self):
+ super().__init__({}, None)
+ self.reads = 0
+
+ def OpenKey(self, parent, name):
+ self.reads += 1
+ return super().OpenKey(parent, name)
+
+ winreg = _CountingWinreg()
+ fake = _FakeCfgMgr({_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0"]})
+ with pytest.MonkeyPatch.context() as patch:
+ patch.setattr(ilp.sys, "platform", "win32")
+ patch.setitem(sys.modules, "winreg", winreg)
+ patch.setattr(ctypes, "WinDLL", lambda _name: fake, raising = False)
+ patch.setenv("VK_DRIVER_FILES", __file__)
+ assert ilp._amd_vulkan_icd_manifest_paths() == [__file__]
+ assert winreg.reads == 0
+ assert fake.calls == []
+
+
+# ---------------------------------------------------------------------------
+# Registration paths, spelled the way Windows spells them
+# ---------------------------------------------------------------------------
+
+
+class _WindowsPaths:
+ """os as the probe sees it on Windows, over a fixed set of existing files."""
+
+ def __init__(self, existing):
+ self.existing = set(existing)
+ self.pathsep = ";"
+ self.environ = os.environ
+ self.path = SimpleNamespace(
+ isabs = ntpath.isabs,
+ isfile = lambda name: name in self.existing,
+ dirname = ntpath.dirname,
+ join = ntpath.join,
+ )
+
+
+ "value, taken",
+ [
+ (r"C:\Windows\System32\DriverStore\amd-vulkan64.json", True),
+ (r"\\?\C:\Windows\System32\amd-vulkan64.json", True),
+ (r"\\host\share\amd-vulkan64.json", True),
+ ("amd-vulkan64.json", False),
+ (r"..\amd-vulkan64.json", False),
+ (r"FileRepository\amd-vulkan64.json", False),
+ (" ", False),
+ ],
+)
+def test_a_registration_is_taken_only_when_windows_calls_it_absolute(monkeypatch, value, taken):
+ # tmp_path is POSIX, so every other test here runs os.path.isabs as posixpath,
+ # which answers False for "C:\..." and True for "/...". Neither is a real value.
+ monkeypatch.setattr(ilp, "os", _WindowsPaths({value.strip()}))
+ got = ilp._windows_vulkan_driver_value_paths(_FakeIcdWinreg, value, _FakeIcdWinreg.REG_SZ)
+ assert got == ([value.strip()] if taken else [])
+
+
+def test_a_multi_sz_registration_keeps_only_the_absolute_entries_that_exist(monkeypatch):
+ here = r"C:\Windows\System32\amd-vulkan64.json"
+ gone = r"C:\Windows\System32\amdvlk64.json"
+ monkeypatch.setattr(ilp, "os", _WindowsPaths({here}))
+ got = ilp._windows_vulkan_driver_value_paths(
+ _FakeIcdWinreg,
+ [here, gone, r"FileRepository\amd-vulkan64.json", "", None],
+ _FakeIcdWinreg.REG_MULTI_SZ,
+ )
+ assert got == [here]
+
+
+# ---------------------------------------------------------------------------
+# Matching what the loader will actually read
+# ---------------------------------------------------------------------------
+
+
+_DN_HAS_PROBLEM = 0x00000400 # cfg.h status flags
+_DN_NEED_RESTART = 0x00000100 # DN_LIAR, a STATUS bit and not a CM_PROB_ code
+_CM_PROB_NEED_RESTART = 0x0000000E # cfg.h problem codes, which stop at 0x39
+_CM_PROB_DISABLED = 0x00000016
+
+
+ "status, problem",
+ [
+ (_DN_HAS_PROBLEM, _CM_PROB_NEED_RESTART),
+ # DN_NEED_RESTART lives in the status word, and a devnode can raise it without
+ # also setting DN_HAS_PROBLEM. Reading it out of pulProblemNumber instead never
+ # matches (no CM_PROB_ code reaches 0x100) and let this device through.
+ (_DN_NEED_RESTART, 0),
+ (_DN_NEED_RESTART | _DN_HAS_PROBLEM, _CM_PROB_DISABLED),
+ ],
+ ids = ["problem-code", "status-bit-alone", "status-bit-with-other-problem"],
+)
+def test_a_device_pending_reboot_is_not_evidence_of_a_loadable_driver(monkeypatch, status, problem):
+ # Present with its manifest on disk, yet unloadable until the reboot binds it.
+ device = "PCI\\VEN_1002&DEV_1586\\0"
+ fake = _FakeCfgMgr(
+ {_DISPLAY_GUID: [device]},
+ driver_of = {device: _DISPLAY_GUID + "\\0000"},
+ status_of = {device: (status, problem)},
+ )
+ _with_cfgmgr(monkeypatch, fake)
+ assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == set()
+
+
+def test_an_unrelated_device_problem_does_not_hide_a_working_adapter(monkeypatch):
+ # Only the pending-reboot codes disqualify, so a disabled sibling costs nothing.
+ working = "PCI\\VEN_1002&DEV_1586\\0"
+ disabled = "PCI\\VEN_1002&DEV_7448\\1"
+ fake = _FakeCfgMgr(
+ {_DISPLAY_GUID: [working, disabled]},
+ driver_of = {
+ working: _DISPLAY_GUID + "\\0000",
+ disabled: _DISPLAY_GUID + "\\0003",
+ },
+ status_of = {disabled: (_DN_HAS_PROBLEM, _CM_PROB_DISABLED)},
+ )
+ _with_cfgmgr(monkeypatch, fake)
+ assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == {
+ "0000",
+ "0003",
+ }
+
+
+def test_a_device_whose_status_cannot_be_read_is_skipped(monkeypatch):
+ # The loader's own answer: a status it cannot read is not a device it will use.
+ device = "PCI\\VEN_1002&DEV_1586\\0"
+ fake = _FakeCfgMgr(
+ {_DISPLAY_GUID: [device]},
+ fail_at = "status",
+ driver_of = {device: _DISPLAY_GUID + "\\0000"},
+ )
+ _with_cfgmgr(monkeypatch, fake)
+ assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == set()
+
+
+def test_a_device_arriving_mid_enumeration_is_read_on_the_retry(monkeypatch):
+ # A device landing between the size and the read answers CR_BUFFER_SMALL; unretried,
+ # that transient drops the whole scan.
+ settled = "PCI\\VEN_1002&DEV_1586\\0"
+ late = "PCI\\VEN_1002&DEV_7448\\1"
+ fake = _FakeCfgMgr(
+ {_DISPLAY_GUID: [settled]},
+ driver_of = {settled: _DISPLAY_GUID + "\\0000", late: _DISPLAY_GUID + "\\0003"},
+ arriving = [late],
+ )
+ _with_cfgmgr(monkeypatch, fake)
+ assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) == {
+ "0000",
+ "0003",
+ }
+ assert [call[0] for call in fake.calls].count("list") == 2, "the read was retried"
+
+
+def test_a_list_that_never_settles_answers_unknown_rather_than_spinning(monkeypatch):
+ class _AlwaysGrowing(_FakeCfgMgr):
+ def CM_Get_Device_ID_ListW(self, guid, buffer, length, flags):
+ self.calls.append(("list", guid, flags))
+ return ilp._CR_BUFFER_SMALL
+
+ fake = _AlwaysGrowing({_DISPLAY_GUID: ["PCI\\VEN_1002&DEV_1586\\0"]})
+ _with_cfgmgr(monkeypatch, fake)
+ assert ilp._windows_present_class_instances(ilp._WINDOWS_DISPLAY_CLASS_KEY) is None
+ assert [call[0] for call in fake.calls].count("list") == ilp._CM_DEVICE_LIST_ATTEMPTS
+
+
+def test_one_unreadable_instance_does_not_cost_the_rest_of_the_class(monkeypatch, tmp_path):
+ # Per instance, not per class: the integrated part often enumerates first.
+ manifest = _icd(tmp_path / "store" / "amd-vulkan64.json")
+
+ class _HalfBrokenWinreg(_FakeIcdWinreg):
+ def QueryValueEx(self, instance, value_name):
+ if instance.get("explode"):
+ raise RuntimeError("an unexpected registry payload")
+ return _FakeIcdWinreg.QueryValueEx(self, instance, value_name)
+
+ devices = {
+ _DISPLAY_CLASS_KEY: _FakeDeviceClass(
+ {
+ "0000": {"explode": True, ilp._WINDOWS_VULKAN_DRIVER_VALUE: (manifest, 1)},
+ "0001": {ilp._WINDOWS_VULKAN_DRIVER_VALUE: (manifest, 1)},
+ }
+ )
+ }
+ monkeypatch.setattr(ilp, "_windows_present_class_instances", lambda _key: {"0000", "0001"})
+ assert _HalfBrokenWinreg({}, devices) is not None
+ winreg = _HalfBrokenWinreg({}, devices)
+ assert ilp._windows_device_icd_manifest_paths(winreg) == [manifest]
diff --git a/studio/install_llama_prebuilt.py b/studio/install_llama_prebuilt.py
index 0bf5a14c4dd..60885779058 100644
--- a/studio/install_llama_prebuilt.py
+++ b/studio/install_llama_prebuilt.py
@@ -7745,6 +7745,13 @@ def _should_auto_vulkan_for_amd_windows(host: HostInfo, published_repo: str | No
# 64-bit only: WOW6432Node holds 32-bit registrations windows-x64-vulkan cannot load.
_VULKAN_ICD_REGISTRY_KEYS = (r"SOFTWARE\Khronos\Vulkan\Drivers",)
+# AMD drivers can register Vulkan on these device classes without a Khronos entry.
+_WINDOWS_VULKAN_DEVICE_CLASS_KEYS = (
+ _WINDOWS_DISPLAY_CLASS_KEY,
+ r"SYSTEM\CurrentControlSet\Control\Class\{5c4c3332-344d-483c-8739-259e934c9cc8}",
+)
+# Use the 64-bit value; VulkanDriverNameWow is for 32-bit drivers.
+_WINDOWS_VULKAN_DRIVER_VALUE = "VulkanDriverName"
# RADV radeon_icd.x86_64.json, AMDVLK amd_icd64 / amd_pro_icd64 / amdvlk64, Adrenalin
# amd-vulkan64.json. Matched on the basename ("amd" names directories too) with "-" folded
# to "_", without which the ordinary Adrenalin host answered False.
@@ -7836,6 +7843,178 @@ def _vulkan_icd_search_dirs() -> list[Path]:
return unique
+# Device presence constants from cfgmgr32.h and cfg.h.
+_CM_LOCATE_DEVNODE_NORMAL = 0x00000000
+_CM_GETIDLIST_FILTER_PRESENT = 0x00000100
+_CM_GETIDLIST_FILTER_CLASS = 0x00000200
+_CM_DRP_DRIVER = 0x0000000A
+_CR_SUCCESS = 0x00000000
+_CR_BUFFER_SMALL = 0x0000001A
+_DN_HAS_PROBLEM = 0x00000400
+_CM_PROB_NEED_RESTART = 0x0000000E
+# cfg.h calls it DN_LIAR and lists it under "Device Instance status flags", so it is a bit
+# in pulStatus. CM_PROB_ codes stop at 0x39, so it can never appear in pulProblemNumber.
+_DN_NEED_RESTART = 0x00000100
+_CM_DEVICE_LIST_ATTEMPTS = 4
+
+
+def _windows_present_class_instances(class_key_path: str) -> set[str] | None:
+ """Return present class instances ("0000"), or None if enumeration fails.
+
+ CM_DRP_DRIVER maps each device to "{class guid}\\NNNN"; an empty set means none
+ are present. Every devnode filter here is the loader's own
+ (windows_get_device_registry_files), but its traversal is NOT: the loader reaches
+ SoftwareComponents only as children of a present adapter, and the caller
+ enumerates that class directly, so discovery there is wider.
+ """
+ guid = class_key_path.rsplit("\\", 1)[-1]
+ try:
+ import ctypes
+ from ctypes import wintypes
+ cfgmgr = ctypes.WinDLL("cfgmgr32.dll")
+ except Exception:
+ return None
+ try:
+ length = wintypes.ULONG(0)
+ flags = _CM_GETIDLIST_FILTER_CLASS | _CM_GETIDLIST_FILTER_PRESENT
+ # A device arriving between the two calls outgrows the buffer and the API refuses
+ # rather than truncating; unretried, that transient drops the whole scan.
+ for _attempt in range(_CM_DEVICE_LIST_ATTEMPTS):
+ if (
+ cfgmgr.CM_Get_Device_ID_List_SizeW(ctypes.pointer(length), guid, flags)
+ != _CR_SUCCESS
+ ):
+ return None
+ buffer = ctypes.create_unicode_buffer(length.value)
+ listed = cfgmgr.CM_Get_Device_ID_ListW(guid, buffer, length.value, flags)
+ if listed == _CR_SUCCESS:
+ break
+ if listed != _CR_BUFFER_SMALL:
+ return None
+ else:
+ return None
+ # One double-NUL-terminated block of device ids.
+ device_ids = [entry for entry in buffer[: length.value].split("\0") if entry]
+ instances: set[str] = set()
+ for device_id in device_ids:
+ devinst = wintypes.DWORD(0)
+ if (
+ cfgmgr.CM_Locate_DevNodeW(
+ ctypes.pointer(devinst), device_id, _CM_LOCATE_DEVNODE_NORMAL
+ )
+ != _CR_SUCCESS
+ ):
+ continue
+ if not _windows_devnode_is_usable(cfgmgr, ctypes, wintypes, devinst):
+ continue
+ size = wintypes.ULONG(0)
+ # First call sizes the value; a device with no driver bound has none.
+ cfgmgr.CM_Get_DevNode_Registry_PropertyW(
+ devinst, _CM_DRP_DRIVER, None, None, ctypes.pointer(size), 0
+ )
+ if not size.value:
+ continue
+ value = ctypes.create_unicode_buffer(size.value // ctypes.sizeof(ctypes.c_wchar) + 1)
+ if (
+ cfgmgr.CM_Get_DevNode_Registry_PropertyW(
+ devinst, _CM_DRP_DRIVER, None, value, ctypes.pointer(size), 0
+ )
+ != _CR_SUCCESS
+ ):
+ continue
+ driver = (value.value or "").strip()
+ if "\\" in driver:
+ instances.add(driver.rsplit("\\", 1)[-1])
+ return instances
+ except Exception:
+ return None
+
+
+def _windows_devnode_is_usable(cfgmgr: Any, ctypes: Any, wintypes: Any, devinst: Any) -> bool:
+ """Whether this devnode is one the Vulkan loader would read, not merely present.
+
+ A driver update registers VulkanDriverName and drops its manifest before the reboot
+ that binds it: until then the adapter is present, the file is on disk, and the driver
+ cannot load. Counting that window hands the host a Vulkan bundle and lets llama-server
+ fall back to CPU, which is what _amd_vulkan_icd_present exists to prevent. An
+ unreadable status is skipped too, since guessing wrong replaces a working ROCm install.
+ """
+ status = wintypes.ULONG(0)
+ problem = wintypes.ULONG(0)
+ if (
+ cfgmgr.CM_Get_DevNode_Status(ctypes.pointer(status), ctypes.pointer(problem), devinst, 0)
+ != _CR_SUCCESS
+ ):
+ return False
+ # Read before the problem word and independently of DN_HAS_PROBLEM: this one is a status
+ # bit, and a devnode that reports it without also raising a problem still needs the reboot.
+ if status.value & _DN_NEED_RESTART:
+ return False
+ if not status.value & _DN_HAS_PROBLEM:
+ return True
+ return problem.value != _CM_PROB_NEED_RESTART
+
+
+def _windows_device_icd_manifest_paths(winreg: Any) -> list[str]:
+ """Read Vulkan manifests from present adapters and SoftwareComponents.
+
+ A removed device keeps its registration AND its files, so presence decides, and a
+ class whose presence is unknown is skipped rather than trusted.
+ """
+ paths: list[str] = []
+ for class_key_path in _WINDOWS_VULKAN_DEVICE_CLASS_KEYS:
+ present = _windows_present_class_instances(class_key_path)
+ if present is None:
+ continue
+ try:
+ with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, class_key_path) as class_key:
+ for index in range(winreg.QueryInfoKey(class_key)[0]):
+ try:
+ name = winreg.EnumKey(class_key, index)
+ # Skip non-instance keys such as the restricted "Properties".
+ if not name.isdigit() or name not in present:
+ continue
+ with winreg.OpenKey(class_key, name) as device_key:
+ value, kind = winreg.QueryValueEx(
+ device_key, _WINDOWS_VULKAN_DRIVER_VALUE
+ )
+ paths.extend(_windows_vulkan_driver_value_paths(winreg, value, kind))
+ # Per instance: the integrated part often enumerates first.
+ except Exception:
+ continue
+ except OSError:
+ continue
+ except Exception:
+ # Discovery failures must not abort installation.
+ continue
+ return paths
+
+
+def _windows_vulkan_driver_value_paths(winreg: Any, value: Any, kind: Any) -> list[str]:
+ """Existing absolute manifest paths from REG_SZ or REG_MULTI_SZ. Relative ones need
+ the device's DriverStore directory, which is not reachable here, so they are skipped.
+ """
+ if kind == winreg.REG_SZ:
+ entries = [value]
+ elif kind == winreg.REG_MULTI_SZ:
+ entries = list(value or [])
+ else:
+ return []
+ found = []
+ for entry in entries:
+ if not isinstance(entry, str) or not entry.strip():
+ continue
+ entry = entry.strip()
+ if not os.path.isabs(entry):
+ continue
+ try:
+ if os.path.isfile(entry):
+ found.append(entry)
+ except OSError:
+ continue
+ return found
+
+
def _amd_vulkan_icd_manifest_paths() -> list[str]:
"""Vulkan ICD manifests this host has registered, by loader search order.
@@ -7863,7 +8042,7 @@ def _amd_vulkan_icd_manifest_paths() -> list[str]:
import winreg
except ImportError:
return []
- paths: list[str] = []
+ paths: list[str] = _windows_device_icd_manifest_paths(winreg)
for key_path in _VULKAN_ICD_REGISTRY_KEYS:
try:
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_path) as key:
@@ -7884,7 +8063,8 @@ def _amd_vulkan_icd_manifest_paths() -> list[str]:
paths.append(name)
except OSError:
continue
- return paths
+ # The same manifest may appear in several registrations.
+ return list(dict.fromkeys(paths))
paths = []
for directory in _vulkan_icd_search_dirs():
try: