Repository navigation
[Warp] Part 2b/8: Reset by mask in Warp kernels where main uses torch #8997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
3c8340f
660eeb2
1a3ef00
a40fdc8
4b9dd92
9ad50a6
09d7e81
b1b963f
1cbb382
61c5ed5
abd4a77
e7c807e
19701d9
70c6ef8
6c27eb3
0ef4971
4634d78
b583572
19d9aab
6f4030a
d98b83c
e5c4d2f
3a8360a
5d49b8d
83b89b8
ec5dc49
ac3553d
55d930d
e07b99f
8f2987c
b052598
beb29c1
b4175b1
46c853c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| * Added :func:`~isaaclab.utils.warp.kernels.initialize_rng_state`, which seeds one Warp random number generator | ||
| state per environment for kernels that draw per-environment random numbers. | ||
| * Added :func:`~isaaclab.utils.io.load_onnx_model`, which reads an ONNX model file, and | ||
| :func:`~isaaclab.utils.io.export_torchscript_to_onnx`, which exports a TorchScript module to an ONNX model in memory. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| * Changed :class:`~isaaclab.actuators.ActuatorNetLSTM` and :class:`~isaaclab.actuators.ActuatorNetMLP` to reset by a | ||
| Warp kernel when given an environment mask, so a CUDA graph can record the reset. | ||
| * Changed :class:`~isaaclab.actuators.ActuatorNetLSTM` and :class:`~isaaclab.actuators.ActuatorNetMLP` to run their | ||
| networks with Warp-NN, a new dependency. The network file may be an ONNX model whose inputs and outputs carry the | ||
| names the actuator looks up (the LSTM's ``input``, ``h0`` and ``c0``, and ``torque``, ``h1`` and ``c1``; the MLP's | ||
| single input and output under any names), or a TorchScript module, which is exported to ONNX when the actuator loads. ``network`` is now the | ||
| Warp-NN runtime instead of the TorchScript module. Warp kernels write the network input (for the MLP, after | ||
| updating the joint history) and scale the torque and clip it to the DC motor limits; the LSTM's hidden and cell | ||
| states carry over in Warp buffers, whose Torch views keep the ``sea_*`` attributes. A Warp graph alone can therefore | ||
| record the whole torque computation, and both actuators are now :attr:`~isaaclab.actuators.ActuatorBase.capturable`. | ||
| * Changed the network actuators to reject a network that uses an operator Warp-NN does not support, with an error | ||
| that names the network file and the operator; Torch ran such networks before. A common case is an LSTM head that | ||
| takes its time step by indexing, ``out[:, -1]``, which exports to ``Gather``: take the step with ``out.squeeze(1)`` | ||
| and save the network again. | ||
| * Changed :class:`~isaaclab.actuators.DCMotor` to clip its effort in the Warp kernel that the network actuators use; | ||
| its ``applied_effort`` is now a view of a preallocated buffer that every step writes in place. | ||
| * Changed the ray-caster drift resampling on reset to draw from per-environment Warp random states, so a reset | ||
| recorded in a CUDA graph resamples the drift of the selected environments on every replay. The states are seeded | ||
| once, when the sensor initializes, from the environment seed and the sensor's prim path and offset: ray casters on | ||
| one prim at different offsets draw different drift, and a sensor draws the same drift in every process with the | ||
| same seed. A later ``env.reset(seed=...)`` does not restart the drift sequence. Drift no longer draws from Torch's | ||
| generator. The drift ranges are unchanged; the drawn values differ. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
| # | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
|
|
||
| """Warp kernels used by actuator collections.""" | ||
| """Warp kernels used by actuators and actuator collections.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
|
|
@@ -14,6 +14,10 @@ | |
|
|
||
| from ..utils.warp.index_kernel import IndexKernelDispatcher | ||
|
|
||
| """ | ||
| Actuator collection kernels. | ||
| """ | ||
|
|
||
|
|
||
| @wp.kernel(enable_backward=False) | ||
| def write_2d_float_with_indices( | ||
|
|
@@ -144,3 +148,140 @@ def compute_implicit_actuator_batch( | |
| computed_effort[env_id, joint_id] = effort | ||
| applied_effort[env_id, joint_id] = clamped_effort | ||
| soft_velocity_limit[env_id, joint_id] = velocity_limit[env_id, batch_joint_id] | ||
|
|
||
|
|
||
| """ | ||
| DC motor and network actuator kernels. | ||
| """ | ||
|
|
||
|
|
||
| @wp.func | ||
| def dc_motor_clip( | ||
| effort: wp.float32, | ||
| joint_vel: wp.float32, | ||
| saturation_effort: wp.float32, | ||
| vel_at_effort_lim: wp.float32, | ||
| velocity_limit: wp.float32, | ||
| effort_limit: wp.float32, | ||
| ) -> wp.float32: | ||
| """Clip an effort to the DC motor's torque-speed limits (see :class:`~isaaclab.actuators.DCMotor`).""" | ||
| vel = wp.min(wp.max(joint_vel, -vel_at_effort_lim), vel_at_effort_lim) | ||
| max_effort = wp.min(saturation_effort * (1.0 - vel / velocity_limit), effort_limit) | ||
| min_effort = wp.max(saturation_effort * (-1.0 - vel / velocity_limit), -effort_limit) | ||
| return wp.min(wp.max(effort, min_effort), max_effort) | ||
|
|
||
|
|
||
| @wp.kernel(enable_backward=False) | ||
| def clip_dc_motor_effort( | ||
| # input | ||
| effort: wp.array2d(dtype=wp.float32), | ||
| joint_vel: wp.array2d(dtype=wp.float32), | ||
| saturation_effort: wp.array2d(dtype=wp.float32), | ||
| vel_at_effort_lim: wp.array2d(dtype=wp.float32), | ||
| velocity_limit: wp.array2d(dtype=wp.float32), | ||
| effort_limit: wp.array2d(dtype=wp.float32), | ||
| # output | ||
| applied_effort: wp.array2d(dtype=wp.float32), | ||
| ): | ||
| """Clip the effort of each (env, joint) to the DC motor limits. Launch with ``dim=(num_envs, num_joints)``.""" | ||
| env_id, joint = wp.tid() | ||
| applied_effort[env_id, joint] = dc_motor_clip( | ||
| effort[env_id, joint], | ||
| joint_vel[env_id, joint], | ||
| saturation_effort[env_id, joint], | ||
| vel_at_effort_lim[env_id, joint], | ||
| velocity_limit[env_id, joint], | ||
| effort_limit[env_id, joint], | ||
| ) | ||
|
|
||
|
|
||
| @wp.kernel(enable_backward=False) | ||
| def scale_and_clip_torque( | ||
| # input | ||
| torque: wp.array(dtype=wp.float32), | ||
| joint_vel: wp.array2d(dtype=wp.float32), | ||
| torque_scale: wp.float32, | ||
| saturation_effort: wp.array2d(dtype=wp.float32), | ||
| vel_at_effort_lim: wp.array2d(dtype=wp.float32), | ||
| velocity_limit: wp.array2d(dtype=wp.float32), | ||
| effort_limit: wp.array2d(dtype=wp.float32), | ||
| # output | ||
| computed_effort: wp.array2d(dtype=wp.float32), | ||
| applied_effort: wp.array2d(dtype=wp.float32), | ||
| ): | ||
| """Scale the network torque of each (env, joint) and clip it to the DC motor limits. | ||
|
|
||
| ``torque`` holds one value per (env, joint), row-major. Launch with ``dim=(num_envs, num_joints)``. | ||
| """ | ||
| env_id, joint = wp.tid() | ||
| effort = torque[env_id * computed_effort.shape[1] + joint] * torque_scale | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The flat index is rebuilt by hand here, but
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried it: the network buffers are one row per (env, joint) by design (the ONNX LSTM has a single batch axis, the MLP starts with a Gemm), so 2-D indexing needed a second view of the same memory. I kept the row index. |
||
| computed_effort[env_id, joint] = effort | ||
| applied_effort[env_id, joint] = dc_motor_clip( | ||
| effort, | ||
| joint_vel[env_id, joint], | ||
| saturation_effort[env_id, joint], | ||
| vel_at_effort_lim[env_id, joint], | ||
| velocity_limit[env_id, joint], | ||
| effort_limit[env_id, joint], | ||
| ) | ||
|
|
||
|
|
||
| @wp.kernel(enable_backward=False) | ||
| def write_lstm_input( | ||
| # input | ||
| joint_pos_target: wp.array2d(dtype=wp.float32), | ||
| joint_pos: wp.array2d(dtype=wp.float32), | ||
| joint_vel: wp.array2d(dtype=wp.float32), | ||
| # output | ||
| network_input: wp.array3d(dtype=wp.float32), | ||
| ): | ||
| """Write the joint position error and velocity of each (env, joint) as its network input row. | ||
|
|
||
| Launch with ``dim=(num_envs, num_joints)``. | ||
| """ | ||
| env_id, joint = wp.tid() | ||
| row = env_id * joint_pos.shape[1] + joint | ||
| network_input[row, 0, 0] = joint_pos_target[env_id, joint] - joint_pos[env_id, joint] | ||
| network_input[row, 0, 1] = joint_vel[env_id, joint] | ||
|
|
||
|
|
||
| @wp.func | ||
| def _push_newest(history: wp.array3d(dtype=wp.float32), env_id: int, joint: int, sample: wp.float32): | ||
| """Move the history of one (env, joint) back by one step and write ``sample`` as its newest entry.""" | ||
| for t in range(history.shape[1] - 1): | ||
| k = history.shape[1] - 1 - t | ||
| history[env_id, k, joint] = history[env_id, k - 1, joint] | ||
| history[env_id, 0, joint] = sample | ||
|
|
||
|
|
||
| @wp.kernel(enable_backward=False) | ||
| def update_mlp_history( | ||
| # input | ||
| joint_pos_target: wp.array2d(dtype=wp.float32), | ||
| joint_pos: wp.array2d(dtype=wp.float32), | ||
| joint_vel: wp.array2d(dtype=wp.float32), | ||
| history_indices: wp.array(dtype=wp.int32), | ||
| pos_scale: wp.float32, | ||
| vel_scale: wp.float32, | ||
| pos_first: bool, | ||
| # output | ||
| joint_pos_error_history: wp.array3d(dtype=wp.float32), | ||
| joint_vel_history: wp.array3d(dtype=wp.float32), | ||
| network_input: wp.array2d(dtype=wp.float32), | ||
| ): | ||
| """Record the newest joint position error and velocity, then write the scaled samples the network reads. | ||
|
|
||
| Each row of ``network_input`` belongs to one (env, joint): its position errors, then its velocities when | ||
| ``pos_first``, otherwise the reverse. Launch with ``dim=(num_envs, num_joints)``. | ||
| """ | ||
| env_id, joint = wp.tid() | ||
| _push_newest(joint_pos_error_history, env_id, joint, joint_pos_target[env_id, joint] - joint_pos[env_id, joint]) | ||
| _push_newest(joint_vel_history, env_id, joint, joint_vel[env_id, joint]) | ||
| num_samples = history_indices.shape[0] | ||
| row = env_id * joint_pos.shape[1] + joint | ||
| pos_column = wp.where(pos_first, 0, num_samples) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried a cached |
||
| vel_column = num_samples - pos_column | ||
| for i in range(num_samples): | ||
| index = history_indices[i] | ||
| network_input[row, pos_column + i] = pos_scale * joint_pos_error_history[env_id, index, joint] | ||
| network_input[row, vel_column + i] = vel_scale * joint_vel_history[env_id, index, joint] | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this is a second copy of
DCMotor._clip_effort. CouldDCMotoruse this function too, so the torque-speed envelope lives in one place?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in ac3553d:
DCMotor._clip_effortlaunches the samedc_motor_clipkernel into a preallocated buffer, so the envelope lives in one place (DCMotor compute also went from ~65 to ~45 µs eager).