Skip to content

[Performance] Cache unchanged local wrench composition - #7362

Closed
AntoineRichard wants to merge 1 commit into
isaac-sim:developfrom
AntoineRichard:antoiner/spike-issue-911-wrench-perf
Closed

AntoineRichard wants to merge 1 commit into
isaac-sim:developfrom
AntoineRichard:antoiner/spike-issue-911-wrench-perf

Conversation

@AntoineRichard

@AntoineRichard AntoineRichard commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Avoids repeated WrenchComposer body-frame composition kernel launches when inputs have not changed and all wrenches are local-frame. A conservative pose-dependency flag keeps global and mixed wrench paths recomposing on every explicit call.

Addresses isaac-sim/IsaacLab-Internal#911.

No new dependencies.

Performance

A writer-level spike with 4,096 environments measured:

  • Newton: about 40–45% lower external-wrench writer time
  • PhysX: about 60% lower external-wrench writer time

These measurements used mocked asset writers and do not claim an end-to-end task speedup. The regression test verifies one composition launch for repeated unchanged local wrenches and two launches for global wrenches.

Type of change

  • Performance improvement (non-breaking change)

Release backport

  • Backport this pull request to the active release branch after it merges into develop

Screenshots

Not applicable.

Validation

  • WrenchComposer unit suite: 376 passed
  • Repository formatting and pre-commit checks passed
  • Changelog fragment validation passed

Checklist

  • I have read and understood the contribution guidelines
  • I have run the pre-commit checks
  • I have updated the affected method documentation
  • My changes generate no new warnings
  • I have added tests that prove the optimization is effective
  • I have added an isaaclab changelog fragment
  • My name is already present in CONTRIBUTORS.md

@github-actions github-actions Bot added the isaac-lab Related to Isaac Lab team label Aug 26, 2026
@kellyguo11

Copy link
Copy Markdown
Contributor

is this one ready?

@kellyguo11

Copy link
Copy Markdown
Contributor

was this closed on purpose?

ooctipus added a commit that referenced this pull request Sep 24, 2026
# Description

`WrenchComposer.compose_to_body_frame()` was called unconditionally by
every backend writer on every physics step. It reads `body_com_pos_w`
and `body_link_quat_w`, which on PhysX chains to `_ensure_fk_fresh()` →
`update_articulations_kinematic()` (a scene-global articulation FK pass)
plus `root_view.get_link_transforms()`, then launches the composition
kernel.

That work is unnecessary whenever the buffered wrench is already in a
frame the consumer accepts. This PR moves the frame decision into the
composer, which hands each writer the cheapest valid representation:

- **all-local content** → the local buffers, untouched. An all-local
wrench composes to exactly itself, because the pose data is multiplied
by zero.
- **all-global-at-CoM content** → the global buffers, untouched,
submitted in the world frame by backends that accept one.
- **anything else** → composed as before.

Writers declare what they can consume once at construction
(`supports_world_at_com`) and call `get_forces_and_torques()` per step.
All frame logic stays inside the composer; no writer touches a raw
buffer or knows a frame rule.

## New public API

```python
class WrenchComposer:
    def __init__(self, asset, *, supports_world_at_com: bool = False) -> None: ...
    def get_forces_and_torques(self) -> tuple[wp.array, wp.array, bool]: ...

forces, torques, is_global = composer.get_forces_and_torques()
```

Additive only. `compose_to_body_frame()`, `out_force_b`, and
`out_torque_b` remain public and behaviorally unchanged, so no
deprecation cycle is needed. `supports_world_at_com` defaults to
`False`, which is exactly the previous behavior.

The composer tracks local contributions, global contributions, and
positioned global forces with plain booleans. These flags stay
conservative across partial resets and clear on a full `reset()`;
checking whether a partial reset removed every contribution would
require scanning the buffers. The returned `is_global` boolean follows
the existing asset API and is passed directly to the backend. No frame
enum, content bitmask, or classification helper is needed.

## Equivalence

All three branches are exact, from `compose_wrench_to_body_frame`:

- **local only**: the three global buffers are zero, so both
`quat_rotate_inv` terms vanish and the output is identically
`(local_force_b, local_torque_b)`. Note a *positioned* local force also
qualifies — the kernel folds `cross(P_b, F_b)` straight into
`local_torque_b`.
- **global-at-CoM only**: `global_force_w` and the local buffers are
zero, so `corrected_torque_w == global_torque_w`. Submitting the world
buffers is the same wrench, provided the consumer applies force at the
CoM — verified against the vendored `omni.physics.tensors` API, where
`position_data=None` means "at the link transform" for **both**
`is_global` values, so the flag only reinterprets the vectors' frame and
never moves the application point.
- **anything else**: unchanged.

## Backend coverage

| backend | `supports_world_at_com` | change |
|---|---|---|
| PhysX | `True` | writer passes the returned `is_global` directly |
| OvPhysX | `True` | writer passes `is_global` to the packing kernel to
skip the inline rotate |
| Newton | `False` (default) | writer swaps to
`get_forces_and_torques()`; no constructor change |

