9fe9d27865
Consolidate next-stage docs, archive superseded narratives, and preserve frozen document inputs. Retain invalid P2 pairs, partial RootXY training, failed attempts and nonselectable checkpoints as explicit limitations. Include supervised evaluation/training, desktop GPU admission, bounded evidence and atomic-publication monitor repairs with CPU regressions. Ignore local dotenv configuration. Validation: 1403 passed, 6 skipped for contact-cycle and related contracts/CLI tests. Full-suite legacy ABI and registration tests require Python 3.12/Torch 2.10/pxr; no runtime checks relaxed. No new physical run or accepted skill.
259 lines
13 KiB
Python
259 lines
13 KiB
Python
#!/usr/bin/env python3
|
|
"""Frozen u256 replay, never training. --check-only performs CPU preflight without Kit.
|
|
|
|
--replay is one nominal first episode (900 controls / 4500 physics ticks per env),
|
|
not continuous cycling or acceptance. Physics execution requires separate approval.
|
|
Use supervise_sim.py with a fresh sibling log/report and <=360s outer timeout.
|
|
"""
|
|
import argparse
|
|
import json
|
|
import os
|
|
import signal
|
|
import sys
|
|
import time
|
|
import traceback
|
|
from pathlib import Path
|
|
|
|
from runtime_common import write_json
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def record_episodes(recorder, output, mode, functional, nominal, boundaries):
|
|
"""Persist full physics-rate first episodes even when execution/auditing fails."""
|
|
import torch
|
|
from audit_contact_cycle_functional import evaluate
|
|
from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES
|
|
|
|
torch.save(recorder.initial_state, output / "initial_state.pt")
|
|
if not recorder.frames:
|
|
write_json(output / "episodes.json", {"rows": [], "error": "No physics samples captured"})
|
|
return []
|
|
trace = {k: torch.stack([f[k] for f in recorder.frames]) for k in recorder.frames[0]}
|
|
# SimCycleState.policy() exposes these signed simulation-truth errors in 46D.
|
|
trace["root_position_error_m"] = trace["policy_snapshot"][..., 32:35].clone()
|
|
trace["root_velocity_error_m_s"] = trace["policy_snapshot"][..., 35:38].clone()
|
|
episodes = {str(e): {k: v[:stop, e:e+1] for k, v in trace.items()}
|
|
for e, stop in enumerate(recorder.stops)}
|
|
torch.save(episodes, output / "first_episodes.pt")
|
|
write_json(output / "boundaries.json", boundaries)
|
|
rows = []
|
|
for e, episode in episodes.items():
|
|
d = {k: v[:, 0] for k, v in episode.items()}
|
|
flags = d["failure_flags"]
|
|
hits = flags.any(-1).nonzero().flatten()
|
|
causes = [name for j, name in enumerate(FAILURE_NAMES) if bool(flags[:, j].any())]
|
|
# The immutable auditor has two metadata interfaces, not three controller claims.
|
|
audit_kind = "learned_policy" if mode == "learned" else "analytic_reference"
|
|
audit = evaluate(episode, functional, audit_kind)
|
|
boundary = boundaries[int(e)]
|
|
actual = d["processed_target"] - nominal.cpu() - .5 * d["reference_used_normalized"]
|
|
invariants = {
|
|
"finite": all(bool(torch.isfinite(v).all()) for v in episode.values()),
|
|
"actor46": d["actor_input"].shape[-1] == 46,
|
|
"request_bound": bool((d["requested_action"].abs() <= 1).all()),
|
|
"reference_bound": bool((d["reference_used_normalized"].abs() <= 1).all()),
|
|
"actual_residual_bound": bool((actual.abs() <= .05 + 1e-6).all()),
|
|
"actual_residual_logged": bool(torch.allclose(actual, d["effective_residual_rad"], atol=1e-6, rtol=0)),
|
|
}
|
|
if mode == "learned":
|
|
invariants["deterministic_action"] = bool(torch.allclose(
|
|
d["requested_action"], d["actor_mean"].tanh(), atol=1e-6, rtol=0))
|
|
elif mode == "reference":
|
|
invariants["zero_residual"] = not bool(d["requested_action"].any() or actual.abs().gt(1e-6).any())
|
|
success = bool(audit["sim_functional_passed"] and all(invariants.values())
|
|
and boundary["reason"] == "timeout" and not recorder.early[int(e)]
|
|
and not causes and not d["would_fail"].any())
|
|
swing = d["phase"] == 3
|
|
rows.append({"env": int(e), "success": success, "functional_audit": audit,
|
|
"audit_controller_kind_is_metadata_only": True, "invariants": invariants,
|
|
"first_episode_samples": len(d["time"]), "termination": boundary,
|
|
"terminal_causes": causes,
|
|
"first_failure_time_s": float(d["time"][int(hits[0])]) if len(hits) else None,
|
|
"phase_samples": {str(p): int((d["phase"] == p).sum()) for p in range(6)},
|
|
"height_rms_m": float(d["signed_height_error"].square().mean().sqrt())
|
|
if invariants["finite"] else None,
|
|
"swing_rms_m": float((d["foot"][swing] - d["target"][swing]).square().sum(-1).mean().sqrt())
|
|
if invariants["finite"] and swing.any() else None,
|
|
"landing_error_m": audit["metrics"].get("landing_error_m")})
|
|
write_json(output / "episodes.json", {"rows": rows, "controller_mode": mode, "accepted_skill": False})
|
|
return rows
|
|
|
|
|
|
def run_episode(env, args, policy, functional, guard):
|
|
import torch
|
|
from SkillFactory.legs.contact_cycle_sim.model import features
|
|
from SkillFactory.legs.contact_cycle_sim.replay import inference_action
|
|
from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state
|
|
from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder
|
|
|
|
s = state(env)
|
|
if (s.robot.is_fixed_base or env.cfg.sim.physics.solver_type != 0
|
|
or env.cfg.sim.physics.enable_external_forces_every_iteration
|
|
or env.cfg.terminations.failed is None or env.physics_dt != .002 or env.step_dt != .01
|
|
or env.cfg.episode_length_s != 9):
|
|
raise RuntimeError("Require frozen floating-root PGS/500Hz physics/100Hz control/9s failure-enabled task")
|
|
x = features(env.reset()[0]["policy"])
|
|
if bool(s.scenarios.any() or s.stand_only.any()):
|
|
raise RuntimeError("Frozen replay must contain nominal complete cycles only")
|
|
boundaries = [{"reason": "incomplete", "control_step": None} for _ in range(env.num_envs)]
|
|
recorder = SimCycleRecorder(env)
|
|
rewards = []
|
|
rows = []
|
|
try:
|
|
with recorder, torch.no_grad():
|
|
for step in range(900):
|
|
guard()
|
|
action, mean, std = inference_action(x, args.controller, policy, args.script_action)
|
|
recorder.before_control(x if policy is not None else None, action, mean, std)
|
|
obs, reward, terminated, truncated, _ = env.step(action)
|
|
rewards.append(reward.detach().cpu().clone())
|
|
recorder.end_control(terminated, truncated, step == 899)
|
|
for e in (terminated | truncated).nonzero().flatten().cpu().tolist():
|
|
if boundaries[e]["control_step"] is None:
|
|
boundaries[e] = {"reason": "physical_failure" if bool(terminated[e]) else "timeout",
|
|
"control_step": step + 1}
|
|
if not torch.isfinite(reward).all():
|
|
raise RuntimeError("Nonfinite replay reward")
|
|
guard()
|
|
# Stop the entire batch on any early first boundary; never splice a reset episode.
|
|
if bool(terminated.any()) or (step != 899 and bool(truncated.any())):
|
|
for boundary in boundaries:
|
|
if boundary["control_step"] is None:
|
|
boundary.update(reason="batch_stopped_after_peer_boundary", control_step=step + 1)
|
|
break
|
|
if bool((terminated | truncated).all()):
|
|
break
|
|
x = features(obs["policy"])
|
|
except BaseException as error:
|
|
for boundary in boundaries:
|
|
if boundary["control_step"] is None:
|
|
boundary.update(reason="exception", error=f"{type(error).__name__}: {error}")
|
|
raise
|
|
finally:
|
|
if rewards:
|
|
torch.save(torch.stack(rewards), args.output / "control_rewards.pt")
|
|
rows = record_episodes(recorder, args.output, args.controller, functional, s.nominal, boundaries)
|
|
return {"passed": len(rows) == env.num_envs and all(row["success"] for row in rows),
|
|
"rows": rows, "control_steps": len(rewards), "physics_samples": len(recorder.frames)}
|
|
|
|
|
|
def main(argv=None):
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
execution = parser.add_mutually_exclusive_group(required=True)
|
|
execution.add_argument("--check-only", action="store_true")
|
|
execution.add_argument("--replay", action="store_true")
|
|
parser.add_argument("--controller", choices=("learned", "reference", "script"), default="learned")
|
|
parser.add_argument("--checkpoint", type=Path)
|
|
parser.add_argument("--script-action", nargs=3, type=float, metavar=("HIP", "THIGH", "CALF"))
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
parser.add_argument("--num-envs", type=int, choices=range(1, 9), default=1)
|
|
parser.add_argument("--device", default="cuda:0")
|
|
parser.add_argument("--headless", action="store_true")
|
|
args = parser.parse_args(argv)
|
|
from SkillFactory.legs.contact_cycle_sim.replay import (
|
|
CANDIDATE,
|
|
controller_identity,
|
|
create_output,
|
|
load_frozen_policy,
|
|
verify_bundle,
|
|
)
|
|
|
|
checkpoint = (args.checkpoint or ROOT / CANDIDATE).resolve()
|
|
args.output = create_output(ROOT, args.output, checkpoint)
|
|
result = {"passed": False, "accepted_skill": False, "optimizer_steps": 0,
|
|
"controller_mode": args.controller, "initialization_entered": False, "physics_executed": False,
|
|
"checkpoint_requested": str(checkpoint)}
|
|
app = env = safety = None
|
|
code = 1
|
|
started = time.monotonic()
|
|
stopped = False
|
|
old_handlers = {}
|
|
|
|
def stop(*_):
|
|
nonlocal stopped
|
|
stopped = True
|
|
|
|
def guard():
|
|
if stopped or time.monotonic() - started >= 300:
|
|
raise InterruptedError("Replay interrupted or 300s wall bound exceeded; no retry")
|
|
if safety is not None:
|
|
safety.check()
|
|
|
|
try:
|
|
result.update(controller_identity(args.controller, args.script_action))
|
|
manifest, binding = verify_bundle(ROOT, checkpoint)
|
|
write_json(args.output / "identity.json", {**binding, **result})
|
|
policy = load_frozen_policy(checkpoint, manifest) if args.controller == "learned" else None
|
|
result["preflight_passed"] = True
|
|
if args.check_only:
|
|
result.update(passed=True, scope="CPU identity/inference loading only; no physical success claim")
|
|
code = 0
|
|
else:
|
|
for sig in (signal.SIGTERM, signal.SIGINT):
|
|
old_handlers[sig] = signal.signal(sig, stop)
|
|
guard()
|
|
# Existing configs/assets resolve relative to the repository, not the caller's cwd.
|
|
os.chdir(ROOT)
|
|
from isaaclab.app import AppLauncher
|
|
|
|
app = AppLauncher(headless=args.headless, device=args.device).app
|
|
guard()
|
|
import torch
|
|
from SkillFactory.tasks.manager_based.contact_cycle.recording import PhysicalSafetyAudit
|
|
from SkillFactory.tasks.manager_based.contact_cycle_sim.cfg import SimContactCycleEnvCfg
|
|
from SkillFactory.tasks.manager_based.contact_cycle_sim.env import SimContactCycleEnv
|
|
|
|
cfg = manifest["training"]
|
|
environment_cfg = SimContactCycleEnvCfg(residual_config=str(ROOT / cfg["task_config"]))
|
|
environment_cfg.scene.num_envs = args.num_envs
|
|
environment_cfg.sim.device = args.device
|
|
environment_cfg.seed = cfg["validation_seed"]
|
|
# Construction resets/warms up PhysX; a constructor failure cannot prove zero physics.
|
|
result.update(initialization_entered=True, physics_executed=None, status="initializing")
|
|
write_json(args.output / "result.json", result)
|
|
env = SimContactCycleEnv(cfg=environment_cfg)
|
|
result["physics_executed"] = True
|
|
if policy is not None:
|
|
policy = policy.to(env.device)
|
|
result["runtime"] = {"torch": str(torch.__version__), "device": str(env.device),
|
|
"seed": cfg["validation_seed"], "num_envs": env.num_envs,
|
|
"physics_dt": env.physics_dt, "control_dt": env.step_dt,
|
|
"max_control_steps": 900, "max_wall_s": 300,
|
|
"note": "Nominal development replay; numerical differences may occur"}
|
|
functional = json.loads((ROOT / cfg["functional_config"]).read_text())
|
|
thresholds = json.loads((ROOT / cfg["safety_config"]).read_text())["gates"]
|
|
with PhysicalSafetyAudit(env, thresholds) as safety:
|
|
result.update(run_episode(env, args, policy, functional, guard))
|
|
guard()
|
|
code = 0 if result["passed"] else 2
|
|
_, final_binding = verify_bundle(ROOT, checkpoint)
|
|
if final_binding != binding:
|
|
raise RuntimeError("Replay inputs changed during execution")
|
|
except BaseException:
|
|
result.update(passed=False, error=traceback.format_exc())
|
|
traceback.print_exc()
|
|
code = 1
|
|
finally:
|
|
if safety is not None:
|
|
result["physics_safety"] = safety.report()
|
|
if env is not None:
|
|
try:
|
|
env.close()
|
|
except BaseException:
|
|
result.update(passed=False, shutdown_error=traceback.format_exc())
|
|
code = 1
|
|
result.update(status="completed" if code == 0 else "failed", wall_seconds=time.monotonic() - started)
|
|
try:
|
|
write_json(args.output / "result.json", result)
|
|
finally:
|
|
for sig, handler in old_handlers.items():
|
|
signal.signal(sig, handler)
|
|
if app is not None:
|
|
app.close(exit_code=code)
|
|
return code
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|