feat(v2): T2 运维层 hw_profile + llama_server 进程管理

This commit is contained in:
tzt
2026-08-30 21:00:52 +08:00
parent 1e51167ea5
commit 78a410b773
9 changed files with 851 additions and 1 deletions
+114
View File
@@ -0,0 +1,114 @@
"""T2 硬件档位检测单测(封闭,纯逻辑 + 注入 runner)。"""
import subprocess
import pytest
from runtime.hw_profile import (
TIER_SPECS,
detect,
nvidia_vram_gb,
pick_tier,
tier_spec,
vulkan_present,
)
class _FakeRun:
"""注入 subprocess runner:按命令返回预置输出。"""
def __init__(self, mapping):
self.mapping = mapping # {关键子串: CompletedProcess}
def __call__(self, cmd, timeout):
joined = " ".join(cmd)
for key, cp in self.mapping.items():
if key in joined:
return cp
raise FileNotFoundError(cmd)
def _cp(stdout="", rc=0):
return subprocess.CompletedProcess(args=[], returncode=rc, stdout=stdout, stderr="")
def test_pick_tier_thresholds():
assert pick_tier(None) == "cpu"
assert pick_tier(6.0) == "cpu"
assert pick_tier(8.0) == "gpu8"
assert pick_tier(11.9) == "gpu8"
assert pick_tier(12.0) == "gpu12"
assert pick_tier(24.0) == "gpu12"
def test_tier_specs_have_required_fields():
for tier, spec in TIER_SPECS.items():
assert spec["tier"] == tier
assert "ngl" in spec and "ctx" in spec and "kv_quant" in spec
# gpu12 ngl 全量卸载,cpu ngl 0
assert TIER_SPECS["gpu12"]["ngl"] == 99
assert TIER_SPECS["cpu"]["ngl"] == 0
def test_tier_spec_unknown_raises():
with pytest.raises(ValueError):
tier_spec("nonexistent")
def test_nvidia_vram_gb_parses():
fake = _FakeRun({"nvidia-smi": _cp("24576\n")})
assert nvidia_vram_gb(runner=fake) == 24.0
def test_nvidia_vram_gb_multi_gpu_takes_max():
fake = _FakeRun({"nvidia-smi": _cp("8192\n12288\n")})
assert nvidia_vram_gb(runner=fake) == 12.0
def test_nvidia_vram_gb_missing_tool_returns_none(monkeypatch):
import shutil
monkeypatch.setattr(shutil, "which", lambda name: None)
assert nvidia_vram_gb() is None
def test_vulkan_present_true():
fake = _FakeRun({"vulkaninfo": _cp("deviceName : NVIDIA GeForce RTX 4090")})
assert vulkan_present(runner=fake) is True
def test_vulkan_present_software_false():
fake = _FakeRun({"vulkaninfo": _cp("deviceName : llvmpipe (LLVM)")})
assert vulkan_present(runner=fake) is False
def test_detect_nvidia_gpu12(monkeypatch):
# 让 shutil.which 只对 nvidia-smi 生效
real_which = __import__("shutil").which
def fake_which(name):
return "C:/x/nvidia-smi.exe" if name == "nvidia-smi" else None
monkeypatch.setattr(__import__("shutil"), "which", fake_which)
fake = _FakeRun({"nvidia-smi": _cp("24576\n")})
spec = detect(runner=fake)
assert spec["tier"] == "gpu12"
assert spec["ngl"] == 99 and spec["ctx"] == 32768
assert spec["probe"] == "nvidia"
def test_detect_cpu_fallback(monkeypatch):
monkeypatch.setattr(__import__("shutil"), "which", lambda name: None)
spec = detect()
assert spec["tier"] == "cpu"
assert spec["ngl"] == 0
assert spec["probe"] == "cpu"
def test_detect_override_tier():
spec = detect(override={"tier": "gpu8"})
assert spec["tier"] == "gpu8"
assert spec["ngl"] == 14 and spec["ctx"] == 16384
assert spec["probe"] == "override"
def test_detect_override_field():
spec = detect(override={"tier": "cpu", "ctx": 16384})
assert spec["ctx"] == 16384
assert spec["tier"] == "cpu"