From 92507aac3833fb0b389c4369bd3fc3fa63b75901 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 9 Oct 2026 15:24:07 +0200 Subject: [PATCH] setup: on a unified-memory APU write the expert cache as a number that leaves the OS its RAM, not auto (#1715) --- docs/STRIX_HALO.md | 1 + setup.py | 28 +++++++++++++++++++++++++ tools/test_setup_unsloth.py | 42 +++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/docs/STRIX_HALO.md b/docs/STRIX_HALO.md index 85a908c9..6365cac7 100644 --- a/docs/STRIX_HALO.md +++ b/docs/STRIX_HALO.md @@ -20,6 +20,7 @@ What the engine does differently on this chip: - **The hipBLASLt table** `tools/hip/gfx1151-hipblaslt-100401.txt` (ROCm 7.14.1's hipBLASLt 1.x `100401`), including the prompt shapes at `--prefill 16384` (T 16383 / 16384). Setup's own ROCm for gfx1151 (the 7.14.0a20260608 wheels, hipBLASLt 1.4.0) takes `tools/hip/gfx1151-hipblaslt-100400.txt`: the same shapes, calibrated against that library (docs/AMD_HIP.md, Tuning table). +- **The expert cache and the RAM budget share the RAM.** For a model with a RAM budget (UD-IQ4_XS, UD-Q4_K_XL) setup writes a number for `--expert-cache` (the RAM less 24 GB less the budget), not `auto`: `auto` counts all the memory the OS can give back at start, and with a 55 GiB budget and a desktop open it took the room the OS needed (#1715, a global OOM on a 128 GB box). The number is in the config's `args`; `--expert-cache N` there sets it. An engine run by hand should pass one too. ## 1. The toolchain (no root needed) diff --git a/setup.py b/setup.py index 321e2bc9..2ba9740e 100644 --- a/setup.py +++ b/setup.py @@ -1538,6 +1538,22 @@ def resident_budget_gib(model, ram, kv_ram_gb=0.0) -> int: return max(8, min(gib, int(MODELS[model]["arena_gb"] / 1.073741824))) +UMA_EXPERTS = 24576 # the Flash-Next model's experts (48 layers x 512): the most an expert cache can hold + + +def uma_expert_cache(model, ram, budget_gib, kv_ram_gb=0.0): + """#1715: on a unified-memory APU (Strix Halo) the GPU's expert cache and the RAM budget of experts are both RAM. + `--expert-cache auto` sizes the cache from the memory the OS can give back at start, which counts all the RAM the + budget has not taken yet: a 128 GB box with a 55 GiB budget and the desktop open was killed by the OOM killer. So + there setup writes a number, not `auto`: the RAM less UNSLOTH_RAM_LEFT_GB less the budget (and a KV cache streamed + to RAM), in slots (an expert is priced at 1.1x the average one: the Q8_0 downs are bigger). None when no room is left + (the config keeps `auto`). A recommendation: the number is in the config's args, and --expert-cache N there sets it.""" + slot_gib = MODELS[model]["arena_gb"] * 1e9 / UMA_EXPERTS * 1.1 / 2**30 + room = ram - UNSLOTH_RAM_LEFT_GB - budget_gib - math.ceil(kv_ram_gb) + slots = min(UMA_EXPERTS, int(room / slot_gib)) + return slots if slots >= 256 else None + + def budget_choice(model, ram, asked) -> float: """S4: UD-Q4_K_XL's RAM budget: --resident-budget-gib N as given, else the recommendation (resident_budget_gib). More than the recommendation is kept, with what it risks (the owner's rule: setup recommends, it never forces).""" @@ -5478,6 +5494,18 @@ def main() -> int: warn("--kv-streaming on: a context under 64K is not streamed (the attention's window holds all of it): off") if budget is not None and not q4_split: # UD-Q4_K_XL: the experts read from the GGUF in place, the most-used N args += ["--resident-budget-gib", f"{budget:g}"] # GiB kept in RAM (#498: a layer split had no budget) + if gpu.get("uma") and "--expert-cache" in args: + # #1715: the cache and the budget draw on the same RAM: a number that leaves the OS its room, not `auto` + slots = uma_expert_cache(model, ram, budget, kv_ram_gb if "--kv-resident" in args else 0.0) + if slots is not None: + args[args.index("--expert-cache") + 1] = str(slots) + ok(f"expert cache: {slots} slots (not auto): this GPU shares the RAM with the {budget:g} GiB budget, " + f"so setup leaves {UNSLOTH_RAM_LEFT_GB} GB of the RAM for the OS and the desktop (#1715). " + "--expert-cache N in the config's args sets it") + else: + warn(f"this GPU shares the RAM, and the {budget:g} GiB RAM budget leaves almost none for the expert " + "cache: it stays on auto, which may take the room the OS needs (#1715). A smaller " + "--resident-budget-gib leaves room") if vision != "none": args += ["--vision", "--vram-reserve-mib", str(VISION[vision]["reserve_mib"])] if vision == "gpu" and a.vram_reserve_mib is None and 0 < gpu.get("vram_gb", 0.0) <= 12.5: diff --git a/tools/test_setup_unsloth.py b/tools/test_setup_unsloth.py index b9ba0536..27a7b9a3 100644 --- a/tools/test_setup_unsloth.py +++ b/tools/test_setup_unsloth.py @@ -556,6 +556,48 @@ class IQ4XS(Base): self.assertIn("RAM budget: 40 GiB of UD-IQ4_XS's experts", out) +class StrixHaloCache(Base): + """#1715: on a unified-memory APU the expert cache and the RAM budget are the same RAM: setup writes a cache size + that leaves the OS room, not `--expert-cache auto` (it OOM-killed a 128 GB Strix Halo with the desktop open).""" + + HALO = [{"index": 0, "name": "AMD Radeon 8060S", "vram_gb": 2.0, "arch": "gfx1151", "driver": "amdgpu", + "uma": True, "dedicated_gb": 2.0, "shared_gb": 112.0}] + + def run_halo(self, ram, argv=("--context", "8192"), amd=None): + with mock.patch.object(setup, "get_prebuilt_hip", lambda *a, **k: self.t / "engine"), mock.patch.object(setup, "WIN", False), mock.patch.object(setup, "build_engine_hip", lambda *a, **k: self.t / "engine"): + return self.main([*argv, "--backend", "hip"], version="0.1.41", amd=self.HALO if amd is None else amd, + ram=ram, m="UD-IQ4_XS") + + def test_cache_is_a_number_that_leaves_room(self): + code, out, cfg = self.run_halo(124.0) + self.assertEqual(code, 0, out) + args = cfg["args"] + self.assertEqual(args[args.index("--resident-budget-gib") + 1], "55") + slots = int(args[args.index("--expert-cache") + 1]) + # 124 - 24 (left) - 55 (budget) = 45 GiB of 1.1 x the average expert (59.5 GB / 24576) + self.assertEqual(slots, int(45 / (59.5e9 / setup.UMA_EXPERTS * 1.1 / 2**30))) + self.assertTrue(0 < slots < setup.UMA_EXPERTS) + self.assertIn("expert cache: %d slots (not auto)" % slots, out) + + def test_never_more_than_all_the_experts(self): + self.assertEqual(setup.uma_expert_cache("UD-IQ4_XS", 512.0, 55), setup.UMA_EXPERTS) + + def test_no_room_keeps_auto_and_says_so(self): + self.assertIsNone(setup.uma_expert_cache("UD-IQ4_XS", 64.0, 40)) + code, out, cfg = self.run_halo(64.0, ("--context", "8192", "--resident-budget-gib", "40")) + self.assertEqual(code, 0, out) + args = cfg["args"] + self.assertEqual(args[args.index("--expert-cache") + 1], "auto") + self.assertIn("leaves almost none for the expert cache", out) + + def test_a_discrete_card_keeps_auto(self): + r9700 = [{"index": 0, "name": "AMD Radeon AI PRO R9700", "vram_gb": 31.9, "arch": "gfx1201", + "driver": "amdgpu"}] + code, out, cfg = self.run_halo(124.0, amd=r9700) + self.assertEqual(code, 0, out) + self.assertEqual(cfg["args"][cfg["args"].index("--expert-cache") + 1], "auto") + + class LayerSplit(unittest.TestCase): """#498: UD-Q4_K_XL across GPUs - asked at setup (one GPU by default), and a start with --gpus no longer keeps the RAM budget the engine refused with a split (it exited with code 2). #642: from RESIDENT_SPLIT_ENGINE the