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 diffusion prequant cache: do not count cgroup shmem as reclaimable (#16154)

Daniel Hane557a7d1f513
diff --git a/studio/backend/core/inference/diffusion_prequant_cache.py b/studio/backend/core/inference/diffusion_prequant_cache.py
index a74e50d8208..abea80f1eeb 100644
--- a/studio/backend/core/inference/diffusion_prequant_cache.py
+++ b/studio/backend/core/inference/diffusion_prequant_cache.py
@@ -1496,7 +1496,9 @@ def _cgroup_memory_bytes() -> tuple[Optional[int], Optional[int]]:
for ln in (node / "memory.stat").read_text(encoding = "utf-8").splitlines()
if ln.strip()
)
- avail = max(0, limit - used + int(stat.get("file", 0)))
+ # ``file`` counts tmpfs / shared memory too (cgroup-v2 docs); only the rest is reclaimable page cache.
+ reclaimable = max(0, int(stat.get("file", 0)) - int(stat.get("shmem", 0)))
+ avail = max(0, limit - used + reclaimable)
if best[1] is None or avail < best[1]:
best = (limit, avail)
except (OSError, ValueError):
diff --git a/studio/backend/tests/test_diffusion_prequant_cgroup_ram.py b/studio/backend/tests/test_diffusion_prequant_cgroup_ram.py
new file mode 100644
index 00000000000..f3d3836ffce
--- /dev/null
+++ b/studio/backend/tests/test_diffusion_prequant_cgroup_ram.py
@@ -0,0 +1,47 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from __future__ import annotations
+
+import builtins
+import io
+import pathlib
+
+from core.inference import diffusion_prequant_cache as C
+
+GiB = 1 << 30
+
+
+def _fake_cgroup(monkeypatch, tmp_path, *, limit, used, file, shmem):
+ node = tmp_path / "sys" / "fs" / "cgroup" / "studio.slice"
+ node.mkdir(parents = True)
+ (node / "memory.max").write_text(f"{limit}\n")
+ (node / "memory.current").write_text(f"{used}\n")
+ (node / "memory.stat").write_text(f"anon {used - file}\nfile {file}\nshmem {shmem}\n")
+ real_path, real_open = pathlib.Path, builtins.open
+
+ def _path(p, *rest):
+ p = str(p)
+ if p.startswith("/sys/fs/cgroup"):
+ return real_path(str(tmp_path / "sys") + p[len("/sys") :], *rest)
+ return real_path(p, *rest)
+
+ def _open(p, *a, **k):
+ if str(p) == "/proc/self/cgroup":
+ return io.StringIO("0::/studio.slice\n")
+ return real_open(p, *a, **k)
+
+ monkeypatch.setattr(C, "Path", _path)
+ monkeypatch.setattr(C, "open", _open, raising = False)
+
+
+def test_shared_memory_is_not_counted_as_available(monkeypatch, tmp_path):
+ _fake_cgroup(monkeypatch, tmp_path, limit = 16 * GiB, used = 15 * GiB, file = 8 * GiB, shmem = 8 * GiB)
+ limit, avail = C._cgroup_memory_bytes()
+ assert limit == 16 * GiB
+ assert avail == 1 * GiB
+
+
+def test_clean_page_cache_is_still_credited(monkeypatch, tmp_path):
+ _fake_cgroup(monkeypatch, tmp_path, limit = 16 * GiB, used = 15 * GiB, file = 8 * GiB, shmem = 2 * GiB)
+ assert C._cgroup_memory_bytes() == (16 * GiB, 7 * GiB)