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 sd.cpp: keep sd.cpp's all-CPU thread count on small SMT hosts nothing restricts (#14847)

Daniel Han0fbe0522b061
diff --git a/studio/backend/core/inference/sd_cpp_backend.py b/studio/backend/core/inference/sd_cpp_backend.py
index 8646f4ece5e..f7a56ef94d1 100644
--- a/studio/backend/core/inference/sd_cpp_backend.py
+++ b/studio/backend/core/inference/sd_cpp_backend.py
@@ -409,6 +409,23 @@ def _sd_cpp_default_threads(cpu: Optional[int]) -> Optional[int]:
return cpu if cpu <= 4 else cpu // 2
+def _allowed_cpus() -> Optional[int]:
+ """CPUs in this process's affinity mask, or None when unreadable."""
+ try:
+ return len(os.sched_getaffinity(0)) or None
+ except (AttributeError, OSError):
+ return None
+
+
+def _cpus_restricted(cpu: Optional[int]) -> bool:
+ """True when a cpuset / taskset or a cgroup quota leaves this process fewer CPUs than the host has."""
+ if not cpu:
+ return False
+ allowed = _allowed_cpus()
+ limit = _cgroup_cpu_limit()
+ return (allowed is not None and allowed < cpu) or (limit is not None and limit < cpu)
+
+
def _explicit_threads() -> Optional[int]:
"""``UNSLOTH_CPU_THREADS``, the macOS fast cores, or (Linux) the thread count above whenever it differs from what
sd.cpp would pick by itself; None keeps sd.cpp's default. sd.cpp builds before the fork's topology fix count half
@@ -423,9 +440,16 @@ def _explicit_threads() -> Optional[int]:
if performance_cores:
return performance_cores
if sys.platform.startswith("linux"):
+ cpu = os.cpu_count()
threads = _default_threads()
- if threads != _sd_cpp_default_threads(os.cpu_count()):
- return threads
+ sd_default = _sd_cpp_default_threads(cpu)
+ if threads == sd_default:
+ return None
+ # Fewer threads than sd.cpp's all-CPU default at 4 CPUs or fewer, only because of SMT: the extra siblings
+ # measured faster (2 vCPU / 1 core, 4 vCPU / 2 cores), so keep sd.cpp's count unless something restricts us.
+ if sd_default is not None and threads < sd_default and not _cpus_restricted(cpu):
+ return None
+ return threads
return None
diff --git a/studio/backend/tests/test_sd_cpp_cpu_defaults.py b/studio/backend/tests/test_sd_cpp_cpu_defaults.py
index a88ea83b02e..43e8bfa6da3 100644
--- a/studio/backend/tests/test_sd_cpp_cpu_defaults.py
+++ b/studio/backend/tests/test_sd_cpp_cpu_defaults.py
@@ -62,12 +62,14 @@ def _host(
limit = None,
platform = "linux",
physical = None,
+ allowed = None,
):
monkeypatch.delenv("UNSLOTH_CPU_THREADS", raising = False)
monkeypatch.setattr(bk.sys, "platform", platform)
monkeypatch.setattr(bk.os, "cpu_count", lambda: cpus)
monkeypatch.setattr(bk, "_cgroup_cpu_limit", lambda: limit)
monkeypatch.setattr(bk, "_linux_physical_cores", lambda *a, **k: physical)
+ monkeypatch.setattr(bk, "_allowed_cpus", lambda: cpus if allowed is None else allowed)
def test_without_a_quota_the_default_is_unchanged(monkeypatch):
@@ -359,19 +361,29 @@ def test_the_physical_count_is_never_read_off_linux(monkeypatch):
@pytest.mark.parametrize(
- "cpus, physical, limit, expected",
+ "cpus, physical, limit, allowed, expected",
[
- (32, 16, None, None), # SMT: sd.cpp's own default is the same count
- (8, 8, None, 8), # no SMT: sd.cpp's fallback would run 4
- (192, 2, None, 2), # cpuset: sd.cpp ignores the affinity mask
- (4, 4, None, None), # sd.cpp keeps every CPU at 4 or fewer
- (32, 16, 6, 6), # a tighter quota, as before
+ (32, 16, None, None, None), # SMT: sd.cpp's own default is the same count
+ (8, 8, None, None, 8), # no SMT: sd.cpp's fallback would run 4
+ (192, 2, None, 4, 2), # cpuset: sd.cpp ignores the affinity mask
+ (4, 4, None, None, None), # sd.cpp keeps every CPU at 4 or fewer
+ (32, 16, 6, None, 6), # a tighter quota, as before
+ (
+ 4,
+ 2,
+ None,
+ None,
+ None,
+ ), # 4 vCPU / 2 cores, nothing restricting: sd.cpp's 4 threads measured faster than 2
+ (2, 1, None, None, None), # 2 vCPU / 1 core: sd.cpp's 2, not 1
+ (4, 2, 2, None, 2), # the same host under a 2-CPU quota: the quota still wins
+ (4, 2, None, 3, 2), # or under a 3-CPU taskset
],
)
def test_explicit_threads_passes_the_count_when_sd_cpp_would_pick_another(
- monkeypatch, cpus, physical, limit, expected
+ monkeypatch, cpus, physical, limit, allowed, expected
):
- _host(monkeypatch, cpus = cpus, physical = physical, limit = limit)
+ _host(monkeypatch, cpus = cpus, physical = physical, limit = limit, allowed = allowed)
monkeypatch.setattr(bk, "_apple_performance_cores", lambda: None)
assert bk._explicit_threads() == expected