Skip to content

Cache constant recomputation graphs and skip unchanged field setup - #1266

Closed
M-Colley wants to merge 1 commit into
mujocolab:mainfrom
M-Colley:codex/upstream-constant-graphs
Closed

M-Colley wants to merge 1 commit into
mujocolab:mainfrom
M-Colley:codex/upstream-constant-graphs

Conversation

@M-Colley

Copy link
Copy Markdown
Contributor

Constant recomputation currently launches from Python on every domain-randomization call, and an unchanged field-expansion request still rebuilds simulation graphs. Lazily cache one CUDA graph per recomputation level, invalidate it whenever simulation graphs are rebuilt, and retain graphs/sensor contexts when storage and metadata are unchanged. CPU execution keeps the direct path; the none level is a no-op.

Tests change per-world mass and physical state across repeated replays, compare against uncaptured execution, and verify graph invalidation after storage replacement. make check and five focused tests passed with the unchanged MuJoCo/Warp 3.16 pair. All 19 combined Linux checks passed across Python 3.10–3.14 and a fresh dependency resolve: https://github.com/M-Colley/mjlab/actions/runs/40094203866. The corresponding CUDA cases also passed with the 3.17 pair.

A synchronized alternating-order RTX 4050 microbenchmark with 512 worlds measured median set_const_0 at 13.31 ms for direct launches versus 0.969 ms for cached replay over 15 samples. Unchanged expansion measured 34.29 ms before and 0.207 ms after in separate runs. Contacts were disabled (zero overflow); these are local recomputation/setup costs under host load, not end-to-end training throughput. First opened in the fork: M-Colley#3.

@zjjott

zjjott commented Oct 31, 2026

Copy link
Copy Markdown

Hi @M-Colley, I independently tested a similar constant-recomputation graph cache and measured a 10.75% end-to-end PPO throughput improvement with 4096 MicroDuck environments. I noticed the overlap with this PR and would be happy to contribute the benchmark and regression coverage.
The end-to-end results were:

Metric Unmodified main Cached constants graphs Change
Run 1 mean iteration time 2.412703 s 2.190699 s Lower
Run 2 mean iteration time 2.430149 s 2.182136 s Lower
Mean across both runs 2.421426 s 2.186417 s -9.71%
End-to-end transitions/s 40,598 44,961 +10.75%
Peak PyTorch allocated memory 520.62 MiB 520.62 MiB Unchanged
Peak PyTorch reserved memory 674.00 MiB 674.00 MiB Unchanged
Peak Warp memory-pool usage 1,108.43 MiB 1,108.43 MiB Unchanged

The common benchmark configuration was:

Setting Value in both groups
Hardware/runtime RTX 3070, 8 GiB; WSL2
Python / PyTorch 3.12.11 / 2.14.1
Warp / MuJoCo / MuJoCo Warp 1.18.0 / 3.16.0 / 3.16.0
BAM main b9e1111, v1.0.3; scan optimization disabled
Task Mjlab-Velocity-Flat-MicroDuck; task base b02fdea plus shared compatibility changes
Parallel environments / rollout length 4096 / 24 steps
Iterations 25 per run; first 5 excluded from steady-state measurements
Repetitions / CPU threads 2 per group / 4 threads
Solver iteration / line-search caps 100 / 50
Constraint capacity Default; unchanged

@kevinzakka, would these end-to-end results be useful for evaluating this approach?

@M-Colley

M-Colley commented Nov 7, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for measuring it end to end, @zjjott. The numbers in the description cover only the recomputation and expansion costs, so a training-throughput figure is what this PR was missing.

Was that run on this branch (428defa) or on your own cache? If it was yours, the same MicroDuck setup on 428defa would show whether this PR gives the same gain.

On tests: tests/test_constant_recompute_graph.py replays each level against an uncaptured run while the per-world masses change between replays. It checks three things:

  • qpos and qvel are left as they were;
  • replacing model storage drops the cached graphs;
  • repeating an unchanged expand_model_fields() keeps storage and graphs.

If your tests cover something else, for example the event manager's recomputation in a full task, I'm happy to take them as a PR against M-Colley:codex/upstream-constant-graphs. Whether a benchmark script belongs in the repo is up to the maintainers.

@M-Colley

Copy link
Copy Markdown
Contributor Author

Closing in favor of #1333, which ports the set_const CUDA graph cache and redundant field-expansion check onto main (mujoco_warp.SetConstGroup on MuJoCo Warp 4.0.1).

@M-Colley M-Colley closed this Nov 14, 2026
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