feat: checkpoint contact-cycle evaluation and RootXY PPO progress
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.
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Nonoptimizing 46D learner API probe: 8 envs, two 32-control short fixtures.
|
||||
|
||||
No optimizer, grant, training ledger, checkpoint load or functional-skill claim.
|
||||
Production failure term and 200ms acquisition deadline remain enabled.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import signal
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import finish_run, write_json
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def model_hash(model):
|
||||
h = hashlib.sha256()
|
||||
for name, value in sorted(model.state_dict().items()):
|
||||
h.update(name.encode())
|
||||
h.update(value.detach().cpu().contiguous().numpy().tobytes())
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
def run(env, output, guard):
|
||||
import torch
|
||||
from SkillFactory.legs.contact_cycle.task import FAILURE_NAMES
|
||||
from SkillFactory.legs.contact_cycle_sim.model import SimResidualActorCritic, features
|
||||
from SkillFactory.legs.contact_cycle_sim.ppo import bootstrap_values, collect_rollout, rollout_diagnostics
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.mdp import state
|
||||
from SkillFactory.tasks.manager_based.contact_cycle_sim.recording import SimCycleRecorder
|
||||
|
||||
torch.set_num_threads(4)
|
||||
torch.manual_seed(91502)
|
||||
model = SimResidualActorCritic().to(env.device)
|
||||
before = model_hash(model)
|
||||
s = state(env)
|
||||
if (s.robot.is_fixed_base or env.cfg.terminations.failed is None or s.acquisition.deadline != .2
|
||||
or env.num_envs != 8 or env.cfg.episode_length_s != .3):
|
||||
raise RuntimeError("Require declared bounded fixture with original active failure/deadline contract")
|
||||
gates = {}
|
||||
fixtures = {}
|
||||
total_controls = 0
|
||||
for name in ("timeout", "true_failure"):
|
||||
x = features(env.reset()[0]["policy"])
|
||||
if name == "true_failure":
|
||||
pose = s.robot.data.root_state_w.torch[:1, :7].clone()
|
||||
pose[:, 2] += .5
|
||||
s.robot.write_root_pose_to_sim(pose, env_ids=torch.tensor([0], device=env.device))
|
||||
x = features(env.observation_manager.compute(update_history=False)["policy"])
|
||||
events, expected_values, terminals = [], [], []
|
||||
current = x.clone()
|
||||
with SimCycleRecorder(env) as recorder:
|
||||
class ObservedEnv:
|
||||
num_envs = env.num_envs
|
||||
device = env.device
|
||||
step_dt = env.step_dt
|
||||
|
||||
def step(self, action):
|
||||
nonlocal current, total_controls
|
||||
with torch.no_grad():
|
||||
dist, _ = model.distribution_value(current)
|
||||
recorder.before_control(current, action, dist.mean, dist.stddev)
|
||||
obs, reward, failed, timeout, info = env.step(action)
|
||||
total_controls += 1
|
||||
recorder.end_control(failed, timeout, False)
|
||||
next_x = features(obs["policy"])
|
||||
values = bootstrap_values(model, next_x, failed, timeout, info)
|
||||
expected_values.append(values.detach().clone())
|
||||
event = {"control": total_controls, "failed": failed.cpu().tolist(),
|
||||
"timeout": timeout.cpu().tolist(),
|
||||
"failure_flags": info["cycle_step"]["failure_flags"].cpu().tolist(),
|
||||
"times_after_reset_s": s.time.cpu().tolist()}
|
||||
if (failed | timeout).any():
|
||||
ids = info["terminal_ids"]
|
||||
terminal = features(info["terminal_observation"]["policy"])
|
||||
pre = recorder.frames[-1]["policy_snapshot"][ids.cpu()]
|
||||
event["terminal_ids"] = ids.cpu().tolist()
|
||||
event["terminal46"] = terminal.shape == (len(ids), 46)
|
||||
event["terminal_matches_prereset"] = bool(torch.allclose(
|
||||
terminal.cpu(), pre, atol=1e-6, rtol=0))
|
||||
event["terminal_differs_from_reset"] = bool((terminal != next_x[ids]).any(-1).all())
|
||||
event["failure_bootstrap_zero"] = bool((values[failed] == 0).all())
|
||||
timeout_only = timeout & ~failed
|
||||
selected = timeout_only[ids]
|
||||
with torch.no_grad():
|
||||
target = model.value(terminal[selected]) if selected.any() else values[:0]
|
||||
event["timeout_bootstrap_matches"] = bool(torch.allclose(
|
||||
values[ids[selected]], target, atol=1e-6, rtol=0))
|
||||
terminals.append({"ids": ids.cpu().clone(), "pre_reset": terminal.cpu().clone(),
|
||||
"post_reset": next_x[ids].cpu().clone(),
|
||||
"bootstrap": values[ids].cpu().clone()})
|
||||
events.append(event)
|
||||
current = next_x.clone()
|
||||
guard()
|
||||
return obs, reward, failed, timeout, info
|
||||
|
||||
data, _, failures, timeouts = collect_rollout(
|
||||
ObservedEnv(), model, x, {"horizon": 32, "failure_penalty": 10.}, guard)
|
||||
trace = {k: torch.stack([f[k] for f in recorder.frames]) for k in recorder.frames[0]}
|
||||
actual = trace["processed_target"] - s.nominal.cpu() - .5 * trace["reference_used_normalized"]
|
||||
with torch.no_grad():
|
||||
logp, _ = model.evaluate(data["features"].flatten(0, 1), data["latent"].flatten(0, 1))
|
||||
logp_error = float((logp.reshape_as(data["log_prob"]) - data["log_prob"]).abs().max())
|
||||
terminal_events = [e for e in events if "terminal_ids" in e]
|
||||
checks = {
|
||||
"finite_rollout": all(bool(torch.isfinite(v).all()) for v in data.values()),
|
||||
"features46": data["features"].shape == (32, 8, 46),
|
||||
"phase_cause_finite": data["failure_flags"].shape == (32, 8, 8)
|
||||
and bool(((data["phase"] >= 0) & (data["phase"] <= 5)).all()),
|
||||
"logprob_recompute": logp_error < 2e-4,
|
||||
"bootstrap_matches": bool(torch.allclose(
|
||||
data["next_value"], torch.stack(expected_values), atol=1e-6, rtol=0)),
|
||||
"terminal_events_present": bool(terminal_events),
|
||||
"terminal_contract": all(all(e[k] for k in (
|
||||
"terminal46", "terminal_matches_prereset", "terminal_differs_from_reset",
|
||||
"failure_bootstrap_zero", "timeout_bootstrap_matches")) for e in terminal_events),
|
||||
"actual_delta_matches": bool(torch.allclose(actual, trace["effective_residual_rad"], atol=1e-6, rtol=0)),
|
||||
"actual_delta_bound": float(actual.abs().max()) <= .050001,
|
||||
"template_bound": bool(((trace["processed_target"] - s.nominal.cpu()).abs() <= .500001).all()),
|
||||
"actor_metadata46": trace["actor_input"].shape[-1] == 46 and bool(trace["actor_metadata_available"].all()),
|
||||
"failure_term_still_enabled": env.cfg.terminations.failed is not None and s.acquisition.deadline == .2,
|
||||
}
|
||||
if name == "timeout":
|
||||
checks["genuine_timeouts_only"] = failures == 0 and timeouts == 8
|
||||
else:
|
||||
failure_events = [e for e in events if any(e["failed"])]
|
||||
checks["genuine_env0_failure"] = (failures == 1 and len(failure_events) == 1
|
||||
and failure_events[0]["failed"][0])
|
||||
checks["other_envs_unaffected"] = not any(any(e["failed"][1:]) for e in events)
|
||||
checks["production_acquisition_failure"] = bool(failure_events) and failure_events[0]["failure_flags"][0][
|
||||
FAILURE_NAMES.index("contact_acquisition_timeout")]
|
||||
checks["other_clocks_not_reset_on_failure"] = bool(failure_events) and all(
|
||||
t >= .19 for t in failure_events[0]["times_after_reset_s"][1:])
|
||||
checks["failure_row_zero_bootstrap"] = bool((data["next_value"][data["failure_flags"].any(-1)] == 0).all())
|
||||
gates[name] = all(checks.values())
|
||||
diagnostics = rollout_diagnostics(data, {"gamma": .99, "gae_lambda": .95})
|
||||
torch.save({"data": {k: v.cpu().clone() for k, v in data.items()}, "terminal_events": terminals},
|
||||
output / f"{name}_api.pt")
|
||||
# Entire fixture is bounded to 160 physics frames x8, not a complete-cycle claim.
|
||||
torch.save(trace, output / f"{name}_trace.pt")
|
||||
torch.save(recorder.initial_state, output / f"{name}_initial.pt")
|
||||
fixtures[name] = {"checks": checks, "failures": failures, "timeouts": timeouts,
|
||||
"logprob_max_abs_error": logp_error, "max_actual_residual_rad": float(actual.abs().max()),
|
||||
"events": events, "diagnostics": diagnostics}
|
||||
after = model_hash(model)
|
||||
gates["parameters_unchanged"] = before == after
|
||||
return {"passed": all(gates.values()), "gates": gates, "fixtures": fixtures,
|
||||
"model_sha256_before": before, "model_sha256_after": after,
|
||||
"model_seed": 91502, "environment_seed": 93502,
|
||||
"optimizer_steps": 0, "training_transitions": 0, "nonoptimizing_api_transitions": total_controls * 8,
|
||||
"physics_steps_per_env": total_controls * env.cfg.decimation,
|
||||
"aggregate_physics_transitions": total_controls * env.cfg.decimation * 8,
|
||||
"fixture_episode_s": .3, "failure_root_raise_m": .5, "acquisition_deadline_s": .2,
|
||||
"failure_term_disabled": False, "accepted_skill": False, "trained_residual_policy": False,
|
||||
"functional_claim": False}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--device", default="cuda:0")
|
||||
parser.add_argument("--headless", action="store_true", required=True)
|
||||
args = parser.parse_args()
|
||||
from SkillFactory.legs.contact_cycle_sim.training import (
|
||||
CONFIG_PATH,
|
||||
assert_no_conflicting_processes,
|
||||
make_manifest,
|
||||
writable_output,
|
||||
)
|
||||
|
||||
args.output = writable_output(ROOT, args.output)
|
||||
assert_no_conflicting_processes()
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
manifest = make_manifest(ROOT, ROOT / CONFIG_PATH)
|
||||
write_json(args.output / "identity.json", manifest)
|
||||
stop = {"requested": False}
|
||||
for sig in (signal.SIGTERM, signal.SIGINT):
|
||||
signal.signal(sig, lambda *_: stop.update(requested=True))
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
app = AppLauncher(headless=True, device=args.device).app
|
||||
env = safety = None
|
||||
result = {"passed": False, "accepted_skill": False, "optimizer_steps": 0, "training_transitions": 0}
|
||||
code = 1
|
||||
try:
|
||||
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 = SimContactCycleEnvCfg()
|
||||
cfg.scene.num_envs = 8
|
||||
cfg.sim.device = args.device
|
||||
cfg.seed = 93502
|
||||
cfg.episode_length_s = .3 # API only; production acquisition deadline and failure term UNCHANGED
|
||||
env = SimContactCycleEnv(cfg=cfg)
|
||||
thresholds = json.loads((ROOT / "configs/contact_cycle/acceptance_v1.json").read_text())["gates"]
|
||||
with PhysicalSafetyAudit(env, thresholds) as safety:
|
||||
def guard():
|
||||
if stop["requested"]:
|
||||
raise InterruptedError("Probe interrupted")
|
||||
safety.check()
|
||||
|
||||
result = run(env, args.output, guard)
|
||||
guard()
|
||||
result["physics_safety"] = safety.report()
|
||||
if manifest != make_manifest(ROOT, ROOT / CONFIG_PATH):
|
||||
raise RuntimeError("Identity changed during API probe")
|
||||
code = 0 if result["passed"] else 2
|
||||
except BaseException:
|
||||
result.update(passed=False, error=traceback.format_exc())
|
||||
traceback.print_exc()
|
||||
finally:
|
||||
if safety is not None:
|
||||
result["physics_safety"] = safety.report()
|
||||
finish_run(app, env, args.output / "result.json", result, code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user