Skip to content

feat: per-environment collision-pair capacity override - #24

Closed
haixuanTao wants to merge 1 commit into
dimforge:mainfrom
haixuanTao:upstream-collisions-capacity
Closed

feat: per-environment collision-pair capacity override#24
haixuanTao wants to merge 1 commit into
dimforge:mainfrom
haixuanTao:upstream-collisions-capacity

Conversation

@haixuanTao

Copy link
Copy Markdown
Contributor

Adds NexusState::set_rbd_collisions_capacity() (+ Python binding): overrides the per-environment collision-pair floor used when the GPU rigid-body state is allocated at finalize. 16 lines, no behavior change unless called.

Why. The 4096-pairs/env default sizes every pair-keyed buffer as capacity × envs. That's right for one busy scene, but batched RL-style workloads (one small robot per env) hit wgpu's 4 GiB max-buffer limit near 512 envs — the failing allocation is a single 4.7 GiB buffer at 1,024 envs for a 2-body scene — and OOM a 32 GiB card at 4,096 envs on the CUDA backend, while actual usage is ~7 pairs/env.

Safety. The value is a floor, not a cap: the default RbdResizePolicy::Grow keeps resizing off measured pair counts, so contacts are never dropped. Verified with a 12-DOF biped MJCF (real ground contacts throughout): bit-identical 400-step end frames at capacity 4, 16, and the default 4096.

Measured (RTX 5090, capacity 16, 800-step sustained windows): the cube-per-env scene goes from a hard failure above ~512 envs to 5.67M env-steps/s at 16,384 envs (WebGPU) / 5.28M (CUDA+graph); a 10-link multibody pendulum per env reaches 3.14M / 2.93M. Numbers were taken on haixuanTao/nexus main (includes recent mb perf work), but the memory scaling this PR fixes is independent of those changes. Merged on the fork as haixuanTao#1; original implementation of the idea in the fork's perf/flat-narrow-phase line (81c76bb).

🤖 Generated with Claude Code

https://claude.ai/code/session_01NTtT75BpS7XH9DoHVbdfwN

Ports set_rbd_collisions_capacity() from perf/flat-narrow-phase (81c76bb)
to the unified line: NexusState grows a setter that overrides the
per-environment collision-pair floor used when the GPU state is
allocated at finalize, plus the Python binding.

Why: the 4096-pairs/env default is sized for one busy scene, not
thousands of small batched envs — pair-keyed buffers scale as
capacity x envs, so batching hits wgpu's 4 GiB max-buffer limit near
512 envs (the failing allocation is 4.7 GiB at 1024) and OOMs a 32 GiB
card at 4096 envs on CUDA. The capacity is a floor, not a cap: the
default RbdResizePolicy::Grow resizes off measured pair counts, so
contacts are never lost — the LeRobot-legs biped MJCF produces
bit-identical 400-step end frames at capacity 4, 16, and 4096.

Measured on the RTX 5090 (capacity 16, 800-step windows, env-steps/s):

  cube    16,384 envs  WebGPU 5.67M | CUDA+graph 5.28M   (was: OOM)
  biped-pendulum 16,384 envs  WebGPU 3.14M | CUDA+graph 2.93M (was: OOM)
  previous ceiling on main: ~170k env-steps/s at <=2048 envs

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NTtT75BpS7XH9DoHVbdfwN
@sebcrozet

Copy link
Copy Markdown
Member

This will be merged as part of #36

sebcrozet added a commit that referenced this pull request Aug 29, 2026
* fix(rbd): apply the per-batch stride to collider_parent reads in the narrow phase

Replaces #21

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* feat: per-environment collision-pair capacity override

Replaces #24

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* feat(rbd): make the narrow-phase contact prediction distance configurable

Replaces #28

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* fix(rbd): thread the configurable prediction distance through the brute-force broad phase

Completes #28

* feat(python): per-environment MJCF insertion

Replaces #16

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* fix(python): drop the duplicated collisions-capacity setter and pass the RbdCoupling to insert_rigid_body_in

Completes #16

* feat(python): per-step MJCF actuator control + multibody state readback

Replaces #12

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@1ms.ai>

* fix(python): gate multibody control/readback on dim3, add the missing PyArray2 import

Replaces #12

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* fix(rbd): decode the SoA link workspace for the multibody readback and drop the stale set_gravity copy

Completes #12

* perf(rbd): dedupe shared TriMesh uploads in from_rapier

Replaces #19

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* perf(rbd): optional GPU contact reduction, merging per-pair manifolds to <=4 points

Replaces #17

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* fix(rbd): pass the prediction distance to manifold_reduction in the contact-reduction kernel

Completes #17

* perf(rbd): flat 1-D narrow-phase dispatch, packing warps across batches

Replaces #21

Co-Authored-By: Haixuan Xavier Tao <tao.xavier@outlook.com>

* fix(rbd): restore the contacts capacity binding and import atomic_load_u32 for the flat dispatch

Completes #21

* fix(rbd): drop the stale 2mm PREDICTION constant reintroduced by the flat-dispatch port

Completes #21

* fix mpm feature-gating

* feat(rbd): expose dof_state_mut, links_static, joint_constraints and link_of_body

* feat(rbd): env-reset primitives, GPU motor scatter, contact sensors, actuator delay, encoded step, substep-refresh cadence and per-DoF armature/frictionloss

* fix(rbd): guard against implicit-coriolis drifting from the batch_indices uniform

* feat(rbd): cluster contact manifolds by normal, matching rapier, with a tunable threshold

* feat(rbd): model multibody joint frictionloss as a constraint instead of a force

* feat(rbd): seed per-DoF joint friction from rapier's Multibody::frictions

* refactor(rbd): read the contact prediction distance from RbdSimParams instead of a dedicated uniform

* chore: cargo fmt

* refactor(rbd): move the contact merge cosine into RbdSimParams

* refactor: move read_multibody_links onto NexusState and drive every env from control_multibody_motors

* test(rbd): add a headless many-small-environments step-timing harness

* revert(rbd): drop the flat 1-D narrow-phase dispatch

Measured 7-21% slower on Metal.

* chore: cleanup comments

* fix: gate control_multibody_motors on dim3 so the 2D build still compiles

* fix instability in joint-ball3 demo

* chore: remove debug test files

* chore: cleanups

* fix(rbd): build the bench harness without the metal feature and only in 3D

* fix(rbd): silence the clippy needless-borrow and unnecessary-mut lints

* fix(rbd): split the joint-constraint back-solve into its own dispatch to fit 8 storage buffers

* test(rbd): keep the bench harness under wgpu's default buffer-size limit

* fix(rbd): split the batched env reset into pose and DoF passes to fit 8 storage buffers

* chore: switch to the published rapier version

* fix: make all envs share the same RbdSimParams

* chore: clippy fixes

---------

Co-authored-by: Haixuan Xavier Tao <tao.xavier@outlook.com>
Co-authored-by: Haixuan Xavier Tao <tao.xavier@1ms.ai>
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.

3 participants