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.
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build separate floating payload + fixed six-axis sensor boundary + original leg asset."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main():
|
||||
if not __debug__:
|
||||
raise RuntimeError("Asset verification requires assertions enabled")
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--config", type=Path, default=Path("configs/standing/go2_fl_support_v0.json"))
|
||||
AppLauncher.add_app_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
from SkillFactory.legs.spec import load_bundle
|
||||
from SkillFactory.legs.support import CONTRACT, MOUNT_JOINT, PAYLOAD, load_config, payload_urdf, sha256
|
||||
|
||||
cfg = load_config(args.config)
|
||||
source, output = Path(cfg["source_bundle"]).resolve(), Path(cfg["support_asset"]).resolve()
|
||||
bundle = load_bundle(source, require_usd=True)
|
||||
if (
|
||||
bundle["spec"]["part_id"] != "go2_fl"
|
||||
or output.exists()
|
||||
or output.is_relative_to(source)
|
||||
or source.is_relative_to(output)
|
||||
):
|
||||
raise ValueError("Require Go2 FL and a new separate output directory")
|
||||
output.mkdir(parents=True)
|
||||
for name in ["LICENSE", *bundle["resources"]]:
|
||||
dest = output / name
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(source / name, dest)
|
||||
(output / "loaded_leg.urdf").write_bytes(payload_urdf(source / "leg.urdf", cfg))
|
||||
app = AppLauncher(args).app
|
||||
code = 0
|
||||
try:
|
||||
from pxr import PhysxSchema, Usd, UsdPhysics
|
||||
|
||||
from isaaclab.sim.converters import UrdfConverter, UrdfConverterCfg
|
||||
|
||||
converter = UrdfConverter(
|
||||
UrdfConverterCfg(
|
||||
asset_path=str(output / "loaded_leg.urdf"),
|
||||
usd_dir=str(output / "usd"),
|
||||
fix_base=False,
|
||||
merge_fixed_joints=False,
|
||||
force_usd_conversion=True,
|
||||
joint_drive=UrdfConverterCfg.JointDriveCfg(target_type="none"),
|
||||
run_multi_physics_conversion=False,
|
||||
)
|
||||
)
|
||||
stage = Usd.Stage.Open(converter.usd_path)
|
||||
prefix = str(stage.GetDefaultPrim().GetPath())
|
||||
bodies = {
|
||||
p.GetName(): str(p.GetPath()).removeprefix(prefix)
|
||||
for p in stage.Traverse()
|
||||
if p.HasAPI(UsdPhysics.RigidBodyAPI)
|
||||
}
|
||||
joints = {
|
||||
p.GetName(): str(p.GetPath()).removeprefix(prefix) for p in stage.Traverse() if p.IsA(UsdPhysics.Joint)
|
||||
}
|
||||
assert PAYLOAD in bodies and bundle["anchor_body"] in bodies and MOUNT_JOINT in joints
|
||||
for prim in stage.Traverse():
|
||||
if prim.IsA(UsdPhysics.Joint):
|
||||
j = UsdPhysics.Joint(prim)
|
||||
targets = j.GetBody0Rel().GetTargets() + j.GetBody1Rel().GetTargets()
|
||||
assert len(targets) == 2 and all(
|
||||
stage.GetPrimAtPath(p).HasAPI(UsdPhysics.RigidBodyAPI) for p in targets
|
||||
), "World weld detected"
|
||||
joint = UsdPhysics.Joint(stage.GetPrimAtPath(prefix + joints[MOUNT_JOINT]))
|
||||
assert joint.GetBody0Rel().GetTargets()[0].name == PAYLOAD
|
||||
assert joint.GetBody1Rel().GetTargets()[0].name == bundle["anchor_body"]
|
||||
# Nested rigid-body hierarchy: spawn-time activation alone does not reach the foot.
|
||||
foot = stage.GetPrimAtPath(prefix + bodies[bundle["foot"]["body"]])
|
||||
PhysxSchema.PhysxContactReportAPI.Apply(foot).CreateThresholdAttr(0.0)
|
||||
stage.GetRootLayer().Save()
|
||||
layers = [Path(layer.realPath).resolve() for layer in stage.GetUsedLayers() if layer.realPath]
|
||||
assert all(p.is_relative_to(output) for p in layers)
|
||||
report = {
|
||||
"contract": CONTRACT,
|
||||
"fixed_to_world": False,
|
||||
"mount_joint": MOUNT_JOINT,
|
||||
"source_manifest_sha256": sha256(source / "manifest.json"),
|
||||
"usd_path": str(Path(converter.usd_path).relative_to(output)),
|
||||
"body_paths": bodies,
|
||||
"joint_paths": joints,
|
||||
"missing_source_inertial_links": bundle["missing_inertial_links"],
|
||||
**{k: cfg[k] for k in ("payload_mass_kg", "payload_size_m", "payload_com_m", "sensor_carrier_mass_kg")},
|
||||
}
|
||||
# USD, meshes, source and license, excluding importer's mutable cache/log files.
|
||||
files = (
|
||||
set(layers)
|
||||
| {output / "loaded_leg.urdf", output / "LICENSE"}
|
||||
| {output / name for name in bundle["resources"]}
|
||||
)
|
||||
report["files"] = {str(p.relative_to(output)): sha256(p) for p in sorted(files)}
|
||||
(output / "support.json").write_text(json.dumps(report, indent=2) + "\n")
|
||||
print(json.dumps(report, indent=2))
|
||||
except BaseException:
|
||||
code = 1
|
||||
raise
|
||||
finally:
|
||||
app.close(exit_code=code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,270 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Locked standing candidate physics trials; never optimizes or selects weights."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import finish_run, write_json
|
||||
|
||||
|
||||
def reset_probes(env, s):
|
||||
import torch
|
||||
|
||||
before, q = s.robot.data.root_state_w.torch.clone(), s.q().clone()
|
||||
ids = torch.arange(0, env.num_envs, 2, device=env.device)
|
||||
others = torch.arange(1, env.num_envs, 2, device=env.device)
|
||||
env.reset(env_ids=ids)
|
||||
indexed = bool(
|
||||
(s.mount_wrench()[ids] == 0).all()
|
||||
and (s.last_guide[ids] == 0).all()
|
||||
and (env.action_manager.action[ids] == 0).all()
|
||||
and torch.equal(before[others], s.robot.data.root_state_w.torch[others])
|
||||
and torch.equal(q[others], s.q()[others])
|
||||
)
|
||||
snapshot = {k: v.clone() for k, v in s.snapshot().items()}
|
||||
env.reset(env_ids=[])
|
||||
indexed &= all(torch.equal(v, s.snapshot()[k]) for k, v in snapshot.items())
|
||||
env.cfg.episode_length_s = 3 * env.step_dt
|
||||
env.reset()
|
||||
action = torch.full((env.num_envs, 3), 0.2, device=env.device)
|
||||
for _ in range(3):
|
||||
obs, _, term, timeout, info = env.step(action)
|
||||
timeout_ok = bool(
|
||||
not term.any()
|
||||
and timeout.all()
|
||||
and len(info["terminal_ids"]) == env.num_envs
|
||||
and torch.equal(info["terminal_observation"]["joint_state"][:, :, 2], action)
|
||||
and (obs["policy"]["joint_state"][:, :, 2] == 0).all()
|
||||
and (obs["policy"]["mount_wrench"] == 0).all()
|
||||
)
|
||||
before = env.action_manager.action.clone(), env.action_manager.prev_action.clone()
|
||||
rejected = 0
|
||||
for bad in (torch.zeros(env.num_envs, 4, device=env.device), torch.full_like(action, torch.nan)):
|
||||
try:
|
||||
env.step(bad)
|
||||
except ValueError:
|
||||
rejected += 1
|
||||
return {
|
||||
"indexed_reset": indexed,
|
||||
"timeout_terminal_reset": timeout_ok,
|
||||
"bad_action_atomic_rejection": rejected == 2
|
||||
and torch.equal(before[0], env.action_manager.action)
|
||||
and torch.equal(before[1], env.action_manager.prev_action),
|
||||
}
|
||||
|
||||
|
||||
def evaluate(env, args, policy, inputs):
|
||||
import torch
|
||||
from SkillFactory.legs.standing_ppo import canonical_hash
|
||||
from SkillFactory.tasks.manager_based.loaded_leg.mdp import state
|
||||
from standing_protocol import WIDTHS, load_at, reduce_trace
|
||||
|
||||
torch.set_num_threads(4)
|
||||
s = state(env)
|
||||
steps = round(inputs["duration_s"] / env.step_dt)
|
||||
total = steps * env.cfg.decimation
|
||||
trace = {k: torch.empty(total, env.num_envs, width, device=env.device) for k, width in WIDTHS.items()}
|
||||
params = [torch.tensor(inputs[k], device=env.device) for k in ("basis", "scale", "shift_s")]
|
||||
original_loads, original_update = s.apply_loads, env.scene.update
|
||||
frames = 0
|
||||
shots, actions, memory = [], [], []
|
||||
|
||||
def apply_loads():
|
||||
s.last_guide[:] = s.guidance()
|
||||
t = env.episode_length_buf.float() * env.step_dt
|
||||
s.last_disturbance[:] = load_at(t, *params, s.amplitude)
|
||||
wrench = s.last_guide + s.last_disturbance
|
||||
s.robot.permanent_wrench_composer.set_forces_and_torques_index(
|
||||
forces=wrench[:, None, :3],
|
||||
torques=wrench[:, None, 3:],
|
||||
positions=s.root()[0][:, None, :],
|
||||
body_ids=s.payload_ids,
|
||||
is_global=True,
|
||||
)
|
||||
|
||||
def observe(dt):
|
||||
nonlocal frames
|
||||
original_update(dt)
|
||||
if dt <= 0:
|
||||
return
|
||||
d = s.robot.data
|
||||
values = {
|
||||
"base": torch.cat(s.root(), -1),
|
||||
"foot": s.foot_center(),
|
||||
"contact": s.contact(),
|
||||
"mount": s.mount_world(),
|
||||
"guide": s.last_guide,
|
||||
"load": s.last_disturbance,
|
||||
"q": s.q(),
|
||||
"dq": d.joint_vel.torch[:, s.joints],
|
||||
"torque": d.applied_torque.torch[:, s.joints],
|
||||
}
|
||||
for key, value in values.items():
|
||||
trace[key][frames].copy_(value)
|
||||
frames += 1
|
||||
if not torch.isfinite(torch.cat(list(values.values()), -1)).all():
|
||||
raise RuntimeError("Non-finite physical/sensor state")
|
||||
|
||||
s.apply_loads, env.scene.update = apply_loads, observe
|
||||
try:
|
||||
for i in range(steps):
|
||||
obs = s.snapshot()
|
||||
action = policy.act(obs) if args.controller == "policy" else torch.zeros(env.num_envs, 3, device=env.device)
|
||||
if i % 200 == 0:
|
||||
shots.append({k: v.clone().cpu() for k, v in obs.items()})
|
||||
actions.append(action.clone().cpu())
|
||||
_, _, term, trunc, _ = env.step(action)
|
||||
if term.any() or trunc.any():
|
||||
raise RuntimeError("Unexpected auto-reset in acceptance")
|
||||
if args.mode == "stability" and (i + 1) % 100 == 0:
|
||||
output = subprocess.check_output(
|
||||
["nvidia-smi", "--query-compute-apps=pid,used_memory", "--format=csv,noheader,nounits"], text=True
|
||||
)
|
||||
rows = [line.split(",") for line in output.splitlines()]
|
||||
used = [int(row[1].strip()) * 1024**2 for row in rows if int(row[0].strip()) == os.getpid()]
|
||||
if len(used) != 1:
|
||||
raise RuntimeError("Missing/ambiguous GPU process memory sample")
|
||||
memory.append({"sim_seconds": (i + 1) * env.step_dt, "bytes": used[0]})
|
||||
if (i + 1) % 1000 == 0:
|
||||
print(f"{args.mode} {i + 1}/{steps}", flush=True)
|
||||
finally:
|
||||
s.apply_loads, env.scene.update = original_loads, original_update
|
||||
cpu = {k: v[:frames].cpu() for k, v in trace.items()}
|
||||
torch.save(cpu, args.output / "physics_trace.pt")
|
||||
if shots:
|
||||
torch.save(
|
||||
{
|
||||
"observations": {k: torch.cat([x[k] for x in shots]) for k in shots[0]},
|
||||
"actions": torch.cat(actions),
|
||||
},
|
||||
args.output / "inference_samples.pt",
|
||||
)
|
||||
write_json(args.output / "memory.json", memory)
|
||||
meta = {
|
||||
"inputs": inputs,
|
||||
"input_hash": canonical_hash(inputs),
|
||||
"physics_dt": env.physics_dt,
|
||||
"control_dt": env.step_dt,
|
||||
"height_m": env.cfg.anchor_height,
|
||||
"floating_root": not s.robot.is_fixed_base,
|
||||
"solver": "PGS" if env.cfg.sim.physics.solver_type == 0 else "TGS",
|
||||
"tgs_force_flag": env.cfg.sim.physics.enable_external_forces_every_iteration,
|
||||
"autoresets": 0,
|
||||
"total_mass_kg": s.robot.data.body_mass.torch[0].sum().item(),
|
||||
"mass_kg": dict(zip(s.robot.body_names, s.robot.data.body_mass.torch[0].tolist())),
|
||||
"limits": [[j["limits"]["lower"], j["limits"]["upper"]] for j in s.bundle["joints"]],
|
||||
"speeds": [j["limits"]["velocity"] for j in s.bundle["joints"]],
|
||||
"efforts": s.effort.tolist(),
|
||||
}
|
||||
write_json(args.output / "metadata.json", meta)
|
||||
result = reduce_trace(cpu, meta, env.cfg.support_parameters)
|
||||
if args.mode == "full":
|
||||
result["gates"].update(reset_probes(env, s))
|
||||
if args.mode == "stability":
|
||||
baseline = [v["bytes"] for v in memory if 2 <= v["sim_seconds"] <= 12]
|
||||
tail = [v["bytes"] for v in memory if 110 <= v["sim_seconds"] <= 120]
|
||||
growth = max(tail) / max(baseline) - 1 if baseline and tail else None
|
||||
result["memory_growth_fraction"] = growth
|
||||
result["gates"]["memory"] = len(baseline) >= 10 and len(tail) >= 10 and growth <= 0.05
|
||||
result.update(
|
||||
passed=all(result["gates"].values()),
|
||||
mode=args.mode,
|
||||
controller=args.controller,
|
||||
input_hash=meta["input_hash"],
|
||||
metadata=meta,
|
||||
accepted_skill=False,
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
from isaaclab_tasks.utils import add_launcher_args, launch_simulation
|
||||
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--package", type=Path, required=True)
|
||||
parser.add_argument("--manifest", required=True)
|
||||
parser.add_argument("--mode", choices=("holdout", "full", "stability", "exploratory"), required=True)
|
||||
parser.add_argument("--controller", choices=("policy", "hold"), default="policy")
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
add_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
write_json(args.output / "result.json", {"passed": False, "status": "running", "accepted_skill": False})
|
||||
app = AppLauncher(args).app
|
||||
env, code, result = None, 1, {"passed": False}
|
||||
try:
|
||||
import gymnasium as gym
|
||||
import SkillFactory.tasks # noqa: F401
|
||||
from SkillFactory.legs.support import sha256
|
||||
from SkillFactory.tasks.manager_based.loaded_leg.cfg import LoadedLegEnvCfg
|
||||
from standing_protocol import inputs_for
|
||||
|
||||
sys.path.insert(0, str(args.package.resolve()))
|
||||
from frozen_standing import FrozenStanding
|
||||
|
||||
inputs = inputs_for(args.mode)
|
||||
config = LoadedLegEnvCfg()
|
||||
config.scene.num_envs = inputs["count"]
|
||||
config.episode_length_s = inputs["duration_s"] + 1
|
||||
config.terminations.fallen, config.terminations.invalid = None, None
|
||||
source_paths = [
|
||||
"scripts/evaluate_standing.py",
|
||||
"scripts/standing_protocol.py",
|
||||
"scripts/runtime_common.py",
|
||||
"scripts/supervise_sim.py",
|
||||
]
|
||||
training_identity = json.loads((args.package / "binding.json").read_text())["training_identity"]
|
||||
for path, digest in training_identity["sources"].items():
|
||||
if sha256(path) != digest:
|
||||
raise ValueError(f"Training-bound source changed: {path}")
|
||||
source_paths += list(training_identity["sources"])
|
||||
before = {p: sha256(p) for p in set(source_paths)}
|
||||
with launch_simulation(config, args):
|
||||
env = gym.make("SkillFactory-LoadedLegStand-v0", cfg=config).unwrapped
|
||||
env.reset(seed=inputs["seed"])
|
||||
policy = FrozenStanding(
|
||||
args.package,
|
||||
manifest_sha256=args.manifest,
|
||||
task_sha256=sha256(config.support_config),
|
||||
support_sha256=sha256(Path(config.support_parameters["support_asset"]) / "support.json"),
|
||||
device=env.device,
|
||||
)
|
||||
result = evaluate(env, args, policy, inputs)
|
||||
if sha256(args.package / "manifest.json") != args.manifest or any(
|
||||
sha256(p) != h for p, h in before.items()
|
||||
):
|
||||
raise RuntimeError("Test identity changed during physics")
|
||||
for path in before:
|
||||
target = args.output / "source_snapshot" / path
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(path, target)
|
||||
result.update(
|
||||
package_manifest_sha256=args.manifest,
|
||||
actor_sha256=sha256(args.package / "actor.pt"),
|
||||
physics_trace_sha256=sha256(args.output / "physics_trace.pt"),
|
||||
source_hashes=before,
|
||||
)
|
||||
print(
|
||||
json.dumps(
|
||||
{k: v for k, v in result.items() if k not in ("rows", "metadata", "source_hashes")}, indent=2
|
||||
)
|
||||
)
|
||||
code = 0 if result["passed"] else 2
|
||||
except BaseException:
|
||||
error = traceback.format_exc()
|
||||
print(error, file=sys.stderr, flush=True)
|
||||
result = {"passed": False, "accepted_skill": False, "error": error}
|
||||
finally:
|
||||
finish_run(app, env, args.output / "result.json", result, code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,201 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Select validation checkpoint, export candidate, or finalize accepted immutable release."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import critical_log_lines, write_json
|
||||
|
||||
|
||||
def manifest(directory, cfg, status):
|
||||
from SkillFactory.legs.offline import sha256
|
||||
from stage2_acceptance import package_version
|
||||
|
||||
files = {
|
||||
str(path.relative_to(directory)): sha256(path)
|
||||
for path in sorted(directory.rglob("*"))
|
||||
if path.is_file() and path.name != "manifest.json" and "__pycache__" not in path.parts
|
||||
}
|
||||
write_json(
|
||||
directory / "manifest.json",
|
||||
{
|
||||
"format": "skillfactory.skillbundle.v1",
|
||||
"interface": "go2-fl-reach-leg-token-v1",
|
||||
"version": package_version(directory, cfg),
|
||||
"status": status,
|
||||
"frozen": True,
|
||||
"hidden": cfg["training"]["hidden"],
|
||||
"dependencies": {"python": "3.12.x", "torch": "2.10.0 (CPU validated; CUDA evaluated separately)"},
|
||||
"scope": cfg["scope"],
|
||||
"files": files,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
USAGE = """# Go2 FL Reach SkillBundle @VERSION@
|
||||
|
||||
仅固定悬空 Go2 FL,100Hz控制/500Hz物理、名义复位、2秒定点目标。
|
||||
目标为 anchor frame 足端 link xyz/L,由 q_nominal+0.25*a,a∈[-.75,.75]^3 的FK生成;不是关节目标输入。
|
||||
不支持接触承重、Go1、其他形态、移动根、噪声、步态、整机或实机。无 latent 技能库声明。
|
||||
|
||||
依赖:Python3.12,torch==2.10.0(其传递依赖由 pip 安装);CPU无Isaac依赖。
|
||||
`python -m pip install torch==2.10.0 --index-url https://download.pytorch.org/whl/cpu`
|
||||
只把本包目录加入 sys.path,然后:
|
||||
|
||||
```python
|
||||
from frozen_policy import FrozenPolicy
|
||||
import json, torch
|
||||
from pathlib import Path
|
||||
root = Path('/path/to/go2_fl_reach-@VERSION@')
|
||||
binding = json.loads((root/'binding.json').read_text())
|
||||
p = FrozenPolicy(root, part_id='go2_fl', manifest_sha256=binding['manifest_sha256'])
|
||||
tokens = torch.load(root/'example_tokens.pt', weights_only=True) # 仅接口示例;实际须提供实时状态/目标
|
||||
command = p.act(tokens) # [B,8] float32,后5槽0;前3槽声明顺序、[-1,1]
|
||||
q_target = p.position_targets(tokens) # [B,3] rad,已按soft_limits裁剪
|
||||
```
|
||||
|
||||
实际调用方必须使用**自身资产的**manifest hash(示例从包读取仅供离线演示),按binding.joint_order映射。
|
||||
输入完整旧leg-token-v1字典,字段/特征顺序见binding.feature_schema;mask bool,其余float32,同device。
|
||||
归一化为固定物理尺度再经训练集mean/std;全部必要状态保存在actor.pt,推理无统计/参数更新。
|
||||
joint_observation为(q-q_nominal)/scale、dq/URDF限速、last_applied_action;goal与foot位置均除length_scale_m。
|
||||
静态tokens/context须匹配绑定;有效槽NaN/Inf、错shape/dtype/版本/硬件hash被拒绝,padding垃圾忽略。
|
||||
执行器为显式IdealPD,Kp/Kd/effort/velocity/soft limits见binding;不能把输出直接当电机力矩。
|
||||
调用方维持控制时序、有效传感、动作历史与驱动限矩限速。无实机watchdog/故障安全认证。
|
||||
这是独立SkillBundle v1,不是声称通用SoftwareSpec v2 adapter已经实现。
|
||||
|
||||
完整性:manifest.json列出所有有效负载SHA256;外部SHA256SUMS固定manifest的信任锚。
|
||||
Hash防意外损坏,不是签名;只执行可信来源的Python代码。拒绝覆盖已有发布目录。
|
||||
任何权重、归一化、接口或元数据变化应发布新版本。查验manifest.status,candidate不是验收发布。
|
||||
"""
|
||||
|
||||
|
||||
def export(args, cfg):
|
||||
import torch
|
||||
from SkillFactory.legs.offline import canonical_hash, sha256
|
||||
|
||||
train_result = json.loads((args.run / "train/result.json").read_text())
|
||||
if not train_result["training_complete"] or train_result["updates"] != cfg["training"]["updates"]:
|
||||
raise ValueError("Formal training not completed")
|
||||
candidates = []
|
||||
for step in cfg["selection"]["candidate_updates"]:
|
||||
path = args.run / f"validation_{step:06d}/result.json"
|
||||
row = json.loads(path.read_text())
|
||||
weights = args.run / f"train/step_{step:06d}.pt"
|
||||
if row["weight_sha256"] != sha256(weights) or row["config_sha256"] != canonical_hash(cfg):
|
||||
raise ValueError("Validation provenance mismatch")
|
||||
if row["passed"] and all(row["gates"].values()):
|
||||
log_path = args.run / f"validation_{step:06d}.log"
|
||||
if not log_path.is_file() or critical_log_lines(log_path):
|
||||
raise ValueError("Missing/critical validation log")
|
||||
candidates.append((row["policy"]["mean_error_m"], step, weights))
|
||||
if not candidates:
|
||||
raise ValueError("No candidate passed pre-registered validation gates; no release")
|
||||
_, step, weights = min(candidates)
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
if getattr(args, "release_version", None):
|
||||
write_json(
|
||||
args.output / "release.json",
|
||||
{
|
||||
"format": "skillfactory.release.v1",
|
||||
"version": args.release_version,
|
||||
"training_config_sha256": canonical_hash(cfg),
|
||||
},
|
||||
)
|
||||
from stage2_acceptance import package_version
|
||||
|
||||
version = package_version(args.output, cfg)
|
||||
checkpoint = torch.load(weights, weights_only=True, map_location="cpu")
|
||||
torch.save(checkpoint["learner"]["models"]["actor"], args.output / "actor.pt")
|
||||
for name in ("binding.json", "example_tokens.pt"):
|
||||
shutil.copy2(args.run / "data" / name, args.output / name)
|
||||
shutil.copy2(
|
||||
Path(__file__).resolve().parents[1] / "source/SkillFactory/SkillFactory/legs/frozen_policy.py",
|
||||
args.output / "frozen_policy.py",
|
||||
)
|
||||
shutil.copy2(Path(cfg["bundle"]) / "LICENSE", args.output / "ASSET_LICENSE")
|
||||
shutil.copy2(args.config, args.output / "config.json")
|
||||
(args.output / "README.md").write_text(USAGE.replace("@VERSION@", version))
|
||||
(args.output / "requirements.txt").write_text("torch==2.10.0\n")
|
||||
selection = {
|
||||
"selected_update": step,
|
||||
"checkpoint_sha256": sha256(weights),
|
||||
"actor_sha256": sha256(args.output / "actor.pt"),
|
||||
"rule": cfg["selection"]["rule"],
|
||||
"selection_seeds": [cfg["selection"]["seed"] + i for i in range(cfg["selection"]["batches"])],
|
||||
"config_sha256": canonical_hash(cfg),
|
||||
"training_identity": checkpoint["identity"],
|
||||
}
|
||||
write_json(args.output / "selection.json", selection)
|
||||
manifest(args.output, cfg, "candidate")
|
||||
print(json.dumps(selection, indent=2))
|
||||
|
||||
|
||||
def finalize(args, cfg):
|
||||
from stage2_acceptance import evaluate_acceptance, evaluate_release, package_version, require_acceptance
|
||||
from verify_frozen_leg import verify_package
|
||||
|
||||
if args.output.exists():
|
||||
raise ValueError("Immutable version already exists; publish a new version")
|
||||
if getattr(args, "release_version", None) not in (None, package_version(args.candidate, cfg)):
|
||||
raise ValueError("Finalization cannot change the tested candidate version")
|
||||
# Mandatory engineering/resume/data/training/selection/physics/load gates are shared
|
||||
# with the delivery checker. Do not create any public package before they pass.
|
||||
require_acceptance(evaluate_acceptance(args.run, args.candidate, cfg))
|
||||
if not getattr(args, "python", None):
|
||||
raise ValueError("Finalization requires --python pointing to an isolated inference interpreter")
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
lock = args.output.parent / f".{args.output.name}.publish.lock"
|
||||
with lock.open("x"):
|
||||
try:
|
||||
with tempfile.TemporaryDirectory(prefix=f".{args.output.name}-", dir=args.output.parent) as tmp:
|
||||
staged = Path(tmp) / "package"
|
||||
shutil.copytree(args.candidate, staged, ignore=shutil.ignore_patterns("__pycache__"))
|
||||
evidence = staged / "evidence"
|
||||
evidence.mkdir(exist_ok=True)
|
||||
for mode in ("test", "boundary", "stability"):
|
||||
shutil.copy2(args.run / mode / "result.json", evidence / f"{mode}.json")
|
||||
for name in ("standalone.json", "resume_exact.json", "engineering_checks.json"):
|
||||
shutil.copy2(args.run / name, evidence / name)
|
||||
shutil.copy2(args.candidate / "manifest.json", evidence / "tested_candidate_manifest.json")
|
||||
for step in cfg["selection"]["candidate_updates"]:
|
||||
shutil.copy2(
|
||||
args.run / f"validation_{step:06d}/result.json", evidence / f"validation_{step:06d}.json"
|
||||
)
|
||||
for name in ("train/result.json", "data/dataset.json"):
|
||||
shutil.copy2(args.run / name, evidence / name.replace("/", "_"))
|
||||
for name in ("provenance.json", "inference_dependencies.txt"):
|
||||
if (args.run / name).is_file():
|
||||
shutil.copy2(args.run / name, evidence / name)
|
||||
# Final metadata changes the manifest hash: independently load these exact
|
||||
# staged bytes too, before the accepted version becomes publicly visible.
|
||||
manifest(staged, cfg, "accepted")
|
||||
standalone = verify_package(staged, args.python, args.run / "release_standalone.json")
|
||||
require_acceptance(evaluate_release(args.run, staged, cfg, standalone))
|
||||
if args.output.exists():
|
||||
raise ValueError("Immutable version already exists; publish a new version")
|
||||
staged.rename(args.output)
|
||||
finally:
|
||||
lock.unlink(missing_ok=True)
|
||||
print(f"Accepted immutable package: {args.output}")
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--config", type=Path, required=True)
|
||||
parser.add_argument("--run", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--candidate", type=Path, help="If supplied, finalize this candidate after independent gates")
|
||||
parser.add_argument("--python", type=Path, help="Required for finalization: isolated CPU inference interpreter")
|
||||
parser.add_argument(
|
||||
"--release-version", help="Candidate release version; does not modify the locked training config"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
cfg = json.loads(args.config.read_text())
|
||||
finalize(args, cfg) if args.candidate else export(args, cfg)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,170 @@
|
||||
#!/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()
|
||||
@@ -0,0 +1,258 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Fail-closed local immutable publication of the already-tested standing candidate."""
|
||||
|
||||
import argparse
|
||||
import ctypes
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import tarfile
|
||||
import tempfile
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import write_json
|
||||
from standing_acceptance import audit, require
|
||||
from verify_frozen_standing import digest, verify
|
||||
|
||||
NAME = "go2_fl_loaded_stand-1.0.0"
|
||||
|
||||
|
||||
def rename_no_replace(source, target):
|
||||
# Linux atomic publication, including protection against non-cooperating target races.
|
||||
libc = ctypes.CDLL(None, use_errno=True)
|
||||
function = libc.renameat2
|
||||
function.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_uint]
|
||||
function.restype = ctypes.c_int
|
||||
if function(-100, os.fsencode(source), -100, os.fsencode(target), 1) != 0:
|
||||
code = ctypes.get_errno()
|
||||
raise OSError(code, os.strerror(code), str(target))
|
||||
|
||||
|
||||
def cleanup_stage(path):
|
||||
if path and path.exists():
|
||||
for p in path.rglob("*"):
|
||||
p.chmod(0o700 if p.is_dir() else 0o600)
|
||||
path.chmod(0o700)
|
||||
shutil.rmtree(path)
|
||||
|
||||
|
||||
def inode(path):
|
||||
stat = path.stat()
|
||||
return stat.st_dev, stat.st_ino
|
||||
|
||||
|
||||
def quarantine_owned(releases, owned):
|
||||
"""Preserve rejected bytes, but remove only this invocation's official artifacts."""
|
||||
quarantine = None
|
||||
for path, identity in owned.items():
|
||||
if path.exists() and inode(path) == identity:
|
||||
if quarantine is None:
|
||||
quarantine = Path(tempfile.mkdtemp(prefix=f".{NAME}.rejected-", dir=releases))
|
||||
write_json(quarantine / "REJECTED.json", {"accepted_skill": False, "status": "rejected"})
|
||||
# Moving a directory between parents updates its '..' link on Linux.
|
||||
# Temporarily grant owner-write on this owned root only, not its contents.
|
||||
mode = path.stat().st_mode & 0o777
|
||||
if path.is_dir():
|
||||
path.chmod(mode | 0o200)
|
||||
try:
|
||||
rename_no_replace(path, quarantine / path.name)
|
||||
finally:
|
||||
remaining = quarantine / path.name if (quarantine / path.name).exists() else path
|
||||
remaining.chmod(mode)
|
||||
return str(quarantine) if quarantine else None
|
||||
|
||||
|
||||
def copy_pinned_waiver(source, target, expected_sha256):
|
||||
shutil.copyfile(source, target)
|
||||
require(digest(target) == expected_sha256, "Staged waiver differs from approved bytes")
|
||||
|
||||
|
||||
def publish(run, releases, python, *, waiver_path=None, waiver_sha256=None, engineering_path=None):
|
||||
package = run / "candidate"
|
||||
target = releases / NAME
|
||||
archive = releases / f"{NAME}.tar.gz"
|
||||
output = releases / f"{NAME}.acceptance.json"
|
||||
releases.mkdir(parents=True, exist_ok=True)
|
||||
require(not any(p.exists() for p in (target, archive, output)), "Immutable version already exists")
|
||||
lock = releases / f".{NAME}.publish.lock"
|
||||
descriptor = os.open(lock, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)
|
||||
os.close(descriptor)
|
||||
# Recheck while holding the lock, outside any handler that writes acceptance.json.
|
||||
try:
|
||||
require(not any(p.exists() for p in (target, archive, output)), "Immutable version already exists")
|
||||
except BaseException:
|
||||
lock.unlink(missing_ok=True)
|
||||
raise
|
||||
stage, temporary_archive = None, None
|
||||
owned = {}
|
||||
try:
|
||||
write_json(output, {"passed": False, "status": "running", "accepted_skill": False})
|
||||
# This re-opens and reduces all raw physics evidence; no stale passed-only shortcut.
|
||||
gate_path = run / ("publication_gate_with_deviation.json" if waiver_path else "publication_gate.json")
|
||||
options = {}
|
||||
if waiver_path is not None or waiver_sha256 is not None:
|
||||
options.update(waiver_path=waiver_path, waiver_sha256=waiver_sha256)
|
||||
if engineering_path is not None:
|
||||
options["engineering_path"] = engineering_path
|
||||
evidence = audit(run, gate_path, **options)
|
||||
require(
|
||||
evidence["passed"] and evidence["stage"] == "eligible_for_frozen_publication", "Acceptance not eligible"
|
||||
)
|
||||
stage = Path(tempfile.mkdtemp(prefix=f".{NAME}.stage-", dir=releases))
|
||||
shutil.copytree(package, stage, dirs_exist_ok=True, ignore=shutil.ignore_patterns("__pycache__"))
|
||||
old = json.loads((stage / "manifest.json").read_text())
|
||||
require(digest(stage / "manifest.json") == evidence["candidate_manifest_sha256"], "Untested candidate identity")
|
||||
for path, value in evidence["core_files"].items():
|
||||
require(digest(stage / path) == value, "Changed tested core")
|
||||
(stage / "evidence").mkdir()
|
||||
shutil.copyfile(package / "manifest.json", stage / "evidence/tested_candidate_manifest.json")
|
||||
shutil.copyfile(gate_path, stage / "evidence/acceptance_gate.json")
|
||||
engineering_file = Path(evidence.get("engineering_path", run / "engineering.json"))
|
||||
shutil.copyfile(engineering_file, stage / "evidence/engineering.json")
|
||||
for name in ("independent_candidate.json", "candidate_lock.json"):
|
||||
shutil.copyfile(run / name, stage / "evidence" / name)
|
||||
if evidence.get("waiver") is not None:
|
||||
copy_pinned_waiver(waiver_path, stage / "evidence/operator_waiver.json", evidence["waiver_sha256"])
|
||||
(stage / "DEVIATIONS.md").write_text(
|
||||
"# Accepted with an operator-approved deviation\n\n"
|
||||
"The original120s stability foot-drift gate FAILED: env6=30.620924mm, env19=30.782583mm "
|
||||
"versus30mm. Raw results and the original threshold are unchanged. "
|
||||
"The operator accepted this exact candidate and recorded trial only; "
|
||||
"see evidence/operator_waiver.json and manifest waiver_sha256. "
|
||||
"This is NOT a general31mm threshold, a new physics pass, or a hardware safety approval. "
|
||||
"No other failed gate is waived; no new weights or trial may reuse this waiver. "
|
||||
"The archive and final directory are independently loaded after eligibility checks.\n"
|
||||
)
|
||||
for name in ("full", "holdout", "hold", "stability", "exploratory"):
|
||||
shutil.copyfile(run / name / "result.json", stage / "evidence" / f"{name}.json")
|
||||
shutil.copyfile(run / f"{name}_process.json", stage / "evidence" / f"{name}_process.json")
|
||||
for key in ("pytest_log", "ruff_log", "review_report"):
|
||||
path = Path(json.loads(engineering_file.read_text())[key])
|
||||
shutil.copyfile(path, stage / "evidence" / path.name)
|
||||
old.update(
|
||||
stage="accepted_with_deviation" if evidence.get("waiver") is not None else "accepted",
|
||||
accepted_skill=True,
|
||||
acceptance_basis=evidence.get("acceptance_basis", "all_required_physics_gates"),
|
||||
unwaived_required_physics_passed=evidence.get("unwaived_required_physics_passed", True),
|
||||
waiver_sha256=evidence.get("waiver_sha256"),
|
||||
waiver_receipts=evidence.get("waiver_receipts", []),
|
||||
tested_candidate_manifest_sha256=evidence["candidate_manifest_sha256"],
|
||||
tested_core_files=evidence["core_files"],
|
||||
files={
|
||||
str(p.relative_to(stage)): digest(p)
|
||||
for p in sorted(stage.rglob("*"))
|
||||
if p.is_file() and p.name != "manifest.json"
|
||||
},
|
||||
)
|
||||
write_json(stage / "manifest.json", old)
|
||||
staged = verify(stage, python, releases / f"{NAME}.staging-load.json")
|
||||
require(staged["passed"], "Staged package independent load failed")
|
||||
# Catch evidence/core changes during the independent subprocess before committing.
|
||||
for path, value in evidence["artifacts"].items():
|
||||
require(digest(path) == value, "Evidence changed during publication")
|
||||
require(digest(package / "manifest.json") == evidence["candidate_manifest_sha256"], "Candidate changed")
|
||||
for path, value in old["files"].items():
|
||||
require(digest(stage / path) == value, "Staged package changed")
|
||||
final_hash = digest(stage / "manifest.json")
|
||||
for p in stage.rglob("*"):
|
||||
p.chmod(0o555 if p.is_dir() else 0o444)
|
||||
stage.chmod(0o555)
|
||||
owned[target] = inode(stage)
|
||||
rename_no_replace(stage, target)
|
||||
stage = None
|
||||
independent = verify(target, python, releases / f"{NAME}.independent-load.json")
|
||||
require(independent["passed"] and independent["manifest_sha256"] == final_hash, "Final independent load failed")
|
||||
with tempfile.NamedTemporaryFile(prefix=f".{NAME}.", suffix=".tar.gz", dir=releases, delete=False) as file:
|
||||
temporary_archive = Path(file.name)
|
||||
with tarfile.open(temporary_archive, "w:gz") as bundle:
|
||||
bundle.add(
|
||||
target, arcname=NAME, filter=lambda info: None if "__pycache__" in Path(info.name).parts else info
|
||||
)
|
||||
with tempfile.TemporaryDirectory(prefix="standing-archive-") as directory:
|
||||
unpacked = Path(directory)
|
||||
with tarfile.open(temporary_archive, "r:gz") as bundle:
|
||||
bundle.extractall(unpacked, filter="data")
|
||||
restored = verify(unpacked / NAME, python, releases / f"{NAME}.archive-load.json")
|
||||
require(restored["passed"] and restored["manifest_sha256"] == final_hash, "Archive independent load failed")
|
||||
for p in unpacked.rglob("*"):
|
||||
p.chmod(0o700 if p.is_dir() else 0o600)
|
||||
owned[archive] = inode(temporary_archive)
|
||||
rename_no_replace(temporary_archive, archive)
|
||||
temporary_archive = None
|
||||
archive.chmod(0o444)
|
||||
summary = {
|
||||
"passed": True,
|
||||
"status": "completed",
|
||||
"accepted_skill": True,
|
||||
"acceptance_basis": evidence.get("acceptance_basis", "all_required_physics_gates"),
|
||||
"unwaived_required_physics_passed": evidence.get("unwaived_required_physics_passed", True),
|
||||
"waiver_sha256": evidence.get("waiver_sha256"),
|
||||
"waiver_receipts": evidence.get("waiver_receipts", []),
|
||||
"waiver": evidence.get("waiver"),
|
||||
"version": "1.0.0",
|
||||
"scope": "guided single Go2 FL, PGS, synthetic3kg payload, single-axis validated loads; not hardware",
|
||||
"manifest_sha256": final_hash,
|
||||
"candidate_manifest_sha256": evidence["candidate_manifest_sha256"],
|
||||
"archive_sha256": digest(archive),
|
||||
"core_files": evidence["core_files"],
|
||||
"trials": evidence["trials"],
|
||||
"holdout_improvement_fraction": evidence["holdout_improvement_fraction"],
|
||||
"independent_candidate": True,
|
||||
"independent_staged": True,
|
||||
"independent_final": True,
|
||||
"independent_archive": True,
|
||||
"raw_trace_reduction": True,
|
||||
"training": evidence["training"],
|
||||
}
|
||||
write_json(output, summary)
|
||||
return summary
|
||||
except BaseException:
|
||||
error = traceback.format_exc()
|
||||
quarantine = quarantine_owned(releases, owned)
|
||||
write_json(
|
||||
output,
|
||||
{
|
||||
"passed": False,
|
||||
"status": "failed",
|
||||
"accepted_skill": False,
|
||||
"error": error,
|
||||
"quarantine": quarantine,
|
||||
"published_directory_exists": target.exists(),
|
||||
"published_archive_exists": archive.exists(),
|
||||
},
|
||||
)
|
||||
raise
|
||||
finally:
|
||||
cleanup_stage(stage)
|
||||
if temporary_archive:
|
||||
temporary_archive.unlink(missing_ok=True)
|
||||
lock.unlink(missing_ok=True)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--run", type=Path, required=True)
|
||||
parser.add_argument("--releases", type=Path, default=Path("releases"))
|
||||
parser.add_argument("--python", type=Path, required=True)
|
||||
parser.add_argument("--waiver", type=Path)
|
||||
parser.add_argument("--waiver-sha256")
|
||||
parser.add_argument("--engineering", type=Path)
|
||||
args = parser.parse_args()
|
||||
print(
|
||||
json.dumps(
|
||||
publish(
|
||||
args.run,
|
||||
args.releases,
|
||||
args.python,
|
||||
waiver_path=args.waiver,
|
||||
waiver_sha256=args.waiver_sha256,
|
||||
engineering_path=args.engineering,
|
||||
),
|
||||
indent=2,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,270 @@
|
||||
"""One fail-closed acceptance policy, shared by publication and later verification."""
|
||||
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import critical_log_lines
|
||||
from stage2_boundary import boundary_coverage
|
||||
|
||||
|
||||
def read_json(path):
|
||||
return json.loads(Path(path).read_text())
|
||||
|
||||
|
||||
def package_version(package, cfg):
|
||||
"""Release metadata may advance without changing the immutable training config."""
|
||||
from SkillFactory.legs.offline import canonical_hash
|
||||
|
||||
path = Path(package) / "release.json"
|
||||
if path.exists():
|
||||
metadata = read_json(path)
|
||||
if metadata.get("format") != "skillfactory.release.v1" or metadata.get(
|
||||
"training_config_sha256"
|
||||
) != canonical_hash(cfg):
|
||||
raise ValueError("Release metadata does not match training config")
|
||||
version = metadata.get("version")
|
||||
else:
|
||||
version = cfg["release_version"] # Historical v1.0.0 packages.
|
||||
if not isinstance(version, str) or not re.fullmatch(r"(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)", version):
|
||||
raise ValueError("Release version must be MAJOR.MINOR.PATCH")
|
||||
return version
|
||||
|
||||
|
||||
def clean_log(path):
|
||||
return Path(path).is_file() and not critical_log_lines(path)
|
||||
|
||||
|
||||
def independent_load_passed(report, package, manifest_hash):
|
||||
from SkillFactory.legs.offline import sha256
|
||||
|
||||
return (
|
||||
report.get("passed") is True
|
||||
and report.get("status") == "completed"
|
||||
and report.get("isolated") is True
|
||||
and report.get("package_only") is True
|
||||
and report.get("parameters_and_buffers_unchanged") is True
|
||||
and report.get("requires_grad") is False
|
||||
and report.get("training_or_simulator_imports") is False
|
||||
and report.get("actor_sha256") == sha256(package / "actor.pt")
|
||||
and report.get("manifest_sha256") == manifest_hash
|
||||
)
|
||||
|
||||
|
||||
def safety_passed(row, cfg):
|
||||
a = cfg["acceptance"]
|
||||
return (
|
||||
all(
|
||||
0 <= row[key] <= a[limit]
|
||||
for key, limit in (
|
||||
("max_normalized_torque", "max_normalized_torque"),
|
||||
("max_normalized_velocity", "max_normalized_velocity"),
|
||||
("joint_limit_excess_rad", "joint_limit_tolerance_rad"),
|
||||
("anchor_pose_error", "anchor_pose_max_error"),
|
||||
)
|
||||
)
|
||||
and row["invalid_states"] == 0
|
||||
)
|
||||
|
||||
|
||||
def goal_metrics_passed(row, cfg, mode):
|
||||
"""Recompute aggregate metrics from raw episodes, not just the saved passed bit."""
|
||||
a = cfg["acceptance"]
|
||||
boundary = mode == "boundary"
|
||||
expected = cfg["num_envs"] * cfg["test"]["boundary_batches" if boundary else "batches"]
|
||||
for name in ("policy", "zero"):
|
||||
p = row[name]
|
||||
errors, peaks = p["episode_mean_errors_m"], p["episode_peak_errors_m"]
|
||||
if len(errors) != expected or len(peaks) != expected or p["episodes"] != expected:
|
||||
return False
|
||||
# Float32 reductions of a constant window can exceed its max by a few ULPs.
|
||||
if not all(math.isfinite(v) and 0 <= v <= peak + 1e-7 for v, peak in zip(errors, peaks)):
|
||||
return False
|
||||
if not all(math.isfinite(v) for v in peaks):
|
||||
return False
|
||||
ordered = sorted(errors)
|
||||
pos = (len(errors) - 1) * 0.95
|
||||
low, high = math.floor(pos), math.ceil(pos)
|
||||
p95 = ordered[low] + (ordered[high] - ordered[low]) * (pos - low)
|
||||
success = sum(v <= a["success_radius_m"] for v in peaks) / len(peaks)
|
||||
if (
|
||||
abs(sum(errors) / len(errors) - p["mean_error_m"]) > 1e-7
|
||||
or abs(p95 - p["p95_error_m"]) > 1e-7
|
||||
or success != p["success_fraction"]
|
||||
):
|
||||
return False
|
||||
if not all(math.isfinite(p[k]) for k in ("mean_error_m", "p95_error_m", "success_fraction")):
|
||||
return False
|
||||
p = row["policy"]
|
||||
if row["zero"]["mean_error_m"] <= 0:
|
||||
return False
|
||||
reduction = 1 - p["mean_error_m"] / row["zero"]["mean_error_m"]
|
||||
return (
|
||||
math.isfinite(row["baseline_error_reduction"])
|
||||
and abs(reduction - row["baseline_error_reduction"]) < 1e-7
|
||||
and p["mean_error_m"] <= a["boundary_mean_error_m" if boundary else "mean_error_m"]
|
||||
and p["p95_error_m"] <= a["boundary_p95_error_m" if boundary else "p95_error_m"]
|
||||
and (
|
||||
boundary or (p["success_fraction"] >= a["success_fraction"] and reduction >= a["baseline_error_reduction"])
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def evaluate_acceptance(run, package, cfg, *, tested_manifest_hash=None):
|
||||
"""All semantic gates required BEFORE a release can become visible. Missing data raises."""
|
||||
from SkillFactory.legs.frozen_policy import FrozenPolicy
|
||||
from SkillFactory.legs.offline import canonical_hash, sha256
|
||||
|
||||
run, package = Path(run), Path(package)
|
||||
binding = read_json(package / "binding.json")
|
||||
FrozenPolicy(package, part_id=binding["part_id"], manifest_sha256=binding["manifest_sha256"])
|
||||
digest = canonical_hash(cfg)
|
||||
actor_hash = sha256(package / "actor.pt")
|
||||
tested_hash = tested_manifest_hash or sha256(package / "manifest.json")
|
||||
index = read_json(run / "data/dataset.json")
|
||||
training = read_json(run / "train/result.json")
|
||||
selection = read_json(package / "selection.json")
|
||||
engineering = read_json(run / "engineering_checks.json")
|
||||
resume = read_json(run / "resume_exact.json")
|
||||
checks = {}
|
||||
checks["release_metadata"] = read_json(package / "manifest.json")["version"] == package_version(package, cfg)
|
||||
checks["engineering"] = (
|
||||
engineering.get("passed") is True
|
||||
and engineering.get("git_diff_check") is True
|
||||
and engineering.get("review_blockers_remaining") == 0
|
||||
and all(
|
||||
sha256(run / log) == engineering.get(key)
|
||||
for log, key in (("pytest.log", "pytest_log_sha256"), ("ruff.log", "ruff_log_sha256"))
|
||||
)
|
||||
)
|
||||
checks["resumable"] = resume.get("passed") is True and resume.get("full_learner_optimizer_rng_exact") is True
|
||||
shards = index["shards"]
|
||||
checks["full_fixed_dataset"] = (
|
||||
index.get("complete") is True
|
||||
and index["config_sha256"] == digest
|
||||
and len(shards) == cfg["collection"]["shards"] == len({r["path"] for r in shards})
|
||||
and index["transitions"]
|
||||
== sum(r["transitions"] for r in shards)
|
||||
== cfg["collection"]["shards"] * cfg["num_envs"] * cfg["episode_steps"]
|
||||
and all(sha256(run / "data" / r["path"]) == r["sha256"] for r in shards)
|
||||
)
|
||||
checks["full_training"] = (
|
||||
training.get("passed") is True
|
||||
and training.get("training_complete") is True
|
||||
and training.get("status") == "completed"
|
||||
and training["updates"] == cfg["training"]["updates"]
|
||||
and training["identity"]["config_sha256"] == digest
|
||||
and training["identity"]["dataset_sha256"] == sha256(run / "data/dataset.json")
|
||||
)
|
||||
valid, validation_ok = [], True
|
||||
for update in cfg["selection"]["candidate_updates"]:
|
||||
row = read_json(run / f"validation_{update:06d}/result.json")
|
||||
weights = run / f"train/step_{update:06d}.pt"
|
||||
validation_ok &= row["config_sha256"] == digest and row["weight_sha256"] == sha256(weights)
|
||||
if row.get("passed") is True:
|
||||
validation_ok &= (
|
||||
row.get("status") == "completed"
|
||||
and bool(row["gates"])
|
||||
and all(value is True for value in row["gates"].values())
|
||||
and row["policy"]["episodes"] == cfg["selection"]["batches"] * cfg["num_envs"]
|
||||
and math.isfinite(row["policy"]["mean_error_m"])
|
||||
and clean_log(run / f"validation_{update:06d}.log")
|
||||
)
|
||||
valid.append((row["policy"]["mean_error_m"], update))
|
||||
checks["validation_only_selection"] = (
|
||||
validation_ok
|
||||
and bool(valid)
|
||||
and min(valid)[1] == selection["selected_update"]
|
||||
and selection["actor_sha256"] == actor_hash
|
||||
and selection["config_sha256"] == digest
|
||||
and selection["training_identity"] == training["identity"]
|
||||
and selection["checkpoint_sha256"] == sha256(run / f"train/step_{selection['selected_update']:06d}.pt")
|
||||
)
|
||||
results = {}
|
||||
for mode in ("test", "boundary", "stability"):
|
||||
row = read_json(run / mode / "result.json")
|
||||
ok = (
|
||||
row.get("passed") is True
|
||||
and row.get("status") == "completed"
|
||||
and row["weight_sha256"] == actor_hash
|
||||
and row.get("package_manifest_sha256") == tested_hash
|
||||
and row["config_sha256"] == digest
|
||||
and clean_log(run / f"{mode}.log")
|
||||
and safety_passed(row, cfg)
|
||||
)
|
||||
if mode == "stability":
|
||||
control_dt = binding["spec"]["physics_dt"] * binding["spec"]["decimation"]
|
||||
steps = round(cfg["test"]["stability_seconds"] / control_dt)
|
||||
ok &= (
|
||||
row["seconds_per_env"] == cfg["test"]["stability_seconds"]
|
||||
and row["num_envs"] == cfg["test"]["stability_envs"]
|
||||
and row["control_steps"] == steps
|
||||
and row["environment_control_steps"] == steps * row["num_envs"]
|
||||
and row["physical_samples"] >= steps * binding["spec"]["decimation"]
|
||||
and 0 <= row["memory_growth_fraction"] <= cfg["acceptance"]["memory_growth_fraction"]
|
||||
)
|
||||
else:
|
||||
ok &= goal_metrics_passed(row, cfg, mode)
|
||||
ok &= row["seed"] == cfg["test"]["boundary_seed" if mode == "boundary" else "seed"]
|
||||
if mode == "boundary":
|
||||
actions = row.get("boundary_actions", [])
|
||||
coverage = boundary_coverage(actions)
|
||||
ok &= (
|
||||
len(actions) == row["policy"]["episodes"]
|
||||
and coverage["complete"]
|
||||
and row.get("boundary_coverage") == coverage
|
||||
)
|
||||
checks[mode] = bool(ok)
|
||||
results[mode] = {
|
||||
k: ({n: v for n, v in value.items() if not n.startswith("episode_")} if k in ("policy", "zero") else value)
|
||||
for k, value in row.items()
|
||||
}
|
||||
standalone = read_json(run / "standalone.json")
|
||||
checks["independent_candidate_load"] = independent_load_passed(standalone, package, tested_hash)
|
||||
return {
|
||||
"passed": all(checks.values()),
|
||||
"milestone_complete": False,
|
||||
"checks": checks,
|
||||
"scope": cfg["scope"],
|
||||
"config_sha256": digest,
|
||||
"actor_sha256": actor_hash,
|
||||
"selected_update": selection["selected_update"],
|
||||
"training_updates": training["updates"],
|
||||
"transitions": index["transitions"],
|
||||
"evaluation": results,
|
||||
"engineering": engineering,
|
||||
}
|
||||
|
||||
|
||||
def evaluate_release(run, package, cfg, standalone):
|
||||
"""Same gates plus exact final-payload integrity and fresh isolated final-package load."""
|
||||
from SkillFactory.legs.offline import sha256
|
||||
|
||||
package = Path(package)
|
||||
tested = package / "evidence/tested_candidate_manifest.json"
|
||||
report = evaluate_acceptance(run, package, cfg, tested_manifest_hash=sha256(tested))
|
||||
manifest = read_json(package / "manifest.json")
|
||||
checks = report["checks"]
|
||||
checks["accepted_immutable_package"] = (
|
||||
manifest["status"] == "accepted"
|
||||
and manifest["frozen"] is True
|
||||
and manifest["version"] == package_version(package, cfg)
|
||||
and all(sha256(package / name) == value for name, value in manifest["files"].items())
|
||||
and all(sha256(package / name) == value for name, value in read_json(tested)["files"].items())
|
||||
)
|
||||
checks["independent_release_load"] = independent_load_passed(standalone, package, sha256(package / "manifest.json"))
|
||||
report.update(
|
||||
passed=all(checks.values()),
|
||||
milestone_complete=all(checks.values()),
|
||||
release=str(package),
|
||||
version=manifest["version"],
|
||||
manifest_sha256=sha256(package / "manifest.json"),
|
||||
independent_load=standalone,
|
||||
)
|
||||
return report
|
||||
|
||||
|
||||
def require_acceptance(report):
|
||||
if report["passed"] is not True:
|
||||
raise ValueError("Acceptance failed: " + ", ".join(k for k, v in report["checks"].items() if not v))
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Complete, auditable cube boundary strata; no simulator imports."""
|
||||
|
||||
import itertools
|
||||
import math
|
||||
|
||||
BOUNDARY_PROTOCOL = "axis-sign-strata-v2"
|
||||
BOUNDARY_LIMIT = 0.75
|
||||
STRATA = tuple(
|
||||
s for fixed in (1, 2, 3) for s in itertools.product((-1, 0, 1), repeat=3) if sum(v != 0 for v in s) == fixed
|
||||
)
|
||||
|
||||
|
||||
def apply_boundary(actions):
|
||||
"""Cycle all 6 face interiors, 12 edge interiors and 8 corners, deterministically."""
|
||||
import torch
|
||||
|
||||
if len(actions) < len(STRATA):
|
||||
raise ValueError("Boundary batches need at least 26 environments for complete coverage")
|
||||
strata = torch.tensor(STRATA, device=actions.device)[torch.arange(len(actions), device=actions.device) % 26]
|
||||
# Free coordinates must be strictly interior, including rare RNG endpoint values.
|
||||
interior = actions.clamp(-BOUNDARY_LIMIT + 1e-6, BOUNDARY_LIMIT - 1e-6)
|
||||
return torch.where(strata != 0, strata.to(actions.dtype) * BOUNDARY_LIMIT, interior)
|
||||
|
||||
|
||||
def boundary_coverage(actions):
|
||||
"""Classify actual sampled actions (not the intended schedule). JSON-safe evidence."""
|
||||
counts = {s: 0 for s in STRATA}
|
||||
for action in actions:
|
||||
if len(action) != 3 or any(not math.isfinite(v) or abs(v) > BOUNDARY_LIMIT for v in action):
|
||||
raise ValueError("Invalid boundary action")
|
||||
signature = tuple((1 if v > 0 else -1) if abs(v) == BOUNDARY_LIMIT else 0 for v in action)
|
||||
if signature not in counts:
|
||||
raise ValueError("Boundary action is not on the boundary")
|
||||
counts[signature] += 1
|
||||
return {
|
||||
"protocol": BOUNDARY_PROTOCOL,
|
||||
"strata_counts": {",".join(map(str, s)): n for s, n in counts.items()},
|
||||
**{
|
||||
name: sum(n > 0 and sum(v != 0 for v in s) == fixed for s, n in counts.items())
|
||||
for fixed, name in ((1, "faces"), (2, "edges"), (3, "corners"))
|
||||
},
|
||||
"complete": all(n > 0 for n in counts.values()),
|
||||
}
|
||||
@@ -0,0 +1,386 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Persistent Kit collection / independent evaluation for pre-registered stage2."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import finish_run, write_json
|
||||
from stage2_boundary import apply_boundary, boundary_coverage
|
||||
|
||||
|
||||
def goals(env, adapter, seed, boundary=False):
|
||||
import torch
|
||||
|
||||
generator = torch.Generator(device=env.device).manual_seed(seed)
|
||||
a = torch.rand((env.num_envs, 3), generator=generator, device=env.device) * 1.5 - 0.75
|
||||
if boundary:
|
||||
a = apply_boundary(a)
|
||||
term = env.command_manager.get_term("reach")
|
||||
term.goal_q[:] = adapter.nominal + a * adapter.scale
|
||||
term._command[:] = adapter.fk.foot_position(term.goal_q)
|
||||
return a
|
||||
|
||||
|
||||
class Audit:
|
||||
"""Observe every PhysX substep, including peaks hidden by control decimation."""
|
||||
|
||||
def __init__(self, env, adapter):
|
||||
import torch
|
||||
|
||||
self.env, self.adapter = env, adapter
|
||||
self.initial = adapter.robot.data.body_link_pose_w.torch[:, adapter.anchor].clone()
|
||||
self.maximum = torch.zeros(4, device=env.device)
|
||||
self.invalid = torch.zeros((), dtype=torch.int64, device=env.device)
|
||||
self.samples = 0
|
||||
self.low = torch.tensor([j["limits"]["lower"] for j in adapter.bundle["joints"]], device=env.device)
|
||||
self.high = torch.tensor([j["limits"]["upper"] for j in adapter.bundle["joints"]], device=env.device)
|
||||
self.velocity = torch.tensor([j["limits"]["velocity"] for j in adapter.bundle["joints"]], device=env.device)
|
||||
original = env.scene.update
|
||||
|
||||
def update(dt):
|
||||
original(dt)
|
||||
q = adapter.q()
|
||||
dq = adapter.robot.data.joint_vel.torch[:, adapter.joint_ids]
|
||||
torque = adapter.robot.data.applied_torque.torch[:, adapter.joint_ids]
|
||||
pose = adapter.robot.data.body_link_pose_w.torch[:, adapter.anchor]
|
||||
self.invalid += (~torch.isfinite(q)).sum() + (~torch.isfinite(dq)).sum()
|
||||
self.invalid += (~torch.isfinite(torque)).sum() + (~torch.isfinite(pose)).sum()
|
||||
values = torch.stack(
|
||||
(
|
||||
(torque / adapter.effort).abs().max(),
|
||||
(dq / self.velocity).abs().max(),
|
||||
torch.maximum(self.low - q, q - self.high).clamp(min=0).max(),
|
||||
(pose - self.initial).abs().max(),
|
||||
)
|
||||
)
|
||||
self.maximum = torch.maximum(self.maximum, values)
|
||||
self.samples += 1
|
||||
|
||||
env.scene.update = update
|
||||
|
||||
def report(self):
|
||||
return dict(
|
||||
zip(
|
||||
("max_normalized_torque", "max_normalized_velocity", "joint_limit_excess_rad", "anchor_pose_error"),
|
||||
self.maximum.cpu().tolist(),
|
||||
),
|
||||
invalid_states=int(self.invalid.item()),
|
||||
physical_samples=self.samples,
|
||||
)
|
||||
|
||||
|
||||
def safety_pass(row, cfg):
|
||||
a = cfg["acceptance"]
|
||||
return (
|
||||
row["max_normalized_torque"] <= a["max_normalized_torque"]
|
||||
and row["max_normalized_velocity"] <= a["max_normalized_velocity"]
|
||||
and row["joint_limit_excess_rad"] <= a["joint_limit_tolerance_rad"]
|
||||
and row["anchor_pose_error"] <= a["anchor_pose_max_error"]
|
||||
and row["invalid_states"] == 0
|
||||
)
|
||||
|
||||
|
||||
def collect(env, adapter, cfg, args):
|
||||
import torch
|
||||
from SkillFactory.legs.offline import atomic_save, canonical_hash, features, sha256
|
||||
from SkillFactory.legs.tokens import FEATURE_SCHEMA
|
||||
|
||||
config_hash = canonical_hash(cfg)
|
||||
index_path = args.output / "dataset.json"
|
||||
index = {
|
||||
"config_sha256": config_hash,
|
||||
"asset_manifest_sha256": adapter.bundle["conversion"]["manifest_sha256"],
|
||||
"feature_schema": FEATURE_SCHEMA,
|
||||
"shards": [],
|
||||
"complete": False,
|
||||
"transitions": 0,
|
||||
"bootstrap": "No true terminations. Artificial 2s truncations bootstrap from pre-reset next-state.",
|
||||
}
|
||||
if index_path.exists():
|
||||
index = json.loads(index_path.read_text())
|
||||
if (
|
||||
index["config_sha256"] != config_hash
|
||||
or index["asset_manifest_sha256"] != adapter.bundle["conversion"]["manifest_sha256"]
|
||||
):
|
||||
raise ValueError("Cannot resume collection with changed config/asset")
|
||||
for shard in index["shards"]:
|
||||
if sha256(args.output / shard["path"]) != shard["sha256"]:
|
||||
raise ValueError("Existing dataset shard corrupted")
|
||||
write_json(index_path, index)
|
||||
first_tokens = None
|
||||
for shard in range(len(index["shards"]), cfg["collection"]["shards"]):
|
||||
seed = cfg["collection"]["seed"] + shard
|
||||
env.reset(seed=seed)
|
||||
target = goals(env, adapter, seed)
|
||||
integral = torch.zeros_like(target)
|
||||
torch.manual_seed(seed + 100000)
|
||||
noise = torch.tensor(cfg["collection"]["noise_std_by_quartile"], device=env.device)
|
||||
noise = noise[torch.arange(env.num_envs, device=env.device) % len(noise)][:, None]
|
||||
rows = {k: [] for k in ("state", "next_state", "action", "reward", "terminated")}
|
||||
if first_tokens is None:
|
||||
first_tokens = {k: v.cpu().clone() for k, v in adapter.snapshot().items()}
|
||||
for _ in range(cfg["episode_steps"]):
|
||||
before = features(adapter.snapshot()).clone()
|
||||
dq = adapter.robot.data.joint_vel.torch[:, adapter.joint_ids]
|
||||
error = (adapter.nominal + target * adapter.scale - adapter.q()) / adapter.scale
|
||||
integral = (integral + cfg["collection"].get("integral_gain_s_inv", 0.0) * env.step_dt * error).clamp(
|
||||
-0.6, 0.6
|
||||
)
|
||||
action = (
|
||||
target
|
||||
+ integral
|
||||
+ cfg["collection"]["feedback_gain"] * error
|
||||
- cfg["collection"]["velocity_gain_s"] * dq / adapter.scale
|
||||
+ torch.randn_like(target) * noise
|
||||
).clamp(-1, 1)
|
||||
_, reward, terminated, truncated, _ = env.step(action)
|
||||
if terminated.any() or truncated.any():
|
||||
raise RuntimeError("Unexpected collection termination/reset")
|
||||
nxt = features(adapter.snapshot()).clone()
|
||||
if not torch.isfinite(nxt).all() or not torch.isfinite(reward).all():
|
||||
raise RuntimeError("Invalid dataset state/reward")
|
||||
for key, value in zip(rows, (before, nxt, action, reward / env.step_dt, terminated)):
|
||||
rows[key].append(value.detach().cpu().clone())
|
||||
data = {k: torch.cat(v) for k, v in rows.items()}
|
||||
path = args.output / f"shard_{shard:03d}.pt"
|
||||
atomic_save(data, path)
|
||||
index["shards"].append(
|
||||
{"path": path.name, "sha256": sha256(path), "seed": seed, "transitions": len(data["state"])}
|
||||
)
|
||||
index["transitions"] += len(data["state"])
|
||||
write_json(index_path, index)
|
||||
print(f"COLLECT shard={shard} transitions={index['transitions']}", flush=True)
|
||||
# Required package binding contains no external asset paths at inference time.
|
||||
tokens = first_tokens or {k: v.cpu().clone() for k, v in adapter.snapshot().items()}
|
||||
atomic_save({k: v[:1].clone() for k, v in tokens.items()}, args.output / "example_tokens.pt")
|
||||
bundle = adapter.bundle
|
||||
binding = {
|
||||
"part_id": bundle["spec"]["part_id"],
|
||||
"manifest_sha256": bundle["conversion"]["manifest_sha256"],
|
||||
"joint_order": bundle["joint_order"],
|
||||
"spec": bundle["spec"],
|
||||
"joints": bundle["joints"],
|
||||
"length_scale_m": bundle["length_scale_m"],
|
||||
"source": bundle["source"],
|
||||
"feature_schema": FEATURE_SCHEMA,
|
||||
"max_joints": cfg["max_joints"],
|
||||
"context": tokens["context"][0].tolist(),
|
||||
"static_tokens": {
|
||||
k: tokens[k][0].tolist()
|
||||
for k in ("joint_description", "joint_mask", "effector_description", "effector_mask")
|
||||
},
|
||||
}
|
||||
write_json(args.output / "binding.json", binding)
|
||||
index["complete"] = True
|
||||
write_json(index_path, index)
|
||||
return {"passed": True, "dataset": index}
|
||||
|
||||
|
||||
def evaluate(env, adapter, cfg, args):
|
||||
import math
|
||||
|
||||
import torch
|
||||
from SkillFactory.legs.offline import Actor, features, sha256
|
||||
|
||||
if args.package:
|
||||
sys.path.insert(0, str(args.package.resolve()))
|
||||
from frozen_policy import FrozenPolicy
|
||||
|
||||
policy = FrozenPolicy(
|
||||
args.package,
|
||||
part_id=adapter.bundle["spec"]["part_id"],
|
||||
manifest_sha256=adapter.bundle["conversion"]["manifest_sha256"],
|
||||
device=env.device,
|
||||
)
|
||||
|
||||
def act():
|
||||
return policy.act(adapter.snapshot())[:, :3]
|
||||
|
||||
digest = sha256(args.package / "actor.pt")
|
||||
package_digest = sha256(args.package / "manifest.json")
|
||||
else:
|
||||
checkpoint = torch.load(args.weights, map_location=env.device, weights_only=True)
|
||||
model = Actor(cfg["training"]["hidden"]).to(env.device).eval().requires_grad_(False)
|
||||
model.load_state_dict(checkpoint["learner"]["models"]["actor"])
|
||||
|
||||
def act():
|
||||
return model(features(adapter.snapshot()))
|
||||
|
||||
digest = sha256(args.weights)
|
||||
package_digest = None
|
||||
audit = Audit(env, adapter)
|
||||
if args.mode == "stability":
|
||||
env.reset(seed=cfg["test"]["stability_seed"])
|
||||
goals(env, adapter, cfg["test"]["stability_seed"])
|
||||
# Warm up, then measure total device used memory (PhysX/Warp included).
|
||||
for _ in range(200):
|
||||
env.step(act())
|
||||
torch.cuda.synchronize()
|
||||
free, total = torch.cuda.mem_get_info()
|
||||
used_before = total - free
|
||||
max_used = used_before
|
||||
steps = round(cfg["test"]["stability_seconds"] / env.step_dt)
|
||||
for step in range(steps):
|
||||
if step % cfg["episode_steps"] == 0:
|
||||
goals(env, adapter, cfg["test"]["stability_seed"] + 1 + step // cfg["episode_steps"])
|
||||
action = act()
|
||||
_, reward, terminated, truncated, _ = env.step(action)
|
||||
if not torch.isfinite(reward).all() or terminated.any() or truncated.any():
|
||||
raise RuntimeError("Non-finite/terminated stability trial")
|
||||
if step % 1000 == 0:
|
||||
torch.cuda.synchronize()
|
||||
free, total = torch.cuda.mem_get_info()
|
||||
max_used = max(max_used, total - free)
|
||||
print(f"STABILITY {step}/{steps}", flush=True)
|
||||
torch.cuda.synchronize()
|
||||
free, total = torch.cuda.mem_get_info()
|
||||
max_used = max(max_used, total - free)
|
||||
row = audit.report()
|
||||
row.update(
|
||||
control_steps=steps,
|
||||
environment_control_steps=steps * env.num_envs,
|
||||
seconds_per_env=steps * env.step_dt,
|
||||
num_envs=env.num_envs,
|
||||
device_used_before=used_before,
|
||||
device_used_max=max_used,
|
||||
memory_growth_fraction=(max_used - used_before) / used_before,
|
||||
weight_sha256=digest,
|
||||
package_manifest_sha256=package_digest,
|
||||
)
|
||||
row["passed"] = (
|
||||
safety_pass(row, cfg) and row["memory_growth_fraction"] <= cfg["acceptance"]["memory_growth_fraction"]
|
||||
)
|
||||
return row
|
||||
section = cfg["selection"] if args.mode == "validation" else cfg["test"]
|
||||
seed = section["boundary_seed"] if args.mode == "boundary" else section["seed"]
|
||||
batches = section["boundary_batches"] if args.mode == "boundary" else section["batches"]
|
||||
results = {}
|
||||
boundary_actions = []
|
||||
for name in ("policy", "zero"):
|
||||
errors, peaks = [], []
|
||||
for batch in range(batches):
|
||||
env.reset(seed=seed + batch)
|
||||
sampled = goals(env, adapter, seed + batch, boundary=args.mode == "boundary")
|
||||
if args.mode == "boundary" and name == "policy":
|
||||
boundary_actions.extend(sampled.cpu().tolist())
|
||||
distances = []
|
||||
for step in range(cfg["episode_steps"]):
|
||||
action = act() if name == "policy" else torch.zeros(env.num_envs, 3, device=env.device)
|
||||
_, reward, terminated, truncated, _ = env.step(action)
|
||||
if terminated.any() or truncated.any() or not torch.isfinite(reward).all():
|
||||
raise RuntimeError("Unexpected evaluation reset/non-finite reward")
|
||||
if step >= cfg["settle_steps"]:
|
||||
distances.append((adapter.foot_position() - env.command_manager.get_command("reach")).norm(dim=-1))
|
||||
distances = torch.stack(distances)
|
||||
errors.extend(distances.mean(0).cpu().tolist())
|
||||
peaks.extend(distances.max(0).values.cpu().tolist())
|
||||
values = torch.tensor(errors)
|
||||
count = sum(p <= cfg["acceptance"]["success_radius_m"] for p in peaks)
|
||||
n, p = len(errors), count / len(errors)
|
||||
z = 1.96
|
||||
center = (p + z * z / (2 * n)) / (1 + z * z / n)
|
||||
radius = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / (1 + z * z / n)
|
||||
results[name] = {
|
||||
"episodes": n,
|
||||
"mean_error_m": values.mean().item(),
|
||||
"p95_error_m": values.quantile(0.95).item(),
|
||||
"max_episode_mean_error_m": values.max().item(),
|
||||
"success_fraction": p,
|
||||
"success_wilson_95": [center - radius, center + radius],
|
||||
"episode_mean_errors_m": errors,
|
||||
"episode_peak_errors_m": peaks,
|
||||
}
|
||||
results.update(audit.report())
|
||||
a, p = cfg["acceptance"], results["policy"]
|
||||
results["baseline_error_reduction"] = 1 - p["mean_error_m"] / results["zero"]["mean_error_m"]
|
||||
boundary = args.mode == "boundary"
|
||||
results["gates"] = {
|
||||
"mean": p["mean_error_m"] <= a["boundary_mean_error_m" if boundary else "mean_error_m"],
|
||||
"p95": p["p95_error_m"] <= a["boundary_p95_error_m" if boundary else "p95_error_m"],
|
||||
"safety": safety_pass(results, cfg),
|
||||
}
|
||||
if boundary:
|
||||
results["boundary_actions"] = boundary_actions
|
||||
results["boundary_coverage"] = boundary_coverage(boundary_actions)
|
||||
results["gates"]["coverage"] = results["boundary_coverage"]["complete"]
|
||||
else:
|
||||
results["gates"].update(
|
||||
success=p["success_fraction"] >= a["success_fraction"],
|
||||
baseline=results["baseline_error_reduction"] >= a["baseline_error_reduction"],
|
||||
)
|
||||
results.update(
|
||||
passed=all(results["gates"].values()),
|
||||
weight_sha256=digest,
|
||||
package_manifest_sha256=package_digest,
|
||||
seed=seed,
|
||||
mode=args.mode,
|
||||
)
|
||||
return results
|
||||
|
||||
|
||||
def main():
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
from isaaclab_tasks.utils import add_launcher_args, launch_simulation
|
||||
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--config", type=Path, required=True)
|
||||
parser.add_argument("--mode", choices=("collect", "validation", "test", "boundary", "stability"), required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--weights", type=Path)
|
||||
parser.add_argument("--package", type=Path)
|
||||
add_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
cfg_data = json.loads(args.config.read_text())
|
||||
args.output.mkdir(parents=True, exist_ok=True)
|
||||
write_json(args.output / "result.json", {"passed": False, "status": "running"})
|
||||
app = AppLauncher(args).app
|
||||
env, code, result = None, 0, {"passed": False}
|
||||
started = time.perf_counter()
|
||||
try:
|
||||
import gymnasium as gym
|
||||
import SkillFactory.tasks # noqa: F401
|
||||
import torch
|
||||
from SkillFactory.legs.offline import canonical_hash
|
||||
from SkillFactory.tasks.manager_based.leg_skill.leg_env_cfg import LegEnvCfg
|
||||
from SkillFactory.tasks.manager_based.leg_skill.mdp import state
|
||||
|
||||
torch.set_num_threads(4)
|
||||
torch.set_float32_matmul_precision("highest")
|
||||
cfg = LegEnvCfg(bundle_dir=str(Path(cfg_data["bundle"]).resolve()), max_joints=cfg_data["max_joints"])
|
||||
cfg.scene.num_envs = cfg_data["test"]["stability_envs"] if args.mode == "stability" else cfg_data["num_envs"]
|
||||
cfg.terminations.time_out = None
|
||||
cfg.commands.reach.resampling_time_range = (1e8, 1e8)
|
||||
cfg.seed = cfg_data["collection"]["seed"]
|
||||
with launch_simulation(cfg, args):
|
||||
env = gym.make("SkillFactory-LegSkill-v0", cfg=cfg).unwrapped
|
||||
env.reset()
|
||||
adapter = state(env)
|
||||
if adapter.bundle["spec"]["part_id"] != "go2_fl" or len(adapter.joint_ids) != 3:
|
||||
raise ValueError("This experiment is bound to Go2 FL only")
|
||||
with torch.inference_mode():
|
||||
result = (
|
||||
collect(env, adapter, cfg_data, args)
|
||||
if args.mode == "collect"
|
||||
else evaluate(env, adapter, cfg_data, args)
|
||||
)
|
||||
result.update(
|
||||
config_sha256=canonical_hash(cfg_data),
|
||||
device=env.device,
|
||||
asset_manifest_sha256=adapter.bundle["conversion"]["manifest_sha256"],
|
||||
wall_time_s=time.perf_counter() - started,
|
||||
)
|
||||
except BaseException:
|
||||
result = {"passed": False, "error": traceback.format_exc()}
|
||||
print(result["error"], file=sys.stderr, flush=True)
|
||||
code = 1
|
||||
finally:
|
||||
finish_run(app, env, args.output / "result.json", result, code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,354 @@
|
||||
"""Fail-closed standing evidence audit. Re-read traces and exercise CPU runtime; no simulation."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import critical_log_lines, write_json
|
||||
|
||||
|
||||
def read_json(path):
|
||||
def invalid(value):
|
||||
raise ValueError(f"Nonfinite JSON: {value}")
|
||||
|
||||
return json.loads(Path(path).read_text(), parse_constant=invalid)
|
||||
|
||||
|
||||
def require(condition, message):
|
||||
if not condition:
|
||||
raise ValueError(message)
|
||||
|
||||
|
||||
def check_process(result, process, log, expected_success):
|
||||
require(
|
||||
process.get("cleanup", {}).get("clean") is True and process["cleanup"]["remaining"] == [], "Unclean process"
|
||||
)
|
||||
critical = critical_log_lines(log)
|
||||
require(critical == process["critical_log_lines"], "Critical log report mismatch")
|
||||
text = Path(log).read_text(errors="replace")
|
||||
require(not re.search(r"Traceback|buffer\s+overflow|CUDA.*out.of.memory", text, re.I), "Failed runtime log")
|
||||
require("error" not in result and "shutdown_error" not in result, "Trial exception/shutdown failure")
|
||||
if expected_success:
|
||||
require(result["passed"] is True and result["status"] == "completed", "Failed required trial")
|
||||
require(
|
||||
process["passed"] is True
|
||||
and process["status"] == "completed"
|
||||
and process["returncode"] == 0
|
||||
and not critical,
|
||||
"Failed required process",
|
||||
)
|
||||
elif result["passed"]:
|
||||
require(
|
||||
process["passed"] and process["returncode"] == 0 and not critical, "Baseline process failed unexpectedly"
|
||||
)
|
||||
else:
|
||||
require(
|
||||
result["status"] == "failed" and process["passed"] is False and process["returncode"] in (1, 2),
|
||||
"Invalid controlled failure",
|
||||
)
|
||||
require(
|
||||
all(
|
||||
line.startswith("[ERROR] Command failed with code 1:") and "scripts/evaluate_standing.py" in line
|
||||
for line in critical
|
||||
),
|
||||
"Unexpected critical failure",
|
||||
)
|
||||
|
||||
|
||||
def memory_gate(samples):
|
||||
require([x["sim_seconds"] for x in samples] == list(range(1, 121)), "Missing stability memory samples")
|
||||
require(all(type(x["bytes"]) is int and x["bytes"] > 0 for x in samples), "Invalid memory measurement")
|
||||
first = max(x["bytes"] for x in samples if 2 <= x["sim_seconds"] <= 12)
|
||||
last = max(x["bytes"] for x in samples if 110 <= x["sim_seconds"] <= 120)
|
||||
return last / first - 1
|
||||
|
||||
|
||||
def verify_trials(run, package, waiver_path=None, waiver_sha256=None):
|
||||
import torch
|
||||
from SkillFactory.legs.frozen_standing import FrozenStanding, checked_manifest, file_hash
|
||||
from standing_deviation import apply_drift_waiver, load_waiver
|
||||
from standing_protocol import inputs_for, reduce_trace
|
||||
|
||||
torch.set_num_threads(4)
|
||||
lock = read_json(run / "candidate_lock.json")
|
||||
manifest_hash = file_hash(package / "manifest.json")
|
||||
manifest = checked_manifest(package, lock["manifest_sha256"])
|
||||
require(
|
||||
lock["passed"] and lock["actor_extraction_max_error"] <= 1e-6 and lock["selected_update"] == 1024,
|
||||
"Invalid candidate extraction",
|
||||
)
|
||||
require(manifest["files"] == lock["core_files"], "Candidate files changed")
|
||||
b = read_json(package / "binding.json")
|
||||
cfg = read_json(package / "task.json")
|
||||
policy = FrozenStanding(
|
||||
package, manifest_sha256=manifest_hash, task_sha256=b["task_sha256"], support_sha256=b["support_sha256"]
|
||||
)
|
||||
independent = read_json(run / "independent_candidate.json")
|
||||
require(
|
||||
independent["passed"]
|
||||
and independent["status"] == "completed"
|
||||
and independent["isolated"]
|
||||
and independent["package_only"]
|
||||
and independent["manifest_sha256"] == manifest_hash
|
||||
and independent["actor_sha256"] == file_hash(package / "actor.pt")
|
||||
and independent["deterministic_batches"] == [1, 16, 64, 256]
|
||||
and independent["calls_per_batch"] == 100
|
||||
and independent["invalid_inputs_binding_corruption_rejected"]
|
||||
and independent["input_state_unchanged"]
|
||||
and not independent["forbidden_imports"]
|
||||
and independent["requires_grad"] is False,
|
||||
"Independent candidate load failed",
|
||||
)
|
||||
waiver = load_waiver(waiver_path, waiver_sha256, run, package)
|
||||
trials, artifacts, waiver_receipts = {}, {}, []
|
||||
source_bundle = read_json(Path(cfg["source_bundle"]) / "manifest.json")
|
||||
expected_mass = read_json(Path("logs/loaded_stand/v0/standing_clean/result.json"))["total_mass_kg"]
|
||||
for name, mode, controller in (
|
||||
("full", "full", "policy"),
|
||||
("holdout", "holdout", "policy"),
|
||||
("hold", "holdout", "hold"),
|
||||
("stability", "stability", "policy"),
|
||||
("exploratory", "exploratory", "policy"),
|
||||
):
|
||||
root = run / name
|
||||
report = read_json(root / "result.json")
|
||||
process = read_json(run / f"{name}_process.json")
|
||||
meta = read_json(root / "metadata.json")
|
||||
require(
|
||||
report["mode"] == mode and report["controller"] == controller and report["metadata"] == meta,
|
||||
"Trial identity/metadata mismatch",
|
||||
)
|
||||
require(meta["inputs"] == inputs_for(mode), "Wrong holdout/full/stability protocol")
|
||||
require(
|
||||
report["package_manifest_sha256"] == manifest_hash
|
||||
and report["actor_sha256"] == file_hash(package / "actor.pt"),
|
||||
"Physics tested different policy identity",
|
||||
)
|
||||
require(report["physics_trace_sha256"] == file_hash(root / "physics_trace.pt"), "Trace changed")
|
||||
require(
|
||||
abs(meta["total_mass_kg"] - expected_mass) < 1e-6 and abs(meta["height_m"] - b["height_m"]) < 1e-8,
|
||||
"Rig changed",
|
||||
)
|
||||
require(
|
||||
meta["limits"] == [[j["limits"]["lower"], j["limits"]["upper"]] for j in source_bundle["joints"]]
|
||||
and meta["speeds"] == [j["limits"]["velocity"] for j in source_bundle["joints"]]
|
||||
and meta["efforts"]
|
||||
== torch.tensor([j["limits"]["effort"] for j in source_bundle["joints"]], dtype=torch.float32).tolist(),
|
||||
"Hardware limits changed",
|
||||
)
|
||||
expected_sources = set(b["training_identity"]["sources"]) | {
|
||||
"scripts/evaluate_standing.py",
|
||||
"scripts/standing_protocol.py",
|
||||
"scripts/runtime_common.py",
|
||||
"scripts/supervise_sim.py",
|
||||
}
|
||||
require(set(report["source_hashes"]) == expected_sources, "Incomplete execution source identity")
|
||||
for path, digest in report["source_hashes"].items():
|
||||
require(
|
||||
file_hash(path) == digest == file_hash(root / "source_snapshot" / path), "Evaluation source changed"
|
||||
)
|
||||
data = torch.load(root / "physics_trace.pt", map_location="cpu", weights_only=True)
|
||||
reduced = reduce_trace(data, meta, cfg)
|
||||
del data
|
||||
require(
|
||||
report["physical_samples"] == reduced["physical_samples"] and report["rows"] == reduced["rows"],
|
||||
"Reduction mismatch",
|
||||
)
|
||||
require(report["mean_height_error_m"] == reduced["mean_height_error_m"], "Metric mismatch")
|
||||
gates = reduced["gates"].copy()
|
||||
if mode == "full":
|
||||
for key in ("indexed_reset", "timeout_terminal_reset", "bad_action_atomic_rejection"):
|
||||
require(report["gates"].get(key) is True, f"Missing real reset/action probe: {key}")
|
||||
gates[key] = True
|
||||
if mode == "stability":
|
||||
growth = memory_gate(read_json(root / "memory.json"))
|
||||
require(growth == report["memory_growth_fraction"], "Memory report mismatch")
|
||||
gates["memory"] = growth <= 0.05
|
||||
require(gates == report["gates"] and all(gates.values()) == report["passed"], "Gates/declared pass mismatch")
|
||||
required = name in ("full", "holdout", "stability")
|
||||
waived = name == "stability" and waiver is not None
|
||||
if waived:
|
||||
waiver_receipts.append(apply_drift_waiver(waiver, gates, reduced["rows"], cfg, meta))
|
||||
# A waiver covers a metric, never backend errors, incomplete traces or bad cleanup.
|
||||
check_process(report, process, run / f"{name}.log", required and not waived)
|
||||
if required:
|
||||
require(all(gates.values()) or waived, f"Required physics failed: {name}")
|
||||
samples = torch.load(root / "inference_samples.pt", map_location="cpu", weights_only=True)
|
||||
if controller == "policy":
|
||||
with torch.inference_mode():
|
||||
error = (policy.act(samples["observations"]) - samples["actions"]).abs().max().item()
|
||||
require(math.isfinite(error) and error <= 1e-5, f"CPU/CUDA inference mismatch: {error}")
|
||||
else:
|
||||
require((samples["actions"] == 0).all().item(), "Hold baseline is not zero-action")
|
||||
error = 0.0
|
||||
trials[name] = {
|
||||
"passed": report["passed"],
|
||||
"accepted_by_operator_waiver": waived,
|
||||
"gates": gates,
|
||||
"mean_height_error_m": reduced["mean_height_error_m"],
|
||||
"cpu_cuda_max_error": error,
|
||||
"physical_samples": reduced["physical_samples"],
|
||||
"max_height_error_m": max(r["max_height_error_m"] for r in report["rows"]),
|
||||
"max_tilt_rad": max(r["max_tilt_rad"] for r in report["rows"]),
|
||||
"max_foot_drift_m": max(r["max_foot_drift_m"] for r in report["rows"]),
|
||||
"min_contact_fraction": min(r["contact_fraction"] for r in report["rows"]),
|
||||
"sensor_force_error_N": reduced["sensor_static_max_force_error_N"],
|
||||
"sensor_torque_error_Nm": reduced["sensor_static_max_torque_error_Nm"],
|
||||
"memory_growth_fraction": report.get("memory_growth_fraction"),
|
||||
}
|
||||
for p in (
|
||||
root / "result.json",
|
||||
root / "metadata.json",
|
||||
root / "physics_trace.pt",
|
||||
root / "inference_samples.pt",
|
||||
root / "memory.json",
|
||||
run / f"{name}_process.json",
|
||||
run / f"{name}.log",
|
||||
):
|
||||
artifacts[str(p.resolve())] = file_hash(p)
|
||||
if waiver is not None:
|
||||
require(len(waiver_receipts) == 1, "Waiver must apply exactly once")
|
||||
artifacts[str(Path(waiver_path).resolve())] = waiver_sha256
|
||||
improvement = 1 - trials["holdout"]["mean_height_error_m"] / trials["hold"]["mean_height_error_m"]
|
||||
require(improvement >= 0.30, "Holdout improvement <30%")
|
||||
return {
|
||||
"trials": trials,
|
||||
"holdout_improvement_fraction": improvement,
|
||||
"artifacts": artifacts,
|
||||
"candidate_manifest_sha256": manifest_hash,
|
||||
"core_files": lock["core_files"],
|
||||
"acceptance_basis": "operator_waiver" if waiver is not None else "all_required_physics_gates",
|
||||
"unwaived_required_physics_passed": all(trials[k]["passed"] for k in ("full", "holdout", "stability")),
|
||||
"waiver": waiver,
|
||||
"waiver_sha256": waiver_sha256,
|
||||
"waiver_receipts": waiver_receipts,
|
||||
}
|
||||
|
||||
|
||||
def verify_training(package):
|
||||
from package_standing import SELECTED_SHA256
|
||||
from SkillFactory.legs.frozen_standing import file_hash
|
||||
from SkillFactory.legs.standing_ppo import canonical_hash
|
||||
|
||||
run = Path("logs/loaded_stand/train_v1")
|
||||
result = read_json(run / "result.json")
|
||||
config = read_json(run / "config.json")
|
||||
binding = read_json(package / "binding.json")
|
||||
require(
|
||||
result["formal_training_complete"]
|
||||
and result["passed"]
|
||||
and result["status"] == "completed"
|
||||
and result["update"] == config["updates"] == 2048
|
||||
and result["transitions"] == 16777216,
|
||||
"Incomplete training",
|
||||
)
|
||||
require(
|
||||
result["identity"] == binding["training_identity"] and result["identity"]["config"] == canonical_hash(config),
|
||||
"Training identity mismatch",
|
||||
)
|
||||
require(
|
||||
binding["checkpoint_sha256"] == SELECTED_SHA256 == file_hash(run / "checkpoints/best_validation.pt"),
|
||||
"Selected checkpoint changed",
|
||||
)
|
||||
rows = [json.loads(line) for line in (run / "metrics.jsonl").read_text().splitlines()]
|
||||
train = [r for r in rows if r["kind"] == "train"]
|
||||
require(
|
||||
[r["update"] for r in train] == list(range(1, 2049))
|
||||
and all(r["transitions"] == r["update"] * 8192 for r in train),
|
||||
"Training sequence incomplete",
|
||||
)
|
||||
values = [read_json(p) for p in sorted((run / "validation").glob("update_*.json"))]
|
||||
require([r["update"] for r in values] == list(range(256, 2049, 256)), "Missing validation")
|
||||
best = min(values, key=lambda r: (r["score"], r["update"]))
|
||||
require(
|
||||
best["update"] == 1024 and read_json(package / "selection.json")["score"] == best["score"],
|
||||
"Not validation-selected",
|
||||
)
|
||||
require(
|
||||
all(r["input_hash"] == canonical_hash(r["inputs"]) == values[0]["input_hash"] for r in values),
|
||||
"Validation inputs changed",
|
||||
)
|
||||
for path, digest in result["identity"]["sources"].items():
|
||||
require(file_hash(path) == digest, "Training-bound source changed")
|
||||
check_process(result, read_json(run.parent / "train_v1_process.json"), run.parent / "train_v1.log", True)
|
||||
return {"updates": 2048, "transitions": 16777216, "selected_update": 1024, "checkpoint_sha256": SELECTED_SHA256}
|
||||
|
||||
|
||||
def audit(run, output, waiver_path=None, waiver_sha256=None, engineering_path=None):
|
||||
from SkillFactory.legs.frozen_standing import file_hash
|
||||
|
||||
write_json(output, {"passed": False, "status": "running", "accepted_skill": False})
|
||||
try:
|
||||
package = run / "candidate"
|
||||
training = verify_training(package)
|
||||
evidence = verify_trials(run, package, waiver_path, waiver_sha256)
|
||||
engineering_path = Path(engineering_path) if engineering_path else run / "engineering.json"
|
||||
engineering = read_json(engineering_path)
|
||||
require(engineering["passed"] is True and engineering["review_passed"] is True, "Engineering/review not passed")
|
||||
require(
|
||||
re.search(r"(?i)(?:merge verdict|verdict).{0,8}\bOK\b", Path(engineering["review_report"]).read_text()),
|
||||
"Missing positive final review verdict",
|
||||
)
|
||||
require(
|
||||
{
|
||||
"scripts/standing_acceptance.py",
|
||||
"scripts/publish_standing.py",
|
||||
"scripts/verify_frozen_standing.py",
|
||||
"tests/test_standing_acceptance.py",
|
||||
engineering["review_report"],
|
||||
engineering["pytest_log"],
|
||||
engineering["ruff_log"],
|
||||
}.issubset(engineering["files"]),
|
||||
"Incomplete engineering identity",
|
||||
)
|
||||
if waiver_path is not None:
|
||||
require("scripts/standing_deviation.py" in engineering["files"], "Missing waiver code review identity")
|
||||
for path, digest in engineering["files"].items():
|
||||
require(file_hash(path) == digest, "Engineering evidence changed")
|
||||
require("All checks passed!" in Path(engineering["ruff_log"]).read_text(), "Ruff not passed")
|
||||
require(
|
||||
re.search(r"\d+ passed", Path(engineering["pytest_log"]).read_text())
|
||||
and not re.search(r"\d+ failed|ERROR collecting", Path(engineering["pytest_log"]).read_text()),
|
||||
"Tests failed",
|
||||
)
|
||||
root = run.parent
|
||||
wall = sum(read_json(p)["wall_seconds"] for p in root.rglob("*_process.json"))
|
||||
size = sum(p.stat().st_size for p in root.rglob("*") if p.is_file())
|
||||
require(wall <= 1800 and size < 2 * 1024**3, "Acceptance resource budget exceeded")
|
||||
evidence.update(
|
||||
passed=True,
|
||||
status="completed",
|
||||
accepted_skill=False,
|
||||
stage="eligible_for_frozen_publication",
|
||||
training=training,
|
||||
physics_wall_seconds=wall,
|
||||
artifact_bytes=size,
|
||||
)
|
||||
evidence["engineering_path"] = str(engineering_path.resolve())
|
||||
for path in (engineering_path, run / "candidate_lock.json", run / "independent_candidate.json"):
|
||||
evidence["artifacts"][str(path.resolve())] = file_hash(path)
|
||||
write_json(output, evidence)
|
||||
return evidence
|
||||
except BaseException:
|
||||
write_json(
|
||||
output, {"passed": False, "status": "failed", "accepted_skill": False, "error": traceback.format_exc()}
|
||||
)
|
||||
raise
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--run", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--waiver", type=Path)
|
||||
parser.add_argument("--waiver-sha256")
|
||||
parser.add_argument("--engineering", type=Path)
|
||||
args = parser.parse_args()
|
||||
audit(args.run, args.output, args.waiver, args.waiver_sha256, args.engineering)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,86 @@
|
||||
"""Explicit, hash-pinned operator exception for one recorded drift failure. No threshold edits."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def sha256(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def reject_constant(value):
|
||||
raise ValueError(f"Nonfinite waiver JSON: {value}")
|
||||
|
||||
|
||||
def load_waiver(path, expected_sha256, run, candidate):
|
||||
if path is None and expected_sha256 is None:
|
||||
return None
|
||||
if path is None or expected_sha256 is None:
|
||||
raise ValueError("Explicit pinned waiver identity required")
|
||||
content = Path(path).read_bytes()
|
||||
if hashlib.sha256(content).hexdigest() != expected_sha256:
|
||||
raise ValueError("Explicit pinned waiver identity required")
|
||||
waiver = json.loads(content, parse_constant=reject_constant)
|
||||
binding = json.loads((candidate / "binding.json").read_text())
|
||||
if (
|
||||
waiver["format"] != "skillfactory.standing-deviation.v1"
|
||||
or waiver["authorization"] != "explicit_user_instruction"
|
||||
or waiver["approved"] is not True
|
||||
or not waiver["operator_statement"].strip()
|
||||
or waiver["scope"] != "exact_candidate_and_trial_only"
|
||||
or waiver["waived_gate"] != "stability.foot_drift"
|
||||
or waiver["original_limit_m"] != 0.03
|
||||
or waiver["duration_s"] != 120.0
|
||||
or waiver["num_envs"] != 26
|
||||
or waiver["physical_samples"] != 60000
|
||||
):
|
||||
raise ValueError("Unsupported waiver scope")
|
||||
for key, actual in (
|
||||
("candidate_manifest_sha256", sha256(candidate / "manifest.json")),
|
||||
("actor_sha256", sha256(candidate / "actor.pt")),
|
||||
("checkpoint_sha256", binding["checkpoint_sha256"]),
|
||||
):
|
||||
if waiver[key] != actual:
|
||||
raise ValueError("Waiver is for a different candidate")
|
||||
required = {
|
||||
"stability/result.json",
|
||||
"stability/metadata.json",
|
||||
"stability/physics_trace.pt",
|
||||
"stability/memory.json",
|
||||
"stability_process.json",
|
||||
"stability.log",
|
||||
}
|
||||
if set(waiver["evidence_sha256"]) != required:
|
||||
raise ValueError("Incomplete waiver evidence identity")
|
||||
for name, digest in waiver["evidence_sha256"].items():
|
||||
if sha256(run / name) != digest:
|
||||
raise ValueError("Waived evidence changed")
|
||||
return waiver
|
||||
|
||||
|
||||
def apply_drift_waiver(waiver, gates, rows, cfg, metadata):
|
||||
"""Return an exception receipt; never mutate raw gates, rows, or original limits."""
|
||||
if (
|
||||
{k for k, v in gates.items() if not v} != {"foot_drift"}
|
||||
or cfg["foot_drift_limit_m"] != waiver["original_limit_m"]
|
||||
or metadata["inputs"]["mode"] != "stability"
|
||||
or metadata["inputs"]["duration_s"] != waiver["duration_s"]
|
||||
or metadata["inputs"]["count"] != waiver["num_envs"]
|
||||
or len(rows) != waiver["num_envs"]
|
||||
):
|
||||
raise ValueError("Waiver cannot cover additional failures or a different protocol")
|
||||
failures = [
|
||||
{"environment": i, "max_foot_drift_m": row["max_foot_drift_m"]}
|
||||
for i, row in enumerate(rows)
|
||||
if row["max_foot_drift_m"] > waiver["original_limit_m"]
|
||||
]
|
||||
if failures != waiver["observed_failures"] or [r["environment"] for r in failures] != [6, 19]:
|
||||
raise ValueError("Waiver cannot cover different drift observations")
|
||||
return {
|
||||
"accepted_by_operator_waiver": True,
|
||||
"raw_gate_passed": False,
|
||||
"gate": "stability.foot_drift",
|
||||
"original_limit_m": waiver["original_limit_m"],
|
||||
"observed_failures": failures,
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
"""Fixed standing evaluation inputs and independent CPU raw-trace reductions; no Kit."""
|
||||
|
||||
import itertools
|
||||
|
||||
import torch
|
||||
from SkillFactory.legs.standing_ppo import canonical_hash
|
||||
|
||||
MODES = {
|
||||
"holdout": (256, 14.0, 83000),
|
||||
"full": (13, 14.0, 71000),
|
||||
"stability": (26, 120.0, 85000),
|
||||
"exploratory": (60, 14.0, 84000),
|
||||
}
|
||||
WIDTHS = {"base": 13, "foot": 3, "contact": 3, "mount": 6, "guide": 6, "load": 6, "q": 3, "dq": 3, "torque": 3}
|
||||
|
||||
|
||||
def inputs_for(mode):
|
||||
count, duration, seed = MODES[mode]
|
||||
gen = torch.Generator().manual_seed(seed)
|
||||
basis = torch.zeros(count, 6)
|
||||
scale, shift = torch.ones(count), torch.zeros(count)
|
||||
if mode == "exploratory":
|
||||
for i, ((a, b), (sa, sb)) in enumerate(
|
||||
itertools.product(itertools.combinations(range(6), 2), itertools.product((-1, 1), repeat=2))
|
||||
):
|
||||
basis[i, a], basis[i, b] = sa, sb
|
||||
else:
|
||||
scenarios = torch.arange(count) % 13
|
||||
if mode == "holdout":
|
||||
scenarios = scenarios[torch.randperm(count, generator=gen)]
|
||||
scale = 0.6 + 0.4 * torch.rand(count, generator=gen)
|
||||
shift = -0.25 + 0.5 * torch.rand(count, generator=gen)
|
||||
mask = scenarios != 0
|
||||
basis[torch.arange(count)[mask], (scenarios[mask] - 1) // 2] = torch.where(scenarios[mask] % 2 == 1, 1.0, -1.0)
|
||||
return {
|
||||
"mode": mode,
|
||||
"count": count,
|
||||
"duration_s": duration,
|
||||
"seed": seed,
|
||||
"basis": basis.tolist(),
|
||||
"scale": scale.tolist(),
|
||||
"shift_s": shift.tolist(),
|
||||
"period_s": 14.0,
|
||||
}
|
||||
|
||||
|
||||
def load_at(time, basis, scale, shift, amplitude):
|
||||
# time may be scalar or [steps,1]; trailing dimensions broadcast against environments.
|
||||
phase = time.remainder(14.0) - shift
|
||||
env = torch.zeros_like(phase)
|
||||
for start in (3.0, 8.0):
|
||||
env += torch.minimum((phase - start) / 0.5, (start + 3.0 - phase) / 0.5).clamp(0, 1)
|
||||
return env[..., None] * scale[..., None] * basis * amplitude
|
||||
|
||||
|
||||
def rotate_xyzw(q, v):
|
||||
cross = torch.cross(q[..., :3], v, dim=-1)
|
||||
return v + 2 * (q[..., 3:] * cross + torch.cross(q[..., :3], cross, dim=-1))
|
||||
|
||||
|
||||
def calibration_errors(measured, expected, duration):
|
||||
"""Never pool cycles: a bad plateau must not cancel or dilute."""
|
||||
residuals, counts = [], []
|
||||
for cycle in range(int(duration // 14) + 1):
|
||||
for start, end in ((2.0, 3.0), (4.5, 5.5), (9.5, 10.5)):
|
||||
a, b = cycle * 14 + start, cycle * 14 + end
|
||||
if b > duration:
|
||||
continue
|
||||
lo, hi = round(a / 0.002) - 1, round(b / 0.002) - 1
|
||||
residuals.append((measured[lo:hi].double().mean(0) - expected[lo:hi].double().mean(0)).abs())
|
||||
counts.append(len(measured[lo:hi]))
|
||||
return torch.stack(residuals), counts
|
||||
|
||||
|
||||
def reduce_trace(data, meta, cfg):
|
||||
"""Recompute safety/standing/calibration/coverage from tensors, not reported booleans."""
|
||||
inputs = meta["inputs"]
|
||||
n, count = round(inputs["duration_s"] / 0.002), inputs["count"]
|
||||
if set(data) != set(WIDTHS) or any(
|
||||
v.shape != (n, count, WIDTHS[k]) or v.dtype != torch.float32 or not torch.isfinite(v).all()
|
||||
for k, v in data.items()
|
||||
):
|
||||
raise ValueError("Incomplete/nonfinite physical trace")
|
||||
if inputs != inputs_for(inputs["mode"]) or meta["input_hash"] != canonical_hash(inputs):
|
||||
raise ValueError("Protocol input mismatch")
|
||||
if meta["physics_dt"] != 0.002 or meta["control_dt"] != 0.01:
|
||||
raise ValueError("Unexpected simulation clocks")
|
||||
times = torch.arange(1, n + 1, dtype=torch.float64) * 0.002
|
||||
settled = times >= cfg["warmup_seconds"]
|
||||
base = data["base"].double()
|
||||
quat = base[..., 3:7]
|
||||
up = torch.zeros_like(quat[..., :3])
|
||||
up[..., 2] = 1
|
||||
tilt = torch.acos(rotate_xyzw(quat, up)[..., 2].clamp(-1, 1))
|
||||
error = (base[..., 2] - meta["height_m"]).abs()
|
||||
foot = data["foot"][settled].double()
|
||||
drift = (foot[..., :2] - foot[0, :, :2]).norm(dim=-1)
|
||||
contact = data["contact"][settled, :, 2].double()
|
||||
required = meta["total_mass_kg"] * 9.81 - data["load"][settled, :, 2].double()
|
||||
limits, speeds, efforts = [torch.tensor(meta[k], dtype=torch.float64) for k in ("limits", "speeds", "efforts")]
|
||||
q = data["q"].double()
|
||||
excess = torch.maximum(limits[:, 0] - q, q - limits[:, 1]).clamp(min=0)
|
||||
# Reconstruct entire applied load sequence independently of the simulator hook.
|
||||
ctrl_t = (torch.arange(n) // 5).float() * 0.01
|
||||
expected_load = load_at(
|
||||
ctrl_t[:, None],
|
||||
torch.tensor(inputs["basis"]),
|
||||
torch.tensor(inputs["scale"]),
|
||||
torch.tensor(inputs["shift_s"]),
|
||||
torch.tensor(cfg["disturbance_amplitude"]),
|
||||
)
|
||||
load_error = (expected_load - data["load"]).abs().max().item()
|
||||
metrics = {
|
||||
"mean_height_error_m": error[settled].mean(0),
|
||||
"max_height_error_m": error[settled].amax(0),
|
||||
"max_height_error_all_m": error.amax(0),
|
||||
"max_tilt_rad": tilt[settled].amax(0),
|
||||
"max_foot_drift_m": drift.amax(0),
|
||||
"contact_fraction": (contact > meta["total_mass_kg"] * 9.81 * 0.2).double().mean(0),
|
||||
"vertical_balance_relative_error": (contact.mean(0) - required.mean(0)).abs() / required.mean(0),
|
||||
"max_normalized_torque": (data["torque"].double() / efforts).abs().amax((0, 2)),
|
||||
"max_normalized_velocity": (data["dq"].double() / speeds).abs().amax((0, 2)),
|
||||
"max_joint_limit_excess_rad": excess.amax((0, 2)),
|
||||
"max_guide_fz_N": data["guide"][..., 2].abs().amax(0).double(),
|
||||
"fallen": ((base[..., 2] < meta["height_m"] - 0.10) | (tilt > torch.pi * 35 / 180)).any(0),
|
||||
}
|
||||
gravity = torch.zeros_like(base[..., :3])
|
||||
gravity[..., 2] = -cfg["payload_mass_kg"] * 9.81
|
||||
com = torch.tensor(cfg["payload_com_m"], dtype=torch.float64).expand_as(gravity)
|
||||
lever = rotate_xyzw(quat, com)
|
||||
oracle = (
|
||||
torch.cat((gravity, torch.cross(lever, gravity, dim=-1)), -1) + data["guide"].double() + data["load"].double()
|
||||
)
|
||||
residual, counts = calibration_errors(data["mount"], oracle, inputs["duration_s"])
|
||||
force, torque = residual[..., :3].max().item(), residual[..., 3:].max().item()
|
||||
gates = {
|
||||
"finite_complete_trace": True,
|
||||
"floating_root": meta["floating_root"] is True,
|
||||
"pgs_compatible": meta["solver"] == "PGS" and meta["tgs_force_flag"] is False,
|
||||
"no_autoreset": meta["autoresets"] == 0,
|
||||
"no_fall": not metrics["fallen"].any().item(),
|
||||
"applied_load_matches_protocol": load_error <= 1e-4,
|
||||
"quaternion_norm": (quat.norm(dim=-1) - 1).abs().max().item() <= 0.002,
|
||||
"no_vertical_guide": metrics["max_guide_fz_N"].max().item() == 0,
|
||||
}
|
||||
for name, key in (
|
||||
("height", "height_error_limit_m"),
|
||||
("tilt", "tilt_limit_rad"),
|
||||
("foot_drift", "foot_drift_limit_m"),
|
||||
):
|
||||
field = {"height": "max_height_error_m", "tilt": "max_tilt_rad", "foot_drift": "max_foot_drift_m"}[name]
|
||||
gates[name] = bool((metrics[field] <= cfg[key]).all())
|
||||
gates["contact"] = bool((metrics["contact_fraction"] >= cfg["contact_fraction_min"]).all())
|
||||
gates["vertical_balance"] = bool(
|
||||
(metrics["vertical_balance_relative_error"] <= cfg["vertical_balance_relative_tolerance"]).all()
|
||||
)
|
||||
gates["limits"] = all(
|
||||
bool((metrics[k] <= cfg[limit]).all())
|
||||
for k, limit in (
|
||||
("max_normalized_torque", "normalized_effort_velocity_limit"),
|
||||
("max_normalized_velocity", "normalized_effort_velocity_limit"),
|
||||
("max_joint_limit_excess_rad", "joint_limit_excess_rad"),
|
||||
)
|
||||
)
|
||||
# Quasi-static metrology gate only on fixed full-amplitude plateaus, not shifted transient windows.
|
||||
if inputs["mode"] in ("full", "stability"):
|
||||
gates.update(
|
||||
sensor_static_force=min(counts) >= 499 and force <= cfg["sensor_static_force_tolerance_N"],
|
||||
sensor_static_torque=min(counts) >= 499 and torque <= cfg["sensor_static_torque_tolerance_Nm"],
|
||||
)
|
||||
return {
|
||||
"passed": all(gates.values()),
|
||||
"gates": gates,
|
||||
"physical_samples": n,
|
||||
"mean_height_error_m": metrics["mean_height_error_m"].mean().item(),
|
||||
"rows": [{k: v[i].item() for k, v in metrics.items()} for i in range(count)],
|
||||
"load_reconstruction_max_error": load_error,
|
||||
"calibration_counts": counts,
|
||||
"sensor_static_max_force_error_N": force,
|
||||
"sensor_static_max_torque_error_Nm": torque,
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Linux process-group supervisor; clean up only this command's adopted descendants."""
|
||||
|
||||
import argparse
|
||||
import ctypes
|
||||
import os
|
||||
import signal
|
||||
import subprocess
|
||||
import time
|
||||
from contextlib import suppress
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import critical_log_lines, write_json
|
||||
|
||||
|
||||
def owned_processes(pgid):
|
||||
rows = []
|
||||
for path in Path("/proc").glob("[0-9]*/stat"):
|
||||
try:
|
||||
text = path.read_text()
|
||||
fields = text.rsplit(") ", 1)[1].split()
|
||||
if int(fields[2]) == pgid or int(fields[1]) == os.getpid():
|
||||
rows.append(
|
||||
{
|
||||
"pid": int(path.parent.name),
|
||||
"state": fields[0],
|
||||
"pgid": int(fields[2]),
|
||||
"name": text.split("(", 1)[1].rsplit(")", 1)[0],
|
||||
}
|
||||
)
|
||||
except (FileNotFoundError, ProcessLookupError):
|
||||
pass
|
||||
return rows
|
||||
|
||||
|
||||
def cleanup(pgid):
|
||||
before = owned_processes(pgid)
|
||||
for sig in (signal.SIGTERM, signal.SIGKILL):
|
||||
for row in owned_processes(pgid):
|
||||
if row["state"] != "Z":
|
||||
with suppress(ProcessLookupError):
|
||||
os.kill(row["pid"], sig)
|
||||
for _ in range(30):
|
||||
try:
|
||||
while os.waitpid(-1, os.WNOHANG)[0]:
|
||||
pass
|
||||
except ChildProcessError:
|
||||
pass
|
||||
if not owned_processes(pgid):
|
||||
return {"before": before, "remaining": [], "clean": True}
|
||||
time.sleep(0.1)
|
||||
remaining = owned_processes(pgid)
|
||||
return {"before": before, "remaining": remaining, "clean": not remaining}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--log", type=Path, required=True)
|
||||
parser.add_argument("--report", type=Path, required=True)
|
||||
parser.add_argument("--timeout", type=float, default=420)
|
||||
parser.add_argument("command", nargs=argparse.REMAINDER)
|
||||
args = parser.parse_args()
|
||||
command = args.command[1:] if args.command[:1] == ["--"] else args.command
|
||||
if not command or args.timeout <= 0 or args.log.exists() or args.report.exists():
|
||||
parser.error("Require command, positive timeout and unused log/report paths")
|
||||
if ctypes.CDLL(None, use_errno=True).prctl(36, 1, 0, 0, 0) != 0:
|
||||
raise RuntimeError("Linux subreaper setup failed")
|
||||
args.log.parent.mkdir(parents=True, exist_ok=True)
|
||||
info = {"passed": False, "status": "running", "command": command}
|
||||
write_json(args.report, info)
|
||||
start = time.monotonic()
|
||||
with args.log.open("x") as stream:
|
||||
child = subprocess.Popen(command, stdout=stream, stderr=subprocess.STDOUT, start_new_session=True)
|
||||
info["pid"] = child.pid
|
||||
try:
|
||||
child.wait(timeout=args.timeout)
|
||||
except BaseException as error:
|
||||
info["error"] = repr(error)
|
||||
os.killpg(child.pid, signal.SIGTERM)
|
||||
try:
|
||||
child.wait(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
os.killpg(child.pid, signal.SIGKILL)
|
||||
child.wait()
|
||||
finally:
|
||||
info["cleanup"] = cleanup(child.pid)
|
||||
info["returncode"] = child.returncode
|
||||
info["wall_seconds"] = time.monotonic() - start
|
||||
info["critical_log_lines"] = critical_log_lines(args.log)
|
||||
info["passed"] = (
|
||||
child.returncode == 0
|
||||
and info["cleanup"]["clean"]
|
||||
and "error" not in info
|
||||
and not info["critical_log_lines"]
|
||||
)
|
||||
info["status"] = "completed" if info["passed"] else "failed"
|
||||
write_json(args.report, info)
|
||||
print(info)
|
||||
raise SystemExit(0 if info["passed"] else 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,420 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Persistent-Kit PPO for the current loaded-standing task. Training is not acceptance."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
import time
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import critical_log_lines, write_json
|
||||
|
||||
|
||||
def source_identity(cfg):
|
||||
import hashlib
|
||||
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
files = [Path(__file__), root / "scripts/runtime_common.py", root / "scripts/supervise_sim.py"]
|
||||
files += list((root / "source/SkillFactory/SkillFactory/legs").glob("*.py"))
|
||||
files += list((root / "source/SkillFactory/SkillFactory/tasks/manager_based/loaded_leg").glob("*.py"))
|
||||
files += list((root / "source/SkillFactory/SkillFactory/tasks/manager_based/leg_skill").glob("*.py"))
|
||||
files += list((root / "source/SkillFactory/SkillFactory/tasks/manager_based/single_leg_anchor/mdp").glob("*.py"))
|
||||
task = json.loads(Path(cfg["task_config"]).read_text())
|
||||
files += [
|
||||
Path(cfg["task_config"]).resolve(),
|
||||
root / "source/SkillFactory/SkillFactory/legs/resources/ground.usda",
|
||||
(root / task["support_asset"] / "support.json"),
|
||||
(root / task["source_bundle"] / "manifest.json"),
|
||||
]
|
||||
return {str(p.relative_to(root)): hashlib.sha256(p.read_bytes()).hexdigest() for p in files}
|
||||
|
||||
|
||||
def checked_checkpoint(args, cfg, model, optimizer, identity, update, elapsed, best, label):
|
||||
from SkillFactory.legs.standing_ppo import save_checkpoint
|
||||
|
||||
if source_identity(cfg) != identity["sources"]:
|
||||
raise RuntimeError("Training source changed while running")
|
||||
if args.console_log and critical_log_lines(args.console_log):
|
||||
raise RuntimeError("Critical Kit log; refusing checkpoint success")
|
||||
save_checkpoint(args.output / "checkpoints" / f"{label}.pt", model, optimizer, identity, update, elapsed, best)
|
||||
write_json(
|
||||
args.output / "latest_checkpoint.json",
|
||||
{"path": f"checkpoints/{label}.pt", "update": update, "resume_physics_exact": False, "accepted_skill": False},
|
||||
)
|
||||
size = sum(p.stat().st_size for p in args.output.rglob("*") if p.is_file())
|
||||
if size > cfg["max_disk_bytes"]:
|
||||
raise RuntimeError("Training disk budget exceeded")
|
||||
|
||||
|
||||
def train(env, cfg, args):
|
||||
import shutil
|
||||
|
||||
import torch
|
||||
from SkillFactory.legs.standing_ppo import (
|
||||
FEATURE_VERSION,
|
||||
StandingActorCritic,
|
||||
canonical_hash,
|
||||
collect_rollout,
|
||||
features,
|
||||
ppo_step,
|
||||
restore_checkpoint,
|
||||
restore_rng,
|
||||
rng_state,
|
||||
selection_height_error,
|
||||
)
|
||||
from SkillFactory.tasks.manager_based.loaded_leg.mdp import state
|
||||
|
||||
torch.set_num_threads(4)
|
||||
torch.set_float32_matmul_precision("highest")
|
||||
torch.manual_seed(cfg["seed"])
|
||||
torch.cuda.manual_seed_all(cfg["seed"])
|
||||
model = StandingActorCritic(cfg["hidden"], cfg["initial_std"]).to(env.device)
|
||||
optimizer = torch.optim.Adam(model.parameters(), lr=cfg["learning_rate"], eps=1e-5)
|
||||
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
|
||||
):
|
||||
raise RuntimeError("Require floating base, PGS and compatible external-force settings")
|
||||
identity = {
|
||||
"config": canonical_hash(cfg),
|
||||
"sources": source_identity(cfg),
|
||||
"features": FEATURE_VERSION,
|
||||
"probe_episode_s": args.probe_episode_s,
|
||||
"device": str(env.device),
|
||||
"torch_version": str(torch.__version__),
|
||||
}
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
if not args.resume:
|
||||
write_json(args.output / "config.json", cfg)
|
||||
write_json(args.output / "identity.json", identity)
|
||||
for path in identity["sources"]:
|
||||
target = args.output / "source_snapshot" / path
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copyfile(root / path, target)
|
||||
elif json.loads((args.output / "identity.json").read_text()) != identity:
|
||||
raise ValueError("Run config/source identity changed; cannot resume")
|
||||
update, elapsed_before, best = 0, 0.0, None
|
||||
if args.resume:
|
||||
checkpoint = restore_checkpoint(args.resume, model, optimizer, identity)
|
||||
update, elapsed_before, best = checkpoint["update"], checkpoint["elapsed_s"], checkpoint["best"]
|
||||
start = time.monotonic()
|
||||
|
||||
def elapsed():
|
||||
return elapsed_before + time.monotonic() - start
|
||||
|
||||
request = {"stop": False}
|
||||
signal.signal(signal.SIGTERM, lambda *_: request.update(stop=True))
|
||||
signal.signal(signal.SIGINT, lambda *_: request.update(stop=True))
|
||||
hardware_limits = torch.tensor(
|
||||
[[j["limits"]["lower"], j["limits"]["upper"]] for j in s.bundle["joints"]], device=env.device
|
||||
)
|
||||
speed_limits = torch.tensor([j["limits"]["velocity"] for j in s.bundle["joints"]], device=env.device)
|
||||
audit = {
|
||||
"samples": 0,
|
||||
"finite": torch.tensor(True, device=env.device),
|
||||
"maxima": torch.zeros(4, device=env.device),
|
||||
"phase": "training",
|
||||
"training_samples": 0,
|
||||
"training_load_sign_samples": torch.zeros(12, dtype=torch.int64, device=env.device),
|
||||
}
|
||||
original_update = env.scene.update
|
||||
|
||||
def observe(dt):
|
||||
original_update(dt)
|
||||
if dt <= 0:
|
||||
return
|
||||
audit["samples"] += 1
|
||||
if audit["phase"] == "training":
|
||||
audit["training_samples"] += 1
|
||||
audit["training_load_sign_samples"] += torch.cat(
|
||||
((s.last_disturbance > 0).sum(0), (s.last_disturbance < 0).sum(0))
|
||||
)
|
||||
d = s.robot.data
|
||||
torque = (d.applied_torque.torch[:, s.joints] / s.effort).abs().amax()
|
||||
velocity = (d.joint_vel.torch[:, s.joints] / speed_limits).abs().amax()
|
||||
excess = torch.maximum(hardware_limits[:, 0] - s.q(), s.q() - hardware_limits[:, 1]).clamp(min=0).amax()
|
||||
audit["maxima"] = torch.maximum(
|
||||
audit["maxima"], torch.stack((torque, velocity, excess, s.last_guide[:, 2].abs().amax()))
|
||||
)
|
||||
audit["finite"] &= (
|
||||
torch.isfinite(d.root_state_w.torch).all()
|
||||
& torch.isfinite(s.mount_wrench()).all()
|
||||
& torch.isfinite(s.q()).all()
|
||||
)
|
||||
|
||||
env.scene.update = observe
|
||||
|
||||
def audit_report():
|
||||
values = audit["maxima"].cpu().tolist()
|
||||
if not audit["finite"].item() or not all(math.isfinite(v) for v in values):
|
||||
raise RuntimeError("Invalid physical/sensor state during training")
|
||||
c = env.cfg.support_parameters
|
||||
if (
|
||||
max(values[:2]) > c["normalized_effort_velocity_limit"]
|
||||
or values[2] > c["joint_limit_excess_rad"]
|
||||
or values[3] != 0
|
||||
):
|
||||
raise RuntimeError(f"Training physical safety gate failed: {values}")
|
||||
return {
|
||||
"physics_samples": audit["samples"],
|
||||
"training_physics_samples_this_process": audit["training_samples"],
|
||||
"training_load_sign_env_samples_positive_then_negative": audit["training_load_sign_samples"].cpu().tolist(),
|
||||
"max_torque_ratio": values[0],
|
||||
"max_velocity_ratio": values[1],
|
||||
"max_joint_limit_excess_rad": values[2],
|
||||
"max_guide_fz_N": values[3],
|
||||
"finite": True,
|
||||
}
|
||||
|
||||
def environment_reset():
|
||||
return features(env.reset()[0]["policy"])
|
||||
|
||||
@torch.no_grad()
|
||||
def validation(hold=False):
|
||||
saved_rng, distribution, episode = rng_state(), env.cfg.load_randomization, env.cfg.episode_length_s
|
||||
audit["phase"] = "validation"
|
||||
env.cfg.load_randomization = None
|
||||
env.cfg.episode_length_s = env.cfg.support_parameters["episode_seconds"]
|
||||
obs, _ = env.reset(seed=cfg["validation_seed"])
|
||||
generator = torch.Generator(device=env.device).manual_seed(cfg["validation_seed"])
|
||||
s.scenarios[:] = torch.arange(env.num_envs, device=env.device) % 13
|
||||
for target, key in ((s.load_scale, "validation_amplitude_scale"), (s.time_shift, "validation_time_shift_s")):
|
||||
low, high = cfg[key]
|
||||
target[:] = low + (high - low) * torch.rand(env.num_envs, device=env.device, generator=generator)
|
||||
test_inputs = {
|
||||
"scenarios": s.scenarios.cpu().tolist(),
|
||||
"scale": s.load_scale.cpu().tolist(),
|
||||
"shift": s.time_shift.cpu().tolist(),
|
||||
}
|
||||
failed = torch.zeros(env.num_envs, device=env.device, dtype=torch.bool)
|
||||
error_sum, contact_sum = (
|
||||
torch.zeros(env.num_envs, device=env.device),
|
||||
torch.zeros(env.num_envs, device=env.device),
|
||||
)
|
||||
peak_error, peak_tilt = torch.zeros_like(error_sum), torch.zeros_like(error_sum)
|
||||
measured_steps = 0
|
||||
steps = round(env.cfg.support_parameters["episode_seconds"] / env.step_dt)
|
||||
x = features(obs["policy"])
|
||||
for step in range(steps):
|
||||
action = torch.zeros(env.num_envs, 3, device=env.device) if hold else model.act(x, deterministic=True)[0]
|
||||
obs, _, terminated, truncated, info = env.step(action)
|
||||
token = {k: v.clone() for k, v in obs["policy"].items()}
|
||||
ids = info["terminal_ids"]
|
||||
if ids is not None:
|
||||
for k in token:
|
||||
token[k][ids.long()] = info["terminal_observation"][k]
|
||||
failed |= terminated
|
||||
# Timeout at the configured final step is valid; an earlier timeout is not.
|
||||
if step < steps - 1:
|
||||
failed |= truncated
|
||||
if (step + 1) * env.step_dt >= env.cfg.support_parameters["warmup_seconds"]:
|
||||
measured_steps += 1
|
||||
error = (token["base_state"][:, 2] - token["goal"][:, 0]).abs()
|
||||
error = selection_height_error(error, failed)
|
||||
error_sum += error
|
||||
peak_error = torch.maximum(peak_error, error)
|
||||
q = token["base_state"][:, 3:7] # xyzw in installed Lab
|
||||
tilt = torch.acos((1 - 2 * (q[:, 0].square() + q[:, 1].square())).clamp(-1, 1))
|
||||
peak_tilt = torch.maximum(peak_tilt, tilt)
|
||||
total_mass = s.robot.data.body_mass.torch[0].sum()
|
||||
contact_sum += ((token["foot_state"][:, 8] > 0.2 * total_mass * 9.81) & ~failed).float()
|
||||
x = features(obs["policy"])
|
||||
result = {
|
||||
"kind": "validation_selection_only",
|
||||
"hold": hold,
|
||||
"seed": cfg["validation_seed"],
|
||||
"update": update,
|
||||
"input_hash": canonical_hash(test_inputs),
|
||||
"inputs": test_inputs,
|
||||
"failure_fraction": failed.float().mean().item(),
|
||||
"mean_height_error_m": (error_sum / measured_steps).mean().item(),
|
||||
"max_selection_height_error_m": peak_error.max().item(),
|
||||
"max_tilt_rad": peak_tilt.max().item(),
|
||||
"min_contact_fraction": (contact_sum / measured_steps).min().item(),
|
||||
"control_samples_after_settle": measured_steps,
|
||||
"physics_safety": audit_report(),
|
||||
}
|
||||
result["score"] = result["mean_height_error_m"] + 0.10 * result["failure_fraction"]
|
||||
env.cfg.load_randomization, env.cfg.episode_length_s = distribution, episode
|
||||
restore_rng(saved_rng)
|
||||
audit["phase"] = "training"
|
||||
return result
|
||||
|
||||
def checkpoint(label):
|
||||
checked_checkpoint(args, cfg, model, optimizer, identity, update, elapsed(), best, label)
|
||||
|
||||
result = {
|
||||
"passed": False,
|
||||
"status": "running",
|
||||
"accepted_skill": False,
|
||||
"formal_training_complete": False,
|
||||
"pid": os.getpid(),
|
||||
"run_kind": "engineering_preflight" if args.probe_episode_s else "formal_training",
|
||||
"planned_updates": cfg["updates"],
|
||||
"planned_transitions": cfg["updates"] * cfg["horizon"] * cfg["num_envs"],
|
||||
"identity": identity,
|
||||
"resume": str(args.resume) if args.resume else None,
|
||||
}
|
||||
write_json(args.output / "progress.json", result)
|
||||
metrics_path = args.output / "metrics.jsonl"
|
||||
|
||||
def log(row):
|
||||
with metrics_path.open("a") as stream:
|
||||
stream.write(json.dumps(row, allow_nan=False) + "\n")
|
||||
|
||||
try:
|
||||
if not args.resume:
|
||||
checkpoint("update_000000")
|
||||
baseline = validation(hold=True)
|
||||
write_json(args.output / "hold_validation.json", baseline)
|
||||
log(baseline)
|
||||
x = environment_reset()
|
||||
while update < cfg["updates"]:
|
||||
if (
|
||||
request["stop"]
|
||||
or (args.output / "STOP").exists()
|
||||
or elapsed() >= cfg["max_wall_seconds"]
|
||||
or (args.stop_after and update >= args.stop_after)
|
||||
):
|
||||
checkpoint(f"interrupted_{update:06d}")
|
||||
result.update(
|
||||
status="paused",
|
||||
reason="stop request/update boundary/resource budget",
|
||||
update=update,
|
||||
elapsed_s=elapsed(),
|
||||
transitions=update * cfg["horizon"] * cfg["num_envs"],
|
||||
)
|
||||
break
|
||||
rollout_start = time.monotonic()
|
||||
data, x, failures, timeouts = collect_rollout(env, model, x, cfg)
|
||||
safety = audit_report()
|
||||
metrics = ppo_step(model, optimizer, data, cfg)
|
||||
update += 1
|
||||
row = {
|
||||
"kind": "train",
|
||||
"update": update,
|
||||
"transitions": update * cfg["horizon"] * cfg["num_envs"],
|
||||
"mean_step_reward": data["reward"].mean().item(),
|
||||
"fall_terminations": failures,
|
||||
"timeouts": timeouts,
|
||||
"update_wall_s": time.monotonic() - rollout_start,
|
||||
"elapsed_s": elapsed(),
|
||||
"physics_safety": safety,
|
||||
**metrics,
|
||||
}
|
||||
log(row)
|
||||
result.update(
|
||||
status="running",
|
||||
update=update,
|
||||
elapsed_s=elapsed(),
|
||||
transitions=row["transitions"],
|
||||
latest=row,
|
||||
best=best,
|
||||
)
|
||||
write_json(args.output / "progress.json", result)
|
||||
print(
|
||||
f"PPO {update}/{cfg['updates']} transitions={row['transitions']} "
|
||||
f"reward={row['mean_step_reward']:.5f} failures={failures} wall={elapsed():.1f}s",
|
||||
flush=True,
|
||||
)
|
||||
if update % cfg["validation_interval"] == 0:
|
||||
validation_row = validation()
|
||||
log(validation_row)
|
||||
write_json(args.output / "validation" / f"update_{update:06d}.json", validation_row)
|
||||
if best is None or validation_row["score"] < best["score"]:
|
||||
best = {
|
||||
"update": update,
|
||||
"score": validation_row["score"],
|
||||
"selection": "validation only, minimum penalized mean height error",
|
||||
}
|
||||
checkpoint("best_validation")
|
||||
write_json(args.output / "selection.json", best)
|
||||
x = environment_reset()
|
||||
if update == 1 or update % cfg["checkpoint_interval"] == 0:
|
||||
checkpoint(f"update_{update:06d}")
|
||||
else:
|
||||
checkpoint(f"update_{update:06d}")
|
||||
result.update(
|
||||
status="completed",
|
||||
passed=True,
|
||||
formal_training_complete=not bool(args.probe_episode_s),
|
||||
update=update,
|
||||
transitions=update * cfg["horizon"] * cfg["num_envs"],
|
||||
elapsed_s=elapsed(),
|
||||
best=best,
|
||||
note="Training complete only; independent holdout/physics acceptance and frozen package still required",
|
||||
)
|
||||
except BaseException:
|
||||
result.update(status="failed", passed=False, update=update, error=traceback.format_exc(), elapsed_s=elapsed())
|
||||
raise
|
||||
finally:
|
||||
env.scene.update = original_update
|
||||
write_json(args.output / "progress.json", result)
|
||||
write_json(args.output / "result.json", result)
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--config", type=Path, default=Path("configs/training/go2_fl_loaded_stand_ppo_v1.json"))
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--resume", type=Path)
|
||||
parser.add_argument("--console-log", type=Path)
|
||||
parser.add_argument("--stop-after", type=int)
|
||||
parser.add_argument(
|
||||
"--probe-episode-s", type=float, default=0.0, help="Engineering preflight only; changes identity"
|
||||
)
|
||||
AppLauncher.add_app_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
if args.stop_after is not None and args.stop_after < 1 or args.probe_episode_s < 0:
|
||||
parser.error("Invalid preflight/stop count")
|
||||
args.output.mkdir(parents=True, exist_ok=bool(args.resume))
|
||||
write_json(args.output / "result.json", {"passed": False, "status": "running", "accepted_skill": False})
|
||||
app = AppLauncher(args).app
|
||||
env, result, code = None, {"passed": False, "status": "failed", "accepted_skill": False}, 1
|
||||
try:
|
||||
import gymnasium as gym
|
||||
import SkillFactory.tasks # noqa: F401
|
||||
from SkillFactory.legs.standing_ppo import load_training_config
|
||||
|
||||
from isaaclab_tasks.utils import parse_env_cfg
|
||||
|
||||
cfg = load_training_config(args.config)
|
||||
env_cfg = parse_env_cfg("SkillFactory-LoadedLegStand-v0", device=args.device, num_envs=cfg["num_envs"])
|
||||
if env_cfg.support_config != cfg["task_config"]:
|
||||
raise ValueError("Task configuration binding mismatch")
|
||||
env_cfg.seed = cfg["seed"]
|
||||
env_cfg.load_randomization = {
|
||||
"amplitude_scale": cfg["train_amplitude_scale"],
|
||||
"time_shift_s": cfg["train_time_shift_s"],
|
||||
}
|
||||
if args.probe_episode_s:
|
||||
env_cfg.episode_length_s = args.probe_episode_s
|
||||
env = gym.make("SkillFactory-LoadedLegStand-v0", cfg=env_cfg).unwrapped
|
||||
result = train(env, cfg, args)
|
||||
code = 0 if result["status"] in ("completed", "paused") else 1
|
||||
except BaseException:
|
||||
result.update(passed=False, status="failed", error=traceback.format_exc())
|
||||
print(result["error"], file=sys.stderr)
|
||||
finally:
|
||||
if env is not None:
|
||||
try:
|
||||
env.close()
|
||||
except BaseException:
|
||||
result.update(passed=False, status="failed", shutdown_error=traceback.format_exc())
|
||||
code = 1
|
||||
write_json(args.output / "result.json", result)
|
||||
# Explicitly preserve paused != completed; supervisor only attests process/log cleanup.
|
||||
app.close(exit_code=code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,155 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Train IQL from immutable shards, with atomic optimizer/target/RNG checkpoints."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import signal
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import write_json
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--config", type=Path, required=True)
|
||||
parser.add_argument("--data", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--resume", type=Path)
|
||||
parser.add_argument("--device", default="cuda:0")
|
||||
parser.add_argument("--stop-after", type=int, help="Operational interruption drill; not a completed run")
|
||||
args = parser.parse_args()
|
||||
os.environ.setdefault("CUBLAS_WORKSPACE_CONFIG", ":4096:8")
|
||||
import torch
|
||||
from SkillFactory.legs.offline import IQL, atomic_save, canonical_hash, sha256
|
||||
|
||||
cfg = json.loads(args.config.read_text())
|
||||
train = cfg["training"]
|
||||
index = json.loads((args.data / "dataset.json").read_text())
|
||||
digest = canonical_hash(cfg)
|
||||
expected = cfg["collection"]["shards"] * cfg["num_envs"] * cfg["episode_steps"]
|
||||
if not index["complete"] or index["config_sha256"] != digest or index["transitions"] != expected:
|
||||
raise ValueError("Require complete frozen dataset matching the pre-registered config")
|
||||
args.output.mkdir(parents=True, exist_ok=True)
|
||||
latest = args.output / "latest.pt"
|
||||
if latest.exists() and args.resume is None:
|
||||
raise ValueError("Existing run requires explicit --resume; will not silently overwrite")
|
||||
write_json(args.output / "result.json", {"passed": False, "status": "running", "training_complete": False})
|
||||
torch.set_num_threads(4)
|
||||
torch.use_deterministic_algorithms(True)
|
||||
torch.set_float32_matmul_precision("highest")
|
||||
torch.manual_seed(train["seed"])
|
||||
data_parts = []
|
||||
for record in index["shards"]:
|
||||
path = args.data / record["path"]
|
||||
if sha256(path) != record["sha256"]:
|
||||
raise ValueError(f"Corrupted dataset: {path}")
|
||||
data_parts.append(torch.load(path, weights_only=True, map_location="cpu"))
|
||||
data = {key: torch.cat([part[key] for part in data_parts]).to(args.device) for key in data_parts[0]}
|
||||
del data_parts
|
||||
if len(data["state"]) != expected or any(not torch.isfinite(v).all() for v in data.values()):
|
||||
raise ValueError("Invalid fixed dataset")
|
||||
mean, std = data["state"].mean(0), data["state"].std(0).clamp(min=0.02)
|
||||
learner = IQL(train, mean, std, args.device)
|
||||
identity = {
|
||||
"config_sha256": digest,
|
||||
"dataset_sha256": sha256(args.data / "dataset.json"),
|
||||
"source_sha256": {
|
||||
name: sha256(Path(__file__).resolve().parents[1] / name)
|
||||
for name in (
|
||||
"scripts/train_offline_leg.py",
|
||||
"source/SkillFactory/SkillFactory/legs/offline.py",
|
||||
"source/SkillFactory/SkillFactory/legs/frozen_policy.py",
|
||||
)
|
||||
},
|
||||
}
|
||||
start_update, elapsed = 0, 0.0
|
||||
if args.resume:
|
||||
saved = torch.load(args.resume, map_location=args.device, weights_only=True)
|
||||
if saved["identity"] != identity:
|
||||
raise ValueError("Resume rejected: config/dataset/training source changed")
|
||||
learner.load_state_dict(saved["learner"])
|
||||
start_update, elapsed = saved["update"], saved["elapsed_s"]
|
||||
started = time.perf_counter()
|
||||
stop = False
|
||||
|
||||
def request_stop(signum, frame):
|
||||
nonlocal stop
|
||||
stop = True
|
||||
|
||||
signal.signal(signal.SIGTERM, request_stop)
|
||||
signal.signal(signal.SIGINT, request_stop)
|
||||
events = args.output / "events.jsonl"
|
||||
with events.open("a") as stream:
|
||||
stream.write(
|
||||
json.dumps(
|
||||
{
|
||||
"event": "resume" if args.resume else "start",
|
||||
"update": start_update,
|
||||
"pid": os.getpid(),
|
||||
"time": time.time(),
|
||||
"identity": identity,
|
||||
}
|
||||
)
|
||||
+ "\n"
|
||||
)
|
||||
update = start_update
|
||||
for update in range(start_update + 1, train["updates"] + 1):
|
||||
ids = torch.randint(expected, (train["batch_size"],), device=args.device)
|
||||
losses = learner.update({key: value[ids] for key, value in data.items()})
|
||||
if update % 100 == 0:
|
||||
with events.open("a") as stream:
|
||||
stream.write(
|
||||
json.dumps(
|
||||
{
|
||||
"event": "update",
|
||||
"update": update,
|
||||
"losses": losses,
|
||||
"elapsed_s": elapsed + time.perf_counter() - started,
|
||||
}
|
||||
)
|
||||
+ "\n"
|
||||
)
|
||||
interrupt = stop or (args.stop_after is not None and update >= args.stop_after)
|
||||
if update % train["checkpoint_every"] == 0 or interrupt or update == train["updates"]:
|
||||
checkpoint = {
|
||||
"learner": learner.state_dict(),
|
||||
"identity": identity,
|
||||
"update": update,
|
||||
"elapsed_s": elapsed + time.perf_counter() - started,
|
||||
}
|
||||
atomic_save(checkpoint, latest)
|
||||
if update % train["checkpoint_every"] == 0:
|
||||
atomic_save(checkpoint, args.output / f"step_{update:06d}.pt")
|
||||
print(
|
||||
f"IQL update={update}/{train['updates']} elapsed={checkpoint['elapsed_s']:.1f}s losses={losses}",
|
||||
flush=True,
|
||||
)
|
||||
if interrupt:
|
||||
break
|
||||
if elapsed + time.perf_counter() - started > cfg["budget"]["wall_seconds"]:
|
||||
# The next update takes the ordinary interrupt/checkpoint path.
|
||||
stop = True
|
||||
complete = update == train["updates"]
|
||||
write_json(
|
||||
args.output / "result.json",
|
||||
{
|
||||
"passed": complete,
|
||||
"training_complete": complete,
|
||||
"status": "completed" if complete else "interrupted",
|
||||
"updates": update,
|
||||
"elapsed_s": elapsed + time.perf_counter() - started,
|
||||
"identity": identity,
|
||||
"checkpoint_sha256": sha256(latest),
|
||||
"transitions": expected,
|
||||
"device": args.device,
|
||||
"acceptance_complete": False,
|
||||
"algorithm": "IQL; twin-Q TD, expectile V, advantage-weighted deterministic regression",
|
||||
},
|
||||
)
|
||||
print("Training finished; independent acceptance still required", flush=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,168 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Copy-only isolated CPU inference validation: no repository, Kit or training data."""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import traceback
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def child(root):
|
||||
import torch
|
||||
|
||||
sys.path.insert(0, str(root))
|
||||
from frozen_policy import FrozenPolicy
|
||||
|
||||
torch.set_num_threads(1)
|
||||
binding = json.loads((root / "binding.json").read_text())
|
||||
kwargs = {"part_id": "go2_fl", "manifest_sha256": binding["manifest_sha256"]}
|
||||
policy = FrozenPolicy(root, **kwargs)
|
||||
example = torch.load(root / "example_tokens.pt", weights_only=True, map_location="cpu")
|
||||
before = {k: v.clone() for k, v in policy._actor.state_dict().items()}
|
||||
checks = []
|
||||
for batch in (1, 16, 64, 256):
|
||||
tokens = {k: v.expand(batch, *v.shape[1:]).clone() for k, v in example.items()}
|
||||
original = {k: v.clone() for k, v in tokens.items()}
|
||||
first = policy.act(tokens)
|
||||
for _ in range(100):
|
||||
out = policy.act(tokens)
|
||||
if not torch.equal(first, out) or out.requires_grad or out.shape != (batch, 8) or out.abs().max() > 1:
|
||||
raise AssertionError("Non-deterministic/stateful/invalid output")
|
||||
if not all(torch.equal(v, tokens[k]) for k, v in original.items()):
|
||||
raise AssertionError("Inference mutated caller inputs")
|
||||
if out[:, 3:].abs().max() != 0 or policy.position_targets(tokens).shape != (batch, 3):
|
||||
raise AssertionError("Bad control mapping")
|
||||
checks.append(f"B={batch}:100 deterministic calls; no input mutation")
|
||||
if not all(torch.equal(v, policy._actor.state_dict()[k]) for k, v in before.items()):
|
||||
raise AssertionError("Parameters/statistics changed")
|
||||
if policy._actor.training or any(p.requires_grad for p in policy._actor.parameters()):
|
||||
raise AssertionError("Not frozen")
|
||||
|
||||
def reject(operation):
|
||||
try:
|
||||
operation()
|
||||
except (ValueError, RuntimeError):
|
||||
return
|
||||
raise AssertionError("Invalid input/bundle accepted")
|
||||
|
||||
reject(lambda: FrozenPolicy(root, part_id="go1_fl", manifest_sha256=kwargs["manifest_sha256"]))
|
||||
reject(lambda: FrozenPolicy(root, part_id="go2_fl", manifest_sha256="0" * 64))
|
||||
for key in ("goal", "context", "joint_observation", "effector_observation", "joint_description"):
|
||||
bad = {k: v.clone() for k, v in example.items()}
|
||||
bad[key].flatten()[0] = float("nan")
|
||||
reject(lambda: policy.act(bad))
|
||||
bad = {k: v.clone() for k, v in example.items()}
|
||||
bad["goal"] = bad["goal"].double()
|
||||
reject(lambda: policy.act(bad))
|
||||
bad = {k: v.clone() for k, v in example.items()}
|
||||
bad["joint_mask"][:, 3] = True
|
||||
reject(lambda: policy.act(bad))
|
||||
bad = {k: v.clone() for k, v in example.items()}
|
||||
bad["context"][:, 3] = 0.02
|
||||
reject(lambda: policy.act(bad))
|
||||
padded = {k: v.clone() for k, v in example.items()}
|
||||
padded["joint_observation"][:, 3:] = float("nan")
|
||||
padded["joint_description"][:, 3:] = float("nan")
|
||||
if not torch.equal(policy.act(padded), policy.act(example)):
|
||||
raise AssertionError("Padding leaked into output")
|
||||
with tempfile.TemporaryDirectory() as corrupted:
|
||||
clone = Path(corrupted) / "package"
|
||||
shutil.copytree(root, clone)
|
||||
# A published package may be read-only; corrupt only the disposable copy.
|
||||
(clone / "actor.pt").chmod(0o600)
|
||||
with (clone / "actor.pt").open("ab") as stream:
|
||||
stream.write(b"corruption")
|
||||
reject(lambda: FrozenPolicy(clone, **kwargs))
|
||||
if any(name.startswith(("isaac", "omni", "SkillFactory")) for name in sys.modules):
|
||||
raise AssertionError("Unexpected simulator/repository import")
|
||||
return {
|
||||
"passed": True,
|
||||
"actor_sha256": hashlib.sha256((root / "actor.pt").read_bytes()).hexdigest(),
|
||||
"manifest_sha256": hashlib.sha256((root / "manifest.json").read_bytes()).hexdigest(),
|
||||
"python": sys.version,
|
||||
"torch": torch.__version__,
|
||||
"device": "cpu",
|
||||
"isolated": bool(sys.flags.isolated),
|
||||
"parameters_and_buffers_unchanged": True,
|
||||
"requires_grad": False,
|
||||
"training_or_simulator_imports": False,
|
||||
"checks": checks
|
||||
+ ["invalid active values/dtype/masks/context/hardware rejected", "padding ignored", "corruption rejected"],
|
||||
"package_only": True,
|
||||
}
|
||||
|
||||
|
||||
def verify_package(package, python, output=None):
|
||||
# Imported only in the parent: the copied isolated child needs no project helper.
|
||||
from runtime_common import write_json
|
||||
|
||||
attempt = {"run_id": uuid.uuid4().hex, "package": str(Path(package).resolve())}
|
||||
if output:
|
||||
write_json(output, dict(attempt, passed=False, status="running"))
|
||||
try:
|
||||
with tempfile.TemporaryDirectory(prefix="skillbundle-independent-") as tmp:
|
||||
root = Path(tmp)
|
||||
shutil.copytree(package, root / "package", ignore=shutil.ignore_patterns("__pycache__"))
|
||||
shutil.copy2(__file__, root / "check.py")
|
||||
env = {
|
||||
k: v
|
||||
for k, v in os.environ.items()
|
||||
if not k.startswith(("PYTHON", "ISAAC", "CARB", "EXP_PATH", "CONDA", "VIRTUAL_ENV"))
|
||||
}
|
||||
result = subprocess.run(
|
||||
[
|
||||
str(Path(python).absolute()),
|
||||
"-I",
|
||||
str(root / "check.py"),
|
||||
"--child",
|
||||
"--package",
|
||||
str(root / "package"),
|
||||
],
|
||||
cwd=root,
|
||||
env=env,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
report = json.loads(result.stdout.strip().splitlines()[-1])
|
||||
if report.get("passed") is not True:
|
||||
raise ValueError("Independent child did not pass")
|
||||
for name, key in (("actor.pt", "actor_sha256"), ("manifest.json", "manifest_sha256")):
|
||||
if report.get(key) != hashlib.sha256((Path(package) / name).read_bytes()).hexdigest():
|
||||
raise ValueError("Package changed during independent verification")
|
||||
report.update(attempt, stderr=result.stderr, status="completed")
|
||||
# Publish success only after copy verification AND temporary-directory cleanup.
|
||||
if output:
|
||||
write_json(output, report)
|
||||
return report
|
||||
except BaseException as error:
|
||||
failure = dict(attempt, passed=False, status="failed", error=traceback.format_exc())
|
||||
if isinstance(error, subprocess.CalledProcessError):
|
||||
failure.update(returncode=error.returncode, stdout=error.stdout, stderr=error.stderr)
|
||||
if output:
|
||||
write_json(output, failure)
|
||||
raise
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--package", type=Path, required=True)
|
||||
parser.add_argument("--python", type=Path, default=Path(sys.executable))
|
||||
parser.add_argument("--output", type=Path)
|
||||
parser.add_argument("--child", action="store_true")
|
||||
args = parser.parse_args()
|
||||
if args.child:
|
||||
print(json.dumps(child(args.package.resolve()), allow_nan=False))
|
||||
return
|
||||
print(json.dumps(verify_package(args.package, args.python, args.output), indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,175 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Independent copy-only CPU standing loader check, no project or simulator in child."""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def digest(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def child(root):
|
||||
import torch
|
||||
|
||||
sys.path.insert(0, str(root))
|
||||
from frozen_standing import FrozenStanding
|
||||
|
||||
torch.set_num_threads(1)
|
||||
binding = json.loads((root / "binding.json").read_text())
|
||||
kwargs = {
|
||||
"manifest_sha256": digest(root / "manifest.json"),
|
||||
"task_sha256": binding["task_sha256"],
|
||||
"support_sha256": binding["support_sha256"],
|
||||
}
|
||||
policy = FrozenStanding(root, **kwargs)
|
||||
example = torch.load(root / "example.pt", map_location="cpu", weights_only=True)
|
||||
initial = {k: v.clone() for k, v in policy._actor.state_dict().items()}
|
||||
for batch in (1, 16, 64, 256):
|
||||
obs = {k: v.expand(batch, *v.shape[1:]).clone() for k, v in example.items()}
|
||||
before = {k: v.clone() for k, v in obs.items()}
|
||||
expected = policy.act(obs)
|
||||
for _ in range(100):
|
||||
action = policy.act(obs)
|
||||
if (
|
||||
not torch.equal(expected, action)
|
||||
or action.shape != (batch, 3)
|
||||
or action.requires_grad
|
||||
or action.abs().max() > 1
|
||||
):
|
||||
raise ValueError("Non-deterministic/invalid output")
|
||||
targets = policy.position_targets(obs)
|
||||
soft = torch.tensor(binding["soft_limits"])
|
||||
if targets.shape != (batch, 3) or not ((targets >= soft[:, 0]) & (targets <= soft[:, 1])).all():
|
||||
raise ValueError("Invalid target mapping")
|
||||
if not all(torch.equal(v, obs[k]) for k, v in before.items()):
|
||||
raise ValueError("Mutated input")
|
||||
if not all(torch.equal(v, policy._actor.state_dict()[k]) for k, v in initial.items()):
|
||||
raise ValueError("Mutated parameters/buffers")
|
||||
if policy._actor.training or any(p.requires_grad for p in policy._actor.parameters()):
|
||||
raise ValueError("Actor not frozen")
|
||||
|
||||
def reject(fn):
|
||||
try:
|
||||
fn()
|
||||
except (ValueError, RuntimeError):
|
||||
return
|
||||
raise AssertionError("Invalid input/package accepted")
|
||||
|
||||
for key in kwargs:
|
||||
reject(lambda key=key: FrozenStanding(root, **{**kwargs, key: "0" * 64}))
|
||||
for key in example:
|
||||
for value in (float("nan"), float("inf")):
|
||||
bad = {k: v.clone() for k, v in example.items()}
|
||||
bad[key].flatten()[0] = value
|
||||
reject(lambda: policy.act(bad))
|
||||
for replacement in (torch.zeros(1, 2), torch.zeros(1, 1, dtype=torch.float64), torch.zeros(1, 1, device="meta")):
|
||||
reject(lambda: policy.act({**example, "goal": replacement}))
|
||||
reject(lambda: policy.act({k: v[:0] for k, v in example.items()}))
|
||||
reject(lambda: policy.act({k: v for k, v in example.items() if k != "goal"}))
|
||||
reject(lambda: policy.act({**example, "goal": example["goal"] + 0.01}))
|
||||
bad = {k: v.clone() for k, v in example.items()}
|
||||
bad["guide_wrench"][:, 2] = 1
|
||||
reject(lambda: policy.act(bad))
|
||||
bad = {k: v.clone() for k, v in example.items()}
|
||||
bad["base_state"][:, 3:7] = 0
|
||||
reject(lambda: policy.act(bad))
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
corrupt = Path(directory) / "package"
|
||||
shutil.copytree(root, corrupt)
|
||||
(corrupt / "actor.pt").chmod(0o600)
|
||||
with (corrupt / "actor.pt").open("ab") as stream:
|
||||
stream.write(b"corruption")
|
||||
reject(lambda: FrozenStanding(corrupt, **kwargs))
|
||||
forbidden = [n for n in sys.modules if n.startswith(("SkillFactory", "isaac", "omni", "standing_ppo"))]
|
||||
if forbidden:
|
||||
raise ValueError(f"Training/simulator/project imports: {forbidden}")
|
||||
return {
|
||||
"passed": True,
|
||||
"package_only": True,
|
||||
"isolated": bool(sys.flags.isolated),
|
||||
"device": "cpu",
|
||||
"python": sys.version,
|
||||
"torch": torch.__version__,
|
||||
"manifest_sha256": kwargs["manifest_sha256"],
|
||||
"actor_sha256": digest(root / "actor.pt"),
|
||||
"forbidden_imports": forbidden,
|
||||
"deterministic_batches": [1, 16, 64, 256],
|
||||
"calls_per_batch": 100,
|
||||
"input_state_unchanged": True,
|
||||
"requires_grad": False,
|
||||
"invalid_inputs_binding_corruption_rejected": True,
|
||||
}
|
||||
|
||||
|
||||
def verify(package, python, output):
|
||||
from runtime_common import write_json
|
||||
|
||||
write_json(output, {"passed": False, "status": "running"})
|
||||
try:
|
||||
with tempfile.TemporaryDirectory(prefix="standing-independent-") as directory:
|
||||
root = Path(directory)
|
||||
shutil.copytree(package, root / "package", ignore=shutil.ignore_patterns("__pycache__"))
|
||||
shutil.copyfile(__file__, root / "check.py")
|
||||
env = {
|
||||
k: v
|
||||
for k, v in os.environ.items()
|
||||
if not k.startswith(("PYTHON", "ISAAC", "CARB", "CONDA", "VIRTUAL_ENV", "EXP_PATH"))
|
||||
}
|
||||
proc = subprocess.run(
|
||||
[
|
||||
str(Path(python).absolute()),
|
||||
"-I",
|
||||
str(root / "check.py"),
|
||||
"--child",
|
||||
"--package",
|
||||
str(root / "package"),
|
||||
],
|
||||
cwd=root,
|
||||
env=env,
|
||||
text=True,
|
||||
capture_output=True,
|
||||
check=True,
|
||||
)
|
||||
report = json.loads(proc.stdout.strip().splitlines()[-1])
|
||||
if not report.get("passed") or report["manifest_sha256"] != digest(package / "manifest.json"):
|
||||
raise ValueError("Independent check failed/package changed")
|
||||
manifest = json.loads((package / "manifest.json").read_text())
|
||||
if any(digest(package / p) != h for p, h in manifest["files"].items()):
|
||||
raise ValueError("Package changed during independent verification")
|
||||
report.update(status="completed", package=str(package.resolve()), stderr=proc.stderr)
|
||||
write_json(output, report)
|
||||
return report
|
||||
except BaseException as error:
|
||||
report = {"passed": False, "status": "failed", "error": traceback.format_exc()}
|
||||
if isinstance(error, subprocess.CalledProcessError):
|
||||
report.update(stdout=error.stdout, stderr=error.stderr, returncode=error.returncode)
|
||||
write_json(output, report)
|
||||
raise
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--package", type=Path, required=True)
|
||||
parser.add_argument("--python", type=Path, default=Path(sys.executable))
|
||||
parser.add_argument("--output", type=Path)
|
||||
parser.add_argument("--child", action="store_true")
|
||||
args = parser.parse_args()
|
||||
if args.child:
|
||||
print(json.dumps(child(args.package.resolve()), allow_nan=False))
|
||||
else:
|
||||
if not args.output:
|
||||
parser.error("--output required")
|
||||
print(json.dumps(verify(args.package, args.python, args.output), indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,387 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Real-physics loaded-standing prototype validation, never a learned-policy acceptance."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import sys
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import finish_run, write_json
|
||||
|
||||
|
||||
def evaluate(env, args):
|
||||
import torch
|
||||
from SkillFactory.legs.support import CONTRACT, negative_control_collapsed
|
||||
from SkillFactory.legs.tokens import LegKinematics
|
||||
from SkillFactory.tasks.manager_based.loaded_leg.mdp import state
|
||||
|
||||
from isaaclab.utils.math import quat_apply, quat_apply_inverse
|
||||
|
||||
s, cfg = state(env), env.cfg.support_parameters
|
||||
robot, count = s.robot, env.num_envs
|
||||
if robot.is_fixed_base:
|
||||
raise RuntimeError("Standing cannot use a world-fixed root")
|
||||
mass = robot.data.body_mass.torch[0]
|
||||
total_mass = mass.sum().item()
|
||||
nominal = s.nominal.expand(count, -1)
|
||||
fk = LegKinematics(s.bundle, env.device)
|
||||
columns = []
|
||||
for j in range(3):
|
||||
delta = torch.zeros_like(nominal)
|
||||
delta[:, j] = 0.0001
|
||||
columns.append((fk.foot_position(nominal + delta) - fk.foot_position(nominal - delta)) / 0.0002)
|
||||
jacobian = torch.stack(columns, -1)
|
||||
inverse = torch.linalg.pinv(jacobian)
|
||||
command_q = nominal.clone()
|
||||
target_pos = env.scene.env_origins.clone()
|
||||
target_pos[:, 2] += env.cfg.anchor_height
|
||||
limits = torch.tensor([[j["limits"]["lower"], j["limits"]["upper"]] for j in s.bundle["joints"]], device=env.device)
|
||||
speed_limit = torch.tensor([j["limits"]["velocity"] for j in s.bundle["joints"]], device=env.device)
|
||||
stats = {
|
||||
k: torch.zeros(count, device=env.device)
|
||||
for k in (
|
||||
"max_height_error_m",
|
||||
"max_base_drop_m",
|
||||
"max_tilt_rad",
|
||||
"max_foot_drift_m",
|
||||
"contact_samples",
|
||||
"sum_fz_N",
|
||||
"sum_required_fz_N",
|
||||
"max_normalized_torque",
|
||||
"max_normalized_velocity",
|
||||
"max_joint_limit_excess_rad",
|
||||
"max_guide_fz_N",
|
||||
)
|
||||
}
|
||||
sample_count, all_count = 0, 0
|
||||
calibration_windows = ((2.0, 3.0), (4.5, 5.5), (9.5, 10.5))
|
||||
calibration_counts = [0, 0, 0]
|
||||
sensor_sum = torch.zeros(3, count, 6, device=env.device)
|
||||
expected_sum = torch.zeros_like(sensor_sum)
|
||||
foot_origin = None
|
||||
peak_load = torch.zeros(count, 6, device=env.device)
|
||||
min_load, max_load = torch.zeros_like(peak_load), torch.zeros_like(peak_load)
|
||||
original_update = env.scene.update
|
||||
steps = round(args.duration / env.step_dt)
|
||||
physics_trace = {
|
||||
name: torch.empty(steps * env.cfg.decimation, count, width, device=env.device)
|
||||
for name, width in (
|
||||
("guide_wrench_world", 6),
|
||||
("disturbance_world", 6),
|
||||
("mount_wrench_world", 6),
|
||||
("base_state_world", 13),
|
||||
("foot_normal_force_world", 3),
|
||||
("joint_pos", 3),
|
||||
("joint_vel", 3),
|
||||
("applied_torque", 3),
|
||||
)
|
||||
}
|
||||
|
||||
def observe(dt):
|
||||
nonlocal sample_count, all_count, foot_origin
|
||||
original_update(dt)
|
||||
all_count += 1
|
||||
t = all_count * env.physics_dt
|
||||
pos, quat, vel, omega = s.root()
|
||||
wrench, force, foot = s.mount_world(), s.contact(), s.foot_center()
|
||||
values = [robot.data.root_state_w.torch, s.q(), robot.data.joint_vel.torch, wrench, force]
|
||||
for name, value in (
|
||||
("guide_wrench_world", s.last_guide),
|
||||
("disturbance_world", s.last_disturbance),
|
||||
("mount_wrench_world", wrench),
|
||||
("base_state_world", torch.cat((pos, quat, vel, omega), -1)),
|
||||
("foot_normal_force_world", force),
|
||||
("joint_pos", s.q()),
|
||||
("joint_vel", robot.data.joint_vel.torch[:, s.joints]),
|
||||
("applied_torque", robot.data.applied_torque.torch[:, s.joints]),
|
||||
):
|
||||
physics_trace[name][all_count - 1].copy_(value)
|
||||
if any(not torch.isfinite(v).all() for v in values):
|
||||
raise RuntimeError("Non-finite physical/sensor state")
|
||||
torque = (robot.data.applied_torque.torch[:, s.joints] / s.effort).abs().amax(-1)
|
||||
velocity = (robot.data.joint_vel.torch[:, s.joints] / speed_limit).abs().amax(-1)
|
||||
excess = torch.maximum(limits[:, 0] - s.q(), s.q() - limits[:, 1]).clamp(min=0).amax(-1)
|
||||
for key, value in (
|
||||
("max_normalized_torque", torque),
|
||||
("max_normalized_velocity", velocity),
|
||||
("max_joint_limit_excess_rad", excess),
|
||||
("max_guide_fz_N", s.last_guide[:, 2].abs()),
|
||||
("max_base_drop_m", (env.cfg.anchor_height - pos[:, 2]).clamp(min=0)),
|
||||
):
|
||||
stats[key] = torch.maximum(stats[key], value)
|
||||
peak_load[:] = torch.maximum(peak_load, s.last_disturbance.abs())
|
||||
min_load[:] = torch.minimum(min_load, s.last_disturbance)
|
||||
max_load[:] = torch.maximum(max_load, s.last_disturbance)
|
||||
if t >= cfg["warmup_seconds"]:
|
||||
sample_count += 1
|
||||
if foot_origin is None:
|
||||
foot_origin = foot.clone()
|
||||
for key, value in (
|
||||
("max_height_error_m", (pos[:, 2] - env.cfg.anchor_height).abs()),
|
||||
("max_tilt_rad", s.tilt()),
|
||||
("max_foot_drift_m", (foot[:, :2] - foot_origin[:, :2]).norm(dim=-1)),
|
||||
):
|
||||
stats[key] = torch.maximum(stats[key], value)
|
||||
stats["contact_samples"] += (force[:, 2] > total_mass * 9.81 * 0.2).float()
|
||||
stats["sum_fz_N"] += force[:, 2]
|
||||
stats["sum_required_fz_N"] += total_mass * 9.81 - s.last_disturbance[:, 2]
|
||||
window = next((i for i, (a, b) in enumerate(calibration_windows) if a <= t < b), None)
|
||||
if window is not None:
|
||||
gravity = torch.zeros(count, 3, device=env.device)
|
||||
gravity[:, 2] = -mass[s.payload] * 9.81
|
||||
lever = quat_apply(quat, torch.tensor(cfg["payload_com_m"], device=env.device).expand(count, -1))
|
||||
expected = torch.cat((gravity, torch.cross(lever, gravity, dim=-1)), -1) + s.last_guide + s.last_disturbance
|
||||
sensor_sum[window].add_(wrench)
|
||||
expected_sum[window].add_(expected)
|
||||
calibration_counts[window] += 1
|
||||
if window == 0 and calibration_counts[window] == 400:
|
||||
import warp as wp
|
||||
|
||||
write_json(
|
||||
args.output / "sensor_diagnostic.json",
|
||||
{
|
||||
"sensor_body_names": s.sensor.body_names,
|
||||
"robot_body_names": robot.body_names,
|
||||
"sensor_index": s.sensor_index,
|
||||
"body_pose_w": robot.data.body_link_pose_w.torch[0].tolist(),
|
||||
"force_joint": s.sensor.data.force.torch[0].tolist(),
|
||||
"torque_joint": s.sensor.data.torque.torch[0].tolist(),
|
||||
"joint_pos_b": wp.to_torch(s.sensor._joint_pos_b).tolist(),
|
||||
"joint_quat_b_xyzw": wp.to_torch(s.sensor._joint_quat_b).tolist(),
|
||||
"root_twist": torch.cat((vel, omega), -1).tolist(),
|
||||
"expected_mount_wrench_world": expected.tolist(),
|
||||
},
|
||||
)
|
||||
|
||||
env.scene.update = observe
|
||||
trace = {
|
||||
name: [] for name in ("base_state", "mount_wrench", "guide_wrench", "foot_state", "disturbance_world", "action")
|
||||
}
|
||||
try:
|
||||
for i in range(steps):
|
||||
if args.controller == "height":
|
||||
pos, quat, vel, _ = s.root()
|
||||
error = quat_apply_inverse(quat, target_pos - pos - 0.12 * vel)
|
||||
dq = -(inverse @ error[:, :, None]).squeeze(-1)
|
||||
command_q += 2.0 * env.step_dt * dq
|
||||
command_q.clamp_(min=nominal - cfg["action_scale_rad"], max=nominal + cfg["action_scale_rad"])
|
||||
action = (command_q - nominal) / cfg["action_scale_rad"]
|
||||
else:
|
||||
action = torch.zeros(count, 3, device=env.device)
|
||||
obs, _, terminated, truncated, _ = env.step(action)
|
||||
if terminated.any() or truncated.any():
|
||||
raise RuntimeError("Verification must not auto-reset")
|
||||
for key in ("base_state", "mount_wrench", "guide_wrench", "foot_state"):
|
||||
trace[key].append(obs["policy"][key].clone().cpu())
|
||||
trace["disturbance_world"].append(s.last_disturbance.clone().cpu())
|
||||
trace["action"].append(action.clone().cpu())
|
||||
finally:
|
||||
env.scene.update = original_update
|
||||
torch.save({k: torch.stack(v) if v else [] for k, v in trace.items()}, args.output / "trace.pt")
|
||||
torch.save({k: v[:all_count].cpu() for k, v in physics_trace.items()}, args.output / "physics_trace.pt")
|
||||
n = max(sample_count, 1)
|
||||
rows = []
|
||||
for i in range(count):
|
||||
row = {key: value[i].item() for key, value in stats.items() if key.startswith("max_")}
|
||||
row.update(
|
||||
scenario=int(s.scenarios[i]),
|
||||
contact_fraction=stats["contact_samples"][i].item() / n,
|
||||
mean_foot_fz_N=stats["sum_fz_N"][i].item() / n,
|
||||
mean_required_fz_N=stats["sum_required_fz_N"][i].item() / n,
|
||||
applied_peak_wrench=peak_load[i].tolist(),
|
||||
applied_min_wrench=min_load[i].tolist(),
|
||||
applied_max_wrench=max_load[i].tolist(),
|
||||
)
|
||||
row["vertical_balance_relative_error"] = (
|
||||
abs(row["mean_foot_fz_N"] - row["mean_required_fz_N"]) / row["mean_required_fz_N"]
|
||||
)
|
||||
rows.append(row)
|
||||
denominators = torch.tensor(calibration_counts, device=env.device).clamp(min=1)[:, None, None]
|
||||
sensor_mean = sensor_sum / denominators
|
||||
expected_mean = expected_sum / denominators
|
||||
residual = (sensor_mean - expected_mean).abs()
|
||||
covered = True
|
||||
for row in rows:
|
||||
expected_min, expected_max = [0.0] * 6, [0.0] * 6
|
||||
scenario = row["scenario"]
|
||||
if scenario:
|
||||
axis = (scenario - 1) // 2
|
||||
if scenario % 2:
|
||||
expected_max[axis] = cfg["disturbance_amplitude"][axis]
|
||||
else:
|
||||
expected_min[axis] = -cfg["disturbance_amplitude"][axis]
|
||||
covered &= row["applied_min_wrench"] == expected_min and row["applied_max_wrench"] == expected_max
|
||||
gates = {
|
||||
"floating_root": not robot.is_fixed_base,
|
||||
"physical_steps": all_count == steps * env.cfg.decimation,
|
||||
"signed_disturbance_coverage": covered,
|
||||
"full_protocol": count >= 13
|
||||
and args.duration >= cfg["episode_seconds"]
|
||||
and set(s.scenarios.tolist()) == set(range(13)),
|
||||
"no_vertical_guide": all(r["max_guide_fz_N"] == 0 for r in rows),
|
||||
"height": all(r["max_height_error_m"] <= cfg["height_error_limit_m"] for r in rows),
|
||||
"tilt": all(r["max_tilt_rad"] <= cfg["tilt_limit_rad"] for r in rows),
|
||||
"foot_drift": all(r["max_foot_drift_m"] <= cfg["foot_drift_limit_m"] for r in rows),
|
||||
"contact": all(r["contact_fraction"] >= cfg["contact_fraction_min"] for r in rows),
|
||||
"vertical_balance": all(
|
||||
r["vertical_balance_relative_error"] <= cfg["vertical_balance_relative_tolerance"] for r in rows
|
||||
),
|
||||
"limits": all(
|
||||
r["max_normalized_torque"] <= cfg["normalized_effort_velocity_limit"]
|
||||
and r["max_normalized_velocity"] <= cfg["normalized_effort_velocity_limit"]
|
||||
and r["max_joint_limit_excess_rad"] <= cfg["joint_limit_excess_rad"]
|
||||
for r in rows
|
||||
),
|
||||
"sensor_static_force": min(calibration_counts) >= 499
|
||||
and residual[..., :3].max().item() <= cfg["sensor_static_force_tolerance_N"],
|
||||
"sensor_static_torque": min(calibration_counts) >= 499
|
||||
and residual[..., 3:].max().item() <= cfg["sensor_static_torque_tolerance_Nm"],
|
||||
}
|
||||
# Indexed/empty reset contract, outside all physics statistics.
|
||||
before = s.robot.data.root_state_w.torch.clone()
|
||||
before_q = s.q().clone()
|
||||
ids = torch.arange(0, count, 2, device=env.device)
|
||||
others = torch.arange(1, count, 2, device=env.device)
|
||||
env.reset(env_ids=ids)
|
||||
reset_ok = (
|
||||
s.mount_wrench()[ids].abs().max().item() == 0
|
||||
and s.last_guide[ids].abs().max().item() == 0
|
||||
and env.action_manager.action[ids].abs().max().item() == 0
|
||||
)
|
||||
if len(others):
|
||||
reset_ok &= torch.equal(before[others], s.robot.data.root_state_w.torch[others]) and torch.equal(
|
||||
before_q[others], s.q()[others]
|
||||
)
|
||||
snapshot = {k: v.clone() for k, v in s.snapshot().items()}
|
||||
env.reset(env_ids=[])
|
||||
reset_ok &= all(torch.equal(v, s.snapshot()[k]) for k, v in snapshot.items())
|
||||
gates["indexed_reset"] = bool(reset_ok)
|
||||
# A separate short real timeout probe exercises pre-reset terminal observations.
|
||||
env.cfg.episode_length_s = 3 * env.step_dt
|
||||
env.reset()
|
||||
probe_action = torch.full((count, 3), 0.2, device=env.device)
|
||||
for _ in range(3):
|
||||
obs, _, term, timeout, info = env.step(probe_action)
|
||||
gates["timeout_terminal_reset"] = bool(
|
||||
not term.any()
|
||||
and timeout.all()
|
||||
and len(info["terminal_ids"]) == count
|
||||
and torch.equal(info["terminal_observation"]["joint_state"][:, :, 2], probe_action)
|
||||
and (obs["policy"]["joint_state"][:, :, 2] == 0).all()
|
||||
and (obs["policy"]["mount_wrench"] == 0).all()
|
||||
)
|
||||
old_actions = (env.action_manager.action.clone(), env.action_manager.prev_action.clone())
|
||||
rejected = 0
|
||||
for bad in (torch.zeros(count, 4, device=env.device), torch.full((count, 3), torch.nan, device=env.device)):
|
||||
try:
|
||||
env.step(bad)
|
||||
except ValueError:
|
||||
rejected += 1
|
||||
gates["bad_action_atomic_rejection"] = (
|
||||
rejected == 2
|
||||
and torch.equal(old_actions[0], env.action_manager.action)
|
||||
and torch.equal(old_actions[1], env.action_manager.prev_action)
|
||||
)
|
||||
passed = all(gates.values())
|
||||
return {
|
||||
"passed": passed,
|
||||
"contract": CONTRACT,
|
||||
"trained_skill": False,
|
||||
"controller": args.controller,
|
||||
"guide_enabled": env.cfg.guide_enabled,
|
||||
"gates": gates,
|
||||
"num_envs": count,
|
||||
"duration_s": args.duration,
|
||||
"physics_dt": env.physics_dt,
|
||||
"control_dt": env.step_dt,
|
||||
"physical_samples": all_count,
|
||||
"settled_physical_samples": sample_count,
|
||||
"rows": rows,
|
||||
"sensor_static_measured_world": sensor_mean.tolist(),
|
||||
"sensor_static_expected_world": expected_mean.tolist(),
|
||||
"calibration_windows_s": calibration_windows,
|
||||
"calibration_samples": calibration_counts,
|
||||
"sensor_static_max_force_error_N": residual[..., :3].max().item(),
|
||||
"sensor_static_max_torque_error_Nm": residual[..., 3:].max().item(),
|
||||
"solver": "PGS" if env.cfg.sim.physics.solver_type == 0 else "TGS",
|
||||
"device": env.device,
|
||||
"effective_mass_kg": dict(zip(robot.body_names, mass.tolist())),
|
||||
"total_mass_kg": total_mass,
|
||||
"sensor_convention": "parent-on-child, joint frame, mount origin; untared N/Nm",
|
||||
"missing_source_inertial_links": env.cfg.support_manifest["missing_source_inertial_links"],
|
||||
"no_autoresets_in_physics_trial": True,
|
||||
"timeout_probe_control_steps": 3,
|
||||
"negative_control_collapsed": args.controller == "disabled" and negative_control_collapsed(rows),
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
if not __debug__:
|
||||
raise RuntimeError("Verification requires assertions enabled")
|
||||
from isaaclab.app import AppLauncher
|
||||
|
||||
from isaaclab_tasks.utils import add_launcher_args, launch_simulation
|
||||
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--config", type=Path, default=Path("configs/standing/go2_fl_support_v0.json"))
|
||||
parser.add_argument("--num_envs", type=int, default=13)
|
||||
parser.add_argument("--duration", type=float, default=14)
|
||||
parser.add_argument("--controller", choices=("height", "hold", "disabled"), default="height")
|
||||
parser.add_argument("--free-base", action="store_true", help="Disable passive XY/rotation guidance; experimental")
|
||||
parser.add_argument("--scenario-offset", type=int, default=0)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
add_launcher_args(parser)
|
||||
args = parser.parse_args()
|
||||
if args.num_envs < 1 or not math.isfinite(args.duration) or args.duration <= 0:
|
||||
parser.error("Require positive environments/duration")
|
||||
args.output.mkdir(parents=True, exist_ok=False)
|
||||
write_json(args.output / "result.json", {"passed": False, "status": "running"})
|
||||
app = AppLauncher(args).app
|
||||
env, code, result = None, 1, {"passed": False}
|
||||
try:
|
||||
import gymnasium as gym
|
||||
import SkillFactory.tasks # noqa: F401
|
||||
from SkillFactory.legs.support import sha256
|
||||
from SkillFactory.tasks.manager_based.loaded_leg.cfg import LoadedLegEnvCfg
|
||||
|
||||
cfg = LoadedLegEnvCfg(support_config=str(args.config.resolve()))
|
||||
cfg.scene.num_envs, cfg.guide_enabled = args.num_envs, not args.free_base
|
||||
cfg.scenario_offset = args.scenario_offset
|
||||
cfg.episode_length_s = args.duration + 1.0 # Keep time_out registered for the later reset probe.
|
||||
cfg.terminations.fallen = None
|
||||
cfg.terminations.invalid = None # Per-physics audit raises instead, never auto-reset failed states.
|
||||
if args.controller == "disabled":
|
||||
cfg.scene.robot.actuators["leg"].stiffness = 0.0
|
||||
cfg.scene.robot.actuators["leg"].damping = 0.0
|
||||
with launch_simulation(cfg, args):
|
||||
env = gym.make("SkillFactory-LoadedLegStand-v0", cfg=cfg).unwrapped
|
||||
env.reset(seed=cfg.seed)
|
||||
result = evaluate(env, args)
|
||||
result.update(
|
||||
config_sha256=sha256(args.config),
|
||||
support_manifest_sha256=sha256(Path(cfg.support_parameters["support_asset"]) / "support.json"),
|
||||
)
|
||||
write_json(args.output / "config.json", cfg.support_parameters)
|
||||
print(
|
||||
json.dumps(
|
||||
{
|
||||
k: v
|
||||
for k, v in result.items()
|
||||
if k not in ("rows", "sensor_static_measured_world", "sensor_static_expected_world")
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
)
|
||||
code = 0 if result["passed"] else 2
|
||||
except BaseException:
|
||||
error = traceback.format_exc()
|
||||
print(error, file=sys.stderr, flush=True)
|
||||
result = {"passed": False, "error": error}
|
||||
finally:
|
||||
finish_run(app, env, args.output / "result.json", result, code)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Re-evaluate a release using the exact same gates as the publishing transaction."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
from runtime_common import write_json
|
||||
from stage2_acceptance import evaluate_release, read_json, require_acceptance
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--run", type=Path, required=True)
|
||||
parser.add_argument("--release", type=Path, required=True)
|
||||
parser.add_argument("--config", type=Path, required=True)
|
||||
parser.add_argument(
|
||||
"--output", type=Path, help="Default: RUN/acceptance.json; use a new path to retain historical evidence"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
output = args.output or args.run / "acceptance.json"
|
||||
write_json(output, {"passed": False, "milestone_complete": False, "status": "running"})
|
||||
try:
|
||||
report = evaluate_release(
|
||||
args.run, args.release, read_json(args.config), read_json(args.run / "release_standalone.json")
|
||||
)
|
||||
report["status"] = "completed" if report["passed"] else "failed"
|
||||
write_json(output, report)
|
||||
print(json.dumps({"milestone_complete": report["milestone_complete"], "checks": report["checks"]}, indent=2))
|
||||
except BaseException:
|
||||
write_json(
|
||||
output, {"passed": False, "milestone_complete": False, "status": "failed", "error": traceback.format_exc()}
|
||||
)
|
||||
raise
|
||||
require_acceptance(report)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user