Files
VibemakerSandbox/app/scripts/import-apollo.py
T

169 lines
8.1 KiB
Python

#!/usr/bin/env python3
"""Import Apollo with mjlab's environment: uv run python /path/to/import-apollo.py ."""
import argparse
import hashlib
import json
from pathlib import Path
import sys
import xml.etree.ElementTree as ET
import mujoco
import numpy as np
import onnx
import torch
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("source", type=Path, help="mjlab checkout")
parser.add_argument("--checkpoint-file", type=Path, help="Exact checkpoint to import")
args = parser.parse_args()
source = args.source.resolve()
sys.path.insert(0, str(source / "src"))
from mjlab.entity import Entity
from mjlab.asset_zoo.robots.apptronik_apollo.apollo_constants import (
APOLLO_ROOT_HEIGHT, get_apollo_robot_cfg, get_apollo_xml,
)
if args.checkpoint_file:
checkpoint = args.checkpoint_file.resolve()
else:
checkpoints = list((source / "logs/rsl_rl/apollo_flat_upright_v3").glob("*/model_*.pt"))
checkpoint = max(checkpoints, key=lambda p: (p.stat().st_mtime_ns, int(p.stem.split("_")[-1])))
if not checkpoint.is_file():
raise FileNotFoundError(checkpoint)
policy = checkpoint.parent / f"{checkpoint.parent.name}.onnx"
if not policy.exists() or policy.stat().st_mtime_ns < checkpoint.stat().st_mtime_ns:
raise RuntimeError(f"Export the latest checkpoint first: {checkpoint}")
policy_bytes = policy.read_bytes()
export = onnx.load_model_from_string(policy_bytes)
# Verify the export contains this checkpoint's actor, not just a newer file.
state = torch.load(checkpoint, map_location="cpu", weights_only=False)["actor_state_dict"]
checked = set()
for initializer in export.graph.initializer:
if initializer.name in state:
np.testing.assert_array_equal(
onnx.numpy_helper.to_array(initializer), state[initializer.name].numpy(),
err_msg=f"ONNX does not match {checkpoint}: {initializer.name}",
)
checked.add(initializer.name)
if not ({k for k in state if k.startswith("mlp.")} | {"obs_normalizer._mean"}) <= checked:
raise RuntimeError("ONNX is missing actor weights or normalization mean")
# The exporter folds normalization std + epsilon into the Div initializer.
initializers = {p.name: onnx.numpy_helper.to_array(p) for p in export.graph.initializer}
divisions = [n for n in export.graph.node if n.op_type == "Div"]
if len(divisions) != 1 or divisions[0].input[1] not in initializers:
raise RuntimeError("Unexpected ONNX normalization graph")
np.testing.assert_array_equal(
initializers[divisions[0].input[1]],
(state["obs_normalizer._std"] + 0.01).numpy(),
err_msg="ONNX normalization does not match checkpoint",
)
onnx.checker.check_model(export)
metadata = {p.key: p.value for p in export.metadata_props}
names = metadata["joint_names"].split(",")
scales = list(map(float, metadata["action_scale"].split(",")))
if len(names) != 32 or len(scales) != 32 or metadata["observation_names"] != "base_lin_vel,base_ang_vel,projected_gravity,joint_pos,joint_vel,actions,command":
raise RuntimeError("Unexpected Apollo policy contract")
target = Path(__file__).resolve().parents[1] / "public/robots/apollo"
target.mkdir(parents=True, exist_ok=True)
records = []
def copy(src, dst, data=None):
data = src.read_bytes() if data is None else data
dest = target / dst
dest.parent.mkdir(parents=True, exist_ok=True)
dest.write_bytes(data)
records.append({"source": str(src), "file": dst, "bytes": len(data),
"sha256": hashlib.sha256(data).hexdigest()})
# Apply training-time collision editors and retain original position actuators.
entity = Entity(get_apollo_robot_cfg())
model = entity.spec.compile()
# Metadata rounds defaults to 3 decimals; use the actual training stand keyframe.
defaults = {}
for name in names:
jid = mujoco.mj_name2id(model, mujoco.mjtObj.mjOBJ_JOINT, name)
defaults[name] = float(model.key_qpos[0, model.jnt_qposadr[jid]])
np.testing.assert_allclose(
list(defaults.values()), list(map(float, metadata["default_joint_pos"].split(","))),
atol=0.000501, rtol=0, err_msg="Training pose does not match ONNX metadata",
)
root = ET.fromstring(entity.spec.to_xml())
for tag in ("keyframe", "size", "option"):
for element in root.findall(tag):
root.remove(element)
ET.SubElement(root, "option", timestep="0.005", integrator="implicitfast",
solver="Newton", cone="pyramidal", iterations="20", ls_iterations="20",
ccd_iterations="50")
# Newer native MuJoCo writes input="pos"; WASM's general actuator uses
# position transmission by default and does not accept that newer keyword.
for actuator in root.findall("./actuator/general"):
if actuator.attrib.get("input") != "pos":
raise RuntimeError("Unexpected non-position Apollo actuator")
del actuator.attrib["input"]
assets = []
xml = get_apollo_xml()
for mesh in root.findall("./asset/mesh"):
filename = mesh.attrib["file"]
dst = f"assets/{filename}"
copy(xml.parent / "assets" / filename, dst)
assets.append({"path": dst, "vfs": dst})
root.insert(0, ET.Comment(" Modified for browser simulation by import-apollo.py; see SOURCE.md. "))
ET.indent(root)
ET.ElementTree(root).write(target / "model.xml", encoding="unicode")
copy(policy, "policies/walk.onnx", policy_bytes)
copy(xml.parent / "LICENSE", "LICENSES/apollo.txt")
copy(source / "LICENSE", "LICENSES/mjlab.txt")
groups = {"policy": names, **{name: [name] for name in names}}
config = {
"schema": 1, "id": "apollo", "name": "Apptronik Apollo", "family": "apollo", "adapter": "generic",
"dimensions": {"height": 1.73}, "model": "model.xml", "assets": assets,
"base": {"type": "floating", "root_body": "base_link", "freejoint": model.joint(0).name,
"spawn": {"pos": [0, 0, APOLLO_ROOT_HEIGHT], "yaw": 0}},
"sim": {"timestep": 0.005, "decimation": 4},
"joints": {"groups": groups, "defaults": defaults},
"observations": {"obs": {"terms": [
{"type": "linear_velocity", "sensor": "imu_lin_vel"},
{"type": "gyro", "sensor": "imu_ang_vel"},
{"type": "projected_gravity", "body": "base_link"},
{"type": "joint_pos_rel", "group": "policy"},
{"type": "joint_vel", "group": "policy"},
{"type": "last_action", "group": "policy"},
{"type": "command", "size": 3},
]}},
"actions": [{"type": "joint_position", "group": name, "scale": scale, "offset": "default"}
for name, scale in zip(names, scales, strict=True)],
"actuation": {"type": "native_position"},
"commands": {"size": 3, "limits": [0.5, -0.3, 0.4]},
"skills": [{"id": "walk", "kind": "primary", "owns": "policy",
"controller": {"type": "onnx", "policy": "policies/walk.onnx"}}],
"input": {"bindings": [
{"action": action, "codes": codes, "label": label}
for action, codes, label in [
("move_forward", ["KeyW", "ArrowUp"], "Forward"),
("move_backward", ["KeyS", "ArrowDown"], "Backward"),
("turn_left", ["KeyA", "ArrowLeft"], "Turn left"),
("turn_right", ["KeyD", "ArrowRight"], "Turn right"),
("reset", ["Space"], "Reset"),
("pause", ["Escape"], "Pause / continue"),
("chaseToggle", ["KeyC"], "Restore chase camera"),
]]},
"render": {"source": "mjcf_visual", "groups": [1], "camera": {"distance": 3.5}},
"presentation": {"theme": "comic"},
"termination": {"tilt_z": -0.5, "min_height": 0.98, "on_fall": "hold"},
"license": "See LICENSES/apollo.txt and LICENSES/mjlab.txt; policy is locally trained.",
}
(target / "robot.json").write_text(json.dumps(config, indent=2) + "\n")
(target / "source-manifest.json").write_text(json.dumps({
"checkpoint": str(checkpoint),
"checkpoint_sha256": hashlib.sha256(checkpoint.read_bytes()).hexdigest(),
"model_source": str(xml), "files": records,
"behaviorReferences": [
{"file": str(p.relative_to(source)), "sha256": hashlib.sha256(p.read_bytes()).hexdigest()}
for p in [source / "src/mjlab/asset_zoo/robots/apptronik_apollo/apollo_constants.py",
source / "src/mjlab/tasks/velocity/config/apptronik_apollo/env_cfgs.py",
source / "src/mjlab/tasks/velocity/velocity_env_cfg.py"]],
}, indent=2) + "\n")
print(f"Imported {checkpoint} into {target}")