Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

OOM guard: Changes following PR review. Removed workspace requirement from dedicated GPUs/VRAM. Added CPU tests.

Resynthaec262e42644
diff --git a/doc/env_vars.md b/doc/env_vars.md
index 79766b9..64db030 100644
--- a/doc/env_vars.md
+++ b/doc/env_vars.md
@@ -741,9 +741,10 @@ exactly at the floor.
### `KYOJIN_OOM_GUARD_WORKSPACE_GIB` (default: `2.0`)
-Estimated transient prefill workspace added to every device footprint. This is model/context
-dependent; raise it for a model with a larger prefill scratch area. It is not a reserve: the
-device cap remains its own reported free memory.
+Estimated transient prefill workspace added to unified (integrated-memory) device footprints;
+dedicated devices use 0. The default 2.0 GiB is a planning estimate, not a universal measurement.
+This is model/context dependent; raise it for a model with a larger prefill scratch area. It is
+not a reserve: the device cap remains its own reported free memory.
### `EXL3_LOAD_ARENA` (default: `1`)
diff --git a/doc/split.md b/doc/split.md
index 9cc0267..43e0b32 100644
--- a/doc/split.md
+++ b/doc/split.md
@@ -90,18 +90,25 @@ Text endcaps are selected by a class/key allowlist, not by per-model placement i
### Pre-flight guard
The guard estimates each device's footprint from actual bytes: weights, KV/recurrent cache,
-the part0 load slab and a prefill workspace. The device cap is the device itself:
+the part0 load slab and, on unified-memory devices only, a prefill workspace. The device cap is
+the device itself:
```text
device_cap = min(mem_get_info().free, mem_get_info().total)
host_cap = MemAvailable - (host_floor + preflight_margin)
```
+Dedicated devices plan without a workspace: `auto` refuses only a hard overcommit of
+weights+cache+slab, and does not refuse a load that merely leaves less than the workspace
+estimate. A dedicated prefill allocation failure is a process-level error. Unified devices add
+the configurable workspace estimate and must also satisfy the host cap; only the unified
+footprints are summed against the host.
+
There is **no GTT or VRAM reserve**. A dedicated-VRAM allocation failure is a process-level
error; the host floor is what prevents a unified-memory box from starving. The only
tunables are `MEMGUARD_FLOOR` (default 8), the planning margin (default 1)
-and the prefill workspace (default 2.0 GiB). The host floor is shared with the supervisor;
-the margin keeps the planner above the supervisor's kill threshold.
+and the unified prefill workspace (default 2.0 GiB). The host floor is shared with the
+supervisor; the margin keeps the planner above the supervisor's kill threshold.
`--oom-guard auto` refuses before allocating weights, cache or slabs, and prints a per-device
table plus the chosen N/context and the largest N/context that fits. `off` disables it. The
diff --git a/exllamav3/memory_guard.py b/exllamav3/memory_guard.py
index af2bec7..1c4649a 100644
--- a/exllamav3/memory_guard.py
+++ b/exllamav3/memory_guard.py
@@ -9,12 +9,11 @@ processes. A supervisor enforces the same floor from outside the process; the p
adds a small planning margin so it does not deliberately plan at the kill threshold.
Models are loaded from actual measured bytes: module weights, cache/recurrent bytes, the
-part0 load slab and a prefill workspace. A dedicated-VRAM OOM is left to the process
-(and is normally a recoverable allocation error); the host floor is what prevents the
-unified-memory box from starving.
+part0 load slab and, on unified devices only, a prefill workspace. A dedicated-VRAM OOM is
+left to the process (and is normally a recoverable allocation error); the host floor is what
+prevents the unified-memory box from starving.
"""
-import glob
import os
from collections.abc import Callable, Iterable
from dataclasses import dataclass, field
@@ -43,7 +42,12 @@ class OOMGuardError(RuntimeError):
@dataclass
class GuardPolicy:
- """The only tunables: host reserve, planner margin and the prefill workspace."""
+ """The only tunables: host reserve, planner margin and the prefill workspace.
+
+ ``workspace_gib`` is the unified-memory prefill planning estimate and applies
+ only to devices the runtime reports as integrated; dedicated devices plan with
+ no workspace.
+ """
mode: str = "auto"
host_floor_gib: float = DEFAULT_HOST_FLOOR_GIB
@@ -115,11 +119,6 @@ def host_available_bytes() -> int | None:
return int(value) if value is not None else None
-def host_total_bytes() -> int | None:
- value = meminfo().get("MemTotal")
- return int(value) if value is not None else None
-
-
def device_total_bytes(index: int) -> int | None:
try:
return int(_torch().cuda.get_device_properties(int(index)).total_memory)
@@ -135,28 +134,18 @@ def device_free_bytes(index: int) -> int | None:
return None
-def is_unified_device(index: int, arch: str | None = None, total: int | None = None) -> bool:
- """Positive unified/GTT detection, independent of a hardcoded device index."""
- arch_key = (arch or "").strip().lower()
- if arch_key.startswith("gfx115"):
- return True
- props = None
+def is_unified_device(index: int) -> bool:
+ """True only when the runtime reports unified/integrated memory for the device.
+
+ ROCm exposes the flag as ``is_integrated``; some runtimes use ``integrated``.
+ An unavailable property or a failed probe means dedicated, which loses the
+ host-pool check rather than guessing from an arch, name or size ratio.
+ """
try:
props = _torch().cuda.get_device_properties(int(index))
- except Exception: # noqa: BLE001, S110 - unavailable probe
- pass
- if props is not None:
- if getattr(props, "is_integrated", False) or getattr(props, "integrated", False):
- return True
- name = (getattr(props, "name", "") or "").lower()
- if "gb10" in name or "dgx spark" in name:
- return True
- total = device_total_bytes(index) if total is None else total
- host = host_total_bytes()
- # A dedicated GPU can be more than half of host RAM, so require a near-host total
- # together with an AMD GTT pool before treating it as unified.
- return (total is not None and host is not None and total >= 0.9 * host
- and bool(glob.glob("/sys/class/drm/card*/device/mem_info_gtt_total")))
+ except Exception: # noqa: BLE001 - unavailable probe
+ return False
+ return bool(getattr(props, "is_integrated", False) or getattr(props, "integrated", False))
# ---------------------------------------------------------------------------------------------------------
@@ -174,7 +163,7 @@ class DeviceFootprint:
weights_bytes: int = 0
cache_bytes: int = 0
slab_bytes: int = 0
- workspace_bytes: int = 0
+ workspace_bytes: int = 0 # unified devices only; dedicated devices plan 0
@property
def need_bytes(self) -> int:
@@ -303,13 +292,14 @@ class OOMGuard:
total_bytes = device_total_bytes(index)
if free_bytes is None:
free_bytes = device_free_bytes(index)
+ unified = is_unified_device(index)
return DeviceFootprint(
index = index, arch = arch, name = name,
- unified = is_unified_device(index, arch, total_bytes),
+ unified = unified,
total_bytes = total_bytes, free_bytes = free_bytes,
weights_bytes = int(weights_bytes), cache_bytes = int(cache_bytes),
slab_bytes = int(reserve_bytes),
- workspace_bytes = int(self.policy.workspace_gib * GIB),
+ workspace_bytes = int(self.policy.workspace_gib * GIB) if unified else 0,
)
def evaluate(self, footprints: Iterable[DeviceFootprint],
diff --git a/tests/test_memory_guard_cpu.py b/tests/test_memory_guard_cpu.py
index 83d3ca1..975f715 100644
--- a/tests/test_memory_guard_cpu.py
+++ b/tests/test_memory_guard_cpu.py
@@ -144,19 +144,85 @@ def test_single_device_uses_the_automatic_policy(mg, monkeypatch):
monkeypatch.setattr(mg, "device_total_bytes", lambda _i: 118 * GIB)
monkeypatch.setattr(mg, "device_free_bytes", lambda _i: 118 * GIB)
monkeypatch.setattr(mg, "host_available_bytes", lambda: 120 * GIB)
- monkeypatch.setattr(mg, "is_unified_device", lambda *_a, **_k: True)
+ monkeypatch.setattr(mg, "is_unified_device", lambda _i: True)
# Device fits, host planning capacity (112) does not.
with pytest.raises(mg.OOMGuardError, match="refused the load"):
mg.check_single_device(116 * GIB, 0, 0, logger=lambda *_a: None)
-def test_unified_detection_does_not_flag_a_large_dedicated_gpu(mg, monkeypatch):
- monkeypatch.setattr(mg, "device_total_bytes", lambda _i: 80 * GIB)
- monkeypatch.setattr(mg, "host_total_bytes", lambda: 128 * GIB)
- monkeypatch.setattr(mg, "_torch", lambda: SimpleNamespace(cuda=SimpleNamespace(
- get_device_properties=lambda _i: (_ for _ in ()).throw(RuntimeError("no gpu")))))
- assert mg.is_unified_device(0, "gfx1201", 80 * GIB) is False
- assert mg.is_unified_device(0, "gfx1151", 80 * GIB) is True
+def test_unified_detection_uses_only_the_integrated_property(mg, monkeypatch):
+ # fake props -> expected classification. A runtime without the property is
+ # dedicated, including a large AMD dGPU whose total is close to host RAM.
+ cases = [
+ (SimpleNamespace(name = "AMD Radeon 8060S", total_memory = 114 * GIB,
+ is_integrated = 1), True),
+ (SimpleNamespace(name = "AMD Radeon Graphics", total_memory = 112 * GIB,
+ integrated = True), True),
+ (SimpleNamespace(name = "NVIDIA GeForce RTX 3080", total_memory = 10 * GIB), False),
+ (SimpleNamespace(name = "NVIDIA GeForce RTX 3080 Ti Laptop GPU",
+ total_memory = 16 * GIB), False),
+ (SimpleNamespace(name = "NVIDIA GeForce RTX 3090", total_memory = 24 * GIB), False),
+ (SimpleNamespace(name = "AMD Radeon RX 9070 XT", total_memory = 118 * GIB), False),
+ ]
+ for props, expected in cases:
+ monkeypatch.setattr(mg, "_torch", lambda props = props: SimpleNamespace(cuda = SimpleNamespace(
+ get_device_properties = lambda _i, props = props: props)))
+ assert mg.is_unified_device(0) is expected
+
+ # A failed probe means dedicated, not a guess.
+ monkeypatch.setattr(mg, "_torch", lambda: SimpleNamespace(cuda = SimpleNamespace(
+ get_device_properties = lambda _i: (_ for _ in ()).throw(RuntimeError("no gpu")))))
+ assert mg.is_unified_device(0) is False
+
+
+def test_dedicated_footprint_has_no_workspace_and_only_the_hard_fit_cap(mg, monkeypatch):
+ monkeypatch.setattr(mg, "is_unified_device", lambda _i: False)
+ guard = _guard(mg)
+
+ fp = guard.footprint(0, total_bytes = 16 * GIB, free_bytes = 16 * GIB,
+ weights_bytes = int(15.5 * GIB))
+ assert fp.unified is False and fp.workspace_bytes == 0
+ # Fits by 0.5 GiB: the old unconditional 2 GiB workspace would have refused it.
+ assert fp.need_bytes <= fp.capacity_bytes() < fp.need_bytes + 2 * GIB
+ report = guard.check([fp], host_available = 1)
+ assert report.ok and report.host_result is None
+
+ # Hard overcommit is still refused under auto.
+ over = guard.footprint(0, total_bytes = 16 * GIB, free_bytes = 16 * GIB,
+ weights_bytes = 17 * GIB)
+ with pytest.raises(mg.OOMGuardError, match = "deficit"):
+ guard.check([over], host_available = 1)
+
+ # A model resident on the card lowers free; the cap follows it.
+ resident = guard.footprint(0, total_bytes = 16 * GIB, free_bytes = 14 * GIB,
+ weights_bytes = 15 * GIB)
+ with pytest.raises(mg.OOMGuardError, match = "deficit"):
+ guard.check([resident], host_available = 1)
+
+
+def test_unified_footprint_adds_the_workspace_and_plans_against_host(mg, monkeypatch):
+ monkeypatch.setattr(mg, "is_unified_device", lambda _i: True)
+ guard = _guard(mg)
+
+ fp = guard.footprint(0, total_bytes = 118 * GIB, free_bytes = 118 * GIB,
+ weights_bytes = 106 * GIB)
+ assert fp.unified is True and fp.workspace_bytes == 2 * GIB
+ # Device fits (108/118); host capacity is 120 - (7 + 1) = 112.
+ report = guard.check([fp], host_available = 120 * GIB)
+ assert report.ok and report.host_result is not None
+
+ # A resident host process lowers MemAvailable below the plan.
+ with pytest.raises(mg.OOMGuardError, match = "deficit"):
+ guard.check([fp], host_available = 109 * GIB)
+
+ # The workspace estimate is env-tunable for unified devices.
+ for key in ("KYOJIN_OOM_GUARD", "MEMGUARD_FLOOR",
+ "KYOJIN_OOM_GUARD_PREFLIGHT_MARGIN_GIB", "KYOJIN_OOM_GUARD_WORKSPACE_GIB"):
+ monkeypatch.delenv(key, raising = False)
+ monkeypatch.setenv("KYOJIN_OOM_GUARD_WORKSPACE_GIB", "5")
+ fp = mg.OOMGuard(mg.GuardPolicy.from_env(), logger = lambda *_a: None).footprint(
+ 0, total_bytes = 118 * GIB, free_bytes = 118 * GIB, weights_bytes = 100 * GIB)
+ assert fp.workspace_bytes == 5 * GIB
def test_supervisor_host_floor_is_env_tunable(guard_mod, monkeypatch):
diff --git a/tests/test_split_plan_cpu.py b/tests/test_split_plan_cpu.py
index d2a4e56..a586b5e 100644
--- a/tests/test_split_plan_cpu.py
+++ b/tests/test_split_plan_cpu.py
@@ -744,8 +744,7 @@ def _fake_guard_probe(dm, monkeypatch, *, mg, part0_total=16 * 1024 ** 3,
lambda i: part0_total if i == 1 else part1_total)
monkeypatch.setattr(dm, "device_free_bytes",
lambda i: part0_total if i == 1 else part1_total)
- monkeypatch.setattr(mg, "is_unified_device",
- lambda i, arch=None, total=None: i == 0)
+ monkeypatch.setattr(mg, "is_unified_device", lambda i: i == 0)
monkeypatch.setattr(dm, "host_available_bytes", lambda: host_available)
@@ -764,6 +763,12 @@ def test_split_plan_guard_passes_or_refuses_before_allocation(split_modules, mon
plan.add("text", small, config = small_cfg)
plan.build()
assert plan.guard_report is not None and plan.guard_report.ok
+ footprints = {r.footprint.index: r.footprint for r in plan.guard_report.device_results}
+ # cuda:0 is the unified part1: configured workspace plus the host check.
+ assert footprints[0].unified and footprints[0].workspace_bytes == 2 * gib
+ # cuda:1 is a dedicated part0: no workspace, only the hard-fit device cap.
+ assert not footprints[1].unified and footprints[1].workspace_bytes == 0
+ assert plan.guard_report.host_result is not None
huge = FakeModel([FakeModule(f"layer.{i}", layer_idx = i) for i in range(4)])
huge_cfg = FakeConfig({f"layer.{i}": 40 * gib for i in range(4)})
@@ -798,7 +803,7 @@ def test_single_device_guard_includes_extra_components(split_modules, monkeypatc
monkeypatch.setattr(mg, "device_total_bytes", lambda _i: 118 * gib)
monkeypatch.setattr(mg, "device_free_bytes", lambda _i: 118 * gib)
monkeypatch.setattr(mg, "host_available_bytes", lambda: 120 * gib)
- monkeypatch.setattr(mg, "is_unified_device", lambda *_a, **_k: True)
+ monkeypatch.setattr(mg, "is_unified_device", lambda _i: True)
target = FakeModel([FakeModule("layer.0", layer_idx = 0)])
target_cfg = FakeConfig({"layer.0": 1 * gib})