arrayfire icon indicating copy to clipboard operation
arrayfire copied to clipboard

How to use half precision ?

Open CodeByMaxx opened this issue 7 years ago • 17 comments

How to use half precision ?, are there any c++ code samples or is this done by some flags ?

Best regards

CodeByMaxx avatar Jul 02 '19 07:07 CodeByMaxx

fp16 is still very much an experimental feature. It can only be used for some operations and only works well on the CUDA backend. Through a couple of issues with indexing and on the OpenCL side of things but I will write a tutorial page after it is more complete.

If you have host side data, I would suggest creating an fp32 array and calling the as member function to convert it to fp16. Here is an example:

vector<float> hdata(100, 3.14);
array data(10, 10, hdata.data());
array fp16data = data.as(fp16);
...

So far all element-wise operations should work, as well as af_gemm, reductions, transpose, and join. Printing also works. If you want to test, please let us know of any functions you need to port to fp16.

umar456 avatar Jul 02 '19 16:07 umar456

Ok thank you, best regards.

CodeByMaxx avatar Jul 03 '19 05:07 CodeByMaxx

Ok but i also have one question how to compile arrayfire with cmake for half precision and how get the right include file(s).

Best regards

CodeByMaxx avatar Jul 03 '19 10:07 CodeByMaxx

I am not sure I understand. Are you running into errors? Can you post the compile log?

umar456 avatar Jul 03 '19 10:07 umar456

No but the datatype fp16 was not found for converting it, so something is missing.

CodeByMaxx avatar Jul 03 '19 10:07 CodeByMaxx

Its defined here: https://github.com/arrayfire/arrayfire/blob/master/include/af/defines.h#L227. That file is included with arrayfire.h. Are you sure you are compiling the latest master?

umar456 avatar Jul 03 '19 10:07 umar456

yes i do

CodeByMaxx avatar Jul 03 '19 11:07 CodeByMaxx

ok now it works

it should be called f16 instead of fp32

arrayfire.h should also look like this

#include "af/compatible.h" #include "af/algorithm.h" #include "af/arith.h" #include "af/array.h" #include "af/backend.h" #include "af/blas.h" #include "af/constants.h" #include "af/complex.h" #include "af/data.h" #include "af/device.h" #include "af/exception.h" #include "af/features.h" #include "af/gfor.h" #include "af/graphics.h" #include "af/half.h" #include "af/image.h" #include "af/index.h" #include "af/lapack.h" #include "af/random.h" #include "af/seq.h" #include "af/signal.h" #include "af/sparse.h" #include "af/statistics.h" #include "af/timing.h" #include "af/util.h" #include "af/version.h" #include "af/vision.h" #include "af/defines.h" //is missing

half precision only works for some specific kinds of operations, i think it does not work for convolution, best regards

CodeByMaxx avatar Jul 03 '19 11:07 CodeByMaxx

the error for convolution would be

ArrayFire Exception (Function does not support this data type:204): In function af_err convolve(void**, af_array, af_array) [with long long int baseDim = 2; bool expand = false; af_array = void*] In file src/api/c/convolve.cpp:154 Invalid type for argument 1

In function af::array af::convolve2(const af::array&, const af::array&, af::convMode, af::convDomain) In file src/api/cpp/convolve.cpp:49 terminate called after throwing an instance of 'af::exception' what(): ArrayFire Exception (Function does not support this data type:204): In function af_err convolve(void**, af_array, af_array) [with long long int baseDim = 2; bool expand = false; af_array = void*] In file src/api/c/convolve.cpp:154 Invalid type for argument 1

In function af::array af::convolve2(const af::array&, const af::array&, af::convMode, af::convDomain) In file src/api/cpp/convolve.cpp:49

CodeByMaxx avatar Jul 03 '19 11:07 CodeByMaxx

Convolve has not been converted to support fp16.

umar456 avatar Jul 03 '19 11:07 umar456

Ok thx.

CodeByMaxx avatar Jul 03 '19 11:07 CodeByMaxx

Convolve has not been converted to support fp16.

It would be nice to have such operations, because these operations are really expensive.

CodeByMaxx avatar Jul 03 '19 12:07 CodeByMaxx

@ZeroCool11000 could you please check if AF 3.7.0 now please you ?

WilliamTambellini avatar Mar 03 '20 18:03 WilliamTambellini

Half precision support is added to the following convolution functions only at the moment. latest release has added couple of Machine learning specific functions

9prady9 avatar Apr 08 '20 10:04 9prady9

@ZeroCool11000 If the above mentioned suffices your case, you may close the issue. If you need f16 support for regular convolution functions. Feel free to rename the issue as feature request to add f16 support for convolutions in general

9prady9 avatar Apr 08 '20 10:04 9prady9

Closing due to inactivity.

9prady9 avatar Apr 23 '20 11:04 9prady9

Is it possible to add half-point support for regular convolutions?

GustavoStahl avatar Dec 08 '22 00:12 GustavoStahl