OpenPi_POC_Dev/pi_pipeline/convert_runner.py
2026-10-09 16:41:13 +08:00

210 lines
9.0 KiB
Python

"""j2t 转换入口 (design_v2 §8, D8/R-10)。
JAX checkpoint (契约管线产物, 含 model_structure.json) -> torch model.safetensors。
改造 (相对 examples/convert_jax_model_to_pytorch.py):
- 结构判定读 model_structure.json (契约快照), 不再用 config 名 / 目录名字符串
(R-10: examples 内三处 "pi05" in checkpoint_dir 已改为 pi05 形参, 本入口以
契约字段驱动);
- 转换前结构校验 (params/ 存在 + 契约可恢复);
- 转换后抽样数值一致性自检 (JAX vs torch 前向对拍: 同一 fake_obs 与噪声,
actions 余弦相似度 / 最大绝对误差), 结果写进产物 metadata;
- 产物附加: model_structure.json 透传 + paligemma_tokenizer.model 拷贝
(下游消费: model.safetensors + tokenizer 同目录)。
超参: CHECKPOINT_PATH (与上游端口 checkpoint 二选一上游优先), OUTPUT 精度
(bfloat16 默认)。
"""
from __future__ import annotations
import importlib.util
import json
import logging
import os
import shutil
import sys
import types
from pathlib import Path
import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from pi_pipeline import contract as _contract # noqa: E402
from ws_common import report_utils # noqa: E402
def locate_checkpoint() -> Path:
"""上游端口 (portId=checkpoint) 优先, CHECKPOINT_PATH 兜底。
与 serve_xpolicylab.locate_checkpoint 同语义; 此处独立实现避免顶层引入
XPolicyLab client_server 依赖 (convert 场景无需 serve 栈)。
"""
import json
artifacts_path = os.environ.get("INPUT_ARTIFACTS_PATH", "")
if artifacts_path and os.path.isfile(artifacts_path):
with open(artifacts_path, "r", encoding="utf-8") as f:
entries = json.load(f) if artifacts_path else []
for entry in entries or []:
if entry.get("portId") == "checkpoint" and not str(entry.get("path", "")).startswith("bos://"):
return Path(entry["path"])
value = os.environ.get("CHECKPOINT_PATH", "").strip()
if value:
return Path(value)
sys.exit("[input] neither upstream checkpoint port nor CHECKPOINT_PATH is set")
logger = logging.getLogger("openpi.convert")
_CONVERT_SCRIPT = Path(__file__).resolve().parents[1] / "openpi" / "examples" / "convert_jax_model_to_pytorch.py"
def _load_convert_module():
spec = importlib.util.spec_from_file_location("openpi_convert", _CONVERT_SCRIPT)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def numeric_consistency_check(checkpoint_dir: Path, contract: _contract.Contract, torch_output: Path) -> dict:
"""JAX vs torch 前向对拍: 同一 obs + 噪声, 比较 sample_actions。"""
import jax
import torch
import openpi.models.model as _model
import dataclasses
from openpi.models.pi0_config import Pi0Config
# 自检单次前向, 关闭 torch.compile (max-autotune 首编 10min+ 且对拍数值无益)
model_cfg = dataclasses.replace(
Pi0Config(**contract.pi0_config_kwargs()), pytorch_compile_mode=None
)
jax_model = model_cfg.load(_model.restore_params(checkpoint_dir / "params", dtype="bfloat16"))
# load_pytorch 按 train_config.model 取模型配置 (官方 scripts 惯例), 此处以
# shim 适配独立调用
train_cfg_shim = types.SimpleNamespace(model=model_cfg)
torch_model = model_cfg.load_pytorch(train_cfg_shim, str(torch_output / "model.safetensors"))
torch_model = torch_model.to("cuda" if torch.cuda.is_available() else "cpu").eval()
device = next(torch_model.parameters()).device
obs_spec, _ = model_cfg.inputs_spec()
obs = jax.tree.map(
lambda s: np.random.uniform(-1.0, 1.0, size=s.shape).astype(np.float32)
if s.dtype == np.float32 or str(s.dtype) == "float32"
else (np.random.uniform(0, 2, size=s.shape).astype(np.int32) if "int" in str(s.dtype) else np.zeros(s.shape, bool)),
obs_spec,
)
rng = np.random.default_rng(0)
noise = rng.normal(size=(1, contract.action_horizon, model_cfg.action_dim)).astype(np.float32)
# jaxtyping>=0.3 的 Array hint 不含 numpy 数组, jax 侧统一转 jnp
import jax.numpy as jnp
jax_obs = jax.tree.map(jnp.asarray, obs)
jax_actions = np.asarray(jax_model.sample_actions(jax.random.key(0), jax_obs, noise=jnp.asarray(noise)))
torch_obs = jax.tree.map(lambda x: torch.from_numpy(np.asarray(x)).to(device), obs)
# torch 栈图像约定 CHW (官方 train_pytorch 数据管线/transformers get_image_features),
# jax 侧 inputs_spec 为 HWC — 图像叶 permute
for cam in list(torch_obs.images.keys()):
torch_obs.images[cam] = torch.permute(torch_obs.images[cam], (0, 3, 1, 2))
torch_noise = torch.from_numpy(noise).to(device)
with torch.no_grad():
torch_actions = np.asarray(torch_model.sample_actions(device, torch_obs, noise=torch_noise).cpu())
a, b = jax_actions.reshape(-1), torch_actions.reshape(-1)
cos = float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b) + 1e-9))
max_abs = float(np.abs(a - b).max())
return {"cosine": cos, "max_abs_diff": max_abs, "shape": list(jax_actions.shape)}
def main() -> None:
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
output_dir = Path(os.environ.get("OUTPUT_DIR", "").strip())
if not output_dir:
sys.exit("[hyperparam] OUTPUT_DIR not set")
checkpoint_dir = locate_checkpoint()
contract = load_contract_checked(checkpoint_dir)
precision = os.environ.get("PRECISION", "bfloat16").strip() or "bfloat16"
logger.info("[convert] JAX %s -> torch (%s)", checkpoint_dir, precision)
convert = _load_convert_module()
from openpi.models.pi0_config import Pi0Config
model_cfg = Pi0Config(**contract.pi0_config_kwargs())
torch_output = output_dir / "checkpoint_torch"
convert.convert_pi0_checkpoint(str(checkpoint_dir), precision, str(torch_output), model_cfg)
# 契约快照透传 (checkpoint 自带或 CONTRACT_JSON 注入时由当前契约生成)
source_structure = checkpoint_dir / "model_structure.json"
if source_structure.is_file():
shutil.copy2(source_structure, torch_output / "model_structure.json")
else:
(torch_output / "model_structure.json").write_text(contract.to_json(), encoding="utf-8")
logger.info("[convert] contract snapshot generated from CONTRACT_JSON injection")
# tokenizer 随行 (量化交接)
tokenizer_src = _find_tokenizer()
if tokenizer_src:
shutil.copy2(tokenizer_src, torch_output / "paligemma_tokenizer.model")
logger.info("[convert] tokenizer copied: %s", tokenizer_src)
else:
logger.warning(
"[convert] paligemma_tokenizer.model not found in cache; quantization handoff "
"requires it next to model.safetensors (run serve/train once or place manually)"
)
# 数值一致性自检
check = numeric_consistency_check(checkpoint_dir, contract, torch_output)
verdict = check["cosine"] >= 0.95
check["verdict"] = "pass" if verdict else "fail"
logger.info("[convert] numeric check: cos=%.4f max_abs=%.4f -> %s", check["cosine"], check["max_abs_diff"], check["verdict"])
(torch_output / "convert_metadata.json").write_text(
json.dumps({"numeric_check": check, "precision": precision, "source_checkpoint": str(checkpoint_dir)},
ensure_ascii=False, indent=2),
encoding="utf-8",
)
report_utils.write_artifacts(
output_dir,
[{"name": "checkpoint_torch", "type": "file", "portId": "checkpoint", "path": str(torch_output)}],
)
if not verdict:
sys.exit("[convert] numeric consistency check FAILED (cosine < 0.95)")
def load_contract_checked(checkpoint_dir: Path) -> _contract.Contract:
"""契约来源优先级: checkpoint 内 model_structure.json > CONTRACT_JSON 超参注入
(官方权重/外部权重无契约快照时, 如 RoboTwin cotrain 权重, T5)。"""
structure = checkpoint_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:
logger.info("[convert] model_structure.json not found; using CONTRACT_JSON injection")
# 快照只落产物侧 (main 透传), 不回写源 checkpoint 目录 (外部资产可能只读)
return _contract.Contract.from_json(raw)
sys.exit(
f"[convert] {structure} not found and CONTRACT_JSON not set; provide either a "
"contract-pipeline checkpoint or an explicit CONTRACT_JSON"
)
def _find_tokenizer() -> Path | None:
"""openpi 缓存中的 paligemma tokenizer (量化交接)。"""
candidates = []
data_home = os.environ.get("OPENPI_DATA_HOME", "")
if data_home:
candidates.append(Path(data_home) / "big_vision" / "paligemma_tokenizer.model")
candidates.append(Path.home() / ".cache" / "openpi" / "big_vision" / "paligemma_tokenizer.model")
for cand in candidates:
if cand.is_file():
return cand
return None
if __name__ == "__main__":
main()