OvPhysX is the largest win. Its wrench binding wants a world-frame
wrench, so a global wrench previously round-tripped: the composer
rotated world→body, then the packing kernel rotated body→world. Measured
on this branch with a body rotated 90° about +z, a local force `(1,0,0)`
emits `(0,1,0)` while a global force `(1,0,0)` emits `(1,0,0)` — an
exact round trip, so skipping both rotations is output-preserving. The
packed `[6:9]` link position is still written unconditionally on both
paths.

Newton keeps the body frame because it binds a body-frame array to the
solver. Worth noting for a follow-up: its writer kernel already performs
the same body→world rotation OvPhysX's does, so Newton could plausibly
take the same flag and win the global-at-CoM path too. That is
deliberately out of scope here.

## Validation

The existing composer rotation, positioned-force, merge, reset, and
overwrite tests now exercise `get_forces_and_torques()` directly.
Existing frame/position cases cover the four add/set index/mask APIs.
The existing articulation body-ordering test checks local/world
forwarding, composition avoidance, and packed link positions. One shared
rigid-object writer check replaces the separate backend/equivalence
suites and their duplicate rotation kernel.

Local results:

- `test_wrench_composer.py`: **375 passed** on CPU/CUDA.
- Articulation ordering and rigid-object wrench writer checks: **24
passed** across PhysX, Newton, and OvPhysX.
- `uv run isaaclab -f`: all pre-commit checks passed.
- Structural audit: no new test classes or kernels; no production
changes in the test consolidation. The composer architecture regression
still rejects frame/content enums, the classification layer, and the old
submission alias.

Regression sensitivity was checked by temporarily restoring
unconditional composition and stale-pose caching: the updated existing
tests fail on unnecessary pose reads and on a second read after the body
rotates, respectively.

The previously recorded paired Python dispatch microbenchmark changed
local buffer selection from about 99 ns to 46 ns per call, and
global-at-CoM selection from 127 ns to 60 ns. This measures buffer
selection only, not end-to-end simulation; production code is unchanged
by the test consolidation.

## Performance

The end-to-end measurements below were recorded for the original
optimization; they were not rerun for the boolean/API simplification.

Real PhysX, RTX 5090, ANYMAL-C (17 bodies) × 4096 envs = 69,632 links.
Same-process paired A/B with the arm order alternated per iteration,
400–500 pairs, 95% CI on paired differences. Baseline is
`compose_to_body_frame()` — what the writers previously called
unconditionally.

| scope | content | baseline | patched | paired delta | 95% CI |
|---|---|---:|---:|---:|---|
| composition path only | global-at-CoM | 0.2194 ms | 0.0191 ms |
**−0.2003 ms** (−91.3%) | [0.1915, 0.2091] |
| composition path only | local | 0.1926 ms | 0.0159 ms | **−0.1767 ms**
(−91.8%) | [0.1712, 0.1822] |
| full `write_data_to_sim` | global-at-CoM | 1.0799 ms | 0.9897 ms |
**−0.0902 ms** (−8.4%) | [0.0797, 0.1007] |
| full `write_data_to_sim` | local | 1.0072 ms | 0.9305 ms | **−0.0767
ms** (−7.6%) | [0.0719, 0.0815] |

Read the end-to-end row as the honest headline: **~0.08 ms per asset per
physics step**. Roughly half the isolated saving does not reach the
caller, because other work in `write_data_to_sim` touches body poses
anyway and pays part of that cost regardless. A cuboid rigid-object
scene at the same env count saved only ~6.5 µs, as expected — there is
no articulation FK pass to skip.

Scope caveat: no in-tree task sets `is_global=True` today (both shipped
`apply_external_force_torque` events use the local default), so the
local path is what delivers value now, and it is the one that reaches
every backend.

## Supersedes

This replaces two open PRs, both of which found one half of this
problem:

- **#7431** (@NeoZng) identified the global-at-CoM case and the cost of
the pose read. Its mechanism is correct and its writer-level measurement
is corroborated here. It is superseded on placement rather than
substance: it put the frame decision in three PhysX writers behind a
public eligibility flag, leaving Newton and OvPhysX unserved. The credit
for the world-at-CoM half of this mechanism is theirs.
- **#7362** cached composition for unchanged local wrenches. Superseded
because not composing at all is strictly stronger — the cache does
nothing when the wrench is rewritten every step, which is the common
case for a per-step randomized push.

## Type of change

- New feature (non-breaking change which adds functionality)

## Checklist

- [x] I have read and understood the [contribution
guidelines](https://isaac-sim.github.io/IsaacLab/main/source/refs/contributing.html)
- [x] I have run the `pre-commit` checks with `./isaaclab.sh --format`
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] I have added a changelog fragment under
`source/<pkg>/changelog.d/` for every touched package
- [x] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Octi Zhang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

isaac-lab Related to Isaac Lab team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants