libnpy icon indicating copy to clipboard operation
libnpy copied to clipboard

[Feature Request] float16 support

Open zhongsanming opened this issue 1 year ago • 7 comments

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.

zhongsanming avatar May 08 '23 09:05 zhongsanming

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)

WilliamTambellini avatar Aug 01 '23 15:08 WilliamTambellini

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

llohse avatar Aug 02 '23 08:08 llohse

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

zhongsanming avatar Aug 02 '23 08:08 zhongsanming

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

WilliamTambellini avatar Aug 03 '23 19:08 WilliamTambellini

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.

StarsX avatar Dec 13 '23 09:12 StarsX

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.

pauljurczak avatar Feb 09 '24 08:02 pauljurczak