093ab883be
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.
176 lines
7.0 KiB
Python
176 lines
7.0 KiB
Python
#!/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()
|