-
Notifications
You must be signed in to change notification settings - Fork 13
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
Optimize performance and memory usage in dynamics computations #280
Conversation
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.
Leaving a preliminary comment before going more in detail with a review.
Do you have any benchmark supporting the need to switch from names to indices? Using indices makes the code much more fragile and, if the indexing logic changes, there will be countless of places to update. The name-to-index helpers provide a centralized location to store this logic.
A very descriptive example of what I just said is the joint name to index map. There is a tricky +- 1 index, that at first sight this PR lacks. I'm not that supportive of gaining minor performance benefits if the price to pay is less maintainable and more error prone code. However, I'll be waiting for some benchmark, we can assess the situation more clearly with data.
Edit: I double checked the indexing returned by jaxsim.api.joint.names_to_idxs
and it actually starts from 0. It's the low-level indexing used by RBDAs that start from 1.
I haven't removed the usage of |
I'm doing the benchmarks using Perfetto Profiler (see https://jax.readthedocs.io/en/latest/profiling.html). I'll post them ASAP |
Yes you introduced the knowledge that you can use |
fc5f82d
to
3a2fc47
Compare
c5da3b4
to
b4c9e2b
Compare
b4c9e2b
to
bab95c1
Compare
bab95c1
to
d783c16
Compare
I believe that the most intuitive behavior for the final user is that js.link.names_to_indexes(joint_names=model.link_names(), **kwargs) and jnp.arange(model.number_of_links()) should return the same result. If there will be any need to change this logic in the future, we will propagate the change over the whole library |
d783c16
to
e0894c1
Compare
My point is not on the user side since users are not supposed to know anything about indices (there are the names to index helpers for this reason). My point was about the developers and the logic within JaxSim functions. I'm not strongly against this change, my whole point is that in case of changes on the indices used internally, there will be the need to change multiple locations. I checked the tests, and we already have checks that ensure that the current indexing does not get broken: jaxsim/tests/test_api_joint.py Lines 22 to 24 in bd59060
Lines 27 to 29 in bd59060
Can you please add a comment to both files that links to our discussion in this PR in case that check starts to fail? |
return jnp.vstack( | ||
[ | ||
jnp.block([A_R_B.T, -A_R_B.T @ A_X_B[0:3, 3:6] @ A_R_B.T]), | ||
jnp.block([jnp.zeros(shape=(3, 3)), A_R_B.T]), | ||
] | ||
) |
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.
I just realized that all these methods are correct only if they operate on transforms for 6D velocites, not those for 6D forces. Let's keep this in mind. We might also want to add an additional argument to turn on logic for 6D forces. I think that the term adjoint only corresponds to the tangent of the original space (
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.
Murray Lee Sastry seems just to write of "adjoint transpose". Indeed Park calls it "dual adjoint" in https://mae.eng.uci.edu/~bobrow/papers_files/DynamicsParkBobrow.pdf , but if you want to avoid confusion in the reader you can also use more user friendly terms like "velocity transform" and "force transform" or something similar.
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.
Thanks for the pointers! If we want to adapt the logic to also support 6D force transforms, probably we can add an additional is_dual=False
argument, with a proper docstring its meaning.
Done in 85ab07c. Thanks! |
Traces available at https://gist.github.com/flferretti/a20c7cd171c25dd784eef361246bd8cd. In order to view them, please run: git clone https://gist.github.com/a20c7cd171c25dd784eef361246bd8cd.git
cd a20c7cd171c25dd784eef361246bd8cd
python -m http.server C.C. @xela-95 @diegoferigo |
Can you please summarize them, if you have any specific improvement in mind? |
I made the gist just for reference and in case you were interested into the specific improvements. For a general benchmark, you can take a look at the PR description |
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.
Great, thanks @flferretti! The improvement of
Co-authored-by: Diego Ferigo <diego.ferigo@iit.it>
03edbf2
to
62df0c2
Compare
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.
Thanks @flferretti for the performance improvements!! I checked your changes and they look good to me!
Enhance efficiency by normalizing quaternions, reducing memory footprint, and speeding up various dynamics computations. Replace
jnp.float32
withfloat
for consistency. Avoid unnecessary calls to.names_to_idxs
where possible.Note
See profiler traces at https://gist.github.com/flferretti/a20c7cd171c25dd784eef361246bd8cd
Tip
js.model.generalized_free_floating_jacobian_derivative
usedjax.vmap
overjs.link.jacobian_derivative
, but a lot of quantities were unnecessarily computed multiple times. For this reason, I moved the major computation tojs.model
and left only the link extraction part injs.link
. Some adjustments regarding the output velocity representation were necessary in order to avoid to calljs.link.velocity
inside thevmap
ped function_compute_row
js.model.generalized_free_floating_jacobian_derivative
js.model.free_floating_mass_matrix
js.model.forward_kinematics
js.model.generalized_free_floating_jacobian
js.references.JaxSimModelReferences.build
js.contact.collidable_point_kinematics
data.base_orientation
model.kin_dyn_parameters.joint_transforms_and_motion_subspaces
references.link_forces
📚 Documentation preview 📚: https://jaxsim--280.org.readthedocs.build//280/