Medial Code Documentation
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
28// Standard 16-bit float type, mostly useful for GPUs. Defines a new
29// type Eigen::half (inheriting either from CUDA's or HIP's __half struct) with
30// operator overloads such that it behaves basically as an arithmetic
31// type. It will be quite slow on CPUs (so it is recommended to stay
32// in fp32 for CPUs, except for simple parameter conversions, I/O
33// to disk and the likes), but fast on GPUs.
34
35
36#ifndef EIGEN_HALF_H
37#define EIGEN_HALF_H
38
39#if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
40// When compiling with GPU support, the "__half_raw" base class as well as
41// some other routines are defined in the GPU compiler header files
42// (cuda_fp16.h, hip_fp16.h), and they are not tagged constexpr
43// As a consequence, we get compile failures when compiling Eigen with
44// GPU support. Hence the need to disable EIGEN_CONSTEXPR when building
45// Eigen with GPU support
46 #pragma push_macro("EIGEN_CONSTEXPR")
47 #undef EIGEN_CONSTEXPR
48 #define EIGEN_CONSTEXPR
49#endif
50
51#define F16_PACKET_FUNCTION(PACKET_F, PACKET_F16, METHOD) \
52 template <> \
53 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_UNUSED \
54 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 }
99 __fp16 x;
100#else
101 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw(numext::uint16_t raw) : x(raw) {}
102 numext::uint16_t x;
103#endif
104};
105
106#elif defined(EIGEN_HAS_HIP_FP16)
107 // Nothing to do here
108 // HIP fp16 header file has a definition for __half_raw
109#elif defined(EIGEN_HAS_CUDA_FP16)
110 #if EIGEN_CUDA_SDK_VER < 90000
111 // In CUDA < 9.0, __half is the equivalent of CUDA 9's __half_raw
112 typedef __half __half_raw;
113 #endif // defined(EIGEN_HAS_CUDA_FP16)
114#elif defined(SYCL_DEVICE_ONLY)
115 typedef cl::sycl::half __half_raw;
116#endif
117
118EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x);
119EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(float ff);
120EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half_raw h);
121
122struct half_base : public __half_raw {
123 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base() {}
124 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half_raw& h) : __half_raw(h) {}
125
126#if defined(EIGEN_HAS_GPU_FP16)
127 #if defined(EIGEN_HAS_HIP_FP16)
128 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half& h) { x = __half_as_ushort(h); }
129 #elif defined(EIGEN_HAS_CUDA_FP16)
130 #if EIGEN_CUDA_SDK_VER >= 90000
131 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half& h) : __half_raw(*(__half_raw*)&h) {}
132 #endif
133 #endif
134#endif
135};
136
137} // namespace half_impl
138
139// Class definition.
140struct half : public half_impl::half_base {
141
142 // Writing this out as separate #if-else blocks to make the code easier to follow
143 // The same applies to most #if-else blocks in this file
144#if !defined(EIGEN_HAS_GPU_FP16) || !defined(EIGEN_GPU_COMPILE_PHASE)
145 // Use the same base class for the following two scenarios
146 // * when compiling without GPU support enabled
147 // * during host compile phase when compiling with GPU support enabled
149#elif defined(EIGEN_HAS_HIP_FP16)
150 // Nothing to do here
151 // HIP fp16 header file has a definition for __half_raw
152#elif defined(EIGEN_HAS_CUDA_FP16)
153 // Note that EIGEN_CUDA_SDK_VER is set to 0 even when compiling with HIP, so
154 // (EIGEN_CUDA_SDK_VER < 90000) is true even for HIP! So keeping this within
155 // #if defined(EIGEN_HAS_CUDA_FP16) is needed
156 #if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER < 90000
158 #endif
159#endif
160
161 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half() {}
162
163 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half_raw& h) : half_impl::half_base(h) {}
164
165#if defined(EIGEN_HAS_GPU_FP16)
166 #if defined(EIGEN_HAS_HIP_FP16)
167 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half& h) : half_impl::half_base(h) {}
168 #elif defined(EIGEN_HAS_CUDA_FP16)
169 #if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
170 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half& h) : half_impl::half_base(h) {}
171 #endif
172 #endif
173#endif
174
175
176 explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(bool b)
177 : half_impl::half_base(half_impl::raw_uint16_to_half(b ? 0x3c00 : 0)) {}
178 template<class T>
179 explicit EIGEN_DEVICE_FUNC half(T val)
180 : half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(val))) {}
181 explicit EIGEN_DEVICE_FUNC half(float f)
182 : half_impl::half_base(half_impl::float_to_half_rtne(f)) {}
183
184 // Following the convention of numpy, converting between complex and
185 // float will lead to loss of imag value.
186 template<typename RealScalar>
187 explicit EIGEN_DEVICE_FUNC half(std::complex<RealScalar> c)
188 : half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(c.real()))) {}
189
190 EIGEN_DEVICE_FUNC operator float() const { // NOLINT: Allow implicit conversion to float, because it is lossless.
191 return half_impl::half_to_float(*this);
192 }
193
194#if defined(EIGEN_HAS_GPU_FP16) && !defined(EIGEN_GPU_COMPILE_PHASE)
195 EIGEN_DEVICE_FUNC operator __half() const {
196 ::__half_raw hr;
197 hr.x = x;
198 return __half(hr);
199 }
200#endif
201};
202
203} // end namespace Eigen
204
205namespace std {
206template<>
207struct numeric_limits<Eigen::half> {
208 static const bool is_specialized = true;
209 static const bool is_signed = true;
210 static const bool is_integer = false;
211 static const bool is_exact = false;
212 static const bool has_infinity = true;
213 static const bool has_quiet_NaN = true;
214 static const bool has_signaling_NaN = true;
215 static const float_denorm_style has_denorm = denorm_present;
216 static const bool has_denorm_loss = false;
217 static const std::float_round_style round_style = std::round_to_nearest;
218 static const bool is_iec559 = false;
219 static const bool is_bounded = false;
220 static const bool is_modulo = false;
221 static const int digits = 11;
222 static const int digits10 = 3; // according to http://half.sourceforge.net/structstd_1_1numeric__limits_3_01half__float_1_1half_01_4.html
223 static const int max_digits10 = 5; // according to http://half.sourceforge.net/structstd_1_1numeric__limits_3_01half__float_1_1half_01_4.html
224 static const int radix = 2;
225 static const int min_exponent = -13;
226 static const int min_exponent10 = -4;
227 static const int max_exponent = 16;
228 static const int max_exponent10 = 4;
229 static const bool traps = true;
230 static const bool tinyness_before = false;
231
232 static Eigen::half (min)() { return Eigen::half_impl::raw_uint16_to_half(0x400); }
233 static Eigen::half lowest() { return Eigen::half_impl::raw_uint16_to_half(0xfbff); }
234 static Eigen::half (max)() { return Eigen::half_impl::raw_uint16_to_half(0x7bff); }
235 static Eigen::half epsilon() { return Eigen::half_impl::raw_uint16_to_half(0x0800); }
236 static Eigen::half round_error() { return Eigen::half(0.5); }
237 static Eigen::half infinity() { return Eigen::half_impl::raw_uint16_to_half(0x7c00); }
238 static Eigen::half quiet_NaN() { return Eigen::half_impl::raw_uint16_to_half(0x7e00); }
239 static Eigen::half signaling_NaN() { return Eigen::half_impl::raw_uint16_to_half(0x7d00); }
240 static Eigen::half denorm_min() { return Eigen::half_impl::raw_uint16_to_half(0x1); }
241};
242
243// If std::numeric_limits<T> is specialized, should also specialize
244// std::numeric_limits<const T>, std::numeric_limits<volatile T>, and
245// std::numeric_limits<const volatile T>
246// https://stackoverflow.com/a/16519653/
247template<>
248struct numeric_limits<const Eigen::half> : numeric_limits<Eigen::half> {};
249template<>
250struct numeric_limits<volatile Eigen::half> : numeric_limits<Eigen::half> {};
251template<>
252struct numeric_limits<const volatile Eigen::half> : numeric_limits<Eigen::half> {};
253} // end namespace std
254
255namespace Eigen {
256
257namespace half_impl {
258
259#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && \
260 EIGEN_CUDA_ARCH >= 530) || \
261 (defined(EIGEN_HAS_HIP_FP16) && defined(HIP_DEVICE_COMPILE))
262// Note: We deliberatly do *not* define this to 1 even if we have Arm's native
263// fp16 type since GPU halfs are rather different from native CPU halfs.
264// TODO: Rename to something like EIGEN_HAS_NATIVE_GPU_FP16
265#define EIGEN_HAS_NATIVE_FP16
266#endif
267
268// Intrinsics for native fp16 support. Note that on current hardware,
269// these are no faster than fp32 arithmetic (you need to use the half2
270// versions to get the ALU speed increased), but you do save the
271// conversion steps back and forth.
272
273#if defined(EIGEN_HAS_NATIVE_FP16)
274EIGEN_STRONG_INLINE __device__ half operator + (const half& a, const half& b) {
275#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
276 return __hadd(::__half(a), ::__half(b));
277#else
278 return __hadd(a, b);
279#endif
280}
281EIGEN_STRONG_INLINE __device__ half operator * (const half& a, const half& b) {
282 return __hmul(a, b);
283}
284EIGEN_STRONG_INLINE __device__ half operator - (const half& a, const half& b) {
285 return __hsub(a, b);
286}
287EIGEN_STRONG_INLINE __device__ half operator / (const half& a, const half& b) {
288#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
289 return __hdiv(a, b);
290#else
291 float num = __half2float(a);
292 float denom = __half2float(b);
293 return __float2half(num / denom);
294#endif
295}
296EIGEN_STRONG_INLINE __device__ half operator - (const half& a) {
297 return __hneg(a);
298}
299EIGEN_STRONG_INLINE __device__ half& operator += (half& a, const half& b) {
300 a = a + b;
301 return a;
302}
303EIGEN_STRONG_INLINE __device__ half& operator *= (half& a, const half& b) {
304 a = a * b;
305 return a;
306}
307EIGEN_STRONG_INLINE __device__ half& operator -= (half& a, const half& b) {
308 a = a - b;
309 return a;
310}
311EIGEN_STRONG_INLINE __device__ half& operator /= (half& a, const half& b) {
312 a = a / b;
313 return a;
314}
315EIGEN_STRONG_INLINE __device__ bool operator == (const half& a, const half& b) {
316 return __heq(a, b);
317}
318EIGEN_STRONG_INLINE __device__ bool operator != (const half& a, const half& b) {
319 return __hne(a, b);
320}
321EIGEN_STRONG_INLINE __device__ bool operator < (const half& a, const half& b) {
322 return __hlt(a, b);
323}
324EIGEN_STRONG_INLINE __device__ bool operator <= (const half& a, const half& b) {
325 return __hle(a, b);
326}
327EIGEN_STRONG_INLINE __device__ bool operator > (const half& a, const half& b) {
328 return __hgt(a, b);
329}
330EIGEN_STRONG_INLINE __device__ bool operator >= (const half& a, const half& b) {
331 return __hge(a, b);
332}
333#endif
334
335#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC) && !defined(EIGEN_GPU_COMPILE_PHASE)
336EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator + (const half& a, const half& b) {
337 return half(vaddh_f16(a.x, b.x));
338}
339EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator * (const half& a, const half& b) {
340 return half(vmulh_f16(a.x, b.x));
341}
342EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a, const half& b) {
343 return half(vsubh_f16(a.x, b.x));
344}
345EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, const half& b) {
346 return half(vdivh_f16(a.x, b.x));
347}
348EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a) {
349 return half(vnegh_f16(a.x));
350}
351EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator += (half& a, const half& b) {
352 a = half(vaddh_f16(a.x, b.x));
353 return a;
354}
355EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator *= (half& a, const half& b) {
356 a = half(vmulh_f16(a.x, b.x));
357 return a;
358}
359EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator -= (half& a, const half& b) {
360 a = half(vsubh_f16(a.x, b.x));
361 return a;
362}
363EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator /= (half& a, const half& b) {
364 a = half(vdivh_f16(a.x, b.x));
365 return a;
366}
367EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator == (const half& a, const half& b) {
368 return vceqh_f16(a.x, b.x);
369}
370EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator != (const half& a, const half& b) {
371 return !vceqh_f16(a.x, b.x);
372}
373EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator < (const half& a, const half& b) {
374 return vclth_f16(a.x, b.x);
375}
376EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator <= (const half& a, const half& b) {
377 return vcleh_f16(a.x, b.x);
378}
379EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator > (const half& a, const half& b) {
380 return vcgth_f16(a.x, b.x);
381}
382EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator >= (const half& a, const half& b) {
383 return vcgeh_f16(a.x, b.x);
384}
385// We need to distinguish ‘clang as the CUDA compiler’ from ‘clang as the host compiler,
386// invoked by NVCC’ (e.g. on MacOS). The former needs to see both host and device implementation
387// of the functions, while the latter can only deal with one of them.
388#elif !defined(EIGEN_HAS_NATIVE_FP16) || (EIGEN_COMP_CLANG && !EIGEN_COMP_NVCC) // Emulate support for half floats
389
390#if EIGEN_COMP_CLANG && defined(EIGEN_CUDACC)
391// We need to provide emulated *host-side* FP16 operators for clang.
392#pragma push_macro("EIGEN_DEVICE_FUNC")
393#undef EIGEN_DEVICE_FUNC
394#if defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_HAS_NATIVE_FP16)
395#define EIGEN_DEVICE_FUNC __host__
396#else // both host and device need emulated ops.
397#define EIGEN_DEVICE_FUNC __host__ __device__
398#endif
399#endif
400
401// Definitions for CPUs and older HIP+CUDA, mostly working through conversion
402// to/from fp32.
403EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator + (const half& a, const half& b) {
404 return half(float(a) + float(b));
405}
406EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator * (const half& a, const half& b) {
407 return half(float(a) * float(b));
408}
409EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a, const half& b) {
410 return half(float(a) - float(b));
411}
412EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, const half& b) {
413 return half(float(a) / float(b));
414}
415EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a) {
416 half result;
417 result.x = a.x ^ 0x8000;
418 return result;
419}
420EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator += (half& a, const half& b) {
421 a = half(float(a) + float(b));
422 return a;
423}
424EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator *= (half& a, const half& b) {
425 a = half(float(a) * float(b));
426 return a;
427}
428EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator -= (half& a, const half& b) {
429 a = half(float(a) - float(b));
430 return a;
431}
432EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator /= (half& a, const half& b) {
433 a = half(float(a) / float(b));
434 return a;
435}
436EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator == (const half& a, const half& b) {
437 return numext::equal_strict(float(a),float(b));
438}
439EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator != (const half& a, const half& b) {
440 return numext::not_equal_strict(float(a), float(b));
441}
442EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator < (const half& a, const half& b) {
443 return float(a) < float(b);
444}
445EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator <= (const half& a, const half& b) {
446 return float(a) <= float(b);
447}
448EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator > (const half& a, const half& b) {
449 return float(a) > float(b);
450}
451EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator >= (const half& a, const half& b) {
452 return float(a) >= float(b);
453}
454
455#if defined(__clang__) && defined(__CUDA__)
456#pragma pop_macro("EIGEN_DEVICE_FUNC")
457#endif
458#endif // Emulate support for half floats
459
460// Division by an index. Do it in full float precision to avoid accuracy
461// issues in converting the denominator to half.
462EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, Index b) {
463 return half(static_cast<float>(a) / static_cast<float>(b));
464}
465
466EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator++(half& a) {
467 a += half(1);
468 return a;
469}
470
471EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator--(half& a) {
472 a -= half(1);
473 return a;
474}
475
476EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator++(half& a, int) {
477 half original_value = a;
478 ++a;
479 return original_value;
480}
481
482EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator--(half& a, int) {
483 half original_value = a;
484 --a;
485 return original_value;
486}
487
488// Conversion routines, including fallbacks for the host or older CUDA.
489// Note that newer Intel CPUs (Haswell or newer) have vectorized versions of
490// these in hardware. If we need more performance on older/other CPUs, they are
491// also possible to vectorize directly.
492
493EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x) {
494 // We cannot simply do a "return __half_raw(x)" here, because __half_raw is union type
495 // in the hip_fp16 header file, and that will trigger a compile error
496 // On the other hand, having anything but a return statement also triggers a compile error
497 // because this is constexpr function.
498 // Fortunately, since we need to disable EIGEN_CONSTEXPR for GPU anyway, we can get out
499 // of this catch22 by having separate bodies for GPU / non GPU
500#if defined(EIGEN_HAS_GPU_FP16)
501 __half_raw h;
502 h.x = x;
503 return h;
504#else
505 return __half_raw(x);
506#endif
507}
508
509EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC numext::uint16_t raw_half_as_uint16(const __half_raw& h) {
510 // HIP/CUDA/Default have a member 'x' of type uint16_t.
511 // For ARM64 native half, the member 'x' is of type __fp16, so we need to bit-cast.
512 // For SYCL, cl::sycl::half is _Float16, so cast directly.
513#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
514 return numext::bit_cast<numext::uint16_t>(h.x);
515#elif defined(SYCL_DEVICE_ONLY)
516 return numext::bit_cast<numext::uint16_t>(h);
517#else
518 return h.x;
519#endif
520}
521
523 unsigned int u;
524 float f;
525};
526
527EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(float ff) {
528#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
529 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
531 return *(__half_raw*)&tmp_ff;
532
533#elif defined(EIGEN_HAS_FP16_C)
534 __half_raw h;
535 #if EIGEN_COMP_MSVC
536 // MSVC does not have scalar instructions.
538 #else
539 h.x = _cvtss_sh(ff, 0);
540 #endif
541 return h;
542
543#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
544 __half_raw h;
545 h.x = static_cast<__fp16>(ff);
546 return h;
547
548#else
549 float32_bits f; f.f = ff;
550
551 const float32_bits f32infty = { 255 << 23 };
552 const float32_bits f16max = { (127 + 16) << 23 };
553 const float32_bits denorm_magic = { ((127 - 15) + (23 - 10) + 1) << 23 };
554 unsigned int sign_mask = 0x80000000u;
555 __half_raw o;
556 o.x = static_cast<numext::uint16_t>(0x0u);
557
558 unsigned int sign = f.u & sign_mask;
559 f.u ^= sign;
560
561 // NOTE all the integer compares in this function can be safely
562 // compiled into signed compares since all operands are below
563 // 0x80000000. Important if you want fast straight SSE2 code
564 // (since there's no unsigned PCMPGTD).
565
566 if (f.u >= f16max.u) { // result is Inf or NaN (all exponent bits set)
567 o.x = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf
568 } else { // (De)normalized number or zero
569 if (f.u < (113 << 23)) { // resulting FP16 is subnormal or zero
570 // use a magic value to align our 10 mantissa bits at the bottom of
571 // the float. as long as FP addition is round-to-nearest-even this
572 // just works.
573 f.f += denorm_magic.f;
574
575 // and one integer subtract of the bias later, we have our final float!
576 o.x = static_cast<numext::uint16_t>(f.u - denorm_magic.u);
577 } else {
578 unsigned int mant_odd = (f.u >> 13) & 1; // resulting mantissa is odd
579
580 // update exponent, rounding bias part 1
581 // Equivalent to `f.u += ((unsigned int)(15 - 127) << 23) + 0xfff`, but
582 // without arithmetic overflow.
583 f.u += 0xc8000fffU;
584 // rounding bias part 2
585 f.u += mant_odd;
586 // take the bits!
587 o.x = static_cast<numext::uint16_t>(f.u >> 13);
588 }
589 }
590
591 o.x |= static_cast<numext::uint16_t>(sign >> 16);
592 return o;
593#endif
594}
595
596EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half_raw h) {
597#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
598 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
599 return __half2float(h);
600#elif defined(EIGEN_HAS_FP16_C)
601 #if EIGEN_COMP_MSVC
602 // MSVC does not have scalar instructions.
603 return _mm_cvtss_f32(_mm_cvtph_ps(_mm_set1_epi16(h.x)));
604 #else
605 return _cvtsh_ss(h.x);
606 #endif
607#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
608 return static_cast<float>(h.x);
609#else
610 const float32_bits magic = { 113 << 23 };
611 const unsigned int shifted_exp = 0x7c00 << 13; // exponent mask after shift
612 float32_bits o;
613
614 o.u = (h.x & 0x7fff) << 13; // exponent/mantissa bits
615 unsigned int exp = shifted_exp & o.u; // just the exponent
616 o.u += (127 - 15) << 23; // exponent adjust
617
618 // handle exponent special cases
619 if (exp == shifted_exp) { // Inf/NaN?
620 o.u += (128 - 16) << 23; // extra exp adjust
621 } else if (exp == 0) { // Zero/Denormal?
622 o.u += 1 << 23; // extra exp adjust
623 o.f -= magic.f; // renormalize
624 }
625
626 o.u |= (h.x & 0x8000) << 16; // sign bit
627 return o.f;
628#endif
629}
630
631// --- standard functions ---
632
633EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isinf)(const half& a) {
634#ifdef EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC
635 return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) == 0x7c00;
636#else
637 return (a.x & 0x7fff) == 0x7c00;
638#endif
639}
640EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isnan)(const half& a) {
641#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
642 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
643 return __hisnan(a);
644#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
645 return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) > 0x7c00;
646#else
647 return (a.x & 0x7fff) > 0x7c00;
648#endif
649}
650EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isfinite)(const half& a) {
651 return !(isinf EIGEN_NOT_A_MACRO (a)) && !(isnan EIGEN_NOT_A_MACRO (a));
652}
653
654EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half abs(const half& a) {
655#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
656 return half(vabsh_f16(a.x));
657#else
658 half result;
659 result.x = a.x & 0x7FFF;
660 return result;
661#endif
662}
663EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half exp(const half& a) {
664#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
665 defined(EIGEN_HIP_DEVICE_COMPILE)
666 return half(hexp(a));
667#else
668 return half(::expf(float(a)));
669#endif
670}
671EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half expm1(const half& a) {
672 return half(numext::expm1(float(a)));
673}
674EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log(const half& a) {
675#if (defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 80000 && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
676 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
677 return half(::hlog(a));
678#else
679 return half(::logf(float(a)));
680#endif
681}
682EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log1p(const half& a) {
683 return half(numext::log1p(float(a)));
684}
685EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log10(const half& a) {
686 return half(::log10f(float(a)));
687}
688EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log2(const half& a) {
689 return half(static_cast<float>(EIGEN_LOG2E) * ::logf(float(a)));
690}
691
692EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sqrt(const half& a) {
693#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
694 defined(EIGEN_HIP_DEVICE_COMPILE)
695 return half(hsqrt(a));
696#else
697 return half(::sqrtf(float(a)));
698#endif
699}
700EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half pow(const half& a, const half& b) {
701 return half(::powf(float(a), float(b)));
702}
703EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sin(const half& a) {
704 return half(::sinf(float(a)));
705}
706EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half cos(const half& a) {
707 return half(::cosf(float(a)));
708}
709EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tan(const half& a) {
710 return half(::tanf(float(a)));
711}
712EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tanh(const half& a) {
713 return half(::tanhf(float(a)));
714}
715EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half asin(const half& a) {
716 return half(::asinf(float(a)));
717}
718EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half acos(const half& a) {
719 return half(::acosf(float(a)));
720}
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) {
738 return half(::rintf(float(a)));
739}
740EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half round(const half& a) {
741 return half(::roundf(float(a)));
742}
743EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half fmod(const half& a, const half& b) {
744 return half(::fmodf(float(a), float(b)));
745}
746
747EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half (min)(const half& a, const half& b) {
748#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
749 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
750 return __hlt(b, a) ? b : a;
751#else
752 const float f1 = static_cast<float>(a);
753 const float f2 = static_cast<float>(b);
754 return f2 < f1 ? b : a;
755#endif
756}
757EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half (max)(const half& a, const half& b) {
758#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
759 (defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
760 return __hlt(a, b) ? b : a;
761#else
762 const float f1 = static_cast<float>(a);
763 const float f2 = static_cast<float>(b);
764 return f1 < f2 ? b : a;
765#endif
766}
767
768#ifndef EIGEN_NO_IO
769EIGEN_ALWAYS_INLINE std::ostream& operator << (std::ostream& os, const half& v) {
770 os << static_cast<float>(v);
771 return os;
772}
773#endif
774
775} // end namespace half_impl
776
777// import Eigen::half_impl::half into Eigen namespace
778// using half_impl::half;
779
780namespace internal {
781
782template<>
784{
785 static inline half run(const half& x, const half& y)
786 {
787 return x + (y-x) * half(float(std::rand()) / float(RAND_MAX));
788 }
789 static inline half run()
790 {
791 return run(half(-1.f), half(1.f));
792 }
793};
794
795template<> struct is_arithmetic<half> { enum { value = true }; };
796
797} // end namespace internal
798
799template<> struct NumTraits<Eigen::half>
800 : GenericNumTraits<Eigen::half>
801{
802 enum {
803 IsSigned = true,
804 IsInteger = false,
805 IsComplex = false,
806 RequireInitialization = false
807 };
808
809 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half epsilon() {
810 return half_impl::raw_uint16_to_half(0x0800);
811 }
812 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half dummy_precision() {
813 return half_impl::raw_uint16_to_half(0x211f); // Eigen::half(1e-2f);
814 }
815 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half highest() {
816 return half_impl::raw_uint16_to_half(0x7bff);
817 }
818 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half lowest() {
819 return half_impl::raw_uint16_to_half(0xfbff);
820 }
821 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half infinity() {
822 return half_impl::raw_uint16_to_half(0x7c00);
823 }
824 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half quiet_NaN() {
825 return half_impl::raw_uint16_to_half(0x7e00);
826 }
827};
828
829} // end namespace Eigen
830
831#if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
832 #pragma pop_macro("EIGEN_CONSTEXPR")
833#endif
834
835namespace Eigen {
836namespace numext {
837
838#if defined(EIGEN_GPU_COMPILE_PHASE)
839
840template <>
841EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isnan)(const Eigen::half& h) {
842 return (half_impl::isnan)(h);
843}
844
845template <>
846EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isinf)(const Eigen::half& h) {
847 return (half_impl::isinf)(h);
848}
849
850template <>
851EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isfinite)(const Eigen::half& h) {
852 return (half_impl::isfinite)(h);
853}
854
855#endif
856
857template <>
858EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half bit_cast<Eigen::half, uint16_t>(const uint16_t& src) {
859 return Eigen::half(Eigen::half_impl::raw_uint16_to_half(src));
860}
861
862template <>
863EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC uint16_t bit_cast<uint16_t, Eigen::half>(const Eigen::half& src) {
864 return Eigen::half_impl::raw_half_as_uint16(src);
865}
866
867} // namespace numext
868} // namespace Eigen
869
870// Add the missing shfl* intrinsics.
871// The __shfl* functions are only valid on HIP or _CUDA_ARCH_ >= 300.
872// CUDA defines them for (__CUDA_ARCH__ >= 300 || !defined(__CUDA_ARCH__))
873//
874// HIP and CUDA prior to SDK 9.0 define
875// __shfl, __shfl_up, __shfl_down, __shfl_xor for int and float
876// CUDA since 9.0 deprecates those and instead defines
877// __shfl_sync, __shfl_up_sync, __shfl_down_sync, __shfl_xor_sync,
878// with native support for __half and __nv_bfloat16
879//
880// Note that the following are __device__ - only functions.
881#if (defined(EIGEN_CUDACC) && (!defined(EIGEN_CUDA_ARCH) || EIGEN_CUDA_ARCH >= 300)) \
882 || defined(EIGEN_HIPCC)
883
884#if defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 90000
885
886__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_sync(unsigned mask, Eigen::half var, int srcLane, int width=warpSize) {
887 const __half h = var;
888 return static_cast<Eigen::half>(__shfl_sync(mask, h, srcLane, width));
889}
890
891__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_up_sync(unsigned mask, Eigen::half var, unsigned int delta, int width=warpSize) {
892 const __half h = var;
893 return static_cast<Eigen::half>(__shfl_up_sync(mask, h, delta, width));
894}
895
896__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_down_sync(unsigned mask, Eigen::half var, unsigned int delta, int width=warpSize) {
897 const __half h = var;
898 return static_cast<Eigen::half>(__shfl_down_sync(mask, h, delta, width));
899}
900
901__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_xor_sync(unsigned mask, Eigen::half var, int laneMask, int width=warpSize) {
902 const __half h = var;
903 return static_cast<Eigen::half>(__shfl_xor_sync(mask, h, laneMask, width));
904}
905
906#else // HIP or CUDA SDK < 9.0
907
908__device__ EIGEN_STRONG_INLINE Eigen::half __shfl(Eigen::half var, int srcLane, int width=warpSize) {
909 const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
910 return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl(ivar, srcLane, width)));
911}
912
913__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_up(Eigen::half var, unsigned int delta, int width=warpSize) {
914 const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
915 return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl_up(ivar, delta, width)));
916}
917
918__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_down(Eigen::half var, unsigned int delta, int width=warpSize) {
919 const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
920 return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl_down(ivar, delta, width)));
921}
922
923__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_xor(Eigen::half var, int laneMask, int width=warpSize) {
924 const int ivar = static_cast<int>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(var));
925 return Eigen::numext::bit_cast<Eigen::half>(static_cast<Eigen::numext::uint16_t>(__shfl_xor(ivar, laneMask, width)));
926}
927
928#endif // HIP vs CUDA
929#endif // __shfl*
930
931// ldg() has an overload for __half_raw, but we also need one for Eigen::half.
932#if (defined(EIGEN_CUDACC) && (!defined(EIGEN_CUDA_ARCH) || EIGEN_CUDA_ARCH >= 350)) \
933 || defined(EIGEN_HIPCC)
934EIGEN_STRONG_INLINE __device__ Eigen::half __ldg(const Eigen::half* ptr) {
935 return Eigen::half_impl::raw_uint16_to_half(__ldg(reinterpret_cast<const Eigen::numext::uint16_t*>(ptr)));
936}
937#endif // __ldg
938
939#if EIGEN_HAS_STD_HASH
940namespace std {
941template <>
942struct hash<Eigen::half> {
943 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t operator()(const Eigen::half& a) const {
944 return static_cast<std::size_t>(Eigen::numext::bit_cast<Eigen::numext::uint16_t>(a));
945 }
946};
947} // end namespace std
948#endif
949
950#endif // EIGEN_HALF_H
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
Namespace containing all symbols from the Eigen library.
Definition LDLT.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:74
Definition BFloat16.h:88
Definition NumTraits.h:156
Holds information about the various numeric (i.e.
Definition NumTraits.h:236
Definition Half.h:85
Definition Half.h:122
Definition Half.h:140
Definition Meta.h:133
Definition MathFunctions.h:806
Definition Half.h:522