212 lines
12 KiB
Python
212 lines
12 KiB
Python
#!/usr/bin/env python3
|
|
"""Import a trusted RP1-Flat checkpoint without starting/stopping Isaac Lab.
|
|
|
|
Run with roboparty_train/.venv/bin/python. Never run on an untrusted .pt file.
|
|
Only the logged 24-DOF Direct/term-major/ELU MLP contract is supported.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import re
|
|
import shutil
|
|
import subprocess
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import numpy as np
|
|
import onnx
|
|
from onnx.reference import ReferenceEvaluator
|
|
import torch
|
|
import yaml
|
|
|
|
|
|
def dump(path, value):
|
|
path.write_text(json.dumps(value, indent=2, ensure_ascii=False) + "\n")
|
|
|
|
|
|
def sha(path):
|
|
return hashlib.sha256(path.read_bytes()).hexdigest()
|
|
|
|
|
|
def resolve_parameter(value, name):
|
|
if not isinstance(value, dict):
|
|
return float(value)
|
|
matches = [float(v) for pattern, v in value.items() if re.fullmatch(pattern, name)]
|
|
if len(matches) != 1:
|
|
raise ValueError(f"Ambiguous/missing actuator parameter: {name}")
|
|
return matches[0]
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--source", type=Path, required=True)
|
|
parser.add_argument("--checkpoint", type=Path, required=True)
|
|
parser.add_argument("--policy-only", action="store_true", help="Preserve assets/other skills; replace walk ONNX and provenance only")
|
|
parser.add_argument("--completed", action="store_true", help="Require final configured iteration and mark walking training complete")
|
|
args = parser.parse_args()
|
|
source, checkpoint = args.source.resolve(), args.checkpoint.resolve()
|
|
public = Path(__file__).resolve().parents[1] / "public" / "robots"
|
|
target = public / "rp1"
|
|
params = checkpoint.parent / "params"
|
|
# BaseLoader reads YAML data only, ignoring Python object construction tags.
|
|
env = yaml.load((params / "env.yaml").read_text(), Loader=yaml.BaseLoader)
|
|
agent = yaml.load((params / "agent.yaml").read_text(), Loader=yaml.BaseLoader)
|
|
robot = env["scene_context"]["robot"]
|
|
names = robot["joint_ordering"]
|
|
if not (len(names) == 24 and env["robot"]["observation_history_layout"] == "term_major"
|
|
and env["robot"]["actor_obs_history_length"] == "3"
|
|
and agent["actor"]["activation"] == "elu"
|
|
and agent["actor"]["obs_normalization"] == "true"
|
|
and all(float(v) == 1 for v in env["normalization"]["obs_scales"].values())):
|
|
raise ValueError("Unsupported training contract; update importer explicitly")
|
|
defaults = {n: float(robot["init_state"]["joint_pos"][n]) for n in names}
|
|
if args.policy_only:
|
|
current = json.loads((target / "robot.json").read_text())
|
|
if not (current["joints"]["groups"]["policy"] == names
|
|
and current["joints"]["defaults"] == defaults
|
|
and current["actions"][0]["scale"] == float(env["robot"]["action_scale"])
|
|
and current["sim"] == {"timestep": float(env["sim"]["dt"]), "decimation": int(env["decimation"])}):
|
|
raise ValueError("Training contract changed; full re-import/revalidation required")
|
|
saved = torch.load(checkpoint, map_location="cpu", weights_only=True)
|
|
if args.completed and int(saved["iter"]) < int(agent["max_iterations"]) - 1:
|
|
raise ValueError("--completed requires the final configured training iteration")
|
|
state = saved["actor_state_dict"]
|
|
expected = [(512, 243), (256, 512), (128, 256), (24, 128)]
|
|
if any(tuple(state[f"mlp.{2*i}.weight"].shape) != shape for i, shape in enumerate(expected)):
|
|
raise ValueError("Unsupported actor architecture")
|
|
if any(not torch.isfinite(v).all() for v in state.values()):
|
|
raise ValueError("Non-finite checkpoint")
|
|
|
|
class Actor(torch.nn.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.register_buffer("mean", state["obs_normalizer._mean"])
|
|
self.register_buffer("std", state["obs_normalizer._std"])
|
|
layers = []
|
|
for i, (out_size, in_size) in enumerate(expected):
|
|
layer = torch.nn.Linear(in_size, out_size)
|
|
layer.load_state_dict({"weight": state[f"mlp.{2*i}.weight"], "bias": state[f"mlp.{2*i}.bias"]})
|
|
layers.append(layer)
|
|
if i < len(expected) - 1:
|
|
layers.append(torch.nn.ELU())
|
|
self.mlp = torch.nn.Sequential(*layers)
|
|
|
|
def forward(self, obs):
|
|
# RSL-RL EmpiricalNormalization epsilon is 1e-2, not sqrt(var+eps).
|
|
action = self.mlp((obs - self.mean) / (self.std + 1e-2))
|
|
return action.clamp(-float(agent["clip_actions"]), float(agent["clip_actions"]))
|
|
|
|
for folder in ["policies", "meshes", "LICENSES"]:
|
|
(target / folder).mkdir(parents=True, exist_ok=True)
|
|
actor = Actor().eval()
|
|
policy = target / "policies" / "walk.export.onnx"
|
|
torch.onnx.export(actor, torch.zeros(1, 243), str(policy), input_names=["obs"],
|
|
output_names=["actions"], opset_version=17, dynamo=False)
|
|
model = onnx.load(policy)
|
|
onnx.helper.set_model_props(model, {
|
|
"checkpoint": checkpoint.name, "checkpoint_sha256": sha(checkpoint),
|
|
"iteration": str(saved["iter"]), "joint_names": ",".join(names),
|
|
"default_joint_pos": ",".join(str(robot["init_state"]["joint_pos"][n]) for n in names),
|
|
"history_layout": "term_major", "history_length": "3", "status": "completed-training" if args.completed else "training-preview",
|
|
})
|
|
onnx.checker.check_model(model)
|
|
onnx.save(model, policy)
|
|
evaluator = ReferenceEvaluator(model)
|
|
rng = np.random.default_rng(42)
|
|
max_error = 0.0
|
|
for obs in [np.zeros((1, 243), dtype=np.float32), *[rng.normal(size=(1, 243)).astype(np.float32) for _ in range(8)]]:
|
|
with torch.no_grad():
|
|
expected_action = actor(torch.from_numpy(obs)).numpy()
|
|
actual = evaluator.run(None, {"obs": obs})[0]
|
|
np.testing.assert_allclose(actual, expected_action, rtol=2e-4, atol=2e-5)
|
|
max_error = max(max_error, float(np.abs(actual - expected_action).max()))
|
|
|
|
policy.replace(target / "policies" / "walk.onnx")
|
|
if args.policy_only:
|
|
provenance_file = target / "source-manifest.json"
|
|
provenance = json.loads(provenance_file.read_text())
|
|
provenance.update({"training_completed": args.completed, "checkpoint": str(checkpoint), "checkpoint_sha256": sha(checkpoint),
|
|
"iteration": int(saved["iter"]), "export_max_abs_error": max_error,
|
|
"training_params_sha256": {name: sha(params / name) for name in ["env.yaml", "agent.yaml"]}})
|
|
for record in provenance["files"]:
|
|
if record["file"] == "policies/walk.onnx":
|
|
record["sha256"] = sha(target / record["file"])
|
|
dump(provenance_file, provenance)
|
|
if args.completed:
|
|
current["name"] = "RP1"
|
|
dump(target / "robot.json", current)
|
|
print(f"Updated walk policy only: iteration {saved['iter']}, max error {max_error:.3g}")
|
|
return
|
|
|
|
asset_root = source / "robolab" / "data" / "robots" / "roboparty" / "rp1"
|
|
tree = ET.parse(asset_root / "mjcf" / "robot_24dof.xml")
|
|
root = tree.getroot()
|
|
root.find("compiler").set("meshdir", ".")
|
|
assets = []
|
|
for mesh in root.findall("./asset/mesh"):
|
|
rel = mesh.attrib["file"]
|
|
shutil.copyfile(asset_root / rel, target / rel)
|
|
assets.append({"path": rel, "vfs": rel})
|
|
tree.write(target / "model.xml", encoding="utf-8", xml_declaration=True)
|
|
actuator = robot["actuators"]["all_joints"]
|
|
parameters = {}
|
|
for name in names:
|
|
parameters[name] = {out: resolve_parameter(actuator[key], name) for out, key in {
|
|
"kp": "stiffness", "kd": "damping", "torque_limit": "actuator_effort_limit",
|
|
"velocity_limit": "actuator_velocity_limit", "keep_velocity": "keep_velocity",
|
|
}.items()}
|
|
config = json.loads((public / "microban" / "robot.json").read_text())
|
|
config.update({
|
|
"id": "rp1", "family": "rp1", "name": "RP1" if args.completed else "RP1 (training preview)",
|
|
"dimensions": {"height": 1.32},
|
|
"assets": assets,
|
|
"base": {"type": "floating", "root_body": "base_link", "freejoint": "base_link_freejoint",
|
|
"spawn": {"pos": [float(v) for v in robot["init_state"]["pos"]], "yaw": 0}, "initial": defaults},
|
|
"sim": {"timestep": float(env["sim"]["dt"]), "decimation": int(env["decimation"])},
|
|
"joints": {"groups": {"policy": names}, "defaults": defaults},
|
|
"observations": {"obs": {"history": 3, "layout": "term_major", "terms": [
|
|
{"type": "gyro", "sensor": "base_ang_vel"},
|
|
{"type": "projected_gravity", "body": "base_link"},
|
|
{"type": "command", "size": 3},
|
|
{"type": "joint_pos_rel", "group": "policy"},
|
|
{"type": "joint_vel", "group": "policy"},
|
|
{"type": "last_action", "group": "policy"},
|
|
]}},
|
|
"actions": [{"type": "joint_position", "group": "policy", "scale": float(env["robot"]["action_scale"]), "offset": "default"}],
|
|
"actuation": {"type": "joint_pd", "kp": 120, "kd": 7.5, "torque_limit": 140, "joints": parameters},
|
|
"commands": {"size": 3, "limits": [0.4, -0.3, 0.6]},
|
|
"skills": [{"id": "walk", "kind": "primary", "owns": "policy", "controller": {"type": "onnx", "policy": "policies/walk.onnx"}},
|
|
*[{"id": s, "kind": "episodic", "owns": "policy", "available": False,
|
|
"duration": d, "blend": 0.5, "controller": {"type": "onnx"}} for s, d in [("dance", 10), ("wave", 4)]]],
|
|
"render": {"source": "mjcf_visual", "groups": [2], "camera": {"distance": 2.4}},
|
|
"termination": {"tilt_z": -0.5, "min_height": 0.4, "on_fall": "hold"},
|
|
"license": "Training preview; RP1 mesh license not supplied upstream. Local evaluation only; see SOURCE.md. Actuation omits randomized delay/domain randomization.",
|
|
})
|
|
config["input"]["bindings"] = [b for b in config["input"]["bindings"] if not b["action"].startswith("toggle:")]
|
|
config["input"]["bindings"].extend([
|
|
{"action": "skill:dance", "codes": ["KeyJ"], "label": "Dance", "disabled": True},
|
|
{"action": "skill:wave", "codes": ["KeyH"], "label": "Wave", "disabled": True},
|
|
])
|
|
# Sensor name is read from the actual source model, not guessed from other robots.
|
|
gyros = root.findall("./sensor/gyro")
|
|
if len(gyros) != 1 or gyros[0].get("site") != "imu":
|
|
raise ValueError("Expected root-frame IMU gyro")
|
|
config["observations"]["obs"]["terms"][0]["sensor"] = gyros[0].get("name")
|
|
dump(target / "robot.json", config)
|
|
shutil.copyfile(source / "robolab" / "LICENSE", target / "LICENSES" / "RoboLab-BSD-3-Clause.txt")
|
|
commit = subprocess.check_output(["git", "-C", str(asset_root), "rev-parse", "HEAD"], text=True).strip()
|
|
files = ["model.xml", "policies/walk.onnx", "LICENSES/RoboLab-BSD-3-Clause.txt", *[a["path"] for a in assets]]
|
|
dump(target / "source-manifest.json", {
|
|
"source": "https://github.com/Roboparty/rp1_description", "commit": commit,
|
|
"training_completed": args.completed, "checkpoint": str(checkpoint), "checkpoint_sha256": sha(checkpoint), "iteration": int(saved["iter"]),
|
|
"training_params_sha256": {name: sha(params / name) for name in ["env.yaml", "agent.yaml"]},
|
|
"source_model_sha256": sha(asset_root / "mjcf" / "robot_24dof.xml"),
|
|
"export_max_abs_error": max_error,
|
|
"files": [{"file": f, "sha256": sha(target / f)} for f in files],
|
|
})
|
|
print(f"Imported {target}: iteration {saved['iter']}, ONNX/PyTorch max error {max_error:.3g}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|