Support for floating point types#1307
Conversation
|
I'll add ap_float.h and mapping to C++23 types. Converting to draft until I make the change |
| #include "nnet_utils/nnet_types.h" | ||
| #include <cstddef> | ||
| #include <cstdio> | ||
| #if __cplusplus > 202002L |
There was a problem hiding this comment.
This check should be #if __cplusplus >= 202302L but since GCC doesn't fully support C++23 the constant is not set yet to that value, hence we check for "newer than C++20". Alternatively, we could check __STDCPP_BFLOAT16_T__ and __STDCPP_FLOAT16_T__ but these may also not be available even if the implementation is there...
There was a problem hiding this comment.
I am running GCC version 11.5 which apparently support C++23, but does not support <stdfloat>. So this guard does not protect from compilation failures in my tests. Checking __STDCPP_BFLOAT16_T__ works, but then I run into further problems with ap_float not naming a type.
There was a problem hiding this comment.
Seems that we need an #include "nnet_utils/nnet_types.h" in this file. With that, compilation works.
There was a problem hiding this comment.
I don't understand, nnet_types.h is already included? As for the other issues, I am thinking to include <stdfloat> only if the type is used, and not otherwise. Same for ap_float.h, because that one is not available in Vivado 2020.1. Such a mess with these types...
There was a problem hiding this comment.
Ah sorry, copied the wrong line. I had to add #include "ap_types/ap_float.h" in the defines.h for it to work.
|
I've been playing with this a bit and see that when running synthesis in Vitis I get this error Looks like we need an implementation of the |
|
I can now synthesize a model in Vitis 2024.1 when I use |
|
Well, it turns out |
|
Dang, that's frustrating. I think we should merge it as is and not advertise it. It all works fine from the hls4ml side and would be nice to just have it in there in case |
* Support for floating point types * Use C++23 types for bfloat16 and half * Implement << op * Use floating-point headers only if the types require them * Print a warning if new C++ types (half and bloat16) are used * Fix typo
Description
Current hls4ml supports only arbitrary precision integer and fixed-point types. This PR adds support for floating point types. Floating point types are defined in
ap_float.handac_float.hof the respective libraries and cover two distinct cases: IEEE floating-point standard (basically C/C++ types) and general floating-point implementation (any combination of mantissa and exponent). AC types library is more broad and offers more flexibility than AP types one. The PR covers both by introducingFloatPrecisionTypefor the general case (covered by theac_float) andStandardFloatPrecisionType(forap_float<W,E>andac_std_float<W,E>). In principle one could cram everything in a single type but that makes it complicated to track what is the actual intended use, especially because of the 1-bit difference between AC and AP types.To use, user can specify
float,double,halfandbfloat16as type and this will result inStandardFloatTypePrecisionobjects to be used and those C++ types used in the generated code. Note thathalfandbfloat16aren't supported out of the box and require the user to tweak the code to make it compile, as it is dependent on the compiler how these are exposed. If the user specifiesstd_float<W,E>theap_float<W,E>andac_std_float<W,E>will be generated using the same object. Finally, for full control of AC type, user can useac_float<W,I,E,Q>which will use theFloatPrecisionTypeclass and emit the corresponding type in code.The PR is somewhat incomplete as there are numerous nuances of full support of the general case and the
half/bfloat16but is a good starting point and is self-contained. The problem remains with AP types that don't have a public version ofap_float.h(we'll ask AMD about open-sourcing it), so if user uses a general floating-point type the local compilation withcompile()won't work. In the future we can tackle the include issue (also forhalfandbfloat16on host compilers), as well as look into optimizations of algorithms (for example, using the accumulator type for the CMVM). The intention right now is not to make this a first-class supported feature, rather an exotic option for users who know what they want and are aware of the caveats and rough edges, but more crucially it allows us to avoid silly test failures due to bitwidth issues. This could be advertised as an experimental feature, but I see we're completely lacking any documentation on type setting, so that may come as a separate PR.Type of change
Tests
Tests for parsing the new types has been added to
test_types.h.Checklist
Yeah, yeah, I did all the things in the checklist.