From 1894bc302dc0058c26b03bd605eb4247d9e1870c Mon Sep 17 00:00:00 2001 From: Mark Colley Date: Sat, 14 Nov 2026 20:46:24 +0000 Subject: [PATCH] Support act and act_dot spans on BoundDataAccessor for actuators --- .../torch/_src/core/torch_wrapper.py | 10 ++- .../torch/_src/core/torch_wrapper_test.py | 86 +++++++++++++++++++ 2 files changed, 94 insertions(+), 2 deletions(-) diff --git a/reaf/simulation/torch/_src/core/torch_wrapper.py b/reaf/simulation/torch/_src/core/torch_wrapper.py index 5c90222..41b493f 100644 --- a/reaf/simulation/torch/_src/core/torch_wrapper.py +++ b/reaf/simulation/torch/_src/core/torch_wrapper.py @@ -840,6 +840,10 @@ def __init__( model_wrapper.actuator_ctrladr[self.id], model_wrapper.actuator_ctrlnum[self.id], ) + self.act_id = _span_indices( + model_wrapper.actuator_actadr[self.id], + model_wrapper.actuator_actnum[self.id], + ) self.out_id = _actuator_out_indices(model_wrapper, self.id) def __getname(self, name: str): # pylint: disable=unused-private-member @@ -848,10 +852,10 @@ def __getname(self, name: str): # pylint: disable=unused-private-member if self.prefix == "sensor_": return name raise AttributeError("sensordata is not available for this type") - if name == "ctrl": + if name in ("ctrl", "act", "act_dot"): if self.prefix == "actuator_": return name - raise AttributeError("ctrl is not available for this type") + raise AttributeError(f"{name} is not available for this type") if ( name == "qpos" or name == "qvel" @@ -875,6 +879,8 @@ def _get_indices(self, name: str) -> torch.Tensor: return self.dof_id if name == "ctrl": return self.ctrl_id + if name in ("act", "act_dot"): + return self.act_id if self.prefix == "actuator_" and name in ("length", "velocity", "force"): # Map actuator IDs to output spans (length, velocity, force). return self.out_id diff --git a/reaf/simulation/torch/_src/core/torch_wrapper_test.py b/reaf/simulation/torch/_src/core/torch_wrapper_test.py index e59e557..96e64f3 100644 --- a/reaf/simulation/torch/_src/core/torch_wrapper_test.py +++ b/reaf/simulation/torch/_src/core/torch_wrapper_test.py @@ -1170,6 +1170,92 @@ def test_mimo_actuators_bind(self): ) self.assertEqual(d.ctrl[5], 99.0) + @parameterized.parameters(types.Impl.CPP, types.Impl.WARP) + def test_actuator_act_bind(self, backend: types.Impl): + xml = """ + + + + + + + + + + + + + + + + + + + + + + """ + spec = mujoco.MjSpec.from_string(xml) + m = spec.compile() + d = mujoco.MjData(m) + d.act[:] = [0.25, 0.5, 0.75] + d.act_dot[:] = [1.5, -0.5, 2.0] + + nworld = 2 if backend == types.Impl.WARP else 1 + mw = wrap_model(backend, m, nworld=nworld) + dw = wrap_data(backend, m, d, nworld=nworld) + device = dw.act.device + + self.assertEqual(dw.bind(mw, spec.actuators[0]).act.shape, (nworld, 0)) + np.testing.assert_allclose( + dw.bind(mw, spec.actuators[1]).act.cpu().numpy(), + np.full((nworld,), 0.25), + ) + np.testing.assert_allclose( + dw.bind(mw, spec.actuators[2]).act.cpu().numpy(), + np.broadcast_to([0.5, 0.75], (nworld, 2)), + ) + np.testing.assert_allclose( + dw.bind(mw, spec.actuators).act.cpu().numpy(), + np.broadcast_to([0.25, 0.5, 0.75], (nworld, 3)), + ) + np.testing.assert_allclose( + dw.bind(mw, spec.actuators).act_dot.cpu().numpy(), + np.broadcast_to([1.5, -0.5, 2.0], (nworld, 3)), + ) + + dw.bind(mw, spec.actuators[1]).set( + "act", + torch.full((nworld,), 0.9, dtype=dw.act.dtype, device=device), + ) + np.testing.assert_allclose( + dw.bind(mw, spec.actuators[1]).act.cpu().numpy(), + np.full((nworld,), 0.9), + ) + + if backend == types.Impl.WARP: + env_mask = torch.tensor([True, False], device=device) + dw.bind(mw, spec.actuators[2]).set( + "act", + torch.tensor( + [[0.1, 0.2], [0.8, 0.9]], dtype=dw.act.dtype, device=device + ), + env_mask=env_mask, + ) + np.testing.assert_allclose( + dw.bind(mw, spec.actuators[2]).act.cpu().numpy(), + [[0.1, 0.2], [0.5, 0.75]], + ) + + with self.assertRaisesRegex( + AttributeError, "act is not available for this type" + ): + _ = dw.bind(mw, spec.geoms).act + with self.assertRaisesRegex( + AttributeError, "act_dot is not available for this type" + ): + _ = dw.bind(mw, spec.geoms).act_dot + def _dtype_matches(mj_dtype: torch.dtype, mjw_dtype: torch.dtype) -> bool: """Checks dtypes match, allowing 64-bit CPU vs 32-bit GPU."""