ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw::cker::random Namespace Reference

Data Structures

class  Array
 
class  NormalDistribution
 
class  NormalDistribution< Generator, double >
 
class  NormalDistribution< Generator, Eigen::half >
 
class  NormalDistribution< Generator, float >
 
class  PhiloxRandom
 
class  SingleSampleAdapter
 
class  TruncatedNormalDistribution
 
class  TruncatedNormalDistribution< SingleSampleGenerator, double >
 
class  TruncatedNormalDistribution< SingleSampleGenerator, Eigen::half >
 
class  TruncatedNormalDistribution< SingleSampleGenerator, float >
 
class  UniformDistribution
 
class  UniformDistribution< Generator, double >
 
class  UniformDistribution< Generator, Eigen::half >
 
class  UniformDistribution< Generator, float >
 
class  UniformDistribution< Generator, int32_t >
 
class  UniformDistribution< Generator, int64_t >
 
class  UniformFullIntDistribution
 
class  UniformFullIntDistribution32
 
class  UniformFullIntDistribution64
 
class  UniformFullIntDistribution< Generator, int32_t >
 
class  UniformFullIntDistribution< Generator, int64_t >
 
class  UniformFullIntDistribution< Generator, uint32_t >
 
class  UniformFullIntDistribution< Generator, uint64_t >
 

Functions

PHILOX_DEVICE_INLINE Eigen::half Uint16ToHalf (uint16_t x)
 
PHILOX_DEVICE_INLINE float Uint32ToFloat (uint32_t x)
 
PHILOX_DEVICE_INLINE double Uint64ToDouble (uint32_t x0, uint32_t x1)
 
template<typename Int >
PHILOX_DEVICE_INLINE Int SignedAdd (Int a, typename std::make_unsigned< Int >::type b)
 
PHILOX_DEVICE_INLINE void BoxMullerFloat (uint32_t x0, uint32_t x1, float *f0, float *f1)
 
PHILOX_DEVICE_INLINE void BoxMullerDouble (uint32_t x0, uint32_t x1, uint32_t x2, uint32_t x3, double *d0, double *d1)
 

Function Documentation

◆ BoxMullerDouble()

PHILOX_DEVICE_INLINE void nnfw::cker::random::BoxMullerDouble ( uint32_t  x0,
uint32_t  x1,
uint32_t  x2,
uint32_t  x3,
double *  d0,
double *  d1 
)

Definition at line 693 of file RandomDistributions.h.

694{
695 // This function implements the Box-Muller transform:
696 // http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform#Basic_form
697 // Do not send a really small number to log().
698 // We cannot mark "epsilon" as "static const" because NVCC would complain
699 const double epsilon = 1.0e-7;
700 double u1 = Uint64ToDouble(x0, x1);
701 if (u1 < epsilon)
702 {
703 u1 = epsilon;
704 }
705 const double v1 = 2 * M_PI * Uint64ToDouble(x2, x3);
706 const double u2 = Eigen::numext::sqrt(-2.0 * Eigen::numext::log(u1));
707#if defined(TENSORFLOW_USE_SYCL) || !defined(__linux__)
708 *d0 = Eigen::numext::sin(v1);
709 *d1 = Eigen::numext::cos(v1);
710#else
711 sincos(v1, d0, d1);
712#endif
713 *d0 *= u2;
714 *d1 *= u2;
715}
PHILOX_DEVICE_INLINE double Uint64ToDouble(uint32_t x0, uint32_t x1)

References Uint64ToDouble().

Referenced by nnfw::cker::random::NormalDistribution< Generator, double >::operator()(), and nnfw::cker::random::TruncatedNormalDistribution< SingleSampleGenerator, double >::operator()().

◆ BoxMullerFloat()

PHILOX_DEVICE_INLINE void nnfw::cker::random::BoxMullerFloat ( uint32_t  x0,
uint32_t  x1,
float *  f0,
float *  f1 
)

Definition at line 666 of file RandomDistributions.h.

667{
668 // This function implements the Box-Muller transform:
669 // http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform#Basic_form
670 // Do not send a really small number to log().
671 // We cannot mark "epsilon" as "static const" because NVCC would complain
672 const float epsilon = 1.0e-7f;
673 float u1 = Uint32ToFloat(x0);
674 if (u1 < epsilon)
675 {
676 u1 = epsilon;
677 }
678 const float v1 = 2.0f * M_PI * Uint32ToFloat(x1);
679 const float u2 = Eigen::numext::sqrt(-2.0f * Eigen::numext::log(u1));
680#if defined(TENSORFLOW_USE_SYCL) || !defined(__linux__)
681 *f0 = Eigen::numext::sin(v1);
682 *f1 = Eigen::numext::cos(v1);
683#else
684 sincosf(v1, f0, f1);
685#endif
686 *f0 *= u2;
687 *f1 *= u2;
688}
PHILOX_DEVICE_INLINE float Uint32ToFloat(uint32_t x)

