Files
UniversalTrain-ai/tests/test_warp_allocation.py
T
chenlin c32df70326
Checks / Layout, translation and Colab checks (push) Has been cancelled
Checks / Ruff (push) Has been cancelled
Checks / Framework tests (native backend, CPU) (push) Has been cancelled
Checks / Portable Rust controller (push) Has been cancelled
release: v0.1 generic robot training and application platform
2026-11-05 11:05:01 +08:00

86 lines
3.3 KiB
Python

"""Warp buffer overrides, replay persistence and live overflow checks."""
from argparse import Namespace
from types import SimpleNamespace
import pytest
from mjrl.backend.warp_allocation import (
FILENAME,
apply_warp_allocation,
write_warp_allocation,
)
def test_saved_allocations_restore_and_respect_scene_and_cli(tmp_path):
selected = SimpleNamespace(njmax=300, nconmax=None)
data = SimpleNamespace(njmax=300, naconmax=192, nworld=4)
write_warp_allocation(tmp_path, SimpleNamespace(cfg=selected, wp_data=data))
checkpoint = tmp_path / "model_10.pt"
replay = SimpleNamespace(njmax=None, nconmax=None)
apply_warp_allocation(replay, Namespace(), "warp", checkpoint)
assert (replay.njmax, replay.nconmax) == (300, 48)
rough = SimpleNamespace(njmax=500, nconmax=128)
apply_warp_allocation(rough, Namespace(warp_max_contacts=80), "warp", checkpoint)
assert (rough.njmax, rough.nconmax) == (500, 80)
native = SimpleNamespace(njmax=None, nconmax=None)
apply_warp_allocation(native, Namespace(), "native", checkpoint)
assert (native.njmax, native.nconmax) == (None, None)
def test_legacy_checkpoint_falls_back_and_invalid_record_fails(tmp_path):
checkpoint = tmp_path / "model_10.pt"
sim = SimpleNamespace(njmax=100, nconmax=80)
apply_warp_allocation(sim, Namespace(), "warp", checkpoint)
assert (sim.njmax, sim.nconmax) == (100, 80)
(tmp_path / FILENAME).write_text('{"allocation": {"njmax": true, "nconmax": 20}}')
with pytest.raises(SystemExit, match="invalid Warp run record"):
apply_warp_allocation(sim, Namespace(), "warp", checkpoint)
def test_warp_reports_constraint_overflow_before_observations():
mujoco = pytest.importorskip("mujoco")
pytest.importorskip("warp")
pytest.importorskip("mujoco_warp")
from mjlab.sim import SimulationCfg
from mjrl.backend.warp_sim import CheckedWarpSimulation
xml = """<mujoco><worldbody><geom type="plane" size="0 0 .1"/>
<body pos="0 0 1"><freejoint/><geom type="box" size=".15 .15 .15" mass="1"/></body>
</worldbody></mujoco>"""
model = mujoco.MjModel.from_xml_string(xml)
sim = CheckedWarpSimulation(1, SimulationCfg(njmax=4), model, "cpu")
sim.sense()
sim.data.qpos[0, 2] = 0.10
sim.forward()
assert sim.wp_data.nefc.numpy()[0] == 16
# Simulate an environment resetting the falling robot before observing.
sim.reset()
sim.data.qpos[0, 2] = 1.0
sim.forward()
assert sim.wp_data.nefc.numpy()[0] == 0
with pytest.raises(RuntimeError, match=r"buffer overflow.*4, demand above capacity: 16"):
sim.sense()
def test_warp_reports_contact_pool_overflow():
mujoco = pytest.importorskip("mujoco")
pytest.importorskip("warp")
pytest.importorskip("mujoco_warp")
from mjlab.sim import SimulationCfg
from mjrl.backend.warp_sim import CheckedWarpSimulation
xml = """<mujoco><worldbody><geom type="plane" size="0 0 .1"/>
<body pos="0 0 1"><freejoint/><geom type="box" size=".15 .15 .15" mass="1"/></body>
</worldbody></mujoco>"""
model = mujoco.MjModel.from_xml_string(xml)
sim = CheckedWarpSimulation(1, SimulationCfg(nconmax=1), model, "cpu")
sim.data.qpos[0, 2] = 0.10
sim.forward()
with pytest.raises(RuntimeError, match=r"Shared contact capacity: 1.*warp-max-contacts"):
sim.sense()