Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mjx/mujoco/mjx/_src/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ def _put_option(
implicitfast = o.integrator == mujoco.mjtIntegrator.mjINT_IMPLICITFAST
if implicitfast and has_fluid_params:
raise NotImplementedError('implicitfast not implemented for fluid drag.')
impl_fields['solver_scan'] = bool(impl_fields.get('solver_scan', False))
impl_fields['has_fluid_params'] = has_fluid_params
return types.Option(**fields, _impl=types.OptionJAX(**impl_fields))

Expand Down
4 changes: 4 additions & 0 deletions mjx/mujoco/mjx/_src/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ def body(ctx: Context) -> Context:
ctx = Context.create(m, d)
if m.opt.iterations == 1:
ctx = body(ctx)
elif m.opt._impl.solver_scan:
# Opt-in scan-based loop for reverse-mode autodiff. The default remains
# `while_loop` to avoid changing solver performance characteristics.
ctx = _while_loop_scan(cond, body, ctx, m.opt.iterations)
else:
ctx = jax.lax.while_loop(cond, body, ctx)

Expand Down
28 changes: 28 additions & 0 deletions mjx/mujoco/mjx/_src/solver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,34 @@ def test_condim(self, condim, cone):
m.opt.cone = cone
solver.solve(mjx.put_model(m), mjx.put_data(m, mujoco.MjData(m)))

def test_solver_reverse_mode_grad(self):
"""Reverse-mode autodiff works with the scan solver loop enabled."""
m = mujoco.MjModel.from_xml_string("""
<mujoco>
<worldbody>
<geom size="0 0 1e-5" type="plane" condim="1"/>
<body pos="0 0 0.09">
<freejoint/>
<geom size="0.1"/>
</body>
</worldbody>
</mujoco>
""")
# iterations > 1 exercises the outer loop that blocks reverse-mode autodiff.
m.opt.iterations = 4
m.opt.ls_iterations = 4
mx = mjx.put_model(m).tree_replace({'opt._impl.solver_scan': True})
dx = mjx.put_data(m, mujoco.MjData(m))

def loss(qvel):
d = dx.replace(qvel=qvel)
d = mjx.solve(mx, d)
return (d.qacc ** 2).sum()

grad = jax.jit(jax.grad(loss))(dx.qvel)
self.assertEqual(grad.shape, dx.qvel.shape)
self.assertTrue(np.all(np.isfinite(grad)))


if __name__ == '__main__':
absltest.main()
2 changes: 1 addition & 1 deletion mjx/mujoco/mjx/_src/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ class OptionJAX(PyTreeNode):
o_friction: jax.Array
disableactuator: int
sdf_initpoints: int
solver_scan: bool
has_fluid_params: bool


Expand Down Expand Up @@ -1213,4 +1214,3 @@ def tree_path_to_attr_str(path: jax.tree_util.KeyPath) -> str:
assert all(isinstance(p, jax.tree_util.GetAttrKey) for p in path)
path = [p for p in path if p.name != '_impl']
return '__'.join(p.name for p in path)

Loading