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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: discrete-first GGUF placement keeps explicit device picks, matching context ceiling; in-process Rosetta check (#14492)
Daniel Hand22feecf2233
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.pyindex 916c8e30bc6..dfdc77bd4ee 100644--- a/studio/backend/core/inference/llama_cpp.py+++ b/studio/backend/core/inference/llama_cpp.py@@ -26796,6 +26796,8 @@ class LlamaCppBackend:_moe_cache_ram_ok = False_shared_gpus = frozenset()_integrated_gpus = frozenset()+ # The discrete-first tier; accounting keeps _integrated_gpus.+ _tier_gpus = frozenset()# Sized inputs for the tensor-spill planner, None when the fit never# priced them. Bound before the try like _placement_verdict_partial:# the except arm restores use_fit=True without rebinding@@ -27009,6 +27011,19 @@ class LlamaCppBackend:))_integrated_gpus = frozenset(idx for idx, _free in gpus if idx in _unified)+ # A user device flag names ordinals of the base pick's mask, and an+ # unmappable mask re-emits ordinals as physical ids: keep the base pick.+ # Host-only bytes still never land on a discrete-only subset.+ _tier_gpus = _integrated_gpus+ if _integrated_gpus and (+ _extra_args_set_any_flag(extra_args, _DEVICE_FLAGS)+ or any(+ str(os.environ.get(_v, "")).strip()+ for _v in ("LLAMA_ARG_DEVICE", "LLAMA_ARG_MAIN_GPU")+ )+ or self._visibility_mask_is_unmappable()+ ):+ _tier_gpus = frozenset()# Vulkan reports total 0 only for integrated GPUs. Their# free "VRAM" is the same host pool the RAM guard prices._shared_gpu_ids = (@@ -27048,9 +27063,7 @@ class LlamaCppBackend:def _tiered_subsets(ranked: list, floor: int) -> list:# (count, prefix) in _select_gpus order: unshared discrete first.- tier = LlamaCppBackend._discrete_tier(- ranked, _integrated_gpus, _shared_gpus- )+ tier = LlamaCppBackend._discrete_tier(ranked, _tier_gpus, _shared_gpus)tiers = ((tier, ranked) if tier is not None and floor <= len(tier) else (ranked,))@@ -27926,7 +27939,7 @@ class LlamaCppBackend:# RAM, so moving the projector frees nothing there. Also pin when that# alone lets the discrete cards hold the model (#7624)._mm_discrete = LlamaCppBackend._discrete_tier(- _mm_budgeted_gpus, _integrated_gpus, _shared_gpus+ _mm_budgeted_gpus, _tier_gpus, _shared_gpus)if (not _mm_needs_fit@@ -28546,13 +28559,30 @@ class LlamaCppBackend:)best_cap = 0_cap_fraction = _vram_frac - _flat_mtp_reserve- for n_gpus in range(1, len(ranked_for_cap) + 1):- subset = ranked_for_cap[:n_gpus]+ # Every subset Auto may take, discrete tier included, priced as Auto prices it.+ # With an integrated GPU, start at Auto's GPU floor: a forced split never takes+ # a checkpoint-free dGPU-only subset, explicit device pick or not.+ _cap_floor = 1+ if _integrated_gpus:+ _cap_floor = max(+ 1,+ min(+ _layer_min_gpus,+ sum(+ 1+ for g in ranked_for_cap+ if _gpu_usable(g, _cap_fraction)+ > _pipeline_overhead_bytes / (1024 * 1024)+ )+ or 1,+ ),+ )+ for n_gpus, subset in _tiered_subsets(ranked_for_cap, _cap_floor):# Per-GPU-consistent pool budget (fixes mixed# known/unknown totals); pass it as an absolute# budget so the fit and the check below agree.pool_budget = _pool_budget_mib(subset, _cap_fraction)- _ms = _subset_model_size(n_gpus)+ _ms = _subset_model_size(n_gpus) - _discrete_host_only(subset)# Compute buffer is replicated per device in a layer# split, so scale the context term by the subset size._cc_sub = lambda c, n = n_gpus: _cc_bytes(c, n)@@ -28572,14 +28602,32 @@ class LlamaCppBackend:budget_frac = 1.0,pooled = True,total_mib = None,- ctx_checkpoints = _placement_ctx_checkpoints,+ ctx_checkpoints = _subset_ckpts(subset),)kv = _kv_bytes(capped)footprint_mib = (_ms + kv + _mtp_bytes(capped) + _cc_sub(capped)) / (1024 * 1024)- if footprint_mib <= pool_budget:- best_cap = max(best_cap, capped)+ if footprint_mib > pool_budget:+ continue+ if (+ n_gpus > 1+ and _integrated_gpus+ and _integrated_gpus.isdisjoint(i for i, _f in subset)+ ):+ # A dGPU-only tier is not a usable-descending prefix, so pooling can+ # hide a small card's replicated reserve: cap as Auto's walk does.+ _usable_mib = [_gpu_usable(g, _cap_fraction) for g in subset]+ _reserve_at = lambda c, n = n_gpus: (+ _pipeline_overhead_bytes + _cc_bytes(c, n) // n+ )+ if not self._every_gpu_holds_reserve(+ _usable_mib, _reserve_at(capped)+ ):+ capped = self._cap_ctx_to_per_device_reserve(+ capped, _usable_mib, _reserve_at+ )+ best_cap = max(best_cap, capped)if best_cap > 0:max_available_ctx = best_cap_ctx_cap_fits = True@@ -28622,7 +28670,7 @@ class LlamaCppBackend:min_gpus = _layer_min_gpus,split_extra_bytes = _cc_split_extra(effective_ctx),shared = _shared_gpus,- integrated = _integrated_gpus,+ integrated = _tier_gpus,discrete_size_bytes = requested_total - _discrete_host_only(),)# No silent shrink: effective_ctx stays == requested_ctx.@@ -28681,6 +28729,13 @@ class LlamaCppBackend:split_extra_bytes = _cc_split_extra(effective_ctx, cache_type = "q8_0"),+ shared = _shared_gpus,+ integrated = _tier_gpus,+ discrete_size_bytes = model_size_fit+ + _q8_kv+ + _mtp_bytes(effective_ctx)+ + _cc_bytes(effective_ctx, cache_type = "q8_0")+ - _discrete_host_only(),)_q8_fits = not _q8_use_fit_cuda_ctx_notice = self._cuda_context_overcommit_notice(@@ -28741,14 +28796,15 @@ class LlamaCppBackend:# discrete-tier subset draws on none.return _subset_model_size(n_gpus) - _discrete_host_only(subset)- for n_gpus, subset in _subsets:+ def _auto_fit(n_gpus: int, subset, ctx: int) -> Optional[int]:+ # The context ``subset`` holds when Auto caps ``ctx`` to it, or None.pool_budget = _pool_budget_mib(subset, pin_fraction)_ms = _subset_ms(n_gpus, subset)# Compute buffer is replicated per device in a layer# split, so scale the context term by the subset size._cc_sub = lambda c, n = n_gpus: _cc_bytes(c, n)capped = self._fit_context_to_vram(- effective_ctx,+ ctx,pool_budget,_ms,cache_type_kv,@@ -28770,7 +28826,7 @@ class LlamaCppBackend:_ms + kv + _mtp_bytes(capped) + _cc_sub(capped)) / (1024 * 1024)if footprint_mib > pool_budget:- continue+ return None# Pooling admits a subset whose per-device reserve# does not fit the smallest card, which then OOMs at# launch; only knowable once `capped` is chosen.@@ -28792,7 +28848,7 @@ class LlamaCppBackend:capped, _usable_mib, _reserve_at)if capped <= 0:- continue+ return None# Pooled terms only shrink with the context, so this# cannot newly fail; re-price rather than lean on that# across the KV and MTP branches.@@ -28801,9 +28857,34 @@ class LlamaCppBackend:_ms + kv + _mtp_bytes(capped) + _cc_sub(capped)) / (1024 * 1024)if footprint_mib > pool_budget:- continue+ return None+ return capped++ for n_gpus, subset in _subsets:+ capped = _auto_fit(n_gpus, subset, effective_ctx)+ if capped is None:+ continueeffective_ctx = capped_ctx_capped_for_vram = 0 < capped < _ctx_before_fit+ if (+ _ctx_capped_for_vram+ and _tier_gpus+ and _tier_gpus.isdisjoint(i for i, _f in subset)+ ):+ # Capped only by the discrete preference: the base walk+ # holds the full context, so nothing evicted would help.+ _base_ctx = next(+ (+ c+ for n in range(_auto_min_gpus, len(ranked) + 1)+ for c in (_auto_fit(n, ranked[:n], _ctx_before_fit),)+ if c is not None+ ),+ None,+ )+ _ctx_capped_for_vram = not (+ _base_ctx is not None and _base_ctx >= _ctx_before_fit+ )gpu_indices = sorted(idx for idx, _ in subset)use_fit = Falsebreak@@ -28855,7 +28936,7 @@ class LlamaCppBackend:per_device_overhead_bytes = _pipeline_overhead_bytes,min_gpus = _layer_min_gpus,shared = _shared_gpus,- integrated = _integrated_gpus,+ integrated = _tier_gpus,discrete_size_bytes = _fs_total - _discrete_host_only(),)if use_fit and not explicit_ctx:@@ -29274,7 +29355,8 @@ class LlamaCppBackend:ubatch_for_slots = _ubatch_for_slots,mtp_bytes_for_slots = lambda s, ub: _mtp_bytes(_reduce_ctx, s, ub),ctx_checkpoints = _placement_ctx_checkpoints,- integrated = _integrated_gpus,+ # Picks a subset, so an explicit device pick keeps the base pick.+ integrated = _tier_gpus,discrete_host_only_bytes = _discrete_host_only(),)if not _uf_slots:@@ -29311,7 +29393,7 @@ class LlamaCppBackend:include_requested = True,exact = True, # only n_parallel is accepted belowctx_checkpoints = _placement_ctx_checkpoints,- integrated = _integrated_gpus,+ integrated = _tier_gpus,discrete_host_only_bytes = _discrete_host_only(),)return _gi if not _uf and _got == n_parallel else Nonediff --git a/studio/backend/tests/test_ctx_checkpoint_host_budget.py b/studio/backend/tests/test_ctx_checkpoint_host_budget.pyindex a1abf1df8d9..2212ba86562 100644--- a/studio/backend/tests/test_ctx_checkpoint_host_budget.py+++ b/studio/backend/tests/test_ctx_checkpoint_host_budget.py@@ -479,8 +479,9 @@ class TestCheckpointsNeverReachAVramFigure:assert source.count("ctx_checkpoints = _fit_ctx_checkpoints,") == 1# +1: the drafter probe's one-slot fit (#8872)# Per subset (incl. the drafter's slot search): a discrete-tier subset pays none.- assert source.count("ctx_checkpoints = _placement_ctx_checkpoints,") == 3- assert source.count("ctx_checkpoints = _subset_ckpts(") == 3+ # The context ceiling (best_cap) prices per subset too.+ assert source.count("ctx_checkpoints = _placement_ctx_checkpoints,") == 2+ assert source.count("ctx_checkpoints = _subset_ckpts(") == 4assert source.count("_kv_bytes(effective_ctx, _effective_ctx_checkpoints)") == 1def test_the_fit_is_unmoved_by_the_new_term(self):diff --git a/studio/backend/tests/test_llama_cpp_placement.py b/studio/backend/tests/test_llama_cpp_placement.pyindex a9d42a35b99..2459d628e18 100644--- a/studio/backend/tests/test_llama_cpp_placement.py+++ b/studio/backend/tests/test_llama_cpp_placement.py@@ -6110,7 +6110,8 @@ def test_discrete_tier_subset_is_not_charged_host_checkpoints(tmp_path, monkeypa)assert got == "1"# The APU candidate makes the pool pay; the dGPU-only subset does not.- assert seen and seen[-1][1] == 0 and seen[-1][0] < 16_384+ dgpu = [ckpts for budget, ckpts in seen if budget < 16_384]+ assert dgpu and all(ckpts == 0 for ckpts in dgpu)def _host_projector_launch(tmp_path, monkeypatch, memory, rocm, gguf_gb):@@ -6412,3 +6413,191 @@ def test_projector_preflight_skips_a_shared_dgpu_like_placement(out = _launch(backend, gguf, n_ctx = 8192, is_vision = True)assert out["env"].get("CUDA_VISIBLE_DEVICES") == expected_gpuassert ("--no-mmproj-offload" in out["cmd"]) is pinned+++def _mixed_auto_backend(+ tmp_path,+ monkeypatch,+ memory,+ gguf_gb,+ rocm = (0,),+):+ """Auto context at a 32K native context; checkpoints cost 512 MiB each on unified memory."""+ monkeypatch.setattr(+ LlamaCppBackend, "_rocm_unified_memory_gpu_ids", staticmethod(lambda: set(rocm))+ )+ monkeypatch.setattr(+ LlamaCppBackend,+ "_INTEGRATED_CUDA_IDS",+ {LlamaCppBackend._integrated_cuda_mask_key(): set()},+ )+ backend, gguf = _backend(tmp_path, vulkan = False, memory = memory)+ backend._get_gguf_size_bytes = lambda _p: int(gguf_gb * 1024**3)+ backend._can_estimate_kv = lambda: True+ backend._estimate_kv_cache_bytes = lambda ctx, *a, ctx_checkpoints = 0, **k: (+ int(ctx) * 64 * 1024 + int(ctx_checkpoints or 0) * 512 * 1024**2+ )+ backend._compute_buffer_ctx_bytes = lambda *a, **k: 0+ backend._estimate_compute_buffer_bytes = lambda **k: 1+ backend._amd_apu_wants_unified_memory = lambda ids = None, *a, **k: bool(+ set(ids or [i for i, _f, _t in memory]) & set(rocm)+ )+ backend.probe_server_capabilities = lambda *a, **k: {+ "ctx_checkpoints_flag": "--ctx-checkpoints"+ }++ def read_metadata(_path):+ backend._context_length = 32_768++ backend._read_gguf_metadata = read_metadata+ return backend, gguf+++def test_a_cap_from_the_discrete_preference_is_not_a_shortfall(tmp_path, monkeypatch):+ # The APU holds the full context; only preferring the dGPU caps it, so a load beside+ # other models must not report a shortfall that evicting them could never cover.+ backend, gguf = _mixed_auto_backend(tmp_path, monkeypatch, [_APU, _DGPU], 14)+ result = _launch(backend, gguf, n_ctx = 0, refuse_partial_gpu_fit = True)+ assert result["env"]["CUDA_VISIBLE_DEVICES"] == "1"+ assert _launched_ctx(result) < 32_768+++ "memory, rocm",+ [+ ([(0, 16_000, 16_384), (1, 15_000, 16_384)], ()),+ ([(0, 15_000, 0), _DGPU], (0,)),+ ],+ ids = ["homogeneous", "apu_caps_too"],+)+def test_a_real_vram_cap_still_refuses_a_partial_fit(tmp_path, monkeypatch, memory, rocm):+ from core.inference.llama_cpp import GpuMemoryShortError++ backend, gguf = _mixed_auto_backend(tmp_path, monkeypatch, memory, 14, rocm = rocm)+ with pytest.raises(GpuMemoryShortError) as exc:+ _launch(backend, gguf, n_ctx = 0, refuse_partial_gpu_fit = True)+ assert exc.value.capped+++def test_the_context_ceiling_covers_a_discrete_auto_launch(tmp_path, monkeypatch):+ # Host-RAM checkpoints are not charged to the dGPU Auto picks, so the published+ # ceiling must not be priced with them either.+ memory = [(0, 16_000, 16_384), (1, 1_000, 0)]+ backend, gguf = _mixed_auto_backend(tmp_path, monkeypatch, memory, 11, rocm = (1,))+ result = _launch(backend, gguf, n_ctx = 0, ctx_checkpoints = 6)+ assert result["env"]["CUDA_VISIBLE_DEVICES"] == "0"+ assert _launched_ctx(result) <= backend.max_context_length+ assert _launched_ctx(result) <= backend.vram_fit_context_length++[email protected]("apu_free, gguf_gb", [(8_000, 13), (10_000, 13.5), (8_000, 12.5)])+def test_a_capped_dgpu_first_shortfall_matches_a_host_without_an_apu(+ tmp_path, monkeypatch, apu_free, gguf_gb+):+ # The dGPU ranks first either way, so the base walk caps exactly as on a host whose+ # second card is discrete: the shortfall answer must not depend on the APU.+ from core.inference.llama_cpp import GpuMemoryShortError++ def outcome(memory, rocm):+ backend, gguf = _mixed_auto_backend(tmp_path, monkeypatch, memory, gguf_gb, rocm = rocm)+ try:+ result = _launch(backend, gguf, n_ctx = 0, refuse_partial_gpu_fit = True)+ except GpuMemoryShortError as exc:+ return ("short", exc.capped)+ return (result["env"]["CUDA_VISIBLE_DEVICES"], _launched_ctx(result))++ mixed = outcome([(0, apu_free, 0), _DGPU], (0,))+ assert mixed == outcome([(0, apu_free, 16_384), _DGPU], ())++[email protected]("flag", [["--device", "ROCm0"], ["--main-gpu", "0"]])+def test_a_user_device_flag_keeps_host_ram_off_the_dgpu_budget(tmp_path, monkeypatch, flag):+ # The flag only turns off the discrete-first pick: the dGPU the base pick lands on+ # still holds no host-RAM checkpoints, so the context matches the flagless load.+ memory = [(0, 16_000, 16_384), (1, 1_000, 0)]++ def launch(**kw):+ backend, gguf = _mixed_auto_backend(tmp_path, monkeypatch, memory, 11, rocm = (1,))+ result = _launch(backend, gguf, n_ctx = 0, ctx_checkpoints = 6, **kw)+ return result["env"]["CUDA_VISIBLE_DEVICES"], _launched_ctx(result)++ assert launch(extra_args = flag) == launch() == ("0", 32_768)+++def test_the_context_ceiling_holds_a_dgpu_tier_to_each_cards_reserve(tmp_path, monkeypatch):+ # An iGPU ranked between two dGPUs: the dGPU-only tier pools a small second card, which Auto+ # caps to its replicated reserve; the published ceiling must apply the same cap.+ memory = [(0, 12_000, 16_384), (1, 300, 0), (2, 2_000, 2_200)]+ backend, gguf = _mixed_auto_backend(tmp_path, monkeypatch, memory, 11, rocm = (1,))+ monkeypatch.setattr(+ LlamaCppBackend,+ "_every_gpu_holds_reserve",+ staticmethod(lambda usable, reserve: len(list(usable)) < 2),+ )+ monkeypatch.setattr(+ LlamaCppBackend,+ "_cap_ctx_to_per_device_reserve",+ staticmethod(lambda ctx, usable, fn, min_ctx = 8192: min(ctx, 12_288)),+ )+ result = _launch(backend, gguf, n_ctx = 0)+ assert result["env"]["CUDA_VISIBLE_DEVICES"] == "0,2"+ assert _launched_ctx(result) == 12_288+ assert backend.max_context_length == 12_288++[email protected]("flag", [None, ["--device", "ROCm0"], ["--main-gpu", "0"]])+def test_the_context_ceiling_starts_at_a_forced_gpu_floor(tmp_path, monkeypatch, flag):+ # A retry that must stay multi-GPU never takes a dGPU-only subset, so its checkpoint-free+ # context must not set the published ceiling, with or without an explicit device pick.+ memory = [(0, 1_500, 0), _DGPU]+ backend, gguf = _mixed_auto_backend(tmp_path, monkeypatch, memory, 11)+ kw = {"extra_args": flag} if flag else {}+ result = _launch(+ backend, gguf, n_ctx = 0, ctx_checkpoints = 6, preserve_multi_gpu_on_layer = True, **kw+ )+ assert result["env"]["CUDA_VISIBLE_DEVICES"] == "0,1"+ assert _launched_ctx(result) < 32_768+ assert backend.max_context_length == _launched_ctx(result)++[email protected]("path", ["filesize", "auto_ctx", "explicit_ctx"])[email protected]("how", ["argv", "env", "main_gpu"])+def test_a_user_device_selection_keeps_the_base_placement(tmp_path, monkeypatch, path, how):+ # A user device flag names ordinals of the pinned mask; re-pinning to the dGPU would+ # silently point "ROCm0" at a different card.+ monkeypatch.delenv("LLAMA_ARG_DEVICE", raising = False)+ kw = {}+ if how == "argv":+ kw["extra_args"] = ["--device", "ROCm0"]+ elif how == "main_gpu":+ kw["extra_args"] = ["--main-gpu", "0"]+ else:+ monkeypatch.setenv("LLAMA_ARG_DEVICE", "ROCm0")+ got = _mixed_host_launch(+ tmp_path, monkeypatch, [_APU, _DGPU], gguf_gb = 8, path = path, rocm = {0}, **kw+ )+ assert got == "0"++[email protected]("path", ["filesize", "auto_ctx", "explicit_ctx"])+def test_an_unmappable_mask_keeps_the_base_placement(tmp_path, monkeypatch, path):+ # UUID tokens cannot be mapped back, so a picked ordinal 1 would be emitted as+ # physical GPU 1, which this mask hides.+ import torch++ monkeypatch.setattr(torch.version, "hip", "6.4.0", raising = False)+ monkeypatch.setattr(sys, "platform", "linux")+ for name in ("CUDA_VISIBLE_DEVICES", "HIP_VISIBLE_DEVICES"):+ monkeypatch.delenv(name, raising = False)+ monkeypatch.setenv("ROCR_VISIBLE_DEVICES", "GPU-aaaaaaaaaaaaaaaa,GPU-bbbbbbbbbbbbbbbb")+ assert LlamaCppBackend._visibility_mask_is_unmappable()+ captured = {}+ real_launch = _launch++ def keep_env(*a, **k):+ captured.update(real_launch(*a, **k))+ return captured++ monkeypatch.setitem(globals(), "_launch", keep_env)+ _mixed_host_launch(tmp_path, monkeypatch, [_APU, _DGPU], gguf_gb = 8, path = path, rocm = {0})+ assert captured["env"].get("ROCR_VISIBLE_DEVICES") != "1"diff --git a/studio/install_llama_prebuilt.py b/studio/install_llama_prebuilt.pyindex 3ec46974fe9..6cc2575a2ef 100644--- a/studio/install_llama_prebuilt.py+++ b/studio/install_llama_prebuilt.py@@ -3085,23 +3085,34 @@ def _windows_display_adapter_in_registry(needles: tuple[tuple[str, str], ...]) -return False[email protected]_cache(maxsize = 1)+def _darwin_hw_optional_arm64() -> bool:+ """sysctl hw.optional.arm64 read in-process (no subprocess on the launch preflight)."""+ try:+ import ctypes+ import ctypes.util++ libc = ctypes.CDLL(ctypes.util.find_library("c"))+ value = ctypes.c_int(0)+ size = ctypes.c_size_t(ctypes.sizeof(value))+ if (+ libc.sysctlbyname(+ b"hw.optional.arm64", ctypes.byref(value), ctypes.byref(size), None, 0+ )+ == 0+ ):+ return value.value == 1+ except (OSError, AttributeError, TypeError, ValueError):+ pass+ return False++def _host_machine(system: str) -> str:"""platform.machine(), but arm64 under Rosetta: macos-x64 is built without Metal."""machine = platform.machine().lower()if system != "Darwin" or machine != "x86_64":return machine- try:- result = subprocess.run(- ["sysctl", "-n", "hw.optional.arm64"],- capture_output = True,- text = True,- timeout = 5,- )- if result.returncode == 0 and result.stdout.strip() == "1":- return "arm64"- except Exception:- pass- return machine+ return "arm64" if _darwin_hw_optional_arm64() else machinedef detect_host(*, probe_rocm_with_nvidia: bool = False) -> HostInfo:diff --git a/tests/studio/install/test_llama_host_platform_edges.py b/tests/studio/install/test_llama_host_platform_edges.pyindex ec0ba5951de..77f797cdeaf 100644--- a/tests/studio/install/test_llama_host_platform_edges.py+++ b/tests/studio/install/test_llama_host_platform_edges.py@@ -192,47 +192,42 @@ MAC_ARM = f"llama-{TAG}-bin-macos-arm64.tar.gz"MAC_X64 = f"llama-{TAG}-bin-macos-x64.tar.gz"-def _fake_mac(- monkeypatch,- machine,- sysctl_out,- sysctl_rc = 0,- sysctl_raises = None,-):+def _fake_mac(monkeypatch, machine, hw_arm64):+ """Darwin with ``platform.machine()`` = machine and sysctl hw.optional.arm64 = hw_arm64.+ Returns the subprocess calls made: reading the key must spawn none."""calls = []real_run = subprocess.rundef fake_run(cmd, *args, **kwargs):if list(cmd)[:1] == ["sysctl"]:calls.append(list(cmd))- if sysctl_raises is not None:- raise sysctl_raises- return subprocess.CompletedProcess(cmd, sysctl_rc, sysctl_out, "")return real_run(cmd, *args, **kwargs)+ reads = []++ def fake_hw_arm64():+ reads.append(1)+ return hw_arm64+monkeypatch.setattr(ILP.platform, "system", lambda: "Darwin")monkeypatch.setattr(ILP.platform, "machine", lambda: machine)monkeypatch.setattr(ILP.platform, "mac_ver", lambda: ("14.5", ("", "", ""), machine))monkeypatch.setattr(ILP.shutil, "which", lambda *_a, **_k: None)monkeypatch.setattr(ILP.subprocess, "run", fake_run)- return calls+ monkeypatch.setattr(ILP, "_darwin_hw_optional_arm64", fake_hw_arm64)+ return calls, reads@pytest.mark.parametrize(- "machine, sysctl_out, rc, raises, expected_machine, expected_asset",+ "machine, hw_arm64, expected_machine, expected_asset",[- ("x86_64", "1\n", 0, None, "arm64", MAC_ARM), # Rosetta- ("x86_64", "0\n", 0, None, "x86_64", MAC_X64), # Intel Mac- ("x86_64", "", 1, None, "x86_64", MAC_X64), # key absent (older Intel macOS)- ("x86_64", "", 0, FileNotFoundError("sysctl"), "x86_64", MAC_X64),- ("x86_64", "", 0, subprocess.TimeoutExpired("sysctl", 5), "x86_64", MAC_X64),- ("arm64", "1\n", 0, None, "arm64", MAC_ARM), # native arm64+ ("x86_64", True, "arm64", MAC_ARM), # Rosetta+ ("x86_64", False, "x86_64", MAC_X64), # Intel Mac, key absent or unreadable+ ("arm64", True, "arm64", MAC_ARM), # native arm64],)-def test_macos_host_machine(- machine, sysctl_out, rc, raises, expected_machine, expected_asset, monkeypatch-):- _fake_mac(monkeypatch, machine, sysctl_out, rc, raises)+def test_macos_host_machine(machine, hw_arm64, expected_machine, expected_asset, monkeypatch):+ calls, _reads = _fake_mac(monkeypatch, machine, hw_arm64)host = ILP.detect_host()assert host.machine == expected_machineassert host.is_arm64 == (expected_machine == "arm64")@@ -247,13 +242,36 @@ def test_macos_host_machine(host.is_arm64,host.is_x86_64,)+ assert calls == []-def test_sysctl_is_not_run_off_macos(monkeypatch):- calls = _fake_mac(monkeypatch, "x86_64", "1\n")+def test_sysctl_is_not_read_off_macos(monkeypatch):+ calls, reads = _fake_mac(monkeypatch, "x86_64", True)monkeypatch.setattr(ILP.platform, "system", lambda: "Linux")assert ILP.platform_only_host().machine == "x86_64"+ assert calls == [] and reads == []+++def test_the_rosetta_read_spawns_no_subprocess_and_is_cached(monkeypatch):+ # platform_only_host() runs on every desktop launch preflight: an Intel Mac must not+ # pay a sysctl subprocess (up to 5 s) per call for an answer that never changes.+ calls = []++ def refuse(cmd, *args, **kwargs):+ calls.append(list(cmd))+ raise AssertionError("no subprocess on the health path")++ monkeypatch.setattr(ILP.platform, "system", lambda: "Darwin")+ monkeypatch.setattr(ILP.platform, "machine", lambda: "x86_64")+ monkeypatch.setattr(ILP.platform, "mac_ver", lambda: ("14.5", ("", "", ""), "x86_64"))+ monkeypatch.setattr(ILP.subprocess, "run", refuse)+ for _ in range(3):+ ILP.platform_only_host()assert calls == []+ # Off macOS the libc has no sysctlbyname: the read fails closed to x86_64.+ if sys.platform.startswith("linux"):+ ILP._darwin_hw_optional_arm64.cache_clear()+ assert ILP._darwin_hw_optional_arm64() is Falsedef _fake_tegra(