Question about numpy.float16 support in cython

287 views
Skip to first unread message

Serhiy Yevtushenko

unread,
Jul 24, 2021, 3:31:03 PM7/24/21
to cython...@googlegroups.com
Hi, 

I was trying to use np.float16 type, and although I'm able to use it in the python program,
when trying to declare it as a type in the cython code (using np.float16_t analogous to np.float32_t, and doing cimport numpy as np at the start of pyx file), I am getting the following error:
 'float16_t' is not a type identifier

Could someone clarify, whether the type np.float16 is somehow supported in the cython, and if yes, how one could use it?

Kind Regards

Serhiy Yevtushenko

D Woods

unread,
Jul 25, 2021, 1:53:54 AM7/25/21
to cython-users
I don't think it's really supported. The trouble is that most modern CPUs don't have a float16 data type so if you managed to get access to it in C (or Cython) then the compiler wouldn't know what to do with it.

It looks like https://github.com/numpy/numpy/blob/623bc1fae1d47df24e7f1e29321d0c0ba2771ce0/numpy/core/include/numpy/halffloat.h defines a few operations on the type, and for everything else you convert it to/from float32 when you need to use it. The advantage is then in space rather than processing time. See https://stackoverflow.com/questions/38975770/python-numpy-float16-datatype-operations-and-float8/40507235

The Numpy pxd files are now maintained by Numpy rather than Cython. It doesn't look like they expose the relevant functions and types, but if the headers are publicly available then you could write your own wrapper pxd. You'd treat `np_half_type` as an opaque type that you can't do much with except copy, and convert to more sensible float types.

Serhiy Yevtushenko

unread,
Jul 26, 2021, 9:18:28 AM7/26/21
to cython-users
Hi, David


Thank you for your clarification.

I have found as well the following stackoverflow answer, which is quite detailed:

For my use case, unfortunately, it did not work out, as I was trying to use numpy float16 together with numpy structured arrays and memory views,
and after replacing np.float16 with uint16, I am getting ValueError: Does not understand character buffer dtype format string ('e').

Nevertheless, may be it will be useful for someone else trying to go this way.

Davie, you mentioned that numpy .pxd files are now being maintained by numpy.
Could you please clarify, if you happen to know, the proper way to ask concerning np.float16 support in cython on numpy project? Where one should write or create issue?

Kind Regards

Serhiy


воскресенье, 25 июля 2021 г. в 07:53:54 UTC+2, D Woods:

Stefan Behnel

unread,
Jul 26, 2021, 9:25:55 AM7/26/21
to cython...@googlegroups.com
Serhiy Yevtushenko schrieb am 26.07.21 um 13:08:
> I have found as well the following stackoverflow answer, which is quite
> detailed:
> https://stackoverflow.com/questions/47421443/using-half-precision-numpy-floats-in-cython
>
> For my use case, unfortunately, it did not work out, as I was trying to use
> numpy float16 together with numpy structured arrays and memory views,
> and after replacing np.float16 with uint16, I am getting ValueError: Does
> not understand character buffer dtype format string ('e').

There's a related pull request, but no ticket, I think.

https://github.com/cython/cython/pull/2285


> Davie, you mentioned that numpy .pxd files are now being maintained by
> numpy.
> Could you please clarify, if you happen to know, the proper way to ask
> concerning np.float16 support in cython on numpy project? Where one should
> write or create issue?

Cython would need to implement support for it.

Stefan

Serhiy Yevtushenko

unread,
Jul 27, 2021, 10:08:01 AM7/27/21
to cython-users
Hi, Stefan

Thank you for the clarification.

What would be the right way to get this functionality in the cython? Should one create issue on cython github (https://github.com/cython/cython) ?  

Thank you in advance.

Serhiy

понедельник, 26 июля 2021 г. в 15:25:55 UTC+2, Stefan Behnel:

D Woods

unread,
Aug 5, 2021, 10:11:46 AM8/5/21
to cython-users
Belatedly,

> What would be the right way to get this functionality in the cython?

I think there's two sides to this. For Numpy you could probably just add `npy_half_to_float` and friends to their .pxd file (and submit that as a PR). That would give you access to those functions, although getting them in/out of the array would remain difficult.

For Cython: you could create a github issue (although that doesn't guarantee anyone does anything about it). You could also pick up the existing PR (i.e. resubmit it with improvements). It looks to me like they didn't typedef it as an *empty* struct at that was why they were having problems.

David
Reply all
Reply to author
Forward
0 new messages