Skip to content

Commit f408033

Browse files
authored
Merge pull request #304 from NeLy-EPFL/warp-typehints
Update type hints to be PEP 484 compliant
2 parents ba9ed22 + 1ecf8bf commit f408033

6 files changed

Lines changed: 50 additions & 42 deletions

File tree

scripts/replay_behavior_gpu.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@
7979

8080
@wp.kernel
8181
def record_joint_angles_kernel(
82-
qpos: wp.array2d(dtype=wp.float32), # type: ignore # (n_worlds, nq)
83-
qpos_adrs: wp.array(dtype=wp.int32), # type: ignore # (n_jointdofs,)
84-
step_counter: wp.array(dtype=wp.int32), # type: ignore
85-
recorded: wp.array3d(dtype=wp.float32), # type: ignore # (n_steps, n_worlds, n_dofs)
82+
qpos: wp.array2d[float], # (n_worlds, nq)
83+
qpos_adrs: wp.array[int], # (n_jointdofs,)
84+
step_counter: wp.array[int],
85+
recorded: wp.array3d[float], # (n_steps, n_worlds, n_dofs)
8686
):
8787
"""Gather this step's joint angles into a pre-allocated, GPU-resident buffer.
8888

src/flygym/compose/world/base_world.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def mjcf_root(self) -> mj.MjSpec:
8181
def fly_lookup(self) -> dict[str, BaseFly]:
8282
"""Lookup for `Fly` objects in the world, keyed by fly name."""
8383
return self._fly_lookup
84-
84+
8585
@property
8686
def fly(self) -> BaseFly:
8787
"""Get the single fly in the world.

src/flygym/simulation.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from flygym.compose.world import BaseWorld
1212
from flygym.rendering import Renderer
1313
from flygym.utils.profiling import print_perf_report
14+
from flygym.utils.typing import n_jointdofs, n_actuators, n_tendon_actuators
1415

1516

1617
class Simulation:
@@ -162,7 +163,7 @@ def render_as_needed_with_profile(self) -> bool:
162163
self._frames_rendered += 1
163164
return render_done
164165

165-
def get_joint_angles(self, fly_name: str) -> Float[np.ndarray, "n_jointdofs"]: # noqa: F821
166+
def get_joint_angles(self, fly_name: str) -> Float[np.ndarray, "n_jointdofs"]:
166167
"""Get current joint angles ordered by the fly's skeleton.
167168
168169
Args:
@@ -175,7 +176,7 @@ def get_joint_angles(self, fly_name: str) -> Float[np.ndarray, "n_jointdofs"]:
175176
internal_ids = self._intern_qposadrs_by_fly[fly_name]
176177
return self.mj_data.qpos[internal_ids]
177178

178-
def get_joint_velocities(self, fly_name: str) -> Float[np.ndarray, "n_jointdofs"]: # noqa: F821
179+
def get_joint_velocities(self, fly_name: str) -> Float[np.ndarray, "n_jointdofs"]:
179180
"""Get current joint angular velocities ordered by the fly's skeleton.
180181
181182
Args:
@@ -216,7 +217,7 @@ def get_body_rotations(self, fly_name: str) -> Float[np.ndarray, "n_bodies 4"]:
216217

