"""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_effective_epa_scratch_capacity_is_recorded(tmp_path): import json sim = SimpleNamespace( cfg=SimpleNamespace(njmax=64, nconmax=48), wp_data=SimpleNamespace(njmax=64, naconmax=96, nworld=2), epa_horizon_capacity=128, ) write_warp_allocation(tmp_path, sim) record = json.loads((tmp_path / FILENAME).read_text()) assert record["effective"]["epa_horizon"] == 128 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 = """ """ 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 = """ """ 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()