|
| 1 | +# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md). |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | + |
| 6 | +"""Unit tests for MuJoCo tendon deduplication in the Newton cloner. |
| 7 | +
|
| 8 | +:func:`_deduplicate_tendon_attributes` corrects the N×T tendon-entry duplication |
| 9 | +that arises when IsaacLab's Newton cloner calls ``add_builder`` once per environment |
| 10 | +(N times) with a prototype that carries T tendon entries. Newton's SolverMuJoCo |
| 11 | +expects only the template-world (world 0) entries and replicates them internally. |
| 12 | +
|
| 13 | +Tests construct builder state directly via Newton's ``ModelBuilder`` API so that |
| 14 | +no Isaac Sim or MJCF XML parsing is required. |
| 15 | +""" |
| 16 | + |
| 17 | +import unittest |
| 18 | + |
| 19 | +import newton |
| 20 | +from isaaclab_newton.cloner.newton_replicate import _deduplicate_tendon_attributes |
| 21 | +from newton.solvers import SolverMuJoCo |
| 22 | + |
| 23 | +# ─── helpers ──────────────────────────────────────────────────────────────── |
| 24 | + |
| 25 | +_TENDON_FREQ = "mujoco:tendon" |
| 26 | +_TENDON_JOINT_FREQ = "mujoco:tendon_joint" |
| 27 | +_TENDON_WRAP_FREQ = "mujoco:tendon_wrap" |
| 28 | + |
| 29 | + |
| 30 | +def _registered_builder() -> newton.ModelBuilder: |
| 31 | + """Return a ModelBuilder with MuJoCo custom attributes registered.""" |
| 32 | + b = newton.ModelBuilder() |
| 33 | + SolverMuJoCo.register_custom_attributes(b) |
| 34 | + return b |
| 35 | + |
| 36 | + |
| 37 | +def _inject_tendon_entries( |
| 38 | + builder: newton.ModelBuilder, |
| 39 | + world: int, |
| 40 | + names: list[str], |
| 41 | + stiffnesses: list[float], |
| 42 | + joint_entries_per_tendon: int, |
| 43 | +) -> None: |
| 44 | + """Append synthetic tendon entries to *builder* for the given *world*. |
| 45 | +
|
| 46 | + Directly manipulates custom attribute lists and frequency counts, mimicking |
| 47 | + what ``add_builder`` does when copying a prototype with tendon data. |
| 48 | + """ |
| 49 | + t = len(names) |
| 50 | + j = t * joint_entries_per_tendon |
| 51 | + world_attr = builder.custom_attributes.get("mujoco:tendon_world") |
| 52 | + label_attr = builder.custom_attributes.get("mujoco:tendon_label") |
| 53 | + stiff_attr = builder.custom_attributes.get("mujoco:tendon_stiffness") |
| 54 | + joint_adr_attr = builder.custom_attributes.get("mujoco:tendon_joint_adr") |
| 55 | + joint_num_attr = builder.custom_attributes.get("mujoco:tendon_joint_num") |
| 56 | + joint_attr = builder.custom_attributes.get("mujoco:tendon_joint") |
| 57 | + coef_attr = builder.custom_attributes.get("mujoco:tendon_coef") |
| 58 | + |
| 59 | + for attr in (world_attr, label_attr, stiff_attr, joint_adr_attr, joint_num_attr): |
| 60 | + if attr is not None and attr.values is None: |
| 61 | + attr.values = [] |
| 62 | + for attr in (joint_attr, coef_attr): |
| 63 | + if attr is not None and attr.values is None: |
| 64 | + attr.values = [] |
| 65 | + |
| 66 | + current_joint_offset = builder._custom_frequency_counts.get(_TENDON_JOINT_FREQ, 0) |
| 67 | + for i, (name, stiff) in enumerate(zip(names, stiffnesses)): |
| 68 | + if world_attr is not None: |
| 69 | + world_attr.values.append(world) |
| 70 | + if label_attr is not None: |
| 71 | + label_attr.values.append(name) |
| 72 | + if stiff_attr is not None: |
| 73 | + stiff_attr.values.append(stiff) |
| 74 | + if joint_adr_attr is not None: |
| 75 | + joint_adr_attr.values.append(current_joint_offset + i * joint_entries_per_tendon) |
| 76 | + if joint_num_attr is not None: |
| 77 | + joint_num_attr.values.append(joint_entries_per_tendon) |
| 78 | + for _ in range(j): |
| 79 | + if joint_attr is not None: |
| 80 | + joint_attr.values.append(0) |
| 81 | + if coef_attr is not None: |
| 82 | + coef_attr.values.append(1.0) |
| 83 | + |
| 84 | + builder._custom_frequency_counts[_TENDON_FREQ] = builder._custom_frequency_counts.get(_TENDON_FREQ, 0) + t |
| 85 | + builder._custom_frequency_counts[_TENDON_JOINT_FREQ] = ( |
| 86 | + builder._custom_frequency_counts.get(_TENDON_JOINT_FREQ, 0) + j |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | +def _tendon_count(b: newton.ModelBuilder) -> int: |
| 91 | + return b._custom_frequency_counts.get(_TENDON_FREQ, 0) |
| 92 | + |
| 93 | + |
| 94 | +def _tendon_joint_count(b: newton.ModelBuilder) -> int: |
| 95 | + return b._custom_frequency_counts.get(_TENDON_JOINT_FREQ, 0) |
| 96 | + |
| 97 | + |
| 98 | +def _world_values(b: newton.ModelBuilder) -> list[int]: |
| 99 | + attr = b.custom_attributes.get("mujoco:tendon_world") |
| 100 | + if attr is None or not isinstance(attr.values, list): |
| 101 | + return [] |
| 102 | + return [int(v) for v in attr.values] |
| 103 | + |
| 104 | + |
| 105 | +def _label_values(b: newton.ModelBuilder) -> list[str]: |
| 106 | + attr = b.custom_attributes.get("mujoco:tendon_label") |
| 107 | + if attr is None or not isinstance(attr.values, list): |
| 108 | + return [] |
| 109 | + return list(attr.values) |
| 110 | + |
| 111 | + |
| 112 | +# ─── test cases ───────────────────────────────────────────────────────────── |
| 113 | + |
| 114 | + |
| 115 | +class TestTendonDeduplication(unittest.TestCase): |
| 116 | + """Verifies that _deduplicate_tendon_attributes trims N×T entries to T.""" |
| 117 | + |
| 118 | + # Test parameters: 2 tendons, 2 joint entries each → 4 joint entries total per world. |
| 119 | + NAMES = ["tendon_coupling", "tendon_sum"] |
| 120 | + STIFFNESSES = [2.0, 1.0] |
| 121 | + JOINT_ENTRIES_PER_TENDON = 2 |
| 122 | + T = len(NAMES) |
| 123 | + T_JOINT = T * JOINT_ENTRIES_PER_TENDON |
| 124 | + |
| 125 | + def _build_duplicated(self, n_envs: int) -> tuple[newton.ModelBuilder, int, int]: |
| 126 | + """Return a builder with N×T entries and the template counts (T, T_joint).""" |
| 127 | + main = _registered_builder() |
| 128 | + template_tendon = 0 |
| 129 | + template_joint = 0 |
| 130 | + for world in range(n_envs): |
| 131 | + _inject_tendon_entries(main, world, self.NAMES, self.STIFFNESSES, self.JOINT_ENTRIES_PER_TENDON) |
| 132 | + if world == 0: |
| 133 | + template_tendon = _tendon_count(main) |
| 134 | + template_joint = _tendon_joint_count(main) |
| 135 | + return main, template_tendon, template_joint |
| 136 | + |
| 137 | + def test_duplication_exists_before_fix(self): |
| 138 | + """Without fix, N add_builder calls produce N×T tendon entries.""" |
| 139 | + n = 4 |
| 140 | + main, _, _ = self._build_duplicated(n) |
| 141 | + self.assertEqual(_tendon_count(main), n * self.T) |
| 142 | + self.assertEqual(_tendon_joint_count(main), n * self.T_JOINT) |
| 143 | + |
| 144 | + def test_deduplication_restores_template_count(self): |
| 145 | + """After fix, exactly T tendon entries remain.""" |
| 146 | + n = 4 |
| 147 | + main, tmpl_t, tmpl_j = self._build_duplicated(n) |
| 148 | + _deduplicate_tendon_attributes(main, tmpl_t, tmpl_j, 0) |
| 149 | + self.assertEqual(_tendon_count(main), self.T) |
| 150 | + self.assertEqual(_tendon_joint_count(main), self.T_JOINT) |
| 151 | + |
| 152 | + def test_deduplication_all_entries_are_world_0(self): |
| 153 | + """After fix, every remaining tendon_world entry equals 0.""" |
| 154 | + n = 5 |
| 155 | + main, tmpl_t, tmpl_j = self._build_duplicated(n) |
| 156 | + _deduplicate_tendon_attributes(main, tmpl_t, tmpl_j, 0) |
| 157 | + worlds = _world_values(main) |
| 158 | + self.assertEqual(len(worlds), self.T) |
| 159 | + self.assertTrue(all(w == 0 for w in worlds), f"Expected world=0, got {worlds}") |
| 160 | + |
| 161 | + def test_deduplication_preserves_template_labels(self): |
| 162 | + """After fix, the surviving labels are the template world's names.""" |
| 163 | + n = 3 |
| 164 | + main, tmpl_t, tmpl_j = self._build_duplicated(n) |
| 165 | + _deduplicate_tendon_attributes(main, tmpl_t, tmpl_j, 0) |
| 166 | + labels = _label_values(main) |
| 167 | + self.assertEqual(sorted(labels), sorted(self.NAMES)) |
| 168 | + |
| 169 | + def test_deduplication_frequency_counts_match_list_lengths(self): |
| 170 | + """_custom_frequency_counts must stay consistent with attribute list lengths.""" |
| 171 | + n = 6 |
| 172 | + main, tmpl_t, tmpl_j = self._build_duplicated(n) |
| 173 | + _deduplicate_tendon_attributes(main, tmpl_t, tmpl_j, 0) |
| 174 | + |
| 175 | + reported_t = main._custom_frequency_counts.get(_TENDON_FREQ, 0) |
| 176 | + actual_t = len(main.custom_attributes["mujoco:tendon_world"].values) |
| 177 | + self.assertEqual(reported_t, actual_t) |
| 178 | + |
| 179 | + reported_j = main._custom_frequency_counts.get(_TENDON_JOINT_FREQ, 0) |
| 180 | + actual_j = len(main.custom_attributes["mujoco:tendon_joint"].values) |
| 181 | + self.assertEqual(reported_j, actual_j) |
| 182 | + |
| 183 | + def test_no_change_for_single_env(self): |
| 184 | + """For N=1 no duplication occurs; counts must stay T after a no-op trim.""" |
| 185 | + main, tmpl_t, tmpl_j = self._build_duplicated(1) |
| 186 | + self.assertEqual(_tendon_count(main), self.T) |
| 187 | + # No trimming needed (caller guards on count > template), but the function |
| 188 | + # should be safe to call anyway. |
| 189 | + _deduplicate_tendon_attributes(main, tmpl_t, tmpl_j, 0) |
| 190 | + self.assertEqual(_tendon_count(main), self.T) |
| 191 | + self.assertEqual(_tendon_joint_count(main), self.T_JOINT) |
| 192 | + |
| 193 | + def test_deduplication_is_idempotent(self): |
| 194 | + """Calling _deduplicate_tendon_attributes twice must not trim further.""" |
| 195 | + n = 4 |
| 196 | + main, tmpl_t, tmpl_j = self._build_duplicated(n) |
| 197 | + _deduplicate_tendon_attributes(main, tmpl_t, tmpl_j, 0) |
| 198 | + _deduplicate_tendon_attributes(main, tmpl_t, tmpl_j, 0) |
| 199 | + self.assertEqual(_tendon_count(main), self.T) |
| 200 | + self.assertEqual(_tendon_joint_count(main), self.T_JOINT) |
| 201 | + |
| 202 | + def test_regression_bug_n_times_t_entries(self): |
| 203 | + """Regression: verify the reported bug (N×T instead of T) is fixed.""" |
| 204 | + n = 8 |
| 205 | + main, tmpl_t, tmpl_j = self._build_duplicated(n) |
| 206 | + |
| 207 | + # BUG: before fix, count == n * T |
| 208 | + self.assertEqual(_tendon_count(main), n * self.T, "pre-condition: duplication present") |
| 209 | + |
| 210 | + _deduplicate_tendon_attributes(main, tmpl_t, tmpl_j, 0) |
| 211 | + |
| 212 | + # FIX: after deduplication, count == T |
| 213 | + self.assertEqual(_tendon_count(main), self.T, "post-condition: deduplication applied") |
| 214 | + self.assertNotEqual(_tendon_count(main), n * self.T, "count must not be N×T after fix") |
| 215 | + |
| 216 | + |
| 217 | +if __name__ == "__main__": |
| 218 | + unittest.main() |
0 commit comments