2016-03-11 17:21:42 -08:00
|
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
|
|
|
|
|
// for linear algebra.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla
|
|
|
|
|
|
// Public License v. 2.0. If a copy of the MPL was not distributed
|
|
|
|
|
|
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
//
|
|
|
|
|
|
// The conversion routines are Copyright (c) Fabian Giesen, 2016.
|
|
|
|
|
|
// The original license follows:
|
|
|
|
|
|
//
|
|
|
|
|
|
// Copyright (c) Fabian Giesen, 2016
|
|
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
// modification, are permitted.
|
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
2017-03-07 10:47:40 +01:00
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2016-03-11 17:21:42 -08:00
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
|
|
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
2016-07-22 14:33:28 +02:00
|
|
|
|
|
|
|
|
|
|
// Standard 16-bit float type, mostly useful for GPUs. Defines a new
|
2018-06-14 10:21:54 -04:00
|
|
|
|
// type Eigen::half (inheriting either from CUDA's or HIP's __half struct) with
|
2016-07-22 14:33:28 +02:00
|
|
|
|
// operator overloads such that it behaves basically as an arithmetic
|
|
|
|
|
|
// type. It will be quite slow on CPUs (so it is recommended to stay
|
|
|
|
|
|
// in fp32 for CPUs, except for simple parameter conversions, I/O
|
|
|
|
|
|
// to disk and the likes), but fast on GPUs.
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-08-27 11:30:31 -07:00
|
|
|
|
#ifndef EIGEN_HALF_H
|
|
|
|
|
|
#define EIGEN_HALF_H
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2020-07-09 17:24:00 +00:00
|
|
|
|
#if EIGEN_HAS_CXX11
|
2016-03-18 12:20:08 -07:00
|
|
|
|
#define EIGEN_EXPLICIT_CAST(tgt_type) explicit operator tgt_type()
|
|
|
|
|
|
#else
|
|
|
|
|
|
#define EIGEN_EXPLICIT_CAST(tgt_type) operator tgt_type()
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2020-02-24 23:09:36 +00:00
|
|
|
|
#include <sstream>
|
2016-03-18 12:20:08 -07:00
|
|
|
|
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
|
2020-09-22 20:28:08 +00:00
|
|
|
|
// When compiling with GPU support, the "__half_raw" base class as well as
|
|
|
|
|
|
// some other routines are defined in the GPU compiler header files
|
|
|
|
|
|
// (cuda_fp16.h, hip_fp16.h), and they are not tagged constexpr
|
|
|
|
|
|
// As a consequence, we get compile failures when compiling Eigen with
|
|
|
|
|
|
// GPU support. Hence the need to disable EIGEN_CONSTEXPR when building
|
|
|
|
|
|
// Eigen with GPU support
|
|
|
|
|
|
#pragma push_macro("EIGEN_CONSTEXPR")
|
2020-10-12 20:24:15 -04:00
|
|
|
|
#undef EIGEN_CONSTEXPR
|
2020-09-22 20:28:08 +00:00
|
|
|
|
#define EIGEN_CONSTEXPR
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2016-07-22 14:33:28 +02:00
|
|
|
|
namespace Eigen {
|
|
|
|
|
|
|
2016-08-04 00:00:43 +02:00
|
|
|
|
struct half;
|
|
|
|
|
|
|
2016-07-22 14:33:28 +02:00
|
|
|
|
namespace half_impl {
|
|
|
|
|
|
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#if !defined(EIGEN_HAS_GPU_FP16)
|
2017-08-31 02:49:39 +00:00
|
|
|
|
// Make our own __half_raw definition that is similar to CUDA's.
|
|
|
|
|
|
struct __half_raw {
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw() : x(0) {}
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
|
|
|
|
|
|
explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw(numext::uint16_t raw) : x(numext::bit_cast<__fp16>(raw)) {
|
|
|
|
|
|
}
|
|
|
|
|
|
__fp16 x;
|
|
|
|
|
|
#else
|
|
|
|
|
|
explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw(numext::uint16_t raw) : x(raw) {}
|
|
|
|
|
|
numext::uint16_t x;
|
|
|
|
|
|
#endif
|
2016-03-11 17:21:42 -08:00
|
|
|
|
};
|
2020-10-28 20:15:09 +00:00
|
|
|
|
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#elif defined(EIGEN_HAS_HIP_FP16)
|
2019-03-19 21:45:25 +00:00
|
|
|
|
// Nothing to do here
|
|
|
|
|
|
// HIP fp16 header file has a definition for __half_raw
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#elif defined(EIGEN_HAS_CUDA_FP16)
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER < 90000
|
2017-08-31 02:49:39 +00:00
|
|
|
|
// In CUDA < 9.0, __half is the equivalent of CUDA 9's __half_raw
|
2018-06-14 10:21:54 -04:00
|
|
|
|
typedef __half __half_raw;
|
2018-08-03 16:59:15 +01:00
|
|
|
|
#endif // defined(EIGEN_HAS_CUDA_FP16)
|
|
|
|
|
|
|
2019-06-27 12:25:09 +01:00
|
|
|
|
#elif defined(SYCL_DEVICE_ONLY)
|
2018-08-03 16:59:15 +01:00
|
|
|
|
typedef cl::sycl::half __half_raw;
|
|
|
|
|
|
|
2016-03-11 17:21:42 -08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2020-10-28 20:15:09 +00:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x);
|
2017-08-31 02:49:39 +00:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(float ff);
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half_raw h);
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2017-08-31 02:49:39 +00:00
|
|
|
|
struct half_base : public __half_raw {
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base() {}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half_raw& h) : __half_raw(h) {}
|
2018-06-14 10:21:54 -04:00
|
|
|
|
|
|
|
|
|
|
#if defined(EIGEN_HAS_GPU_FP16)
|
|
|
|
|
|
#if defined(EIGEN_HAS_HIP_FP16)
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half& h) { x = __half_as_ushort(h); }
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#elif defined(EIGEN_HAS_CUDA_FP16)
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if (defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000)
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half_base(const __half& h) : __half_raw(*(__half_raw*)&h) {}
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#endif
|
2020-06-25 14:31:16 -07:00
|
|
|
|
#endif
|
2017-08-31 02:49:39 +00:00
|
|
|
|
#endif
|
2016-08-04 00:00:43 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace half_impl
|
|
|
|
|
|
|
2016-03-11 17:21:42 -08:00
|
|
|
|
// Class definition.
|
2016-08-04 00:00:43 +02:00
|
|
|
|
struct half : public half_impl::half_base {
|
2018-06-14 10:21:54 -04:00
|
|
|
|
|
|
|
|
|
|
// Writing this out as separate #if-else blocks to make the code easier to follow
|
|
|
|
|
|
// The same applies to most #if-else blocks in this file
|
|
|
|
|
|
#if !defined(EIGEN_HAS_GPU_FP16)
|
|
|
|
|
|
typedef half_impl::__half_raw __half_raw;
|
|
|
|
|
|
#elif defined(EIGEN_HAS_HIP_FP16)
|
2019-03-19 21:45:25 +00:00
|
|
|
|
// Nothing to do here
|
|
|
|
|
|
// HIP fp16 header file has a definition for __half_raw
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#elif defined(EIGEN_HAS_CUDA_FP16)
|
2019-05-31 15:26:06 -07:00
|
|
|
|
// Note that EIGEN_CUDA_SDK_VER is set to 0 even when compiling with HIP, so
|
|
|
|
|
|
// (EIGEN_CUDA_SDK_VER < 90000) is true even for HIP! So keeping this within
|
|
|
|
|
|
// #if defined(EIGEN_HAS_CUDA_FP16) is needed
|
|
|
|
|
|
#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER < 90000
|
|
|
|
|
|
typedef half_impl::__half_raw __half_raw;
|
|
|
|
|
|
#endif
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#endif
|
2016-08-04 00:00:43 +02:00
|
|
|
|
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half() {}
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half_raw& h) : half_impl::half_base(h) {}
|
2019-08-27 11:30:31 -07:00
|
|
|
|
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#if defined(EIGEN_HAS_GPU_FP16)
|
|
|
|
|
|
#if defined(EIGEN_HAS_HIP_FP16)
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half& h) : half_impl::half_base(h) {}
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#elif defined(EIGEN_HAS_CUDA_FP16)
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(const __half& h) : half_impl::half_base(h) {}
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#endif
|
|
|
|
|
|
#endif
|
2017-08-31 02:49:39 +00:00
|
|
|
|
#endif
|
2019-08-27 11:30:31 -07:00
|
|
|
|
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2020-09-18 17:38:58 +00:00
|
|
|
|
explicit EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR half(bool b)
|
2016-08-04 00:00:43 +02:00
|
|
|
|
: half_impl::half_base(half_impl::raw_uint16_to_half(b ? 0x3c00 : 0)) {}
|
2016-05-23 10:03:03 +02:00
|
|
|
|
template<class T>
|
2016-05-25 10:00:55 +02:00
|
|
|
|
explicit EIGEN_DEVICE_FUNC half(const T& val)
|
2016-08-04 00:00:43 +02:00
|
|
|
|
: half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(val))) {}
|
2016-03-23 09:44:52 -07:00
|
|
|
|
explicit EIGEN_DEVICE_FUNC half(float f)
|
2016-08-04 00:00:43 +02:00
|
|
|
|
: half_impl::half_base(half_impl::float_to_half_rtne(f)) {}
|
2020-10-28 20:15:09 +00:00
|
|
|
|
|
2020-06-25 14:31:16 -07:00
|
|
|
|
// Following the convention of numpy, converting between complex and
|
|
|
|
|
|
// float will lead to loss of imag value.
|
|
|
|
|
|
template<typename RealScalar>
|
|
|
|
|
|
explicit EIGEN_DEVICE_FUNC half(std::complex<RealScalar> c)
|
|
|
|
|
|
: half_impl::half_base(half_impl::float_to_half_rtne(static_cast<float>(c.real()))) {}
|
2016-03-23 09:44:52 -07:00
|
|
|
|
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(bool) const {
|
|
|
|
|
|
// +0.0 and -0.0 become false, everything else becomes true.
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
|
|
|
|
|
|
return (numext::bit_cast<numext::uint16_t>(x) & 0x7fff) != 0;
|
|
|
|
|
|
#else
|
2016-04-06 20:48:55 -07:00
|
|
|
|
return (x & 0x7fff) != 0;
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#endif
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(signed char) const {
|
2016-08-04 00:00:43 +02:00
|
|
|
|
return static_cast<signed char>(half_impl::half_to_float(*this));
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned char) const {
|
2016-08-04 00:00:43 +02:00
|
|
|
|
return static_cast<unsigned char>(half_impl::half_to_float(*this));
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(short) const {
|
2016-08-04 00:00:43 +02:00
|
|
|
|
return static_cast<short>(half_impl::half_to_float(*this));
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
2020-10-28 20:15:09 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(numext::uint16_t) const {
|
|
|
|
|
|
return static_cast<numext::uint16_t>(half_impl::half_to_float(*this));
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(int) const {
|
2016-08-04 00:00:43 +02:00
|
|
|
|
return static_cast<int>(half_impl::half_to_float(*this));
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned int) const {
|
2016-08-04 00:00:43 +02:00
|
|
|
|
return static_cast<unsigned int>(half_impl::half_to_float(*this));
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(long) const {
|
2016-08-04 00:00:43 +02:00
|
|
|
|
return static_cast<long>(half_impl::half_to_float(*this));
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned long) const {
|
2016-08-04 00:00:43 +02:00
|
|
|
|
return static_cast<unsigned long>(half_impl::half_to_float(*this));
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(long long) const {
|
2016-08-04 00:00:43 +02:00
|
|
|
|
return static_cast<long long>(half_impl::half_to_float(*this));
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(unsigned long long) const {
|
2016-07-22 14:33:28 +02:00
|
|
|
|
return static_cast<unsigned long long>(half_to_float(*this));
|
2016-03-23 09:44:52 -07:00
|
|
|
|
}
|
2016-03-18 12:20:08 -07:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(float) const {
|
2016-08-04 00:00:43 +02:00
|
|
|
|
return half_impl::half_to_float(*this);
|
2016-03-11 17:21:42 -08:00
|
|
|
|
}
|
2016-03-18 12:20:08 -07:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(double) const {
|
2016-08-04 00:00:43 +02:00
|
|
|
|
return static_cast<double>(half_impl::half_to_float(*this));
|
2016-03-11 17:21:42 -08:00
|
|
|
|
}
|
2020-06-25 14:31:16 -07:00
|
|
|
|
|
|
|
|
|
|
template<typename RealScalar>
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_EXPLICIT_CAST(std::complex<RealScalar>) const {
|
|
|
|
|
|
return std::complex<RealScalar>(static_cast<RealScalar>(*this), RealScalar(0));
|
|
|
|
|
|
}
|
2016-03-11 17:21:42 -08:00
|
|
|
|
};
|
|
|
|
|
|
|
2018-02-18 15:35:45 -08:00
|
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
template<>
|
|
|
|
|
|
struct numeric_limits<Eigen::half> {
|
|
|
|
|
|
static const bool is_specialized = true;
|
|
|
|
|
|
static const bool is_signed = true;
|
|
|
|
|
|
static const bool is_integer = false;
|
|
|
|
|
|
static const bool is_exact = false;
|
|
|
|
|
|
static const bool has_infinity = true;
|
|
|
|
|
|
static const bool has_quiet_NaN = true;
|
|
|
|
|
|
static const bool has_signaling_NaN = true;
|
|
|
|
|
|
static const float_denorm_style has_denorm = denorm_present;
|
|
|
|
|
|
static const bool has_denorm_loss = false;
|
|
|
|
|
|
static const std::float_round_style round_style = std::round_to_nearest;
|
|
|
|
|
|
static const bool is_iec559 = false;
|
|
|
|
|
|
static const bool is_bounded = false;
|
|
|
|
|
|
static const bool is_modulo = false;
|
|
|
|
|
|
static const int digits = 11;
|
|
|
|
|
|
static const int digits10 = 3; // according to http://half.sourceforge.net/structstd_1_1numeric__limits_3_01half__float_1_1half_01_4.html
|
|
|
|
|
|
static const int max_digits10 = 5; // according to http://half.sourceforge.net/structstd_1_1numeric__limits_3_01half__float_1_1half_01_4.html
|
|
|
|
|
|
static const int radix = 2;
|
|
|
|
|
|
static const int min_exponent = -13;
|
|
|
|
|
|
static const int min_exponent10 = -4;
|
|
|
|
|
|
static const int max_exponent = 16;
|
|
|
|
|
|
static const int max_exponent10 = 4;
|
|
|
|
|
|
static const bool traps = true;
|
|
|
|
|
|
static const bool tinyness_before = false;
|
|
|
|
|
|
|
|
|
|
|
|
static Eigen::half (min)() { return Eigen::half_impl::raw_uint16_to_half(0x400); }
|
|
|
|
|
|
static Eigen::half lowest() { return Eigen::half_impl::raw_uint16_to_half(0xfbff); }
|
|
|
|
|
|
static Eigen::half (max)() { return Eigen::half_impl::raw_uint16_to_half(0x7bff); }
|
|
|
|
|
|
static Eigen::half epsilon() { return Eigen::half_impl::raw_uint16_to_half(0x0800); }
|
|
|
|
|
|
static Eigen::half round_error() { return Eigen::half(0.5); }
|
|
|
|
|
|
static Eigen::half infinity() { return Eigen::half_impl::raw_uint16_to_half(0x7c00); }
|
|
|
|
|
|
static Eigen::half quiet_NaN() { return Eigen::half_impl::raw_uint16_to_half(0x7e00); }
|
|
|
|
|
|
static Eigen::half signaling_NaN() { return Eigen::half_impl::raw_uint16_to_half(0x7e00); }
|
|
|
|
|
|
static Eigen::half denorm_min() { return Eigen::half_impl::raw_uint16_to_half(0x1); }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// If std::numeric_limits<T> is specialized, should also specialize
|
|
|
|
|
|
// std::numeric_limits<const T>, std::numeric_limits<volatile T>, and
|
|
|
|
|
|
// std::numeric_limits<const volatile T>
|
|
|
|
|
|
// https://stackoverflow.com/a/16519653/
|
|
|
|
|
|
template<>
|
|
|
|
|
|
struct numeric_limits<const Eigen::half> : numeric_limits<Eigen::half> {};
|
|
|
|
|
|
template<>
|
|
|
|
|
|
struct numeric_limits<volatile Eigen::half> : numeric_limits<Eigen::half> {};
|
|
|
|
|
|
template<>
|
|
|
|
|
|
struct numeric_limits<const volatile Eigen::half> : numeric_limits<Eigen::half> {};
|
|
|
|
|
|
} // end namespace std
|
|
|
|
|
|
|
|
|
|
|
|
namespace Eigen {
|
|
|
|
|
|
|
2016-08-04 00:00:43 +02:00
|
|
|
|
namespace half_impl {
|
|
|
|
|
|
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && \
|
|
|
|
|
|
EIGEN_CUDA_ARCH >= 530) || \
|
|
|
|
|
|
(defined(EIGEN_HAS_HIP_FP16) && defined(HIP_DEVICE_COMPILE))
|
2020-10-28 20:15:09 +00:00
|
|
|
|
// Note: We deliberatly do *not* define this to 1 even if we have Arm's native
|
|
|
|
|
|
// fp16 type since GPU halfs are rather different from native CPU halfs.
|
|
|
|
|
|
// TODO: Rename to something like EIGEN_HAS_NATIVE_GPU_FP16
|
2019-05-15 13:32:15 -07:00
|
|
|
|
#define EIGEN_HAS_NATIVE_FP16
|
|
|
|
|
|
#endif
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
|
|
|
|
|
// Intrinsics for native fp16 support. Note that on current hardware,
|
|
|
|
|
|
// these are no faster than fp32 arithmetic (you need to use the half2
|
|
|
|
|
|
// versions to get the ALU speed increased), but you do save the
|
|
|
|
|
|
// conversion steps back and forth.
|
|
|
|
|
|
|
2019-05-15 13:32:15 -07:00
|
|
|
|
#if defined(EIGEN_HAS_NATIVE_FP16)
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ half operator + (const half& a, const half& b) {
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
|
2018-09-08 12:05:33 -07:00
|
|
|
|
return __hadd(::__half(a), ::__half(b));
|
|
|
|
|
|
#else
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return __hadd(a, b);
|
2018-09-08 12:05:33 -07:00
|
|
|
|
#endif
|
2016-03-11 17:21:42 -08:00
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ half operator * (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return __hmul(a, b);
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ half operator - (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return __hsub(a, b);
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ half operator / (const half& a, const half& b) {
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if defined(EIGEN_CUDA_SDK_VER) && EIGEN_CUDA_SDK_VER >= 90000
|
2018-09-08 12:05:33 -07:00
|
|
|
|
return __hdiv(a, b);
|
|
|
|
|
|
#else
|
2016-03-11 17:21:42 -08:00
|
|
|
|
float num = __half2float(a);
|
|
|
|
|
|
float denom = __half2float(b);
|
|
|
|
|
|
return __float2half(num / denom);
|
2018-09-08 12:05:33 -07:00
|
|
|
|
#endif
|
2016-03-11 17:21:42 -08:00
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ half operator - (const half& a) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return __hneg(a);
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ half& operator += (half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
a = a + b;
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ half& operator *= (half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
a = a * b;
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ half& operator -= (half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
a = a - b;
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ half& operator /= (half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
a = a / b;
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ bool operator == (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return __heq(a, b);
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ bool operator != (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return __hne(a, b);
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ bool operator < (const half& a, const half& b) {
|
2016-04-06 09:59:51 -07:00
|
|
|
|
return __hlt(a, b);
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ bool operator <= (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return __hle(a, b);
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ bool operator > (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return __hgt(a, b);
|
|
|
|
|
|
}
|
2017-10-21 00:50:38 +00:00
|
|
|
|
EIGEN_STRONG_INLINE __device__ bool operator >= (const half& a, const half& b) {
|
2016-04-06 09:59:51 -07:00
|
|
|
|
return __hge(a, b);
|
|
|
|
|
|
}
|
2019-05-15 13:32:15 -07:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator + (const half& a, const half& b) {
|
|
|
|
|
|
return half(vaddh_f16(a.x, b.x));
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator * (const half& a, const half& b) {
|
|
|
|
|
|
return half(vmulh_f16(a.x, b.x));
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a, const half& b) {
|
|
|
|
|
|
return half(vsubh_f16(a.x, b.x));
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, const half& b) {
|
|
|
|
|
|
return half(vdivh_f16(a.x, b.x));
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a) {
|
|
|
|
|
|
return half(vnegh_f16(a.x));
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator += (half& a, const half& b) {
|
|
|
|
|
|
a = half(vaddh_f16(a.x, b.x));
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator *= (half& a, const half& b) {
|
|
|
|
|
|
a = half(vmulh_f16(a.x, b.x));
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator -= (half& a, const half& b) {
|
|
|
|
|
|
a = half(vsubh_f16(a.x, b.x));
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator /= (half& a, const half& b) {
|
|
|
|
|
|
a = half(vdivh_f16(a.x, b.x));
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator == (const half& a, const half& b) {
|
|
|
|
|
|
return vceqh_f16(a.x, b.x);
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator != (const half& a, const half& b) {
|
|
|
|
|
|
return !vceqh_f16(a.x, b.x);
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator < (const half& a, const half& b) {
|
|
|
|
|
|
return vclth_f16(a.x, b.x);
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator <= (const half& a, const half& b) {
|
|
|
|
|
|
return vcleh_f16(a.x, b.x);
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator > (const half& a, const half& b) {
|
|
|
|
|
|
return vcgth_f16(a.x, b.x);
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator >= (const half& a, const half& b) {
|
|
|
|
|
|
return vcgeh_f16(a.x, b.x);
|
|
|
|
|
|
}
|
2019-06-20 16:18:37 -07:00
|
|
|
|
// We need to distinguish ‘clang as the CUDA compiler’ from ‘clang as the host compiler,
|
|
|
|
|
|
// invoked by NVCC’ (e.g. on MacOS). The former needs to see both host and device implementation
|
2019-06-20 16:23:19 -07:00
|
|
|
|
// of the functions, while the latter can only deal with one of them.
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#elif !defined(EIGEN_HAS_NATIVE_FP16) || (EIGEN_COMP_CLANG && !EIGEN_COMP_NVCC) // Emulate support for half floats
|
2019-05-15 13:32:15 -07:00
|
|
|
|
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if EIGEN_COMP_CLANG && defined(EIGEN_CUDACC)
|
2019-05-15 13:32:15 -07:00
|
|
|
|
// We need to provide emulated *host-side* FP16 operators for clang.
|
|
|
|
|
|
#pragma push_macro("EIGEN_DEVICE_FUNC")
|
|
|
|
|
|
#undef EIGEN_DEVICE_FUNC
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_HAS_NATIVE_FP16)
|
2019-05-15 13:32:15 -07:00
|
|
|
|
#define EIGEN_DEVICE_FUNC __host__
|
|
|
|
|
|
#else // both host and device need emulated ops.
|
|
|
|
|
|
#define EIGEN_DEVICE_FUNC __host__ __device__
|
|
|
|
|
|
#endif
|
|
|
|
|
|
#endif
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2018-06-14 10:21:54 -04:00
|
|
|
|
// Definitions for CPUs and older HIP+CUDA, mostly working through conversion
|
2016-03-11 17:21:42 -08:00
|
|
|
|
// to/from fp32.
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator + (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return half(float(a) + float(b));
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator * (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return half(float(a) * float(b));
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return half(float(a) - float(b));
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return half(float(a) / float(b));
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator - (const half& a) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
half result;
|
|
|
|
|
|
result.x = a.x ^ 0x8000;
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator += (half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
a = half(float(a) + float(b));
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator *= (half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
a = half(float(a) * float(b));
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator -= (half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
a = half(float(a) - float(b));
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half& operator /= (half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
a = half(float(a) / float(b));
|
|
|
|
|
|
return a;
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator == (const half& a, const half& b) {
|
2018-04-11 15:24:13 +02:00
|
|
|
|
return numext::equal_strict(float(a),float(b));
|
2016-03-11 17:21:42 -08:00
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator != (const half& a, const half& b) {
|
2018-04-11 15:24:13 +02:00
|
|
|
|
return numext::not_equal_strict(float(a), float(b));
|
2016-03-11 17:21:42 -08:00
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator < (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return float(a) < float(b);
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator <= (const half& a, const half& b) {
|
2016-04-06 09:59:51 -07:00
|
|
|
|
return float(a) <= float(b);
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator > (const half& a, const half& b) {
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return float(a) > float(b);
|
|
|
|
|
|
}
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool operator >= (const half& a, const half& b) {
|
2016-04-06 09:59:51 -07:00
|
|
|
|
return float(a) >= float(b);
|
|
|
|
|
|
}
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2019-05-15 13:32:15 -07:00
|
|
|
|
#if defined(__clang__) && defined(__CUDA__)
|
|
|
|
|
|
#pragma pop_macro("EIGEN_DEVICE_FUNC")
|
|
|
|
|
|
#endif
|
2016-03-14 08:37:58 -07:00
|
|
|
|
#endif // Emulate support for half floats
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2016-03-23 09:46:42 -07:00
|
|
|
|
// Division by an index. Do it in full float precision to avoid accuracy
|
|
|
|
|
|
// issues in converting the denominator to half.
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half operator / (const half& a, Index b) {
|
2016-07-22 14:33:28 +02:00
|
|
|
|
return half(static_cast<float>(a) / static_cast<float>(b));
|
2016-03-23 09:46:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-03-11 17:21:42 -08:00
|
|
|
|
// Conversion routines, including fallbacks for the host or older CUDA.
|
|
|
|
|
|
// Note that newer Intel CPUs (Haswell or newer) have vectorized versions of
|
|
|
|
|
|
// these in hardware. If we need more performance on older/other CPUs, they are
|
|
|
|
|
|
// also possible to vectorize directly.
|
|
|
|
|
|
|
2020-10-28 20:15:09 +00:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR __half_raw raw_uint16_to_half(numext::uint16_t x) {
|
2020-09-22 20:28:08 +00:00
|
|
|
|
// We cannot simply do a "return __half_raw(x)" here, because __half_raw is union type
|
|
|
|
|
|
// in the hip_fp16 header file, and that will trigger a compile error
|
2020-10-28 20:15:09 +00:00
|
|
|
|
// On the other hand, having anything but a return statement also triggers a compile error
|
2020-09-22 20:28:08 +00:00
|
|
|
|
// because this is constexpr function.
|
|
|
|
|
|
// Fortunately, since we need to disable EIGEN_CONSTEXPR for GPU anyway, we can get out
|
|
|
|
|
|
// of this catch22 by having separate bodies for GPU / non GPU
|
|
|
|
|
|
#if defined(EIGEN_HAS_GPU_FP16)
|
|
|
|
|
|
__half_raw h;
|
|
|
|
|
|
h.x = x;
|
|
|
|
|
|
return h;
|
|
|
|
|
|
#else
|
2020-09-18 17:38:58 +00:00
|
|
|
|
return __half_raw(x);
|
2020-09-22 20:28:08 +00:00
|
|
|
|
#endif
|
2016-03-11 17:21:42 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-28 13:21:28 +02:00
|
|
|
|
union float32_bits {
|
2016-03-11 19:34:21 -08:00
|
|
|
|
unsigned int u;
|
2016-03-11 17:21:42 -08:00
|
|
|
|
float f;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2017-08-31 02:49:39 +00:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC __half_raw float_to_half_rtne(float ff) {
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
|
|
|
|
|
|
(defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
|
2017-08-31 02:49:39 +00:00
|
|
|
|
__half tmp_ff = __float2half(ff);
|
|
|
|
|
|
return *(__half_raw*)&tmp_ff;
|
2016-04-06 17:11:31 -07:00
|
|
|
|
|
|
|
|
|
|
#elif defined(EIGEN_HAS_FP16_C)
|
2017-08-31 02:49:39 +00:00
|
|
|
|
__half_raw h;
|
2016-04-06 17:11:31 -07:00
|
|
|
|
h.x = _cvtss_sh(ff, 0);
|
|
|
|
|
|
return h;
|
|
|
|
|
|
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
|
|
|
|
|
|
__half_raw h;
|
|
|
|
|
|
h.x = static_cast<__fp16>(ff);
|
|
|
|
|
|
return h;
|
|
|
|
|
|
|
2016-03-11 17:21:42 -08:00
|
|
|
|
#else
|
2018-08-28 13:21:28 +02:00
|
|
|
|
float32_bits f; f.f = ff;
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2018-08-28 13:21:28 +02:00
|
|
|
|
const float32_bits f32infty = { 255 << 23 };
|
|
|
|
|
|
const float32_bits f16max = { (127 + 16) << 23 };
|
|
|
|
|
|
const float32_bits denorm_magic = { ((127 - 15) + (23 - 10) + 1) << 23 };
|
2016-03-11 19:34:21 -08:00
|
|
|
|
unsigned int sign_mask = 0x80000000u;
|
2017-08-31 02:49:39 +00:00
|
|
|
|
__half_raw o;
|
2020-10-28 20:15:09 +00:00
|
|
|
|
o.x = static_cast<numext::uint16_t>(0x0u);
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2016-03-11 19:34:21 -08:00
|
|
|
|
unsigned int sign = f.u & sign_mask;
|
2016-03-11 17:21:42 -08:00
|
|
|
|
f.u ^= sign;
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE all the integer compares in this function can be safely
|
|
|
|
|
|
// compiled into signed compares since all operands are below
|
|
|
|
|
|
// 0x80000000. Important if you want fast straight SSE2 code
|
|
|
|
|
|
// (since there's no unsigned PCMPGTD).
|
|
|
|
|
|
|
|
|
|
|
|
if (f.u >= f16max.u) { // result is Inf or NaN (all exponent bits set)
|
|
|
|
|
|
o.x = (f.u > f32infty.u) ? 0x7e00 : 0x7c00; // NaN->qNaN and Inf->Inf
|
|
|
|
|
|
} else { // (De)normalized number or zero
|
|
|
|
|
|
if (f.u < (113 << 23)) { // resulting FP16 is subnormal or zero
|
|
|
|
|
|
// use a magic value to align our 10 mantissa bits at the bottom of
|
|
|
|
|
|
// the float. as long as FP addition is round-to-nearest-even this
|
|
|
|
|
|
// just works.
|
|
|
|
|
|
f.f += denorm_magic.f;
|
|
|
|
|
|
|
|
|
|
|
|
// and one integer subtract of the bias later, we have our final float!
|
2020-10-28 20:15:09 +00:00
|
|
|
|
o.x = static_cast<numext::uint16_t>(f.u - denorm_magic.u);
|
2016-03-11 17:21:42 -08:00
|
|
|
|
} else {
|
2016-03-11 19:34:21 -08:00
|
|
|
|
unsigned int mant_odd = (f.u >> 13) & 1; // resulting mantissa is odd
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
|
|
|
|
|
// update exponent, rounding bias part 1
|
2020-09-10 16:22:28 +02:00
|
|
|
|
// Equivalent to `f.u += ((unsigned int)(15 - 127) << 23) + 0xfff`, but
|
|
|
|
|
|
// without arithmetic overflow.
|
|
|
|
|
|
f.u += 0xc8000fffU;
|
2016-03-11 17:21:42 -08:00
|
|
|
|
// rounding bias part 2
|
|
|
|
|
|
f.u += mant_odd;
|
|
|
|
|
|
// take the bits!
|
2020-10-28 20:15:09 +00:00
|
|
|
|
o.x = static_cast<numext::uint16_t>(f.u >> 13);
|
2016-03-11 17:21:42 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-28 20:15:09 +00:00
|
|
|
|
o.x |= static_cast<numext::uint16_t>(sign >> 16);
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return o;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-31 02:49:39 +00:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC float half_to_float(__half_raw h) {
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
|
|
|
|
|
|
(defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return __half2float(h);
|
2016-04-06 17:11:31 -07:00
|
|
|
|
#elif defined(EIGEN_HAS_FP16_C)
|
|
|
|
|
|
return _cvtsh_ss(h.x);
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
|
|
|
|
|
|
return static_cast<float>(h.x);
|
2016-03-11 17:21:42 -08:00
|
|
|
|
#else
|
2018-08-28 13:21:28 +02:00
|
|
|
|
const float32_bits magic = { 113 << 23 };
|
2016-03-11 19:34:21 -08:00
|
|
|
|
const unsigned int shifted_exp = 0x7c00 << 13; // exponent mask after shift
|
2018-08-28 13:21:28 +02:00
|
|
|
|
float32_bits o;
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2016-03-11 19:34:21 -08:00
|
|
|
|
o.u = (h.x & 0x7fff) << 13; // exponent/mantissa bits
|
|
|
|
|
|
unsigned int exp = shifted_exp & o.u; // just the exponent
|
|
|
|
|
|
o.u += (127 - 15) << 23; // exponent adjust
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
|
|
|
|
|
// handle exponent special cases
|
2016-03-11 19:34:21 -08:00
|
|
|
|
if (exp == shifted_exp) { // Inf/NaN?
|
2016-03-11 17:21:42 -08:00
|
|
|
|
o.u += (128 - 16) << 23; // extra exp adjust
|
2016-03-11 19:34:21 -08:00
|
|
|
|
} else if (exp == 0) { // Zero/Denormal?
|
2016-03-11 17:21:42 -08:00
|
|
|
|
o.u += 1 << 23; // extra exp adjust
|
|
|
|
|
|
o.f -= magic.f; // renormalize
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
o.u |= (h.x & 0x8000) << 16; // sign bit
|
|
|
|
|
|
return o.f;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-22 14:33:28 +02:00
|
|
|
|
// --- standard functions ---
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isinf)(const half& a) {
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#ifdef EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC
|
|
|
|
|
|
return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) == 0x7c00;
|
|
|
|
|
|
#else
|
2016-03-11 17:21:42 -08:00
|
|
|
|
return (a.x & 0x7fff) == 0x7c00;
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#endif
|
2016-03-11 17:21:42 -08:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isnan)(const half& a) {
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
|
|
|
|
|
|
(defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
|
2016-03-14 09:25:16 -07:00
|
|
|
|
return __hisnan(a);
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#elif defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
|
|
|
|
|
|
return (numext::bit_cast<numext::uint16_t>(a.x) & 0x7fff) > 0x7c00;
|
2016-03-11 17:21:42 -08:00
|
|
|
|
#else
|
|
|
|
|
|
return (a.x & 0x7fff) > 0x7c00;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool (isfinite)(const half& a) {
|
2016-07-21 15:10:48 +02:00
|
|
|
|
return !(isinf EIGEN_NOT_A_MACRO (a)) && !(isnan EIGEN_NOT_A_MACRO (a));
|
2016-04-07 11:40:15 -07:00
|
|
|
|
}
|
2016-04-27 12:10:25 -07:00
|
|
|
|
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half abs(const half& a) {
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#if defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
|
|
|
|
|
|
return half(vabsh_f16(a.x));
|
|
|
|
|
|
#else
|
2016-07-22 14:33:28 +02:00
|
|
|
|
half result;
|
2016-04-07 11:40:15 -07:00
|
|
|
|
result.x = a.x & 0x7FFF;
|
|
|
|
|
|
return result;
|
2020-10-28 20:15:09 +00:00
|
|
|
|
#endif
|
2016-04-07 11:40:15 -07:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half exp(const half& a) {
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
|
2018-06-14 10:21:54 -04:00
|
|
|
|
defined(EIGEN_HIP_DEVICE_COMPILE)
|
2016-11-16 09:01:51 -08:00
|
|
|
|
return half(hexp(a));
|
|
|
|
|
|
#else
|
|
|
|
|
|
return half(::expf(float(a)));
|
|
|
|
|
|
#endif
|
2016-04-07 11:40:15 -07:00
|
|
|
|
}
|
2016-12-02 14:13:01 -08:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half expm1(const half& a) {
|
|
|
|
|
|
return half(numext::expm1(float(a)));
|
|
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log(const half& a) {
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if (defined(EIGEN_HAS_CUDA_FP16) && EIGEN_CUDA_SDK_VER >= 80000 && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
|
2018-06-14 10:21:54 -04:00
|
|
|
|
(defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
|
2016-11-16 09:01:51 -08:00
|
|
|
|
return half(::hlog(a));
|
2016-08-29 11:05:32 +02:00
|
|
|
|
#else
|
2016-07-22 14:33:28 +02:00
|
|
|
|
return half(::logf(float(a)));
|
2016-08-29 11:05:32 +02:00
|
|
|
|
#endif
|
2016-04-07 11:40:15 -07:00
|
|
|
|
}
|
2016-08-08 20:24:59 +01:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log1p(const half& a) {
|
2016-08-22 15:44:21 +02:00
|
|
|
|
return half(numext::log1p(float(a)));
|
2016-08-08 20:24:59 +01:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half log10(const half& a) {
|
|
|
|
|
|
return half(::log10f(float(a)));
|
2016-07-21 15:46:45 +02:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sqrt(const half& a) {
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 530) || \
|
2018-06-14 10:21:54 -04:00
|
|
|
|
defined(EIGEN_HIP_DEVICE_COMPILE)
|
2016-11-16 09:01:51 -08:00
|
|
|
|
return half(hsqrt(a));
|
|
|
|
|
|
#else
|
|
|
|
|
|
return half(::sqrtf(float(a)));
|
|
|
|
|
|
#endif
|
2016-04-07 11:40:15 -07:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half pow(const half& a, const half& b) {
|
|
|
|
|
|
return half(::powf(float(a), float(b)));
|
2016-04-08 14:22:39 -07:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half sin(const half& a) {
|
|
|
|
|
|
return half(::sinf(float(a)));
|
2016-04-13 14:12:38 -07:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half cos(const half& a) {
|
|
|
|
|
|
return half(::cosf(float(a)));
|
2016-04-13 14:12:38 -07:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tan(const half& a) {
|
|
|
|
|
|
return half(::tanf(float(a)));
|
2016-04-13 14:12:38 -07:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half tanh(const half& a) {
|
|
|
|
|
|
return half(::tanhf(float(a)));
|
2016-04-13 14:12:38 -07:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half floor(const half& a) {
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 300) || \
|
2018-06-14 10:21:54 -04:00
|
|
|
|
defined(EIGEN_HIP_DEVICE_COMPILE)
|
2016-11-16 09:01:51 -08:00
|
|
|
|
return half(hfloor(a));
|
|
|
|
|
|
#else
|
2016-07-22 14:33:28 +02:00
|
|
|
|
return half(::floorf(float(a)));
|
2016-11-16 09:01:51 -08:00
|
|
|
|
#endif
|
2016-04-07 11:40:15 -07:00
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half ceil(const half& a) {
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if (EIGEN_CUDA_SDK_VER >= 80000 && defined EIGEN_CUDA_ARCH && EIGEN_CUDA_ARCH >= 300) || \
|
2018-06-14 10:21:54 -04:00
|
|
|
|
defined(EIGEN_HIP_DEVICE_COMPILE)
|
2016-11-16 09:01:51 -08:00
|
|
|
|
return half(hceil(a));
|
|
|
|
|
|
#else
|
2016-07-22 14:33:28 +02:00
|
|
|
|
return half(::ceilf(float(a)));
|
2016-11-16 09:01:51 -08:00
|
|
|
|
#endif
|
2016-04-07 11:40:15 -07:00
|
|
|
|
}
|
2016-04-13 18:44:48 -07:00
|
|
|
|
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half (min)(const half& a, const half& b) {
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
|
|
|
|
|
|
(defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
|
2016-04-27 12:57:21 -07:00
|
|
|
|
return __hlt(b, a) ? b : a;
|
|
|
|
|
|
#else
|
|
|
|
|
|
const float f1 = static_cast<float>(a);
|
|
|
|
|
|
const float f2 = static_cast<float>(b);
|
|
|
|
|
|
return f2 < f1 ? b : a;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half (max)(const half& a, const half& b) {
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#if (defined(EIGEN_HAS_CUDA_FP16) && defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 530) || \
|
|
|
|
|
|
(defined(EIGEN_HAS_HIP_FP16) && defined(EIGEN_HIP_DEVICE_COMPILE))
|
2016-04-27 12:57:21 -07:00
|
|
|
|
return __hlt(a, b) ? b : a;
|
|
|
|
|
|
#else
|
|
|
|
|
|
const float f1 = static_cast<float>(a);
|
|
|
|
|
|
const float f2 = static_cast<float>(b);
|
|
|
|
|
|
return f1 < f2 ? b : a;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
}
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
2018-10-22 21:14:40 +02:00
|
|
|
|
#ifndef EIGEN_NO_IO
|
2016-07-22 14:33:28 +02:00
|
|
|
|
EIGEN_ALWAYS_INLINE std::ostream& operator << (std::ostream& os, const half& v) {
|
|
|
|
|
|
os << static_cast<float>(v);
|
|
|
|
|
|
return os;
|
|
|
|
|
|
}
|
2018-10-22 21:14:40 +02:00
|
|
|
|
#endif
|
2016-07-22 14:33:28 +02:00
|
|
|
|
|
|
|
|
|
|
} // end namespace half_impl
|
|
|
|
|
|
|
|
|
|
|
|
// import Eigen::half_impl::half into Eigen namespace
|
2016-08-04 00:00:43 +02:00
|
|
|
|
// using half_impl::half;
|
2016-07-22 14:33:28 +02:00
|
|
|
|
|
2016-07-21 15:46:45 +02:00
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
|
|
template<>
|
2016-08-04 00:00:43 +02:00
|
|
|
|
struct random_default_impl<half, false, false>
|
2016-07-21 15:46:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
static inline half run(const half& x, const half& y)
|
|
|
|
|
|
{
|
|
|
|
|
|
return x + (y-x) * half(float(std::rand()) / float(RAND_MAX));
|
|
|
|
|
|
}
|
|
|
|
|
|
static inline half run()
|
|
|
|
|
|
{
|
|
|
|
|
|
return run(half(-1.f), half(1.f));
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-08-04 00:00:43 +02:00
|
|
|
|
template<> struct is_arithmetic<half> { enum { value = true }; };
|
2016-07-21 15:46:45 +02:00
|
|
|
|
|
|
|
|
|
|
} // end namespace internal
|
|
|
|
|
|
|
2016-08-04 00:00:43 +02:00
|
|
|
|
template<> struct NumTraits<Eigen::half>
|
|
|
|
|
|
: GenericNumTraits<Eigen::half>
|
2016-07-22 14:33:28 +02:00
|
|
|
|
{
|
2017-06-09 11:51:53 +02:00
|
|
|
|
enum {
|
|
|
|
|
|
IsSigned = true,
|
|
|
|
|
|
IsInteger = false,
|
|
|
|
|
|
IsComplex = false,
|
|
|
|
|
|
RequireInitialization = false
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half epsilon() {
|
2016-07-22 14:33:28 +02:00
|
|
|
|
return half_impl::raw_uint16_to_half(0x0800);
|
|
|
|
|
|
}
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half dummy_precision() {
|
|
|
|
|
|
return half_impl::raw_uint16_to_half(0x211f); // Eigen::half(1e-2f);
|
|
|
|
|
|
}
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half highest() {
|
2016-07-22 14:33:28 +02:00
|
|
|
|
return half_impl::raw_uint16_to_half(0x7bff);
|
|
|
|
|
|
}
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half lowest() {
|
2016-07-22 14:33:28 +02:00
|
|
|
|
return half_impl::raw_uint16_to_half(0xfbff);
|
|
|
|
|
|
}
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half infinity() {
|
2016-07-22 14:33:28 +02:00
|
|
|
|
return half_impl::raw_uint16_to_half(0x7c00);
|
|
|
|
|
|
}
|
2020-09-18 17:38:58 +00:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR static EIGEN_STRONG_INLINE Eigen::half quiet_NaN() {
|
2016-07-22 14:33:28 +02:00
|
|
|
|
return half_impl::raw_uint16_to_half(0x7c01);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-03-11 17:21:42 -08:00
|
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
|
2020-11-17 08:24:58 -08:00
|
|
|
|
#if defined(EIGEN_HAS_GPU_FP16) || defined(EIGEN_HAS_ARM64_FP16_SCALAR_ARITHMETIC)
|
2020-09-22 20:28:08 +00:00
|
|
|
|
#pragma pop_macro("EIGEN_CONSTEXPR")
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2016-03-29 09:20:36 -07:00
|
|
|
|
namespace std {
|
|
|
|
|
|
|
2016-03-31 14:44:55 -07:00
|
|
|
|
#if __cplusplus > 199711L
|
2016-03-31 13:09:58 -07:00
|
|
|
|
template <>
|
2016-03-31 13:09:23 -07:00
|
|
|
|
struct hash<Eigen::half> {
|
2016-04-06 13:44:08 -07:00
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE std::size_t operator()(const Eigen::half& a) const {
|
|
|
|
|
|
return static_cast<std::size_t>(a.x);
|
2016-03-31 13:09:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
};
|
2016-03-31 14:44:55 -07:00
|
|
|
|
#endif
|
2016-03-11 17:21:42 -08:00
|
|
|
|
|
|
|
|
|
|
} // end namespace std
|
|
|
|
|
|
|
2016-03-22 09:30:23 -07:00
|
|
|
|
// Add the missing shfl_xor intrinsic
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#if (defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 300) || \
|
2020-03-11 23:06:56 +00:00
|
|
|
|
defined(EIGEN_HIPCC)
|
2018-06-14 10:21:54 -04:00
|
|
|
|
|
2017-10-21 00:50:38 +00:00
|
|
|
|
__device__ EIGEN_STRONG_INLINE Eigen::half __shfl_xor(Eigen::half var, int laneMask, int width=warpSize) {
|
2019-05-31 15:26:06 -07:00
|
|
|
|
#if (EIGEN_CUDA_SDK_VER < 90000) || \
|
2018-06-14 10:21:54 -04:00
|
|
|
|
defined(EIGEN_HAS_HIP_FP16)
|
2016-03-22 09:30:23 -07:00
|
|
|
|
return static_cast<Eigen::half>(__shfl_xor(static_cast<float>(var), laneMask, width));
|
2017-08-31 02:49:39 +00:00
|
|
|
|
#else
|
|
|
|
|
|
return static_cast<Eigen::half>(__shfl_xor_sync(0xFFFFFFFF, static_cast<float>(var), laneMask, width));
|
|
|
|
|
|
#endif
|
2016-03-22 09:30:23 -07:00
|
|
|
|
}
|
2016-03-31 10:55:03 -07:00
|
|
|
|
#endif
|
2016-03-22 09:30:23 -07:00
|
|
|
|
|
2017-08-31 02:49:39 +00:00
|
|
|
|
// ldg() has an overload for __half_raw, but we also need one for Eigen::half.
|
2020-10-29 07:37:52 +01:00
|
|
|
|
#if (defined(EIGEN_CUDA_ARCH) && EIGEN_CUDA_ARCH >= 350) || defined(EIGEN_HIPCC)
|
2016-05-25 10:00:55 +02:00
|
|
|
|
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Eigen::half __ldg(const Eigen::half* ptr) {
|
2020-10-29 15:34:05 +00:00
|
|
|
|
return Eigen::half_impl::raw_uint16_to_half(__ldg(reinterpret_cast<const Eigen::numext::uint16_t*>(ptr)));
|
2016-03-31 10:55:03 -07:00
|
|
|
|
}
|
2016-03-22 09:30:23 -07:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2018-06-14 10:21:54 -04:00
|
|
|
|
#if defined(EIGEN_GPU_COMPILE_PHASE)
|
2016-08-04 17:25:53 -07:00
|
|
|
|
namespace Eigen {
|
|
|
|
|
|
namespace numext {
|
|
|
|
|
|
|
2020-10-29 07:37:52 +01:00
|
|
|
|
template <>
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isnan)(const Eigen::half& h) {
|
2016-08-04 17:25:53 -07:00
|
|
|
|
return (half_impl::isnan)(h);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 07:37:52 +01:00
|
|
|
|
template <>
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isinf)(const Eigen::half& h) {
|
2016-08-04 17:25:53 -07:00
|
|
|
|
return (half_impl::isinf)(h);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-29 07:37:52 +01:00
|
|
|
|
template <>
|
|
|
|
|
|
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE bool(isfinite)(const Eigen::half& h) {
|
2016-08-04 17:25:53 -07:00
|
|
|
|
return (half_impl::isfinite)(h);
|
|
|
|
|
|
}
|
|
|
|
|
|
} // namespace numext
|
2020-10-29 07:37:52 +01:00
|
|
|
|
} // namespace Eigen
|
2016-08-04 17:25:53 -07:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2019-08-27 11:30:31 -07:00
|
|
|
|
#endif // EIGEN_HALF_H
|