mjbatch-uni 0.2.3__tar.gz → 0.2.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/PKG-INFO +1 -1
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/pyproject.toml +1 -1
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/src/mjbatch/__init__.py +2 -1
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/src/mjbatch/variants.py +262 -86
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/CMakeLists.txt +0 -0
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/LICENSE +0 -0
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/README.md +0 -0
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/src/mjbatch/_bindings.pyi +0 -0
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/src/mjbatch/csrc/batch.h +0 -0
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/src/mjbatch/csrc/bindings.cpp +0 -0
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/src/mjbatch/csrc/threadpool.h +0 -0
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/src/mjbatch/groups.py +0 -0
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/src/mjbatch/model.py +0 -0
- {mjbatch_uni-0.2.3 → mjbatch_uni-0.2.4}/src/mjbatch/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mjbatch-uni
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.4
|
|
4
4
|
Summary: A Python library for running thousands of MuJoCo simulations in parallel on CPU (unilabsim fork).
|
|
5
5
|
Keywords: mujoco,physics,simulation,batched,reinforcement-learning
|
|
6
6
|
Author-Email: Kevin Zakka <zakka@berkeley.edu>, Yufei Jia <jyf23@mails.tsinghua.edu.cn>
|
|
@@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "mjbatch-uni"
|
|
7
|
-
version = "0.2.
|
|
7
|
+
version = "0.2.4"
|
|
8
8
|
description = "A Python library for running thousands of MuJoCo simulations in parallel on CPU (unilabsim fork)."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|
|
@@ -15,7 +15,8 @@ from mjbatch.groups import TopologyGroup as TopologyGroup
|
|
|
15
15
|
from mjbatch.model import ModelFieldSpec as ModelFieldSpec
|
|
16
16
|
from mjbatch.model import RecomputeLevel as RecomputeLevel
|
|
17
17
|
from mjbatch.model import build_model_fields
|
|
18
|
-
from mjbatch.variants import VariantPack
|
|
18
|
+
from mjbatch.variants import VariantPack as VariantPack
|
|
19
|
+
from mjbatch.variants import VariantPackBuilder as VariantPackBuilder
|
|
19
20
|
|
|
20
21
|
_QPOS_WIDTH = {0: 7, 1: 4, 2: 1, 3: 1} # by mjtJoint: free, ball, slide, hinge
|
|
21
22
|
_DOF_WIDTH = {0: 6, 1: 3, 2: 1, 3: 1}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"""Compiler-coherent, same-layout mesh variant construction."""
|
|
2
2
|
|
|
3
|
+
import hashlib
|
|
3
4
|
from dataclasses import dataclass
|
|
4
5
|
from pathlib import Path
|
|
5
6
|
from types import MappingProxyType
|
|
@@ -124,83 +125,217 @@ class VariantPack:
|
|
|
124
125
|
contain optional mesh-geom slots that are absent in another variant; missing
|
|
125
126
|
slots are disabled with ``mjGEOM_NONE``, ``geom_dataid=-1``, and zero contact
|
|
126
127
|
bits. Non-mesh topology and parameters must not vary.
|
|
128
|
+
|
|
129
|
+
Construction is streamed through :class:`VariantPackBuilder`: one variant is
|
|
130
|
+
compiled at a time and only small compiler-derived rows are retained, so
|
|
131
|
+
peak memory stays independent of the variant count.
|
|
127
132
|
"""
|
|
128
133
|
if not specs:
|
|
129
134
|
raise ValueError("at least one variant spec is required")
|
|
135
|
+
builder = VariantPackBuilder()
|
|
136
|
+
for spec in specs:
|
|
137
|
+
builder.add_variant(spec)
|
|
138
|
+
return builder.build(specs[builder.canonical_index].copy())
|
|
139
|
+
|
|
140
|
+
@classmethod
|
|
141
|
+
def builder(cls) -> "VariantPackBuilder":
|
|
142
|
+
"""Return an incremental, streaming VariantPack constructor."""
|
|
143
|
+
return VariantPackBuilder()
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
class VariantPackBuilder:
|
|
147
|
+
"""Incrementally accumulate same-layout variants with streaming compilation.
|
|
148
|
+
|
|
149
|
+
``add_variant`` compiles one spec (or adopts an existing compilation of it),
|
|
150
|
+
snapshots the small per-variant rows the pack needs, and returns the model so
|
|
151
|
+
the caller can validate it and then release both spec and model. Peak memory
|
|
152
|
+
is bounded by the canonical model, the unique mesh pool, and one transient
|
|
153
|
+
variant realization instead of scaling with the variant count.
|
|
154
|
+
|
|
155
|
+
``build`` must receive a spec equivalent to the one added at
|
|
156
|
+
``canonical_index`` (a fresh parse/copy is expected); it applies the pooled
|
|
157
|
+
mesh catalog, compiles the canonical model, validates every snapshot against
|
|
158
|
+
it, and assembles a :class:`VariantPack` identical to ``from_specs``.
|
|
159
|
+
"""
|
|
160
|
+
|
|
161
|
+
def __init__(self) -> None:
|
|
162
|
+
self._snapshots: list[_VariantSnapshot] = []
|
|
163
|
+
|
|
164
|
+
@property
|
|
165
|
+
def num_variants(self) -> int:
|
|
166
|
+
return len(self._snapshots)
|
|
167
|
+
|
|
168
|
+
@property
|
|
169
|
+
def canonical_index(self) -> int:
|
|
170
|
+
"""Index of the variant with the most geoms (first wins ties)."""
|
|
171
|
+
if not self._snapshots:
|
|
172
|
+
raise ValueError("at least one variant spec is required")
|
|
173
|
+
return max(
|
|
174
|
+
range(len(self._snapshots)),
|
|
175
|
+
key=lambda index: (self._snapshots[index].ngeom, -index),
|
|
176
|
+
)
|
|
130
177
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
178
|
+
def add_variant(self, spec: mujoco.MjSpec, model: mujoco.MjModel | None = None) -> mujoco.MjModel:
|
|
179
|
+
"""Snapshot one variant; ``model`` must be ``spec.compile()`` when given."""
|
|
180
|
+
if model is None:
|
|
181
|
+
model = spec.compile()
|
|
182
|
+
assert model is not None
|
|
183
|
+
self._snapshots.append(_snapshot_variant(spec, model))
|
|
184
|
+
return model
|
|
185
|
+
|
|
186
|
+
def build(self, canonical_spec: mujoco.MjSpec) -> VariantPack:
|
|
187
|
+
"""Assemble the pack around a fresh spec of the canonical variant."""
|
|
188
|
+
snapshots = self._snapshots
|
|
189
|
+
if not snapshots:
|
|
190
|
+
raise ValueError("at least one variant spec is required")
|
|
191
|
+
canonical_index = self.canonical_index
|
|
134
192
|
|
|
135
|
-
mesh_pool: dict[
|
|
193
|
+
mesh_pool: dict[bytes, str] = {}
|
|
136
194
|
for mesh in canonical_spec.meshes:
|
|
137
|
-
mesh_pool[
|
|
195
|
+
mesh_pool[_mesh_digest(canonical_spec, mesh)] = mesh.name
|
|
138
196
|
|
|
139
197
|
mesh_names_by_variant: list[dict[str, str]] = []
|
|
140
|
-
for variant_index,
|
|
198
|
+
for variant_index, snapshot in enumerate(snapshots):
|
|
141
199
|
mesh_names: dict[str, str] = {}
|
|
142
|
-
for
|
|
200
|
+
for record in snapshot.meshes:
|
|
143
201
|
if variant_index == canonical_index:
|
|
144
|
-
pooled_name =
|
|
202
|
+
pooled_name = record.name
|
|
145
203
|
else:
|
|
146
|
-
|
|
147
|
-
pooled_name = mesh_pool.get(key)
|
|
204
|
+
pooled_name = mesh_pool.get(record.key)
|
|
148
205
|
if pooled_name is None:
|
|
149
|
-
pooled_name =
|
|
150
|
-
mesh_pool[key] = pooled_name
|
|
151
|
-
if
|
|
152
|
-
raise ValueError(f"variant has duplicate mesh name {
|
|
153
|
-
mesh_names[
|
|
206
|
+
pooled_name = _copy_mesh_record(canonical_spec, record, variant_index)
|
|
207
|
+
mesh_pool[record.key] = pooled_name
|
|
208
|
+
if record.name in mesh_names:
|
|
209
|
+
raise ValueError(f"variant has duplicate mesh name {record.name!r}")
|
|
210
|
+
mesh_names[record.name] = pooled_name
|
|
154
211
|
mesh_names_by_variant.append(mesh_names)
|
|
155
212
|
|
|
156
|
-
_disable_simple_where_variants_break_inertial_frame(canonical_spec,
|
|
213
|
+
_disable_simple_where_variants_break_inertial_frame(canonical_spec, snapshots, canonical_index)
|
|
157
214
|
canonical = canonical_spec.compile()
|
|
158
|
-
_validate_layout(
|
|
159
|
-
geom_maps = _validate_names_and_build_geom_maps(
|
|
160
|
-
_validate_shared_parameters(
|
|
161
|
-
_validate_shared_options(
|
|
162
|
-
mesh_id_maps = _build_mesh_id_maps(
|
|
163
|
-
reference_models,
|
|
164
|
-
canonical,
|
|
165
|
-
specs,
|
|
166
|
-
mesh_names_by_variant,
|
|
167
|
-
)
|
|
215
|
+
_validate_layout(snapshots, canonical)
|
|
216
|
+
geom_maps = _validate_names_and_build_geom_maps(snapshots, canonical)
|
|
217
|
+
_validate_shared_parameters(snapshots, canonical, geom_maps)
|
|
218
|
+
_validate_shared_options(snapshots, canonical)
|
|
219
|
+
mesh_id_maps = _build_mesh_id_maps(snapshots, canonical, mesh_names_by_variant)
|
|
168
220
|
|
|
169
221
|
fields: dict[str, np.ndarray] = {}
|
|
170
222
|
for name in _VARIANT_FIELDS:
|
|
171
223
|
canonical_value = np.asarray(getattr(canonical, name))
|
|
172
|
-
values = np.tile(canonical_value, (len(
|
|
224
|
+
values = np.tile(canonical_value, (len(snapshots), *(1,) * canonical_value.ndim))
|
|
173
225
|
if name in _GEOM_VARIANT_FIELDS:
|
|
174
|
-
for variant, (
|
|
175
|
-
values[variant][geom_map] =
|
|
226
|
+
for variant, (snapshot, geom_map) in enumerate(zip(snapshots, geom_maps, strict=True)):
|
|
227
|
+
values[variant][geom_map] = snapshot.fields[name]
|
|
176
228
|
values = _disable_missing_geom_slots(
|
|
177
229
|
values,
|
|
178
230
|
name,
|
|
179
231
|
geom_maps,
|
|
180
232
|
canonical,
|
|
181
233
|
)
|
|
182
|
-
|
|
183
|
-
for variant,
|
|
184
|
-
values[variant] =
|
|
234
|
+
else:
|
|
235
|
+
for variant, snapshot in enumerate(snapshots):
|
|
236
|
+
values[variant] = snapshot.fields[name]
|
|
185
237
|
fields[name] = values
|
|
186
238
|
|
|
187
|
-
dataids = fields["geom_dataid"] = np.full((len(
|
|
188
|
-
for variant, (
|
|
239
|
+
dataids = fields["geom_dataid"] = np.full((len(snapshots), canonical.ngeom), -1, dtype=np.int32)
|
|
240
|
+
for variant, (snapshot, geom_map) in enumerate(zip(snapshots, geom_maps, strict=True)):
|
|
189
241
|
mesh_ids = mesh_id_maps[variant]
|
|
190
242
|
for reference_geom, canonical_geom in enumerate(geom_map):
|
|
191
|
-
dataid = int(
|
|
243
|
+
dataid = int(snapshot.fields["geom_dataid"][reference_geom])
|
|
192
244
|
dataids[variant, canonical_geom] = mesh_ids.get(dataid, -1)
|
|
193
245
|
|
|
194
246
|
for values in fields.values():
|
|
195
247
|
values.flags.writeable = False
|
|
196
248
|
|
|
197
|
-
return
|
|
249
|
+
return VariantPack(
|
|
198
250
|
model=canonical,
|
|
199
|
-
num_variants=len(
|
|
251
|
+
num_variants=len(snapshots),
|
|
200
252
|
fields=MappingProxyType(fields),
|
|
201
253
|
)
|
|
202
254
|
|
|
203
255
|
|
|
256
|
+
@dataclass
|
|
257
|
+
class _MeshRecord:
|
|
258
|
+
"""One spec mesh's identity digest and the payload needed to re-add it."""
|
|
259
|
+
|
|
260
|
+
name: str
|
|
261
|
+
key: bytes
|
|
262
|
+
file: str
|
|
263
|
+
content_type: str
|
|
264
|
+
refpos: np.ndarray
|
|
265
|
+
refquat: np.ndarray
|
|
266
|
+
scale: np.ndarray
|
|
267
|
+
inertia: int
|
|
268
|
+
smoothnormal: bool
|
|
269
|
+
needsdf: bool
|
|
270
|
+
maxhullvert: int
|
|
271
|
+
octree_maxdepth: int
|
|
272
|
+
material: Any
|
|
273
|
+
uservert: list
|
|
274
|
+
usernormal: list
|
|
275
|
+
usertexcoord: list
|
|
276
|
+
userface: list
|
|
277
|
+
userfacenormal: list
|
|
278
|
+
userfacetexcoord: list
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
@dataclass
|
|
282
|
+
class _VariantSnapshot:
|
|
283
|
+
"""Small compiler-derived rows retained per variant by the builder."""
|
|
284
|
+
|
|
285
|
+
ngeom: int
|
|
286
|
+
nbody: int
|
|
287
|
+
layout: tuple[tuple[str, int], ...]
|
|
288
|
+
entity_names: dict[str, list[str]]
|
|
289
|
+
fields: dict[str, np.ndarray]
|
|
290
|
+
body_simple: np.ndarray
|
|
291
|
+
shared: dict[str, np.ndarray]
|
|
292
|
+
options: dict[str, Any]
|
|
293
|
+
mesh_ids: dict[str, int]
|
|
294
|
+
meshes: list[_MeshRecord]
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _snapshot_variant(spec: mujoco.MjSpec, model: mujoco.MjModel) -> _VariantSnapshot:
|
|
298
|
+
fields = {name: np.asarray(getattr(model, name)).copy() for name in _VARIANT_FIELDS}
|
|
299
|
+
fields["geom_dataid"] = np.asarray(model.geom_dataid).copy()
|
|
300
|
+
shared: dict[str, np.ndarray] = {}
|
|
301
|
+
for name in dir(model):
|
|
302
|
+
if (
|
|
303
|
+
name.startswith(_SHARED_PARAMETER_PREFIXES)
|
|
304
|
+
and name not in _IGNORED_COMPILER_FLAGS
|
|
305
|
+
and not name.startswith(_IGNORED_COMPILER_METADATA_PREFIXES)
|
|
306
|
+
and name not in _ALLOWED_BODY_FIELDS
|
|
307
|
+
and name not in _ALLOWED_DOF_FIELDS
|
|
308
|
+
and name not in _ALLOWED_GEOM_FIELDS
|
|
309
|
+
and name not in _ALLOWED_DERIVED_FIELDS
|
|
310
|
+
):
|
|
311
|
+
value = getattr(model, name, None)
|
|
312
|
+
if isinstance(value, np.ndarray):
|
|
313
|
+
shared[name] = value.copy()
|
|
314
|
+
options: dict[str, Any] = {}
|
|
315
|
+
for name in dir(model.opt):
|
|
316
|
+
if name.startswith("_") or name == "timestep":
|
|
317
|
+
continue
|
|
318
|
+
value = getattr(model.opt, name, None)
|
|
319
|
+
if isinstance(value, np.ndarray):
|
|
320
|
+
options[name] = value.copy()
|
|
321
|
+
elif isinstance(value, (bool, int, float)):
|
|
322
|
+
options[name] = value
|
|
323
|
+
entity_names = {kind: _names(model, kind, getattr(model, count)) for kind, count in _ENTITY_KIND_COUNTS}
|
|
324
|
+
entity_names["geom"] = _names(model, "geom", model.ngeom)
|
|
325
|
+
return _VariantSnapshot(
|
|
326
|
+
ngeom=int(model.ngeom),
|
|
327
|
+
nbody=int(model.nbody),
|
|
328
|
+
layout=_layout(model),
|
|
329
|
+
entity_names=entity_names,
|
|
330
|
+
fields=fields,
|
|
331
|
+
body_simple=np.asarray(model.body_simple).copy(),
|
|
332
|
+
shared=shared,
|
|
333
|
+
options=options,
|
|
334
|
+
mesh_ids={mesh.name: int(model.mesh(mesh.name).id) for mesh in spec.meshes},
|
|
335
|
+
meshes=[_record_mesh(spec, mesh) for mesh in spec.meshes],
|
|
336
|
+
)
|
|
337
|
+
|
|
338
|
+
|
|
204
339
|
def _mesh_path(spec: mujoco.MjSpec, mesh: mujoco.MjsMesh) -> Path | None:
|
|
205
340
|
if not mesh.file:
|
|
206
341
|
return None
|
|
@@ -246,13 +381,58 @@ def _mesh_key(spec: mujoco.MjSpec, mesh: mujoco.MjsMesh) -> tuple[Any, ...]:
|
|
|
246
381
|
)
|
|
247
382
|
|
|
248
383
|
|
|
249
|
-
def
|
|
384
|
+
def _mesh_digest(spec: mujoco.MjSpec, mesh: mujoco.MjsMesh) -> bytes:
|
|
385
|
+
"""Content-addressed mesh identity bounded to a fixed-size digest.
|
|
386
|
+
|
|
387
|
+
The raw key embeds whole mesh files and vertex buffers; retaining one digest
|
|
388
|
+
per variant mesh keeps builder memory independent of mesh payload size.
|
|
389
|
+
"""
|
|
390
|
+
digest = hashlib.sha256()
|
|
391
|
+
for part in _mesh_key(spec, mesh):
|
|
392
|
+
if isinstance(part, bytes):
|
|
393
|
+
digest.update(b"\xfe")
|
|
394
|
+
digest.update(part)
|
|
395
|
+
elif isinstance(part, tuple):
|
|
396
|
+
for name, payload in part:
|
|
397
|
+
digest.update(name.encode())
|
|
398
|
+
digest.update(b"\xfe")
|
|
399
|
+
digest.update(payload)
|
|
400
|
+
else:
|
|
401
|
+
digest.update(repr(part).encode())
|
|
402
|
+
return digest.digest()
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
def _record_mesh(spec: mujoco.MjSpec, mesh: mujoco.MjsMesh) -> _MeshRecord:
|
|
406
|
+
path = _mesh_path(spec, mesh)
|
|
407
|
+
return _MeshRecord(
|
|
408
|
+
name=mesh.name,
|
|
409
|
+
key=_mesh_digest(spec, mesh),
|
|
410
|
+
file="" if path is None else str(path),
|
|
411
|
+
content_type=mesh.content_type,
|
|
412
|
+
refpos=np.array(mesh.refpos),
|
|
413
|
+
refquat=np.array(mesh.refquat),
|
|
414
|
+
scale=np.array(mesh.scale),
|
|
415
|
+
inertia=int(mesh.inertia),
|
|
416
|
+
smoothnormal=bool(mesh.smoothnormal),
|
|
417
|
+
needsdf=bool(mesh.needsdf),
|
|
418
|
+
maxhullvert=int(mesh.maxhullvert),
|
|
419
|
+
octree_maxdepth=int(mesh.octree_maxdepth),
|
|
420
|
+
material=mesh.material,
|
|
421
|
+
uservert=list(mesh.uservert),
|
|
422
|
+
usernormal=list(mesh.usernormal),
|
|
423
|
+
usertexcoord=list(mesh.usertexcoord),
|
|
424
|
+
userface=list(mesh.userface),
|
|
425
|
+
userfacenormal=list(mesh.userfacenormal),
|
|
426
|
+
userfacetexcoord=list(mesh.userfacetexcoord),
|
|
427
|
+
)
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def _copy_mesh_record(
|
|
250
431
|
target: mujoco.MjSpec,
|
|
251
|
-
|
|
252
|
-
source: mujoco.MjsMesh,
|
|
432
|
+
record: _MeshRecord,
|
|
253
433
|
variant_index: int,
|
|
254
434
|
) -> str:
|
|
255
|
-
base_name =
|
|
435
|
+
base_name = record.name or "mesh"
|
|
256
436
|
index = variant_index
|
|
257
437
|
while True:
|
|
258
438
|
pooled_name = f"mjbatch_mesh_v{index}_{base_name}"
|
|
@@ -261,22 +441,23 @@ def _copy_mesh(
|
|
|
261
441
|
index += 1
|
|
262
442
|
|
|
263
443
|
copied = target.add_mesh(name=pooled_name)
|
|
264
|
-
|
|
265
|
-
copied.
|
|
266
|
-
copied.
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
)
|
|
279
|
-
|
|
444
|
+
copied.file = record.file
|
|
445
|
+
copied.content_type = record.content_type
|
|
446
|
+
copied.refpos = record.refpos
|
|
447
|
+
copied.refquat = record.refquat
|
|
448
|
+
copied.scale = record.scale
|
|
449
|
+
copied.inertia = record.inertia
|
|
450
|
+
copied.smoothnormal = record.smoothnormal
|
|
451
|
+
copied.needsdf = record.needsdf
|
|
452
|
+
copied.maxhullvert = record.maxhullvert
|
|
453
|
+
copied.octree_maxdepth = record.octree_maxdepth
|
|
454
|
+
copied.material = record.material
|
|
455
|
+
copied.uservert = list(record.uservert)
|
|
456
|
+
copied.usernormal = list(record.usernormal)
|
|
457
|
+
copied.usertexcoord = list(record.usertexcoord)
|
|
458
|
+
copied.userface = list(record.userface)
|
|
459
|
+
copied.userfacenormal = list(record.userfacenormal)
|
|
460
|
+
copied.userfacetexcoord = list(record.userfacetexcoord)
|
|
280
461
|
return pooled_name
|
|
281
462
|
|
|
282
463
|
|
|
@@ -284,15 +465,15 @@ def _layout(model: mujoco.MjModel) -> tuple[tuple[str, int], ...]:
|
|
|
284
465
|
return tuple((name, int(getattr(model, name))) for name in _LAYOUT_SCALARS if hasattr(model, name))
|
|
285
466
|
|
|
286
467
|
|
|
287
|
-
def _validate_layout(
|
|
468
|
+
def _validate_layout(snapshots: Sequence[_VariantSnapshot], canonical: mujoco.MjModel) -> None:
|
|
288
469
|
canonical_layout = dict(_layout(canonical))
|
|
289
|
-
for variant,
|
|
290
|
-
for name, value in
|
|
470
|
+
for variant, snapshot in enumerate(snapshots):
|
|
471
|
+
for name, value in snapshot.layout:
|
|
291
472
|
if value != canonical_layout[name]:
|
|
292
473
|
raise ValueError(
|
|
293
474
|
f"variant {variant} changes layout field {name}: {value} != {canonical_layout[name]}"
|
|
294
475
|
)
|
|
295
|
-
if
|
|
476
|
+
if snapshot.ngeom > canonical.ngeom:
|
|
296
477
|
raise ValueError(f"variant {variant} has more geoms than the canonical model")
|
|
297
478
|
|
|
298
479
|
|
|
@@ -302,7 +483,7 @@ def _names(model: mujoco.MjModel, kind: str, count: int) -> list[str]:
|
|
|
302
483
|
|
|
303
484
|
|
|
304
485
|
def _validate_names_and_build_geom_maps(
|
|
305
|
-
|
|
486
|
+
snapshots: Sequence[_VariantSnapshot], canonical: mujoco.MjModel
|
|
306
487
|
) -> list[np.ndarray]:
|
|
307
488
|
canonical_geoms = _names(canonical, "geom", canonical.ngeom)
|
|
308
489
|
if len(set(canonical_geoms)) != len(canonical_geoms) or "" in canonical_geoms:
|
|
@@ -311,16 +492,15 @@ def _validate_names_and_build_geom_maps(
|
|
|
311
492
|
canonical_by_kind = {
|
|
312
493
|
kind: _names(canonical, kind, getattr(canonical, count)) for kind, count in _ENTITY_KIND_COUNTS
|
|
313
494
|
}
|
|
314
|
-
count_by_kind = dict(_ENTITY_KIND_COUNTS)
|
|
315
495
|
canonical_geom_ids = {name: i for i, name in enumerate(canonical_geoms)}
|
|
316
496
|
|
|
317
497
|
maps: list[np.ndarray] = []
|
|
318
|
-
for variant,
|
|
498
|
+
for variant, snapshot in enumerate(snapshots):
|
|
319
499
|
for kind, expected in canonical_by_kind.items():
|
|
320
|
-
actual =
|
|
500
|
+
actual = snapshot.entity_names[kind]
|
|
321
501
|
if actual != expected:
|
|
322
502
|
raise ValueError(f"variant {variant} changes {kind} names or order")
|
|
323
|
-
names =
|
|
503
|
+
names = snapshot.entity_names["geom"]
|
|
324
504
|
if len(set(names)) != len(names) or "" in names:
|
|
325
505
|
raise ValueError(f"variant {variant} geoms must have unique, non-empty names")
|
|
326
506
|
unknown = set(names) - set(canonical_geoms)
|
|
@@ -331,7 +511,7 @@ def _validate_names_and_build_geom_maps(
|
|
|
331
511
|
|
|
332
512
|
|
|
333
513
|
def _validate_shared_parameters(
|
|
334
|
-
|
|
514
|
+
snapshots: Sequence[_VariantSnapshot],
|
|
335
515
|
canonical: mujoco.MjModel,
|
|
336
516
|
geom_maps: Sequence[np.ndarray],
|
|
337
517
|
) -> None:
|
|
@@ -348,10 +528,10 @@ def _validate_shared_parameters(
|
|
|
348
528
|
and name not in _ALLOWED_GEOM_FIELDS
|
|
349
529
|
and name not in _ALLOWED_DERIVED_FIELDS
|
|
350
530
|
)
|
|
351
|
-
for variant, (
|
|
531
|
+
for variant, (snapshot, geom_map) in enumerate(zip(snapshots, geom_maps, strict=True)):
|
|
352
532
|
for name in shared_names:
|
|
353
533
|
expected = getattr(canonical, name, None)
|
|
354
|
-
actual =
|
|
534
|
+
actual = snapshot.shared.get(name)
|
|
355
535
|
if not isinstance(expected, np.ndarray) or not isinstance(actual, np.ndarray):
|
|
356
536
|
continue
|
|
357
537
|
if expected.shape != actual.shape:
|
|
@@ -363,7 +543,7 @@ def _validate_shared_parameters(
|
|
|
363
543
|
|
|
364
544
|
def _disable_simple_where_variants_break_inertial_frame(
|
|
365
545
|
canonical_spec: mujoco.MjSpec,
|
|
366
|
-
|
|
546
|
+
snapshots: Sequence[_VariantSnapshot],
|
|
367
547
|
canonical_index: int,
|
|
368
548
|
) -> None:
|
|
369
549
|
"""Force the general (non-simple) code path where variant inertia requires it.
|
|
@@ -377,7 +557,7 @@ def _disable_simple_where_variants_break_inertial_frame(
|
|
|
377
557
|
``simple=False`` is a codegen hint only; the compiled dynamics are
|
|
378
558
|
numerically identical.
|
|
379
559
|
"""
|
|
380
|
-
canonical =
|
|
560
|
+
canonical = snapshots[canonical_index]
|
|
381
561
|
# MjSpec.bodies is the flattened depth-first list, aligned with the compiled
|
|
382
562
|
# model's body ids (world first in both).
|
|
383
563
|
spec_bodies = list(canonical_spec.bodies)[1:]
|
|
@@ -391,23 +571,20 @@ def _disable_simple_where_variants_break_inertial_frame(
|
|
|
391
571
|
for spec_body, body_id in zip(spec_bodies, range(1, canonical.nbody), strict=True):
|
|
392
572
|
if not canonical.body_simple[body_id]:
|
|
393
573
|
continue
|
|
394
|
-
for
|
|
395
|
-
if not np.array_equal(
|
|
396
|
-
|
|
574
|
+
for snapshot in snapshots:
|
|
575
|
+
if not np.array_equal(snapshot.fields["body_ipos"][body_id], zero) or not np.array_equal(
|
|
576
|
+
snapshot.fields["body_iquat"][body_id], identity
|
|
397
577
|
):
|
|
398
578
|
spec_body.simple = False
|
|
399
579
|
break
|
|
400
580
|
|
|
401
581
|
|
|
402
|
-
def _validate_shared_options(
|
|
403
|
-
references: Sequence[mujoco.MjModel],
|
|
404
|
-
canonical: mujoco.MjModel,
|
|
405
|
-
) -> None:
|
|
582
|
+
def _validate_shared_options(snapshots: Sequence[_VariantSnapshot], canonical: mujoco.MjModel) -> None:
|
|
406
583
|
option_names = tuple(name for name in dir(canonical.opt) if not name.startswith("_") and name != "timestep")
|
|
407
|
-
for variant,
|
|
584
|
+
for variant, snapshot in enumerate(snapshots):
|
|
408
585
|
for name in option_names:
|
|
409
586
|
expected = getattr(canonical.opt, name, None)
|
|
410
|
-
actual =
|
|
587
|
+
actual = snapshot.options.get(name)
|
|
411
588
|
if isinstance(expected, np.ndarray) and isinstance(actual, np.ndarray):
|
|
412
589
|
same = np.array_equal(expected, actual)
|
|
413
590
|
elif isinstance(expected, (bool, int, float)) and isinstance(actual, (bool, int, float)):
|
|
@@ -419,17 +596,16 @@ def _validate_shared_options(
|
|
|
419
596
|
|
|
420
597
|
|
|
421
598
|
def _build_mesh_id_maps(
|
|
422
|
-
|
|
599
|
+
snapshots: Sequence[_VariantSnapshot],
|
|
423
600
|
canonical: mujoco.MjModel,
|
|
424
|
-
specs: Sequence[mujoco.MjSpec],
|
|
425
601
|
mesh_names_by_variant: Sequence[Mapping[str, str]],
|
|
426
602
|
) -> list[dict[int, int]]:
|
|
427
603
|
maps: list[dict[int, int]] = []
|
|
428
|
-
for
|
|
604
|
+
for snapshot, mesh_names in zip(snapshots, mesh_names_by_variant, strict=True):
|
|
429
605
|
mesh_ids: dict[int, int] = {}
|
|
430
|
-
for
|
|
431
|
-
reference_id =
|
|
432
|
-
pooled_name = mesh_names[
|
|
606
|
+
for record in snapshot.meshes:
|
|
607
|
+
reference_id = snapshot.mesh_ids[record.name]
|
|
608
|
+
pooled_name = mesh_names[record.name]
|
|
433
609
|
mesh_ids[reference_id] = canonical.mesh(pooled_name).id
|
|
434
610
|
maps.append(mesh_ids)
|
|
435
611
|
return maps
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|