9fe9d27865
Consolidate next-stage docs, archive superseded narratives, and preserve frozen document inputs. Retain invalid P2 pairs, partial RootXY training, failed attempts and nonselectable checkpoints as explicit limitations. Include supervised evaluation/training, desktop GPU admission, bounded evidence and atomic-publication monitor repairs with CPU regressions. Ignore local dotenv configuration. Validation: 1403 passed, 6 skipped for contact-cycle and related contracts/CLI tests. Full-suite legacy ABI and registration tests require Python 3.12/Torch 2.10/pxr; no runtime checks relaxed. No new physical run or accepted skill.
265 lines
12 KiB
Python
265 lines
12 KiB
Python
"""CPU/synthetic learner tests only; none performs robot training or holdout evaluation."""
|
|
|
|
import copy
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import torch
|
|
from SkillFactory.legs.contact_cycle.config import load_config
|
|
from SkillFactory.legs.contact_cycle.curriculum import EpisodeSampler, assert_sample_domain, curriculum_stage
|
|
from SkillFactory.legs.contact_cycle.ppo import (
|
|
CycleActorCritic,
|
|
bootstrap_values,
|
|
collect_rollout,
|
|
features,
|
|
ppo_step,
|
|
restore_checkpoint,
|
|
save_checkpoint,
|
|
selection_score,
|
|
)
|
|
from SkillFactory.legs.contact_cycle.training import (
|
|
BudgetExceeded,
|
|
BudgetLedger,
|
|
digest,
|
|
load_training_config,
|
|
require_authorization,
|
|
save_trace_subset,
|
|
)
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
TASK = ROOT/"configs/contact_cycle/go2_fl_v1.json"
|
|
TRAINING = ROOT/"configs/contact_cycle/training_v1.json"
|
|
|
|
|
|
@pytest.mark.parametrize("update,stage", [(1, 0), (512, 0), (513, 1), (1024, 1), (1025, 2), (2048, 2)])
|
|
def test_stage_boundaries(update, stage):
|
|
assert curriculum_stage(update) == stage
|
|
|
|
|
|
def test_sampler_no_holdout_and_reset_order_independence():
|
|
cfg = load_config(TASK)
|
|
with pytest.raises(ValueError, match="Independent"):
|
|
EpisodeSampler(cfg, 4, 1, "holdout")
|
|
a, b = EpisodeSampler(cfg, 4, 17, update=1500), EpisodeSampler(cfg, 4, 17, update=1500)
|
|
first = a.sample(torch.tensor([0, 2]))
|
|
second = b.sample(torch.tensor([2, 0]))
|
|
for key in first:
|
|
torch.testing.assert_close(first[key], second[key].flip(0))
|
|
a.sample(torch.tensor([1]))
|
|
resumed = EpisodeSampler(cfg, 4, 17)
|
|
resumed.load_state_dict(a.state_dict())
|
|
x, y = a.sample(torch.tensor([0])), resumed.sample(torch.tensor([0]))
|
|
for key in x:
|
|
torch.testing.assert_close(x[key], y[key])
|
|
assert b.episodes.tolist() == [1, 0, 1, 0]
|
|
assert a.sample(torch.tensor([], dtype=torch.long))["commands"].shape == (0, 4, 6)
|
|
with pytest.raises(ValueError):
|
|
a.sample(torch.tensor([0, 0]))
|
|
|
|
|
|
def test_curriculum_distribution_budgets_exclusions_and_regression():
|
|
cfg = load_config(TASK)
|
|
for update in (1, 600, 1500):
|
|
sample = EpisodeSampler(cfg, 512, 91101, update=update).sample(torch.arange(512))
|
|
assert_sample_domain(cfg, sample)
|
|
if update == 1:
|
|
assert not sample["commands"].any()
|
|
assert 0.15 < sample["stand"].float().mean() < 0.35
|
|
if update == 600:
|
|
assert not sample["commands"][..., 3:].any()
|
|
if update == 1500:
|
|
assert set(sample["scenario"].tolist()) == {0, 1, 2, 3}
|
|
assert set(sample["stage"].tolist()) == {0, 1, 2}
|
|
assert sample["commands"][..., 3:].abs().max() > 0
|
|
for fault in ("commands", "points", "delays"):
|
|
bad = copy.deepcopy(sample)
|
|
bad[fault][0] = 100
|
|
with pytest.raises(ValueError):
|
|
assert_sample_domain(cfg, bad)
|
|
|
|
|
|
def test_validation_distribution_independent_of_training_update():
|
|
cfg = load_config(TASK)
|
|
a = EpisodeSampler(cfg, 8, 93101, "validation", update=1).sample(torch.arange(8))
|
|
b = EpisodeSampler(cfg, 8, 93101, "validation", update=1500).sample(torch.arange(8))
|
|
for key in a:
|
|
torch.testing.assert_close(a[key], b[key])
|
|
assert not a["stand"].any()
|
|
|
|
|
|
@pytest.mark.parametrize("bad", [torch.zeros(2, 45), torch.zeros(2, 32, dtype=torch.float64),
|
|
torch.full((2, 32), float("nan")), {"privileged": torch.zeros(2, 32)}])
|
|
def test_feature_contract_rejects_other_interfaces(bad):
|
|
with pytest.raises(ValueError):
|
|
features(bad)
|
|
|
|
|
|
class ValueModel:
|
|
def value(self, x):
|
|
return x[:, 0].clone()
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", [torch.int32, torch.int64])
|
|
def test_timeout_bootstraps_terminal_not_reset_and_failure_zero(dtype):
|
|
x = torch.ones(3, 32)
|
|
terminal = torch.zeros(2, 32)
|
|
terminal[:, 0] = torch.tensor([30., 20.])
|
|
terminated, truncated = torch.tensor([True, False, False]), torch.tensor([True, True, True])
|
|
info = {"terminal_ids": torch.tensor([2, 1], dtype=dtype), "terminal_observation": {"policy": terminal}}
|
|
v = bootstrap_values(ValueModel(), x, terminated, truncated, info)
|
|
torch.testing.assert_close(v, torch.tensor([0., 20., 30.]))
|
|
with pytest.raises(RuntimeError):
|
|
bootstrap_values(ValueModel(), x, terminated, truncated, {})
|
|
|
|
|
|
class FakeEnv:
|
|
num_envs, device, step_dt = 2, "cpu", 0.01
|
|
|
|
def step(self, action):
|
|
assert action.shape == (2, 3) and (action.abs() <= 1).all()
|
|
# Both episodes timeout; deliberately distinct reset/terminal states.
|
|
return ({"policy": torch.zeros(2, 32), "privileged": torch.full((2, 71), float("nan"))},
|
|
torch.ones(2)*0.01, torch.zeros(2, dtype=torch.bool), torch.ones(2, dtype=torch.bool),
|
|
{"terminal_ids": torch.tensor([0, 1]), "terminal_observation": {"policy": torch.ones(2, 32)}})
|
|
|
|
|
|
def test_synthetic_ppo_and_atomic_restore(tmp_path):
|
|
torch.set_num_threads(1)
|
|
torch.manual_seed(4)
|
|
model = CycleActorCritic(hidden=16)
|
|
cfg = {**json.loads(TRAINING.read_text())["algorithm"], "horizon": 4, "minibatch_size": 4}
|
|
optimizer = torch.optim.Adam(model.parameters(), lr=cfg["learning_rate"])
|
|
data, x, failures, timeouts = collect_rollout(FakeEnv(), model, torch.zeros(2, 32), cfg)
|
|
assert x.shape == (2, 32) and failures == 0 and timeouts == 8
|
|
assert data["done"].all() and torch.isfinite(data["features"]).all()
|
|
result = ppo_step(model, optimizer, data, cfg)
|
|
assert result["sampling_log_prob_error"] <= 2e-4
|
|
sampler = EpisodeSampler(load_config(TASK), 2, 11)
|
|
sampler.sample(torch.arange(2))
|
|
path = tmp_path/"model.pt"
|
|
save_checkpoint(path, model, optimizer, {"test": "synthetic"}, 1, sampler, None, {})
|
|
expected = model.act(x, deterministic=True)[0].detach().clone()
|
|
with torch.no_grad():
|
|
model.actor[-1].bias.add_(1)
|
|
restore_checkpoint(path, model, optimizer, {"test": "synthetic"}, sampler)
|
|
torch.testing.assert_close(model.act(x, deterministic=True)[0], expected)
|
|
with pytest.raises(ValueError, match="mismatch"):
|
|
restore_checkpoint(path, model, optimizer, {"test": "different"}, sampler)
|
|
with pytest.raises(RuntimeError, match="budget"):
|
|
save_checkpoint(tmp_path/"too_big.pt", model, optimizer, {}, 1, sampler, None, {}, max_bytes=1)
|
|
assert not (tmp_path/"too_big.pt").exists()
|
|
|
|
|
|
def test_selection_failure_cannot_profit_from_missing_metrics():
|
|
good = {"success": True, "swing_rms_m": 0.025, "stance_load_mae_weight_fraction": 0.25, "stance_slip_m": 0.03}
|
|
assert selection_score([good]) == 3
|
|
assert selection_score([{"success": False}]) == 16
|
|
assert selection_score([good, {"success": False}]) == 9.5
|
|
with pytest.raises(ValueError):
|
|
selection_score([])
|
|
with pytest.raises(ValueError):
|
|
selection_score([{**good, "swing_rms_m": float("nan")}])
|
|
|
|
|
|
def test_configuration_and_explicit_authorization(tmp_path):
|
|
cfg = load_training_config(TRAINING, ROOT)
|
|
manifest = {"training": cfg}
|
|
with pytest.raises(PermissionError):
|
|
require_authorization(None, manifest)
|
|
grant = {"format": "skillfactory.contact-cycle-authorization.v1", "approved": True,
|
|
"manifest_sha256": digest(manifest), "operator": "synthetic_test_only",
|
|
"authorization_reference": "unit-test fixture, not robot training approval"}
|
|
path = tmp_path/"grant.json"
|
|
path.write_text(json.dumps(grant))
|
|
require_authorization(path, manifest)
|
|
with pytest.raises(PermissionError):
|
|
require_authorization(path, {"training": {}})
|
|
bad = copy.deepcopy(cfg)
|
|
bad["horizon"] = True
|
|
path.write_text(json.dumps(bad))
|
|
with pytest.raises(ValueError):
|
|
load_training_config(path, ROOT)
|
|
|
|
|
|
def small_manifest():
|
|
return {"training": {"seeds": [1, 2, 3], "num_envs": 2, "horizon": 4, "updates_per_seed": 2,
|
|
"max_total_transitions": 16, "max_total_wall_s": 100, "max_artifact_bytes": 100000}}
|
|
|
|
|
|
def test_global_budget_crash_charge_resume_and_lock(tmp_path):
|
|
now = [100.]
|
|
manifest = small_manifest()
|
|
with BudgetLedger(tmp_path, manifest, lambda: now[0], boot_now=lambda: now[0], boot_id="test-boot") as ledger:
|
|
ledger.reserve_rollout(1, 1)
|
|
with pytest.raises(ValueError):
|
|
ledger.reserve_rollout(1, 1)
|
|
with pytest.raises(BlockingIOError), BudgetLedger(
|
|
tmp_path, manifest, lambda: now[0], boot_now=lambda: now[0], boot_id="test-boot"):
|
|
pass
|
|
with BudgetLedger(tmp_path, manifest, lambda: now[0], boot_now=lambda: now[0], boot_id="test-boot") as resumed:
|
|
resumed.reconcile_checkpoint(1, 0) # interrupted rollout lost, no refund
|
|
resumed.reserve_rollout(1, 1)
|
|
resumed.commit_update(1, 1)
|
|
assert resumed.data["reserved_transitions"] == 16
|
|
with pytest.raises(BudgetExceeded, match="transition"):
|
|
resumed.reserve_rollout(2, 1)
|
|
now[0] = 201
|
|
with pytest.raises(BudgetExceeded, match="wall"):
|
|
resumed.guard()
|
|
|
|
|
|
def test_validation_rejects_missing_or_partial_disturbance_delivery():
|
|
from SkillFactory.legs.contact_cycle.mechanics import delivered_events
|
|
|
|
events = load_config(TASK)["events"]
|
|
scenario = torch.tensor([0, 1])
|
|
mask = scenario[:, None] == torch.tensor([e["scenario"] for e in events])
|
|
zero = torch.zeros_like(mask, dtype=torch.float32)
|
|
assert delivered_events(events, scenario, mask, mask, zero).tolist() == [True, False]
|
|
areas = torch.tensor([e["duration_s"] - e["ramp_s"] for e in events])
|
|
assert delivered_events(events, scenario, mask, mask, mask*areas).all()
|
|
assert delivered_events(events, scenario, mask, torch.zeros_like(mask), mask*areas).tolist() == [True, False]
|
|
|
|
|
|
def test_engine_int32_reset_ids_and_invalid_float():
|
|
from SkillFactory.legs.contact_cycle.mechanics import canonical_ids
|
|
|
|
ids = canonical_ids(torch.tensor([0, 2], dtype=torch.int32), 4)
|
|
assert ids.dtype == torch.int64 and ids.tolist() == [0, 2]
|
|
sampler = EpisodeSampler(load_config(TASK), 4, 1)
|
|
sampler.sample(torch.tensor([0, 2], dtype=torch.int32))
|
|
assert sampler.episodes.tolist() == [1, 0, 1, 0]
|
|
with pytest.raises(ValueError):
|
|
canonical_ids(torch.tensor([0., 2.]), 4)
|
|
|
|
|
|
def test_trace_storage_is_bounded_and_budget_failure_leaves_no_file(tmp_path):
|
|
trace = {"foot": torch.arange(100*128*3, dtype=torch.float32).reshape(100, 128, 3)}
|
|
sizes = []
|
|
path = tmp_path/"subset.pt"
|
|
size = save_trace_subset(path, trace, [1], [2]*128, sizes.append)
|
|
assert size == path.stat().st_size == sizes[0] and size < 8192
|
|
saved = torch.load(path, weights_only=True)["1"]["foot"]
|
|
assert saved.untyped_storage().nbytes() == saved.numel()*saved.element_size() == 24
|
|
torch.testing.assert_close(saved, trace["foot"][:2, 1:2])
|
|
|
|
def refuse(size):
|
|
raise BudgetExceeded(f"synthetic capacity less than {size}")
|
|
|
|
with pytest.raises(BudgetExceeded):
|
|
save_trace_subset(tmp_path/"insufficient.pt", trace, [1], [2]*128, refuse)
|
|
assert not (tmp_path/"insufficient.pt").exists()
|
|
assert list(tmp_path.iterdir()) == [path]
|
|
|
|
|
|
def test_checkpoint_commit_crash_window_and_disk_guard(tmp_path):
|
|
with BudgetLedger(tmp_path, small_manifest(), lambda: 100.) as ledger:
|
|
ledger.reserve_rollout(1, 1)
|
|
ledger.reconcile_checkpoint(1, 1) # optimizer/checkpoint completed before commit
|
|
assert ledger.data["updates"]["1"] == 1 and ledger.data["pending"] is None
|
|
with pytest.raises(ValueError):
|
|
ledger.commit_update(1, 2)
|
|
with pytest.raises(BudgetExceeded, match="artifact"):
|
|
ledger.guard(reserve_bytes=100001)
|