release: SkillFactory v0.0.1
Add declarative single-leg import, fixed-anchor benchmark, masked shared-policy PPO prototype, regression tests, and consolidated documentation.
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
"""Contract tests; opt into fresh Kit integration runs with ANCHOR_RUNTIME_TESTS=1."""
|
||||
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import runpy
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).parents[1]
|
||||
|
||||
|
||||
def test_metadata_schema(tmp_path):
|
||||
spec = importlib.util.spec_from_file_location("extract_schema", ROOT / "scripts/assets/extract_go2_leg.py")
|
||||
extractor = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(extractor)
|
||||
manifest = extractor.extract(extractor.DEFAULT_SOURCE, tmp_path / "asset")
|
||||
assert manifest["schema_version"] == 1
|
||||
assert manifest["interface"]["policy_shape"] == ["B", 19]
|
||||
assert manifest["interface"]["action_shape"] == ["B", 3]
|
||||
assert manifest["interface"]["quaternion_order"] == "xyzw"
|
||||
assert manifest["control"]["physics_dt"] == 0.002
|
||||
assert manifest["control"]["decimation"] == 5
|
||||
assert [j["limits"]["effort"] for j in manifest["joints"]] == [23.7, 23.7, 45.43]
|
||||
assert [j["limits"]["velocity"] for j in manifest["joints"]] == [30.1, 30.1, 15.7]
|
||||
assert all(len(v["origin"]["xyz"]) == 3 for v in manifest["links"].values() if v is not None)
|
||||
|
||||
|
||||
def test_task_registration_preserves_template():
|
||||
import gymnasium as gym
|
||||
import SkillFactory.tasks # noqa: F401
|
||||
|
||||
ids = ["Template-Skillfactory-v0", "SkillFactory-SingleLegAnchor-Go2-v0"]
|
||||
for task in ids:
|
||||
spec = gym.spec(task)
|
||||
assert spec.entry_point == "isaaclab.envs:ManagerBasedRLEnv"
|
||||
assert spec.kwargs["env_cfg_entry_point"].startswith("SkillFactory.")
|
||||
generic = gym.spec("SkillFactory-LegSkill-v0")
|
||||
assert generic.entry_point == "SkillFactory.tasks.manager_based.leg_skill.leg_env:LegSkillEnv"
|
||||
|
||||
|
||||
def test_packaging_discovers_task_subpackages(monkeypatch):
|
||||
import setuptools
|
||||
|
||||
captured = {}
|
||||
monkeypatch.setattr(setuptools, "setup", lambda **kwargs: captured.update(kwargs))
|
||||
monkeypatch.chdir(ROOT / "source/SkillFactory")
|
||||
runpy.run_path(str(ROOT / "source/SkillFactory/setup.py"))
|
||||
prefix = "SkillFactory.tasks.manager_based.single_leg_anchor"
|
||||
assert prefix in captured["packages"]
|
||||
assert prefix + ".mdp" in captured["packages"]
|
||||
assert "SkillFactory.assets" in captured["packages"]
|
||||
assert "SkillFactory.legs" in captured["packages"]
|
||||
assert "SkillFactory.tasks.manager_based.leg_skill" in captured["packages"]
|
||||
assert captured["package_data"]["SkillFactory.legs"] == ["resources/*.usda"]
|
||||
assert (ROOT / "source/SkillFactory/SkillFactory/legs/resources/ground.usda").is_file()
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.environ.get("ANCHOR_RUNTIME_TESTS") != "1", reason="Opt-in Kit/CUDA integration")
|
||||
@pytest.mark.parametrize("num_envs", [1, 16, 64])
|
||||
def test_cuda_contract(num_envs, tmp_path):
|
||||
env = dict(os.environ)
|
||||
env["PYTHONPATH"] = str(ROOT / "source/SkillFactory") + os.pathsep + env.get("PYTHONPATH", "")
|
||||
with (tmp_path / "runtime.log").open("w") as log:
|
||||
result = subprocess.run(
|
||||
[
|
||||
sys.executable,
|
||||
str(ROOT / "scripts/verify_anchor.py"),
|
||||
"--suite",
|
||||
"contract",
|
||||
"--num_envs",
|
||||
str(num_envs),
|
||||
"--output",
|
||||
str(tmp_path / "report"),
|
||||
"--headless",
|
||||
],
|
||||
cwd=ROOT,
|
||||
env=env,
|
||||
stdout=log,
|
||||
stderr=subprocess.STDOUT,
|
||||
)
|
||||
assert result.returncode == 0, (tmp_path / "runtime.log").read_text()[-4000:]
|
||||
report = json.loads((tmp_path / "report/result.json").read_text())
|
||||
assert report["passed"] and report["contract"]["policy_shape"] == [num_envs, 19]
|
||||
@@ -0,0 +1,71 @@
|
||||
"""Pure XML tests: no Kit, Isaac Lab, network or GPU required."""
|
||||
|
||||
import importlib.util
|
||||
import shutil
|
||||
import xml.etree.ElementTree as ET
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
SPEC = importlib.util.spec_from_file_location(
|
||||
"extract_leg", Path(__file__).parents[1] / "scripts/assets/extract_go2_leg.py"
|
||||
)
|
||||
extractor = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(extractor)
|
||||
|
||||
|
||||
def test_repeatable_and_real_subtree(tmp_path):
|
||||
a, b = tmp_path / "a", tmp_path / "b"
|
||||
manifest = extractor.extract(extractor.DEFAULT_SOURCE, a)
|
||||
extractor.extract(extractor.DEFAULT_SOURCE, b)
|
||||
assert (a / "go2_fl.urdf").read_bytes() == (b / "go2_fl.urdf").read_bytes()
|
||||
assert (a / "manifest.json").read_bytes() == (b / "manifest.json").read_bytes()
|
||||
robot = ET.parse(a / "go2_fl.urdf").getroot()
|
||||
assert manifest["joint_order"] == list(extractor.JOINTS)
|
||||
assert len([j for j in robot.findall("joint") if j.get("type") == "revolute"]) == 3
|
||||
assert {link.get("name") for link in robot.findall("link")} == {
|
||||
"anchor_base",
|
||||
"FL_hip",
|
||||
"FL_thigh",
|
||||
"FL_calf",
|
||||
"FL_foot",
|
||||
"FL_hip_rotor",
|
||||
"FL_thigh_rotor",
|
||||
"FL_calf_rotor",
|
||||
"FL_calflower",
|
||||
"FL_calflower1",
|
||||
}
|
||||
assert robot.find("joint[@name='FL_foot_joint']").get("dont_collapse") == "true"
|
||||
assert robot.find("joint[@name='FL_hip_joint']/origin").get("xyz") == "0 0 0"
|
||||
assert robot.find("link[@name='anchor_base']/collision") is None
|
||||
for mesh in robot.findall(".//mesh"):
|
||||
assert (a / mesh.get("filename")).is_file()
|
||||
assert manifest["skill"]["valid"] is False
|
||||
assert (a / "LICENSE").is_file()
|
||||
assert manifest["missing_inertial_fixed_links"] == ["FL_calflower", "FL_calflower1"]
|
||||
center = extractor.foot_center(robot)
|
||||
assert abs(center[2] + manifest["anchor"]["default_height"] - 0.022 - 0.01) < 1e-12
|
||||
original = ET.parse(extractor.DEFAULT_SOURCE / "urdf/go2_description.urdf").getroot()
|
||||
old = extractor.foot_center(original)
|
||||
assert all(
|
||||
abs(new + shift - old_value) < 1e-10
|
||||
for new, shift, old_value in zip(center, manifest["anchor"]["original_mount_translation"], old)
|
||||
)
|
||||
|
||||
|
||||
def test_missing_reference_fails(tmp_path):
|
||||
source = tmp_path / "unitree/robots/go2_description"
|
||||
shutil.copytree(extractor.DEFAULT_SOURCE, source)
|
||||
(source / "dae/hip.dae").unlink()
|
||||
with pytest.raises(FileNotFoundError):
|
||||
extractor.extract(source, tmp_path / "out")
|
||||
|
||||
|
||||
def test_invalid_inertia_fails(tmp_path):
|
||||
source = tmp_path / "unitree/robots/go2_description"
|
||||
shutil.copytree(extractor.DEFAULT_SOURCE, source)
|
||||
tree = ET.parse(source / "urdf/go2_description.urdf")
|
||||
tree.find("link[@name='FL_thigh']/inertial/inertia").set("ixy", "100")
|
||||
tree.write(source / "urdf/go2_description.urdf")
|
||||
with pytest.raises(ValueError, match="Invalid inertia"):
|
||||
extractor.extract(source, tmp_path / "out")
|
||||
@@ -0,0 +1,214 @@
|
||||
"""Declarative extraction tests, including non-Go2 names and arbitrary axes."""
|
||||
|
||||
import dataclasses
|
||||
import json
|
||||
import math
|
||||
import xml.etree.ElementTree as ET
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from SkillFactory.legs import JointSpec, LegSpec
|
||||
from SkillFactory.legs.spec import load_bundle
|
||||
from SkillFactory.legs.urdf import extract_leg, forward, multiply, pose
|
||||
|
||||
ROOT = Path(__file__).parents[1]
|
||||
|
||||
|
||||
def fixture_spec(tmp_path, n=2, box=False):
|
||||
robot = ET.Element("robot", name="fixture")
|
||||
for name in ["mount", *(f"segment{i}" for i in range(n)), "toe"]:
|
||||
link = ET.SubElement(robot, "link", name=name)
|
||||
link.append(
|
||||
ET.fromstring('<inertial><mass value="0.2"/><inertia ixx="0.001" iyy="0.001" izz="0.001"/></inertial>')
|
||||
)
|
||||
if name == "toe":
|
||||
link.append(
|
||||
ET.fromstring(
|
||||
'<collision><origin xyz="0.01 0 0" rpy="0 0.2 0"/><geometry>'
|
||||
+ ('<box size="0.03 0.04 0.05"/>' if box else '<sphere radius="0.02"/>')
|
||||
+ "</geometry></collision>"
|
||||
)
|
||||
)
|
||||
for index in range(n):
|
||||
joint = ET.SubElement(robot, "joint", name=f"pivot{index}", type="revolute")
|
||||
ET.SubElement(joint, "parent", link="mount" if index == 0 else f"segment{index - 1}")
|
||||
ET.SubElement(joint, "child", link=f"segment{index}")
|
||||
ET.SubElement(
|
||||
joint,
|
||||
"origin",
|
||||
xyz="0.2 0.1 -0.3" if index == 0 else "0.1 0 -0.15",
|
||||
rpy="0.1 0.2 0.3" if index == 0 else "0 0 0",
|
||||
)
|
||||
ET.SubElement(joint, "axis", xyz="0.2 0.7 -0.3")
|
||||
ET.SubElement(joint, "limit", lower="-1", upper="1", effort="10", velocity="5")
|
||||
joint = ET.SubElement(robot, "joint", name="toe_mount", type="fixed")
|
||||
ET.SubElement(joint, "parent", link=f"segment{n - 1}")
|
||||
ET.SubElement(joint, "child", link="toe")
|
||||
ET.SubElement(joint, "origin", xyz="0 0 -0.1", rpy="0 0 0")
|
||||
ET.ElementTree(robot).write(tmp_path / "robot.urdf")
|
||||
(tmp_path / "LICENSE").write_text("Fixture license\n")
|
||||
return LegSpec(
|
||||
1,
|
||||
"fixture",
|
||||
tmp_path,
|
||||
"robot.urdf",
|
||||
"fixture",
|
||||
"LICENSE",
|
||||
"test",
|
||||
"pivot0",
|
||||
(),
|
||||
tuple(JointSpec(f"pivot{i}", 0.0, 0.1, 20.0, 0.5) for i in range(n)),
|
||||
"toe",
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("preset", ["go2_fl", "go1_fl"])
|
||||
def test_real_legs_deterministic_and_complete(tmp_path, preset):
|
||||
spec = LegSpec.load(ROOT / f"configs/legs/{preset}.json")
|
||||
first = extract_leg(spec, tmp_path / "one")
|
||||
second = extract_leg(spec, tmp_path / "two")
|
||||
assert first == second
|
||||
assert (tmp_path / "one/leg.urdf").read_bytes() == (tmp_path / "two/leg.urdf").read_bytes()
|
||||
assert first["joint_order"] == [item.name for item in spec.joints]
|
||||
assert spec.foot_body in first["links"] and "FL_hip_rotor" in first["links"]
|
||||
assert all(name.startswith("FL_") for name in first["links"])
|
||||
assert first["skill"]["trained"] is False
|
||||
assert load_bundle(tmp_path / "one")["source"]["sha256"] == first["source"]["sha256"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("n", [1, 2, 4])
|
||||
def test_arbitrary_names_axes_and_mount_frame(tmp_path, n):
|
||||
spec = fixture_spec(tmp_path, n)
|
||||
bundle = extract_leg(spec, tmp_path / "out")
|
||||
q = {joint.name: 0.3 for joint in spec.joints}
|
||||
links, _ = forward(bundle["tree"], q)
|
||||
expected = pose([0.2, 0.1, -0.3], [0.1, 0.2, 0.3])
|
||||
original = ET.parse(tmp_path / "robot.urdf").getroot()
|
||||
from SkillFactory.legs.urdf import origin, rotation, vector
|
||||
|
||||
for joint in original.findall("joint"):
|
||||
expected = multiply(expected, pose(**origin(joint))) if joint.get("name") != "pivot0" else expected
|
||||
if joint.get("type") == "revolute":
|
||||
axis = vector(joint.find("axis").get("xyz"))
|
||||
norm = math.sqrt(sum(v * v for v in axis))
|
||||
expected = multiply(expected, rotation([v / norm for v in axis], 0.3))
|
||||
actual = multiply(pose([0.2, 0.1, -0.3], [0.1, 0.2, 0.3]), links["toe"])
|
||||
assert max(abs(a - b) for row_a, row_b in zip(actual, expected) for a, b in zip(row_a, row_b)) < 1e-10
|
||||
assert len(bundle["joints"]) == n
|
||||
|
||||
|
||||
def test_box_foot_and_declared_actuator_order(tmp_path):
|
||||
spec = fixture_spec(tmp_path, 2, box=True)
|
||||
spec = dataclasses.replace(spec, joints=tuple(reversed(spec.joints)))
|
||||
bundle = extract_leg(spec, tmp_path / "out")
|
||||
assert bundle["foot"]["type"] == "box"
|
||||
assert bundle["joint_order"] == ["pivot1", "pivot0"]
|
||||
assert bundle["tree"][0]["name"] == "pivot0"
|
||||
|
||||
|
||||
def test_invalid_uncontrolled_and_stale_inputs(tmp_path):
|
||||
spec = fixture_spec(tmp_path)
|
||||
with pytest.raises(ValueError, match="moving joints"):
|
||||
extract_leg(dataclasses.replace(spec, joints=spec.joints[:1]), tmp_path / "bad")
|
||||
with pytest.raises(ValueError):
|
||||
JointSpec("a", 0.0, 0.0, 20.0, 0.5)
|
||||
with pytest.raises(ValueError):
|
||||
dataclasses.replace(spec, decimation=0)
|
||||
extract_leg(spec, tmp_path / "out")
|
||||
with (tmp_path / "out/leg.urdf").open("a") as stream:
|
||||
stream.write("<!-- tampered -->")
|
||||
with pytest.raises(ValueError, match="hash mismatch"):
|
||||
load_bundle(tmp_path / "out")
|
||||
|
||||
|
||||
def test_missing_license_and_unsafe_resource(tmp_path):
|
||||
spec = fixture_spec(tmp_path)
|
||||
with pytest.raises(FileNotFoundError):
|
||||
extract_leg(dataclasses.replace(spec, license_file="missing"), tmp_path / "bad")
|
||||
robot = ET.parse(tmp_path / "robot.urdf")
|
||||
visual = ET.SubElement(robot.getroot().find("link[@name='toe']"), "visual")
|
||||
ET.SubElement(ET.SubElement(visual, "geometry"), "mesh", filename="../../outside.stl")
|
||||
robot.write(tmp_path / "robot.urdf")
|
||||
with pytest.raises(ValueError, match="escapes"):
|
||||
extract_leg(spec, tmp_path / "bad")
|
||||
|
||||
|
||||
def test_output_cannot_overwrite_sources_or_escape_through_symlink(tmp_path):
|
||||
spec = fixture_spec(tmp_path)
|
||||
before = (tmp_path / "robot.urdf").read_bytes()
|
||||
with pytest.raises(ValueError, match="source root"):
|
||||
extract_leg(spec, tmp_path)
|
||||
assert (tmp_path / "robot.urdf").read_bytes() == before
|
||||
output = tmp_path / "out"
|
||||
output.mkdir()
|
||||
(output / "leg.urdf").symlink_to(tmp_path / "robot.urdf")
|
||||
with pytest.raises(ValueError, match="symlink"):
|
||||
extract_leg(spec, output)
|
||||
assert (tmp_path / "robot.urdf").read_bytes() == before
|
||||
|
||||
|
||||
def test_nested_output_does_not_overwrite_an_existing_source_leg(tmp_path):
|
||||
source = tmp_path / "nested"
|
||||
source.mkdir()
|
||||
spec = fixture_spec(source)
|
||||
(source / "robot.urdf").rename(source / "leg.urdf")
|
||||
spec = dataclasses.replace(spec, source_root=tmp_path, urdf="nested/leg.urdf", license_file="nested/LICENSE")
|
||||
before = (source / "leg.urdf").read_bytes()
|
||||
with pytest.raises(ValueError, match="overwrite source file"):
|
||||
extract_leg(spec, source)
|
||||
assert (source / "leg.urdf").read_bytes() == before
|
||||
|
||||
|
||||
@pytest.mark.parametrize("change", ["missing_parent", "missing_limit", "missing_geometry"])
|
||||
def test_malformed_urdf_fails_with_actionable_error(tmp_path, change):
|
||||
spec = fixture_spec(tmp_path)
|
||||
tree = ET.parse(tmp_path / "robot.urdf")
|
||||
root = tree.getroot()
|
||||
if change == "missing_geometry":
|
||||
item = root.find("link[@name='toe']/collision")
|
||||
item.remove(item.find("geometry"))
|
||||
else:
|
||||
item = root.find("joint[@name='pivot0']")
|
||||
item.remove(item.find("parent" if change == "missing_parent" else "limit"))
|
||||
tree.write(tmp_path / "robot.urdf")
|
||||
with pytest.raises(ValueError, match="Missing"):
|
||||
extract_leg(spec, tmp_path / "out")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("invalid", ["scale", "mapping", "layer", "support"])
|
||||
def test_stale_or_invalid_conversion_rejected_before_kit(tmp_path, invalid):
|
||||
from SkillFactory.legs.urdf import digest
|
||||
|
||||
bundle = extract_leg(fixture_spec(tmp_path), tmp_path / "out")
|
||||
directory = tmp_path / "out"
|
||||
(directory / "leg.usda").write_text("#usda 1.0\\n")
|
||||
conversion = {
|
||||
"usd_path": "leg.usda",
|
||||
"manifest_sha256": digest(directory / "manifest.json"),
|
||||
"used_layers": ["leg.usda"],
|
||||
"fixed_to_world": True,
|
||||
"body_paths": {name: "/Geometry/" + name for name in (*bundle["links"], bundle["anchor_body"])},
|
||||
"joint_paths": {name: "/Joints/" + name for name in bundle["joint_order"]},
|
||||
}
|
||||
if invalid == "scale":
|
||||
bundle["length_scale_m"] = 0
|
||||
(directory / "manifest.json").write_text(json.dumps(bundle))
|
||||
conversion["manifest_sha256"] = digest(directory / "manifest.json")
|
||||
elif invalid == "mapping":
|
||||
conversion["body_paths"]["toe"] = "/Geometry/" + bundle["anchor_body"]
|
||||
elif invalid == "layer":
|
||||
conversion["used_layers"] = []
|
||||
else:
|
||||
conversion["fixed_to_world"] = False
|
||||
(directory / "conversion.json").write_text(json.dumps(conversion))
|
||||
with pytest.raises(ValueError):
|
||||
load_bundle(directory, require_usd=True)
|
||||
|
||||
|
||||
def test_unknown_spec_fields_rejected(tmp_path):
|
||||
data = LegSpec.load(ROOT / "configs/legs/go2_fl.json").to_dict()
|
||||
data["typo"] = True
|
||||
path = tmp_path / "spec.json"
|
||||
path.write_text(json.dumps(data))
|
||||
with pytest.raises(TypeError):
|
||||
LegSpec.load(path)
|
||||
@@ -0,0 +1,183 @@
|
||||
"""Masking, variable DoF, equivalent token permutations, FK, and PPO semantics."""
|
||||
|
||||
import copy
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
from SkillFactory.legs.policy import LegActorCritic, compute_gae, load_policy, ppo_update, save_policy
|
||||
from SkillFactory.legs.tokens import FEATURE_SCHEMA, LegKinematics, pad_joint_observations, static_tokens
|
||||
from SkillFactory.legs.urdf import extract_leg, forward
|
||||
from test_leg_assets import fixture_spec
|
||||
|
||||
|
||||
def batch(b=3, j=6, e=1):
|
||||
result = {
|
||||
name: torch.randn(b, j if name.startswith("joint") else e, len(FEATURE_SCHEMA[name]))
|
||||
for name in ("joint_description", "joint_observation", "effector_description", "effector_observation")
|
||||
}
|
||||
result.update(
|
||||
joint_mask=torch.tensor([[True, True, False, False, False, False]]).expand(b, -1).clone(),
|
||||
effector_mask=torch.ones(b, e, dtype=torch.bool),
|
||||
context=torch.randn(b, len(FEATURE_SCHEMA["context"])),
|
||||
goal=torch.randn(b, 3),
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def test_padding_and_missing_feet_do_not_affect_active_policy():
|
||||
torch.manual_seed(3)
|
||||
model = LegActorCritic()
|
||||
original = batch()
|
||||
action, logp, value = model.act(original, deterministic=True)
|
||||
changed = copy.deepcopy(original)
|
||||
changed["joint_description"][:, 2:] = torch.nan
|
||||
changed["joint_observation"][:, 2:] = torch.nan
|
||||
other, other_log, other_value = model.act(changed, deterministic=True)
|
||||
assert torch.equal(action, other) and torch.equal(logp, other_log) and torch.equal(value, other_value)
|
||||
assert (action[:, 2:] == 0).all()
|
||||
original["effector_mask"][:] = False
|
||||
original["effector_description"][:] = torch.nan
|
||||
original["effector_observation"][:] = torch.nan
|
||||
assert all(torch.isfinite(t).all() for t in model.act(original))
|
||||
|
||||
|
||||
def test_permutation_equivariance_and_no_inactive_gradients():
|
||||
torch.manual_seed(42)
|
||||
model = LegActorCritic()
|
||||
original = batch()
|
||||
original["joint_description"].requires_grad_()
|
||||
original["joint_observation"].requires_grad_()
|
||||
permutation = torch.tensor([3, 1, 5, 0, 4, 2])
|
||||
shuffled = {k: v[:, permutation] if k.startswith("joint") else v for k, v in original.items()}
|
||||
a, _, v = model.act(original, deterministic=True)
|
||||
b, _, w = model.act(shuffled, deterministic=True)
|
||||
torch.testing.assert_close(b, a[:, permutation], atol=1e-6, rtol=1e-6)
|
||||
torch.testing.assert_close(v, w, atol=1e-6, rtol=1e-6)
|
||||
(a.sum() + v.sum()).backward()
|
||||
assert original["joint_description"].grad[:, 2:].abs().max() == 0
|
||||
assert original["joint_observation"].grad[:, 2:].abs().max() == 0
|
||||
assert original["joint_observation"].grad[:, :2].abs().sum() > 0
|
||||
distribution, _ = model.distribution_value(original)
|
||||
_, entropy, _ = model.evaluate(original, a)
|
||||
torch.testing.assert_close(entropy, distribution.entropy()[:, :2].sum(dim=-1))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("n", [1, 2, 4])
|
||||
def test_actual_fk_and_shared_network_across_declared_dof(tmp_path, n):
|
||||
spec = fixture_spec(tmp_path, n)
|
||||
bundle = extract_leg(spec, tmp_path / "asset")
|
||||
static = static_tokens(bundle, 6)
|
||||
q = torch.linspace(-0.2, 0.3, n, dtype=torch.float32)[None, :].expand(3, -1)
|
||||
fk = LegKinematics(bundle)
|
||||
actual = fk.foot_position(q)[0]
|
||||
expected, _ = forward(bundle["tree"], dict(zip(bundle["joint_order"], q[0].tolist())))
|
||||
torch.testing.assert_close(actual, torch.tensor([expected["toe"][i][3] for i in range(3)]), atol=1e-6, rtol=1e-6)
|
||||
tokens = {k: v.expand(3, *v.shape[1:]) for k, v in static.items()}
|
||||
tokens.update(
|
||||
joint_observation=pad_joint_observations(q, q * 0, q * 0, bundle, 6),
|
||||
effector_observation=torch.zeros(3, 1, 11),
|
||||
context=torch.zeros(3, len(FEATURE_SCHEMA["context"])),
|
||||
goal=torch.zeros(3, 3),
|
||||
)
|
||||
model = LegActorCritic()
|
||||
action, _, _ = model.act(tokens)
|
||||
assert action.shape == (3, 6) and (action[:, n:] == 0).all()
|
||||
assert static["joint_mask"].sum() == n
|
||||
with pytest.raises(ValueError):
|
||||
static_tokens(bundle, n - 1)
|
||||
|
||||
|
||||
def test_inertia_and_com_are_in_anchor_nominal_frame(tmp_path):
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
spec = fixture_spec(tmp_path, 1)
|
||||
tree = ET.parse(tmp_path / "robot.urdf")
|
||||
item = tree.getroot().find("link[@name='segment0']/inertial")
|
||||
ET.SubElement(item, "origin", xyz="0.01 0 0", rpy="0 0 1.5707963267948966")
|
||||
item.find("inertia").set("iyy", "0.002")
|
||||
item.find("inertia").set("izz", "0.003")
|
||||
tree.write(tmp_path / "robot.urdf")
|
||||
bundle = extract_leg(spec, tmp_path / "asset")
|
||||
description = static_tokens(bundle, 3)["joint_description"][0, 0]
|
||||
scale = bundle["known_mass_scale_kg"] * bundle["length_scale_m"] ** 2
|
||||
torch.testing.assert_close(
|
||||
description[[15, 18, 20]] * scale, torch.tensor([0.002, 0.001, 0.003]), atol=1e-7, rtol=1e-6
|
||||
)
|
||||
torch.testing.assert_close(
|
||||
description[21:24] * bundle["length_scale_m"], torch.tensor([0.01, 0.0, 0.0]), atol=1e-7, rtol=1e-6
|
||||
)
|
||||
|
||||
|
||||
def test_checkpoint_reload_is_exact(tmp_path):
|
||||
model = LegActorCritic().eval()
|
||||
observation = batch()
|
||||
save_policy(model, tmp_path / "policy.pt", {"test": True})
|
||||
loaded = load_policy(tmp_path / "policy.pt").eval()
|
||||
assert all(torch.equal(a, b) for a, b in zip(model.act(observation, True), loaded.act(observation, True)))
|
||||
|
||||
|
||||
def test_gae_bootstraps_timeout_not_reset_state():
|
||||
rewards = torch.tensor([[1.0, 1.0], [1.0, 1.0]])
|
||||
values = torch.zeros_like(rewards)
|
||||
# terminal first env bootstrap=0, timeout second env terminal critic=10
|
||||
next_values = torch.tensor([[0.0, 0.0], [0.0, 10.0]])
|
||||
done = torch.tensor([[False, False], [True, True]])
|
||||
advantage, returns = compute_gae(rewards, values, next_values, done, gamma=0.9, lam=1.0)
|
||||
torch.testing.assert_close(returns, torch.tensor([[1.9, 10.0], [1.0, 10.0]]))
|
||||
assert torch.equal(returns, advantage)
|
||||
|
||||
|
||||
def test_ppo_mixed_masks_updates_actor_without_rollout_gradients():
|
||||
torch.manual_seed(1)
|
||||
model = LegActorCritic(hidden=16, latent=8)
|
||||
observation = batch(b=4)
|
||||
observation["joint_mask"][2:, 2] = True
|
||||
action, logp, value = model.act(observation)
|
||||
data = {
|
||||
"observation": {key: item[None].repeat(3, *([1] * item.ndim)) for key, item in observation.items()},
|
||||
"actions": action[None].expand(3, -1, -1),
|
||||
"log_prob": logp[None].expand(3, -1),
|
||||
"values": value[None].expand(3, -1),
|
||||
"rewards": torch.randn(3, 4),
|
||||
"next_values": value[None].expand(3, -1),
|
||||
"done": torch.zeros(3, 4, dtype=torch.bool),
|
||||
}
|
||||
before = model.mean[-1].weight.detach().clone()
|
||||
stats = ppo_update(model, torch.optim.Adam(model.parameters(), lr=1e-3), data, epochs=2, minibatch=5)
|
||||
assert not torch.equal(before, model.mean[-1].weight)
|
||||
assert all(torch.isfinite(torch.tensor(v)) for v in stats.values())
|
||||
advantage, _ = compute_gae(data["rewards"], data["values"], data["next_values"], data["done"])
|
||||
assert not advantage.requires_grad
|
||||
with pytest.raises(ValueError, match="positive"):
|
||||
ppo_update(model, torch.optim.Adam(model.parameters()), data, epochs=0)
|
||||
data["log_prob"] = torch.full((3, 4), torch.nan)
|
||||
frozen = {key: item.clone() for key, item in model.state_dict().items()}
|
||||
with pytest.raises(ValueError, match="finite"):
|
||||
ppo_update(model, torch.optim.Adam(model.parameters()), data)
|
||||
assert all(torch.equal(model.state_dict()[key], item) for key, item in frozen.items())
|
||||
|
||||
|
||||
@pytest.mark.parametrize("empty", [True, False])
|
||||
def test_gae_rejects_empty_or_nonfinite_inputs(empty):
|
||||
reward = torch.zeros(0 if empty else 1, 2)
|
||||
if not empty:
|
||||
reward[0, 0] = torch.nan
|
||||
with pytest.raises(ValueError):
|
||||
compute_gae(reward, reward, reward, torch.zeros_like(reward, dtype=torch.bool))
|
||||
|
||||
|
||||
def test_float32_contract_ignores_global_default_dtype(tmp_path):
|
||||
spec = fixture_spec(tmp_path, 2)
|
||||
bundle = extract_leg(spec, tmp_path / "asset")
|
||||
old = torch.get_default_dtype()
|
||||
try:
|
||||
torch.set_default_dtype(torch.float64)
|
||||
static = static_tokens(bundle, 4)
|
||||
q = torch.zeros(2, 2, dtype=torch.float32)
|
||||
assert static["joint_description"].dtype == torch.float32
|
||||
assert static["effector_description"].dtype == torch.float32
|
||||
assert LegKinematics(bundle).foot_position(q).dtype == torch.float32
|
||||
assert pad_joint_observations(q, q, q, bundle, 4).dtype == torch.float32
|
||||
assert all(p.dtype == torch.float32 for p in LegActorCritic().parameters())
|
||||
finally:
|
||||
torch.set_default_dtype(old)
|
||||
@@ -0,0 +1,55 @@
|
||||
"""Reporting must never turn validation/shutdown failures into success."""
|
||||
|
||||
import importlib.util
|
||||
import json
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SPEC = importlib.util.spec_from_file_location("runtime_common", ROOT / "scripts/runtime_common.py")
|
||||
runtime = importlib.util.module_from_spec(SPEC)
|
||||
SPEC.loader.exec_module(runtime)
|
||||
|
||||
|
||||
def test_atomic_report_rejects_nan_without_overwriting(tmp_path):
|
||||
path = tmp_path / "result.json"
|
||||
runtime.write_json(path, {"passed": False})
|
||||
with pytest.raises(ValueError):
|
||||
runtime.write_json(path, {"passed": True, "value": float("nan")})
|
||||
assert json.loads(path.read_text()) == {"passed": False}
|
||||
assert list(tmp_path.iterdir()) == [path]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("close_fails", [False, True])
|
||||
def test_success_only_after_environment_close(tmp_path, close_fails):
|
||||
events = []
|
||||
|
||||
def close():
|
||||
events.append("env")
|
||||
assert not (tmp_path / "result.json").exists()
|
||||
if close_fails:
|
||||
raise RuntimeError("shutdown failed")
|
||||
|
||||
def close_app(exit_code):
|
||||
events.append("app")
|
||||
report = json.loads((tmp_path / "result.json").read_text())
|
||||
assert report["passed"] is (not close_fails)
|
||||
assert exit_code == int(close_fails)
|
||||
if close_fails:
|
||||
assert report["milestone_complete"] is False
|
||||
|
||||
runtime.finish_run(
|
||||
SimpleNamespace(close=close_app),
|
||||
SimpleNamespace(close=close),
|
||||
tmp_path / "result.json",
|
||||
{"passed": True, "milestone_complete": True},
|
||||
)
|
||||
assert events == ["env", "app"]
|
||||
|
||||
|
||||
def test_critical_log_scan_includes_buffer_overflow(tmp_path):
|
||||
path = tmp_path / "worker.log"
|
||||
path.write_text("[Warning] harmless\n[error] physics\ncontact buffer overflow\n[Fatal] stopped\n")
|
||||
assert runtime.critical_log_lines(path) == ["[error] physics", "contact buffer overflow", "[Fatal] stopped"]
|
||||
Reference in New Issue
Block a user