References Uint32ToFloat().

Referenced by nnfw::cker::random::NormalDistribution< Generator, Eigen::half >::operator()(), nnfw::cker::random::NormalDistribution< Generator, float >::operator()(), nnfw::cker::random::TruncatedNormalDistribution< SingleSampleGenerator, Eigen::half >::operator()(), and nnfw::cker::random::TruncatedNormalDistribution< SingleSampleGenerator, float >::operator()().

◆ SignedAdd()

template<typename Int >
PHILOX_DEVICE_INLINE Int nnfw::cker::random::SignedAdd ( Int  a,
typename std::make_unsigned< Int >::type  b 
)

Definition at line 57 of file RandomDistributions.h.

58{
59 // Implementation note: both b_div_2 and b - b_div_2 are positive and
60 // representable as Int.
61 auto b_div_2 = b >> 1;
62 return a + static_cast<Int>(b_div_2) + static_cast<Int>(b - b_div_2);
63}

Referenced by nnfw::cker::random::UniformDistribution< Generator, int32_t >::operator()(), and nnfw::cker::random::UniformDistribution< Generator, int64_t >::operator()().

◆ Uint16ToHalf()

PHILOX_DEVICE_INLINE Eigen::half nnfw::cker::random::Uint16ToHalf ( uint16_t  x)

Definition at line 718 of file RandomDistributions.h.

719{
720 // IEEE754 halfs are formatted as follows (MSB first):
721 // sign(1) exponent(5) mantissa(10)
722 // Conceptually construct the following:
723 // sign == 0
724 // exponent == 15 -- an excess 15 representation of a zero exponent
725 // mantissa == 10 random bits
726 const uint16_t man = x & 0x3ffu; // 10 bit mantissa
727 const uint16_t exp = static_cast<uint16_t>(15);
728 const uint16_t val = (exp << 10) | man;
729
730 Eigen::half result;
731 result.x = val;
732 return result - Eigen::half(1.0);
733}

Referenced by nnfw::cker::random::UniformDistribution< Generator, Eigen::half >::operator()().

◆ Uint32ToFloat()

PHILOX_DEVICE_INLINE float nnfw::cker::random::Uint32ToFloat ( uint32_t  x)

Definition at line 736 of file RandomDistributions.h.

737{
738 // IEEE754 floats are formatted as follows (MSB first):
739 // sign(1) exponent(8) mantissa(23)
740 // Conceptually construct the following:
741 // sign == 0
742 // exponent == 127 -- an excess 127 representation of a zero exponent
743 // mantissa == 23 random bits
744 const uint32_t man = x & 0x7fffffu; // 23 bit mantissa
745 const uint32_t exp = static_cast<uint32_t>(127);
746 const uint32_t val = (exp << 23) | man;
747
748 // Assumes that endian-ness is same for float and uint32.
749 float result;
750 memcpy(&result, &val, sizeof(val));
751 return result - 1.0f;
752}

Referenced by BoxMullerFloat(), and nnfw::cker::random::UniformDistribution< Generator, float >::operator()().

◆ Uint64ToDouble()

PHILOX_DEVICE_INLINE double nnfw::cker::random::Uint64ToDouble ( uint32_t  x0,
uint32_t  x1 
)

Definition at line 755 of file RandomDistributions.h.

756{
757 // IEEE754 doubles are formatted as follows (MSB first):
758 // sign(1) exponent(11) mantissa(52)
759 // Conceptually construct the following:
760 // sign == 0
761 // exponent == 1023 -- an excess 1023 representation of a zero exponent
762 // mantissa == 52 random bits
763 const uint32_t mhi = x0 & 0xfffffu; // upper 20 bits of mantissa
764 const uint32_t mlo = x1; // lower 32 bits of mantissa
765 const uint64_t man = (static_cast<uint64_t>(mhi) << 32) | mlo; // mantissa
766 const uint64_t exp = static_cast<uint64_t>(1023);
767 const uint64_t val = (exp << 52) | man;
768 // Assumes that endian-ness is same for double and uint64.
769 double result;
770 memcpy(&result, &val, sizeof(val));
771 return result - 1.0;
772}

Referenced by BoxMullerDouble(), and nnfw::cker::random::UniformDistribution< Generator, double >::operator()().