镜像与构建 - 六阶段 Dockerfile(base/wheels/deps/manifest/core|guard),core 不含 torch - 构建期自证:gen_manifest --strict 拦未登记模块,core 阶段重算 sha256 - compose 四服务 host 网络(llm.url 强制回环),config.core/local 可挂载覆盖 无 docker 环境的四段链路 - pull_oci_image.py 直拉 OCI 镜像 rootfs(纯标准库,走加速器) - replay_image_build.sh 按 stage chroot 重放 Dockerfile - run_image_chroot.sh 等价 docker run(uid 10001 + 入口 + host 网络) - pack_image.py 封成 docker save 格式包,自带 diff_id 校验 交付 - make_package.sh 生成自包含目录:镜像包 + 溯源清单 + 构建输入 + 脚本 + 源码快照 - 文档:从零复现指南、docker 打包方案、镜像构建限制与替代方案、交付包 README 命名 - Qwen 品牌名统一替换为 large_model / small_model / large_model_guard, 覆盖 vLLM 服务名、模型目录、配置、脚本、测试与文档 - 模型架构字段(architectures / model_type / tokenizer_class)属上游定义, 改名会导致加载失败,保留原值;check_local_model.py 的架构判断同理 验证:smoke 6/6;test.py 20 通过 0 失败 1 警告;gen --strict 67 文件 / 22 进镜像
151 lines
5.6 KiB
Python
151 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
||
"""部署前自检:本地守卫模型与 config.yaml 是否对得上。
|
||
|
||
python tools/check_local_model.py [--config config.yaml]
|
||
|
||
只读检查,不加载模型、不发请求。覆盖三类会让宿主启动失败的问题:
|
||
1. llm 段的 file 不是完整的 HuggingFace 目录(宿主直接 LLMUnavailable)
|
||
2. 插件的 llm.allow 与 llm 段 id 不一致(LLMRegistry 构造时 KeyError)
|
||
3. torch / transformers 缺失(llm_worker 导入即失败)
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import json
|
||
import pathlib
|
||
import sys
|
||
|
||
import yaml
|
||
|
||
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
||
|
||
WEIGHT_SUFFIXES = (".safetensors", ".bin", ".pt")
|
||
TOKENIZER_FILES = ("tokenizer.json", "tokenizer.model", "vocab.json", "spiece.model")
|
||
|
||
|
||
def fail(label: str, detail: str = "") -> bool:
|
||
print(f" FAIL {label}{(' -> ' + detail) if detail else ''}")
|
||
return False
|
||
|
||
|
||
def ok(label: str, detail: str = "") -> bool:
|
||
print(f" PASS {label}{(' -> ' + detail) if detail else ''}")
|
||
return True
|
||
|
||
|
||
def check_model_dir(path: pathlib.Path) -> bool:
|
||
if not path.is_dir():
|
||
return fail(f"模型目录 {path}", "不存在")
|
||
passed = ok(f"模型目录 {path}")
|
||
|
||
config_json = path / "config.json"
|
||
passed &= ok("config.json 存在") if config_json.is_file() else fail("config.json 缺失")
|
||
if config_json.is_file():
|
||
# 宿主按守卫模型的 Safety/Categories 文本解析,换别的 guard 模型会全部 503
|
||
try:
|
||
cfg = json.loads(config_json.read_text(encoding="utf-8"))
|
||
except (OSError, ValueError):
|
||
cfg = {}
|
||
model_type = str(cfg.get("model_type", ""))
|
||
# model_type 取自上游 config.json,是架构标识而非品牌名:改了模型就加载不了
|
||
if model_type.lower().startswith("qwen"):
|
||
passed &= ok("model_type", model_type)
|
||
else:
|
||
print(f" WARN model_type={model_type or '?'};宿主解析器按守卫模型的输出格式工作,"
|
||
"其它 guard 模型会解析失败")
|
||
|
||
weights = [p for p in path.iterdir() if p.suffix in WEIGHT_SUFFIXES]
|
||
if weights:
|
||
total = sum(p.stat().st_size for p in weights)
|
||
passed &= ok("权重文件", f"{len(weights)} 个,共 {total / 2**30:.1f} GiB")
|
||
else:
|
||
passed &= fail("权重文件缺失", "需要 " + " / ".join(WEIGHT_SUFFIXES))
|
||
|
||
tokenizer = [name for name in TOKENIZER_FILES if (path / name).is_file()]
|
||
if tokenizer:
|
||
passed &= ok("tokenizer 文件", ", ".join(tokenizer))
|
||
else:
|
||
passed &= fail("tokenizer 文件缺失", "需要 " + " / ".join(TOKENIZER_FILES))
|
||
|
||
# chat template 决定 prompt 怎么包进模型;缺失时 apply_chat_template 会失败
|
||
if (path / "tokenizer_config.json").is_file():
|
||
passed &= ok("tokenizer_config.json 存在")
|
||
else:
|
||
passed &= fail("tokenizer_config.json 缺失", "apply_chat_template 需要它")
|
||
return passed
|
||
|
||
|
||
def main() -> int:
|
||
parser = argparse.ArgumentParser()
|
||
parser.add_argument("--config", default=str(ROOT / "config.yaml"))
|
||
args = parser.parse_args()
|
||
|
||
config_path = pathlib.Path(args.config)
|
||
print(f"配置文件: {config_path}")
|
||
if not config_path.is_file():
|
||
fail("config.yaml 存在")
|
||
return 1
|
||
|
||
settings = yaml.safe_load(config_path.read_text(encoding="utf-8")) or {}
|
||
print("\n[1] llm 段")
|
||
models = settings.get("llm") or []
|
||
if not models:
|
||
print(" FAIL 没有 llm 段:插件调 host.llm 会必然失败")
|
||
return 1
|
||
passed = True
|
||
model_ids = set()
|
||
for model in models:
|
||
model_id = model.get("id", "")
|
||
model_ids.add(model_id)
|
||
print(f" - id={model_id} type={model.get('type')} file={model.get('file')}")
|
||
if model.get("type") != "guard":
|
||
passed &= fail(f"{model_id} type 必须是 guard", str(model.get("type")))
|
||
target = pathlib.Path(model.get("file", ""))
|
||
if not target.is_absolute():
|
||
target = (config_path.parent / target).resolve()
|
||
passed &= check_model_dir(target)
|
||
|
||
print("\n[2] 插件 llm.allow 与 llm 段一致性")
|
||
plugins = settings.get("plugins") or []
|
||
if not plugins:
|
||
fail("plugins 段非空")
|
||
return 1
|
||
enabled = [p for p in plugins if p.get("enabled", True)]
|
||
print(f" 启用插件: {[p.get('id') for p in enabled] or '(无)'}")
|
||
for plugin in enabled:
|
||
allowed = ((plugin.get("llm") or {}).get("allow")) or []
|
||
if not allowed:
|
||
continue
|
||
for model_id in allowed:
|
||
if model_id in model_ids:
|
||
passed &= ok(f"{plugin.get('id')} → {model_id}")
|
||
else:
|
||
passed &= fail(f"{plugin.get('id')} 引用了未定义的模型 {model_id}")
|
||
|
||
print("\n[3] 推理依赖")
|
||
for module in ("torch", "transformers", "tokenizers", "safetensors"):
|
||
try:
|
||
__import__(module)
|
||
passed &= ok(f"import {module}")
|
||
except ImportError:
|
||
passed &= fail(f"import {module}", "缺失,llm_worker 会 ModelLoadFailed")
|
||
|
||
print("\n[4] 设备")
|
||
try:
|
||
import torch
|
||
|
||
if torch.cuda.is_available():
|
||
print(f" PASS cuda 可用,{torch.cuda.device_count()} 张卡")
|
||
else:
|
||
print(" WARN cuda 不可用;load.device=cpu 可用但慢,cuda 需改配置")
|
||
except ImportError:
|
||
print(" WARN 无法判断(torch 未安装)")
|
||
|
||
print("\n结论:", "可以启动" if passed else "存在阻塞项,宿主会起不来")
|
||
return 0 if passed else 1
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|