Skip to content

perf(rbd): optional GPU contact reduction (merge per-pair manifolds to ≤4 points) - #17

Closed
haixuanTao wants to merge 1 commit into
dimforge:mainfrom
haixuanTao:perf/contact-reduction
Closed

perf(rbd): optional GPU contact reduction (merge per-pair manifolds to ≤4 points)#17
haixuanTao wants to merge 1 commit into
dimforge:mainfrom
haixuanTao:perf/contact-reduction

Conversation

@haixuanTao

Copy link
Copy Markdown
Contributor

What

Opt-in narrow-phase pass (RbdPipeline::contact_reduction, default off) that compacts each batch's contacts in place after pfm_pfm: the first record of each collider pair is kept verbatim, and every later record with the same key (e.g. per-triangle trimesh manifolds under one foot) is merged into it — pool both manifolds' points (≤8), keep the deeper manifold's normal, and reuse the existing manifold_reduction deepest+spread selector down to MAX_MANIFOLD_POINTS.

Why

A convex collider resting on a trimesh emits one manifold per overlapping triangle, all sharing one colliders key and one collider-A local frame. Downstream solver kernels then pay per-point cost for near-duplicate information — on our rough-terrain RL workload the multibody contact-constraint kernels ran 4–8× their flat-ground cost (169 vs 76 µs/call). Merging to one ≤4-point manifold per pair is the standard engine remedy (PhysX/MuJoCo do the equivalent).

Measured on a 29-DOF humanoid batch (4096 envs, trimesh terrain, RTX 5090, native-CUDA backend): full training iteration 3.8 → 3.3 s (−13%), contact-constraint kernels 2–4× cheaper. Flat-ground scenes are unaffected.

Design notes

  • Off = zero cost: the kernel is simply not dispatched, and init_contacts_indirect_args runs on the untouched counts.
  • On, single-manifold pairs: copied verbatim → bit-identical to the unreduced path (verified on a deterministic training reference with the pass active on flat ground).
  • threads(1), grid [1, num_batches, 1], two-cursor in-place compaction (write cursor ≤ read cursor); contacts_len rewritten by the single writer per batch.
  • dim3-only (manifold_reduction is dim3); dim2 keeps compiling — the flag is accepted and ignored.

Documented approximations (why it's opt-in)

  • One normal per merged manifold (a collider straddling a sharp edge loses the second normal — a normal-cone split would be the v2).
  • Greedy pairwise merging in emission order (emission order is already nondeterministic upstream, so no new nondeterminism).
  • Warm-start proximity matching can miss merged points on the first step after a merge (mild convergence cost only).

cargo check clean on nexus_rbd3d, nexus_rbd2d, and both shader crates.

🤖 Generated with Claude Code

https://claude.ai/code/session_01U2n9RqmxTJb8UG5d1Sjw4W

Training-grade, default OFF (pipeline.contact_reduction). A convex resting
on a trimesh emits one manifold per overlapping triangle, all sharing one
colliders key and one collider-A local frame; the mb contact-constraint
kernels then pay 6 solver slots per point (4-8x flat-ground cost). The
pass compacts each batch's contacts in place after pfm_pfm: first record
of a pair copies verbatim (flat-ground = bit-identical, verified on the
trainer's iter-0 reference), later records merge via the existing
manifold_reduction selector (pool <=8 points, deepest manifold's normal,
deepest+spread down to 4). threads(1) per batch, single writer.
Documented approximations: one normal per merged manifold, greedy
emission-order merging. Full-stack terrain @4096: 3.8 -> 3.3 s/iter.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@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.

2 participants