Eigen  3.4.90 (git rev 9589cc4e7fd8e4538bedef80dd36c7738977a8be)
 
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Loading...
Searching...
No Matches
Half.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// This Source Code Form is subject to the terms of the Mozilla
5// Public License v. 2.0. If a copy of the MPL was not distributed
6// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
7//
8// The conversion routines are Copyright (c) Fabian Giesen, 2016.
9// The original license follows:
10//
11// Copyright (c) Fabian Giesen, 2016
12// All rights reserved.
13// Redistribution and use in source and binary forms, with or without
14// modification, are permitted.
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27// Standard 16-bit float type, mostly useful for GPUs. Defines a new
28// type Eigen::half (inheriting either from CUDA's or HIP's __half struct) with
29// operator overloads such that it behaves basically as an arithmetic
30// type. It will be quite slow on CPUs (so it is recommended to stay
31// in fp32 for CPUs, except for simple parameter conversions, I/O
32// to disk and the likes), but fast on GPUs.
33
34#ifndef EIGEN_HALF_H
35#define EIGEN_HALF_H
36
37// IWYU pragma: private
38#include "../../InternalHeaderCheck.h"
39
40#if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
41// When compiling with GPU support, the "__half_raw" base class as well as
42// some other routines are defined in the GPU compiler header files
43// (cuda_fp16.h, hip_fp16.h), and they are not tagged constexpr
44// As a consequence, we get compile failures when compiling Eigen with
45// GPU support. Hence the need to disable EIGEN_CONSTEXPR when building
46// Eigen with GPU support
47#pragma push_macro("EIGEN_CONSTEXPR")
48#undef EIGEN_CONSTEXPR
49#define EIGEN_CONSTEXPR
50#endif
51
52#define F16_PACKET_FUNCTION(PACKET_F, PACKET_F16, METHOD) \
53 template <> \
54 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_UNUSED PACKET_F16 METHOD<PACKET_F16>(const PACKET_F16& _x) { \
55 return float2half(METHOD<PACKET_F>(half2float(_x))); \
56 }
57
58namespace Eigen {
59
60struct half;
61
62namespace half_impl {
63
64// We want to use the __half_raw struct from the HIP header file only during the device compile phase.
65// This is required because of a quirk in the way TensorFlow GPU builds are done.
66// When compiling TensorFlow source code with GPU support, files that
67// * contain GPU kernels (i.e. *.cu.cc files) are compiled via hipcc
68// * do not contain GPU kernels ( i.e. *.cc files) are compiled via gcc (typically)
69//
70// Tensorflow uses the Eigen::half type as its FP16 type, and there are functions that
71// * are defined in a file that gets compiled via hipcc AND
72// * have Eigen::half as a pass-by-value argument AND
73// * are called in a file that gets compiled via gcc
74//
75// In the scenario described above the caller and callee will see different versions
76// of the Eigen::half base class __half_raw, and they will be compiled by different compilers
77//
78// There appears to be an ABI mismatch between gcc and clang (which is called by hipcc) that results in
79// the callee getting corrupted values for the Eigen::half argument.
80//
81// Making the host side compile phase of hipcc use the same Eigen::half impl, as the gcc compile, resolves
82// this error, and hence the following convoluted #if condition
83#if !defined(EIGEN_HAS_GPU_FP16) || !defined(EIGEN_GPU_COMPILE_PHASE)
84// Make our own __half_raw definition that is similar to CUDA's.
85struct __half_raw {
86#if (defined(EIGEN_HAS_GPU_FP16) && !defined(EIGEN_GPU_COMPILE_PHASE))
87 // Eigen::half can be used as the datatype for shared memory declarations (in Eigen and TF)
88 // The element type for shared memory cannot have non-trivial constructors
89 // and hence the following special casing (which skips the zero-initilization).
90 // Note that this check gets done even in the host compilation phase, and
91 // hence the need for this
92 EIGEN_DEVICE_FUNC __half_raw() {}
93#else
94 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw() : x(0) {}
95#endif
96#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
97 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw(numext::uint16_t raw) : x(numext::bit_cast<__fp16>(raw)) {}
98 __fp16 x;
99#else
100 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw(numext::uint16_t raw) : x(raw) {}
101 numext::uint16_t x;
102#endif
103};
104
105#elif defined(EIGEN_HAS_HIP_FP16)
106// Nothing to do here
107// HIP fp16 header file has a definition for __half_raw
108#elif defined(EIGEN_HAS_CUDA_FP16)
109#if EIGEN_CUDA_SDK_VER < 90000
110// In CUDA < 9.0, __half is the equivalent of CUDA 9's __half_raw
111typedef __half __half_raw;
112#endif // defined(EIGEN_HAS_CUDA_FP16)
113#elif defined(SYCL_DEVICE_ONLY)
114typedef cl::sycl::half __half_raw;
115#endif
116
117EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x);
118EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(float ff);
119EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half_raw h);
120
121struct half_base : public __half_raw {
122 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base() {}
123 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half_raw& h) : __half_raw(h) {}
124
125#if defined(EIGEN_HAS_GPU_FP16)
126#if defined(EIGEN_HAS_HIP_FP16)
127 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half& h) { x = __half_as_ushort(h); }
128#elif defined(EIGEN_HAS_CUDA_FP16)
129#if EIGEN_CUDA_SDK_VER >= 90000
130 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half& h) : __half_raw(*(__half_raw*)&h) {}
131#endif
132#endif
133#endif
134};
135
136} // namespace half_impl
137
138// Class definition.
139struct half : public half_impl::half_base {
140 // Writing this out as separate #if-else blocks to make the code easier to follow
141 // The same applies to most #if-else blocks in this file
142#if !defined(EIGEN_HAS_GPU_FP16) || !defined(EIGEN_GPU_COMPILE_PHASE)
143 // Use the same base class for the following two scenarios
144 // * when compiling without GPU support enabled
145 // * during host compile phase when compiling with GPU support enabled
146 typedef half_impl::__half_raw __half_raw;
147#elif defined(EIGEN_HAS_HIP_FP16)
148 // Nothing to do here
149 // HIP fp16 header file has a definition for __half_raw
150#elif defined(EIGEN_HAS_CUDA_FP16)
151// Note that EIGEN_CUDA_SDK_VER is set to 0 even when compiling with HIP, so
152// (EIGEN_CUDA_SDK_VER < 90000) is true even for HIP! So keeping this within
153// #if defined(EIGEN_HAS_CUDA_FP16) is needed
154#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER < 90000
155 typedef half_impl::__half_raw __half_raw;
156#endif
157#endif
158
159 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half() {}
160
161 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half_raw& h) : half_impl::half_base(h) {}
162
163#if defined(EIGEN_HAS_GPU_FP16)
164#if defined(EIGEN_HAS_HIP_FP16)
165 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half& h) : half_impl::half_base(h) {}
166#elif defined(EIGEN_HAS_CUDA_FP16)
167#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
168 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half& h) : half_impl::half_base(h) {}
169#endif
170#endif
171#endif
172
173 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(bool b)
174 : half_impl::half_base(half_impl::raw_uint16_to_half(b ? 0x3c00 : 0)) {}
175 template <class T>
176 explicit EIGEN_DEVICE_FUNC half(T val)
177 : half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(val))) {}
178 explicit EIGEN_DEVICE_FUNC half(float f) : half_impl::half_base(half_impl::float_to_half_rtne(f)) {}
179
180 // Following the convention of numpy, converting between complex and
181 // float will lead to loss of imag value.
182 template <typename RealScalar>
183 explicit EIGEN_DEVICE_FUNC half(std::complex<RealScalar> c)
184 : half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(c.real()))) {}
185
186 EIGEN_DEVICE_FUNC operator float() const { // NOLINT: Allow implicit conversion to float, because it is lossless.
187 return half_impl::half_to_float(*this);
188 }
189
190#if defined(EIGEN_HAS_GPU_FP16) && !defined(EIGEN_GPU_COMPILE_PHASE)
191 EIGEN_DEVICE_FUNC operator __half() const {
192 ::__half_raw hr;
193 hr.x = x;
194 return __half(hr);
195 }
196#endif
197};
198
199// TODO(majnemer): Get rid of this once we can rely on C++17 inline variables do
200// solve the ODR issue.
201namespace half_impl {
202template <typename = void>
203struct numeric_limits_half_impl {
204 static EIGEN_CONSTEXPR const bool is_specialized = true;
205 static EIGEN_CONSTEXPR const bool is_signed = true;
206 static EIGEN_CONSTEXPR const bool is_integer = false;
207 static EIGEN_CONSTEXPR const bool is_exact = false;
208 static EIGEN_CONSTEXPR const bool has_infinity = true;
209 static EIGEN_CONSTEXPR const bool has_quiet_NaN = true;
210 static EIGEN_CONSTEXPR const bool has_signaling_NaN = true;
211 EIGEN_DIAGNOSTICS(push)
212 EIGEN_DISABLE_DEPRECATED_WARNING
213 static EIGEN_CONSTEXPR const std::float_denorm_style has_denorm = std::denorm_present;
214 static EIGEN_CONSTEXPR const bool has_denorm_loss = false;
215 EIGEN_DIAGNOSTICS(pop)
216 static EIGEN_CONSTEXPR const std::float_round_style round_style = std::round_to_nearest;
217 static EIGEN_CONSTEXPR const bool is_iec559 = true;
218 // The C++ standard defines this as "true if the set of values representable
219 // by the type is finite." Half has finite precision.
220 static EIGEN_CONSTEXPR const bool is_bounded = true;
221 static EIGEN_CONSTEXPR const bool is_modulo = false;
222 static EIGEN_CONSTEXPR const int digits = 11;
223 static EIGEN_CONSTEXPR const int digits10 =
224 3; // according to http://half.sourceforge.net/structstd_1_1numeric__limits_3_01half__float_1_1half_01_4.html
225 static EIGEN_CONSTEXPR const int max_digits10 =
226 5; // according to http://half.sourceforge.net/structstd_1_1numeric__limits_3_01half__float_1_1half_01_4.html
227 static EIGEN_CONSTEXPR const int radix = std::numeric_limits<float>::radix;
228 static EIGEN_CONSTEXPR const int min_exponent = -13;
229 static EIGEN_CONSTEXPR const int min_exponent10 = -4;
230 static EIGEN_CONSTEXPR const int max_exponent = 16;
231 static EIGEN_CONSTEXPR const int max_exponent10 = 4;
232 static EIGEN_CONSTEXPR const bool traps = std::numeric_limits<float>::traps;
233 // IEEE754: "The implementer shall choose how tininess is detected, but shall
234 // detect tininess in the same way for all operations in radix two"
235 static EIGEN_CONSTEXPR const bool tinyness_before = std::numeric_limits<float>::tinyness_before;
236
237 static EIGEN_CONSTEXPR Eigen::half(min)() { return Eigen::half_impl::raw_uint16_to_half(0x0400); }
238 static EIGEN_CONSTEXPR Eigen::half lowest() { return Eigen::half_impl::raw_uint16_to_half(0xfbff); }
239 static EIGEN_CONSTEXPR Eigen::half(max)() { return Eigen::half_impl::raw_uint16_to_half(0x7bff); }
240 static EIGEN_CONSTEXPR Eigen::half epsilon() { return Eigen::half_impl::raw_uint16_to_half(0x1400); }
241 static EIGEN_CONSTEXPR Eigen::half round_error() { return Eigen::half_impl::raw_uint16_to_half(0x3800); }
242 static EIGEN_CONSTEXPR Eigen::half infinity() { return Eigen::half_impl::raw_uint16_to_half(0x7c00); }
243 static EIGEN_CONSTEXPR Eigen::half quiet_NaN() { return Eigen::half_impl::raw_uint16_to_half(0x7e00); }
244 static EIGEN_CONSTEXPR Eigen::half signaling_NaN() { return Eigen::half_impl::raw_uint16_to_half(0x7d00); }
245 static EIGEN_CONSTEXPR Eigen::half denorm_min() { return Eigen::half_impl::raw_uint16_to_half(0x0001); }
246};
247
248template <typename T>
249EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::is_specialized;
250template <typename T>
251EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::is_signed;
252template <typename T>
253EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::is_integer;
254template <typename T>
255EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::is_exact;
256template <typename T>
257EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::has_infinity;
258template <typename T>
259EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::has_quiet_NaN;
260template <typename T>
261EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::has_signaling_NaN;
262EIGEN_DIAGNOSTICS(push)
263EIGEN_DISABLE_DEPRECATED_WARNING
264template <typename T>
265EIGEN_CONSTEXPR const std::float_denorm_style numeric_limits_half_impl<T>::has_denorm;
266template <typename T>
267EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::has_denorm_loss;
268EIGEN_DIAGNOSTICS(pop)
269template <typename T>
270EIGEN_CONSTEXPR const std::float_round_style numeric_limits_half_impl<T>::round_style;
271template <typename T>
272EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::is_iec559;
273template <typename T>
274EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::is_bounded;
275template <typename T>
276EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::is_modulo;
277template <typename T>
278EIGEN_CONSTEXPR const int numeric_limits_half_impl<T>::digits;
279template <typename T>
280EIGEN_CONSTEXPR const int numeric_limits_half_impl<T>::digits10;
281template <typename T>
282EIGEN_CONSTEXPR const int numeric_limits_half_impl<T>::max_digits10;
283template <typename T>
284EIGEN_CONSTEXPR const int numeric_limits_half_impl<T>::radix;
285template <typename T>
286EIGEN_CONSTEXPR const int numeric_limits_half_impl<T>::min_exponent;
287template <typename T>
288EIGEN_CONSTEXPR const int numeric_limits_half_impl<T>::min_exponent10;
289template <typename T>
290EIGEN_CONSTEXPR const int numeric_limits_half_impl<T>::max_exponent;
291template <typename T>
292EIGEN_CONSTEXPR const int numeric_limits_half_impl<T>::max_exponent10;
293template <typename T>
294EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::traps;
295template <typename T>
296EIGEN_CONSTEXPR const bool numeric_limits_half_impl<T>::tinyness_before;
297} // end namespace half_impl
298} // end namespace Eigen
299
300namespace std {
301// If std::numeric_limits<T> is specialized, should also specialize
302// std::numeric_limits<const T>, std::numeric_limits<volatile T>, and
303// std::numeric_limits<const volatile T>
304// https://stackoverflow.com/a/16519653/
305template <>
306class numeric_limits<Eigen::half> : public Eigen::half_impl::numeric_limits_half_impl<> {};
307template <>
308class numeric_limits<const Eigen::half> : public numeric_limits<Eigen::half> {};
309template <>
310class numeric_limits<volatile Eigen::half> : public numeric_limits<Eigen::half> {};
311template <>
312class numeric_limits<const volatile Eigen::half> : public numeric_limits<Eigen::half> {};
313} // end namespace std
314
315namespace Eigen {
316
317namespace half_impl {
318
319#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
320 (defined(EIGEN_HAS_HIP_FP16) && defined(HIP_DEVICE_COMPILE))
321// Note: We deliberately do *not* define this to 1 even if we have Arm's native
322// fp16 type since GPU half types are rather different from native CPU half types.
323// TODO: Rename to something like EIGEN_HAS_NATIVE_GPU_FP16
324#define EIGEN_HAS_NATIVE_FP16
325#endif
326
327// Intrinsics for native fp16 support. Note that on current hardware,
328// these are no faster than fp32 arithmetic (you need to use the half2
329// versions to get the ALU speed increased), but you do save the
330// conversion steps back and forth.
331
332#if defined(EIGEN_HAS_NATIVE_FP16)
333EIGEN_STRONG_INLINE __device__ half operator+(const half& a, const half& b) {
334#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
335 return __hadd(::__half(a), ::__half(b));
336#else
337 return __hadd(a, b);
338#endif
339}
340EIGEN_STRONG_INLINE __device__ half operator*(const half& a, const half& b) { return __hmul(a, b); }
341EIGEN_STRONG_INLINE __device__ half operator-(const half& a, const half& b) { return __hsub(a, b); }
342EIGEN_STRONG_INLINE __device__ half operator/(const half& a, const half& b) {
343#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
344 return __hdiv(a, b);
345#else
346 float num = __half2float(a);
347 float denom = __half2float(b);
348 return __float2half(num / denom);
349#endif
350}
351EIGEN_STRONG_INLINE __device__ half operator-(const half& a) { return __hneg(a); }
352EIGEN_STRONG_INLINE __device__ half& operator+=(half& a, const half& b) {
353 a = a + b;
354 return a;
355}
356EIGEN_STRONG_INLINE __device__ half& operator*=(half& a, const half& b) {
357 a = a * b;
358 return a;
359}
360EIGEN_STRONG_INLINE __device__ half& operator-=(half& a, const half& b) {
361 a = a - b;
362 return a;
363}
364EIGEN_STRONG_INLINE __device__ half& operator/=(half& a, const half& b) {
365 a = a / b;
366 return a;
367}
368EIGEN_STRONG_INLINE __device__ bool operator==(const half& a, const half& b) { return __heq(a, b); }
369EIGEN_STRONG_INLINE __device__ bool operator!=(const half& a, const half& b) { return __hne(a, b); }
370EIGEN_STRONG_INLINE __device__ bool operator<(const half& a, const half& b) { return __hlt(a, b); }
371EIGEN_STRONG_INLINE __device__ bool operator<=(const half& a, const half& b) { return __hle(a, b); }
372EIGEN_STRONG_INLINE __device__ bool operator>(const half& a, const half& b) { return __hgt(a, b); }
373EIGEN_STRONG_INLINE __device__ bool operator>=(const half& a, const half& b) { return __hge(a, b); }
374#endif
375
376#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC) && !defined(EIGEN_GPU_COMPILE_PHASE)
377EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator+(const half& a, const half& b) { return half(vaddh_f16(a.x, b.x)); }
378EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator*(const half& a, const half& b) { return half(vmulh_f16(a.x, b.x)); }
379EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(const half& a, const half& b) { return half(vsubh_f16(a.x, b.x)); }
380EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator/(const half& a, const half& b) { return half(vdivh_f16(a.x, b.x)); }
381EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(const half& a) { return half(vnegh_f16(a.x)); }
382EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator+=(half& a, const half& b) {
383 a = half(vaddh_f16(a.x, b.x));
384 return a;
385}
386EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator*=(half& a, const half& b) {
387 a = half(vmulh_f16(a.x, b.x));
388 return a;
389}
390EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator-=(half& a, const half& b) {
391 a = half(vsubh_f16(a.x, b.x));
392 return a;
393}
394EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator/=(half& a, const half& b) {
395 a = half(vdivh_f16(a.x, b.x));
396 return a;
397}
398EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator==(const half& a, const half& b) { return vceqh_f16(a.x, b.x); }
399EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator!=(const half& a, const half& b) { return !vceqh_f16(a.x, b.x); }
400EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator<(const half& a, const half& b) { return vclth_f16(a.x, b.x); }
401EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator<=(const half& a, const half& b) { return vcleh_f16(a.x, b.x); }
402EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator>(const half& a, const half& b) { return vcgth_f16(a.x, b.x); }
403EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator>=(const half& a, const half& b) { return vcgeh_f16(a.x, b.x); }
404// We need to distinguish ‘clang as the CUDA compiler’ from ‘clang as the host compiler,
405// invoked by NVCC’ (e.g. on MacOS). The former needs to see both host and device implementation
406// of the functions, while the latter can only deal with one of them.
407#elif !defined(EIGEN_HAS_NATIVE_FP16) || (EIGEN_COMP_CLANG && !EIGEN_COMP_NVCC) // Emulate support for half floats
408
409#if EIGEN_COMP_CLANG && defined(EIGEN_GPUCC)
410// We need to provide emulated *host-side* FP16 operators for clang.
411#pragma push_macro("EIGEN_DEVICE_FUNC")
412#undef EIGEN_DEVICE_FUNC
413#if defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_HAS_NATIVE_FP16)
414#define EIGEN_DEVICE_FUNC __host__
415#else // both host and device need emulated ops.
416#define EIGEN_DEVICE_FUNC __host__ __device__
417#endif
418#endif
419
420// Definitions for CPUs and older HIP+CUDA, mostly working through conversion
421// to/from fp32.
422EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator+(const half& a, const half& b) { return half(float(a) + float(b)); }
423EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator*(const half& a, const half& b) { return half(float(a) * float(b)); }
424EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(const half& a, const half& b) { return half(float(a) - float(b)); }
425EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator/(const half& a, const half& b) { return half(float(a) / float(b)); }
426EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator-(const half& a) {
427 half result;
428 result.x = a.x ^ 0x8000;
429 return result;
430}
431EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator+=(half& a, const half& b) {
432 a = half(float(a) + float(b));
433 return a;
434}
435EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator*=(half& a, const half& b) {
436 a = half(float(a) * float(b));
437 return a;
438}
439EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator-=(half& a, const half& b) {
440 a = half(float(a) - float(b));
441 return a;
442}
443EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator/=(half& a, const half& b) {
444 a = half(float(a) / float(b));
445 return a;
446}
447EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator==(const half& a, const half& b) {
448 return numext::equal_strict(float(a), float(b));
449}
450EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator!=(const half& a, const half& b) {
451 return numext::not_equal_strict(float(a), float(b));
452}
453EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator<(const half& a, const half& b) { return float(a) < float(b); }
454EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator<=(const half& a, const half& b) { return float(a) <= float(b); }
455EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator>(const half& a, const half& b) { return float(a) > float(b); }
456EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator>=(const half& a, const half& b) { return float(a) >= float(b); }
457
458#if EIGEN_COMP_CLANG && defined(EIGEN_GPUCC)
459#pragma pop_macro("EIGEN_DEVICE_FUNC")
460#endif
461#endif // Emulate support for half floats
462
463// Division by an index. Do it in full float precision to avoid accuracy
464// issues in converting the denominator to half.
465EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator/(const half& a, Index b) {
466 return half(static_cast<float>(a) / static_cast<float>(b));
467}
468
469EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator++(half& a) {
470 a += half(1);
471 return a;
472}
473
474EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator--(half& a) {
475 a -= half(1);
476 return a;
477}
478
479EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator++(half& a, int) {
480 half original_value = a;
481 ++a;
482 return original_value;
483}
484
485EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator--(half& a, int) {
486 half original_value = a;
487 --a;
488 return original_value;
489}
490
491// Conversion routines, including fallbacks for the host or older CUDA.
492// Note that newer Intel CPUs (Haswell or newer) have vectorized versions of
493// these in hardware. If we need more performance on older/other CPUs, they are
494// also possible to vectorize directly.
495
496EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x) {
497 // We cannot simply do a "return __half_raw(x)" here, because __half_raw is union type
498 // in the hip_fp16 header file, and that will trigger a compile error
499 // On the other hand, having anything but a return statement also triggers a compile error
500 // because this is constexpr function.
501 // Fortunately, since we need to disable EIGEN_CONSTEXPR for GPU anyway, we can get out
502 // of this catch22 by having separate bodies for GPU / non GPU
503#if defined(EIGEN_HAS_GPU_FP16)
504 __half_raw h;
505 h.x = x;
506 return h;
507#else
508 return __half_raw(x);
509#endif
510}
511
512EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC numext::uint16_t raw_half_as_uint16(const __half_raw& h) {
513 // HIP/CUDA/Default have a member 'x' of type uint16_t.
514 // For ARM64 native half, the member 'x' is of type __fp16, so we need to bit-cast.
515 // For SYCL, cl::sycl::half is _Float16, so cast directly.
516#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
517 return numext::bit_cast<numext::uint16_t>(h.x);
518#elif defined(SYCL_DEVICE_ONLY)
519 return numext::bit_cast<numext::uint16_t>(h);
520#else
521 return h.x;
522#endif
523}
524
525union float32_bits {
526 unsigned int u;
527 float f;
528};
529
530EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(float ff) {
531#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
532 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
533 __half tmp_ff = __float2half(ff);
534 return *(__half_raw*)&tmp_ff;
535
536#elif defined(EIGEN_HAS_FP16_C)
537 __half_raw h;
538#if EIGEN_COMP_MSVC
539 // MSVC does not have scalar instructions.
540 h.x = _mm_extract_epi16(_mm_cvtps_ph(_mm_set_ss(ff), 0), 0);
541#else
542 h.x = _cvtss_sh(ff, 0);
543#endif
544 return h;
545
546#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
547 __half_raw h;
548 h.x = static_cast<__fp16>(ff);
549 return h;
550
551#else
552 float32_bits f;
553 f.f = ff;
554
555 const float32_bits f32infty = {255 << 23};
556 const float32_bits f16max = {(127 + 16) << 23};
557 const float32_bits denorm_magic = {((127 - 15) + (23 - 10) + 1) << 23};
558 unsigned int sign_mask = 0x80000000u;
559 __half_raw o;
560 o.x = static_cast<numext::uint16_t>(0x0u);
561
562 unsigned int sign = f.u & sign_mask;
563 f.u ^= sign;
564
565 // NOTE all the integer compares in this function can be safely
566 // compiled into signed compares since all operands are below
567 // 0x80000000. Important if you want fast straight SSE2 code
568 // (since there's no unsigned PCMPGTD).
569
570 if (f.u >= f16max.u) { // result is Inf or NaN (all exponent bits set)
571 o.x = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf
572 } else { // (De)normalized number or zero
573 if (f.u < (113 << 23)) { // resulting FP16 is subnormal or zero
574 // use a magic value to align our 10 mantissa bits at the bottom of
575 // the float. as long as FP addition is round-to-nearest-even this
576 // just works.
577 f.f += denorm_magic.f;
578
579 // and one integer subtract of the bias later, we have our final float!
580 o.x = static_cast<numext::uint16_t>(f.u - denorm_magic.u);
581 } else {
582 unsigned int mant_odd = (f.u >> 13) & 1; // resulting mantissa is odd
583
584 // update exponent, rounding bias part 1
585 // Equivalent to `f.u += ((unsigned int)(15 - 127) << 23) + 0xfff`, but
586 // without arithmetic overflow.
587 f.u += 0xc8000fffU;
588 // rounding bias part 2
589 f.u += mant_odd;
590 // take the bits!
591 o.x = static_cast<numext::uint16_t>(f.u >> 13);
592 }
593 }
594
595 o.x |= static_cast<numext::uint16_t>(sign >> 16);
596 return o;
597#endif
598}
599
600EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half_raw h) {
601#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
602 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
603 return __half2float(h);
604#elif defined(EIGEN_HAS_FP16_C)
605#if EIGEN_COMP_MSVC
606 // MSVC does not have scalar instructions.
607 return _mm_cvtss_f32(_mm_cvtph_ps(_mm_set1_epi16(h.x)));
608#else
609 return _cvtsh_ss(h.x);
610#endif
611#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
612 return static_cast<float>(h.x);
613#else
614 const float32_bits magic = {113 << 23};
615 const unsigned int shifted_exp = 0x7c00 << 13; // exponent mask after shift
616 float32_bits o;
617
618 o.u = (h.x & 0x7fff) << 13; // exponent/mantissa bits
619 unsigned int exp = shifted_exp & o.u; // just the exponent
620 o.u += (127 - 15) << 23; // exponent adjust
621
622 // handle exponent special cases
623 if (exp == shifted_exp) { // Inf/NaN?
624 o.u += (128 - 16) << 23; // extra exp adjust
625 } else if (exp == 0) { // Zero/Denormal?
626 o.u += 1 << 23; // extra exp adjust
627 o.f -= magic.f; // renormalize
628 }
629
630 o.u |= (h.x & 0x8000) << 16; // sign bit
631 return o.f;
632#endif
633}
634
635// --- standard functions ---
636
637EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool(isinf)(const half& a) {
638#ifdef EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC
639 return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) == 0x7c00;
640#else
641 return (a.x & 0x7fff) == 0x7c00;
642#endif
643}
644EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool(isnan)(const half& a) {
645#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
646 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
647 return __hisnan(a);
648#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
649 return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) > 0x7c00;
650#else
651 return (a.x & 0x7fff) > 0x7c00;
652#endif
653}
654EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool(isfinite)(const half& a) {
655 return !(isinf EIGEN_NOT_A_MACRO(a)) && !(isnan EIGEN_NOT_A_MACRO(a));
656}
657
658EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half abs(const half& a) {
659#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
660 return half(vabsh_f16(a.x));
661#else
662 half result;
663 result.x = a.x & 0x7FFF;
664 return result;
665#endif
666}
667EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half exp(const half& a) {
668#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
669 defined(EIGEN_HIP_DEVICE_COMPILE)
670 return half(hexp(a));
671#else
672 return half(::expf(float(a)));
673#endif
674}
675EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half exp2(const half& a) {
676#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
677 defined(EIGEN_HIP_DEVICE_COMPILE)
678 return half(hexp2(a));
679#else
680 return half(::exp2f(float(a)));
681#endif
682}
683EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half expm1(const half& a) { return half(numext::expm1(float(a))); }
684EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log(const half& a) {
685#if (defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 80000 && defined(EIGEN_CUDA_ARCH) && \
686 EIGEN_CUDA_ARCH >= 530) || \
687 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
688 return half(hlog(a));
689#else
690 return half(::logf(float(a)));
691#endif
692}
693EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log1p(const half& a) { return half(numext::log1p(float(a))); }
694EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log10(const half& a) { return half(::log10f(float(a))); }
695EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log2(const half& a) {
696 return half(static_cast<float>(EIGEN_LOG2E) * ::logf(float(a)));
697}
698
699EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sqrt(const half& a) {
700#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
701 defined(EIGEN_HIP_DEVICE_COMPILE)
702 return half(hsqrt(a));
703#else
704 return half(::sqrtf(float(a)));
705#endif
706}
707EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half pow(const half& a, const half& b) {
708 return half(::powf(float(a), float(b)));
709}
710EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half atan2(const half& a, const half& b) {
711 return half(::atan2f(float(a), float(b)));
712}
713EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sin(const half& a) { return half(::sinf(float(a))); }
714EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half cos(const half& a) { return half(::cosf(float(a))); }
715EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tan(const half& a) { return half(::tanf(float(a))); }
716EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tanh(const half& a) { return half(::tanhf(float(a))); }
717EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half asin(const half& a) { return half(::asinf(float(a))); }
718EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half acos(const half& a) { return half(::acosf(float(a))); }
719EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half atan(const half& a) { return half(::atanf(float(a))); }
720EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half atanh(const half& a) { return half(::atanhf(float(a))); }
721EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half floor(const half& a) {
722#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 300) || \
723 defined(EIGEN_HIP_DEVICE_COMPILE)
724 return half(hfloor(a));
725#else
726 return half(::floorf(float(a)));
727#endif
728}
729EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half ceil(const half& a) {
730#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 300) || \
731 defined(EIGEN_HIP_DEVICE_COMPILE)
732 return half(hceil(a));
733#else
734 return half(::ceilf(float(a)));
735#endif
736}
737EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half rint(const half& a) { return half(::rintf(float(a))); }
738EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half round(const half& a) { return half(::roundf(float(a))); }
739EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half trunc(const half& a) { return half(::truncf(float(a))); }
740EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half fmod(const half& a, const half& b) {
741 return half(::fmodf(float(a), float(b)));
742}
743
744EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half(min)(const half& a, const half& b) {
745#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
746 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
747 return __hlt(b, a) ? b : a;
748#else
749 const float f1 = static_cast<float>(a);
750 const float f2 = static_cast<float>(b);
751 return f2 < f1 ? b : a;
752#endif
753}
754EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half(max)(const half& a, const half& b) {
755#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
756 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
757 return __hlt(a, b) ? b : a;
758#else
759 const float f1 = static_cast<float>(a);
760 const float f2 = static_cast<float>(b);
761 return f1 < f2 ? b : a;
762#endif
763}
764
765#ifndef EIGEN_NO_IO
766EIGEN_ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, const half& v) {
767 os << static_cast<float>(v);
768 return os;
769}
770#endif
771
772} // end namespace half_impl
773
774// import Eigen::half_impl::half into Eigen namespace
775// using half_impl::half;
776
777namespace internal {
778
779template <>
780struct is_arithmetic<half> {
781 enum { value = true };
782};
783
784template <>
785struct random_impl<half> {
786 enum : int { MantissaBits = 10 };
787 using Impl = random_impl<float>;
788 static EIGEN_DEVICE_FUNC inline half run(const half& x, const half& y) {
789 float result = Impl::run(x, y, MantissaBits);
790 return half(result);
791 }
792 static EIGEN_DEVICE_FUNC inline half run() {
793 float result = Impl::run(MantissaBits);
794 return half(result);
795 }
796};
797
798} // end namespace internal
799
800template <>
801struct NumTraits<Eigen::half> : GenericNumTraits<Eigen::half> {
802 enum { IsSigned = true, IsInteger = false, IsComplex = false, RequireInitialization = false };
803
804 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half epsilon() {
805 return half_impl::raw_uint16_to_half(0x0800);
806 }
807 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half dummy_precision() {
808 return half_impl::raw_uint16_to_half(0x211f); // Eigen::half(1e-2f);
809 }
810 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half highest() {
811 return half_impl::raw_uint16_to_half(0x7bff);
812 }
813 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half lowest() {
814 return half_impl::raw_uint16_to_half(0xfbff);
815 }
816 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half infinity() {
817 return half_impl::raw_uint16_to_half(0x7c00);
818 }
819 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half quiet_NaN() {
820 return half_impl::raw_uint16_to_half(0x7e00);
821 }
822};
823
824} // end namespace Eigen
825
826#if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
827#pragma pop_macro("EIGEN_CONSTEXPR")
828#endif
829
830namespace Eigen {
831namespace numext {
832
833#if defined(EIGEN_GPU_COMPILE_PHASE)
834
835template <>
836EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isnan)(const Eigen::half& h) {
837 return (half_impl::isnan)(h);
838}
839
840template <>
841EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isinf)(const Eigen::half& h) {
842 return (half_impl::isinf)(h);
843}
844
845template <>
846EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isfinite)(const Eigen::half& h) {
847 return (half_impl::isfinite)(h);
848}
849
850#endif
851
852template <>
853EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half bit_cast<Eigen::half, uint16_t>(const uint16_t& src) {
854 return Eigen::half(Eigen::half_impl::raw_uint16_to_half(src));
855}
856
857template <>
858EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC uint16_t bit_cast<uint16_t, Eigen::half>(const Eigen::half& src) {
859 return Eigen::half_impl::raw_half_as_uint16(src);
860}
861
862} // namespace numext
863} // namespace Eigen
864
865// Add the missing shfl* intrinsics.
866// The __shfl* functions are only valid on HIP or _CUDA_ARCH_ >= 300.
867// CUDA defines them for (__CUDA_ARCH__ >= 300 || !defined(__CUDA_ARCH__))
868//
869// HIP and CUDA prior to SDK 9.0 define
870// __shfl, __shfl_up, __shfl_down, __shfl_xor for int and float
871// CUDA since 9.0 deprecates those and instead defines
872// __shfl_sync, __shfl_up_sync, __shfl_down_sync, __shfl_xor_sync,
873// with native support for __half and __nv_bfloat16
874//
875// Note that the following are __device__ - only functions.
876#if (defined(EIGEN_CUDACC) && (!defined(EIGEN_CUDA_ARCH) || EIGEN_CUDA_ARCH >= 300)) || defined(EIGEN_HIPCC)
877
878#if defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 90000
879
880__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_sync(unsigned mask, Eigen::half var, int srcLane,
881 int width = warpSize) {
882 const __half h = var;
883 return static_cast<Eigen::half>(__shfl_sync(mask, h, srcLane, width));
884}
885
886__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_up_sync(unsigned mask, Eigen::half var, unsigned int delta,
887 int width = warpSize) {
888 const __half h = var;
889 return static_cast<Eigen::half>(__shfl_up_sync(mask, h, delta, width));
890}
891
892__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_down_sync(unsigned mask, Eigen::half var, unsigned int delta,
893 int width = warpSize) {
894 const __half h = var;
895 return static_cast<Eigen::half>(__shfl_down_sync(mask, h, delta, width));
896}
897
898__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_xor_sync(unsigned mask, Eigen::half var, int laneMask,
899 int width = warpSize) {
900 const __half h = var;
901 return static_cast<Eigen::half>(__shfl_xor_sync(mask, h, laneMask, width));
902}
903
904#else // HIP or CUDA SDK < 9.0
905
906__device__ EIGEN_STRONG_INLINE Eigen::half __shfl(Eigen::half var, int srcLane, int width = warpSize) {
907 const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
908 return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl(ivar, srcLane, width)));
909}
910
911__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_up(Eigen::half var, unsigned int delta, int width = warpSize) {
912 const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
913 return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl_up(ivar, delta, width)));
914}
915
916__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_down(Eigen::half var, unsigned int delta, int width = warpSize) {
917 const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
918 return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl_down(ivar, delta, width)));
919}
920
921__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_xor(Eigen::half var, int laneMask, int width = warpSize) {
922 const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
923 return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl_xor(ivar, laneMask, width)));
924}
925
926#endif // HIP vs CUDA
927#endif // __shfl*
928
929// ldg() has an overload for __half_raw, but we also need one for Eigen::half.
930#if (defined(EIGEN_CUDACC) && (!defined(EIGEN_CUDA_ARCH) || EIGEN_CUDA_ARCH >= 350)) || defined(EIGEN_HIPCC)
931EIGEN_STRONG_INLINE __device__ Eigen::half __ldg(const Eigen::half* ptr) {
932 return Eigen::half_impl::raw_uint16_to_half(__ldg(reinterpret_cast<const Eigen::numext::uint16_t*>(ptr)));
933}
934#endif // __ldg
935
936#if EIGEN_HAS_STD_HASH
937namespace std {
938template <>
939struct hash<Eigen::half> {
940 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t operator()(const Eigen::half& a) const {
941 return static_cast<std::size_t>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(a));
942 }
943};
944} // end namespace std
945#endif
946
947namespace Eigen {
948namespace internal {
949
950template <>
951struct cast_impl<float, half> {
952 EIGEN_DEVICE_FUNC static inline half run(const float& a) {
953#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
954 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
955 return __float2half(a);
956#else
957 return half(a);
958#endif
959 }
960};
961
962template <>
963struct cast_impl<int, half> {
964 EIGEN_DEVICE_FUNC static inline half run(const int& a) {
965#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
966 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
967 return __float2half(static_cast<float>(a));
968#else
969 return half(static_cast<float>(a));
970#endif
971 }
972};
973
974template <>
975struct cast_impl<half, float> {
976 EIGEN_DEVICE_FUNC static inline float run(const half& a) {
977#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
978 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
979 return __half2float(a);
980#else
981 return static_cast<float>(a);
982#endif
983 }
984};
985
986} // namespace internal
987} // namespace Eigen
988
989#endif // EIGEN_HALF_H
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_real_op< typename Derived::Scalar >, const Derived > real(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sign_op< typename Derived::Scalar >, const Derived > sign(const Eigen::ArrayBase< Derived > &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition Meta.h:522