|
| 1 | +# Copyright 2022 DeepMind Technologies Limited |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for all MJX models.""" |
| 15 | + |
| 16 | +import pathlib |
| 17 | +from typing import List |
| 18 | + |
| 19 | +from absl.testing import absltest |
| 20 | +from absl.testing import parameterized |
| 21 | +import jax |
| 22 | +import jax.numpy as jp |
| 23 | +import mujoco |
| 24 | +from mujoco import mjx |
| 25 | + |
| 26 | +# Internal import. |
| 27 | + |
| 28 | + |
| 29 | +_ROOT_DIR = pathlib.Path(__file__).parent.parent |
| 30 | +_MODEL_DIRS = [f for f in _ROOT_DIR.iterdir() if f.is_dir()] |
| 31 | +_MJX_MODEL_XMLS: List[pathlib.Path] = [] |
| 32 | + |
| 33 | + |
| 34 | +def _get_xmls(pattern: str) -> List[pathlib.Path]: |
| 35 | + for d in _MODEL_DIRS: |
| 36 | + # Produce tuples of test name and XML path. |
| 37 | + for f in d.glob(pattern): |
| 38 | + test_name = str(f).removeprefix(str(f.parent.parent)) |
| 39 | + yield (test_name, f) |
| 40 | + |
| 41 | +_MJX_MODEL_XMLS = list(_get_xmls('scene*mjx.xml')) |
| 42 | + |
| 43 | +# Total simulation duration, in seconds. |
| 44 | +_MAX_SIM_TIME = 0.1 |
| 45 | + |
| 46 | + |
| 47 | +class MjxModelsTest(parameterized.TestCase): |
| 48 | + """Tests that MJX models load and do not return NaNs.""" |
| 49 | + |
| 50 | + @parameterized.named_parameters(_MJX_MODEL_XMLS) |
| 51 | + def test_compiles_and_steps(self, xml_path: pathlib.Path) -> None: |
| 52 | + model = mujoco.MjModel.from_xml_path(str(xml_path)) |
| 53 | + model = mjx.put_model(model) |
| 54 | + data = mjx.make_data(model) |
| 55 | + ctrlrange = jp.where( |
| 56 | + model.actuator_ctrllimited[:, None], |
| 57 | + model.actuator_ctrlrange, |
| 58 | + jp.array([-10.0, 10.0]), |
| 59 | + ) |
| 60 | + |
| 61 | + def step(x, _): |
| 62 | + data, rng = x |
| 63 | + rng, key = jax.random.split(rng) |
| 64 | + ctrl = jax.random.uniform( |
| 65 | + key, |
| 66 | + shape=(model.nu,), |
| 67 | + minval=ctrlrange[:, 0], |
| 68 | + maxval=ctrlrange[:, 1], |
| 69 | + ) |
| 70 | + data = mjx.step(model, data.replace(ctrl=ctrl)) |
| 71 | + return (data, rng), () |
| 72 | + |
| 73 | + (data, _), _ = jax.lax.scan( |
| 74 | + step, |
| 75 | + (data, jax.random.PRNGKey(0)), |
| 76 | + (), |
| 77 | + length=min(_MAX_SIM_TIME // model.opt.timestep, 100), |
| 78 | + ) |
| 79 | + |
| 80 | + self.assertFalse(jp.isnan(data.qpos).any()) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + absltest.main() |
0 commit comments