-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PiperOrigin-RevId: 590287478 Change-Id: Iab96c438d2dda8fcb575e6ef03091bdcdb00c559
- Loading branch information
Showing
16 changed files
with
330 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Copyright 2023 The Brax Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2023 The Brax Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# pylint:disable=g-multiple-import | ||
"""Brax adapter for MJX physics engine.""" | ||
|
||
from brax import base | ||
from flax import struct | ||
from mujoco import mjx | ||
|
||
|
||
@struct.dataclass | ||
class State(base.State): | ||
"""Dynamic state that changes after every step. | ||
Attributes: | ||
data: mjx.Data | ||
""" | ||
data: mjx.Data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2023 The Brax Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# pylint:disable=g-multiple-import | ||
"""PBD perf tests.""" | ||
|
||
from absl.testing import absltest | ||
from brax import test_utils | ||
from brax.mjx import pipeline | ||
import jax | ||
from jax import numpy as jp | ||
|
||
|
||
class PerfTest(absltest.TestCase): | ||
|
||
def test_pipeline_ant(self): | ||
sys = test_utils.load_fixture('ant.xml') | ||
|
||
def init_fn(rng): | ||
rng1, rng2 = jax.random.split(rng, 2) | ||
q = jax.random.uniform(rng1, (sys.q_size(),), minval=-0.1, maxval=0.1) | ||
qd = 0.1 * jax.random.normal(rng2, (sys.qd_size(),)) | ||
return pipeline.init(sys, q, qd) | ||
|
||
def step_fn(state): | ||
return pipeline.step(sys, state, jp.zeros(sys.act_size())) | ||
|
||
test_utils.benchmark('mjx pipeline ant', init_fn, step_fn) | ||
|
||
|
||
if __name__ == '__main__': | ||
absltest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Copyright 2023 The Brax Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Physics pipeline for fully articulated dynamics and collisiion.""" | ||
# pylint:disable=g-multiple-import | ||
# pylint:disable=g-importing-member | ||
from brax.base import Motion, System, Transform | ||
from brax.mjx.base import State | ||
import jax | ||
from jax import numpy as jp | ||
from mujoco import mjx | ||
|
||
|
||
def init( | ||
sys: System, q: jax.Array, qd: jax.Array, debug: bool = False | ||
) -> State: | ||
"""Initializes physics state. | ||
Args: | ||
sys: a brax system | ||
q: (q_size,) joint angle vector | ||
qd: (qd_size,) joint velocity vector | ||
debug: if True, adds contact to the state for debugging | ||
Returns: | ||
state: initial physics state | ||
""" | ||
del debug # ignored in mjx pipeline | ||
|
||
model = sys.get_mjx_model() | ||
data = mjx.make_data(model) | ||
data = data.replace(qpos=q, qvel=qd) | ||
data = mjx.forward(model, data) | ||
|
||
q, qd = data.qpos, data.qvel | ||
x = Transform(pos=data.xpos[1:], rot=data.xquat[1:]) | ||
cvel = Motion(vel=data.cvel[1:, 3:], ang=data.cvel[1:, :3]) | ||
offset = data.xpos[1:, :] - data.subtree_com[model.body_rootid[1:]] | ||
offset = Transform.create(pos=offset) | ||
xd = offset.vmap().do(cvel) | ||
contact = None | ||
|
||
return State(q, qd, x, xd, contact, data) | ||
|
||
|
||
def step( | ||
sys: System, state: State, act: jax.Array, debug: bool = False | ||
) -> State: | ||
"""Performs a single physics step using position-based dynamics. | ||
Resolves actuator forces, joints, and forces at acceleration level, and | ||
resolves collisions at velocity level with baumgarte stabilization. | ||
Args: | ||
sys: system defining the kinematic tree and other properties | ||
state: physics state prior to step | ||
act: (act_size,) actuator input vector | ||
debug: if True, adds contact to the state for debugging | ||
Returns: | ||
x: updated link transform in world frame | ||
xd: updated link motion in world frame | ||
""" | ||
del debug # ignored in mjx pipeline | ||
|
||
model = sys.get_mjx_model() | ||
data = state.data.replace(ctrl=act) | ||
data = mjx.step(model, data) | ||
|
||
q, qd = data.qpos, data.qvel | ||
x = Transform(pos=data.xpos[1:], rot=data.xquat[1:]) | ||
cvel = Motion(vel=data.cvel[1:, 3:], ang=data.cvel[1:, :3]) | ||
offset = data.xpos[1:, :] - data.subtree_com[model.body_rootid[1:]] | ||
offset = Transform.create(pos=offset) | ||
xd = offset.vmap().do(cvel) | ||
contact = None | ||
|
||
return State(q, qd, x, xd, contact, data) |
Oops, something went wrong.