Files
SkillFactory/tests/test_standing_ppo.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

165 lines
5.9 KiB
Python

"""Pure PPO/interface tests; these fixtures are not physical standing acceptance."""
import copy
import json
from pathlib import Path
import pytest
import torch
from SkillFactory.legs.standing_ppo import (
SHAPES,
StandingActorCritic,
bootstrap_values,
canonical_hash,
features,
load_training_config,
ppo_step,
restore_checkpoint,
save_checkpoint,
selection_height_error,
)
ROOT = Path(__file__).resolve().parents[1]
CONFIG = ROOT / "configs/training/go2_fl_loaded_stand_ppo_v1.json"
def observations(n=13):
result = {k: torch.zeros(n, *shape) for k, shape in SHAPES.items()}
result["joint_state"][:, :, 0] = torch.tensor([0.0, 0.8, -1.5])
result["goal"][:] = 0.3346
result["base_state"][:, 2] = 0.3346
result["base_state"][:, 6] = 1 # xyzw
result["mount_wrench"][:, 2] = -29.43
result["foot_state"][:, 8] = 52.86
return result
def test_features_include_six_axis_sensor_without_input_mutation():
obs = observations()
old = {k: v.clone() for k, v in obs.items()}
x = features(obs)
assert x.shape == (13, 45) and (x[:, 11] == 0).all()
assert torch.equal(x[:, 22:28], obs["mount_wrench"])
assert all(torch.equal(v, old[k]) for k, v in obs.items())
model = StandingActorCritic(hidden=32)
assert (model.scale > 0).all()
with torch.no_grad():
a, latent, logp, value = model.act(x)
assert a.shape == (13, 3) and a.abs().max() <= 1 and torch.equal(a, latent.tanh())
torch.testing.assert_close(model.evaluate(x, latent)[0], logp)
assert value.shape == (13,)
assert torch.equal(model.act(x, True)[0], model.act(x, True)[0])
@pytest.mark.parametrize("mutation", ["missing", "dtype", "shape", "nan", "empty"])
def test_feature_contract_rejects(mutation):
obs = observations(0 if mutation == "empty" else 2)
if mutation == "missing":
obs.pop("mount_wrench")
if mutation == "dtype":
obs["mount_wrench"] = obs["mount_wrench"].double()
if mutation == "shape":
obs["mount_wrench"] = obs["mount_wrench"][:, :3]
if mutation == "nan":
obs["mount_wrench"][0, 0] = torch.nan
with pytest.raises(ValueError):
features(obs)
def test_selection_is_independent_of_post_failure_autoreset_trajectory():
failed = torch.tensor([False, True, True])
a = selection_height_error(torch.tensor([0.01, 0.02, 1.0]), failed)
b = selection_height_error(torch.tensor([0.01, 9.0, 0.0]), failed)
assert torch.equal(a, b)
torch.testing.assert_close(a, torch.tensor([0.01, 0.1, 0.1]))
def test_timeout_bootstrap_uses_terminal_sensor_not_reset_and_termination_wins():
class ValueFromSensor:
def value(self, x):
return x[:, 22].clone()
reset = observations(3)
reset["mount_wrench"][:, 0] = torch.tensor([1.0, 2.0, 3.0])
terminal = observations(2)
terminal["mount_wrench"][:, 0] = torch.tensor([30.0, 99.0])
info = {"terminal_ids": torch.tensor([1, 2], dtype=torch.int32), "terminal_observation": terminal}
value = bootstrap_values(
ValueFromSensor(), features(reset), torch.tensor([False, False, True]), torch.tensor([False, True, True]), info
)
assert value.tolist() == [1.0, 30.0, 0.0]
with pytest.raises(RuntimeError, match="pre-reset"):
bootstrap_values(
ValueFromSensor(),
features(reset),
torch.zeros(3, dtype=torch.bool),
torch.ones(3, dtype=torch.bool),
{"terminal_ids": None},
)
def rollout(model):
x = torch.randn(4, 13, 45)
with torch.no_grad():
_, z, logp, value = model.act(x.flatten(0, 1))
return {
"features": x,
"latent": z.reshape(4, 13, 3),
"log_prob": logp.reshape(4, 13),
"value": value.reshape(4, 13),
"next_value": torch.zeros(4, 13),
"reward": torch.randn(4, 13),
"done": torch.zeros(4, 13, dtype=torch.bool),
}
def test_optimizer_and_rng_exact_restore_for_same_next_batch(tmp_path):
torch.set_num_threads(1)
torch.manual_seed(123)
cfg = load_training_config(CONFIG)
cfg.update(minibatch_size=26, epochs=2, target_kl=1.0)
model = StandingActorCritic(hidden=32)
optimizer = torch.optim.Adam(model.parameters(), lr=cfg["learning_rate"], eps=1e-5)
ppo_step(model, optimizer, rollout(model), cfg)
data = rollout(model)
identity = {"config": canonical_hash(cfg)}
path = tmp_path / "checkpoint.pt"
save_checkpoint(path, model, optimizer, identity, 1, 3.0, None)
before = copy.deepcopy(model.state_dict())
a = ppo_step(model, optimizer, data, cfg)
result_a, opt_a = copy.deepcopy(model.state_dict()), copy.deepcopy(optimizer.state_dict())
restored = restore_checkpoint(path, model, optimizer, identity)
assert restored["update"] == 1 and restored["accepted_skill"] is False
assert all(torch.equal(model.state_dict()[k], v) for k, v in before.items())
b = ppo_step(model, optimizer, data, cfg)
assert a == b and any(not torch.equal(v, before[k]) for k, v in result_a.items())
assert all(torch.equal(model.state_dict()[k], v) for k, v in result_a.items())
for key, values in opt_a["state"].items():
for name, value in values.items():
assert torch.equal(optimizer.state_dict()["state"][key][name], value)
with pytest.raises(ValueError, match="identity"):
restore_checkpoint(path, model, optimizer, {"config": "wrong"})
@pytest.mark.parametrize(
"key,value",
[
("seed", 82000),
("gamma", 2.0),
("epochs", 0),
("initial_std", 1.0),
("train_amplitude_scale", [0.0, 1.0]),
("train_time_shift_s", [-1.0, 0.0]),
("num_envs", 12),
("learning_rate", float("nan")),
("horizon", True),
],
)
def test_training_config_rejects_invalid(tmp_path, key, value):
cfg = load_training_config(CONFIG)
cfg[key] = value
path = tmp_path / "cfg.json"
path.write_text(json.dumps(cfg))
with pytest.raises(ValueError):
load_training_config(path)