223 lines
10 KiB
Python
223 lines
10 KiB
Python
#!/usr/bin/env python3
|
|
"""权重适配: 训练镜像 convert 产物 -> SDK (llm_compression) 期望的 model_dir。
|
|
|
|
输入契约 (与训练镜像一致, 见 quant_design_v1.md §4/§5):
|
|
CHECKPOINT_PATH convert 产物 checkpoint_torch 目录 (含 model.safetensors + model_structure.json
|
|
+ paligemma_tokenizer.model + assets/<asset_id>/norm_stats.json)
|
|
CONTRACT_JSON checkpoint 缺 model_structure.json 时的契约兜底 (同 convert_runner 语义)
|
|
OUTPUT_DIR 必填, 产物根; 产出 OUTPUT_DIR/quant_input/model_dir/
|
|
MATERIALIZE =1 时将重映射后的 safetensors 实体化到 model_dir (默认仅符号链接)
|
|
|
|
键名核对以 vendor/llm_compression/models/pi05/pi05_model.py build_model 为唯一事实:
|
|
vision: paligemma_with_expert.paligemma.model.vision_tower.vision_model.*
|
|
+ paligemma_with_expert.paligemma.model.multi_modal_projector.*
|
|
lm: paligemma_with_expert.paligemma.model.language_model.*
|
|
+ paligemma_with_expert.paligemma.lm_head.weight (SDK 重绑为 embed_tokens)
|
|
expert: paligemma_with_expert.gemma_expert.*
|
|
+ 顶层 action_in_proj/action_out_proj/time_mlp_in/time_mlp_out (.weight/.bias)
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import json
|
|
import logging
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
import contract as _contract # noqa: E402 训练镜像同一份契约实现
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
|
logger = logging.getLogger("make_model_dir")
|
|
|
|
# vendor build_model 的键期望 (llm_compression pi05_model.py:74-113)
|
|
VIS_PREFIX = "paligemma_with_expert.paligemma.model.vision_tower.vision_model."
|
|
VIS_PROJ_PREFIX = "paligemma_with_expert.paligemma.model.multi_modal_projector."
|
|
LM_PREFIX = "paligemma_with_expert.paligemma.model.language_model."
|
|
LM_HEAD_KEY = "paligemma_with_expert.paligemma.lm_head.weight"
|
|
LM_EMBED_KEY = "paligemma_with_expert.paligemma.model.language_model.embed_tokens.weight"
|
|
EXP_PREFIX = "paligemma_with_expert.gemma_expert."
|
|
TOP_PROJ_KEYS = [f"{n}.{p}" for n in ("action_in_proj", "action_out_proj", "time_mlp_in", "time_mlp_out") for p in ("weight", "bias")]
|
|
QUANT_BUF_SUFFIXES = (".buf_scales", ".buf_zeros", ".buf_qmin", ".buf_qmax")
|
|
|
|
# pi05_base 固定架构事实, 抄自 vendor/llm_compression/models/pi05/model.py Pi05Config 类默认值 (:27-60)。
|
|
# 写全字段集是因为 tools/utils._validate_pi05_model 要求 config.json 含 vis_num_patches 并与
|
|
# vision_token_num 交叉校验; 下游 strict=True 加载会对形状再兜底。
|
|
PI05_BASE_ARCH = {
|
|
"pal_hidden": 2048, "pal_layers": 18, "pal_heads": 8, "pal_kv_heads": 1, "pal_head_dim": 256,
|
|
"pal_intermediate": 16384, "pal_vocab": 257152, "pal_norm_eps": 1e-6, "pal_rope_theta": 10000.0, "pal_max_pos": 8192,
|
|
"exp_hidden": 1024, "exp_layers": 18, "exp_heads": 8, "exp_kv_heads": 1, "exp_head_dim": 256,
|
|
"exp_intermediate": 4096, "exp_norm_eps": 1e-6, "exp_rope_theta": 10000.0, "exp_max_pos": 8192, "exp_adarms_cond": 1024,
|
|
"vis_hidden": 1152, "vis_layers": 27, "vis_heads": 16, "vis_head_dim": 72, "vis_intermediate": 4304,
|
|
"vis_patch": 14, "vis_image": 224, "vis_proj_dim": 2048, "vis_norm_eps": 1e-6, "vis_num_patches": 256,
|
|
}
|
|
|
|
|
|
def load_contract(ckpt_dir: Path) -> _contract.Contract:
|
|
structure = ckpt_dir / "model_structure.json"
|
|
if structure.is_file():
|
|
return _contract.Contract.from_model_structure(str(structure))
|
|
raw = os.environ.get("CONTRACT_JSON", "").strip()
|
|
if raw:
|
|
return _contract.Contract.from_json(raw)
|
|
sys.exit(f"[hyperparam] {structure} not found and CONTRACT_JSON not set")
|
|
|
|
|
|
def classify_keys(keys: list[str]) -> dict:
|
|
fam = {"vision": [], "vision_proj": [], "lm": [], "expert": [], "top_proj": [], "quant_buf": [], "other": []}
|
|
for k in keys:
|
|
if k.endswith(QUANT_BUF_SUFFIXES):
|
|
fam["quant_buf"].append(k)
|
|
elif k.startswith(VIS_PREFIX):
|
|
fam["vision"].append(k)
|
|
elif k.startswith(VIS_PROJ_PREFIX):
|
|
fam["vision_proj"].append(k)
|
|
elif k.startswith(LM_PREFIX):
|
|
fam["lm"].append(k)
|
|
elif k.startswith(EXP_PREFIX):
|
|
fam["expert"].append(k)
|
|
elif k in TOP_PROJ_KEYS:
|
|
fam["top_proj"].append(k)
|
|
elif k == LM_HEAD_KEY:
|
|
fam["lm"].append(k) # lm_head 计入 lm 族
|
|
else:
|
|
fam["other"].append(k)
|
|
return fam
|
|
|
|
|
|
def main() -> None:
|
|
t0 = time.time()
|
|
output_dir = Path(os.environ.get("OUTPUT_DIR", "").strip())
|
|
if not output_dir:
|
|
sys.exit("[hyperparam] OUTPUT_DIR not set")
|
|
ckpt_dir = Path(os.environ.get("CHECKPOINT_PATH", "").strip())
|
|
st = ckpt_dir / "model.safetensors"
|
|
if not st.is_file():
|
|
sys.exit(f"[hyperparam] CHECKPOINT_PATH 需为 convert 产物目录 (缺 model.safetensors): {ckpt_dir}")
|
|
|
|
ct = load_contract(ckpt_dir)
|
|
model_dir = output_dir / "quant_input" / "model_dir"
|
|
model_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
# ---- 1) 键名核对 ----
|
|
from safetensors import safe_open
|
|
|
|
keys: list[str] = []
|
|
dtypes: dict[str, str] = {}
|
|
with safe_open(str(st), framework="numpy") as f:
|
|
for k in f.keys():
|
|
keys.append(k)
|
|
dtypes[k] = str(f.get_slice(k).get_dtype())
|
|
fam = classify_keys(keys)
|
|
missing_top = [k for k in TOP_PROJ_KEYS if k not in keys]
|
|
lm_head_present = LM_HEAD_KEY in keys
|
|
report = {
|
|
"source_checkpoint": str(ckpt_dir),
|
|
"safetensors_keys": len(keys),
|
|
"families": {k: len(v) for k, v in fam.items()},
|
|
"missing_top_proj": missing_top,
|
|
"lm_head_key_present": lm_head_present,
|
|
"tied_lm_head_remap": None,
|
|
"unclassified_examples": fam["other"][:40],
|
|
"dtype_summary": sorted(set(dtypes.values())),
|
|
}
|
|
|
|
# ---- 1b) 绑定权重 (tied lm_head) 的确定性重映射 ----
|
|
materialize = os.environ.get("MATERIALIZE", "").strip().lower() in ("1", "true", "yes")
|
|
remaps: dict[str, str] = {}
|
|
if not lm_head_present:
|
|
if LM_EMBED_KEY not in keys:
|
|
sys.exit(f"[keys] lm_head 与 embed_tokens 均缺失, 无法重绑: {LM_HEAD_KEY}")
|
|
remaps[LM_HEAD_KEY] = LM_EMBED_KEY
|
|
if not materialize:
|
|
logger.warning("[keys] 需要实体化重映射 (lm_head<-embed_tokens), 自动启用 MATERIALIZE")
|
|
materialize = True
|
|
if not remaps and any(len(v) == 0 for v in (fam["vision"], fam["lm"], fam["expert"])):
|
|
for name in ("vision", "lm", "expert"):
|
|
if not fam[name]:
|
|
logger.error("[keys] 键族 %s 为空; other 样例: %s", name, fam["other"][:20])
|
|
sys.exit("[keys] 键族缺失且无自动重映射规则命中, 见上方报告")
|
|
|
|
# ---- 2) model.safetensors 落位 ----
|
|
dst_st = model_dir / "model.safetensors"
|
|
if remaps:
|
|
import numpy as np
|
|
from safetensors.numpy import save_file
|
|
|
|
logger.info("[remap] 实体化 safetensors 并注入重映射键: %s", remaps)
|
|
tensors: dict[str, "np.ndarray"] = {}
|
|
with safe_open(str(st), framework="numpy") as f:
|
|
for k in f.keys():
|
|
tensors[k] = f.get_tensor(k)
|
|
for dst, src in remaps.items():
|
|
tensors[dst] = tensors[src]
|
|
save_file(tensors, str(dst_st))
|
|
report["tied_lm_head_remap"] = {"added": LM_HEAD_KEY, "from": LM_EMBED_KEY}
|
|
else:
|
|
if dst_st.exists() or dst_st.is_symlink():
|
|
dst_st.unlink()
|
|
dst_st.symlink_to(st.resolve())
|
|
report["model_safetensors"] = {"path": str(dst_st), "materialized": bool(remaps)}
|
|
|
|
# ---- 3) config.json 从契约派生 (SDK Pi05Config 默认值 + 契约覆盖) ----
|
|
derived = {
|
|
"action_dim": ct.action_dim,
|
|
"action_horizon": ct.action_horizon,
|
|
"max_token_len": ct.max_token_len,
|
|
"num_views": len(ct.cameras),
|
|
"vision_token_num": 256,
|
|
}
|
|
cfg_out = dict(PI05_BASE_ARCH)
|
|
cfg_out.update(derived)
|
|
cfg_out["_meta"] = {
|
|
"generated_by": "quant_pipeline.make_model_dir",
|
|
"source_checkpoint": str(ckpt_dir),
|
|
"contract_sha256": hashlib.sha256(ct.to_json().encode()).hexdigest()[:16],
|
|
"created_at": time.strftime("%Y-%m-%d %H:%M:%S"),
|
|
}
|
|
(model_dir / "config.json").write_text(json.dumps(cfg_out, indent=2, ensure_ascii=False), encoding="utf-8")
|
|
report["config_json"] = cfg_out
|
|
|
|
# ---- 4) tokenizer 与 norm_stats ----
|
|
tok_src = ckpt_dir / "paligemma_tokenizer.model"
|
|
if tok_src.is_file():
|
|
shutil.copy2(tok_src, model_dir / "paligemma_tokenizer.model")
|
|
report["tokenizer"] = "copied"
|
|
else:
|
|
sys.exit(f"[hyperparam] tokenizer 缺失: {tok_src} (convert 侧量化交接物)")
|
|
|
|
# norm_stats 来源优先级: 显式 NORM_STATS_PATH (训练同名超参) > checkpoint_torch/assets > 源 checkpoint assets
|
|
ns_env = os.environ.get("NORM_STATS_PATH", "").strip()
|
|
ns_candidates: list[Path] = ([Path(ns_env)] if ns_env else []) + sorted((ckpt_dir / "assets").glob("*/norm_stats.json"))
|
|
src_ckpt_env = os.environ.get("SOURCE_CKPT_PATH", "").strip()
|
|
if src_ckpt_env:
|
|
ns_candidates += sorted((Path(src_ckpt_env) / "assets").glob("*/norm_stats.json"))
|
|
ns_candidates = [c for c in ns_candidates if c.is_file()]
|
|
if not ns_candidates:
|
|
sys.exit(f"[assets] norm_stats.json 未找到 (NORM_STATS_PATH / {ckpt_dir}/assets/*/ / SOURCE_CKPT_PATH/assets/*/)")
|
|
ns_link = model_dir / "norm_stats.json"
|
|
if ns_link.exists() or ns_link.is_symlink():
|
|
ns_link.unlink()
|
|
ns_link.symlink_to(ns_candidates[0].resolve())
|
|
report["norm_stats"] = str(ns_candidates[0])
|
|
|
|
# ---- 5) 判定与报告 ----
|
|
ok = (
|
|
not missing_top
|
|
and all(report["families"][k] > 0 for k in ("vision", "vision_proj", "lm", "expert"))
|
|
and (lm_head_present or bool(remaps))
|
|
)
|
|
report["verdict"] = "pass" if ok else "fail"
|
|
report["elapsed_sec"] = round(time.time() - t0, 1)
|
|
(model_dir / "make_model_dir_report.json").write_text(json.dumps(report, indent=2, ensure_ascii=False), encoding="utf-8")
|
|
print(f"DROBOTICFLOW_PROGRESS 50 make_model_dir {report['verdict']} keys={len(keys)} fam={report['families']}")
|
|
if not ok:
|
|
sys.exit("[keys] 键名核对未通过, 见 make_model_dir_report.json")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|