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

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

169 lines
7.2 KiB
Python

#!/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()