Skip to content

A GPU task manager for Windows, Linux, and WSL: per-process VRAM, GPU activity, and LLM model/KV-cache insights (Windows AMD and NVIDIA).

Python

Count one GGUF once however its path is spelled

Scott Hanselman001d5c639cd8
diff --git a/src/gputaskman/llm.py b/src/gputaskman/llm.py
index 448c4d7..0ef2a07 100644
--- a/src/gputaskman/llm.py
+++ b/src/gputaskman/llm.py
@@ -21,7 +21,7 @@ import urllib.request
from dataclasses import dataclass, field
from .gguf import GgufInfo, is_gguf, read_gguf
-from .procinfo import MappedModel, ProcDetail, make_model
+from .procinfo import MappedModel, ProcDetail, file_key, make_model
MiB = 1 << 20
KV_TYPE_BYTES = {"f32": 4, "f16": 2, "bf16": 2, "q8_0": 34 / 32, "q4_0": 18 / 32, "q4_1": 20 / 32,
@@ -383,8 +383,8 @@ def _merge_api(files: list[MappedModel], server: ServerInfo | None, host: Server
"""Mark files a runtime API also reports; return the API entries that matched this process."""
matched: list[dict] = []
if server and server.kind == "llama-server" and server.model_path:
- key = os.path.normcase(os.path.abspath(server.model_path))
- hit = next((f for f in files if os.path.normcase(os.path.abspath(f.path)) == key), None)
+ key = file_key(server.model_path)
+ hit = next((f for f in files if file_key(f.path) == key), None)
if hit:
hit.add_source("api")
elif os.path.isfile(server.model_path): # loaded without mmap or via the router: API-only evidence
diff --git a/src/gputaskman/procinfo.py b/src/gputaskman/procinfo.py
index e179ab6..53542e6 100644
--- a/src/gputaskman/procinfo.py
+++ b/src/gputaskman/procinfo.py
@@ -148,6 +148,16 @@ def _match_runtimes(dll_names: set[str]) -> list[str]:
return [tag for tag, rx in _RUNTIME_RX if any(rx.match(d) for d in dll_names)]
+def file_key(path: str) -> tuple[int, int] | str:
+ """Identity of the file itself, so one model reached by two spellings (extended-length prefix, link,
+ case) is one model. Falls back to the normalized path when the file can't be read or has no file id."""
+ try:
+ st = os.stat(path)
+ except (OSError, ValueError):
+ return os.path.normcase(os.path.abspath(path))
+ return (st.st_dev, st.st_ino) if st.st_ino else os.path.normcase(os.path.abspath(path))
+
+
def _models_from_cmdline(cmd: list[str]) -> list[str]:
paths = []
flags = {"-m", "--model", "--mmproj", "-md", "--model-draft", "--lora"}
@@ -396,12 +406,13 @@ class ProcInspector:
continue
store = store_name(path)
if _is_model_file(ext, size, gg, store):
- models[path.lower()] = make_model(path, size, m.rss, gg, "mapped", store)
+ models[file_key(path)] = make_model(path, size, m.rss, gg, "mapped", store)
for path in _models_from_cmdline(d.cmdline):
- if path.lower() in models:
- models[path.lower()].add_source("cmdline")
+ key = file_key(path)
+ if key in models:
+ models[key].add_source("cmdline")
else:
- models[path.lower()] = make_model(path, os.path.getsize(path), 0, is_gguf(path), "cmdline")
+ models[key] = make_model(path, os.path.getsize(path), 0, is_gguf(path), "cmdline")
d.runtimes = _match_runtimes(dlls)
d.models = sorted(models.values(), key=lambda m: -m.size)
ai = any(r in AI_RUNTIMES for r in d.runtimes)
diff --git a/tests/test_core.py b/tests/test_core.py
index 95d7e46..c356b3e 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -34,7 +34,7 @@ from gputaskman.llm import (LlmView, ServerInfo, ServerProber, _get, api_key, bu
from gputaskman.nvml import NvDevice, Nvml
from gputaskman.pdh import AdapterGpu, GpuCounters, ProcGpu
from gputaskman.procinfo import (AI_KINDS, MappedModel, ProcDetail, ProcInspector, _is_model_file, _match_runtimes,
- _models_from_cmdline, classify, installed_models, make_model)
+ _models_from_cmdline, classify, file_key, installed_models, make_model)
from gputaskman.report import to_dict, to_text
from gputaskman.stores import store_name
from tools.capture_screenshots import for_publication
@@ -894,6 +894,55 @@ class IdentityTests(unittest.TestCase):
self.assertEqual(len(detail.models), 1)
self.assertEqual(detail.models[0].sources, ["cmdline"])
+ def assert_one_model_under_two_spellings(self, mapped_path, launch_path):
+ detail = ProcDetail(6, "llama-server", cmdline=["server", "-m", launch_path])
+ with patch("gputaskman.procinfo._memory_maps", return_value=[SimpleNamespace(path=mapped_path, rss=0)]):
+ ProcInspector().scan(detail)
+ self.assertEqual([m.sources for m in detail.models], [["mapped", "cmdline"]])
+ server = ServerInfo("llama-server", "http://127.0.0.1:1", model_path=launch_path)
+ v = build_view(detail, 4 * MiB, 0, server)
+ self.assertEqual([f.sources for f in v.files + v.other_files], [["mapped", "cmdline", "api"]])
+
+ @unittest.skipUnless(os.name == "nt", "\\\\?\\ paths are Windows syntax")
+ def test_extended_length_launch_path_does_not_duplicate_the_mapped_model(self):
+ with tempfile.TemporaryDirectory() as directory:
+ path = str(Path(directory) / "model.gguf")
+ Path(path).write_bytes(bytes(4 * MiB))
+ self.assert_one_model_under_two_spellings(path, "\\\\?\\" + path)
+
+ def test_a_second_name_for_the_same_file_does_not_duplicate_the_model(self):
+ with tempfile.TemporaryDirectory() as directory:
+ path, alias = str(Path(directory) / "model.gguf"), str(Path(directory) / "alias.gguf")
+ Path(path).write_bytes(bytes(4 * MiB))
+ try:
+ os.link(path, alias)
+ except OSError as error:
+ self.skipTest(f"hard links are unavailable here: {error}")
+ self.assert_one_model_under_two_spellings(path, alias)
+
+ def test_different_files_are_not_merged(self):
+ with tempfile.TemporaryDirectory() as directory:
+ first, second = str(Path(directory) / "a.gguf"), str(Path(directory) / "b.gguf")
+ for path in (first, second):
+ Path(path).write_bytes(bytes(4 * MiB)) # same size and bytes, still two files
+ detail = ProcDetail(6, "llama-server", cmdline=["server", "-m", second])
+ with patch("gputaskman.procinfo._memory_maps", return_value=[SimpleNamespace(path=first, rss=0)]):
+ ProcInspector().scan(detail)
+ self.assertEqual(sorted(m.sources for m in detail.models), [["cmdline"], ["mapped"]])
+
+ def test_file_key_is_the_file_and_falls_back_to_the_path(self):
+ with tempfile.TemporaryDirectory() as directory:
+ a, b = os.path.join(directory, "a.gguf"), os.path.join(directory, "b.gguf")
+ for path in (a, b):
+ Path(path).write_bytes(b"x")
+ self.assertEqual(file_key(a), file_key(os.path.join(directory, ".", "a.gguf")))
+ self.assertNotEqual(file_key(a), file_key(b))
+ missing = os.path.join(directory, "missing.gguf")
+ self.assertEqual(file_key(missing), os.path.normcase(os.path.abspath(missing))) # unreadable: the path
+ self.assertEqual(file_key("model\0.gguf"), os.path.normcase(os.path.abspath("model\0.gguf"))) # odd API data
+ with patch("gputaskman.procinfo.os.stat", return_value=SimpleNamespace(st_ino=0, st_dev=7)):
+ self.assertEqual(file_key(a), os.path.normcase(os.path.abspath(a))) # no file id: the path
+
def test_ambiguous_extensions_need_size_or_store(self):
self.assertFalse(_is_model_file(".bin", 10 * MiB, False, None)) # e.g. a Start menu tile cache
self.assertTrue(_is_model_file(".bin", 10 * MiB, False, "org/repo"))