Skip to content

Conversation

@swankystark
Copy link
Contributor

Implement mj_setConst for Safe Runtime Model Updates

This PR implements mj_setConst functionality in mujoco_warp, enabling safe runtime modification of model parameters on device, which is required for domain randomization.

This work addresses Issue #893 (Implement mj_setConst).


Summary

MuJoCo allows certain model parameters (e.g., mass, inertia, spring parameters) to be modified at runtime, but requires recomputation of dependent model-constant fields via mj_setConst.
This PR ports the relevant parts of mj_setConst into mujoco_warp.


Changes

New Functionality

  • Implemented set_const(m, d, mjm) in mujoco_warp/_src/setconst.py as the main entry point.

Implemented Components

  • _set_stat

    • Recomputes mass statistics.
    • Currently computes stat.meaninertia using a custom Warp kernel.
  • _set_spring

    • Recomputes tendon_lengthspring.
    • Runs the kinematics pipeline on qpos_spring, matching C MuJoCo behavior.

API Update

  • Updated put_model in io.py to accept recompute_const: bool = False.
    • When True, set_const is invoked during model creation.

Tests

  • Added unit tests in mujoco_warp/_src/setconst_test.py.
  • Tests validate:
    • meaninertia computation
    • tendon_lengthspring recomputation
  • Results are compared against C MuJoCo reference values.

Relation to PR #905

This PR complements PR #905.

Placeholders with TODOs referencing PR #905 are included to facilitate clean merging.


Verification

All added tests pass successfully, confirming correctness against C MuJoCo behavior.

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.

1 participant