From cb539dab78414252ca992a00252639d13dbde941 Mon Sep 17 00:00:00 2001 From: Alexander Freudenberg <50404848+alexfreudenberg@users.noreply.github.com> Date: Sat, 1 Oct 2022 04:51:30 +0200 Subject: [PATCH] Correct typos in comments (#639) * Correct typos in comments Correct comments in code on type of generated distribution. Improve Gaussian RNG to take advantage of Box Muller method * Inline Box Muller Added inline function for the Box Muller algorithm and updated code comments to be more concise * Update tensor_fill.h * Update tensor_fill.h * small changes to pass tests Co-authored-by: Haicheng Wu --- .../cutlass/util/reference/host/tensor_fill.h | 98 +++++++++++-------- 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/tools/util/include/cutlass/util/reference/host/tensor_fill.h b/tools/util/include/cutlass/util/reference/host/tensor_fill.h index b76100b7..a795d5f8 100644 --- a/tools/util/include/cutlass/util/reference/host/tensor_fill.h +++ b/tools/util/include/cutlass/util/reference/host/tensor_fill.h @@ -92,6 +92,25 @@ struct TensorFillFunc { } }; +/// Returns a pair of values of the Gaussian distribution generated by the Box Muller method +struct BoxMullerFunc { + + BoxMullerFunc() {} + + void operator()( + double* rnd, ///< Size-2 vector to be filled with random values + double mean = 0, ///< Mean of the Gaussian distribution + double stddev = 1, ///< Standard deviation of the Gaussian distribution + double pi = std::acos(-1)) const { + + double u1 = double(std::rand()) / double(RAND_MAX); + double u2 = double(std::rand()) / double(RAND_MAX); + rnd[0] = std::sqrt(-2 * std::log(u1)) * std::cos(2 * pi * u2); + rnd[1] = std::sqrt(-2 * std::log(u1)) * std::sin(2 * pi * u2); + rnd[0] = mean + stddev * rnd[0]; + rnd[1] = mean + stddev * rnd[1]; + } +}; } // namespace detail /////////////////////////////////////////////////////////////////////////////////////////////////// @@ -205,22 +224,18 @@ struct RandomGaussianFunc > { Element reals[2]; - for (int i = 0; i < 2; ++i) { - // Box-Muller transform to generate random numbers with Normal distribution - double u1 = double(std::rand()) / double(RAND_MAX); - double u2 = double(std::rand()) / double(RAND_MAX); + double rnd[2]; + detail::BoxMullerFunc func; + func(rnd, mean, stddev, pi); - // Compute Gaussian random value - double rnd = std::sqrt(-2 * std::log(u1)) * std::cos(2 * pi * u2); - rnd = mean + stddev * rnd; - - if (int_scale >= 0) { - rnd = double(int(rnd * double(1 << int_scale))); - reals[i] = from_real(rnd / double(1 << int_scale)); - } - else { - reals[i] = from_real(rnd); - } + if (int_scale >= 0) { + rnd[0] = double(int(rnd[0] * double(1 << int_scale))); + rnd[1] = double(int(rnd[1] * double(1 << int_scale))); + reals[0] = from_real(rnd[0] / double(1 << int_scale)); + reals[1] = from_real(rnd[1] / double(1 << int_scale)); + } else { + reals[0] = from_real(rnd[0]); + reals[1] = from_real(rnd[1]); } return complex(reals[0], reals[1]); @@ -255,22 +270,27 @@ struct RandomGaussianFunc > { Element reals[4]; - for (int i = 0; i < 4; ++i) { - // Box-Muller transform to generate random numbers with Normal distribution - double u1 = double(std::rand()) / double(RAND_MAX); - double u2 = double(std::rand()) / double(RAND_MAX); + double rnd1[2]; + double rnd2[2]; + detail::BoxMullerFunc func; + func(rnd1, mean, stddev, pi); + func(rnd2, mean, stddev, pi); - // Compute Gaussian random value - double rnd = std::sqrt(-2 * std::log(u1)) * std::cos(2 * pi * u2); - rnd = mean + stddev * rnd; + if (int_scale >= 0) { + rnd1[0] = double(int(rnd1[0] * double(1 << int_scale))); + rnd1[1] = double(int(rnd1[1] * double(1 << int_scale))); + rnd2[0] = double(int(rnd2[0] * double(1 << int_scale))); + rnd2[1] = double(int(rnd2[1] * double(1 << int_scale))); - if (int_scale >= 0) { - rnd = double(int(rnd * double(1 << int_scale))); - reals[i] = from_real(rnd / double(1 << int_scale)); - } - else { - reals[i] = from_real(rnd); - } + reals[0] = from_real(rnd1[0] / double(1 << int_scale)); + reals[1] = from_real(rnd1[1] / double(1 << int_scale)); + reals[2] = from_real(rnd2[0] / double(1 << int_scale)); + reals[3] = from_real(rnd2[1] / double(1 << int_scale)); + } else { + reals[0] = from_real(rnd1[0]); + reals[1] = from_real(rnd1[1]); + reals[2] = from_real(rnd2[0]); + reals[3] = from_real(rnd2[1]); } return Quaternion(reals[0], reals[1], reals[2], reals[3]); @@ -311,7 +331,7 @@ struct TensorFillGaussianFunc { } }; -/// Computes a random Gaussian distribution +/// Computes a random Gaussian distribution for a rank-2 tensor template < typename Element, ///< Element type typename Layout> ///< Layout function @@ -404,7 +424,7 @@ void TensorFillRandomGaussian( } /////////////////////////////////////////////////////////////////////////////////////////////////// -/// Fills a tensor with random values with a Gaussian distribution. +/// Fills the upper or lower part of a symmetric rank-2 tensor with random values of a Gaussian distribution. template < typename Element, ///< Element type typename Layout> ///< Layout function @@ -434,7 +454,7 @@ void TensorFillSymmetricRandomGaussian( /////////////////////////////////////////////////////////////////////////////////////////////////// -/// Fills a tensor with random values with a Gaussian distribution. +/// Fills a tensor with random values of a Gaussian distribution. template < typename Element ///< Element type > @@ -614,7 +634,7 @@ struct RandomUniformFunc > { } }; -/// Computes a random Gaussian distribution +/// Computes a random uniform distribution template < typename Element, ///< Element type typename Layout> ///< Layout function @@ -633,7 +653,7 @@ struct TensorFillRandomUniformFunc { // Methods // - /// Construction of Gaussian RNG functor. + /// Construction of uniform RNG functor. TensorFillRandomUniformFunc( TensorView view_ = TensorView(), RandomUniformFunc func_ = RandomUniformFunc() @@ -649,7 +669,7 @@ struct TensorFillRandomUniformFunc { } }; -/// Computes a random Gaussian distribution +/// Fills the upper or lower part of a symmetric rank-2 tensor with random values of a uniform distribution. template < typename Element, ///< Element type typename Layout> ///< Layout function @@ -669,7 +689,7 @@ struct TensorFillSymmetricRandomUniformFunc { // Methods // - /// Construction of Gaussian RNG functor. + /// Construction of uniform RNG functor. TensorFillSymmetricRandomUniformFunc( TensorView view_ = TensorView(), RandomUniformFunc func_ = RandomUniformFunc(), @@ -715,7 +735,7 @@ struct TensorFillPadDiagonalRandomUniformFunc { // Methods // - /// Construction of Gaussian RNG functor. + /// Construction of uniform RNG functor. TensorFillPadDiagonalRandomUniformFunc( TensorView view_ = TensorView(), RandomUniformFunc func_ = RandomUniformFunc(), @@ -747,7 +767,7 @@ struct TensorFillPadDiagonalRandomUniformFunc { /////////////////////////////////////////////////////////////////////////////////////////////////// -/// Fills a tensor with random values with a uniform random distribution. +/// Fills a tensor with random values of a uniform random distribution. template < typename Element, ///< Element type typename Layout> ///< Layout function @@ -772,7 +792,7 @@ void TensorFillRandomUniform( ); } -/// Fills a tensor with random values with a uniform random distribution. +/// Fills a tensor with random values of a uniform random distribution. template < typename Element, ///< Element type typename Layout> ///< Layout function