-
Notifications
You must be signed in to change notification settings - Fork 85
Zarr+CuPy+GDS+nvCOMP made easy #267
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 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
910fb3a
cleanup
madsbk 5a24541
Impl. compressor config overwrite
madsbk 1c050bd
example
madsbk 92de6d4
doc
madsbk 68669dc
Impl. CompatCompressor
madsbk d7bc19b
doc
madsbk 768f503
Spelling
madsbk 656d439
Spelling
madsbk 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 |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. | ||
| # See file LICENSE for terms. | ||
|
|
||
| import cupy | ||
| import numpy | ||
| import zarr | ||
|
|
||
| import kvikio | ||
| import kvikio.zarr | ||
|
|
||
|
|
||
| def main(path): | ||
| a = cupy.arange(20) | ||
|
|
||
| # Let's use KvikIO's convenience function `open_cupy_array()` to create | ||
| # a new Zarr file on disk. Its semantic is the same as `zarr.open_array()` | ||
| # but uses a GDS file store, nvCOMP compression, and CuPy arrays. | ||
| z = kvikio.zarr.open_cupy_array(store=path, mode="w", shape=(20,), chunks=(5,)) | ||
|
|
||
| # `z` is a regular Zarr Array that we can write to as usual | ||
| z[0:10] = numpy.arange(0, 10) | ||
| # but it also support direct reads and writes of CuPy arrays | ||
| z[10:20] = cupy.arange(10, 20) | ||
|
|
||
| # Reading `z` returns a CuPy array | ||
| assert isinstance(z[:], cupy.ndarray) | ||
| assert (a == z[:]).all() | ||
|
|
||
| # Normally, we cannot assume that GPU and CPU compressors are compatible. | ||
| # E.g., `open_cupy_array()` uses nvCOMP's Snappy GPU compression by default, | ||
| # which, as far as we know, isn’t compatible with any CPU compressor. Thus, | ||
| # let’s re-write our Zarr array using a CPU and GPU compatible compressor. | ||
| z = kvikio.zarr.open_cupy_array( | ||
| store=path, | ||
| mode="w", | ||
| shape=(20,), | ||
| chunks=(5,), | ||
| compressor=kvikio.zarr.CompatCompressor.lz4(), | ||
| ) | ||
| z[:] = a | ||
|
|
||
| # Because we are using a CompatCompressor, it is now possible to open the file | ||
| # using Zarr's built-in LZ4 decompressor that uses the CPU. | ||
| z = zarr.open_array(path) | ||
| # `z` is now read as a regular NumPy array | ||
| assert isinstance(z[:], numpy.ndarray) | ||
| assert (a.get() == z[:]).all() | ||
| # and we can write to is as usual | ||
| z[:] = numpy.arange(20, 40) | ||
|
|
||
| # And we can read the Zarr file back into a CuPy array. | ||
| z = kvikio.zarr.open_cupy_array(store=path, mode="r") | ||
| assert isinstance(z[:], cupy.ndarray) | ||
| assert (cupy.arange(20, 40) == z[:]).all() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main("/tmp/zarr-cupy-nvcomp") |
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
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.