google-deepmind / mujoco

Multi-Joint dynamics with Contact. A general purpose physics simulator.
https://mujoco.org
Apache License 2.0
8.25k stars 823 forks source link

Getting error in mjx.get_data(mj_model, mjx_data) function #2234

Closed KamatMayur closed 3 days ago

KamatMayur commented 4 days ago

Intro

Hi!

I am a graduate . I use MuJoCo for my research on Robotics.

My setup

mujoco version: 3.2.5 python api 64 bit Ubuntu 24.04.1 LTS

What's happening? What did you expect?

I was testing the humanoid model which comes by default with mujoco. I copied the model and stored in the xml file. Then i ran the simulation first with mujoco on cpu and it ran fine. Then i tried to run it using mujoco mjx and i was getting error pointing to mj_data = mjx.get_data(mj_model, mjx_data)

Steps for reproduction

Just run the code below to reproduce the error

Minimal model for reproduction

Use the model that comes with mujoco called "humanoid.xml"

Code required for reproduction


import mujoco
import jax
from jax import numpy as jp
import numpy as np
import mediapy as media
from mujoco import mjx

# Make model, data, and renderer
mj_model = mujoco.MjModel.from_xml_path("humanoid.xml")
mj_data = mujoco.MjData(mj_model)

mjx_model = mjx.put_model(mj_model)
mjx_data = mjx.put_data(mj_model, mj_data)

scene_option = mujoco.MjvOption()
scene_option.flags[mujoco.mjtVisFlag.mjVIS_JOINT] = True

duration = 3.8  # (seconds)
framerate = 60  # (Hz)

# frames = []
# mujoco.mj_resetData(mj_model, mj_data)
# with mujoco.Renderer(mj_model) as renderer:
#     while mj_data.time < duration:
#         mujoco.mj_step(mj_model, mj_data)
#         if len(frames) < mj_data.time * framerate:
#             renderer.update_scene(mj_data, scene_option=scene_option)
#             pixels = renderer.render()
#             frames.append(pixels)

jit_step = jax.jit(mjx.step)

frames = []
with mujoco.Renderer(mj_model) as renderer:
    while mjx_data.time < duration:
        mjx_data = jit_step(mjx_model, mjx_data)
        if len(frames) < mjx_data.time * framerate:
            mj_data = mjx.get_data(mj_model, mjx_data)
            renderer.update_scene(mj_data, scene_option=scene_option)
            pixels = renderer.render()
            frames.append(pixels)

media.write_video("vid.mp4", frames, fps=framerate)

Confirmations

KamatMayur commented 3 days ago

Never-mind it seems that the current version installed from pip isn't updated and the issue was on line 435, 436, 437. In the io.py file in mjx/_src. These lines were initializing the numpy arrays as int instead of np.int32. This is apparently fixed on the GitHub version but not present in the pip version so adding just that solved the issue..

bd-rtao commented 3 days ago

Thanks for posting this! I also ran into this problem with the get_data_into function, which is the same thing, but It took me a while to find this issue. So I'll post some of the error message here as well to make this post more searchable.

python3.10/site-packages/mujoco/mjx/_src/io.py", line 439, in get_data_into
    mujoco.mju_dense2sparse(
TypeError: mju_dense2sparse(): incompatible function arguments. The following argument types are supported:
    1. (res: numpy.ndarray[numpy.float64[m, 1], flags.writeable], mat: numpy.ndarray[numpy.float64[m, n], flags.c_contiguous], rownnz: numpy.ndarray[numpy.int32[m, 1], flags.writeable], rowadr: numpy.ndarray[numpy.int32[m, 1], flags.writeable], colind: numpy.ndarray[numpy.int32[m, 1], flags.writeable]) -> int

Invoked with: array([0., 0., 0., ..., 0., 0., 0.]), array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]], dtype=float32), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0]), array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0]), array([0, 0, 0, ..., 0, 0, 0])