Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

feat(planner): the partial chain's layouts of deepseek_v41 and kimi_k3

JustVugg8d88c8576105
diff --git a/c/resource_plan.py b/c/resource_plan.py
index 3d1460ab..9cfdab60 100644
--- a/c/resource_plan.py
+++ b/c/resource_plan.py
@@ -976,6 +976,203 @@ def _glm53_chain_layout(info, env, vulkan):
_VK_CHAIN_LAYOUT["glm53"] = _glm53_chain_layout
+def _vk_rows(env, default):
+ """vkc_fit_rows(default): COLI_VK_CHAIN_ROWS when a number, else the engine's block."""
+ value = (env.get("COLI_VK_CHAIN_ROWS") or "").strip()
+ if not value or value == "auto":
+ return default
+ match = re.match(r"\s*([+-]?\d+)", value)
+ rows = int(match[1]) if match else 0
+ return min(max(rows, 1), 65535)
+
+
+def _v41_chain_layout(info, env, vulkan):
+ """deepseek_v41_chain.h: v41c_layer_bytes, v41c_fixed_bytes, v41c_tail_bytes, with the
+ device-only placement's extra matrices when COLI_VK_DENSE_HOST may drop the host copies
+ (v41c_fit_now runs at v41_dho_open then)."""
+ root = info.get("config") or {}
+ c = root.get("text_config") or root
+ try:
+ L = int(c.get("n_layers") or c["num_hidden_layers"])
+ D = int(c.get("dim") or c["hidden_size"])
+ nh = int(c.get("n_heads") or c["num_attention_heads"])
+ hd, QL, ol = int(c["head_dim"]), int(c["q_lora_rank"]), int(c["o_lora_rank"])
+ og = int(c.get("o_groups") or 1)
+ inter = int(c.get("moe_inter_dim") or c["moe_intermediate_size"])
+ rd = int(c.get("rope_head_dim") or c["qk_rope_head_dim"])
+ W = int(c.get("window_size") or c.get("sliding_window") or 128)
+ ratios = [int(r) for r in c["compress_ratios"][:L]]
+ except (KeyError, TypeError, ValueError):
+ return None
+ if L < 1 or len(ratios) < L or og < 1:
+ return None
+ H = int(c.get("hc_mult") or 1)
+ IH0, ID0 = int(c.get("index_n_heads") or 0), int(c.get("index_head_dim") or 0)
+ K = max(int(c.get("index_topk") or 0), 0)
+ kv_source = set(c.get("kv_source_layers") or c.get("kv_source_layer_ids") or [])
+ index_source = set(c.get("index_source_layers") or c.get("index_source_layer_ids") or [])
+ targets = len(c.get("dspark_target_layer_ids") or [])
+ maxpos = int(c.get("max_seq_len") or c.get("max_position_embeddings") or 4096)
+ match = re.match(r"\s*([+-]?\d+)", env.get("CTX") or "")
+ if match and 2 <= int(match[1]) < maxpos:
+ maxpos = int(match[1])
+ nm, hr, HD = (2 + H) * H, 2 * H + H * H, H * D
+ # the engram tables (a sidecar the scan does not read): the layers whose wkv is there
+ engram = {}
+ for tensor in info.get("dense_tensors", []):
+ name = _text_weight_name(tensor["name"])
+ found = re.fullmatch(r"layers\.(\d+)\.engram\.wkv\.weight", name)
+ if found and isinstance(tensor.get("shape"), list) and len(tensor["shape"]) == 2:
+ engram[int(found[1])] = tensor["shape"]
+ host = _vk_flag(env, "COLI_VK_DENSE_HOST")
+ dho = host is None or host == 0
+ prefill = _vk_flag(env, "COLI_VK_CHAIN") == 2
+ views = prefill and dho
+ rows = _vk_rows(env, 512)
+ ring = _vk_rows(env, 512)
+
+ def t(fmt, columns, out):
+ return vk_tensor_bytes(fmt, columns, out, 32 if fmt == 12 else 0)
+ layers = []
+ for i in range(L):
+ r = ratios[i]
+ b = 2 * t(10, HD, nm) + t(12, D, QL) + t(12, QL, nh * hd) + t(12, D, hd)
+ b += t(12, nh * hd // og, og * ol) + (og * t(12, nh * hd // og, ol) if views else 0)
+ b += t(12, og * ol, D) + 2 * t(12, D, inter) + t(12, inter, D)
+ if i in kv_source:
+ b += t(11, D, hd) + (t(11, D, hd) if r > 1 else 0) + t(11, hd, ID0)
+ if i in index_source and (r > 0 or dho):
+ b += t(12, QL, IH0 * ID0) + t(11, D, IH0)
+ if i in engram:
+ b += t(12, engram[i][1], engram[i][0])
+ b += vk_buf_bytes((W + ring) * hd * 4)
+ floats = 2 * D + QL + hd + nh + 2 * (3 + nm)
+ if i in kv_source and r > 0:
+ first = min(64, max(maxpos // r, 1))
+ if r > 1:
+ b += vk_buf_bytes(2 * r * hd * 4)
+ b += vk_buf_bytes(first * hd * 4) + vk_buf_bytes(first * ID0 * 4)
+ floats += hd + ID0 + (rows // r) * (hd + ID0) + (2 * r * hd if r > 1 else 0)
+ if i in engram:
+ floats += 2 * H * D
+ floats += min(rows, W) * hd
+ layers.append(b + 4 * floats)
+ # v41c_scratch's counting pass at `rows` rows, the context of the window and them
+ IH, ID = IH0 if IH0 > 0 else 1, ID0 if ID0 > 0 else 1
+ rmax = max([1] + [ratios[i] for i in kv_source if i < L])
+ ew = next(iter(engram.values()))[1] if engram else 1
+ T = targets if targets > 0 else 1
+ wcap, n = W + rows, rows
+ counts = [n * HD, n * HD, n * nm, n * hr, n * hr, n * D, n * D, n * QL, n * QL, n * nh * hd, n * hd,
+ n * nh * hd, n * og * ol, n * D, n * hd, n * hd, n * IH * ID, n * IH, n * wcap, n * wcap,
+ n * (W + K), (n + rmax) * 2 * rd, n * ew, n * D * (H + 1), n * inter, n * inter, n * inter,
+ n * D, n * D, n * T * D, n * HD, n * hr, n * D]
+ fixed = sum(4 * (x if x else 1) for x in counts)
+ _, dense = _vk_chain_dense("deepseek_v41", env, vulkan)
+ tail = 0
+ if dense:
+ tail = t(11, D, int(c.get("vocab_size") or 0)) + L * t(11, D, int(c.get("n_routed_experts") or 0))
+ if prefill and not dho:
+ tail += L * og * t(12, nh * hd // og, ol)
+ return VkChainLayout(layers, fixed, tail)
+
+
+def _k3_vk_fmt(tensors, name, out, columns, bits, explicit):
+ """kimi_k3.c w_load as the device takes the result (k3_vk_fmt): (fmt, group)."""
+ tensor = tensors.get(name)
+ if tensor is not None and tensor["dtype"] == "U8": # a repacked container: its own bits
+ if tensor["size"] == out * columns:
+ return (4, 64) if explicit and bits == 4 and columns % 64 == 0 else (1, 0)
+ return (4, 64)
+ if bits >= 32:
+ return (10, 0)
+ if bits <= 4 and columns % 64 == 0:
+ return (4, 64)
+ return (1, 0)
+
+
+def _k3_chain_layout(info, env, vulkan):
+ """kimi_k3_chain.h: k3c_layer_bytes, k3c_fixed_bytes, the head as the tail."""
+ root = info.get("config") or {}
+ c = root.get("text_config") or root
+ la = c.get("linear_attn_config") or {}
+ try:
+ L, D, vocab = int(c["num_hidden_layers"]), int(c["hidden_size"]), int(c["vocab_size"])
+ first_dense, dense_inter = int(c["first_k_dense_replace"]), int(c["intermediate_size"])
+ nh, QL, KL = int(c["num_attention_heads"]), int(c["q_lora_rank"]), int(c["kv_lora_rank"])
+ nope, R, V = int(c["qk_nope_head_dim"]), int(c["qk_rope_head_dim"]), int(c["v_head_dim"])
+ E, inter, LT = int(c["num_experts"]), int(c["moe_intermediate_size"]), int(c["routed_expert_hidden_size"])
+ shared, res_bs = int(c["num_shared_experts"]), int(c["attn_res_block_size"])
+ heads, hd, conv = int(la["num_heads"]), int(la["head_dim"]), int(la["short_conv_kernel_size"])
+ kda = {int(v) - 1 for v in la["kda_layers"]}
+ except (KeyError, TypeError, ValueError):
+ return None
+ if L < 1 or res_bs < 1:
+ return None
+ P = heads * hd
+
+ def number(name, default):
+ match = re.match(r"\s*([+-]?\d+)", env.get(name) or "")
+ return int(match[1]) if match else default
+ explicit = (env.get("K3_BITS") is not None)
+ bits, mbits, hbits = number("K3_BITS", 4), number("K3_MLA_BITS", 8), number("K3_HEAD_BITS", 8)
+ tensors = {_text_weight_name(tensor["name"]): tensor for tensor in info.get("dense_tensors", [])}
+
+ def w(name, out, columns, b):
+ fmt, group = _k3_vk_fmt(tensors, name, out, columns, b, explicit)
+ return vk_tensor_bytes(fmt, columns, out, group)
+ rows = _vk_rows(env, 256)
+ layers = []
+ for i in range(L):
+ p = f"model.layers.{i}."
+ b, floats = 0, 4 * D
+ if i in kda:
+ for part in ("q", "k", "v", "g"):
+ b += w(p + f"self_attn.{part}_proj.weight", P, D, bits)
+ b += w(p + "self_attn.o_proj.weight", D, P, bits)
+ b += vk_tensor_bytes(10, D, hd) + vk_tensor_bytes(10, hd, P) + vk_tensor_bytes(10, D, heads)
+ b += vk_buf_bytes(3 * P * conv * 4) + vk_buf_bytes(heads * hd * hd * 4)
+ floats += 3 * P * conv + heads + P + hd
+ else:
+ b += (w(p + "self_attn.q_a_proj.weight", QL, D, mbits) +
+ w(p + "self_attn.q_b_proj.weight", nh * (nope + R), QL, mbits) +
+ w(p + "self_attn.kv_a_proj_with_mqa.weight", KL + R, D, mbits) +
+ w(p + "self_attn.kv_b_proj.weight", nh * (nope + V), KL, mbits) +
+ w(p + "self_attn.o_proj.weight", D, nh * V, mbits) +
+ w(p + "self_attn.g_proj.weight", nh * V, D, mbits))
+ b += vk_buf_bytes(rows * KL * 4) + (vk_buf_bytes(rows * R * 4) if R > 0 else 0)
+ floats += QL + KL + rows * (KL + R)
+ if i >= first_dense:
+ moe, si = p + "block_sparse_moe.", inter * shared
+ b += vk_tensor_bytes(10, D, E)
+ b += (w(moe + "routed_expert_down_proj.weight", LT, D, bits) + w(moe + "routed_expert_up_proj.weight", D, LT, bits) +
+ w(moe + "shared_experts.gate_proj.weight", si, D, bits) + w(moe + "shared_experts.up_proj.weight", si, D, bits) +
+ w(moe + "shared_experts.down_proj.weight", D, si, bits))
+ floats += LT
+ else:
+ b += (w(p + "mlp.gate_proj.weight", dense_inter, D, bits) + w(p + "mlp.up_proj.weight", dense_inter, D, bits) +
+ w(p + "mlp.down_proj.weight", D, dense_inter, bits))
+ layers.append(b + 4 * floats)
+ # k3c_scratch's counting pass at `rows` rows, the MLA scratch as k3c_chunk_rows counts
+ # it, and the output mix's parameters
+ nbmax = (L + res_bs - 1) // res_bs
+ MI = max(inter * shared, dense_inter)
+ n = rows
+ counts = [n * D, n * nbmax * D, n * D, n * D, n * D, 3 * n * P, n * 3 * P, n * hd, n * P, n * heads, n * P, n * P,
+ n * nh * V, n * E, n * LT, n * MI, n * MI, n * MI, n * D, n * LT, n * D, n * D, n * E, n * LT,
+ n * (KL + R), n * D, vocab, n * LT]
+ fixed = sum(4 * (x if x else 1) for x in counts) + (4 * n * R if R > 0 else 0)
+ if any(i not in kda for i in range(L)):
+ fixed += 4 * n * (QL + nh * (nope + R) + (KL + R) + 2 * nh * KL + nh * V)
+ fixed += 8 * D
+ tail = w("lm_head.weight", vocab, D, hbits)
+ return VkChainLayout(layers, fixed, tail)
+
+
+_VK_CHAIN_LAYOUT["deepseek_v41"] = _v41_chain_layout
+_VK_CHAIN_LAYOUT["kimi"] = _k3_chain_layout
+
+
def _analysis_signature(shards, config_path):
parts = [f"v{_ANALYSIS_CACHE_VERSION}"]
st = config_path.stat()
diff --git a/c/tests/test_resource_plan.py b/c/tests/test_resource_plan.py
index 18c51de9..f1cf5460 100644
--- a/c/tests/test_resource_plan.py
+++ b/c/tests/test_resource_plan.py
@@ -2290,6 +2290,201 @@ class VulkanPartialChainGlm53Test(unittest.TestCase):
self.assertEqual(none["tiers"]["ram"]["vk_chain_layers"]["on_device"], 0)
+class VulkanPartialChainDskTest(unittest.TestCase):
+ """The partial chain's layouts of DeepSeek V4.1 Flash (deepseek_v41) and Kimi K3
+ (kimi): resource_plan.vk_chain_fit counts each layer, the fixed bytes and the tail as
+ the engine's fit prints them for its tiny fixture (tools/make_dsv41_tiny.py,
+ tools/make_kimi_k3_tiny.py), and the plan credits only the N layers' host copies."""
+
+ V41 = {
+ "architectures": ["DeepseekV41ForCausalLM"], "model_type": "deepseek_v41",
+ "text_config": {
+ "vocab_size": 256, "dim": 128, "moe_inter_dim": 64, "n_layers": 6, "n_heads": 4, "head_dim": 64,
+ "rope_head_dim": 16, "q_lora_rank": 64, "o_lora_rank": 32, "o_groups": 2, "n_routed_experts": 8,
+ "n_shared_experts": 1, "n_activated_experts": 2, "window_size": 8, "compress_ratios": [0, 2, 2, 1, 1, 0],
+ "kv_source_layers": [1, 3], "index_source_layers": [1, 3, 4], "index_n_heads": 4, "index_head_dim": 32,
+ "index_topk": 4, "candidate_source_layer": 3, "candidate_topk_blocks": 2, "candidate_block_size": 2,
+ "hc_mult": 4, "engram_layer_ids": [1, 4], "dspark_block_size": 3, "dspark_target_layer_ids": [3, 4, 5],
+ "max_position_embeddings": 256, "model_type": "deepseek_v41_text", "hidden_size": 128,
+ "num_hidden_layers": 6, "num_attention_heads": 4, "moe_intermediate_size": 64, "num_experts_per_tok": 2,
+ "sliding_window": 8, "rope_theta": 10000.0, "original_seq_len": 32},
+ }
+ K3 = {
+ "model_type": "kimi_linear", "architectures": ["KimiLinearForCausalLM"], "hidden_size": 128,
+ "num_hidden_layers": 6, "vocab_size": 320, "first_k_dense_replace": 2, "intermediate_size": 64,
+ "num_attention_heads": 4, "num_key_value_heads": 4, "q_lora_rank": 32, "kv_lora_rank": 32,
+ "qk_nope_head_dim": 16, "qk_rope_head_dim": 8, "v_head_dim": 16, "num_experts": 8,
+ "num_experts_per_token": 2, "moe_intermediate_size": 32, "routed_expert_hidden_size": 32,
+ "num_shared_experts": 1, "attn_res_block_size": 2, "max_position_embeddings": 256,
+ "linear_attn_config": {"num_heads": 2, "head_dim": 16, "short_conv_kernel_size": 4,
+ "kda_layers": [1, 3], "full_attn_layers": [2, 4, 5, 6]},
+ }
+
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory()
+ self.model = Path(self.tmp.name)
+
+ def tearDown(self):
+ self.tmp.cleanup()
+
+ def write(self, config, tensors):
+ (self.model / "config.json").write_text(json.dumps(config))
+ write_shard(self.model / "model.safetensors", tensors)
+ return analyze_model(self.model)
+
+ def write_v41(self):
+ """The tiny V4.1 fixture's trunk: every matrix the chain or the device-only
+ placement takes, the router, the norms, the experts."""
+ t = []
+ width = {"F8_E4M3": 1, "BF16": 2, "F32": 4, "I8": 1, "U8": 1}
+
+ def add(name, dtype, *shape):
+ n = 1
+ for d in shape:
+ n *= d
+ t.append((name, n * width[dtype], dtype, list(shape)))
+ add("embed.weight", "BF16", 256, 128)
+ add("head.weight", "BF16", 256, 128)
+ for i, r in enumerate([0, 2, 2, 1, 1, 0]):
+ p = f"layers.{i}."
+ for name, rows, cols in (("attn.wq_a", 64, 128), ("attn.wq_b", 256, 64), ("attn.wkv", 64, 128),
+ ("attn.wo_a", 64, 128), ("attn.wo_b", 128, 64), ("ffn.shared_experts.w1", 64, 128),
+ ("ffn.shared_experts.w3", 64, 128), ("ffn.shared_experts.w2", 128, 64)):
+ add(p + name + ".weight", "F8_E4M3", rows, cols)
+ add(p + "ffn.gate.weight", "BF16", 8, 128)
+ add(p + "attn_norm.weight", "BF16", 128)
+ add(p + "hc_attn_fn", "F32", 24, 512)
+ if i in (1, 3):
+ add(p + "attn.compressor.wkv.weight", "BF16", 64, 128)
+ if r > 1:
+ add(p + "attn.compressor.wgate.weight", "BF16", 64, 128)
+ add(p + "attn.indexer.wk.weight", "BF16", 32, 64)
+ if i in (1, 3, 4):
+ add(p + "attn.indexer.wq_b.weight", "F8_E4M3", 128, 64)
+ add(p + "attn.indexer.weights_proj.weight", "BF16", 4, 128)
+ if i in (1, 4):
+ add(p + "engram.wkv.weight", "F8_E4M3", 640, 192)
+ for e in range(8):
+ add(p + f"ffn.experts.{e}.w1.weight", "I8", 64, 64)
+ return self.write(self.V41, t)
+
+ def write_k3(self, dtype="F32"):
+ """The tiny Kimi K3 fixture's matrices (f32 as the generator writes them)."""
+ t = []
+ c = self.K3
+
+ def add(name, *shape):
+ n = 1
+ for d in shape:
+ n *= d
+ t.append((name, n * 4, dtype, list(shape)))
+ add("model.embed_tokens.weight", 320, 128)
+ add("lm_head.weight", 320, 128)
+ for i in range(6):
+ p = f"model.layers.{i}."
+ add(p + "input_layernorm.weight", 128)
+ if i in (0, 2):
+ for part in ("q", "k", "v", "g"):
+ add(p + f"self_attn.{part}_proj.weight", 32, 128)
+ add(p + "self_attn.o_proj.weight", 128, 32)
+ add(p + "self_attn.f_a_proj.weight", 16, 128)
+ add(p + "self_attn.f_b_proj.weight", 32, 16)
+ add(p + "self_attn.b_proj.weight", 2, 128)
+ else:
+ add(p + "self_attn.q_a_proj.weight", 32, 128)
+ add(p + "self_attn.q_b_proj.weight", 96, 32)
+ add(p + "self_attn.kv_a_proj_with_mqa.weight", 40, 128)
+ add(p + "self_attn.kv_b_proj.weight", 128, 32)
+ add(p + "self_attn.o_proj.weight", 128, 64)
+ add(p + "self_attn.g_proj.weight", 64, 128)
+ if i >= c["first_k_dense_replace"]:
+ m = p + "block_sparse_moe."
+ add(m + "gate.weight", 8, 128)
+ add(m + "routed_expert_down_proj.weight", 32, 128)
+ add(m + "routed_expert_up_proj.weight", 128, 32)
+ add(m + "shared_experts.gate_proj.weight", 32, 128)
+ add(m + "shared_experts.up_proj.weight", 32, 128)
+ add(m + "shared_experts.down_proj.weight", 128, 32)
+ else:
+ add(p + "mlp.gate_proj.weight", 64, 128)
+ add(p + "mlp.up_proj.weight", 64, 128)
+ add(p + "mlp.down_proj.weight", 128, 64)
+ return self.write(c, t)
+
+ def test_deepseek_v41_layout_is_the_engines(self):
+ # "[VK] deepseek_v41 chain fit: ... fixed ... (the engine's E B, ...), tail T B, layers
+ # b0 .. B" for dsv41_tiny on Lavapipe, per configuration
+ from resource_plan import vk_chain_fit, vk_fit_pools
+ info = self.write_v41()
+ cpu = {"type": "cpu", "budget_bytes": 64 * GB}
+ on = {"COLI_VULKAN": "1", "COLI_VK_CHAIN": "1"}
+ for env, layers, fixed, tail in (
+ (on, [318696, 634472, 318696, 571752, 471528, 318696], 12124416, 0),
+ (dict(on, COLI_VK_DENSE="1"), [318696, 634472, 318696, 571752, 471528, 318696], 12124416, 79616),
+ # prompts only, device only: wo_a per output group as well; chunks of 7 rows
+ (dict(on, COLI_VK_CHAIN="2", COLI_VK_DENSE_HOST="0", COLI_VK_CHAIN_ROWS="7"),
+ [198376, 417000, 198376, 257512, 351208, 198376], 137736, 0)):
+ with self.subTest(env=env):
+ fit = vk_chain_fit(info, "deepseek_v41", env, cpu)
+ self.assertEqual(fit["layers"], layers)
+ self.assertEqual(fit["fixed"] - vk_fit_pools(0), fixed)
+ self.assertEqual(fit["n"], 6)
+ self.assertEqual(fit["tail"], True)
+
+ def test_kimi_layout_is_the_engines(self):
+ # "[VK] kimi_k3 chain fit: ..." for kimi_k3_tiny on Lavapipe at each bit width: the
+ # matrices go up in the forms the loader made of them (int4-g64, int8 rows, f32)
+ from resource_plan import vk_chain_fit, vk_fit_pools
+ info = self.write_k3()
+ cpu = {"type": "cpu", "budget_bytes": 64 * GB}
+ on = {"COLI_VULKAN": "1", "COLI_VK_CHAIN": "1"}
+ for bits, layers, tail in (
+ ({}, [47048, 133120, 53832, 139904, 139904, 139904], 42240),
+ ({"K3_BITS": "32", "K3_MLA_BITS": "32", "K3_HEAD_BITS": "32"}, [201672, 315904, 190280, 304512, 304512, 304512], 164096),
+ ({"K3_BITS": "4", "K3_MLA_BITS": "4", "K3_HEAD_BITS": "4"}, [47048, 120832, 53832, 127616, 127616, 127616], 23040),
+ ({"K3_BITS": "8"}, [67016, 144896, 68168, 146048, 146048, 146048], 42240)):
+ with self.subTest(bits=bits):
+ fit = vk_chain_fit(info, "kimi", dict(on, **bits), cpu)
+ self.assertEqual(fit["layers"], layers)
+ self.assertEqual(fit["fixed"] - vk_fit_pools(0), 2715904)
+ self.assertEqual(vk_chain_fit(info, "kimi", dict(on, COLI_VK_CHAIN_LAYERS="6", **bits), cpu)["tail"], True)
+ # the head is the tail: a device with room for every layer but not it
+ room = (1 << 30) + fit["fixed"] + sum(layers)
+ short = vk_chain_fit(info, "kimi", dict(on, **bits), {"type": "discrete", "budget_bytes": room + tail - 1})
+ self.assertEqual((short["n"], short["tail"]), (6, False))
+
+ def credit(self, info, family, k):
+ from resource_plan import _vk_released_tensor_bytes, _vk_layer_index
+ return sum(_vk_released_tensor_bytes(t, family, {}) for t in info["dense_tensors"]
+ if (_vk_layer_index(t["name"]) if _vk_layer_index(t["name"]) is not None else 99) < k)
+
+ def test_device_only_credits_the_layers_on_the_device(self):
+ from resource_plan import vk_chain_fit
+ 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"}
+ big = {"type": "discrete", "budget_bytes": 64 * GB}
+ for family, write in (("deepseek_v41", self.write_v41), ("kimi", self.write_k3)):
+ info = write()
+ fit = vk_chain_fit(info, family, on, big)
+ layers, full_credit = fit["layers"], self.credit(info, family, 6)
+ self.assertGreater(full_credit, self.credit(info, family, 5))
+ full = build_plan(self.model, env=on, vulkan=big, **kwargs)["tiers"]["ram"]
+ self.assertEqual(full["vk_chain_layers"]["on_device"], 6)
+ for k in (1, 3, 5):
+ device = {"type": "discrete", "budget_bytes": fit["fixed"] + sum(layers[:k]) + layers[k] // 2}
+ with self.subTest(family=family, k=k):
+ self.assertEqual(vk_chain_fit(info, family, on, device)["n"], 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"], self.credit(info, family, k))
+ self.assertIn(f"the first {k} of 6 layers", format_plan(plan))
+ forced = build_plan(self.model, env=dict(on, COLI_VK_CHAIN_LAYERS=str(k)), vulkan=big, **kwargs)
+ self.assertEqual(forced["tiers"]["ram"]["dense_on_device_bytes"], self.credit(info, family, k))
+ none = build_plan(self.model, env=on, vulkan={"type": "discrete", "budget_bytes": 1 << 20}, **kwargs)["tiers"]["ram"]
+ self.assertEqual((none["vk_chain_layers"]["on_device"], none["dense_on_device_bytes"]), (0, 0))
+
+
class PhysicalCpuCountTest(unittest.TestCase):
"""Regression for #325: --auto-tier pinned decode to one core because
physical_cpu_count() silently returned 1.