Add bit_cast for half/bfloat to/from uint16_t, fix TensorRandom

The existing `TensorRandom.h` implementation makes the assumption that
`half` (`bfloat16`) has a `uint16_t` member `x` (`value`), which is not
always true. This currently fails on arm64, where `x` has type `__fp16`.
Added `bit_cast` specializations to allow casting to/from `uint16_t`
for both `half` and `bfloat16`.  Also added tests in
`half_float`, `bfloat16_float`, and `cxx11_tensor_random` to catch
these errors in the future.
This commit is contained in:
Antonio Sanchez
2020-11-17 15:32:44 -08:00
committed by Antonio Sánchez
parent 41d5d5334b
commit 17268b155d
6 changed files with 196 additions and 150 deletions

View File

@@ -11,9 +11,10 @@
#include <Eigen/CXX11/Tensor>
template<typename Scalar>
static void test_default()
{
Tensor<float, 1> vec(6);
Tensor<Scalar, 1> vec(6);
vec.setRandom();
// Fixme: we should check that the generated numbers follow a uniform
@@ -23,10 +24,11 @@ static void test_default()
}
}
template<typename Scalar>
static void test_normal()
{
Tensor<float, 1> vec(6);
vec.setRandom<Eigen::internal::NormalRandomGenerator<float>>();
Tensor<Scalar, 1> vec(6);
vec.template setRandom<Eigen::internal::NormalRandomGenerator<Scalar>>();
// Fixme: we should check that the generated numbers follow a gaussian
// distribution instead.
@@ -72,7 +74,13 @@ static void test_custom()
EIGEN_DECLARE_TEST(cxx11_tensor_random)
{
CALL_SUBTEST(test_default());
CALL_SUBTEST(test_normal());
CALL_SUBTEST((test_default<float>()));
CALL_SUBTEST((test_normal<float>()));
CALL_SUBTEST((test_default<double>()));
CALL_SUBTEST((test_normal<double>()));
CALL_SUBTEST((test_default<Eigen::half>()));
CALL_SUBTEST((test_normal<Eigen::half>()));
CALL_SUBTEST((test_default<Eigen::bfloat16>()));
CALL_SUBTEST((test_normal<Eigen::bfloat16>()));
CALL_SUBTEST(test_custom());
}