mjbatch-uni 0.2.3__tar.gz → 0.2.5__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.
@@ -17,45 +17,21 @@ execute_process(
17
17
  OUTPUT_VARIABLE MUJOCO_PKG_DIR
18
18
  COMMAND_ERROR_IS_FATAL ANY)
19
19
  if(WIN32)
20
- set(MUJOCO_DLL "${MUJOCO_PKG_DIR}/mujoco.dll")
21
- if(NOT EXISTS "${MUJOCO_DLL}")
22
- message(FATAL_ERROR "Could not find mujoco.dll in ${MUJOCO_PKG_DIR}")
23
- endif()
24
-
25
- # The MuJoCo wheel exports the C API from mujoco.dll but does not ship an
26
- # MSVC import library. Generate one at build time; it is not installed.
27
- get_filename_component(MSVC_TOOL_BIN_DIR "${CMAKE_CXX_COMPILER}" DIRECTORY)
28
- find_program(MSVC_LIB_EXECUTABLE NAMES lib HINTS "${MSVC_TOOL_BIN_DIR}")
29
- if(NOT MSVC_LIB_EXECUTABLE)
30
- message(FATAL_ERROR "Could not find MSVC lib.exe")
31
- endif()
32
- find_program(MSVC_DUMPBIN_EXECUTABLE NAMES dumpbin HINTS "${MSVC_TOOL_BIN_DIR}")
33
- if(NOT MSVC_DUMPBIN_EXECUTABLE)
34
- message(FATAL_ERROR "Could not find MSVC dumpbin.exe")
35
- endif()
36
-
20
+ # The Windows wheel ships mujoco.dll without an import library, so make one from
21
+ # the DLL's export table.
37
22
  execute_process(
38
- COMMAND "${MSVC_DUMPBIN_EXECUTABLE}" "/EXPORTS" "${MUJOCO_DLL}"
23
+ COMMAND "${CMAKE_LINKER}" /dump /exports "${MUJOCO_PKG_DIR}/mujoco.dll"
39
24
  OUTPUT_VARIABLE MUJOCO_EXPORTS
40
25
  COMMAND_ERROR_IS_FATAL ANY)
