Repository navigation
Refresh PhysX and OVPhysX contact sensor buffers on every physics step - #7614
Conversation
PhysX zeroes the net contact force of a body only on the exact physics step where its contact is lost. The PhysX and OVPhysX contact sensors only called their contact getters when their data was accessed unless the sensor carried a history buffer, so a reader that polls the sensor once per policy step (the default lazy_sensor_update=True with a decimation greater than one) skipped the contact-loss step and kept reporting the last in-contact force until the next touchdown. This only affects GPU simulation; the CPU pipeline zeroes the entry regardless of when the getter runs. Both sensors now refresh the physics-owned buffers on every physics step from update(), while the warp kernels that turn those buffers into sensor data still run lazily on data access. The PhysX CUDA graph capture guard moves into a helper shared by the eager fetch and the buffer update, so an outer capture is rejected on either path. Fixes #7613
Greptile SummaryThis PR fixes stale contact forces in lazy, history-less PhysX and OVPhysX contact sensors by refreshing physics-owned contact buffers after every physics step while retaining lazy Warp-kernel computation.
Confidence Score: 4/5The PR appears safe to merge, with a non-blocking opportunity to avoid eager pose reads for lazy sensors. The contact-loss behavior is covered through the full lazy read path, and no correctness or security failure remains; the only accepted issue is avoidable simulation-rate pose fetching introduced by reusing the full buffer helper. Files Needing Attention: source/isaaclab_ov/isaaclab_ov/sensors/contact_sensor/contact_sensor.py, source/isaaclab_physx/isaaclab_physx/sensors/contact_sensor/contact_sensor.py Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart LR
P[Physics step] --> U[ContactSensor.update]
U --> F[Refresh backend buffers]
F --> C[Stable cached buffer views]
D[Later data access] --> B[_update_buffers_impl]
B --> F
B --> K[Run lazy Warp kernels]
C --> K
K --> O[Sensor data]
Reviews (1): Last reviewed commit: "Refresh contact sensor buffers on every ..." | Re-trigger Greptile |
| super().update(dt, force_recompute=force_recompute) | ||
| # Skip the read if the base class already refreshed the buffers on this step, which it | ||
| # does when the sensor carries a history buffer. | ||
| if self._is_initialized and self._data_generation != self._data_generation_last_update: |
There was a problem hiding this comment.
The eager refresh uses the full buffer-fetch helper, so a lazy, history-less sensor with track_pose=True now reads rigid-body transforms on every physics step. Pose data does not have the contact-loss transient addressed here, and _update_buffers_impl reads it again when the sensor data is consumed. This defeats lazy pose sampling and adds avoidable backend and GPU work at simulation rate. The PhysX path has the same behavior. Please limit the per-step refresh to contact-derived buffers and fetch pose only during data recomputation.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Done in 21280d6: the per-step refresh in update() now calls the fetch helper with include_pose=False on both the PhysX and OVPhysX sensors, so poses are only read on data access as before. Measured on an RTX 5090 at 4096 envs x 17 bodies: the pose read is 32 us per physics step, and sensor.update() with track_pose=True went from 73 us to 48 us, matching the no-pose case.
There was a problem hiding this comment.
Isaac Lab Review Bot
The per-step backend-buffer refresh addresses stale contact loss while keeping Warp computation lazy, but the PhysX implementation introduces an undocumented exception path during outer CUDA graph capture.
- Design and architecture: Refreshing physics-owned buffers in each backend subclass is appropriately scoped, avoids duplicate history-bearing refreshes, and preserves the stable buffers consumed by cached Warp views and the captured compute graph.
- API: Public signatures and return types remain unchanged. However,
ContactSensor.update()for a lazy, history-less PhysX sensor can now raiseRuntimeErrorduring outer CUDA graph capture, whereas that call path previously did not access PhysX. This behavior change needs documentation in the public method and PhysX changelog, including guidance to update the sensor outside capture. - Implementation: The generation-based condition implements the intended cadence: history-less sensors fetch every physics step while remaining computationally lazy, and history-bearing sensors avoid duplicate fetches. The shared PhysX capture guard correctly runs before tensor reads, and the OVPhysX force, force-matrix, and pose paths retain their existing guards.
Minor fixes needed. Posted 1 actionable finding inline.
Automated review; human maintainers own approval decisions.
| # Skip the fetch if the base class already refreshed the buffers on this step, which it | ||
| # does when the sensor carries a history buffer. | ||
| if self._is_initialized and self._data_generation != self._data_generation_last_update: | ||
| self._fetch_physx_buffers() |
There was a problem hiding this comment.
🔵 Suggestion · Api — New capture rejection for lazy history-less sensors
Previously a history_length=0 lazy sensor never touched PhysX inside update(), so callers could run it under an outer CUDA graph capture and read data afterwards. The added fetch reaches _raise_if_graph_capturing() and now raises RuntimeError on that path. The restriction is inherent to the fix, but it is a user-visible behavior change: document it in the update() docstring and the PhysX changelog fragment with guidance to step the sensor outside capture.
There was a problem hiding this comment.
Done in 21280d6: update() now documents the RuntimeError raised under an outer CUDA graph capture, and the PhysX changelog fragment notes that lazy history-less sensors now reject a capture like history-bearing sensors already did, with the guidance to update the sensor outside the capture.
ClemensSchwarke
left a comment
There was a problem hiding this comment.
Is the overhead of reading the pose every step significant? If so, maybe address the bot comment. Otherwise looks good.
|
run-ci |
Only the contact-derived PhysX buffers carry the contact-loss transient that motivates refreshing them on every physics step. Body poses are read again on data access anyway, so the per-step refresh in update() now skips them on both the PhysX and OVPhysX sensors. Document that a lazy history-less PhysX sensor now raises inside an outer CUDA graph capture like history-bearing sensors already did.
|
@ClemensSchwarke Measured it on the RTX 5090 workstation rather than guessing. Short answer: not significant, and the pose read is now out of the per-step path anyway (21280d6). Micro-timing inside the live env (
So the PR adds four extra fetches per env step: about 0.1 ms, or 0.7% of a 16.5 ms env step, for a End-to-end runtime benchmark, same machine,
Every delta is within one standard error of zero. Run-to-run scatter is 3-4% on flat and ~10% on rough, so end-to-end can only bound the effect; the micro-timing above is the actual number. The shipped velocity tasks use The Both bot comments are addressed in 21280d6: pose skipped in the per-step refresh (PhysX and OVPhysX), and the new capture-time |
|
run-ci |
|
run-ci |
|
run-ci |
marcodiiga
left a comment
There was a problem hiding this comment.
Reviewed at 2bd5e60. The per-step contact refresh fixes the stale-force regression while preserving sensor output cadence and avoiding duplicate fetches after eager updates. I found no new correctness blocker.
Validation: 14 focused PhysX tests passed, including CPU/GPU contact loss, history cadence, reset, and CUDA-graph behavior. The ovphysx contact-loss regression passed on CPU and GPU. A GPU two-cube release check also cleared both net and filtered forces; restoring the old update cadence reproduced stale forces in both the ground and two-cube cases. The previous Docker CI reports for this same head also show all four new contact-loss cases passing.
Approving with three non-blocking follow-ups inline (one redundant-fetch optimization and two regression-test additions); please address them despite the approval. The fresh Docker CI run is still in progress. The existing Multi-GPU failure occurred during runner setup, before checkout or test execution.
| # Skip the fetch if the base class already refreshed the buffers on this step, which it | ||
| # does when the sensor carries a history buffer. | ||
| if self._is_initialized and self._data_generation != self._data_generation_last_update: | ||
| self._fetch_physx_buffers(include_pose=False) |
There was a problem hiding this comment.
Non-blocking test follow-up: please extend test_contact_sensor_refuses_update_inside_outer_graph_capture to exercise public update(dt) with history_length=0 and force_recompute=False. The existing test calls _update_buffers_impl() and replaces _fetch_physx_buffers(), so it does not protect the new eager path shown here. Keep the real fetch helper and assert that the capture raises before any backend contact getter is called.
| for _ in range(decimation): | ||
| shape.write_root_pose_to_sim_index(root_pose=root_pose) | ||
| _perform_sim_step(sim, scene, sim_dt) | ||
| return torch.linalg.norm(sensor.data.net_normal_forces_w.torch, dim=-1).max().item() |
There was a problem hiding this comment.
Non-blocking test follow-up for both backend suites: this regression has no partner filter and only checks net_normal_forces_w, so it does not verify filtered contact loss. Please extend the contact-loss coverage with a filtered body pair and assert that normal_force_matrix_w also returns to zero when read once per four physics steps, with a nonzero-contact assertion first.
An ovphysx GPU check with two 0.5 m cubes, no terrain, zero gravity, and 0.05 m overlap followed by 10 m separation cleared both outputs with this fix. Restoring the old update cadence left both outputs stuck around 4687 N. This is worth retaining as a regression alongside the ground-contact case.
| expected_fetches_after_read = 1 if history_length == 0 else expected_fetches | ||
| # A history-less sensor did not recompute during the steps, so the read triggers one | ||
| # more fetch; a history-bearing one is already up to date. | ||
| expected_fetches_after_read = expected_fetches + 1 if history_length == 0 else expected_fetches |
There was a problem hiding this comment.
Non-blocking performance follow-up for both backends: the extra fetch asserted here means that a lazy, history-less sensor fetches contacts in update() and fetches them again when .data is read in the same generation. With decimation 1, this doubles contact-buffer retrievals per physics step. Please consider tracking which sensor generation already fetched contacts so data recomputation can reuse them, while still fetching poses lazily and invalidating that state on reset. The existing cadence test can then protect the single-fetch behavior.
Description
On the PhysX and OVPhysX backends on GPU, a
ContactSensorwith the defaulthistory_length=0and the defaultlazy_sensor_update=Truekept reporting the last in-contact force after the body had left contact, for as long as the body stayed in the air, whenever its data was read less often than every physics step (for example once per policy step withdecimation=4).Root cause. PhysX zeroes a body's entry in
get_net_contact_forces()only on the exact physics step where that body's contact is lost. Withhistory_length=0the sensor only called the getter when.datawas accessed, so a policy-rate reader skipped most contact-lost steps and never saw the zero. The Isaac Lab kernels and the CUDA-graph replay are not involved: the raw getter output, the cached Warp view, and the sensor output agree at every read, and disabling the graph changes nothing. CPU is unaffected.history_length > 0is unaffected because the base class already refreshes every physics step. The experiment table and a standalone reproduction are in #7613; OVPhysX reproduces identically through its contact binding.Fix. Both sensors override
update()to fetch the physics-owned buffers on every physics step, skipping the fetch when the base class already refreshed on that step (history-bearing sensors, orforce_recompute). The Warp kernels that turn the fetched buffers into sensor data still run lazily on data access, so the per-step cost is one getter call, which is exactly what history-bearing sensors already pay. The buffers are refreshed in place, so the cached views and the captured CUDA graph stay valid. On PhysX the outer-graph-capture guard moves into a helper shared by the eager fetch and the buffer update, so a capture is rejected on either path and the existing capture test keeps its "raise before touching PhysX" contract.Tests.
test_lazy_sensor_reports_contact_lossin both the PhysX and OVPhysX contact sensor suites: settle a cube for six policy steps of four physics steps each, reading the sensor once per policy step, then hold it in the air for six more, and require every air read after the first to be below 0.1 N. On the unfixed code it fails oncuda:0with the force stuck at the resting value for all six reads, and passes oncpu; with the fix it passes on both. The PhysXtest_contact_history_updates_at_sensor_periodrow forhistory_length=0asserted the old zero-fetch cadence and was updated to the new one; its history-bearing rows are unchanged.Exposure. Every shipped velocity task sets
history_length=3and is unaffected. The Kuka Allegro and Franka lift tasks configure fingertip contact sensors with the default history length and feed the force matrix into observations, so on PhysX GPU those observations could hold a stale contact force after a finger released the object.Found while working on #7283 / #7574.
Fixes #7613
Type of change
Release backport
developChecklist
pre-commitchecks with./isaaclab.sh --formatsource/<pkg>/changelog.d/for every touched package (do not editCHANGELOG.rstor bumpextension.toml— CI handles that)CONTRIBUTORS.mdor my name already exists there