Skip to content

Avoid atomics in deterministic actuation derivative, contact force and sleep kernels - #1935

Closed
johnnynunez wants to merge 7 commits into
google-deepmind:mainfrom
johnnynunez:feature/determinism-atomics-implicit
Closed

johnnynunez wants to merge 7 commits into
google-deepmind:mainfrom
johnnynunez:feature/determinism-atomics-implicit

Conversation

@johnnynunez

@johnnynunez johnnynunez commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

With opt.deterministic=ATOMICS, _qderiv_actuator_passive_actuation_sparse, _cfrc_ext_contact, _update_sleep_bodies and _update_sleep_dofs run float atomics or atomic counters in Warp's RUN_TO_RUN mode. Their deterministic variants now need no scatter buffers: one thread per world sums the derivatives in order, one thread per body adds its contacts in constraint row order (new kernel _cfrc_ext_contact_body), and one thread per tree writes its awake dofs. The default kernels generate the same CUDA source as before.

Launch time per step, before to after (default): Unitree G1 with 8192 worlds, derivatives 3360 to 63 us (22) and contact forces 385 to 100 us (32); ALOHA clutter with 256 worlds, sleep lists 1995 to 191 us (133). mjwarp-testspeed on franka_emika_panda with ATOMICS, median of 3: 4.88M to 11.36M steps/s; NONE is unchanged (44.8M).

Merges cleanly with #1932 and #1933. #1790 and #1680 edit the same sleep kernels and will conflict.

cc @thowell @adenzler-nvidia

johnnynunez and others added 4 commits November 10, 2026 15:15
With `DeterminismType.ATOMICS`, `_qderiv_actuator_passive_actuation_sparse` scattered `moment_i * moment_j * vel` into `qDeriv` with a float atomic compiled in Warp's `RUN_TO_RUN` mode, which records and sorts every contribution. With the implicit integrators that launch took about 786 us per step on the Franka Panda benchmark with 32768 worlds, against 19 us for the default kernel, and about 4269 us against 22 us on the Unitree G1 benchmark with 8192 worlds.

The deterministic variant now lets the thread of the first actuator of each world accumulate all the actuators of that world in ascending order with plain loads and stores. Each element has one writer and a fixed summation order, and the compiled kernel has no atomic left, so Warp neither allocates scatter buffers nor sorts. The update is written as `x = x + y` because Warp compiles `+=` on an array element to an atomic. The launch now takes about 39 us per step on the Franka Panda benchmark and 60 us on the Unitree G1 benchmark.

The factory signature, its `RUN_TO_RUN` module options and the default kernel are unchanged: the generated CUDA source of the default variant is the same as before.

Add a test that compares the deterministic derivatives with MuJoCo and with the default kernel for one and two worlds, on a model where every degree of freedom is driven by several actuators.
The deterministic variant of `_cfrc_ext_contact` added the wrench of each
contact to its two bodies with float atomics, which Warp's RUN_TO_RUN
mode replays through scatter buffers.

The deterministic kernel now stores the force of each contact in its own
slot and marks, with an integer maximum, the first constraint row of the
contacts of each body. `_cfrc_ext_contact_body` then runs one thread per
body, which walks the constraint rows from that mark and adds the
contacts of its body in row order. The default kernel is not edited.

Unitree G1 (8192 worlds), time per step of the launches: 383 us before,
91 us now for the two kernels, 34 us for the default kernel.
The deterministic variants of `_update_sleep_bodies` and
`_update_sleep_dofs` allocated a list slot with an atomic counter, which
Warp's RUN_TO_RUN mode replays with a counting pass and a prefix scan.

The body list is now written by the thread of the first body of each
world, in ascending order. The dof list is written with one thread per
tree: the thread counts the dofs of the awake trees before its tree and
writes the dofs of its tree after them. The default kernels are
unchanged.

ALOHA clutter (256 worlds, four launches per step), time per step:
`_update_sleep_bodies` 1025 us before, 120 us now, 73 us by default;
`_update_sleep_dofs` 890 us before, 66 us now, 59 us by default.
With opt.deterministic & ATOMICS, _cfrc_ext_contact_body walks the
constraint rows of a world from the first contact of each body and read
the type of every row it landed on, to step over rows that are not
contacts. The contact rows are the last rows of a world (the constraint
kernels rely on it already), so every row the walk lands on is the first
row of a contact: the load of efc.type and its branch are removed. The
default kernel is not edited.

unitree_g1_flat, 8192 worlds, Nsight Systems, us of GPU time per step:
  _cfrc_ext_contact_body    10.7 -> 9.0 (three runs: 9.0, 9.2, 8.9)
  _cfrc_ext_contact          8.6 -> 8.7
  default _cfrc_ext_contact  9.7

Measured on top of this and rejected (body kernel + contact kernel):
stopping the walk of a body at its last contact row 9.1 + 9.0; force and
position of a contact in one element 8.3 + 9.0; bodies, force and
position in one struct per contact 8.4 + 9.1.
@johnnynunez

Copy link
Copy Markdown
Contributor Author

Benchmarks on main 60dbbdf: mjwarp-testspeed, benchmarks/ settings, opt.deterministic=ATOMICS, steps/s, median of 3 interleaved runs (spread under 1%, one NONE run of main on Franka 3.8% low), zero overflow, RTX PRO 6000 Blackwell. "Stack" is main with #1931, #1932, #1933, #1935, #1936, #1937 and #1940.

benchmark worlds main this PR stack without this PR stack NONE on main
franka_emika_panda 32768 4.86M 11.3M 7.09M 44.1M 44.7M
unitree_g1_flat 8192 fails fails fails 4.83M 5.07M
aloha_clutter 2048 fails fails 433k 510k 525k

"fails": Warp's scatter buffer for a kernel exceeds int32: the solver's (#1933) on main and with this PR alone, the actuation derivative's on the G1 without this PR. With NONE this PR is within 0.1% of main on the three benchmarks.

@thowell

thowell commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

@johnnynunez plan for now is to utilize warp's built-in deterministic atomics functionality

@thowell thowell closed this Nov 12, 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