Skip to content
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

Update and document GPU buffer handling #2751

Merged
merged 13 commits into from
Feb 14, 2025

Conversation

TomAugspurger
Copy link
Contributor

@TomAugspurger TomAugspurger commented Jan 22, 2025

This updates how we handle GPU buffers. See the new docs page for a simple example.

The basic idea, as discussed in #2574 is to use host buffers for all metadata objects and device buffers for data.

Zarr has two types of buffers: plain buffers (used for a stream of bytes) and ndbuffers (used for bytes that represent ndarrays). To make it easier for users, I've added a new config option zarr.config.enable_gpu() that can be used to update those both. If we need additional customizations in the future (like gpu-accelerated codecs), we can add them here.

I've opened this as a draft for now. I want to look a bit more at exactly when data is copied between the host and device. Right now, it looks like the default Zarr v3 codec pipeline will automatically transfer bytes to the host in ZstdCodec._encode_single:

as_numpy_array_wrapper, self._zstd_codec.encode, chunk_bytes, chunk_spec.prototype

This PR ensures that you end up with GPU bytes when reading data, but all data I/O and encoding / decoding still happens on the CPU. I think in the future we'll be able to do more work on the GPU while avoiding copies back to the host.

TODO:

  • Add unit tests and/or doctests in docstrings
  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • New/modified features documented in docs/user-guide/*.rst
  • Changes documented as a new file in changes/
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

Clsoes #2574

This updates how we handle GPU buffers. See the new docs page for a
simple example.

The basic idea, as discussed in ..., is to use host buffers for all
metadata objects and device buffers for data.

Zarr has two types of buffers: plain buffers (used for a stream of
bytes) and ndbuffers (used for bytes that represent ndarrays). To make
it easier for users, I've added a new config option
`zarr.config.enable_gpu()` that can be used to update those both. If
we need additional customizations in the future, we can add them here.
@TomAugspurger TomAugspurger changed the title Update GPU handling Update and document GPU buffer handling Jan 22, 2025
@TomAugspurger
Copy link
Contributor Author

I've opened this as a draft for now. I want to look a bit more at exactly when data is copied between the host and device. Right now, it looks like the default Zarr v3 codec pipeline will automatically transfer bytes to the host in ZstdCodec._encode_single:

Still looking at this, but there is currently a copy to the host for running Zstd (the zarr.codecs.zstd.ZstdCodec codec explicitly asks for a NumPy array, which (silently) copies from device to host if necessary). I'll probably leave codecs for a separate issue / PR, but I want to at least get an example working where everything stays on the GPU before moving this out of draft.

cc @madsbk.

@github-actions github-actions bot added the needs release notes Automatically applied to PRs which haven't added release notes label Jan 30, 2025
@TomAugspurger TomAugspurger marked this pull request as ready for review January 30, 2025 21:44
@TomAugspurger
Copy link
Contributor Author

This should be ready for review now.

This fixes the issue with storing metadata in host memory, rather than device memory.

It adds a convenience function for setting up everything to use GPUs. Currently, that just sets the buffer prototype. In the future, I think it could also update the default codecs to use GPU codecs. https://gist.github.com/TomAugspurger/ba13bc29b27f587ae4709ac7f30d89c8 has a snippet hacking together an example of what that future might look like, but IIUC @akshaysubr is working on new APIs for compression / codecs, so IMO it makes sense to address that separately. For now, I've left a note that codecs will run on the CPU by default.

Copy link
Contributor

@madsbk madsbk left a comment

Choose a reason for hiding this comment

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

Looks good, I only have a minor suggestion

Copy link
Contributor

@akshaysubr akshaysubr left a comment

Choose a reason for hiding this comment

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

This looks good to me, approving!

@TomAugspurger
Copy link
Contributor Author

TomAugspurger commented Feb 5, 2025

@dstansby would you be able to confirm that I did the changelog correctly? Happy to add a note to the contributing guide like

Pull requests should usually include a note for the changelog as a towncrier news fragment. Use the GitHub issue number along with the type of your change (feature, bugfix, doc, removal, misc)

towncrier create

You'll be prompted to input the issue number, fix type, and text of the changelog. See the towncrier docs for more.

@dstansby
Copy link
Contributor

dstansby commented Feb 5, 2025

Release notes look good to me 👍 Adding that text in our contributor guide would be awesome (and TIL that you can use towncrier create, I've been doing them by hand until now!)

@github-actions github-actions bot removed the needs release notes Automatically applied to PRs which haven't added release notes label Feb 5, 2025
@TomAugspurger
Copy link
Contributor Author

Should be good to go.

Copy link
Contributor Author

@TomAugspurger TomAugspurger left a comment

Choose a reason for hiding this comment

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

@jhamman or would @d-v-b would you be able to take a look at this sometime? Thanks!

"""
Configure Zarr to use GPUs where possible.
"""
return self.set(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dcherian dcherian enabled auto-merge (squash) February 14, 2025 15:39
@dcherian dcherian merged commit 24ef221 into zarr-developers:main Feb 14, 2025
29 of 30 checks passed
weiji14 added a commit to xarray-contrib/cupy-xarray that referenced this pull request Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants