Files
SkillFactory/scripts/package_standing.py
T
chenlin 093ab883be feat: release guided Go2 standing skill with explicit drift waiver
Add loaded-standing physics, PPO training, fixed-candidate acceptance, isolated inference, and fail-closed publication. Preserve the original 120-second drift failure and bind the operator-approved exception to the exact candidate and evidence.

Include pending Reach training/release infrastructure and archived packages. Leave pre-existing short-worker visualization changes and large generated training artifacts out of this commit.
2026-10-12 17:26:03 +08:00

171 lines
9.0 KiB
Python

#!/usr/bin/env python3
"""Prepare one locked standing actor candidate; publication is gated separately."""
import argparse
import json
import shutil
from pathlib import Path
from runtime_common import write_json
SELECTED_SHA256 = "f2197339dea3622ed92b82fdddff3400f2a82f3ca4d4fd3be6c72b45f1a77704"
def prepare(run, output):
import torch
from SkillFactory.legs.frozen_standing import SHAPES, Actor, FrozenStanding, file_hash
from SkillFactory.legs.standing_ppo import StandingActorCritic, features
from SkillFactory.legs.urdf import forward, multiply, pose
torch.set_num_threads(4)
selected = run / "checkpoints/best_validation.pt"
if file_hash(selected) != SELECTED_SHA256:
raise ValueError("Fixed selected checkpoint changed; do not reselect from holdout")
result = json.loads((run / "result.json").read_text())
selection = json.loads((run / "selection.json").read_text())
if not result["formal_training_complete"] or result["update"] != 2048 or selection["update"] != 1024:
raise ValueError("Incomplete training/wrong validation selection")
checkpoint = torch.load(selected, map_location="cpu", weights_only=True)
if checkpoint["update"] != 1024 or checkpoint["identity"] != result["identity"]:
raise ValueError("Checkpoint identity mismatch")
for name, digest in checkpoint["identity"]["sources"].items():
if file_hash(name) != digest:
raise ValueError(f"Training source modified: {name}")
task_path = Path("configs/standing/go2_fl_support_v0.json")
task = json.loads(task_path.read_text())
support = Path(task["support_asset"]) / "support.json"
bundle = json.loads((Path(task["source_bundle"]) / "manifest.json").read_text())
nominal = [j["q_default"] for j in bundle["spec"]["joints"]]
poses, _ = forward(bundle["tree"], dict(zip(bundle["joint_order"], nominal)))
foot = bundle["foot"]
height = foot["radius"] - multiply(poses[foot["body"]], pose(foot["xyz"], foot["rpy"]))[2][3]
output.mkdir(parents=True, exist_ok=False)
binding = {
"task_sha256": file_hash(task_path),
"support_sha256": file_hash(support),
"source_manifest_sha256": file_hash(Path(task["source_bundle"]) / "manifest.json"),
"training_identity": checkpoint["identity"],
"checkpoint_sha256": SELECTED_SHA256,
"height_m": height,
"nominal": nominal,
"action_scale_rad": task["action_scale_rad"],
"soft_limits": [j["soft_limits"] for j in bundle["joints"]],
"joint_order": bundle["joint_order"],
"control_dt": 0.01,
"physics_dt": 0.002,
"quaternion_order": "xyzw",
"solver": "PGS",
"mount_wrench": "parent-on-child; child joint frame at mount origin; untared N/Nm",
"limits": {
"scope": "guided single Go2 FL; synthetic3kg payload; zero vertical guide",
"not_claimed": ["unsupported balance", "full quadruped", "Go1", "hardware safety", "generic v2 adapter"],
},
"support_manifest": json.loads(support.read_text()),
}
write_json(output / "binding.json", binding)
for source, target in (
(task_path, "task.json"),
(run / "config.json", "training.json"),
(run / "selection.json", "selection.json"),
(Path("source/SkillFactory/SkillFactory/legs/frozen_standing.py"), "frozen_standing.py"),
(Path(task["source_bundle"]) / "LICENSE", "ASSET_LICENSE"),
):
shutil.copyfile(source, output / target)
actor = Actor(checkpoint["model_config"]["hidden"])
actor.load_state_dict(
{k: v for k, v in checkpoint["model"].items() if k.startswith("actor.") or k in ("offset", "scale")}
)
torch.save(actor.state_dict(), output / "actor.pt")
obs = {k: torch.zeros(1, *shape) for k, shape in SHAPES.items()}
obs["joint_state"][0, :, 0] = torch.tensor(nominal)
obs["base_state"][0, 2], obs["base_state"][0, 6], obs["goal"][0, 0] = height, 1, height
torch.save(obs, output / "example.pt")
(output / "requirements.txt").write_text(
"# Python3.12; CPU or CUDA PyTorch2.10.0 as separately validated\ntorch==2.10.0\n"
)
(output / "README.md").write_text(
"# Go2 FL guided loaded stand 1.0.0\n\n"
"Inference requires Python3.12/Torch2.10.0 only, no Kit/project/training checkpoint. "
"Load FrozenStanding(directory, manifest_sha256=<trusted manifest hash>, "
"task_sha256=<deployed task hash>, support_sha256=<deployed support manifest hash>, device='cpu'). "
"act(observation) returns float32[B,3] bounded normalized position actions; position_targets returns rad. "
"See example.pt and binding.json. Caller supplies six exact observation fields with mixed frames, "
"unit xyzw quaternion, actual last applied actions, true six-axis mount wrench, "
"100Hz sampling, and all driver limits.\n\n"
"Scope: floating 3kg synthetic payload, Go2 FL, PGS, XY/rotation guidance, zero vertical assistance; "
"not unsupported balance, full quadruped, arbitrary mechanics, general v2 adapter or hardware certification. "
"Source missing inertias/imported assumptions are disclosed in binding support_manifest; "
"no source mechanical changes. "
"The asset itself is externally bound, not required for inference or bundled as a turnkey simulator.\n\n"
"Checkpoint1024 was selected by validation only. Validation is not holdout or acceptance. "
"Only an accepted manifest together with its bound acceptance evidence establishes a release. "
"Two-axis combinations are exploratory and outside validated simulation scope. SHA256 is not a signature.\n\n"
"## Exact observation ABI (all float32, same device, nonempty batch B)\n\n"
"- joint_state[B,3,3]: declared joint order; q(rad), dq(rad/s), previous normalized action.\n"
"- base_state[B,13]: payload mounting-origin position minus environment origin (m); "
"world orientation xyzw; world linear velocity(m/s); world angular velocity(rad/s). "
"The validated ground/environment origins have world z=0. Not root-frame velocities.\n"
"- mount_wrench[B,6]: [Fx,Fy,Fz,Tx,Ty,Tz], N and Nm; measured parent-on-child reaction, "
"child joint frame, common mount origin; no gravity tare.\n"
"- foot_state[B,10]: spherical foot center position relative to payload origin in rotating root frame(3,m); "
"point velocity relative to translating AND rotating root in root frame(3,m/s; subtract omega cross r); "
"ground normal contact force in WORLD frame(3,N); world foot-center z minus foot radius(1,m).\n"
"- guide_wrench[B,6]: actual passive guide force/torque in WORLD frame, mount origin, N/Nm; Fz exactly0.\n"
"- goal[B,1]: fixed nominal payload mounting-origin height in m, binding.height_m.\n"
"The loader subtracts goal from base_state z internally; do not pre-subtract it. "
"Foot force is normal-only, not full friction wrench. Shapes alone cannot verify sensor frame correctness.\n"
)
manifest = {
"format": "skillfactory.standing-bundle.v1",
"contract": "go2-fl-loaded-stand-v0",
"version": "1.0.0",
"stage": "candidate",
"hidden": checkpoint["model_config"]["hidden"],
"checkpoint_sha256": SELECTED_SHA256,
"files": {p.name: file_hash(p) for p in sorted(output.iterdir()) if p.is_file()},
}
write_json(output / "manifest.json", manifest)
frozen = FrozenStanding(
output,
manifest_sha256=file_hash(output / "manifest.json"),
task_sha256=binding["task_sha256"],
support_sha256=binding["support_sha256"],
)
model = StandingActorCritic(**checkpoint["model_config"]).eval()
model.load_state_dict(checkpoint["model"])
gen = torch.Generator().manual_seed(91000)
worst = 0.0
for batch in (1, 16, 64, 256):
sample = {k: v.expand(batch, *v.shape[1:]).clone() for k, v in obs.items()}
for key in ("joint_state", "mount_wrench", "foot_state"):
sample[key] += 0.05 * torch.randn(sample[key].shape, generator=gen)
with torch.inference_mode():
difference = (model.act(features(sample), deterministic=True)[0] - frozen.act(sample)).abs().max().item()
worst = max(worst, difference)
if worst > 1e-6:
raise ValueError("Actor extraction mismatch")
write_json(
output.parent / "candidate_lock.json",
{
"passed": True,
"accepted_skill": False,
"checkpoint_sha256": SELECTED_SHA256,
"selected_update": 1024,
"manifest_sha256": file_hash(output / "manifest.json"),
"core_files": manifest["files"],
"actor_extraction_max_error": worst,
},
)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--run", type=Path, default=Path("logs/loaded_stand/train_v1"))
parser.add_argument("--output", type=Path, required=True)
args = parser.parse_args()
prepare(args.run, args.output)
if __name__ == "__main__":
main()