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

Refactor integrators module and redefine JaxSimModel class attributes #252

Merged
merged 23 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8ef1c84
Add `dt` and `integrator` as static attributes of `JaxSimModel`
flferretti Oct 4, 2024
9c9a890
Allow wrapping system dynamics without passing `model` and `data`
flferretti Oct 4, 2024
f2b7abb
Update typing in `js.ode`
flferretti Oct 4, 2024
433c2cc
Make build methods `classmethod`s
flferretti Oct 4, 2024
84fe786
Remove `Integrator.init` method
flferretti Oct 4, 2024
87b2beb
Update `js.model.step` with the new `JaxSimModel` attributes
flferretti Oct 4, 2024
8bfe997
Rename `Integrator.params` to `Integrator.state_aux_dict`
flferretti Oct 4, 2024
926c831
Update tests with the new API
flferretti Oct 4, 2024
eee60c2
Update notebooks to the new API
flferretti Oct 18, 2024
4e81ecf
Allow backward compatibility passing `integrator` to `step`
flferretti Oct 18, 2024
5898b36
Modify simulation test to verify backward compatibility
flferretti Oct 18, 2024
0d8d807
Ensure `_integrator` field in model is static
flferretti Oct 18, 2024
d823cfb
Update how the integrator is built and stored into JaxSimModel
diegoferigo Oct 28, 2024
58a1ca0
Remove closure of the integrated dynamics over constant model and data
diegoferigo Oct 29, 2024
503bec5
Small refactor of integrators
diegoferigo Oct 29, 2024
f1de65c
Restore initialization logic of variable-step integrators
diegoferigo Oct 29, 2024
6841080
Align step function with the new integrator logic
diegoferigo Oct 29, 2024
7234614
Apply suggestions from code review
diegoferigo Oct 29, 2024
56e7435
Fix tests
diegoferigo Oct 29, 2024
7ceca8a
Minor changes of jaxsim.api.model
diegoferigo Oct 29, 2024
c5e048f
Preserve time step in reduced models
diegoferigo Oct 22, 2024
0bb25b1
Fix integrator signature
diegoferigo Oct 22, 2024
1f7eec4
Update notebooks to the latest API
flferretti Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 27 additions & 35 deletions examples/jaxsim_as_physics_engine.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@
"import jax\n",
"import jax.numpy as jnp\n",
"import jaxsim.api as js\n",
"import jaxsim\n",
"import rod\n",
"from jaxsim import integrators, logging\n",
"from jaxsim import logging\n",
"from rod.builder.primitives import SphereBuilder\n",
"\n",
"logging.set_logging_level(logging.LoggingLevel.WARNING)\n",
Expand Down Expand Up @@ -142,8 +143,11 @@
"\n",
"- `model`: an object that defines the dynamics of the system.\n",
"- `data`: an object that contains the state of the system.\n",
"- `integrator`: an object that defines the integration method.\n",
"- `integrator_state`: an object that contains the state of the integrator."
"- `integrator` *(Optional)*: an object that defines the integration method.\n",
"- `integrator_state` *(Optional)*: an object that contains the state of the integrator.\n",
"\n",
"The `JaxSimModel` object contains the simulation time step, the integrator and the contact model.\n",
"In this example, we will explicity pass an integrator class to the `model` object and we will use the default `SoftContacts` contact model."
]
},
{
Expand All @@ -157,7 +161,9 @@
"# Create the JaxSim model.\n",
"# This is shared among all the parallel instances.\n",
"model = js.model.JaxSimModel.build_from_model_description(\n",
" model_description=model_sdf_string, time_step=0.001\n",
" model_description=model_sdf_string,\n",
" time_step=0.001,\n",
" integrator=jaxsim.integrators.fixed_step.Heun2,\n",
")\n",
"\n",
"# Create the data of a single model.\n",
Expand Down Expand Up @@ -240,21 +246,7 @@
},
"outputs": [],
"source": [
"# Create the integrator.\n",
"integrator = integrators.fixed_step.Heun2SO3.build(\n",
" dynamics=js.ode.wrap_system_dynamics_for_integration(\n",
" model=model,\n",
" data=data_single,\n",
" system_dynamics=js.ode.system_dynamics,\n",
" ),\n",
")\n",
"\n",
"# Initialize the integrator.\n",
"integrator_state = integrator.init(\n",
" x0=data_single.state,\n",
" t0=0.0,\n",
" dt=model.time_step,\n",
")\n",
"print(f\"Using integrator: {model.integrator}\")\n",
"\n",
"# Initialize the simulated time.\n",
"T = jnp.arange(start=0, stop=1.0, step=model.time_step)"
Expand Down Expand Up @@ -324,46 +316,42 @@
"def step_single(\n",
" model: js.model.JaxSimModel,\n",
" data: js.data.JaxSimModelData,\n",
" integrator_state: dict[str, Any],\n",
") -> tuple[js.data.JaxSimModelData, dict[str, Any]]:\n",
"\n",
" # Close step over static arguments.\n",
" return js.model.step(\n",
" model=model,\n",
" data=data,\n",
" integrator=integrator,\n",
" integrator_state=integrator_state,\n",
" link_forces=None,\n",
" joint_force_references=None,\n",
" )\n",
"\n",
"\n",
"@jax.jit\n",
"@functools.partial(jax.vmap, in_axes=(None, 0, None))\n",
"@functools.partial(jax.vmap, in_axes=(None, 0))\n",
"def step_parallel(\n",
" model: js.model.JaxSimModel,\n",
" data: js.data.JaxSimModelData,\n",
" integrator_state: dict[str, Any],\n",
") -> tuple[js.data.JaxSimModelData, dict[str, Any]]:\n",
"\n",
" return step_single(\n",
" model=model, data=data, integrator_state=integrator_state\n",
" model=model, data=data\n",
" )\n",
"\n",
"\n",
"# The first run will be slow since JAX needs to JIT-compile the functions.\n",
"_ = step_single(model, data_single, integrator_state)\n",
"_ = step_parallel(model, data_batch_t0, integrator_state)\n",
"_ = step_single(model, data_single)\n",
"_ = step_parallel(model, data_batch_t0)\n",
"\n",
"# Benchmark the execution of a single step.\n",
"print(\"\\nSingle simulation step:\")\n",
"%timeit step_single(model, data_single, integrator_state)\n",
"%timeit step_single(model, data_single)\n",
"\n",
"# On hardware accelerators, there's a range of batch_size values where\n",
"# increasing the number of parallel instances doesn't affect computation time.\n",
"# This range depends on the GPU/TPU specifications.\n",
"print(f\"\\nParallel simulation steps (batch_size={batch_size} on {jax.devices()[0]}):\")\n",
"%timeit step_parallel(model, data_batch_t0, integrator_state)"
"%timeit step_parallel(model, data_batch_t0)"
]
},
{
Expand All @@ -381,7 +369,7 @@
"\n",
"for _ in T:\n",
"\n",
" data, integrator_state = step_parallel(model, data, integrator_state)\n",
" data, _ = step_parallel(model, data)\n",
" data_trajectory_list.append(data)"
]
},
Expand All @@ -404,9 +392,7 @@
"source": [
"# Convert a list of PyTrees to a batched PyTree.\n",
"# This operation is called 'tree transpose' in JAX.\n",
"data_trajectory = jax.tree.map(\n",
" lambda *leafs: jnp.stack(leafs), *data_trajectory_list\n",
")\n",
"data_trajectory = jax.tree.map(lambda *leafs: jnp.stack(leafs), *data_trajectory_list)\n",
"\n",
"print(f\"W_p_B: shape={data_trajectory.base_position().shape}\")"
]
Expand Down Expand Up @@ -448,6 +434,11 @@
"\n",
"Have fun!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
Expand All @@ -459,7 +450,8 @@
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "jaxsim",
"language": "python",
"name": "python3"
},
"language_info": {
Expand All @@ -472,7 +464,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
34 changes: 7 additions & 27 deletions examples/jaxsim_for_robot_controllers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
"# @title Create the model and its data\n",
"\n",
"import jaxsim.api as js\n",
"from jaxsim import integrators\n",
"\n",
"# Create the model from the model description.\n",
"model = js.model.JaxSimModel.build_from_model_description(\n",
Expand All @@ -143,23 +142,7 @@
},
"outputs": [],
"source": [
"# @title Select the integrator\n",
"\n",
"# Create the integrator.\n",
"integrator = integrators.fixed_step.RungeKutta4.build(\n",
" dynamics=js.ode.wrap_system_dynamics_for_integration(\n",
" model=model,\n",
" data=data_zero,\n",
" system_dynamics=js.ode.system_dynamics,\n",
" ),\n",
")\n",
"\n",
"# Initialize the integrator.\n",
"integrator_state = integrator.init(\n",
" x0=data_zero.state,\n",
" t0=0.0,\n",
" dt=model.time_step,\n",
")\n",
"# @title Define simulation parameters\n",
"\n",
"# Initialize the simulated time.\n",
"T = jnp.arange(start=0, stop=5.0, step=model.time_step)"
Expand Down Expand Up @@ -255,11 +238,9 @@
"for _ in T:\n",
"\n",
" # Step the JaxSim simulation.\n",
" data, integrator_state = js.model.step(\n",
" data, _ = js.model.step(\n",
" model=model,\n",
" data=data,\n",
" integrator=integrator,\n",
" integrator_state=integrator_state,\n",
" joint_force_references=None,\n",
" link_forces=None,\n",
" )\n",
Expand Down Expand Up @@ -359,7 +340,7 @@
" ṡ = data.joint_velocities()\n",
"\n",
" # Compute the actuated joint torques.\n",
" s_star = - kp * (s - s_des) - kd * (ṡ - s_dot_des)\n",
" s_star = -kp * (s - s_des) - kd * (ṡ - s_dot_des)\n",
" τ = Mss @ s_star + hs\n",
"\n",
" return τ"
Expand Down Expand Up @@ -407,11 +388,9 @@
" )\n",
"\n",
" # Step the JaxSim simulation.\n",
" data, integrator_state = js.model.step(\n",
" data, _ = js.model.step(\n",
" model=model,\n",
" data=data,\n",
" integrator=integrator,\n",
" integrator_state=integrator_state,\n",
" joint_force_references=τ,\n",
" )\n",
"\n",
Expand Down Expand Up @@ -461,7 +440,8 @@
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "jaxsim",
"language": "python",
"name": "python3"
},
"language_info": {
Expand All @@ -474,7 +454,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
Loading