217218
def get_actuator_forces(
218219
self, fly_name: str, actuator_type: ActuatorType
219-
) -> Float[np.ndarray, "n_actuators"]: # noqa: F821
220+
) -> Float[np.ndarray, "n_actuators"]:
220221
"""Get actuator forces for the given actuator type.
221222
222223
Args:
@@ -354,7 +355,7 @@ def set_actuator_inputs(
354355
self,
355356
fly_name: str,
356357
actuator_type: ActuatorType,
357-
inputs: Float[np.ndarray, "n_actuators"], # noqa: F821
358+
inputs: Float[np.ndarray, "n_actuators"],
358359
) -> None:
359360
"""Set control inputs for the given actuator type.
360361
@@ -393,7 +394,7 @@ def set_leg_adhesion_states(
393394
def set_tendon_actuator_inputs(
394395
self,
395396
fly_name: str,
396-
inputs: Float[np.ndarray, "n_tendon_actuators"], # noqa: F821
397+
inputs: Float[np.ndarray, "n_tendon_actuators"],
397398
) -> None:
398399
"""Set control inputs for tendon actuators.
399400
@@ -770,13 +771,13 @@ def close(self):
770771
self.eye_renderer = None
771772
# Don't destruct self.retina and self.eye_renderer_scene_option: they can be
772773
# reused and retina init requires some IO ops.
773-
774+
774775
@property
775776
def fly(self) -> BaseFly:
776777
"""Return the single fly in the world, or raise an error if there are multiple."""
777778
return self.world.fly
778-
779+
779780
@property
780781
def fly_lookup(self) -> dict[str, BaseFly]:
781782
"""Return the fly lookup dictionary from the world."""
782-
return self.world.fly_lookup
783+
return self.world.fly_lookup

src/flygym/utils/typing.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Canonical axis-name bindings for jaxtyping shape hints.
2+
3+
Referencing these (instead of bare string identifiers) lets pyflakes/ruff
4+
resolve the forward references inside shape strings like
5+
``Float[np.ndarray, "n_bodies"]`` instead of flagging them as undefined
6+
names (F821) — no lint ignores needed.
7+
"""
8+
9+
from typing import TypeVar
10+
11+
n_worlds = TypeVar("n_worlds")
12+
n_jointdofs = TypeVar("n_jointdofs")
13+
n_actuators = TypeVar("n_actuators")
14+
n_tendon_actuators = TypeVar("n_tendon_actuators")
15+
n_bodies = TypeVar("n_bodies")
16+
n_sites = TypeVar("n_sites")
17+
n_bodysegments = TypeVar("n_bodysegments")
18+
n_cameras = TypeVar("n_cameras")
19+
n_ommatidia = TypeVar("n_ommatidia")

src/flygym/warp/utils.py

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
@wp.kernel
99
def wp_gather_indexed_rows_3d(
10-
src: wp.array3d(dtype=wp.float32), # type: ignore
11-
dst: wp.array3d(dtype=wp.float32), # type: ignore
12-
rows: wp.array(dtype=wp.int32), # type: ignore
10+
src: wp.array3d[float], dst: wp.array3d[float], rows: wp.array[int]
1311
):
1412
"""Gather specific rows (dim 1) from a 3D Warp array into a narrower destination.
1513
@@ -30,9 +28,7 @@ def wp_gather_indexed_rows_3d(
3028

3129
@wp.kernel
3230
def wp_gather_indexed_rows_vec3f(
33-
src: wp.array2d(dtype=wp.vec3f), # type: ignore
34-
dst: wp.array3d(dtype=wp.float32), # type: ignore
35-
rows: wp.array(dtype=wp.int32), # type: ignore
31+
src: wp.array2d[wp.vec3], dst: wp.array3d[float], rows: wp.array[int]
3632
):
3733
"""Gather specific rows from a 2D ``vec3f`` array into a ``(n_worlds, n_rows_narrow, 3)``
3834
``float32`` destination.
@@ -57,9 +53,7 @@ def wp_gather_indexed_rows_vec3f(
5753

5854
@wp.kernel
5955
def wp_gather_indexed_rows_quatf(
60-
src: wp.array2d(dtype=wp.quatf), # type: ignore
61-
dst: wp.array3d(dtype=wp.float32), # type: ignore
62-
rows: wp.array(dtype=wp.int32), # type: ignore
56+
src: wp.array2d[wp.quat], dst: wp.array3d[float], rows: wp.array[int]
6357
):
6458
"""Gather specific rows from a 2D ``quatf`` array into a ``(n_worlds, n_rows_narrow, 4)``
6559
``float32`` destination.
@@ -85,9 +79,7 @@ def wp_gather_indexed_rows_quatf(
8579

8680
@wp.kernel
8781
def wp_scatter_indexed_cols_2d(
88-
src: wp.array2d(dtype=wp.float32), # type: ignore
89-
dst: wp.array2d(dtype=wp.float32), # type: ignore
90-
cols: wp.array(dtype=wp.int32), # type: ignore
82+
src: wp.array2d[float], dst: wp.array2d[float], cols: wp.array[int]
9183
):
9284
"""Scatter a 2D Warp array into specific columns of a wider destination array.
9385
@@ -108,9 +100,7 @@ def wp_scatter_indexed_cols_2d(
108100

109101
@wp.kernel
110102
def wp_gather_indexed_cols_2d(
111-
src: wp.array2d(dtype=wp.float32), # type: ignore
112-
dst: wp.array2d(dtype=wp.float32), # type: ignore
113-
cols: wp.array(dtype=wp.int32), # type: ignore
103+
src: wp.array2d[float], dst: wp.array2d[float], cols: wp.array[int]
114104
):
115105
"""Gather specific columns from a 2D Warp array into a narrower destination array.
116106
@@ -132,12 +122,12 @@ def wp_gather_indexed_cols_2d(
132122
@wp.kernel
133123
def unpack_rgb_kernel_selected_worlds_and_cameras(
134124
# In:
135-
packed: wp.array2d(dtype=wp.uint32), # type: ignore
136-
rgb_adr: wp.array(dtype=int), # type: ignore
137-
worldids_to_render: wp.array(dtype=int), # type: ignore
138-
camids_to_render: wp.array(dtype=int), # type: ignore
125+
packed: wp.array2d[wp.uint32],
126+
rgb_adr: wp.array[int],
127+
worldids_to_render: wp.array[int],
128+
camids_to_render: wp.array[int],
139129
# Out:
140-
rgb_out: wp.array4d(dtype=wp.vec3), # type: ignore
130+
rgb_out: wp.array4d[wp.vec3],
141131
):
142132
"""Unpack ABGR uint32 packed pixel data into separate R, G, and B channels."""
143133
idx_within_worldids, idx_within_camids, pixelid = wp.tid()
@@ -156,9 +146,9 @@ def unpack_rgb_kernel_selected_worlds_and_cameras(
156146

157147
def get_rgb_selected_worlds_and_cameras(
158148
rc: mjw.RenderContext,
159-
worldids: wp.array(dtype=int), # type: ignore
160-
camids: wp.array(dtype=int), # type: ignore
161-
rgb_out: wp.array4d(dtype=wp.vec3), # type: ignore
149+
worldids: wp.array[int],
150+
camids: wp.array[int],
151+
rgb_out: wp.array4d[wp.vec3],
162152
):
163153
"""Get the RGB data output from the render context buffers for the selected worlds
164154
and cameras.

src/flygym_demo/benchmark/time_gpu_simulation.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ def make_target_angles_all_worlds(self, n_worlds: int, sim_steps: int):
8888

8989
@wp.kernel
9090
def update_target_angles_kernel(
91-
dof_angles_all_worlds_gpu: wp.array3d(dtype=wp.float32), # type: ignore
92-
step_counter_gpu: wp.array(dtype=wp.int32), # type: ignore
93-
curr_target_angles_gpu: wp.array2d(dtype=wp.float32), # type: ignore
91+
dof_angles_all_worlds_gpu: wp.array3d[float],
92+
step_counter_gpu: wp.array[int],
93+
curr_target_angles_gpu: wp.array2d[float],
9494
):
9595
world_id, actuator_id = wp.tid()
9696
step = step_counter_gpu[0]
@@ -99,9 +99,7 @@ def update_target_angles_kernel(
9999

100100

101101
@wp.kernel
102-
def increment_counter_kernel(
103-
step_counter_gpu: wp.array(dtype=wp.int32), # type: ignore
104-
):
102+
def increment_counter_kernel(step_counter_gpu: wp.array[int]):
105103
step_counter_gpu[0] = step_counter_gpu[0] + 1
106104

107105

0 commit comments

Comments
 (0)