fix(模型): 显示加载失败阶段和依赖诊断
模型子进程返回固定的加载阶段、异常类型和已知依赖名称,主进程在启动日志及异常中展示诊断信息,不输出原始异常文本。保留加载失败时阻止就绪与子进程清理行为。 新增六个加载阶段、异常信息过滤与进程清理测试;相关测试共 28 项通过,Ruff 检查通过。
This commit is contained in:
1 parent
5d71803b02
commit
eab1af6c5f
4 files changed
+150
-5
No files matched your search
+28
-2
@@ -14,6 +14,30 @@ from .config import LLMConfig, Settings
|
||||
|
||||
logger = logging.getLogger("middleware.audit")
|
||||
|
||||
# Only fixed diagnostic labels may cross the startup IPC boundary or enter logs.
|
||||
MODEL_LOAD_STAGES = {
|
||||
"import_torch", "import_transformers", "load_tokenizer", "load_model",
|
||||
"move_to_device", "eval_model",
|
||||
}
|
||||
MODEL_LOAD_ERRORS = {
|
||||
"ModuleNotFoundError", "ImportError", "FileNotFoundError", "PermissionError",
|
||||
"OSError", "ValueError", "TypeError", "RuntimeError", "MemoryError",
|
||||
"SafetensorError", "OutOfMemoryError",
|
||||
}
|
||||
MODEL_DEPENDENCIES = {"torch", "transformers", "tokenizers", "safetensors", "numpy", "accelerate"}
|
||||
|
||||
|
||||
def model_load_diagnostic(result: dict[str, Any]) -> str:
|
||||
labels = []
|
||||
for key, allowed in (
|
||||
("stage", MODEL_LOAD_STAGES),
|
||||
("exception", MODEL_LOAD_ERRORS),
|
||||
("dependency", MODEL_DEPENDENCIES),
|
||||
):
|
||||
value = result.get(key)
|
||||
labels.append(f"{key}={value if isinstance(value, str) and value in allowed else 'unknown'}")
|
||||
return " ".join(labels)
|
||||
|
||||
|
||||
class LLMFailure(Exception):
|
||||
"""A deliberately non-sensitive LLM capability failure."""
|
||||
@@ -149,7 +173,7 @@ class GuardAdapter:
|
||||
from .llm_worker import guard_worker_main
|
||||
|
||||
if not self.spec.file.is_dir():
|
||||
raise LLMUnavailable()
|
||||
raise LLMUnavailable("ModelDirectoryMissing")
|
||||
parent, child = self.context.Pipe(duplex=True)
|
||||
process = self.context.Process(
|
||||
target=guard_worker_main,
|
||||
@@ -177,7 +201,9 @@ class GuardAdapter:
|
||||
)
|
||||
result = json.loads(response)
|
||||
if not result.get("ok"):
|
||||
raise LLMUnavailable()
|
||||
diagnostic = model_load_diagnostic(result)
|
||||
logger.error("model_load_failed model=%s %s", self.spec.id, diagnostic)
|
||||
raise LLMUnavailable(f"ModelLoadFailed {diagnostic}")
|
||||
except BaseException as exc:
|
||||
await self.stop()
|
||||
if isinstance(exc, (LLMFailure, asyncio.CancelledError)):
|
||||
|
||||
Reference in new issue
Block a user