improve random

This commit is contained in:
Charles Schlosser
2024-01-31 08:16:29 +00:00
parent a9ddab3e06
commit d626762e3f
9 changed files with 405 additions and 151 deletions

View File

@@ -677,16 +677,22 @@ EIGEN_ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, const bfloat16& v
namespace internal {
template <>
struct random_default_impl<bfloat16, false, false> {
static inline bfloat16 run(const bfloat16& x, const bfloat16& y) {
return x + (y - x) * bfloat16(float(std::rand()) / float(RAND_MAX));
}
static inline bfloat16 run() { return run(bfloat16(-1.f), bfloat16(1.f)); }
struct is_arithmetic<bfloat16> {
enum { value = true };
};
template <>
struct is_arithmetic<bfloat16> {
enum { value = true };
struct random_impl<bfloat16> {
enum : int { MantissaBits = 7 };
using Impl = random_impl<float>;
static EIGEN_DEVICE_FUNC inline bfloat16 run(const bfloat16& x, const bfloat16& y) {
float result = Impl::run(x, y, MantissaBits);
return bfloat16(result);
}
static EIGEN_DEVICE_FUNC inline bfloat16 run() {
float result = Impl::run(MantissaBits);
return bfloat16(result);
}
};
} // namespace internal

View File

@@ -762,16 +762,22 @@ EIGEN_ALWAYS_INLINE std::ostream& operator<<(std::ostream& os, const half& v) {
namespace internal {
template <>
struct random_default_impl<half, false, false> {
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)); }
struct is_arithmetic<half> {
enum { value = true };
};
template <>
struct is_arithmetic<half> {
enum { value = true };
struct random_impl<half> {
enum : int { MantissaBits = 10 };
using Impl = random_impl<float>;
static EIGEN_DEVICE_FUNC inline half run(const half& x, const half& y) {
float result = Impl::run(x, y, MantissaBits);
return half(result);
}
static EIGEN_DEVICE_FUNC inline half run() {
float result = Impl::run(MantissaBits);
return half(result);
}
};
} // end namespace internal