FP16 support #1849
Replies: 4 comments 3 replies
-
Objections! In C++23 there is a float type: https://en.cppreference.com/w/cpp/types/floating-point On clang/gcc _Float16 is a thing - that's what we should be using Otherwise - OK - have a custom struct. I think spy should give us sonething helpful here, @jfalcou? Let's have
where it's equal to std::float16_t, when that's avaliable. |
Beta Was this translation helpful? Give feedback.
-
That's an absolutely separate issue, as you have already encountered.
|
Beta Was this translation helpful? Give feedback.
-
I don't understand what are you trying to do. There is no way to support arbitrary types in eve |
Beta Was this translation helpful? Give feedback.
-
Ok. After some implementation tests with @jfalcou, it turns out you were right, we need to split the enum support and the fp16 support into two separated PRs, as we found out that we could not easily implement the actual fp16 backend based on a transparent type. As such, we propose to keep the work on transparent types but reduce its scope to enums and single-member structs, and implement the fp16 support in another separated PR, which will need more discussion. Let's discuss the details on Wednesday. |
Beta Was this translation helpful? Give feedback.
-
EVE FP16 support proposal
Introduction
We propose to implement support for the IEEE-754 half-precision floating-point format (aka FP16) in the EVE library in three steps.
There is no standard fp16 type in C++ as of yet, the only way to represent it is to either use platform-specific intrinsics types, such as
_Float16
on x86 andfloat16_t
on ARM, or use a custom 16-bit type on which specialized operations are implemented.To unify the representation of fp16 across architectures and compilers, we propose to implement it as a
struct
guarenteed to be exactly 16 bits wide.This will allow us to implement the necessary operators on this type to allow interop with the platform-specific fp16 types.
Step 1. Transparent types
This step consists of making
eve::wide
support transparent types (see definitions below). The goal of this step is to prepare for the end goal: being able to expresseve::wide<fp16_t>
.This allows us to instantiate objects of type
eve::wide<fp16_t>
, and to use many of theeve::wide
methods on them, such asget
,set
,operator==
, etc.As as side-effect, this will also effectively resolve the issue #1573.
Step 2. Internal function rework
This step consists of reworking internal functions such as
as_register
(as layed-out in issue #1844) to allow for users to extends the list of supported types, for example for transparent types:Other impacted functions may include
make
,combine
,slice
.Note that reworking these function may also reduce the overwall compile time and make it easier to extend them later for new types.
Step 3. FP16 Support in EVE
Finally, once the groundwork is laid, we can then add the fp16 type to EVE, implement the necessary operators for interop with platform-specific fp16 types and implement the necessary intrinsics in the backends.
Open questions & research
FP16 codegen
Consider the following C++ code targeting a F16C capable x86 CPU:
The codegen when compiling using Clang 18 is the following:
We can observe that a lot of the instructions produced (mostly the function prelude and epilogue) consists of clearing the highest 112 bits of the xmm vectors. While apparently due to the ABI imposed upon the compiler, this indicates that, while usefull, the compilers' native support for fp16 scalar operations cannot be fully relied upon codegen-wise when optimizing for maximum performance. As such, it is possible that a hand-written implementation for fp16 support in EVE could be beneficial in the long term, although optional.
Definitions & Name bikeshedding
Transparent
From
#[repr(transparent)]
from Rust, also used in C++ (see transparent comparators). Refers to an enum, or to a struct which has a single member of non-zero size, and for which the layout in memory is guaranteed to be the same as that of that member.Exemple:
Enums are considered transparent types by default:
Structs which can satisfy the definition of a transparent type can implement the transparent trait:
For any transparent type
T
, this assertion must hold (ie. the memory layout ofT
andtransparent_trait<T>::type
must be the same):Beta Was this translation helpful? Give feedback.
All reactions