llohse / libnpy

C++ library for reading and writing of numpy's .npy files
MIT License
361 stars 72 forks source link

[Feature Request] float16 support #33

Open zhongsanming opened 1 year ago

zhongsanming commented 1 year ago

Can we add half as a dependency to support half precision floating point number? I've done a bit of simple test, it seems work fine.

WilliamTambellini commented 1 year ago

Have you checked it s compat with np.half ?

import numpy as np
a = np.array([2,2], dtype=np.half)
np.save('f16.npy', a)
llohse commented 1 year ago

I try to avoid adding non-standard dependencies. Let me think about this.

zhongsanming commented 1 year ago

I try to avoid adding non-standard dependencies. Let me think about this.

OK, just let you know, I don't need this anymore, XD

WilliamTambellini commented 1 year ago

I would still appreciate this new feature. Adding a dep to the half headers is perhaps not necessary anyway.

StarsX commented 9 months ago

I have the similar case, but there is a simple way to implement the reader: struct half { uint16_t u; }; // Can be inside namespace npy

Then, add {std::type_index(typeid(half)), {host_endian_char, 'f', sizeof(half)}}, into dtype_map.

Thanks.

pauljurczak commented 7 months ago

A standard conforming way would be:

const std::unordered_map<std::type_index, dtype_t> dtype_map = {
    {std::type_index(typeid(float16_t)), {host_endian_char, 'f', sizeof(float16_t)}},

This requires #include <stdfloat> header, which is a C++23 feature.

llohse commented 1 day ago

@pauljurczak I like this approach, but I don't want to require C++23 support.

If there is a way to provide that feature depending on compiler support, I would include it. PRs are very welcome.