2026-10-09 16:41:13 +08:00

148 lines
7.3 KiB
Python

#!/usr/bin/env python3
"""量化编排: 契约 -> 渲染 yml -> 依次调 vendor 脚本 (calib / torch_eval / compile)。
输入 (见 quant_design_v1.md §7):
OUTPUT_DIR 必填; 需已含 quant_input/{model_dir, calib/} (make_model_dir + make_calib_data 产物)
CHECKPOINT_PATH 契约来源 (含 model_structure.json); CONTRACT_JSON 兜底
STAGES "calib,eval,compile" (默认全跑; eval=torch_eval 伪量化评测)
JOBS compile 并行数, 默认 32
产物:
OUTPUT_DIR/quant_input/pi05_quant.yml + calib_ckpt/ + eval_result/ + eval_dump/ + compile/ + logs/
设计约束: yml 的 model.* 部署形状字段全部从契约派生, 禁止手填 (契约一致性机械化保证)。
"""
from __future__ import annotations
import json
import logging
import os
import subprocess
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("run_quant")
PROJECT_ROOT = Path(__file__).resolve().parent.parent
VENDOR = PROJECT_ROOT / "vendor" / "llm_compression"
TEMPLATE = PROJECT_ROOT / "configs" / "pi05_quant.template.yml"
def load_contract() -> _contract.Contract:
ckpt = os.environ.get("CHECKPOINT_PATH", "").strip()
if ckpt and (Path(ckpt) / "model_structure.json").is_file():
return _contract.Contract.from_model_structure(str(Path(ckpt) / "model_structure.json"))
raw = os.environ.get("CONTRACT_JSON", "").strip()
if raw:
return _contract.Contract.from_json(raw)
sys.exit("[hyperparam] 需要 CHECKPOINT_PATH(含 model_structure.json) 或 CONTRACT_JSON")
def render_yml(ct: _contract.Contract, out: Path) -> None:
output_dir = out.parent.parent # OUTPUT_DIR/quant_input/pi05_quant.yml
calib = out.parent / "calib"
model_dir = out.parent / "model_dir"
calib_steps = int(os.environ.get("CALIB_SIZE", "32"))
eval_steps = int(os.environ.get("EVAL_SIZE", str(calib_steps)))
use_delta = os.environ.get("USE_DELTA_JOINT_ACTIONS", "1")
lm_prefix = 256 * len(ct.cameras) # vision_token_num 固定 256
lm_hpc = (lm_prefix + ct.max_token_len) % 32 == 0 # vendor 注释依据: 可整除才开 HPC
text = TEMPLATE.read_text(encoding="utf-8")
subs = {
"__MODEL_DIR__": str(model_dir),
"__CALIB_ACTION__": str(calib / "action"),
"__CALIB_IMAGE__": str(calib / "image"),
"__CALIB_PROMPT__": str(calib / "prompt.json"),
"__NORM_STATS__": str(model_dir / "norm_stats.json"),
"__OUT__": str(output_dir),
"__CACHE__": os.environ.get("LLM_CACHE_DIR", "/tmp/pi_quant_llm_cache"),
}
# 相对输出路径全部绝对化 (vendor 脚本按 CWD 解析相对路径, 而 run_stage 的 cwd=vendor)
for rel, abs_ in [
("./calib_ckpt", str(output_dir / "calib_ckpt")),
("./eval_result", str(output_dir / "eval_result")),
("./eval_dump", str(output_dir / "eval_dump")),
("./compile", str(output_dir / "compile")),
("./llm_cache", subs["__CACHE__"]),
]:
text = text.replace(rel, abs_)
for k, v in subs.items():
text = text.replace(k, v)
text = text.replace("__QUANT_DEVICE__", os.environ.get("QUANT_DEVICE", "cuda"))
# 契约派生字段 (禁止手填项)
text = text.replace("num_views: 3", f"num_views: {len(ct.cameras)}")
text = text.replace("action_horizon: 50", f"action_horizon: {ct.action_horizon}")
text = text.replace("max_token_len: 200", f"max_token_len: {ct.max_token_len}")
text = text.replace("discrete_state_input: true", f"discrete_state_input: {'true' if ct.state_inject else 'false'}")
text = text.replace("valid_state_dim: 14", f"valid_state_dim: {ct.state_dim}")
text = text.replace("calibration_step: 32", f"calibration_step: {calib_steps}")
text = text.replace("eval_step: 8", f"eval_step: {eval_steps}")
text = text.replace("use_delta_joint_actions: true", f"use_delta_joint_actions: {use_delta}")
text = text.replace("valid_action_dim: 14", f"valid_action_dim: {ct.state_dim}") # aloha 家族: 有效动作维=state 维
text = text.replace("jobs: 32", f"jobs: {os.environ.get('JOBS', '32')}")
text = text.replace(" enable_hpc: false # lm 关", f" enable_hpc: {'false' if not lm_hpc else 'true'} # lm ")
out.write_text(text, encoding="utf-8")
logger.info("[yml] 渲染完成: %s (num_views=%d horizon=%d lm_hpc=%s)", out, len(ct.cameras), ct.action_horizon, lm_hpc)
def run_stage(stage: str, yml: Path, output_dir: Path) -> None:
script = {"calib": "calib.sh", "eval": "torch_eval.sh", "compile": "compile.sh"}[stage]
log = output_dir / "logs" / f"{stage}.log"
log.parent.mkdir(parents=True, exist_ok=True)
env = dict(os.environ)
env["CONFIG_PATH"] = str(yml)
env["RUN_MODE"] = "local"
env["PATH"] = str(Path(sys.executable).parent) + os.pathsep + env.get("PATH", "") # vendor 脚本调 `python`
patches_dir = str(Path(__file__).resolve().parent / "patches")
inherited_pp = env.get("PYTHONPATH", "").strip()
# 注意不可产生空条目 (尾随冒号会把 CWD 带进 sys.path, 遮蔽 HF datasets 造成循环导入)
env["PYTHONPATH"] = patches_dir if not inherited_pp else f"{patches_dir}:{inherited_pp}"
if stage == "calib":
env["QUANT_OBS_CPU_QPARAMS"] = "1" # observer scale 求解搬 CPU, 规避 GPU OOM
if stage == "compile":
env["CUDA_VISIBLE_DEVICES"] = "" # vendor 约定: 编译走 CPU
cmd = ["bash", str(VENDOR / "scripts" / script), "--config_path", str(yml)]
logger.info("[stage:%s] %s (log: %s)", stage, " ".join(cmd), log)
t0 = time.time()
with open(log, "w") as f:
ret = subprocess.call(cmd, stdout=f, stderr=subprocess.STDOUT, env=env, cwd=str(VENDOR))
dt = time.time() - t0
print(f"DROBOTICFLOW_PROGRESS stage_{stage}_{'done' if ret == 0 else 'fail'} {dt:.0f}s")
if ret != 0:
sys.exit(f"[stage:{stage}] 失败 (exit {ret}), 见 {log}")
def main() -> None:
output_dir = Path(os.environ.get("OUTPUT_DIR", "").strip())
if not output_dir:
sys.exit("[hyperparam] OUTPUT_DIR not set")
qi = output_dir / "quant_input"
for req in ("model_dir/model.safetensors", "calib/prompt.json"):
if not (qi / req).is_file():
sys.exit(f"[hyperparam] 缺前置产物 {qi}/{req} (先跑 make_model_dir / make_calib_data)")
ct = load_contract()
yml = qi / "pi05_quant.yml"
render_yml(ct, yml)
stages = [s.strip() for s in os.environ.get("STAGES", "calib,eval,compile").split(",") if s.strip()]
for stage in stages:
run_stage(stage, yml, output_dir)
artifacts = [
{"name": "quant_input", "type": "file", "portId": "quant_input", "path": str(qi)},
{"name": "calib_ckpt", "type": "file", "portId": "calib_ckpt", "path": str(output_dir / "calib_ckpt")},
{"name": "hbm", "type": "file", "portId": "hbm", "path": str(output_dir / "compile")},
{"name": "eval_result", "type": "file", "portId": "eval_result", "path": str(output_dir / "eval_result")},
]
(output_dir / ".artifacts.json").write_text(json.dumps(artifacts, indent=2), encoding="utf-8")
logger.info("[done] stages=%s artifacts 已写", stages)
if __name__ == "__main__":
main()