Repository navigation
Conversation
Boolean-mask indexing runs a host-synchronizing nonzero and the any() gate converts a device scalar to a Python bool; both stall the stream on every append while resets are pending. torch.where with out= keeps the backfill on-device.
Greptile SummaryThis PR makes CircularBuffer first-push backfill branchless. The main changes are:
Confidence Score: 4/5The non-stacked CircularBuffer backfill path needs a mask-shape fix before merging.
source/isaaclab/isaaclab/utils/buffers/circular_buffer.py Important Files Changed
Reviews (1): Last reviewed commit: "Make CircularBuffer first-push backfill ..." | Re-trigger Greptile |
| if self._stack_dim_internal is None: | ||
| mask = is_first_push.view(1, -1, *([1] * (data.ndim - 1))) |
There was a problem hiding this comment.
Mask Broadcasts Across Wrong Axis
When stack_dim is None and data has more than one dimension, this mask has shape (1, batch, ...), but torch.where aligns broadcast dimensions from the right against _buffer shaped (history, batch, ...). For a common shape such as data=(8192, obs_dim), the mask aligns its batch axis with the feature axis instead of _buffer's batch axis, so append can either raise a broadcast error or backfill the wrong elements after a reset.
An empty reset otherwise arms the first-push backfill, costing one full-buffer pass on the next append. Carried over from isaac-sim#5395.
## Description `CircularBuffer.append` backfills all history slots on the first push after a reset. The current implementation gates the backfill on `torch.any(is_first_push)` (device-scalar to Python bool) and applies it with boolean-mask indexing (host-synchronizing `nonzero`). With per-env resets, both fire on nearly every append at scale: with 8 history-enabled observation buffers this costs ~24 stream syncs per env step at 8192 envs. This change applies the backfill branchlessly with `torch.where(..., out=self._buffer)`: in-place, no allocation, no host synchronization, identical results. ## Benchmark Standalone A/B on this file only (RTX 5090, torch CUDA): `batch_size=8192`, `max_len=5`, feature dim 32, 2000 steps, each step resetting a random 2% of batch ids (typical episode-termination rate) followed by one append. Warmup excluded, `torch.cuda.synchronize()` outside the timed loop. | layout | develop | this PR | speedup | |---|---|---|---| | legacy (`stack_dim=None`) | 143.3 µs/append | 78.6 µs/append | **1.82×** | | stacked (`stack_dim=1`) | 152.0 µs/append | 85.9 µs/append | **1.77×** | | no pending resets (both layouts' shared path) | 30.1 µs/append | 30.0 µs/append | 1.00× | Equivalence: after the 2000 mixed reset/append steps, `torch.equal(buffer_old, buffer_new)` is `True` in both layouts. In a full dexsuite training loop (Isaac-Lift-Franka, Newton/mjwarp, 8192 envs) this was part of a sweep that removed ~56 of ~133 `cudaStreamSynchronize` calls per env step, worth ~+20% collection FPS overall. ## Related work isaac-sim#5395 and isaac-sim#4275 attacked the same sync class earlier; most of their content has since landed via the frame-stacking refactor (isaac-sim#5574/isaac-sim#5849): the CPU-side `_need_reset` gate, `_max_len_int` (no `.item()` in the `max_length` property), the sync-free `__getitem__`, and the `DelayBuffer` clone removal are all in `develop` today. The masked first-push backfill inside the `_need_reset` branch is the one remaining per-step sync from that series — this PR completes it. ## Type of change - Bug fix / performance (non-breaking) ## Checklist - [x] I have run the `pre-commit` checks with `./isaaclab.sh --format` - [x] I have added my changelog fragment under `changelog.d`
Description
CircularBuffer.appendbackfills all history slots on the first push after a reset. The current implementation gates the backfill ontorch.any(is_first_push)(device-scalar to Python bool) and applies it with boolean-mask indexing (host-synchronizingnonzero). With per-env resets, both fire on nearly every append at scale: with 8 history-enabled observation buffers this costs ~24 stream syncs per env step at 8192 envs.This change applies the backfill branchlessly with
torch.where(..., out=self._buffer): in-place, no allocation, no host synchronization, identical results.Benchmark
Standalone A/B on this file only (RTX 5090, torch CUDA):
batch_size=8192,max_len=5, feature dim 32, 2000 steps, each step resetting a random 2% of batch ids (typical episode-termination rate) followed by one append. Warmup excluded,torch.cuda.synchronize()outside the timed loop.stack_dim=None)stack_dim=1)Equivalence: after the 2000 mixed reset/append steps,
torch.equal(buffer_old, buffer_new)isTruein both layouts.In a full dexsuite training loop (Isaac-Lift-Franka, Newton/mjwarp, 8192 envs) this was part of a sweep that removed ~56 of ~133
cudaStreamSynchronizecalls per env step, worth ~+20% collection FPS overall.Related work
#5395 and #4275 attacked the same sync class earlier; most of their content has since landed via the frame-stacking refactor (#5574/#5849): the CPU-side
_need_resetgate,_max_len_int(no.item()in themax_lengthproperty), the sync-free__getitem__, and theDelayBufferclone removal are all indeveloptoday. The masked first-push backfill inside the_need_resetbranch is the one remaining per-step sync from that series — this PR completes it.Type of change
Checklist
pre-commitchecks with./isaaclab.sh --formatchangelog.d