-
Notifications
You must be signed in to change notification settings - Fork 2k
perf: optimize buffer array allocations using np.asarray to avoid unnecessary copies #2154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sxngt
wants to merge
4
commits into
DLR-RM:master
Choose a base branch
from
sxngt:perf/optimize-buffer-array-copies
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+129
−22
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cf0bd78
perf: optimize buffer array allocations using np.asarray
sxngt 7708e80
test: add comprehensive tests for buffer optimization
sxngt 6f6ed05
docs: update changelog for buffer optimization
sxngt a77d068
style: fix import order in test_buffer_optimization.py to comply with…
sxngt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import numpy as np | ||
import pytest | ||
from stable_baselines3.common.buffers import ReplayBuffer | ||
from gymnasium import spaces | ||
|
||
|
||
def test_replay_buffer_no_copy_when_already_array(): | ||
"""Test that ReplayBuffer avoids unnecessary copies when inputs are already numpy arrays.""" | ||
obs_space = spaces.Box(low=-1, high=1, shape=(4,), dtype=np.float32) | ||
action_space = spaces.Box(low=-1, high=1, shape=(2,), dtype=np.float32) | ||
buffer = ReplayBuffer(buffer_size=10, observation_space=obs_space, action_space=action_space) | ||
|
||
# Create numpy arrays | ||
obs = np.array([1, 2, 3, 4], dtype=np.float32) | ||
next_obs = np.array([2, 3, 4, 5], dtype=np.float32) | ||
action = np.array([0.5, -0.5], dtype=np.float32) | ||
reward = np.array([1.0], dtype=np.float32) | ||
done = np.array([False], dtype=np.float32) | ||
|
||
# Add to buffer | ||
buffer.add(obs, next_obs, action, reward, done, [{}]) | ||
|
||
# Verify data was stored correctly | ||
assert np.array_equal(buffer.observations[0], obs) | ||
assert np.array_equal(buffer.next_observations[0], next_obs) | ||
assert np.array_equal(buffer.actions[0], action) | ||
assert np.array_equal(buffer.rewards[0], reward) | ||
assert np.array_equal(buffer.dones[0], done) | ||
|
||
# Verify that modifying original arrays doesn't affect buffer (copy was made for observations) | ||
obs[:] = 0 | ||
next_obs[:] = 0 | ||
assert not np.array_equal(buffer.observations[0], obs) | ||
assert not np.array_equal(buffer.next_observations[0], next_obs) | ||
|
||
# Actions, rewards, dones don't need copy protection | ||
action[:] = 99 | ||
reward[:] = 99 | ||
done[:] = 1 | ||
# These may or may not be equal depending on implementation details | ||
# The important thing is that the buffer functions correctly | ||
|
||
|
||
def test_replay_buffer_handles_lists_and_scalars(): | ||
"""Test that ReplayBuffer correctly handles different input types.""" | ||
obs_space = spaces.Box(low=-1, high=1, shape=(4,), dtype=np.float32) | ||
action_space = spaces.Discrete(3) | ||
buffer = ReplayBuffer(buffer_size=10, observation_space=obs_space, action_space=action_space) | ||
|
||
# Test with lists | ||
obs_list = [1.0, 2.0, 3.0, 4.0] | ||
next_obs_list = [2.0, 3.0, 4.0, 5.0] | ||
action_scalar = 1 | ||
reward_scalar = 2.5 | ||
done_bool = True | ||
|
||
buffer.add(obs_list, next_obs_list, action_scalar, reward_scalar, done_bool, [{}]) | ||
|
||
# Verify conversion worked | ||
assert buffer.observations[0].shape == (4,) | ||
assert buffer.actions[0].shape == (1,) | ||
assert isinstance(buffer.rewards[0], np.ndarray) | ||
assert isinstance(buffer.dones[0], np.ndarray) | ||
|
||
|
||
def test_replay_buffer_memory_optimization_mode(): | ||
"""Test that memory optimization mode works correctly with the optimization.""" | ||
obs_space = spaces.Box(low=0, high=255, shape=(84, 84, 4), dtype=np.uint8) | ||
action_space = spaces.Discrete(4) | ||
|
||
buffer = ReplayBuffer( | ||
buffer_size=100, | ||
observation_space=obs_space, | ||
action_space=action_space, | ||
optimize_memory_usage=True | ||
) | ||
|
||
obs = np.random.randint(0, 255, size=(84, 84, 4), dtype=np.uint8) | ||
next_obs = np.random.randint(0, 255, size=(84, 84, 4), dtype=np.uint8) | ||
|
||
buffer.add(obs, next_obs, 2, 1.0, False, [{}]) | ||
|
||
# In optimize_memory_usage mode, next_obs is stored at (pos + 1) % buffer_size | ||
assert np.array_equal(buffer.observations[0], obs) | ||
assert np.array_equal(buffer.observations[1], next_obs) | ||
|
||
# Verify buffer doesn't have next_observations array | ||
assert not hasattr(buffer, 'next_observations') or buffer.next_observations is None | ||
|
||
|
||
def test_replay_buffer_discrete_observation_space(): | ||
"""Test that discrete observation spaces are handled correctly.""" | ||
obs_space = spaces.Discrete(10) | ||
action_space = spaces.Discrete(2) | ||
buffer = ReplayBuffer(buffer_size=10, observation_space=obs_space, action_space=action_space) | ||
|
||
obs = 5 | ||
next_obs = 7 | ||
action = 1 | ||
|
||
buffer.add(obs, next_obs, action, 1.0, False, [{}]) | ||
|
||
# Check reshaping worked correctly | ||
assert buffer.observations[0].shape == (1,) | ||
assert buffer.observations[0][0] == 5 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the difference between that and simply
np.array()
?I also need to check if it's needed at all (in the sense if side effects are possible)