315 lines
14 KiB
Python
315 lines
14 KiB
Python
"""Synthetic CPU-only four-skills seam tests: no Kit, robot data, or physical claims."""
|
|
|
|
import hashlib
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import sys
|
|
import types
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
LEGS = ROOT / "source/SkillFactory/SkillFactory/legs"
|
|
SPEC = importlib.util.spec_from_file_location("_four_skills_model_test", LEGS / "four_skills/model.py")
|
|
model_module = importlib.util.module_from_spec(SPEC)
|
|
SPEC.loader.exec_module(model_module)
|
|
FourSkillActorCritic = model_module.FourSkillActorCritic
|
|
features = model_module.features
|
|
save_checkpoint = model_module.save_checkpoint
|
|
load_policy = model_module.load_policy
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def cpu_only():
|
|
# Small synthetic matrices do not benefit from a large host thread pool.
|
|
previous = torch.get_num_threads()
|
|
torch.set_num_threads(1)
|
|
yield
|
|
torch.set_num_threads(previous)
|
|
|
|
|
|
def test_four_skills_model_shapes_zero_baseline_and_shared_inputs():
|
|
model = FourSkillActorCritic(hidden=16)
|
|
x = torch.randn(7, 48)
|
|
assert features(x) is x
|
|
action, latent, logp, value = model.act(x, deterministic=True)
|
|
assert action.shape == latent.shape == (7, 3)
|
|
assert logp.shape == value.shape == (7,)
|
|
assert not action.any() and not latent.any()
|
|
assert torch.isfinite(logp).all() and torch.isfinite(value).all()
|
|
assert torch.equal(value, model.value(x))
|
|
assert model.actor[0].in_features == model.critic[0].in_features == 48
|
|
assert model.feature_version == "four-skills-48-fixed-si-v1"
|
|
assert model.model_config == {"hidden": 16, "initial_std": 0.08}
|
|
assert model.critic[-1].weight.any()
|
|
assert not torch.optim.Adam(model.parameters()).state
|
|
|
|
|
|
def test_four_skills_model_fixed_si_order_and_actor_critic_normalization():
|
|
model = FourSkillActorCritic(hidden=8)
|
|
expected_offset = torch.zeros(48)
|
|
expected_offset[:3] = torch.tensor([0.0, 0.8, -1.5])
|
|
expected_scale = torch.tensor(
|
|
[0.5] * 3 + [10.0] * 3 + [50.0] * 3 + [5.0] * 3
|
|
+ [1.0] * 3 + [5.0] * 3 + [0.1] * 3 + [0.5] * 3
|
|
+ [0.4] * 3 + [1.0] * 3 + [50.0] * 3 + [0.05]
|
|
+ [1.0] * 4 + [0.4] * 3 + [1.0] + [1.0] * 3 + [1.0] * 3
|
|
)
|
|
assert torch.equal(model.offset, expected_offset)
|
|
assert torch.equal(model.scale, expected_scale)
|
|
seen = []
|
|
handles = [net[0].register_forward_pre_hook(lambda _, args: seen.append(args[0]))
|
|
for net in (model.actor, model.critic)]
|
|
normalized = torch.linspace(-2, 2, 48).expand(2, -1)
|
|
model.value(expected_offset + expected_scale * normalized)
|
|
for handle in handles:
|
|
handle.remove()
|
|
assert len(seen) == 2
|
|
assert torch.allclose(seen[0], normalized, atol=1e-6)
|
|
assert torch.equal(seen[0], seen[1])
|
|
|
|
|
|
def test_four_skills_model_latent_logp_tanh_and_std_bounds():
|
|
model = FourSkillActorCritic(hidden=8)
|
|
x = torch.randn(11, 48)
|
|
action, latent, logp, value = model.act(x)
|
|
assert latent.any() and action.abs().max() <= 1
|
|
assert torch.equal(action, latent.tanh())
|
|
evaluated_logp, evaluated_value = model.evaluate(x, latent)
|
|
assert torch.equal(logp, evaluated_logp) and torch.equal(value, evaluated_value)
|
|
expected = torch.distributions.Normal(torch.zeros_like(latent), 0.08).log_prob(latent).sum(-1)
|
|
assert torch.allclose(logp, expected, atol=1e-6)
|
|
for log_std, expected_std in [(-100.0, 0.02), (100.0, 0.5)]:
|
|
with torch.no_grad():
|
|
model.log_std.fill_(log_std)
|
|
assert torch.allclose(model.distribution_value(x)[0].stddev, torch.full_like(latent, expected_std))
|
|
|
|
|
|
@pytest.mark.parametrize("kwargs", [
|
|
{"hidden": True}, {"hidden": 8.0}, {"hidden": 7}, {"hidden": 0}, {"hidden": -1},
|
|
{"initial_std": True}, {"initial_std": "0.08"}, {"initial_std": 0.0},
|
|
{"initial_std": float("nan")}, {"initial_std": float("inf")},
|
|
{"initial_std": 0.019}, {"initial_std": 0.501},
|
|
])
|
|
def test_four_skills_model_rejects_invalid_args(kwargs):
|
|
with pytest.raises(ValueError):
|
|
FourSkillActorCritic(**kwargs)
|
|
|
|
|
|
@pytest.mark.parametrize("bad", [
|
|
torch.zeros(2, 46), torch.zeros(2, 32), torch.zeros(48), torch.zeros(0, 48),
|
|
torch.zeros(2, 48, dtype=torch.float64), torch.zeros(2, 48, dtype=torch.int64),
|
|
torch.full((2, 48), float("nan")), torch.full((2, 48), float("inf")), [[0.0] * 48],
|
|
torch.empty(2, 48, device="meta"), torch.zeros(2, 48).to_sparse(),
|
|
])
|
|
def test_four_skills_model_rejects_bad_features(bad):
|
|
model = FourSkillActorCritic(hidden=8)
|
|
for method in (features, model.act, model.value):
|
|
with pytest.raises(ValueError, match="features"):
|
|
method(bad)
|
|
|
|
|
|
@pytest.mark.parametrize("bad", [torch.zeros(2, 4), torch.zeros(3, 3),
|
|
torch.zeros(2, 3, dtype=torch.float64),
|
|
torch.full((2, 3), float("nan")), None,
|
|
torch.empty(2, 3, device="meta")])
|
|
def test_four_skills_model_rejects_bad_latents(bad):
|
|
with pytest.raises(ValueError, match="latent"):
|
|
FourSkillActorCritic(hidden=8).evaluate(torch.zeros(2, 48), bad)
|
|
|
|
|
|
def test_four_skills_model_exact_types_and_global_default_dtype():
|
|
with pytest.raises(ValueError, match="bool"):
|
|
FourSkillActorCritic(hidden=8).act(torch.zeros(2, 48), deterministic=1)
|
|
default = FourSkillActorCritic()
|
|
assert default.model_config == {"hidden": 128, "initial_std": 0.08}
|
|
for std in (0.02, 0.5):
|
|
assert FourSkillActorCritic(hidden=8, initial_std=std).model_config["initial_std"] == std
|
|
previous = torch.get_default_dtype()
|
|
try:
|
|
torch.set_default_dtype(torch.float64)
|
|
model = FourSkillActorCritic(hidden=8)
|
|
assert all(p.dtype == torch.float32 for p in model.parameters())
|
|
assert model.value(torch.zeros(2, 48, dtype=torch.float32)).dtype == torch.float32
|
|
finally:
|
|
torch.set_default_dtype(previous)
|
|
|
|
|
|
def test_four_skills_model_generic_standing_ppo_step_synthetic():
|
|
# Private test namespace avoids SkillFactory.__init__ (Kit registration), even
|
|
# when this test is invoked directly rather than through scripts/test_cpu.py.
|
|
name = "_four_skills_cpu_legs"
|
|
package = types.ModuleType(name)
|
|
package.__path__ = [str(LEGS)]
|
|
sys.modules[name] = package
|
|
try:
|
|
spec = importlib.util.spec_from_file_location(name + ".standing_ppo", LEGS / "standing_ppo.py")
|
|
standing = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(standing)
|
|
torch.manual_seed(23)
|
|
model = FourSkillActorCritic(hidden=8)
|
|
optimizer = torch.optim.Adam(model.parameters(), lr=3e-4)
|
|
x = torch.randn(3, 4, 48)
|
|
with torch.no_grad():
|
|
_, latent, logp, value = model.act(x.flatten(0, 1))
|
|
data = {"features": x, "latent": latent.reshape(3, 4, 3),
|
|
"log_prob": logp.reshape(3, 4), "value": value.reshape(3, 4),
|
|
"next_value": torch.zeros(3, 4), "reward": torch.randn(3, 4),
|
|
"done": torch.zeros(3, 4, dtype=torch.bool)}
|
|
cfg = {"gamma": 0.99, "gae_lambda": 0.95, "epochs": 2, "minibatch_size": 4,
|
|
"target_kl": 1.0, "clip_ratio": 0.2, "value_coefficient": 0.5, "max_gradient_norm": 1.0}
|
|
result = standing.ppo_step(model, optimizer, data, cfg)
|
|
assert result["minibatches"] == 6 and result["sampling_log_prob_error"] == 0
|
|
assert optimizer.state and model.actor[-1].weight.any()
|
|
assert all(torch.isfinite(p).all() for p in model.parameters())
|
|
finally:
|
|
for module in list(sys.modules):
|
|
if module == name or module.startswith(name + "."):
|
|
del sys.modules[module]
|
|
|
|
|
|
@pytest.fixture
|
|
def checkpoint(tmp_path):
|
|
torch.manual_seed(7)
|
|
model = FourSkillActorCritic(hidden=8)
|
|
optimizer = torch.optim.Adam(model.parameters(), lr=3e-4)
|
|
# Exercise nonempty Adam moments without robot data or a training run.
|
|
x = torch.randn(4, 48)
|
|
logp, value = model.evaluate(x, torch.randn(4, 3) * 0.08)
|
|
(value.square().mean() - logp.mean()).backward()
|
|
optimizer.step()
|
|
path = tmp_path / "policy.pt"
|
|
identity = {"task": "go2-fl-four-skills-v0.5.0", "source": "synthetic", "seed": 7}
|
|
save_checkpoint(path, model, optimizer, identity, 1, {"loss": 0.25})
|
|
return path, model, optimizer, identity, x
|
|
|
|
|
|
def assert_tree_equal(left, right):
|
|
assert type(left) is type(right)
|
|
if isinstance(left, torch.Tensor):
|
|
assert torch.equal(left, right)
|
|
elif isinstance(left, dict):
|
|
assert left.keys() == right.keys()
|
|
for key in left:
|
|
assert_tree_equal(left[key], right[key])
|
|
elif isinstance(left, (list, tuple)):
|
|
assert len(left) == len(right)
|
|
for a, b in zip(left, right):
|
|
assert_tree_equal(a, b)
|
|
else:
|
|
assert left == right
|
|
|
|
|
|
def test_four_skills_model_checkpoint_exact_roundtrip(checkpoint):
|
|
path, model, optimizer, identity, x = checkpoint
|
|
rng = torch.get_rng_state().clone()
|
|
digest = hashlib.sha256(path.read_bytes()).hexdigest()
|
|
loaded, payload = load_policy(path, expected_sha256=digest, expected_identity=dict(reversed(list(identity.items()))))
|
|
assert not loaded.training and next(loaded.parameters()).device.type == "cpu"
|
|
assert torch.equal(rng, torch.get_rng_state()) # loading does not reseed callers
|
|
assert payload["format"] == "skillfactory.four-skills-ppo.v1"
|
|
assert payload["features"] == model.feature_version and payload["feature_dim"] == 48
|
|
assert payload["update"] == 1 and payload["metrics"] == {"loss": 0.25}
|
|
assert_tree_equal(dict(model.state_dict()), dict(loaded.state_dict()))
|
|
assert_tree_equal(optimizer.state_dict(), payload["optimizer"])
|
|
assert torch.equal(payload["rng"]["cpu"], rng)
|
|
assert payload["rng"]["cuda"] == []
|
|
for a, b in zip(model.act(x, deterministic=True), loaded.act(x, deterministic=True)):
|
|
assert torch.equal(a, b)
|
|
with pytest.raises(ValueError, match="identity"):
|
|
load_policy(path, expected_identity={**identity, "source": "wrong"})
|
|
with pytest.raises(ValueError, match="device"):
|
|
load_policy(path, device="not-a-device")
|
|
|
|
|
|
def test_four_skills_model_digest_checked_before_torch_load(checkpoint, monkeypatch):
|
|
path, *_ = checkpoint
|
|
calls = []
|
|
monkeypatch.setattr(torch, "load", lambda *args, **kwargs: calls.append(args))
|
|
with pytest.raises(ValueError, match="SHA256"):
|
|
load_policy(path, expected_sha256="0" * 64)
|
|
assert not calls
|
|
|
|
|
|
@pytest.mark.parametrize("field,value", [
|
|
("format", "skillfactory.standing-ppo-checkpoint.v1"),
|
|
("features", "contact-cycle-sim-46-fixed-si-v1"), ("feature_dim", 46), ("feature_dim", 32),
|
|
("feature_dim", 48.0), ("model_config", {"hidden": True, "initial_std": 0.08}),
|
|
("model_config", {"hidden": 8, "initial_std": float("nan")}),
|
|
("model_config", {"hidden": 8, "initial_std": 0.08, "extra": 1}),
|
|
("update", True), ("update", -1), ("rng", {}), ("optimizer", {}), ("identity", {}),
|
|
("rng", {"cpu": torch.zeros(2, dtype=torch.uint8), "cuda": []}),
|
|
("identity", {"not_json": torch.zeros(1)}), ("metrics", {"loss": float("inf")}),
|
|
])
|
|
def test_four_skills_model_rejects_malformed_checkpoint(checkpoint, field, value):
|
|
path, *_ = checkpoint
|
|
payload = torch.load(path, weights_only=True)
|
|
payload[field] = value
|
|
torch.save(payload, path)
|
|
with pytest.raises(ValueError):
|
|
load_policy(path)
|
|
|
|
|
|
@pytest.mark.parametrize("corruption", ["nonfinite", "optimizer_nonfinite", "shape", "dtype", "scale", "missing", "extra"])
|
|
def test_four_skills_model_rejects_corrupt_state(checkpoint, corruption):
|
|
path, *_ = checkpoint
|
|
payload = torch.load(path, weights_only=True)
|
|
state = payload["model"]
|
|
if corruption == "nonfinite":
|
|
state["critic.0.weight"][0, 0] = float("inf")
|
|
elif corruption == "optimizer_nonfinite":
|
|
next(iter(payload["optimizer"]["state"].values()))["exp_avg"].flatten()[0] = float("nan")
|
|
elif corruption == "shape":
|
|
state["actor.0.weight"] = torch.zeros(8, 46)
|
|
elif corruption == "dtype":
|
|
state["log_std"] = state["log_std"].double()
|
|
elif corruption == "scale":
|
|
state["scale"][0] = 1.0
|
|
elif corruption == "missing":
|
|
del state["log_std"]
|
|
else:
|
|
state["extra"] = torch.zeros(1)
|
|
torch.save(payload, path)
|
|
with pytest.raises(ValueError):
|
|
load_policy(path)
|
|
|
|
|
|
@pytest.mark.parametrize("corruption", ["parameter", "optimizer", "metrics", "custom"])
|
|
def test_four_skills_model_atomic_nonfinite_and_custom_object_rejection(checkpoint, corruption):
|
|
path, model, optimizer, identity, _ = checkpoint
|
|
original = path.read_bytes()
|
|
metrics = None
|
|
if corruption == "parameter":
|
|
with torch.no_grad():
|
|
next(model.parameters()).flatten()[0] = float("nan")
|
|
elif corruption == "optimizer":
|
|
next(iter(optimizer.state.values()))["exp_avg"].flatten()[0] = float("inf")
|
|
elif corruption == "metrics":
|
|
metrics = {"loss": float("nan")}
|
|
else:
|
|
metrics = {"custom": object()}
|
|
with pytest.raises(ValueError):
|
|
save_checkpoint(path, model, optimizer, identity, 2, metrics)
|
|
assert path.read_bytes() == original
|
|
assert list(path.parent.iterdir()) == [path]
|
|
|
|
|
|
def test_four_skills_model_atomic_replace_failure_cleanup(checkpoint, monkeypatch):
|
|
path, model, optimizer, identity, _ = checkpoint
|
|
original = path.read_bytes()
|
|
def fail_replace(*args):
|
|
raise OSError("synthetic replace failure")
|
|
monkeypatch.setattr(model_module.os, "replace", fail_replace)
|
|
with pytest.raises(OSError, match="synthetic"):
|
|
save_checkpoint(path, model, optimizer, identity, 2)
|
|
assert path.read_bytes() == original
|
|
assert list(path.parent.iterdir()) == [path]
|
|
|
|
|
|
def test_four_skills_model_rejects_unreadable_checkpoint(tmp_path):
|
|
path = tmp_path / "bad.pt"
|
|
path.write_bytes(b"not a torch checkpoint")
|
|
with pytest.raises(ValueError, match="weights-only"):
|
|
load_policy(path)
|