# Favorite way to go from netCDF (&xarray) to torch/TF/Jax et al

**URL:** <https://discourse.pangeo.io/t/favorite-way-to-go-from-netcdf-xarray-to-torch-tf-jax-et-al/2663>\
**Category:** Data\
**Tags:** location-ncar, machine-learning\
**Created:** [August 17, 2022, 12:39am UTC](https://discourse.pangeo.io/t/favorite-way-to-go-from-netcdf-xarray-to-torch-tf-jax-et-al/2663 "2022-08-17T00:39:50Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![weiji14](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/weiji14/32/389_2.png) [@weiji14](https://discourse.pangeo.io/u/weiji14)\
**Post date:** [August 17, 2022, 1:14pm UTC](https://discourse.pangeo.io/t/favorite-way-to-go-from-netcdf-xarray-to-torch-tf-jax-et-al/2663/6 "2022-08-17T13:14:20Z")

</div>

I love how this discussion is steering more from metadata to speed 🤣 Just to clarify, NetCDF _can_ indeed be fast enough if you’re going from File → CPU-RAM → GPU-RAM (assuming you’ve got enough I/O, RAM, etc) as @rabernat pointed out.

Now, if you want **end-to-end**  **pure speed** (and have enough GPU RAM) then the CPU-RAM to GPU-RAM data transfer will be the main bottleneck. You’ll then need to look at things like GPU direct storage:

- [GPUDirect Storage: A Direct Path Between Storage and GPU Memory | NVIDIA Technical Blog](https://developer.nvidia.com/blog/gpudirect-storage/)
- [NVIDIA GPUDirect Storage Documentation](https://docs.nvidia.com/gpudirect-storage/)

[![image](https://canada1.discourse-cdn.com/flex030/uploads/pangeo/original/2X/9/9c10586bf7cae8d337bc1a2f42b46e995e2def5f.png)](https://developer-blogs.nvidia.com/wp-content/uploads/2019/08/GPUDirect-Fig-1-New.png)

This would involve using libraries that handle loading/pre-processing directly on the GPU like:

- [RAPIDS](https://rapids.ai/), in particular, CUCIM - [GitHub - rapidsai/cucim](https://github.com/rapidsai/cucim) for computer vision/image processing
- [NVIDIA DALI](https://developer.nvidia.com/dali), which does data loading and augmentation on the GPU, see [NVIDIA DALI Documentation — NVIDIA DALI 1.16.0 documentation](https://docs.nvidia.com/deeplearning/dali/archives/dali_1_16_0/user-guide/docs/index.html)

Caveat with this is that you can’t read NetCDFs or most ‘geo’ formats directly into GPU yet (as far as I’m aware). Relevant issues include:

- [[FEA] Support Zarr-based image format (such as NGFF) · Issue #94 · rapidsai/cucim · GitHub](https://github.com/rapidsai/cucim/issues/94)
- [Pythonic TIFF Reader · Issue #1272 · NVIDIA/DALI · GitHub](https://github.com/NVIDIA/DALI/issues/1272)
- [Please expose \_\_cuda\_array\_interface\_\_ via the xarray.\_\_array\_\_() function if present · Issue #6847 · pydata/xarray · GitHub](https://github.com/pydata/xarray/issues/6847)

That said, there is a way to map CPU/NumPy tensors to GPU/CuPy tensors in xarray as with [`cupy-xarray`](https://github.com/xarray-contrib/cupy-xarray), and then use GPU zero-copy methods to convert CuPy tensors to Pytorch/Tensorflow tensors. See:

- [https://developer.nvidia.com/blog/machine-learning-frameworks-interoperability-part-1-memory-layouts-and-memory-pools/](https://developer.nvidia.com/blog/machine-learning-frameworks-interoperability-part-1-memory-layouts-and-memory-pools/)
- [https://developer.nvidia.com/blog/machine-learning-frameworks-interoperability-part-2-data-loading-and-data-transfer-bottlenecks/](https://developer.nvidia.com/blog/machine-learning-frameworks-interoperability-part-2-data-loading-and-data-transfer-bottlenecks/)

 ![](https://canada1.discourse-cdn.com/flex030/uploads/pangeo/original/2X/5/5662bde74af744e080a44a43a913f3531d6939ee.png)

But again, you will still need to load the NetCDF from File → CPU-RAM → GPU-RAM until someone figures out a more direct NetCDF file → GPU-RAM path. This has been on my wishlist for quite a while, and most of the interoperability standards are in place, we just need to get some smart people to do it 🙂

---

_[View the full topic](https://discourse.pangeo.io/t/favorite-way-to-go-from-netcdf-xarray-to-torch-tf-jax-et-al/2663)._
