Repository navigation
Fix Newton deferred graph warmup - #5983
AntoineRichard wants to merge 9 commits into
Conversation
There was a problem hiding this comment.
Review Summary
This PR fixes #5961 where the CUDA graph warmup run was inadvertently modifying state that would leak into the first user-visible physics step.
Root Cause & Fix
The warmup step in _capture_relaxed_graph() advances Newton outside normal IsaacLab step bookkeeping, purely to trigger lazy solver allocations before graph capture. The fix snapshots _state_0 and _state_1 before warmup, runs the warmup, then restores both object references and tensor data in a finally block — ensuring the internal warmup's state changes do not leak into the first real graph replay.
Incremental Review (e632b39)
The new commit addresses feedback from @pbarejko's review:
Changes Made
-
Changelog clarified: The entry now accurately describes the fix as preventing "the internal warmup step" from leaking state, rather than the previous wording about preserving external wrenches. This better communicates what the code actually does.
-
Docstring improved: The explanation of the warmup purpose is now clearer — emphasizes that warmup triggers lazy solver allocations and that state is restored so the internal advance doesn't become the initial state for real replays.
-
Removed redundant
wp.synchronize_stream:- Before: Two syncs — one after
simulate(), one after.assign()in finally - After: One sync — only after the
.assign()calls complete
This addresses @pbarejko's question about why two syncs were needed. The warmup
simulate()doesn't need a sync afterward because we're immediately restoring state anyway — we only need to ensure the restoration completes before graph capture begins. - Before: Two syncs — one after
Assessment
✅ The changelog now accurately describes the fix
✅ Double sync removed as @pbarejko questioned
✅ Comments/docstrings are clearer about the warmup's purpose
✅ pre-commit passed, CI still running
Previous Findings (from initial review)
1. Mixed cls vs NewtonManager in finally block — Minor (Style)
finally:
NewtonManager._state_0 = state_0 # explicit class name
NewtonManager._state_1 = state_1
cls._state_0.assign(state_0_snapshot) # clsStill present. The inconsistency remains (functionally correct, stylistically inconsistent). Non-blocking.
2. Test Coverage — Positive
The regression test properly validates the fix by:
- Forcing the deferred capture path
- Staging a wrench that cancels gravity
- Verifying first-step velocity remains ~0
Verdict
The incremental changes appropriately address reviewer feedback. The changelog is now accurate, and the double-sync question has been resolved by removing the unnecessary first sync. The fix remains correct.
No blocking issues. CI is still in progress at time of review.
Reviewed at e632b39 🤖
Greptile SummaryThis PR fixes a bug in the Newton deferred CUDA graph warmup path where external wrenches (and other user-staged writes to
Confidence Score: 4/5Safe to merge; the change is narrowly scoped to the deferred RTX warmup path, and the snapshot/restore logic correctly handles both single-state and double-buffered substep configurations. The snapshot-and-restore approach is sound: state references are saved before warmup, GPU memory is snapshotted via No files require special attention; the change is confined to a single method in Important Files Changed
Sequence DiagramsequenceDiagram
participant step as step()
participant crg as _capture_relaxed_graph()
participant sim as simulate()
participant stream as CUDA stream
step->>crg: first call (RTX active, _graph_capture_pending)
Note over crg: Save state_0/state_1 refs
crg->>stream: state_0_snapshot.assign(state_0)
crg->>stream: state_1_snapshot.assign(state_1)
Note over crg: [try]
crg->>sim: simulate() — warmup (pre-allocs solver buffers, consumes wrenches)
sim-->>crg: done
crg->>stream: wp.synchronize_stream (drain warmup)
Note over crg: [finally — restore]
crg->>crg: "_state_0 = state_0 (restore ref)"
crg->>crg: "_state_1 = state_1 (restore ref)"
crg->>stream: _state_0.assign(state_0_snapshot) (restore data + wrenches)
crg->>stream: _state_1.assign(state_1_snapshot) (restore data)
crg->>stream: wp.synchronize_stream (drain restore)
Note over crg: Create non-blocking stream, begin relaxed capture
crg->>sim: simulate() — graph capture (sees restored wrenches)
sim-->>crg: done
crg->>crg: wp.capture_end → wp.Graph
crg-->>step: graph
step->>stream: wp.capture_launch(graph)
Reviews (1): Last reviewed commit: "Fix Newton deferred graph warmup" | Re-trigger Greptile |
e53e8f5 to
3f47b17
Compare
| Fixed | ||
| ^^^^^ | ||
|
|
||
| * Fixed deferred Newton CUDA graph capture with Kit/RTX so first-step external wrenches are preserved. |
There was a problem hiding this comment.
What I really find ambiguous. The changes are related to newton manager, however the comment explains CUDA graph capture with Kit/RTX.
Also, I have been wondering what does this really fix? You create snapshots, you stash them, and after sim you recover them.
Why do we need to call wp.synchronize_stream twice?
There was a problem hiding this comment.
Yeah, we were seeing an issue with defered capture.
The warmup steps the simulation without calling lab internals so on the very first step we can get incorrect behaviors. So we snapshot the state before warm-up and restore them after the warmup that's it.
We may not need the wp.sync twice.
|
@YizeWang is this still needed? |
Restore Newton states after relaxed CUDA graph warmup so first-step staged external wrenches are consumed by the captured physics step instead of the warmup step.
e632b39 to
5142b7f
Compare
Clone arrays from the live state instead of constructing snapshots from the model. This preserves warmup rollback when solver-owned state buffers have been lazily resized.
Refresh the rendering baselines with outputs from the official CI runner. The corrected deferred warmup rollback removes a hidden Newton step and intentionally shifts the cloth trajectory.
| wp.synchronize_device("cuda:0") | ||
|
|
||
| joint_qd = NewtonManager._state_0.joint_qd.numpy() | ||
| assert np.isclose(joint_qd[2], 0.0, atol=1e-3, rtol=0.0) |
There was a problem hiding this comment.
You observe here effect of gravity, why not additionally observe initial conditions from joint_q? They should be all initial values?
|
run-ci |
|
this probably needs a rebase, but I don't seem to have access to do that |
|
@AntoineRichard I merged the latest Besides resolving conflicts, the merge adapts the fix to
I also benchmarked it against |
|
Closing as this is now fixed by #8058 using native warp graph capture in RELAXED mode. More elegant fix and we can remove all the custom code. |
Description
Fixes #5961.
When Kit/RTX is active, CUDA graph capture is deferred until the first
NewtonManager.step()because initialization may overlap RTX background-stream work. Before capture,_capture_relaxed_graph()performs an eager warmup simulation to allocate Newton solver buffers.The warmup is not a user-visible IsaacLab step, but it was mutating the live state: it can advance positions and velocities, clear a staged
state.body_fwrench, and swap the manager’s double-buffered states. The first graph replay could therefore begin from the warmup state rather than the caller-prepared state.This PR snapshots both state buffers before warmup and restores their references and contents in a
finallyblock before capture begins. Restoring both buffers is required because a solver may ping-pong them during warmup; synchronizing after restoration ensures capture sees the restored data.The regression test forces deferred relaxed capture, stages a wrench that cancels gravity, and checks that the first visible replay has zero vertical velocity. The test uses native Warp arrays and adds no dependencies.
Dependencies: none.
Type of change
Screenshots
N/A — backend-only physics change.
Checklist
pre-commitchecks with./isaaclab.sh -fsource/<pkg>/changelog.d/for every touched package (do not editCHANGELOG.rstor bumpextension.toml— CI handles that)CONTRIBUTORS.mdor my name already exists thereTest Plan
./isaaclab.sh -fgit diff --check