Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
refactor(plan): isolate unqualified GPU discovery
Kenneth-Javier Ortigozab5a8bacf5efe
diff --git a/c/resource_plan.py b/c/resource_plan.pyindex 7bf18570..41f0c887 100644--- a/c/resource_plan.py+++ b/c/resource_plan.py@@ -338,6 +338,26 @@ def _discover_amd_gpus():return devices+def plans_placement(gpu):+ """Whether a discovered device's memory is qualified to drive placement.++ Discovery is a fact; a placement budget is a policy decision, and the two+ are not the same thing. ``free_bytes`` normally carries both, because a+ discrete card's free VRAM *is* the budget. It is ``None`` for a device that+ exists and is worth reporting but whose free memory has not been qualified+ as a Colibri budget -- today, a Windows AMD part found through hipInfo,+ where the runtime's free figure describes the same physical pages the host+ RAM tier is already counting.++ ``None`` is deliberately distinct from ``0``. Zero is a measurement ("the+ card is full") and keeps every behaviour it has always had. ``None`` says+ "not measured in a way this planner may spend", which is a different claim+ and must not silently collapse into the other -- hence ``is not None``+ rather than a truthiness test.+ """+ return gpu.get("free_bytes") is not None++def _physical_cores_warn(message):"""Visibility for a mis-detected core count: a silent "1" here becomesOMP_NUM_THREADS=1 and pins the whole run to a single core (#325). Emit on@@ -566,7 +586,13 @@ def build_plan(model, ram_gb=0, context=4096, gpu_indices=None, vram_gb=0,wanted = set(gpu_indices)gpus = [gpu for gpu in gpus if gpu["index"] in wanted]- unified = any(gpu.get("unified_memory", False) for gpu in gpus)+ # Every discovered device is reported; only the ones whose free memory is a+ # qualified budget may steer placement. Keeping the two lists apart is what+ # stops "a GPU exists" from being read as "a GPU should be used" -- see+ # plans_placement().+ planning_gpus = [gpu for gpu in gpus if plans_placement(gpu)]++ unified = any(gpu.get("unified_memory", False) for gpu in planning_gpus)typical = info["typical_expert_bytes"]max_expert = info["max_expert_bytes"] or typicallayers = int(cfg.get("num_hidden_layers") or 0) + 1@@ -582,7 +608,7 @@ def build_plan(model, ram_gb=0, context=4096, gpu_indices=None, vram_gb=0,gpu_plan = []safe_vram = 0for gpu in gpus:- usable = max(0, gpu["free_bytes"] - reserve)+ usable = max(0, gpu["free_bytes"] - reserve) if plans_placement(gpu) else 0safe_vram += usablegpu_plan.append(dict(gpu, reserve_bytes=reserve, usable_bytes=usable))requested_vram = int(vram_gb * GB) if vram_gb > 0 else safe_vram@@ -623,8 +649,14 @@ def build_plan(model, ram_gb=0, context=4096, gpu_indices=None, vram_gb=0,warnings.append("RAM budget cannot hold one expert slot per sparse layer")if gpu_indices is not None and len(gpus) != len(set(gpu_indices)):warnings.append("one or more requested GPUs were not detected")- if gpus and vram_budget < requested_vram_before_clamp:+ if planning_gpus and vram_budget < requested_vram_before_clamp:warnings.append("VRAM tier was clamped by free VRAM, shared memory, or model expert size")+ for gpu in gpus:+ if not plans_placement(gpu):+ warnings.append(+ f"GPU {gpu['index']} ({gpu['name']}) was detected but its free memory is "+ "not qualified as a placement budget on this platform; it is reported "+ "only and drives no automatic tier")if unified:warnings.append("GPU and RAM share one physical memory pool; budgets were jointly constrained")@@ -633,9 +665,9 @@ def build_plan(model, ram_gb=0, context=4096, gpu_indices=None, vram_gb=0,# pessimistic hit rate that describe nothing. That is exactly when a user# reaches for `coli plan` -- before changing a live deployment -- so say so# rather than let the numbers be read as a capacity answer.- if gpus:- gpu_total = sum(g["total_bytes"] for g in gpus)- gpu_free = sum(g["free_bytes"] for g in gpus)+ if planning_gpus:+ gpu_total = sum(g["total_bytes"] for g in planning_gpus)+ gpu_free = sum(g["free_bytes"] for g in planning_gpus)if gpu_total and gpu_free < 0.75 * gpu_total:warnings.append(f"{format_bytes(gpu_total - gpu_free)} of VRAM is already in use "@@ -652,11 +684,11 @@ def build_plan(model, ram_gb=0, context=4096, gpu_indices=None, vram_gb=0,if cold_bytes:bottleneck = "disk expert misses"bottleneck_class = "disk"- elif warm_bytes and gpus:+ elif warm_bytes and planning_gpus:bottleneck = "CPU expert tail and GPU compute"bottleneck_class = "mixed"elif projected_hit >= 0.99:- if gpus:+ if planning_gpus:bottleneck = "GPU compute and interconnect"else:bottleneck = "CPU expert compute (fully resident)"@@ -665,7 +697,7 @@ def build_plan(model, ram_gb=0, context=4096, gpu_indices=None, vram_gb=0,bottleneck = "CPU expert compute and RAM bandwidth"bottleneck_class = "memory"- tune = _auto_tune(bottleneck_class, projected_hit, gpus, cpu_sockets,+ tune = _auto_tune(bottleneck_class, projected_hit, planning_gpus, cpu_sockets,plan_has_metal=False)probe_state, probe_gbs = ssd_probe_state(info["path"])@@ -736,7 +768,9 @@ def environment_for_plan(plan, env=None, cuda_enabled=True):result.setdefault("RAM_GB", f"{ram['budget_bytes'] / GB:.3f}")vram = plan["tiers"]["vram"]- devices = [device["index"] for device in vram["devices"]]+ # Report every device, but only name the placement-qualified ones to the+ # engine: COLI_GPU/COLI_GPUS is an instruction, not an inventory.+ devices = [device["index"] for device in vram["devices"] if plans_placement(device)]if not cuda_enabled or not devices or vram["budget_bytes"] <= 0:return resultif result.get("COLI_CUDA", "1") == "0":@@ -770,11 +804,16 @@ def format_plan(plan):f"cap {tiers['ram']['cache_slots_per_layer']}/layer"]vram = tiers["vram"]if vram["devices"]:- names = ", ".join(f"{gpu['index']}:{gpu['name']}" for gpu in vram["devices"])+ names = ", ".join(+ f"{gpu['index']}:{gpu['name']}"+ + ("" if plans_placement(gpu) else " (identity only)")+ for gpu in vram["devices"])lines.append(f"VRAM {format_bytes(vram['budget_bytes'])} hot tier · "f"~{vram['expert_capacity']} experts · {names}")else:- lines.append("VRAM no NVIDIA device detected · CPU path")+ # Backend-neutral, matching the accelerator wording #903 settled on:+ # an AMD or Intel host that finds nothing is not "no NVIDIA device".+ lines.append("VRAM no supported GPU detected · CPU path")if plan.get("ssd_probe_gbs") is not None:lines.append(f"ssd {plan['ssd_probe_gbs']:.1f} GB/s F_NOCACHE (cached probe, #379)")elif plan.get("ssd_probe_state") in SSD_PROBE_PENDING:diff --git a/c/tests/test_resource_plan.py b/c/tests/test_resource_plan.pyindex 7e27aaf8..a576ecfd 100644--- a/c/tests/test_resource_plan.py+++ b/c/tests/test_resource_plan.py@@ -225,6 +225,111 @@ class ResourcePlanTest(unittest.TestCase):devices = _discover_nvidia_gpus()self.assertTrue(devices[0]["unified_memory"])+ # --- identity-only devices -------------------------------------------+ # A device can be discovered without its free memory being qualified as a+ # Colibri placement budget. That is the state of a Windows AMD device found+ # through hipInfo: the runtime may well report a free figure, but on an+ # integrated part it describes the same physical pages the host RAM tier is+ # already counting. Until a later slice qualifies that relationship, such a+ # device carries free_bytes=None -- "unknown for planning", which is NOT the+ # same claim as free_bytes=0 ("measured, and none is free").++ def _identity_only_gpu(self):+ return {"index": 0, "name": "AMD Radeon(TM) 8060S Graphics",+ "total_bytes": 78 * GB, "free_bytes": None,+ "unified_memory": True}++ def test_identity_only_gpu_is_planned_without_a_free_memory_value(self):+ plan = build_plan(self.model, ram_gb=16, available_memory=32 * GB,+ available_disk=1, gpus=[self._identity_only_gpu()],+ physical_cpus=8, cpu_sockets=1)+ # It is still reported: the hardware exists and the user should see it.+ names = [gpu["name"] for gpu in plan["tiers"]["vram"]["devices"]]+ self.assertIn("AMD Radeon(TM) 8060S Graphics", names)+ text = format_plan(plan)+ self.assertIn("8060S", text)+ # ...and the reader is told why it earns no tier, rather than being+ # left to read "0.0 GB hot tier" as "the card is full".+ self.assertIn("identity only", text)+ self.assertTrue(any("not qualified as a placement budget" in warning+ for warning in plan["warnings"]))+ # But it buys no tier.+ self.assertEqual(plan["tiers"]["vram"]["budget_bytes"], 0)+ self.assertEqual(plan["tiers"]["vram"]["expert_capacity"], 0)++ def test_plan_wording_is_backend_neutral_without_a_gpu(self):+ plan = build_plan(self.model, ram_gb=16, available_memory=32 * GB,+ available_disk=1, gpus=[], physical_cpus=8,+ cpu_sockets=1)+ text = format_plan(plan)+ self.assertIn("no supported GPU detected", text)+ self.assertNotIn("NVIDIA", text)++ def test_identity_only_gpu_decides_nothing_a_cpu_only_host_would_not(self):+ gpu_plan = build_plan(self.model, ram_gb=16, available_memory=32 * GB,+ available_disk=1, gpus=[self._identity_only_gpu()],+ physical_cpus=8, cpu_sockets=1)+ cpu_plan = build_plan(self.model, ram_gb=16, available_memory=32 * GB,+ available_disk=1, gpus=[], physical_cpus=8,+ cpu_sockets=1)+ # Presence alone must not reclassify the bottleneck or move DRAFT.+ self.assertEqual(gpu_plan["bottleneck_class"], cpu_plan["bottleneck_class"])+ self.assertNotEqual(gpu_plan["bottleneck_class"], "mixed")+ self.assertEqual(gpu_plan["tune"].get("DRAFT"), cpu_plan["tune"].get("DRAFT"))+ # Nor may it switch on the resident pipeline.+ self.assertNotIn("COLI_CUDA_PIPE", gpu_plan["tune"])+ # The strongest statement of the contract: for the same inputs, the+ # recommended environment is byte-identical to the CPU-only host's.+ # PIN_GB=all may legitimately appear in BOTH -- that is the no-GPU+ # residency advice (_auto_tune, `not has_gpu`), not a VRAM-derived+ # budget -- so equality is the assertion, not absence.+ env = environment_for_plan(gpu_plan, {"PIN": "stats.txt"})+ cpu_env = environment_for_plan(cpu_plan, {"PIN": "stats.txt"})+ self.assertEqual(env, cpu_env)+ self.assertNotIn("COLI_CUDA_PIPE", env)+ self.assertNotIn("COLI_CUDA", env)+ self.assertNotIn("COLI_GPU", env)+ self.assertNotIn("CUDA_EXPERT_GB", env)+ self.assertNotEqual(env.get("PIN_GB"), f"{gpu_plan['tiers']['vram']['budget_bytes'] / GB:.3f}")++ def test_identity_only_gpu_does_not_claim_vram_is_in_use(self):+ # The "already in use" warning divides free by total. With no qualified+ # free value there is nothing to divide, and telling the user to stop a+ # running engine would be a fabricated diagnosis.+ plan = build_plan(self.model, ram_gb=16, available_memory=32 * GB,+ available_disk=1, gpus=[self._identity_only_gpu()],+ physical_cpus=8, cpu_sockets=1)+ self.assertFalse(any("already in use" in warning+ for warning in plan["warnings"]))++ def test_measured_zero_free_memory_still_plans_as_before(self):+ # free_bytes=0 is a MEASUREMENT, not the unqualified state, and keeps+ # every behaviour it had: the tier is empty because the card is full,+ # the pipeline knob is still offered, and the in-use warning still fires.+ gpus = [{"index": 0, "name": "full-gpu", "total_bytes": 12 * GB,+ "free_bytes": 0}]+ plan = build_plan(self.model, ram_gb=16, available_memory=32 * GB,+ available_disk=1, gpus=gpus, physical_cpus=8,+ cpu_sockets=1)+ self.assertEqual(plan["tiers"]["vram"]["budget_bytes"], 0)+ self.assertEqual(plan["tune"]["COLI_CUDA_PIPE"]["value"], "1")+ self.assertTrue(any("already in use" in warning+ for warning in plan["warnings"]))++ def test_mixed_fleet_plans_only_the_qualified_device(self):+ gpus = [self._identity_only_gpu(),+ {"index": 1, "name": "discrete", "total_bytes": 12 * GB,+ "free_bytes": 10 * GB}]+ plan = build_plan(self.model, ram_gb=16, available_memory=32 * GB,+ available_disk=1, gpus=gpus, physical_cpus=8,+ cpu_sockets=1)+ env = environment_for_plan(plan)+ # The qualified card earns a tier; the unqualified one must not be+ # named to the engine as a placement target.+ self.assertGreater(plan["tiers"]["vram"]["budget_bytes"], 0)+ self.assertEqual(env.get("COLI_GPU"), "1")+ self.assertNotIn("COLI_GPUS", env)+def test_auto_tier_thread_count_uses_physical_cores(self):# End-to-end for #325: build_plan + environment_for_plan must export the# physical (not logical SMT) core count as OMP_NUM_THREADS. The original