Repository navigation
Optimizes the performance of circular buffer - #4275
iansseijelly wants to merge 8 commits into
Conversation
Greptile SummaryOptimized circular buffer performance by eliminating GPU-CPU synchronization in the hot path, addressing the performance issue identified in #4274. Key optimizations:
Performance impact: Code correctness:
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Actuator as DelayedPDActuator
participant DelayBuf as DelayBuffer
participant CircBuf as CircularBuffer
Note over Actuator,CircBuf: Hot Path (called every physics step)
Actuator->>DelayBuf: compute(control_action.joint_positions)
DelayBuf->>CircBuf: append(data)
alt First time after reset
CircBuf->>CircBuf: Check _all_initialized flag (false)
CircBuf->>CircBuf: Check is_first_push = (_num_pushes == 0)
CircBuf->>CircBuf: Call .any().item() (GPU sync)
CircBuf->>CircBuf: Initialize buffer if needed
CircBuf->>CircBuf: Set _all_initialized = true
else All batches initialized (optimized path)
CircBuf->>CircBuf: Skip initialization check
Note over CircBuf: No GPU-CPU sync needed!
end
CircBuf->>CircBuf: Increment _num_pushes
DelayBuf->>CircBuf: __getitem__(time_lags)
CircBuf-->>DelayBuf: Return delayed data (view)
DelayBuf-->>Actuator: Return delayed data (no clone)
Actuator->>Actuator: In-place assign with [:]
Note over Actuator,CircBuf: Optimizations Applied:<br/>1. Cached max_length as int (avoid .item())<br/>2. Skip initialization check after warmup<br/>3. Removed unnecessary .clone()<br/>4. In-place assignment in actuator
|
Greptile found no issues!From now on, if a review finishes and we haven't found any issues, we will not post anything, but you can confirm that we reviewed your changes in the status check section. This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR". |
|
Hi, this is a cool optimization, I'm excited to try it, thank you! Two quick qs:
|
| control_action.joint_positions = self.positions_delay_buffer.compute(control_action.joint_positions) | ||
| control_action.joint_velocities = self.velocities_delay_buffer.compute(control_action.joint_velocities) | ||
| control_action.joint_efforts = self.efforts_delay_buffer.compute(control_action.joint_efforts) | ||
| control_action.joint_positions[:] = self.positions_delay_buffer.compute(control_action.joint_positions) |
There was a problem hiding this comment.
Is the assignment operation here needed? 🤔
There was a problem hiding this comment.
This uses slice assignment to keep the original tensor storage (control_action.joint_positions etc.) and overwrite its contents in place. Without this, compute() may return a new tensor, causing buffer replacement and additional allocation or copy overhead.
There was a problem hiding this comment.
The delay buffer anyway returns a copied tensor since the time-lags indexed the torch tensor internally. The operation here will do another copy of that tensor which I don't think is needed. Also it will override the initial command set into the environment (after action processing). This may affect the next decimation consequently.
There was a problem hiding this comment.
@iansseijelly and @T-K-233 : Let me know your thoughts on the above. Thanks again!
There was a problem hiding this comment.
Sorry, I missed this comment earlier.
The intention of line this was just to replace the .clone called from the original compute here to save the cost of one allocation. Indeed, the time-lag index should create a copied tensor, so if the original clone is unuseful, then so is this one.
Regarding the decimation comment, I do not fully get it. But the decimation semantics should remain unchanged before and after this PR.
There was a problem hiding this comment.
I agree it'd be better removing the [:] in these lines if they are not necessary.
There was a problem hiding this comment.
Thanks, I have removed them accordingly.
|
Awesome, thank you for answering my questions, I really appreciate it! |
AntoineRichard
left a comment
There was a problem hiding this comment.
The policies are training well on this branch and they do transfer to main, so it's likely the implementation is correct.
Code-wise it also looks good. I've suggested some modification to further improve the performance, but it's not as substantial as removing the cloning step. If we are in a rush we can get this in as is. These will likely get reworked to support warp anyway.
| if self._need_reset: | ||
| if self._buffer is None or (self._num_pushes == 0).any().item(): | ||
| raise RuntimeError("Attempting to retrieve data on an empty circular buffer. Please append data first.") |
There was a problem hiding this comment.
Mostly a NIT but I'm not sure, about these lines.
If you need a reset, do we need to perform that extra check to raise the exception?
Also what happens if the self._need_reset flag is True, but none of the other checks are True. Should we still return the buffer?
It looks to me that if _need_reset is False, then append should have made sure none of the other conditions could be True.
There was a problem hiding this comment.
In practice, this is never raised. I wanted to match the semantics of the original code, but guard the check with a scalar value to make it cheaper, so hence the change. The assumption here is that when _need_reset is True, the conditions in here better be true, because _need_reset is a shadow boolean state in cpu.
There was a problem hiding this comment.
could we simplify this to check only for self._need_reset then if that's the only value needed?
There was a problem hiding this comment.
Yeah, sounds good. I have made teh changes accordingly. This should also be safe, since overall we are expected to only call getitem after at least one append.
| @@ -100,6 +104,8 @@ def reset(self, batch_ids: Sequence[int] | None = None): | |||
| batch_ids = slice(None) | |||
| # reset the number of pushes for the specified batch indices | |||
| self._num_pushes[batch_ids] = 0 | |||
| # reset is needed on next update to fill entire buffer with initial data | |||
| self._need_reset = True | |||
| if self._buffer is not None: | |||
| # set buffer at batch_id reset indices to 0.0 so that the buffer() getter returns the cleared circular buffer after reset. | |||
| self._buffer[:, batch_ids, :] = 0.0 | |||
There was a problem hiding this comment.
Consider replacing with:
self._buffer[:, batch_ids].zero_()
Benchmarking with BATCH_SIZE=4096, NUM_JOINTS=12, HISTORY_LENGTH=6
====================================================================================================
Scenario | Method 1 (= 0.0) | Method 2 (.zero_()) | Best
----------------------------------------------------------------------------------------------------
1 reset (single index) | 20.33 ± 1.51 µs | 19.52 ± 1.70 µs | Method 2 (1.04x)
10 resets | 21.42 ± 1.55 µs | 21.20 ± 1.67 µs | Method 2 (1.01x)
100 resets | 22.24 ± 1.60 µs | 21.64 ± 1.20 µs | Method 2 (1.03x)
500 resets | 21.91 ± 1.03 µs | 21.57 ± 1.10 µs | Method 2 (1.02x)
1000 resets | 22.03 ± 1.30 µs | 21.79 ± 1.16 µs | Method 2 (1.01x)
2048 resets (half) | 22.61 ± 2.16 µs | 22.31 ± 1.34 µs | Method 2 (1.01x)
All resets (N) | 23.23 ± 1.37 µs | 23.18 ± 0.51 µs | Method 2 (1.00x)
====================================================================================================
There was a problem hiding this comment.
I agree with this. Thanks for the detailed microbenchmark.
| is_first_push = self._num_pushes == 0 | ||
| if is_first_push.any().item(): | ||
| self._buffer[:, is_first_push] = data[is_first_push] |
There was a problem hiding this comment.
I would also consider doing:
is_first_push = self._num_pushes == 0
expanded_mask = is_first_push[None, :, None].expand_as(self._buffer)
expanded_data = data[None].expand_as(self._buffer)
torch.where(expanded_mask, expanded_data, self._buffer, out=self._buffer)This should be about 3 times faster than:
is_first_push = self._num_pushes == 0
self._buffer[:, is_first_push] = data[is_first_push]And then it saves you the:
if is_first_push.any().item():which on its own costs about 0.6x the time of:
is_first_push = self._num_pushes == 0
expanded_mask = is_first_push[None, :, None].expand_as(self._buffer)
expanded_data = data[None].expand_as(circular_buffer)
torch.where(expanded_mask, expanded_data, self._buffer, self._buffer)
TLDR; replace:
is_first_push = self._num_pushes == 0
if is_first_push.any().item():
self._buffer[:, is_first_push] = data[is_first_push]
with:
is_first_push = self._num_pushes == 0
if is_first_push.any().item():
expanded_mask = is_first_push[None, :, None].expand_as(self._buffer)
expanded_data = data[None].expand_as(self._buffer)
torch.where(expanded_mask, expanded_data, self._buffer, out=self._buffer)[INFO] Using python from: /home/antoiner/Documents/IsaacLab-Internal/_isaac_sim/python.sh
Benchmarking with BATCH_SIZE=4096, NUM_JOINTS=12, HISTORY_LENGTH=6
============================================================================================================================================
Scenario | direct_mask | where_mask | Best
--------------------------------------------------------------------------------------------------------------------------------------------
1 reset | 94.4± 3.6µs | 25.0± 1.1µs | where_mask
10 resets | 96.8± 4.2µs | 25.3± 0.8µs | where_mask
100 resets | 99.3± 4.2µs | 25.3± 1.7µs | where_mask
500 resets | 99.7± 4.5µs | 25.5± 1.5µs | where_mask
1000 resets | 98.9± 3.4µs | 25.3± 1.0µs | where_mask
2048 resets (half) | 99.8± 3.4µs | 26.0± 1.4µs | where_mask
All resets (N) | 100.3± 3.7µs | 26.0± 1.7µs | where_mask
============================================================================================================================================
There was a problem hiding this comment.
I also agree with this. Looks like it saves a lot of time.
Probably need a little more comment/documentation on what's happening here since mask programming is less interpretable.
2ef7fc8 to
f3061a4
Compare
|
Hi, we have just addressed the suggestions that @AntoineRichard provided. |
|
@iansseijelly Thanks for the improvements and addressing the comments! As we've shifted our development to the new |
2a30454 to
d8658a8
Compare
We just rebased atop |
|
hmm github is not giving me the option to update the branch. could you grab the latest changes from tot develop so that the CI tests can run? we added a fix recently that was preventing the installation from working. |
…n the hot path, avoiding unnecessary kernel and synchronization
…verting all boolean conditions
…y returns a copy.
d03d7bf to
20b53cf
Compare
|
Hi, I just rebased again atop develop. Can you try again? Thanks for the help. |
|
Thanks a lot! seeing a test failure in test_observation_manager.py Dexsuite task in test_environments.py also failing with similar error: |
|
Thanks. We have fixed the logic. I think @AntoineRichard 's solution only applies to the actuator's case. However, Isaac Lab test catches a case where input tensor can be higher dimensional (dim=5 in the failing case). Additionally, we realized that the suggested line torch.where(expanded_mask, expanded_data, self._buffer, out=self._buffer)does not apply any effect. We are reverting to our initial solution. |
|
Hi @iansseijelly, sorry for the delay in getting back to you here. We are still interested in getting this merged in, are you able to update the MR and resolve the conflicts? If you can also post up-to-date benchmark results in the perf difference that would be appreciated! |
|
Thanks @iansseijelly (and apologies for the long open time) — the ideas here have since landed on |
## 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 - [x] I have run the `pre-commit` checks with `./isaaclab.sh --format` - [x] I have added my changelog fragment under `changelog.d`
## 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
This PR addresses issue #4274.
Type of change
Screenshots
Training throughput before and after the patch running training on task
[Isaac-Velocity-Flat-Spot-v0].pre-commitchecks with./isaaclab.sh --formatconfig/extension.tomlfileCONTRIBUTORS.mdor my name already exists there