41
- string(REGEX MATCHALL
42
- " +[0-9]+ +[0-9A-Fa-f]+ +[0-9A-Fa-f]+ +([^ \r\n]+)"
43
- MUJOCO_EXPORT_MATCHES "${MUJOCO_EXPORTS}")
44
- if(NOT MUJOCO_EXPORT_MATCHES)
45
- message(FATAL_ERROR "Could not parse exports from ${MUJOCO_DLL}")
46
- endif()
47
-
48
- set(MUJOCO_DEF_FILE "${CMAKE_CURRENT_BINARY_DIR}/mujoco.def")
49
- file(WRITE "${MUJOCO_DEF_FILE}" "LIBRARY mujoco.dll\nEXPORTS\n")
50
- foreach(export_match IN LISTS MUJOCO_EXPORT_MATCHES)
51
- string(REGEX REPLACE ".* +([^ \r\n]+)$" "\\1" export_symbol "${export_match}")
52
- file(APPEND "${MUJOCO_DEF_FILE}" " ${export_symbol}\n")
53
- endforeach()
54
-
26
+ string(REGEX MATCHALL "[0-9]+ +[0-9A-F]+ +[0-9A-F]+ +[A-Za-z_][A-Za-z0-9_]*"
27
+ MUJOCO_EXPORTS "${MUJOCO_EXPORTS}")
28
+ list(TRANSFORM MUJOCO_EXPORTS REPLACE "^.* " "")
29
+ list(JOIN MUJOCO_EXPORTS "\n" MUJOCO_EXPORTS)
30
+ file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/mujoco.def" "EXPORTS\n${MUJOCO_EXPORTS}\n")
55
31
  set(MUJOCO_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/mujoco.lib")
56
32
  execute_process(
57
- COMMAND "${MSVC_LIB_EXECUTABLE}" "/def:${MUJOCO_DEF_FILE}" "/machine:x64"
58
- "/out:${MUJOCO_LIBRARY}"
33
+ COMMAND "${CMAKE_AR}" /nologo /machine:x64 /name:mujoco.dll
34
+ "/def:${CMAKE_CURRENT_BINARY_DIR}/mujoco.def" "/out:${MUJOCO_LIBRARY}"
59
35
  COMMAND_ERROR_IS_FATAL ANY)
60
36
  else()
61
37
  file(GLOB MUJOCO_LIBRARY "${MUJOCO_PKG_DIR}/libmujoco*${CMAKE_SHARED_LIBRARY_SUFFIX}*")
@@ -66,17 +42,16 @@ else()
66
42
  endif()
67
43
 
68
44
  nanobind_add_module(_bindings FREE_THREADED NOMINSIZE src/mjbatch/csrc/bindings.cpp)
69
- if(WIN32)
70
- # Windows headers are nested one directory deeper than the Unix wheels.
71
- target_include_directories(_bindings PRIVATE "${MUJOCO_PKG_DIR}/include/mujoco")
72
- else()
73
- target_include_directories(_bindings PRIVATE "${MUJOCO_PKG_DIR}/include")
74
- endif()
45
+ # The Windows wheel nests the headers one directory deeper.
46
+ find_path(MUJOCO_INCLUDE_DIR mujoco/mujoco.h REQUIRED NO_DEFAULT_PATH
47
+ PATHS "${MUJOCO_PKG_DIR}/include" "${MUJOCO_PKG_DIR}/include/mujoco")
48
+ target_include_directories(_bindings PRIVATE "${MUJOCO_INCLUDE_DIR}")
75
49
  target_link_libraries(_bindings PRIVATE "${MUJOCO_LIBRARY}")
76
50
 
77
51
  # Find libmujoco next to the installed extension, <site-packages>/mujoco. The
78
52
  # macOS wheel's install name is a framework path that does not exist, so point
79
- # our load command at the plain file name instead.
53
+ # our load command at the plain file name instead. Windows has no rpath: the
54
+ # package adds that directory to the DLL search path before importing us.
80
55
  if(APPLE)
81
56
  get_filename_component(MUJOCO_LIB_NAME "${MUJOCO_LIBRARY}" NAME)
82
57
  execute_process(COMMAND otool -D "${MUJOCO_LIBRARY}" OUTPUT_VARIABLE _otool)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mjbatch-uni
3
- Version: 0.2.3
3
+ Version: 0.2.5
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>
@@ -42,7 +42,7 @@ Features include:
42
42
  * Per-simulation model parameters, with `expand` for MjModel fields and `set_const` to recompute derived constants.
43
43
  * Same-layout compiler-coherent mesh variants, with `VariantPack.from_specs()` and `Batch.from_variant_pack()`.
44
44
  * Explicit topology-affine groups, with `ModelAffineBatch` routing global ids to incompatible layouts.
45
- * Batched queries beyond stepping: site Jacobians with `jac_site` and heightfield sampling with `sample_hfield` (world/yaw grid alignment); query ops return caller-allocated results without refreshing the bound views.
45
+ * Batched queries beyond stepping: ray casting with `rays`, body-point Jacobians with `jac`, and heightfield sampling with `sample_hfield` (world/yaw grid alignment); query ops return caller-allocated results without refreshing the bound views.
46
46
  * Per-substep control from Python with `step(..., callback=...)`.
47
47
  * Opt-in split substeps with fresh position/velocity sensor views for body wrench control.
48
48
  * Optional per-worker CPU pinning on Linux, with `cpu_ids` binding pool worker `i` to `cpu_ids[i]`.
@@ -12,7 +12,7 @@ Features include:
12
12
  * Per-simulation model parameters, with `expand` for MjModel fields and `set_const` to recompute derived constants.
13
13
  * Same-layout compiler-coherent mesh variants, with `VariantPack.from_specs()` and `Batch.from_variant_pack()`.
14
14
  * Explicit topology-affine groups, with `ModelAffineBatch` routing global ids to incompatible layouts.
15
- * Batched queries beyond stepping: site Jacobians with `jac_site` and heightfield sampling with `sample_hfield` (world/yaw grid alignment); query ops return caller-allocated results without refreshing the bound views.
15
+ * Batched queries beyond stepping: ray casting with `rays`, body-point Jacobians with `jac`, and heightfield sampling with `sample_hfield` (world/yaw grid alignment); query ops return caller-allocated results without refreshing the bound views.
16
16
  * Per-substep control from Python with `step(..., callback=...)`.
17
17
  * Opt-in split substeps with fresh position/velocity sensor views for body wrench control.
18
18
  * Optional per-worker CPU pinning on Linux, with `cpu_ids` binding pool worker `i` to `cpu_ids[i]`.
@@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"
4
4
 
5
5
  [project]
6
6
  name = "mjbatch-uni"
7
- version = "0.2.3"
7
+ version = "0.2.5"
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"
@@ -80,7 +80,7 @@ testpaths = ["tests"]
80
80
 
81
81
  [tool.cibuildwheel]
82
82
  build = "cp310-* cp311-* cp312-* cp313-*"
83
- skip = "*-musllinux_* *_i686"
83
+ skip = "*-musllinux_* *-win32 *_i686"
84
84
  test-requires = "pytest"
85
85
  test-command = "pytest {project}/tests"
86
86
 
@@ -1,21 +1,29 @@
1
1
  # SPDX-License-Identifier: Apache-2.0
2
2
 
3
+ import os
4
+ import sys
3
5
  from collections.abc import Sequence
4
6
  from contextlib import contextmanager
5
7
  from dataclasses import dataclass
6
8
  from functools import cached_property
9
+ from pathlib import Path
7
10
  from typing import Any, Iterator, Mapping
8
11
 
9
12
  import mujoco
10
13
  import numpy as np
11
14
 
12
- from mjbatch._bindings import Batch as _Batch
15
+ if sys.platform == "win32":
16
+ # The extension links mujoco.dll, which lives in the mujoco package.
17
+ os.add_dll_directory(str(Path(mujoco.__file__).parent))
18
+
19
+ from mjbatch._bindings import Batch as _Batch # noqa: E402
13
20
  from mjbatch.groups import ModelAffineBatch as ModelAffineBatch
14
21
  from mjbatch.groups import TopologyGroup as TopologyGroup
15
22
  from mjbatch.model import ModelFieldSpec as ModelFieldSpec
16
23
  from mjbatch.model import RecomputeLevel as RecomputeLevel
17
24
  from mjbatch.model import build_model_fields
18
- from mjbatch.variants import VariantPack
25
+ from mjbatch.variants import VariantPack as VariantPack
26
+ from mjbatch.variants import VariantPackBuilder as VariantPackBuilder
19
27
 
20
28
  _QPOS_WIDTH = {0: 7, 1: 4, 2: 1, 3: 1} # by mjtJoint: free, ball, slide, hinge
21
29
  _DOF_WIDTH = {0: 6, 1: 3, 2: 1, 3: 1}
@@ -195,20 +203,6 @@ class Batch(_Batch):
195
203
  ids = np.asarray(ids)
196
204
  return int(ids.sum()) if ids.dtype == bool else len(ids)
197
205
 
198
- def jac_site( # pyright: ignore[reportIncompatibleMethodOverride] # name-based allocating wrapper
199
- self, name: str, ids: Any = None
200
- ) -> tuple[np.ndarray, np.ndarray]:
201
- """World-frame position/rotation Jacobians of a site, per selected simulation.
202
-
203
- Returns (jacp, jacr) with shape (nsel, 3, nv). Runs kinematics and comPos
204
- only, not mj_forward, and does not refresh the bound views."""
205
- i = self.model.site(name).id
206
- n = self._nsel(ids)
207
- jacp = np.zeros((n, 3, self.model.nv))
208
- jacr = np.zeros((n, 3, self.model.nv))
209
- super().jac_site(i, jacp, jacr, ids)
210
- return jacp, jacr
211
-
212
206
  def sample_hfield( # pyright: ignore[reportIncompatibleMethodOverride] # name-based allocating wrapper
213
207
  self,
214
208
  geom: str,
@@ -1,6 +1,7 @@
1
1
  from collections.abc import Callable, Sequence
2
2
  from typing import Annotated
3
3
 
4
+ import numpy
4
5
  from numpy.typing import NDArray
5
6
 
6
7
 
@@ -18,19 +19,23 @@ class Batch:
18
19
  bind("state") returns the (N, nstate) integration states themselves, in
19
20
  mj_getState's mjSTATE_INTEGRATION order: opaque rows for copying, saving and
20
21
  restoring simulations, native dtype only. A row written is the simulation's
21
- state at its next call, with pending field writes applied on top; the field
22
- views are stale until then. reset discards it, as it discards a field write.
22
+ state at its next call, with pending writes to copied fields (below) applied on
23
+ top. reset applies it element by element where it changed, like a field write.
23
24
 
24
25
  bind(field) returns an (N, ...) array over an mjData field in MjData's layout.
25
- Input fields (the mjtState components and warning) are copied in before
26
- a physics call, element by element, where they changed since we last wrote
27
- them; every bound field is copied out after, and a derived field bound between
28
- calls is filled for a simulation by its next call. reset applies pending writes
29
- after mj_resetData and then runs mj_forward, so derived fields are valid. After
30
- step, sensordata and every derived field are one substep behind qpos and qvel,
31
- as with mj_step itself, unless the batch was made with forward=True, which ends
32
- every step with mj_forward so they are current, at the cost of one forward per
33
- simulation per call.
26
+ An mjtNum state component bound in its native dtype (qpos, qvel, ctrl,
27
+ mocap_pos, ...) is a strided view into the state rows, so a write through it and
28
+ a write to the row are one write, the later winning. The other input fields
29
+ (eq_active, warning, and float32 bindings) are copies, copied in before a
30
+ physics call, element by element, where they changed since we last wrote them;
31
+ after a state write they are stale until the next call, and a write of the
32
+ stale value goes unseen. Every bound field is copied out after a call, and a
33
+ derived field bound between calls is filled for a simulation by its next call.
34
+ reset applies pending writes after mj_resetData and then runs mj_forward, so
35
+ derived fields are valid. After step, sensordata and every derived field are
36
+ one substep behind qpos and qvel, as with mj_step itself, unless the batch was
37
+ made with forward=True, which ends every step with mj_forward so they are
38
+ current, at the cost of one forward per simulation per call.
34
39
 
35
40
  expand(field) returns (N, ...) per-simulation values of an mjModel field, seeded
36
41
  from the model and applied before every physics call for that simulation.
@@ -49,6 +54,11 @@ class Batch:
49
54
  the first failing simulation; the others still ran, and the failing one keeps the
50
55
  state it had before the call, its writes still pending. The trap is a MuJoCo log
51
56
  handler installed at import; installing another handler later disables it.
57
+
58
+ rays and jac are queries: mj_ray and mj_jac for each simulation against its own
59
+ geometry, which is its state with pending writes on top and its expanded model
60
+ fields. A query runs only the part of the pipeline it needs, consumes no pending
61
+ write, and updates no bound field.
52
62
  """
53
63
 
54
64
  def __init__(self, model: object, num_sims: int, num_threads: int = 0, forward: bool = False, cpu_ids: Sequence[int] | None = None) -> None:
@@ -95,14 +105,19 @@ class Batch:
95
105
  mj_resetData, or mj_resetDataKeyframe when keyframe >= 0, then mj_forward.
96
106
  """
97
107
 
98
- def jac_site(self, site: int, jacp: NDArray | None = None, jacr: NDArray | None = None, ids: Annotated[NDArray, dict(shape=(None,), order='C')] | None = None) -> None:
99
- """
100
- mj_jacSite per selected simulation into caller-allocated (sel, 3, nv) rows; either output may be None. Runs mj_kinematics and mj_comPos only, not mj_forward, and does not refresh the bound views: the outputs are the caller-allocated rows.
101
- """
102
-
103
108
  def sample_hfield(self, geom: int, body: int, offsets: NDArray, out: NDArray, ids: Annotated[NDArray, dict(shape=(None,), order='C')] | None = None, alignment: str = 'world') -> None:
104
109
  """
105
110
  Bilinear hfield sampling per selected simulation at XY offsets around a frame body's origin, into caller-allocated (sel, npoint) rows: the world z of the sampled hfield surface (the local elevation for an unrotated geom at the origin). alignment rotates the sampling grid: "world" keeps offsets in world axes, "yaw" rotates them by the frame body's yaw about world z. Runs mj_kinematics only, not mj_forward, and does not refresh the bound views. All simulations sample the template's hfield; a per-sim geom_pos or geom_quat moves the sampling frame.
106
111
  """
107
112
 
108
113
  def set_const(self, ids: Annotated[NDArray, dict(shape=(None,), order='C')] | None = None) -> None: ...
114
+
115
+ def rays(self, pnt: NDArray, vec: NDArray, dist: NDArray, geomid: NDArray | None = None, normal: NDArray | None = None, geomgroup: Annotated[NDArray[numpy.uint8], dict(shape=(6), order='C', writable=False)] | None = None, flg_static: bool = True, bodyexclude: Annotated[NDArray[numpy.int32], dict(shape=(None,), order='C', writable=False)] | None = None, ids: Annotated[NDArray, dict(shape=(None,), order='C')] | None = None) -> None:
116
+ """
117
+ mj_ray for every ray of every simulation. pnt and vec are (num_sims, nray, 3) origins and directions in the world frame, float32 or the native dtype. dist (num_sims, nray), and the optional geomid (int32) and normal (num_sims, nray, 3), are caller-allocated and filled in place: dist is -1 and normal zero where a ray hits nothing. geomgroup is mj_ray's six-entry uint8 mask, bodyexclude one int32 body id per ray (-1 for none), and ids restricts which rows are computed.
118
+ """
119
+
120
+ def jac(self, jacp: NDArray | None, jacr: NDArray | None, point: NDArray, body: Annotated[NDArray[numpy.int32], dict(shape=(None,), order='C', writable=False)], ids: Annotated[NDArray, dict(shape=(None,), order='C')] | None = None) -> None:
121
+ """
122
+ mj_jac for one point per simulation. point is (num_sims, 3) in the world frame, float32 or the native dtype, and body (num_sims,) int32 is the body it moves with. jacp and jacr are caller-allocated (num_sims, 3, nv) arrays filled in place with the translational and rotational Jacobians; either may be None. ids restricts which rows are computed.
123
+ """