Repository navigation
Keep Newton CUDA graph captures free of graph memory - #8528
egolubev-nvda wants to merge 1 commit into
Conversation
On Windows, the first graph-memory allocation recorded after the renderer's first frame can stall the CUDA driver for minutes. Serve Python array allocations made during Newton captures from ordinary device memory held until the graph is released, reusing blocks freed in the top-level recording. Fixes isaac-sim#8342.
|
There was a problem hiding this comment.
Isaac Lab Review Bot
Needs attention: Implementation (1), Style consistency (2), Test quality (1). Other reviewed categories look good.
- PR description: The description matches the diff and supplies revisions, hardware, workload, and metrics; add exact benchmark commands, warmup details, and the focused regression-test command.
- Design and architecture: Architecture looks good.
- Scope and over-engineering: No over-engineering issues.
- API: API changes look good.
- Compatibility and deprecation: No breaking changes identified.
- Implementation: Capture-block reuse ignores the calling thread, allowing another thread using the device-wide scoped allocator to take graph-referenced memory; restrict reuse to the capturing thread.
- Style consistency: The new private allocator violates the prescribed module layout by preceding public classes; move it after the module’s public classes.
- Test quality: The allocator regression covers graph memory, replay, capture modes, and release, but its final post-release assertion is vacuous; assert the underlying deallocation or remove it.
Minor fixes needed. Posted 4 inline findings.
Automated review; human maintainers own approval decisions.
| self.bvh_refit = TimestampedBuffer() | ||
|
|
||
|
|
||
| class _GraphLifetimeAllocator: |
There was a problem hiding this comment.
🟡 Warning · Style Consistency — Private class precedes public classes
The documented file structure places private classes after public ones. Move _GraphLifetimeAllocator below NewtonQueries and the other public classes in this module.
| be read by replays, so they are freed when the graph is released. | ||
| """ | ||
|
|
||
| memory_kind = wp.MemoryKind.CUDA_DEVICE |
There was a problem hiding this comment.
🟡 Warning · Style Consistency — Class variable missing ClassVar hint
Class variables must carry a ClassVar[type] hint; ClassVar is already imported here. Annotate as memory_kind: ClassVar[wp.MemoryKind] = wp.MemoryKind.CUDA_DEVICE.
|
|
||
| def allocate(self, size_in_bytes: int) -> int: | ||
| self.allocation_count += 1 | ||
| reusable = self._reusable_blocks.get(size_in_bytes) if self._in_top_level_recording() else None |
There was a problem hiding this comment.
🟡 Warning · Implementation — Capture-time reuse ignores calling thread
_in_top_level_recording queries the capture stream, not the caller, and ScopedAllocator is device-wide. A concurrent thread allocating during capture can receive a block the graph still writes on replay. Record and compare the capturing thread id.
| # After release, frees go straight to the device. | ||
| survivor_ptr = survivors.pop().ptr | ||
| gc.collect() | ||
| assert all(ptr != survivor_ptr for ptr, _ in allocator._held_blocks) |
There was a problem hiding this comment.
🟡 Warning · Test Quality — Vacuous post-release assertion
_held_blocks was just asserted empty and _graph_released prevents further appends, so this check passes regardless of behavior. Assert the device allocator receives the free, or drop these three lines.
# Description On Windows, the first CUDA graph capture after the renderer's first frame can stall for many minutes. This PR applies the workaround from @shi-eric's analysis in #8342: record one tiny allocation in a CUDA graph when `SimulationContext` is created, so the CUDA driver reserves graph memory before anything can block its placement. Fixes #8342. Supersedes #8528. ## Root cause (summary of #8342) - The first allocation recorded in a CUDA graph makes the driver reserve a large virtual-address range for graph memory (96 GiB on our machine). - Cubric allocates managed memory, which creates a 52 GiB managed-memory window. The driver places the graph-memory range directly above that window. - When a rendered frame already occupies those addresses, the driver retries placement 2 MiB higher each time, at about 0.8 s of kernel CPU per attempt. The GPU stays idle; getting past the blocked range takes about 15 minutes. - #8058 moved Newton's first capture to the first physics step, after the first frame. MuJoCo-Warp allocates temporaries on every step, so the capture contains allocations. ## Change - `isaaclab.utils.device.reserve_cuda_graph_memory(device)`: records one `wp.empty(1)` inside `wp.ScopedCapture`, once per CUDA device. It skips non-CUDA devices, devices with the Warp memory pool disabled, and devices that are already capturing. A failure is logged as a warning and never blocks startup. - `SimulationContext.__init__` calls it right after selecting the device, before physics backends and renderers are created. Every entry point (environments, benchmarks, tests) goes through this path. - Runs on all platforms; on platforms without the driver issue the extra capture is harmless. ## Measurements Windows 11, RTX 6000 Ada, driver 596.72, `Isaac-Lift-KukaAllegro-Camera`, 1024 envs, `newton_mjwarp`, capture watchdog 8–15 s: | Setup | `develop` | This PR | |---|---|---| | OVRTX (kitless), `presets=newton_mjwarp,ovrtx,single_camera,simple_shading_full_mdl64` | about 70% of runs stalled | 0 / 10 | | Kit with Isaac Sim RTX, `presets=newton_mjwarp,isaacsim_rtx,single_camera` | 7 / 8 stalled | 0 / 8 | The Kit result means this stall is not limited to OVRTX: Kit with Newton physics hits it too. Other checks with the same workaround (applied at `gym.make`, physics-only task, 8192 envs): - A graph-memory trim right before Newton's capture does not release the early reservation (0 / 8 stalls). - Physics step, full environment step and peak GPU memory are within noise of `develop`. ## Limits - Under Kit, Kit's renderer starts before `SimulationContext`, so the reservation cannot precede it. It still precedes Cubric's managed memory and the next rendered frame, which was enough in the measurements above. - OvPhysX imports `ovstage` in `_prepare_stage_creation`, before this call. OvPhysX with OVRTX was not measured. - Linux, L40, and long training runs were not tested here. - Kit with PhysX physics was not tested. ## Type of change - Bug fix (non-breaking change which fixes an issue) ## Release backport - [x] <!-- backport-active-release --> Backport this pull request to the active release branch after it merges into `develop` ## 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](https://pre-commit.com/) with `uv run isaaclab --format` (ruff check and format on the changed files) - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have validated the changed behavior with focused coverage and applied the [test-audit criteria](https://github.com/isaac-sim/IsaacLab/blob/develop/skills/developer/test-audit/SKILL.md) to any test changes - [x] I have added a changelog fragment under `source/<pkg>/changelog.d/` for every touched package - [ ] I have added my name to the `CONTRIBUTORS.md` or my name already exists there
Description
On Windows, Newton's first CUDA graph capture can stall for many minutes when it runs after the OVRTX renderer's first frame. This PR keeps Newton captures free of graph memory, which removes the trigger.
Fixes #8342.
Root cause
Measured on Windows 11, RTX 6000 Ada, driver 596.72, with
Isaac-Lift-KukaAllegro-Camera(presets=newton_mjwarp,ovrtx,single_camera,simple_shading_full_mdl64):make_constraint,_create_solver_context,implicit, ...). InsideNewtonQueries.capture_graphthese become graph memory nodes (60 allocations per physics graph).ISAAC_LAB_OVRTX_USE_OVSTAGE=0, and Cubric allocates managed memory.cuMemAllocAsync, and in runs that completed the reservation was placed on the first attempt.Change
NewtonQueries.capture_graphinstalls_GraphLifetimeAllocatorfor the capture. It serves Python array allocations made during capture from ordinary device memory, with the thread's capture mode exchanged to relaxed aroundcudaMalloc, as Warp's native sort does for its capture-time allocations. The captured graph has no memory nodes, so the driver never reserves graph memory.capture_while/capture_if) or after recording may still be read by replays, so they are held and freed when the graph is released (weakref.finalize).run_querygraphs. Behavior is the same on all platforms.Measurements
1d5a8fc)Measured on the commit above with the change applied; this branch is the same change rebased onto current
develop.Limits
array_scan,array_sum, radix sort, run-length encode) bypass Python allocators and still create graph memory. Captures that reach them (for example Newton mesh-plane contacts withreduce_contacts, SAP broad phase, deterministic contact sorting) are not covered. The Kuka tasks do not reach them (graph memory measured 0).wp_cuda_thread_exchange_capture_modeandwp_cuda_stream_get_capture_id.wp.ScopedAllocatoris device-wide during capture, so another thread allocating on the same device during capture would get memory held until that graph is released.Type of change
Checklist
pre-commitchecks withuv run isaaclab --format(ruff check and format on the changed files)source/<pkg>/changelog.d/for every touched packageCONTRIBUTORS.mdor my name already exists there