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

Allow pytorch batching using jax2torch #93

Merged
merged 16 commits into from
Jun 27, 2024
Merged

Allow pytorch batching using jax2torch #93

merged 16 commits into from
Jun 27, 2024

Conversation

Giulero
Copy link
Collaborator

@Giulero Giulero commented Jun 27, 2024

This PR exploits the package jax2torch, based on the gist which explains how to convert jax functions in pytorch ones, preserving also the gradients.

This should allow to perform batch computations, e.g.

H_b_batch = torch.tile(torch.tensor(H_b), (n_samples, 1, 1)).requires_grad_()
joints_val_batch = torch.tile(torch.tensor(joints_val), (n_samples, 1)).requires_grad_()

mass_matrix = kindyn.mass_matrix(H_b_batch, joints_val_batch)
# The mass matrix dimension should have dimension (n_samples, n_dofs + 6, n_dofs + 6)

# and gradient
mass_matrix.sum().backward()

I put this interface in adam.pytorch as KinDynComputionBatch, even if it using jax under the hood.

from adam.pytorch import KinDynComputationsBatch
# and it is instantiated in the vary same way of the other interfaces
kindyn = KinDynComputationsBatch(model_path, joints_name_list)

@traversaro already opened conda-forge/staged-recipes#26780 (comment), so everything will be soon conda ready.

@traversaro
Copy link
Contributor

@traversaro already opened conda-forge/staged-recipes#26780 (comment), so everything will be soon conda ready.

Merged: https://github.com/conda-forge/jax2torch-feedstock .

@Giulero
Copy link
Collaborator Author

Giulero commented Jun 27, 2024

All tests are passing now!

@Giulero Giulero merged commit c6dc157 into main Jun 27, 2024
15 checks passed
@Giulero Giulero deleted the jax2torch branch June 27, 2024 16:42
@Giulero
Copy link
Collaborator Author

Giulero commented Jun 28, 2024

Cc @evelyd @Zweisteine96

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.

2 participants