Skip to content
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

[Sprint] Improve caching mechanism #359

Merged
merged 9 commits into from
Feb 3, 2025
Merged

[Sprint] Improve caching mechanism #359

merged 9 commits into from
Feb 3, 2025

Conversation

younik
Copy link
Member

@younik younik commented Jan 30, 2025

This PR goes a bit beyond what we agreed, so feel free to reject it. I can easily do a simpler version where we call update_cached inside the reset function.

The motivation is that update cached was always called after data.replace (and the cached values are needed at every step), so it probably makes sense to compute it during data construction and at every replace.

The reset_ methods are calling a replace, which is an expensive operation. This is not immediately clear to the user, and leads to sequentially reset calls:

data.reset_base_position(...)
data.reset_base_velocity(...)
...

One method that allows to change multiple things would be better, so I used the replace itself.

The other selling point is that with the current solution, we need to remember to call update_cached from now on, which may lead to future bugs.

One negative aspect of this is that we need model in replace, which differs from the dataclass.replace as the model is not an attribute of data.


📚 Documentation preview 📚: https://jaxsim--359.org.readthedocs.build//359/

@CarlottaSartore CarlottaSartore self-requested a review January 30, 2025 09:56
@younik younik self-assigned this Jan 30, 2025
Copy link
Collaborator

@flferretti flferretti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @younik, it looks to me overall. It does make sense to remove the reset_* methods, but the problem is that we are storing some quantities in VelRepr.Inertial, so we either need to add a comment to make this clear to the user, or to change the cached quantities in the same velocity representation of the JaxSimModelData instance

@@ -549,6 +446,64 @@ def reset_base_velocity(
base_linear_velocity=W_v_WB[0:3].squeeze().astype(float),
base_angular_velocity=W_v_WB[3:6].squeeze().astype(float),
)

def replace(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding typing.override decorator would prevent in the future errors related to overriding methods from parent class

Suggested change
def replace(
@override
def replace(

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Copy link
Member

@xela-95 xela-95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @younik ! The replace approach in which to update the cached values is elegant to me and avoid potential future issues! I added some comments related to the handling of data attributes and their representation.

@@ -295,7 +295,8 @@ def jacobian(
B_J_WL_I = B_J_WL_B

case VelRepr.Mixed:
W_R_B = data.base_orientation(dcm=True)
W_R_B = data.base_orientation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now data.base_orientation return a quaternion

Suggested change
W_R_B = data.base_orientation
W_Q_B = data.base_orientation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

Comment on lines 529 to 530
base_linear_velocity=base_linear_velocity,
base_angular_velocity=base_angular_velocity,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forward kinematics works only in inertial (this has been made more clear in #361 , so we need to convert base linear and angular velocities to this representation before calling it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, done!

@CarlottaSartore CarlottaSartore changed the title Improve caching mechanism [Sprint] Improve caching mechanism Jan 30, 2025
@xela-95
Copy link
Member

xela-95 commented Jan 31, 2025

@younik I noted that here we also miss a property for the base transform, since it is only cached as a private attribute.

@younik younik merged commit 6a85c9c into sprint/dev Feb 3, 2025
17 of 24 checks passed
@younik younik deleted the sprint/caching branch February 3, 2025 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants