This commit is contained in:
2026-03-29 05:47:30 +08:00
parent dd5afbe6d0
commit 23909c205e
5 changed files with 46 additions and 7 deletions
+27 -1
View File
@@ -29,6 +29,27 @@ def _to_float(value: Any, default: float) -> float:
return default
def _to_str(value: Any, default: str = "") -> str:
return str(value).strip() if value is not None else default
def _join_posix(base_path: str, suffix_path: str) -> str:
return f"{base_path.rstrip('/')}/{suffix_path.lstrip('/')}"
def _resolve_profile_model_path(profile: dict[str, Any], model_root: str, model_key: str) -> str:
local_path = _to_str(profile.get("local_path"))
if not local_path:
raise ValueError(f"model profile '{model_key}' must provide local_path")
if "://" in local_path:
raise ValueError(f"model profile '{model_key}' local_path must be local filesystem path")
if local_path.startswith("/"):
return local_path
if not model_root:
raise ValueError("config.json model_root cannot be empty when local_path is relative")
return _join_posix(model_root, local_path)
def load_catalog(catalog_path: str = "config.json") -> dict[str, Any]:
content = json.loads(Path(catalog_path).read_text(encoding="utf-8"))
if not isinstance(content, dict):
@@ -50,6 +71,8 @@ def resolve_runtime_settings(content: dict[str, Any]) -> dict[str, Any]:
"tensor_parallel_size": _to_int(content.get("tensor_parallel_size"), 2),
"dtype": str(content.get("dtype", "bfloat16")),
"revision": str(content.get("revision", "")).strip() or None,
"model_root": _to_str(content.get("model_root"), "/opt/model"),
"offline_mode": True,
"model_key": str(models.get("selected", "")).strip() or None,
}
@@ -66,6 +89,7 @@ def resolve_model_profile(
profile = profiles[model_key]
if not isinstance(profile, dict):
raise ValueError(f"model profile '{model_key}' must be a JSON object")
model_root = _to_str(content.get("model_root"), "/opt/model")
valid_tp_raw = profile.get("valid_tp", [])
valid_tp = [_to_int(item, 0) for item in valid_tp_raw if _to_int(item, 0) > 0]
resolved_tp = requested_tp
@@ -73,7 +97,7 @@ def resolve_model_profile(
resolved_tp = valid_tp[0]
updates = {
"selected_model": model_key,
"model_name": profile.get("hf_model_id", model_key),
"model_name": _resolve_profile_model_path(profile, model_root, model_key),
"served_model_name": profile.get("served_model_name", model_key),
"max_model_len": _to_int(profile.get("ctx"), 8192),
"max_num_seqs": _to_int(profile.get("max_num_seqs"), 64),
@@ -86,4 +110,6 @@ def resolve_model_profile(
"enable_auto_tool_choice": _to_bool(profile.get("enable_auto_tool_choice"), False),
}
env_vars = {str(k): str(v) for k, v in dict(profile.get("env", {})).items()}
env_vars["HF_HUB_OFFLINE"] = "1"
env_vars["TRANSFORMERS_OFFLINE"] = "1"
return model_key, updates, env_vars