Support for floating point types #1307
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.h
andac_float.h
of 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 introducingFloatPrecisionType
for 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
,half
andbfloat16
as type and this will result inStandardFloatTypePrecision
objects to be used and those C++ types used in the generated code. Note thathalf
andbfloat16
aren'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 theFloatPrecisionType
class 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
/bfloat16
but 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 forhalf
andbfloat16
on 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.