Skip to content

Make CircularBuffer first-push backfill branchless - #6471

Merged
ooctipus merged 2 commits into
isaac-sim:developfrom
ooctipus:zhengyuz/perf/circular-buffer-branchless-backfill
Jul 13, 2026
Merged

ooctipus merged 2 commits into
isaac-sim:developfrom
ooctipus:zhengyuz/perf/circular-buffer-branchless-backfill

Conversation

@ooctipus

@ooctipus ooctipus commented Jul 12, 2026 •

Copy link
Copy Markdown
Collaborator

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

#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_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

  • I have run the pre-commit checks with ./isaaclab.sh --format
  • I have added my changelog fragment under changelog.d

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.
@ooctipus
ooctipus requested a review from jtigue-bdai as a code owner July 12, 2026 10:49
@github-actions github-actions Bot added the isaac-lab Related to Isaac Lab team label Jul 12, 2026
@greptile-apps

greptile-apps Bot commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR makes CircularBuffer first-push backfill branchless. The main changes are:

  • Replaces the torch.any gate and boolean-mask assignment in CircularBuffer.append.
  • Uses in-place torch.where for stacked and non-stacked buffer layouts.
  • Adds a changelog entry for the performance change.

Confidence Score: 4/5

The non-stacked CircularBuffer backfill path needs a mask-shape fix before merging.

  • The changed torch.where call can align the reset mask with the wrong dimension for multi-dimensional observations.
  • This affects the reset backfill path used by history-enabled buffers.
  • The changelog-only file has no code risk.

source/isaaclab/isaaclab/utils/buffers/circular_buffer.py

Important Files Changed

Filename Overview
source/isaaclab/isaaclab/utils/buffers/circular_buffer.py Changes first-push backfill to use branchless torch.where; the non-stacked mask shape can mis-broadcast for multi-dimensional data.
source/isaaclab/changelog.d/zhengyuz-perf-circular-buffer-branchless-backfill.rst Adds a changelog note for the CircularBuffer backfill performance change.

Reviews (1): Last reviewed commit: "Make CircularBuffer first-push backfill ..." | Re-trigger Greptile

Comment on lines +195 to +196
if self._stack_dim_internal is None:
mask = is_first_push.view(1, -1, *([1] * (data.ndim - 1)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.
@ooctipus
ooctipus merged commit aec76c0 into isaac-sim:develop Jul 13, 2026
119 of 128 checks passed
matthewtrepte pushed a commit to matthewtrepte/IsaacLab that referenced this pull request Aug 4, 2026
## 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`
@ooctipus
ooctipus deleted the zhengyuz/perf/circular-buffer-branchless-backfill branch October 14, 2026 14:29
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