HighFive icon indicating copy to clipboard operation
HighFive copied to clipboard

Support for libtorch tensors

Open evanberkowitz opened this issue 4 years ago • 1 comments

Is your feature request related to a problem? Please describe. I have a C++ library built on top of libtorch. I would like to use HighFive to provide an hdf5 interface. However, the dimension of a torch tensor is determined at runtime, so converting to (for example) an std::vector of std::vector of ... of (say) complex<double>s, which would need to be done at compile time, is not possible.

Describe the solution you'd like An interface to support torch tensors directly, just as Eigen and Xtensor are supported.

Describe alternatives you've considered We've considered linearizing our data and storing the dimensions as attributes (for example) but that makes reading the data in other software (say, in python) more difficult and seems prone to error.

Additional context ø

evanberkowitz avatar Apr 07 '22 13:04 evanberkowitz

The interface H5Easy supports the library xtensor; and from what I can gather from the example and source code should be able to read/write xarrays which look to be essentially a pointer with a shape whose dimensions can be changed at runtime. They seem to also have views. There's an example here: https://github.com/BlueBrain/HighFive/blob/master/include/highfive/h5easy_bits/H5Easy_xtensor.hpp

Therefore, you might be able to imitate their solution. If you can access the raw pointer (and the array/tensor isn't padded, strided or offset) you can probably do something like:

auto data_set = file.createDataSet<double>("dset_name", DataSpace(dims));
data_set.write_raw(raw_ptr);

1uc avatar Apr 07 '22 14:04 1uc