-
Notifications
You must be signed in to change notification settings - Fork 401
[Feature] Compressed storage gpu #3062
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff88f73
amend
vmoens d91f764
compression is helpful for data transfer
AdrianOrenstein 84d00a8
Move CompressedStorage to CompressedListStorage. Moved out all of the…
53bea2f
Refactor out the storage view. Expose functions in the ListStorage cl…
16aba7f
Using python's default compressor. Created to_bytestream. Created a t…
eade378
add-examples
vmoens 783e3ee
move benchmarks to dedicated workflow
vmoens 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,4 @@ dependencies: | |
- transformers | ||
- ninja | ||
- timm | ||
- safetensors |
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
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 |
---|---|---|
@@ -1,2 +1,7 @@ | ||
pytest-benchmark | ||
tenacity | ||
safetensors | ||
tqdm | ||
pandas | ||
numpy | ||
matplotlib |
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,145 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
import io | ||
import pickle | ||
|
||
import pytest | ||
import torch | ||
try: | ||
from safetensors.torch import save | ||
except ImportError: | ||
save = None | ||
|
||
from torchrl.data import CompressedListStorage | ||
|
||
|
||
class TestCompressedStorageBenchmark: | ||
"""Benchmark tests for CompressedListStorage.""" | ||
|
||
@staticmethod | ||
def make_compressible_mock_data(num_experiences: int, device=None) -> dict: | ||
"""Easily compressible data for testing.""" | ||
if device is None: | ||
device = torch.device("cpu") | ||
|
||
return { | ||
"observations": torch.zeros( | ||
(num_experiences, 4, 84, 84), | ||
dtype=torch.uint8, | ||
device=device, | ||
), | ||
"actions": torch.zeros((num_experiences,), device=device), | ||
"rewards": torch.zeros((num_experiences,), device=device), | ||
"next_observations": torch.zeros( | ||
(num_experiences, 4, 84, 84), | ||
dtype=torch.uint8, | ||
device=device, | ||
), | ||
"terminations": torch.zeros( | ||
(num_experiences,), dtype=torch.bool, device=device | ||
), | ||
"truncations": torch.zeros( | ||
(num_experiences,), dtype=torch.bool, device=device | ||
), | ||
"batch_size": [num_experiences], | ||
} | ||
|
||
@staticmethod | ||
def make_uncompressible_mock_data(num_experiences: int, device=None) -> dict: | ||
"""Uncompressible data for testing.""" | ||
if device is None: | ||
device = torch.device("cpu") | ||
return { | ||
"observations": torch.randn( | ||
(num_experiences, 4, 84, 84), | ||
dtype=torch.float32, | ||
device=device, | ||
), | ||
"actions": torch.randint(0, 10, (num_experiences,), device=device), | ||
"rewards": torch.randn( | ||
(num_experiences,), dtype=torch.float32, device=device | ||
), | ||
"next_observations": torch.randn( | ||
(num_experiences, 4, 84, 84), | ||
dtype=torch.float32, | ||
device=device, | ||
), | ||
"terminations": torch.rand((num_experiences,), device=device) | ||
< 0.2, # ~20% True | ||
"truncations": torch.rand((num_experiences,), device=device) | ||
< 0.1, # ~10% True | ||
"batch_size": [num_experiences], | ||
} | ||
|
||
@pytest.mark.benchmark( | ||
group="tensor_serialization_speed", | ||
min_time=0.1, | ||
max_time=0.5, | ||
min_rounds=5, | ||
disable_gc=True, | ||
warmup=False, | ||
) | ||
@pytest.mark.parametrize( | ||
"serialization_method", | ||
["pickle", "torch.save", "untyped_storage", "numpy", "safetensors"], | ||
) | ||
def test_tensor_to_bytestream_speed(self, benchmark, serialization_method: str): | ||
"""Benchmark the speed of different tensor serialization methods. | ||
|
||
TODO: we might need to also test which methods work on the gpu. | ||
pytest benchmarks/test_compressed_storage_benchmark.py::TestCompressedStorageBenchmark::test_tensor_to_bytestream_speed -v --benchmark-only --benchmark-sort='mean' --benchmark-columns='mean, ops' | ||
|
||
------------------------ benchmark 'tensor_to_bytestream_speed': 5 tests ------------------------- | ||
Name (time in us) Mean (smaller is better) OPS (bigger is better) | ||
-------------------------------------------------------------------------------------------------- | ||
test_tensor_serialization_speed[numpy] 2.3520 (1.0) 425,162.1779 (1.0) | ||
test_tensor_serialization_speed[safetensors] 14.7170 (6.26) 67,948.7129 (0.16) | ||
test_tensor_serialization_speed[pickle] 19.0711 (8.11) 52,435.3333 (0.12) | ||
test_tensor_serialization_speed[torch.save] 32.0648 (13.63) 31,186.8261 (0.07) | ||
test_tensor_serialization_speed[untyped_storage] 38,227.0224 (>1000.0) 26.1595 (0.00) | ||
-------------------------------------------------------------------------------------------------- | ||
""" | ||
|
||
def serialize_with_pickle(data: torch.Tensor) -> bytes: | ||
"""Serialize tensor using pickle.""" | ||
buffer = io.BytesIO() | ||
pickle.dump(data, buffer) | ||
return buffer.getvalue() | ||
|
||
def serialize_with_untyped_storage(data: torch.Tensor) -> bytes: | ||
"""Serialize tensor using torch's built-in method.""" | ||
return bytes(data.untyped_storage()) | ||
|
||
def serialize_with_numpy(data: torch.Tensor) -> bytes: | ||
"""Serialize tensor using numpy.""" | ||
return data.numpy().tobytes() | ||
|
||
def serialize_with_safetensors(data: torch.Tensor) -> bytes: | ||
return save({"0": data}) | ||
|
||
def serialize_with_torch(data: torch.Tensor) -> bytes: | ||
"""Serialize tensor using torch's built-in method.""" | ||
buffer = io.BytesIO() | ||
torch.save(data, buffer) | ||
return buffer.getvalue() | ||
|
||
# Benchmark each serialization method | ||
if serialization_method == "pickle": | ||
serialize_fn = serialize_with_pickle | ||
elif serialization_method == "torch.save": | ||
serialize_fn = serialize_with_torch | ||
elif serialization_method == "untyped_storage": | ||
serialize_fn = serialize_with_untyped_storage | ||
elif serialization_method == "numpy": | ||
serialize_fn = serialize_with_numpy | ||
elif serialization_method == "safetensors": | ||
serialize_fn = serialize_with_safetensors | ||
else: | ||
raise ValueError(f"Unknown serialization method: {serialization_method}") | ||
|
||
data = self.make_compressible_mock_data(1).get("observations") | ||
|
||
# Run the actual benchmark | ||
benchmark(serialize_fn, data) |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.