Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
feat(planner): predict the partial chain's N and credit only its layers
JustVuggf1d2cdd68851
diff --git a/c/resource_plan.py b/c/resource_plan.pyindex df21cc43..440984f0 100644--- a/c/resource_plan.py+++ b/c/resource_plan.py@@ -1,6 +1,7 @@#!/usr/bin/env python3"""Hardware and model placement planning for colibri's disk/RAM/VRAM tiers."""+import collectionsimport jsonimport osimport platform@@ -132,13 +133,16 @@ def _vk_device(env, vulkan):return None-def _vk_dense_active(family_id, env, vulkan):+def _vk_chain_dense(family_id, env, vulkan):+ """(chain, dense): whether the engine runs its dense chain, and its per-matrix+ path, on the Vulkan device (coli_vk_chain_decide, coli_vk_dense_decide); both+ False when it does not open one."""if (family_id not in _VK_DENSE_HOST_FAMILIES ornot _vk_flag(env, "COLI_VULKAN") or _vk_flag(env, "COLI_CUDA")):- return False+ return False, Falsekind = (vulkan or {}).get("type")if kind not in ("discrete", "integrated", "cpu", "virtual", "other"):- return False+ return False, Falsetier = _vk_flag(env, "COLI_VK_TIER") != 0chain, dense = _vk_flag(env, "COLI_VK_CHAIN"), _vk_flag(env, "COLI_VK_DENSE")if chain is None:@@ -148,7 +152,11 @@ def _vk_dense_active(family_id, env, vulkan):# colibri.c passes default=0 to coli_vk_dense_decide; its chain is# independent, but disabling that chain does not enable per-matrix GEMVs.dense = family_id != "glm" and (kind not in ("integrated", "cpu") or not tier)- return bool(chain or dense)+ return bool(chain), bool(dense)+++def _vk_dense_active(family_id, env, vulkan):+ return any(_vk_chain_dense(family_id, env, vulkan))def _text_weight_name(name):@@ -362,6 +370,218 @@ def vk_dense_device_only(dense_bytes, family_id=None, env=None, vulkan=None):return False, None+# ---- the partial chain (docs/vulkan.md, "A partial chain") ------------------------------+# When the dense layers do not all fit the device, the chain takes the first N of them and+# the CPU the rest and the head; only the N layers' host copies can then be dropped. The+# engine decides N with vkc_fit (vk_chain.c) from what the device has free; this mirrors+# it, per family, from the scanned tensors and the config:+# _VK_CHAIN_LAYOUT[family_id](info, env) -> VkChainLayout(layers, fixed, tail) or None+# layers each layer's device bytes as the engine's fit counts them (its matrices,+# each range aligned, its state at its first size, its share of the+# parameters), fixed the engine's fixed bytes (the scratch of one prompt+# chunk, without the pools' granularity), tail what goes up only with every+# layer (the head, matrices the per-matrix path uploads as it meets them).+# Families without an entry keep N = L (the plan as before).+VkChainLayout = collections.namedtuple("VkChainLayout", "layers fixed tail")+_VK_CHAIN_LAYOUT = {}+_VK_ALIGN = 256 # coli_vk_buffer_alignment: a storage buffer's alignment, at least 256+++def _vk_up(value, align=_VK_ALIGN):+ return (value + align - 1) // align * align+++def vk_tensor_bytes(fmt, columns, rows, group=0):+ """vkc_fit_tensor: a resident tensor's rows at their padded stride and its scales,+ each range aligned (fmt as the backend numbers them: 10 f32, 11 bf16, 12 fp8 in+ groups, 1 int8 rows, 2 int4 rows, 4 int4 in groups, 7 MXFP4, 13 int8 in groups)."""+ row = {10: 4 * columns, 11: 2 * columns, 14: 2 * columns, 12: columns, 13: columns, 1: columns,+ 2: (columns + 1) // 2, 4: (columns + 1) // 2, 7: (columns + 1) // 2}[fmt]+ data = (row + 3) // 4 * 4 * rows+ if fmt in (10, 11, 14):+ scales = 1+ elif fmt in (4, 7, 12, 13):+ scales = rows * -(-columns // group)+ else:+ scales = rows+ return _vk_up(data or 4) + _vk_up(4 * scales)+++def vk_buf_bytes(value):+ """vkc_fit_buf: a chain buffer (vkc_buf) of `value` bytes."""+ return _vk_up((max(value, 4) + 3) // 4 * 4)+++def _vk_layer_index(name):+ match = re.match(r"(?:model\.)?layers\.(\d+)\.", _text_weight_name(name))+ return int(match[1]) if match else None+++def _vk_cap_bytes(env):+ value = (env.get("COLI_VK_DEVICE_CAP_MB") or "").strip()+ try:+ return max(0, int(float(value) * 1048576)) if value else 0+ except ValueError:+ return 0+++def _vk_block(cap, default):+ """coli_vk_block_bytes: a pool's block, smaller under COLI_VK_DEVICE_CAP_MB."""+ if not cap:+ return default+ block = 64 << 10+ while block < cap // 4096:+ block <<= 1+ return min(block, default)+++def vk_fit_pools(cap=0):+ """vkc_fit_pools: a weight block, a block of each chain pool, the frames' staging."""+ return _vk_block(cap, 256 << 20) + 3 * _vk_block(cap, 64 << 20) + 4 * (4 << 20)+++def _vk_free_bytes(env, vulkan):+ """coli_vk_free_bytes as a plan sees it: the cap (the process holds nothing yet),+ else the device's budget (or its device-local heap) less its heaps' usage."""+ cap = _vk_cap_bytes(env)+ if cap:+ return cap+ vulkan = vulkan or {}+ used = sum(heap.get("usage", 0) for heap in vulkan.get("heaps", []) if heap.get("device_local"))+ return max(0, (vulkan.get("budget_bytes") or vulkan.get("device_local_bytes") or 0) - used)+++def vk_chain_fit(info, family_id, env=None, vulkan=None):+ """The engine's vkc_fit for this model and device: {"n", "L", "tail", "forced",+ "free", "reserve", "fixed", "layers"}, or None when the chain does not run or the+ family has no layout (N = L then, as before)."""+ env = os.environ if env is None else env+ vulkan = _vk_device(env, vulkan)+ chain, _ = _vk_chain_dense(family_id, env, vulkan)+ layout_of = _VK_CHAIN_LAYOUT.get(family_id)+ layout = layout_of(info, env, vulkan) if chain and layout_of else None+ if layout is None:+ return None+ layers = list(layout.layers)+ count = len(layers)+ free = _vk_free_bytes(env, vulkan)+ try:+ reserve = max(0, int(float(env.get("COLI_VK_TIER_RESERVE_GB") or 1.0) * (1 << 30)))+ except ValueError:+ reserve = 1 << 30+ fixed = layout.fixed + vk_fit_pools(_vk_cap_bytes(env))+ room = max(0, free - reserve)+ forced = (env.get("COLI_VK_CHAIN_LAYERS") or "").strip()+ if forced and forced != "auto":+ match = re.match(r"\s*([+-]?\d+)", forced)+ n = min(max(int(match[1]) if match else 0, 0), count)+ tail = n == count and count > 0+ elif fixed + sum(layers) <= room:+ n, tail = count, fixed + sum(layers) + layout.tail <= room+ else:+ n, used = 0, fixed+ while n < count and used + layers[n] <= room:+ used += layers[n]+ n += 1+ tail = False+ return {"n": n, "L": count, "tail": tail, "forced": bool(forced and forced != "auto"),+ "free": free, "reserve": reserve, "fixed": fixed, "layers": layers}+++def _v4_chain_rows(env):+ """deepseek_v4_chain.h v4c_rows: the CPU's prefill block, COLI_VK_CHAIN_ROWS below it."""+ def number(name, default):+ match = re.match(r"\s*([+-]?\d+)", env.get(name) or "")+ return int(match[1]) if match else default+ block = number("V4_PREFILL_CHUNK", 128)+ if block < 1 or block > 128:+ block = 128+ value = env.get("COLI_VK_CHAIN_ROWS") or ""+ rows = number("COLI_VK_CHAIN_ROWS", block) if value.strip() and value.strip() != "auto" else block+ return min(max(rows, 1), block)+++def _v4_chain_layout(info, env, vulkan):+ """deepseek_v4_chain.h: v4c_layer_bytes, v4c_fixed_bytes, v4c_tail_bytes."""+ c = info.get("config") or {}+ try:+ L = int(c["num_hidden_layers"])+ ratios = [int(r) for r in c["compress_ratios"][:L]]+ D, H, nh, hd = c["hidden_size"], c.get("hc_mult", 1), c["num_attention_heads"], c["head_dim"]+ rd, QL, og, ol = c["qk_rope_head_dim"], c["q_lora_rank"], c.get("o_groups", 1), c["o_lora_rank"]+ W, inter = c["sliding_window"], c["moe_intermediate_size"]+ except (KeyError, TypeError, ValueError):+ return None+ if len(ratios) < L or L < 1:+ return None+ IH = c.get("index_n_heads") or 0+ IH = IH if IH > 0 else 1+ ID = c.get("index_head_dim") or 0+ ID = ID if ID > 0 else 2+ topk = c.get("index_topk") or 0+ HD, nm, hr = H * D, (2 + H) * H, 2 * H + H * H+ rows = _v4_chain_rows(env)+ layers = [0] * L+ for tensor in info.get("dense_tensors", []):+ layer, shape = _vk_layer_index(tensor["name"]), tensor.get("shape")+ if layer is None or layer >= L or not isinstance(shape, list) or len(shape) != 2:+ continue+ suffix = re.sub(r"^(?:model\.)?layers\.\d+\.", "", _text_weight_name(tensor["name"]))+ out, columns, parts, fmt = shape[0], shape[1], 1, 0+ if tensor["dtype"] == "F8_E4M3":+ fmt = 12+ if suffix == "attn.wo_a.weight":+ parts, out = og, ol+ elif tensor["dtype"] == "BF16" and (suffix.endswith(("compressor.wkv.weight", "compressor.wgate.weight"))+ or suffix == "attn.indexer.weights_proj.weight"):+ fmt = 11+ elif tensor["dtype"] == "F32" and suffix in ("hc_attn_fn", "hc_ffn_fn"):+ fmt = 10+ if fmt:+ layers[layer] += parts * vk_tensor_bytes(fmt, columns, out, 128 if fmt == 12 else 0)+ for i, r in enumerate(ratios):+ crows, cproj = (8 if r == 4 else r), (2 if r == 4 else 1) * hd+ state = vk_buf_bytes((W + rows) * hd * 4)+ if r > 0:+ state += vk_buf_bytes(2 * crows * cproj * 4) + vk_buf_bytes(64 * hd * 4)+ if r == 4:+ state += vk_buf_bytes(2 * 8 * 2 * ID * 4) + vk_buf_bytes(64 * ID * 4)+ floats = 2 * D + QL + hd + nh + 2 * (3 + nm)+ if r > 0:+ floats += hd + r * cproj+ if r == 4:+ floats += ID + 8 * ID+ floats += min(rows, W) * hd+ if r > 0:+ floats += (rows // r) * hd + 2 * (crows if r == 4 else min(rows, r)) * cproj+ if r == 4:+ floats += (rows // 4) * ID + 2 * 8 * 2 * ID+ layers[i] += state + 4 * floats+ # v4c_scratch's counting pass at `rows` rows, the context of the window and them+ E = W + rows+ K = max([topk if r == 4 else E // r if r > 0 else 0 for r in ratios] + [0])+ LR = W + (K if K > 0 else 1)+ kinds = 1 + len({r for r in ratios if r > 0 and r != 4})+ rmax = max([1] + ratios)+ taps = 3 if int(c.get("num_nextn_predict_layers") or 0) >= 3 else 1+ r_ = rows+ counts = [r_ * HD, r_ * HD, r_ * nm, r_ * hr, r_ * hr, r_ * D, r_ * D, r_ * D, r_ * QL, r_ * QL, r_ * QL,+ r_ * nh * hd, r_ * hd, r_ * nh * hd, r_ * og * ol, r_ * D, r_ * 2 * hd, r_ * 2 * hd,+ r_ * 2 * ID, r_ * 2 * ID, r_ * IH * ID, r_ * IH, r_ * max(E // 4, 1), kinds * r_ * LR,+ (2 * r_ + rmax) * rd, r_ * D, r_ * inter, r_ * inter, r_ * inter, r_ * D, r_ * D, r_ * D,+ r_ * HD, taps * r_ * HD]+ fixed = sum(4 * (n if n else 1) for n in counts)+ _, dense = _vk_chain_dense("deepseek_v4", env, vulkan)+ tail = 0+ if dense:+ tail = (vk_tensor_bytes(11, D, int(c.get("vocab_size") or 0)) ++ L * vk_tensor_bytes(11, D, int(c.get("n_routed_experts") or 0)))+ return VkChainLayout(layers, fixed, tail)+++_VK_CHAIN_LAYOUT["deepseek_v4"] = _v4_chain_layout++def _analysis_signature(shards, config_path):parts = [f"v{_ANALYSIS_CACHE_VERSION}"]st = config_path.stat()@@ -1829,18 +2049,36 @@ def build_plan(model, ram_gb=0, context=4096, gpu_indices=None, vram_gb=0,family_id = info["resolved_family"].descriptor.idkept = min(info["dense_bytes"], info.get("embed_bytes", 0))device_dense_bytes = max(0, info["dense_bytes"] - kept)- vk_dense, vk_dense_why = vk_dense_device_only(- device_dense_bytes, family_id, env_now, vulkan)+ # A partial chain (vk_chain_fit): only the first N layers' tensors go to the device;+ # the CPU's layers and the head keep their host copies.+ chain_fit = vk_chain_fit(info, family_id, env_now, vulkan)+ partial = chain_fit is not None and (chain_fit["n"] < chain_fit["L"] or not chain_fit["tail"])++ def on_device(tensor):+ if not partial:+ return True+ layer = _vk_layer_index(tensor["name"])+ return layer is not None and layer < chain_fit["n"]+ tensors_on_device = [tensor for tensor in info.get("dense_tensors", []) if on_device(tensor)]+ if partial:+ released = sum(_vk_released_tensor_bytes(tensor, family_id, env_now) for tensor in tensors_on_device)+ vk_dense, vk_dense_why = ((False, None) if chain_fit["n"] == 0 else+ vk_dense_device_only(released, family_id, env_now, vulkan))+ else:+ vk_dense, vk_dense_why = vk_dense_device_only(+ device_dense_bytes, family_id, env_now, vulkan)dense_on_device = 0if vk_dense:dense_on_device = min(device_dense_bytes, sum(_vk_released_tensor_bytes(tensor, family_id, env_now)- for tensor in info.get("dense_tensors", [])))+ for tensor in tensors_on_device))info = dict(info, dense_bytes=info["dense_bytes"] - dense_on_device)# On an integrated/software device the remaining device copy still consumes# physical RAM. Keeping host copies costs two copies; dropping them saves one,# not both. Price the device copy even when COLI_VK_DENSE_HOST=1.- shared_dense_bytes = (device_dense_bytes+ device_copy = (min(device_dense_bytes, sum(tensor["resident"] for tensor in tensors_on_device))+ if partial else device_dense_bytes)+ shared_dense_bytes = (device_copyif (vulkan or {}).get("type") in ("integrated", "cpu")and _vk_dense_active(family_id, env_now, vulkan) else 0)physical_cpus = physical_cpu_count() if physical_cpus is None else physical_cpus@@ -2078,6 +2316,9 @@ def build_plan(model, ram_gb=0, context=4096, gpu_indices=None, vram_gb=0,"budget_bytes": ram_budget, "dense_bytes": info["dense_bytes"],"dense_on_device_bytes": dense_on_device,"dense_on_device_reason": vk_dense_why if vk_dense else None,+ "vk_chain_layers": ({"on_device": chain_fit["n"], "layers": chain_fit["L"],+ "tail": chain_fit["tail"], "forced": chain_fit["forced"]}+ if chain_fit is not None else None),"shared_device_dense_bytes": shared_dense_bytes,"runtime_bytes": runtime_bytes,"expert_fixed_bytes": info["expert_fixed_bytes"],@@ -2199,7 +2440,11 @@ def format_plan(plan):f"{format_bytes(tiers['disk']['available_bytes'])} free",f"RAM {format_bytes(tiers['ram']['budget_bytes'])} budget · "f"{format_bytes(tiers['ram']['dense_bytes'])} dense"- + (f" (+{format_bytes(tiers['ram']['dense_on_device_bytes'])} on the Vulkan device only)"+ + (f" (+{format_bytes(tiers['ram']['dense_on_device_bytes'])} on the Vulkan device only"+ + (f", the first {tiers['ram']['vk_chain_layers']['on_device']} of "+ f"{tiers['ram']['vk_chain_layers']['layers']} layers"+ if (tiers['ram'].get('vk_chain_layers') or {}).get('on_device', 0)+ < (tiers['ram'].get('vk_chain_layers') or {}).get('layers', 0) else "") + ")"if tiers['ram'].get('dense_on_device_bytes') else "") + " · "f"{format_bytes(tiers['ram']['runtime_bytes'])} runtime · "f"{format_bytes(tiers['ram']['warm_expert_bytes'])} warm experts · "diff --git a/c/tests/test_resource_plan.py b/c/tests/test_resource_plan.pyindex 4c21d6e4..7c73b39a 100644--- a/c/tests/test_resource_plan.py+++ b/c/tests/test_resource_plan.py@@ -1574,6 +1574,202 @@ memInfo.free: 23.50 GB (97%)self.assertEqual(plan["next_actions"][0]["id"], "measure-kernels")+class VulkanPartialChainTest(unittest.TestCase):+ """The partial chain (docs/vulkan.md, "A partial chain"): resource_plan.vk_chain_fit+ predicts the engine's N with vkc_fit's rule from the device's budget, and the plan+ credits only the N layers' host copies."""++ CONFIG = {+ "architectures": ["DeepseekV4ForCausalLM"], "model_type": "deepseek_v4",+ "hidden_size": 128, "num_attention_heads": 4, "num_key_value_heads": 1, "head_dim": 32,+ "q_lora_rank": 128, "qk_rope_head_dim": 16, "o_groups": 1, "o_lora_rank": 128,+ "sliding_window": 8, "index_n_heads": 2, "index_head_dim": 32, "index_topk": 2,+ "n_routed_experts": 4, "num_experts_per_tok": 2, "n_shared_experts": 1,+ "moe_intermediate_size": 128, "num_hash_layers": 1, "num_nextn_predict_layers": 1,+ "hc_mult": 2, "hc_sinkhorn_iters": 3, "vocab_size": 128, "max_position_embeddings": 128,+ "rms_norm_eps": 1e-06, "hc_eps": 1e-06, "routed_scaling_factor": 1.5, "swiglu_limit": 10.0,+ "rope_theta": 10000.0, "compress_rope_theta": 40000.0,+ "rope_scaling": {"type": "yarn", "factor": 1.0, "original_max_position_embeddings": 128,+ "beta_fast": 32, "beta_slow": 1},+ }++ def setUp(self):+ self.tmp = tempfile.TemporaryDirectory()+ self.model = Path(self.tmp.name)++ def tearDown(self):+ self.tmp.cleanup()++ def write_v4(self, ratios):+ """The tiny DeepSeek V4 fixture's geometry (tools/make_deepseek_v4_tiny.py) with+ len(ratios) layers: every tensor coli_v4_layer_plan names, and its experts."""+ c = dict(self.CONFIG, num_hidden_layers=len(ratios), compress_ratios=list(ratios) + [0])+ (self.model / "config.json").write_text(json.dumps(c))+ width = {"F8_E4M3": 1, "F8_E8M0": 1, "BF16": 2, "F32": 4, "I64": 8, "I8": 1}+ tensors = []++ def add(name, dtype, *shape):+ count = 1+ for n in shape:+ count *= n+ tensors.append((name, count * width[dtype], dtype, list(shape)))++ def fp8(name, rows, cols):+ add(name + ".weight", "F8_E4M3", rows, cols)+ add(name + ".scale", "F8_E8M0", -(-rows // 128), -(-cols // 128))+ add("embed.weight", "BF16", 128, 128)+ add("head.weight", "BF16", 128, 128)+ add("norm.weight", "BF16", 128)+ add("hc_head_fn", "F32", 2, 256)+ for i, r in enumerate(ratios):+ p = f"layers.{i}."+ add(p + "attn.attn_sink", "F32", 4)+ add(p + "attn.kv_norm.weight", "BF16", 32)+ add(p + "attn.q_norm.weight", "BF16", 128)+ for name, rows, cols in (("wkv", 32, 128), ("wo_a", 128, 128), ("wo_b", 128, 128),+ ("wq_a", 128, 128), ("wq_b", 128, 128)):+ fp8(p + "attn." + name, rows, cols)+ add(p + "attn_norm.weight", "BF16", 128)+ if r:+ proj = (2 if r == 4 else 1) * 32+ add(p + "attn.compressor.ape", "F32", r, proj)+ add(p + "attn.compressor.norm.weight", "BF16", 32)+ add(p + "attn.compressor.wgate.weight", "BF16", proj, 128)+ add(p + "attn.compressor.wkv.weight", "BF16", proj, 128)+ if r == 4:+ add(p + "attn.indexer.compressor.ape", "F32", 4, 64)+ add(p + "attn.indexer.compressor.norm.weight", "BF16", 32)+ add(p + "attn.indexer.compressor.wgate.weight", "BF16", 64, 128)+ add(p + "attn.indexer.compressor.wkv.weight", "BF16", 64, 128)+ add(p + "attn.indexer.weights_proj.weight", "BF16", 2, 128)+ fp8(p + "attn.indexer.wq_b", 64, 128)+ add(p + "ffn.gate.weight", "BF16", 4, 128)+ if i == 0:+ add(p + "ffn.gate.tid2eid", "I64", 128, 2)+ else:+ add(p + "ffn.gate.bias", "F32", 4)+ for name in ("w1", "w2", "w3"):+ fp8(p + "ffn.shared_experts." + name, 128, 128)+ add(p + "ffn_norm.weight", "BF16", 128)+ for site in ("attn", "ffn"):+ add(p + f"hc_{site}_base", "F32", 8)+ add(p + f"hc_{site}_fn", "F32", 8, 256)+ add(p + f"hc_{site}_scale", "F32", 3)+ for e in range(4):+ for name in ("w1", "w2", "w3"):+ add(p + f"ffn.experts.{e}.{name}.scale", "F8_E8M0", 128, 4)+ add(p + f"ffn.experts.{e}.{name}.weight", "I8", 128, 64)+ write_shard(self.model / "model.safetensors", tensors)+ return analyze_model(self.model)++ def test_deepseek_v4_layout_is_the_engines(self):+ # The numbers the engine printed for tools/make_deepseek_v4_tiny.py's fixture on+ # Lavapipe ("[VK] deepseek_v4 chain fit: ... the engine's 1891840 B ..., layers+ # 159720 278760 192104 B"): the same per-layer bytes and fixed bytes here.+ from resource_plan import vk_chain_fit, vk_fit_pools+ info = self.write_v4([0, 4, 8])+ env = {"COLI_VULKAN": "1", "COLI_VK_CHAIN": "1"}+ fit = vk_chain_fit(info, "deepseek_v4", env, {"type": "cpu", "budget_bytes": 64 * GB})+ self.assertEqual(fit["layers"], [159720, 278760, 192104])+ self.assertEqual(fit["fixed"] - vk_fit_pools(0), 1891840)+ self.assertEqual(fit["n"], 3)+ # COLI_VK_CHAIN_ROWS lowers the chunk the fit counts (the window rings, the scratch)+ small = vk_chain_fit(info, "deepseek_v4", dict(env, COLI_VK_CHAIN_ROWS="3"), {"type": "cpu", "budget_bytes": 64 * GB})+ self.assertLess(small["fixed"], fit["fixed"])+ self.assertTrue(all(a < b for a, b in zip(small["layers"], fit["layers"])))++ def test_n_follows_the_engines_rule(self):+ from resource_plan import VkChainLayout, vk_chain_fit, vk_fit_pools+ MiB, GiB = 1 << 20, 1 << 30+ layout = VkChainLayout([100 * MiB, 200 * MiB, 300 * MiB, 400 * MiB], 50 * MiB, 70 * MiB)+ on = {"COLI_VULKAN": "1"}+ pools = vk_fit_pools(0)+ fixed = 50 * MiB + pools+ with mock.patch.dict("resource_plan._VK_CHAIN_LAYOUT", {"deepseek_v4": lambda info, env, vk: layout}):+ def fit(free, env=on, kind="discrete", heaps=()):+ return vk_chain_fit({}, "deepseek_v4", env, {"type": kind, "budget_bytes": free,+ "heaps": list(heaps)})+ room = GiB + fixed # free = reserve + fixed + what the layers may take+ self.assertEqual((fit(room + 1000 * MiB + 70 * MiB)["n"], fit(room + 1000 * MiB + 70 * MiB)["tail"]), (4, True))+ self.assertEqual((fit(room + 1000 * MiB)["n"], fit(room + 1000 * MiB)["tail"]), (4, False))+ self.assertEqual(fit(room + 1000 * MiB - 1)["n"], 3)+ self.assertEqual(fit(room + 600 * MiB)["n"], 3)+ self.assertEqual(fit(room + 300 * MiB - 1)["n"], 1)+ self.assertEqual(fit(room + 99 * MiB)["n"], 0)+ self.assertEqual(fit(GiB // 2)["n"], 0)+ # the heaps' usage is taken off the budget, as coli_vk_free_bytes does+ self.assertEqual(fit(room + 600 * MiB, heaps=[{"device_local": True, "usage": 300 * MiB}])["n"], 2)+ # the reserve is COLI_VK_TIER_RESERVE_GB+ self.assertEqual(fit(fixed + 600 * MiB, dict(on, COLI_VK_TIER_RESERVE_GB="0"))["n"], 3)+ # COLI_VK_CHAIN_LAYERS forces N, capped at L; the tail goes up with every layer+ for want, n, tail in (("2", 2, False), ("0", 0, False), ("9", 4, True), ("auto", 3, False)):+ got = fit(room + 600 * MiB, dict(on, COLI_VK_CHAIN_LAYERS=want))+ self.assertEqual((got["n"], got["tail"], got["forced"]), (n, tail, want != "auto"))+ # COLI_VK_DEVICE_CAP_MB: the device is the cap, with small pool blocks+ cap = 2 * GiB+ capped = fit(64 * GiB, dict(on, COLI_VK_DEVICE_CAP_MB=str(cap // MiB)))+ self.assertEqual(capped["free"], cap)+ self.assertLess(capped["fixed"], fixed)+ self.assertEqual(capped["n"], 3)+ # no chain, no fit: the plan as before+ self.assertIsNone(fit(64 * GiB, dict(on, COLI_VK_CHAIN="0")))+ self.assertIsNone(fit(64 * GiB, {}))++ def test_device_only_credits_the_layers_on_the_device(self):+ from resource_plan import vk_chain_fit, vk_fit_pools+ info = self.write_v4([0, 4, 8, 0, 0, 4])+ kwargs = dict(context=64, ram_gb=4, available_memory=16 * GB, available_disk=16 * GB, gpus=[])+ on = {"COLI_VULKAN": "1", "COLI_VK_CHAIN": "1", "COLI_VK_DENSE_HOST": "0",+ "COLI_VK_TIER_RESERVE_GB": "0"}+ layers = vk_chain_fit(info, "deepseek_v4", on, {"type": "discrete", "budget_bytes": 64 * GB})["layers"]+ # what the device can drop for the first k layers: fp8 matrices and the compressors'+ # bf16 projections (coli_v4_dense_device_only_tensor)+ def droppable(k):+ total = 0+ for t in info["dense_tensors"]:+ parts = t["name"].split(".")+ if parts[0] != "layers" or int(parts[1]) >= k or len(t["shape"]) != 2:+ continue+ if t["dtype"] == "F8_E4M3" or (t["dtype"] == "BF16" and t["name"].endswith(+ ("compressor.wkv.weight", "compressor.wgate.weight"))):+ total += t["size"]+ return total+ full = build_plan(self.model, env=on, vulkan={"type": "discrete", "budget_bytes": 64 * GB}, **kwargs)+ self.assertEqual(full["tiers"]["ram"]["dense_on_device_bytes"], droppable(6))+ self.assertEqual(full["tiers"]["ram"]["vk_chain_layers"]["on_device"], 6)+ for k in (1, 3, 5):+ free = vk_fit_pools(0) + vk_chain_fit(info, "deepseek_v4", on, {"type": "discrete", "budget_bytes": 64 * GB})["fixed"] \+ - vk_fit_pools(0) + sum(layers[:k]) + layers[k] // 2+ device = {"type": "discrete", "budget_bytes": free}+ with self.subTest(k=k):+ plan = build_plan(self.model, env=on, vulkan=device, **kwargs)+ ram = plan["tiers"]["ram"]+ self.assertEqual(ram["vk_chain_layers"]["on_device"], k)+ self.assertEqual(ram["dense_on_device_bytes"], droppable(k))+ self.assertEqual(full["tiers"]["ram"]["dense_bytes"] + droppable(6) - droppable(k), ram["dense_bytes"])+ self.assertIn(f"the first {k} of 6 layers", format_plan(plan))+ # forced, the same credit+ forced = build_plan(self.model, env=dict(on, COLI_VK_CHAIN_LAYERS=str(k)),+ vulkan={"type": "discrete", "budget_bytes": 64 * GB}, **kwargs)+ self.assertEqual(forced["tiers"]["ram"]["dense_on_device_bytes"], droppable(k))+ # no layer fits: nothing on the device, the host copies stay+ none = build_plan(self.model, env=on, vulkan={"type": "discrete", "budget_bytes": 1 << 20}, **kwargs)+ self.assertEqual(none["tiers"]["ram"]["dense_on_device_bytes"], 0)+ self.assertEqual(none["tiers"]["ram"]["vk_chain_layers"]["on_device"], 0)+ # host copies kept: no credit whatever N+ kept = build_plan(self.model, env=dict(on, COLI_VK_DENSE_HOST="1", COLI_VK_CHAIN_LAYERS="3"),+ vulkan={"type": "discrete", "budget_bytes": 64 * GB}, **kwargs)+ self.assertEqual(kept["tiers"]["ram"]["dense_on_device_bytes"], 0)+ # an integrated GPU's device copy (physical RAM, priced once) is the N layers' alone+ igpu = {"type": "integrated", "budget_bytes": 64 * GB}+ two = build_plan(self.model, env=dict(on, COLI_VK_CHAIN_LAYERS="2"), vulkan=igpu, **kwargs)["tiers"]["ram"]+ every = build_plan(self.model, env=on, vulkan=igpu, **kwargs)["tiers"]["ram"]+ self.assertEqual(two["dense_on_device_bytes"], droppable(2))+ self.assertEqual(every["dense_on_device_bytes"], droppable(6))+ self.assertGreaterEqual(two["shared_device_dense_bytes"], droppable(2))+ self.assertLess(two["shared_device_dense_bytes"], every["shared_device_dense_bytes"])++class PhysicalCpuCountTest(unittest.TestCase):"""Regression for #325: --auto-tier pinned decode to one core becausephysical_cpu_count() silently returned 1.