Support BFloat16 in Eigen

This commit is contained in:
Teng Lu
2020-06-20 19:16:24 +00:00
committed by Rasmus Munk Larsen
parent 6b9c92fe7e
commit 386d809bde
19 changed files with 1893 additions and 14 deletions

View File

@@ -101,6 +101,17 @@ Eigen::half RandomToTypeUniform<Eigen::half>(uint64_t* state, uint64_t stream) {
return result - Eigen::half(1.0f);
}
template <> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
Eigen::bfloat16 RandomToTypeUniform<Eigen::bfloat16>(uint64_t* state, uint64_t stream) {
Eigen::bfloat16 result;
// Generate 7 random bits for the mantissa
unsigned rnd = PCG_XSH_RS_generator(state, stream);
result.value = static_cast<uint16_t>(rnd & 0x7fu);
// Set the exponent
result.value |= (static_cast<uint16_t>(127) << 7);
// Return the final result
return result - Eigen::bfloat16(1.0f);
}
template <> EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
float RandomToTypeUniform<float>(uint64_t* state, uint64_t stream) {