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.
191 lines
7.7 KiB
Python
191 lines
7.7 KiB
Python
"""No Kit required: offline updates, exact resume, padding and immutable inference."""
|
|
|
|
import copy
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
import torch
|
|
from SkillFactory.legs.frozen_policy import Actor, FrozenPolicy
|
|
from SkillFactory.legs.offline import IQL, atomic_save, canonical_hash, features, sha256
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
|
|
def training_config():
|
|
return json.loads((ROOT / "configs/training/go2_fl_offline_v1.json").read_text())["training"]
|
|
|
|
|
|
def test_iql_exact_resume_and_finite_update(tmp_path):
|
|
torch.set_num_threads(1)
|
|
torch.manual_seed(7)
|
|
cfg = training_config()
|
|
learner = IQL(cfg, torch.zeros(15), torch.ones(15))
|
|
data = {
|
|
"state": torch.randn(64, 15),
|
|
"next_state": torch.randn(64, 15),
|
|
"action": torch.randn(64, 3).tanh(),
|
|
"reward": torch.rand(64),
|
|
"terminated": torch.zeros(64, dtype=torch.bool),
|
|
}
|
|
before = copy.deepcopy(learner.actor.state_dict())
|
|
for _ in range(3):
|
|
learner.update(data)
|
|
atomic_save(learner.state_dict(), tmp_path / "checkpoint.pt")
|
|
ids = torch.randint(64, (32,))
|
|
learner.update({k: v[ids] for k, v in data.items()})
|
|
after = learner.state_dict()
|
|
restored = IQL(cfg, torch.zeros(15), torch.ones(15))
|
|
restored.load_state_dict(torch.load(tmp_path / "checkpoint.pt", weights_only=True))
|
|
restored_ids = torch.randint(64, (32,))
|
|
assert torch.equal(ids, restored_ids)
|
|
restored.update({k: v[restored_ids] for k, v in data.items()})
|
|
for name, values in after["models"].items():
|
|
assert all(torch.equal(value, restored.state_dict()["models"][name][key]) for key, value in values.items())
|
|
assert any(not torch.equal(value, learner.actor.state_dict()[key]) for key, value in before.items())
|
|
data["reward"][0] = float("nan")
|
|
with pytest.raises(RuntimeError, match="Non-finite"):
|
|
learner.update(data)
|
|
|
|
|
|
def make_package(tmp_path):
|
|
torch.manual_seed(1)
|
|
shapes = {
|
|
"joint_description": (1, 8, 25),
|
|
"joint_observation": (1, 8, 3),
|
|
"joint_mask": (1, 8),
|
|
"effector_description": (1, 1, 16),
|
|
"effector_observation": (1, 1, 11),
|
|
"effector_mask": (1, 1),
|
|
"context": (1, 10),
|
|
"goal": (1, 3),
|
|
}
|
|
tokens = {
|
|
key: torch.zeros(shape, dtype=torch.bool if "mask" in key else torch.float32) for key, shape in shapes.items()
|
|
}
|
|
tokens["joint_mask"][:, :3] = True
|
|
tokens["effector_mask"][:] = True
|
|
binding = {
|
|
"part_id": "go2_fl",
|
|
"manifest_sha256": "a" * 64,
|
|
"context": tokens["context"][0].tolist(),
|
|
"static_tokens": {
|
|
k: tokens[k][0].tolist()
|
|
for k in ("joint_description", "joint_mask", "effector_description", "effector_mask")
|
|
},
|
|
}
|
|
(tmp_path / "binding.json").write_text(json.dumps(binding))
|
|
torch.save(Actor().state_dict(), tmp_path / "actor.pt")
|
|
for name in ("config.json", "README.md", "frozen_policy.py"):
|
|
(tmp_path / name).write_text("test")
|
|
manifest = {
|
|
"format": "skillfactory.skillbundle.v1",
|
|
"interface": "go2-fl-reach-leg-token-v1",
|
|
"hidden": 128,
|
|
"version": "test",
|
|
"files": {p.name: sha256(p) for p in tmp_path.iterdir()},
|
|
}
|
|
(tmp_path / "manifest.json").write_text(json.dumps(manifest))
|
|
return tokens, binding
|
|
|
|
|
|
def test_frozen_inference_and_fail_closed(tmp_path):
|
|
tokens, binding = make_package(tmp_path)
|
|
policy = FrozenPolicy(tmp_path, part_id="go2_fl", manifest_sha256=binding["manifest_sha256"])
|
|
state = copy.deepcopy(policy._actor.state_dict())
|
|
result = policy.act(tokens)
|
|
assert result.shape == (1, 8) and not result.requires_grad
|
|
assert features(tokens).shape == (1, 15)
|
|
tokens["joint_observation"][:, 3:] = float("nan")
|
|
tokens["joint_description"][:, 3:] = float("nan")
|
|
assert torch.equal(result, policy.act(tokens))
|
|
assert all(torch.equal(value, policy._actor.state_dict()[key]) for key, value in state.items())
|
|
tokens["goal"][0, 0] = float("inf")
|
|
with pytest.raises(ValueError, match="Non-finite"):
|
|
policy.act(tokens)
|
|
with pytest.raises(ValueError, match="hardware"):
|
|
FrozenPolicy(tmp_path, part_id="go1_fl", manifest_sha256=binding["manifest_sha256"])
|
|
with (tmp_path / "actor.pt").open("ab") as stream:
|
|
stream.write(b"x")
|
|
with pytest.raises(ValueError, match="integrity"):
|
|
FrozenPolicy(tmp_path, part_id="go2_fl", manifest_sha256=binding["manifest_sha256"])
|
|
|
|
|
|
def test_release_rejects_same_actor_different_tested_package(tmp_path, monkeypatch):
|
|
monkeypatch.syspath_prepend(str(ROOT / "scripts"))
|
|
from stage2_acceptance import independent_load_passed
|
|
|
|
candidate = tmp_path / "candidate"
|
|
candidate.mkdir()
|
|
make_package(candidate)
|
|
tested_digest = sha256(candidate / "manifest.json")
|
|
# Same weights, changed inference payload/binding metadata: old evidence is stale.
|
|
(candidate / "README.md").write_text("changed payload")
|
|
manifest = json.loads((candidate / "manifest.json").read_text())
|
|
manifest["files"]["README.md"] = sha256(candidate / "README.md")
|
|
(candidate / "manifest.json").write_text(json.dumps(manifest))
|
|
report = {
|
|
"passed": True,
|
|
"status": "completed",
|
|
"isolated": True,
|
|
"package_only": True,
|
|
"parameters_and_buffers_unchanged": True,
|
|
"requires_grad": False,
|
|
"training_or_simulator_imports": False,
|
|
"actor_sha256": sha256(candidate / "actor.pt"),
|
|
"manifest_sha256": tested_digest,
|
|
}
|
|
assert independent_load_passed(report, candidate, tested_digest)
|
|
assert not independent_load_passed(report, candidate, sha256(candidate / "manifest.json"))
|
|
|
|
|
|
def test_hash_is_order_independent_and_nan_rejected():
|
|
assert canonical_hash({"a": 1, "b": 2}) == canonical_hash({"b": 2, "a": 1})
|
|
with pytest.raises(ValueError):
|
|
canonical_hash({"bad": float("nan")})
|
|
|
|
|
|
def test_stability_samples_memory_at_end(tmp_path, monkeypatch):
|
|
monkeypatch.syspath_prepend(str(ROOT / "scripts"))
|
|
spec = importlib.util.spec_from_file_location("stage2_sim", ROOT / "scripts/stage2_sim.py")
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
tokens, _ = make_package(tmp_path)
|
|
weights = tmp_path / "checkpoint.pt"
|
|
torch.save({"learner": {"models": {"actor": Actor().state_dict()}}}, weights)
|
|
env = SimpleNamespace(
|
|
device="cpu",
|
|
num_envs=1,
|
|
step_dt=0.01,
|
|
reset=lambda **kwargs: None,
|
|
step=lambda action: (
|
|
None,
|
|
torch.zeros(1),
|
|
torch.zeros(1, dtype=torch.bool),
|
|
torch.zeros(1, dtype=torch.bool),
|
|
None,
|
|
),
|
|
)
|
|
adapter = SimpleNamespace(snapshot=lambda: tokens)
|
|
monkeypatch.setattr(module, "goals", lambda *a, **kw: None)
|
|
audit = {
|
|
"max_normalized_torque": 0,
|
|
"max_normalized_velocity": 0,
|
|
"joint_limit_excess_rad": 0,
|
|
"anchor_pose_error": 0,
|
|
"invalid_states": 0,
|
|
"physical_samples": 0,
|
|
}
|
|
monkeypatch.setattr(module, "Audit", lambda *a: SimpleNamespace(report=lambda: audit))
|
|
memory = iter([(1000, 2000), (1000, 2000), (800, 2000)])
|
|
monkeypatch.setattr(torch.cuda, "synchronize", lambda: None)
|
|
monkeypatch.setattr(torch.cuda, "mem_get_info", lambda: next(memory))
|
|
cfg = json.loads((ROOT / "configs/training/go2_fl_offline_v1.json").read_text())
|
|
cfg["test"]["stability_seconds"] = 0.1
|
|
args = SimpleNamespace(package=None, weights=weights, mode="stability")
|
|
report = module.evaluate(env, adapter, cfg, args)
|
|
assert report["device_used_before"] == 1000 and report["device_used_max"] == 1200
|
|
assert report["memory_growth_fraction"] == 0.2 and not report["passed"]
|