Files
colibri-strix/c/tests/test_resource_plan.py
T
ZacharyZcR cbd599024e Unify continuous batching + heterogeneous runtime: decode batching, physical-core planning, disjoint VRAM/RAM placement, topp-policy warning (CPU-validated, CUDA on 6x5090) (#68)
* Fuse CUDA expert MLP execution

* Group CUDA expert transfers by device

* Instrument grouped CUDA expert execution

* Bound grouped CUDA decode scratch

* Execute expert groups across GPUs in parallel

* Release host backing for multi-GPU experts

* Define quality-preserving memory policies

* Overlap cold expert loading with resident compute

* Adapt expert placement with session LFRU

* Fuse q4 expert gate and up dispatch

* Plan CPU work on physical cores

* Batch grouped expert CUDA kernels

* Separate VRAM and RAM expert placement

* Add ragged multi-sequence decode forward

* feat(runtime): add continuous decode scheduler

* Route concurrent API requests through batch scheduler

* Harden multiplex request lifecycle and framing

* Cancel disconnected multiplex requests

* Bind API port before starting the engine

* fix automatic KV slot allocation

* add native int4 Tensor Core grouped GEMM

* add Tensor Core throughput benchmark

* optimize packed int4 low-row kernels

* add asynchronous CUDA staging streams

* document validated six-GPU dense acceleration

* tune six-GPU expert hot set

* raise validated expert hot-set target

* add CUDA MLA absorption core

* fuse grouped expert gate and up projections

* Warn for explicit lossy routing flags
2026-07-13 14:30:36 +02:00

150 lines
6.8 KiB
Python

import json
import struct
import subprocess
import sys
import tempfile
import unittest
from pathlib import Path
from resource_plan import GB, analyze_model, build_plan, environment_for_plan, format_plan
def write_shard(path, tensors):
offset = 0
header = {}
payload = b""
for name, size in tensors:
header[name] = {"dtype": "U8", "shape": [size], "data_offsets": [offset, offset + size]}
payload += b"\0" * size
offset += size
raw = json.dumps(header).encode()
path.write_bytes(struct.pack("<Q", len(raw)) + raw + payload)
class ResourcePlanTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.model = Path(self.tmp.name)
(self.model / "config.json").write_text(json.dumps({
"num_hidden_layers": 2,
"n_routed_experts": 2,
"kv_lora_rank": 4,
"qk_rope_head_dim": 2,
"qk_nope_head_dim": 3,
"v_head_dim": 5,
"num_attention_heads": 2,
}))
write_shard(self.model / "model.safetensors", [
("model.embed_tokens.weight", 100),
("model.layers.0.self_attn.q_a_proj.weight", 200),
("model.layers.1.mlp.experts.0.gate_proj.weight", 30),
("model.layers.1.mlp.experts.0.up_proj.weight", 30),
("model.layers.1.mlp.experts.1.gate_proj.weight", 30),
("model.layers.1.mlp.experts.1.up_proj.weight", 30),
])
def tearDown(self):
self.tmp.cleanup()
def test_analyzes_dense_and_expert_storage(self):
info = analyze_model(self.model)
self.assertEqual(info["dense_bytes"], 300)
self.assertEqual(info["expert_bytes"], 120)
self.assertEqual(info["expert_count"], 2)
self.assertEqual(info["per_cap_bytes"], 60)
def test_builds_bounded_three_tier_plan(self):
gpus = [{"index": 0, "name": "test-gpu", "total_bytes": 12 * GB,
"free_bytes": 10 * GB}]
plan = build_plan(self.model, ram_gb=16, context=32, vram_gb=20,
available_memory=32 * GB, available_disk=100 * GB, gpus=gpus,
physical_cpus=24)
self.assertEqual(plan["version"], 2)
self.assertEqual(plan["policy"]["name"], "quality")
self.assertEqual(plan["cpu"]["physical_cores"], 24)
self.assertTrue(plan["policy"]["preserve_quantization"])
self.assertFalse(plan["tiers"]["vram"]["requires_host_backing"])
self.assertEqual(plan["tiers"]["ram"]["budget_bytes"], 16 * GB)
self.assertLessEqual(plan["tiers"]["vram"]["budget_bytes"], 8 * GB)
self.assertIn("clamped", plan["warnings"][0])
self.assertIn("0:test-gpu", format_plan(plan))
def test_filters_requested_devices(self):
gpus = [{"index": 0, "name": "a", "total_bytes": 8 * GB, "free_bytes": 8 * GB}]
plan = build_plan(self.model, available_memory=16 * GB, available_disk=1,
gpus=gpus, gpu_indices=[1])
self.assertEqual(plan["tiers"]["vram"]["devices"], [])
self.assertIn("not detected", plan["warnings"][0])
def test_cli_emits_versioned_json(self):
cli = Path(__file__).parents[1] / "coli"
run = subprocess.run([
sys.executable, str(cli), "plan", "--model", str(self.model),
"--gpu", "none", "--json",
], text=True, capture_output=True, check=True)
plan = json.loads(run.stdout)
self.assertEqual(plan["version"], 2)
self.assertEqual(plan["model"]["expert_count"], 2)
def test_applies_plan_without_overriding_explicit_settings(self):
gpus = [
{"index": 0, "name": "a", "total_bytes": 12 * GB, "free_bytes": 10 * GB},
{"index": 1, "name": "b", "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)
env = environment_for_plan(plan, {"RAM_GB": "12", "PIN": "stats.txt",
"COLI_GPUS": "1"})
self.assertEqual(env["RAM_GB"], "12")
self.assertEqual(env["COLI_CUDA"], "1")
self.assertEqual(env["COLI_GPUS"], "1")
self.assertEqual(env["OMP_NUM_THREADS"], str(plan["cpu"]["physical_cores"]))
self.assertEqual(env["OMP_PROC_BIND"], "spread")
self.assertEqual(env["OMP_PLACES"], "cores")
self.assertEqual(env["PIN_GB"], env["CUDA_EXPERT_GB"])
explicit_threads = environment_for_plan(plan, {"OMP_NUM_THREADS": "7",
"OMP_PROC_BIND": "close"})
self.assertEqual(explicit_threads["OMP_NUM_THREADS"], "7")
self.assertEqual(explicit_threads["OMP_PROC_BIND"], "close")
def test_cpu_binary_does_not_apply_gpu_tier(self):
plan = build_plan(self.model, available_memory=16 * GB, available_disk=1,
gpus=[{"index": 0, "name": "a", "total_bytes": 8 * GB,
"free_bytes": 8 * GB}])
env = environment_for_plan(plan, cuda_enabled=False)
self.assertIn("RAM_GB", env)
self.assertNotIn("COLI_CUDA", env)
disabled = environment_for_plan(plan, {"COLI_CUDA": "0"}, cuda_enabled=True)
self.assertNotIn("COLI_GPU", disabled)
self.assertNotIn("CUDA_EXPERT_GB", disabled)
def test_rejects_unknown_policy_and_marks_experimental_policy(self):
with self.assertRaisesRegex(ValueError, "unknown policy"):
build_plan(self.model, available_memory=16 * GB, available_disk=1,
gpus=[], policy="fast-ish")
plan = build_plan(self.model, available_memory=16 * GB, available_disk=1,
gpus=[], policy="experimental-fast")
self.assertFalse(plan["policy"]["quality_preserving"])
self.assertFalse(plan["policy"]["preserve_router"])
def test_balanced_policy_enables_lossless_live_repin(self):
plan = build_plan(self.model, available_memory=16 * GB, available_disk=1,
gpus=[], policy="balanced")
env = environment_for_plan(plan)
self.assertEqual(env["COLI_POLICY"], "balanced")
self.assertEqual(env["REPIN"], "64")
explicit = environment_for_plan(plan, {"REPIN": "0"})
self.assertEqual(explicit["REPIN"], "0")
def test_plan_explains_hot_warm_and_cold_placement(self):
plan = build_plan(self.model, ram_gb=4, vram_gb=0,
available_memory=4 * GB, available_disk=1, gpus=[])
self.assertEqual([item["target"] for item in plan["decisions"]],
["VRAM", "RAM", "Disk"])
self.assertIn("quality-preserving yes", format_plan(plan))
self.assertIn("expected_bottleneck", plan)
if __name__ == "__main__":
unittest.main()