Compute Library
 23.11
Random.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_MISC_RANDOM_H
25 #define ARM_COMPUTE_MISC_RANDOM_H
26 
27 #include "arm_compute/core/Error.h"
28 
29 #include "utils/Utils.h"
30 
31 #include <random>
32 #include <type_traits>
33 
34 namespace arm_compute
35 {
36 namespace utils
37 {
38 namespace random
39 {
40 /** Uniform distribution within a given number of sub-ranges
41  *
42  * @tparam T Distribution primitive type
43  */
44 template <typename T>
46 {
47 public:
48  static constexpr bool is_fp_16bit = std::is_same<T, half>::value || std::is_same<T, bfloat16>::value;
49  static constexpr bool is_integral = std::is_integral<T>::value && !is_fp_16bit;
50 
51  using fp_dist = typename std::conditional<is_fp_16bit,
53  std::uniform_real_distribution<T>>::type;
54  using DT = typename std::conditional<is_integral, std::uniform_int_distribution<T>, fp_dist>::type;
55  using result_type = T;
56  using range_pair = std::pair<result_type, result_type>;
57 
58  /** Constructor
59  *
60  * @param[in] low lowest value in the range (inclusive)
61  * @param[in] high highest value in the range (inclusive for uniform_int_distribution, exclusive for uniform_real_distribution)
62  * @param[in] exclude_ranges Ranges to exclude from the generator
63  */
64  RangedUniformDistribution(result_type low, result_type high, const std::vector<range_pair> &exclude_ranges)
65  : _distributions(), _selector()
66  {
67  result_type clow = low;
68  for (const auto &erange : exclude_ranges)
69  {
71 
72  ARM_COMPUTE_ERROR_ON(clow > erange.first || clow >= erange.second);
73 
74  _distributions.emplace_back(DT(clow, erange.first - epsilon));
75  clow = erange.second + epsilon;
76  }
77  ARM_COMPUTE_ERROR_ON(clow > high);
78  _distributions.emplace_back(DT(clow, high));
79  _selector = std::uniform_int_distribution<uint32_t>(0, _distributions.size() - 1);
80  }
81  /** Generate random number
82  *
83  * @tparam URNG Random number generator object type
84  *
85  * @param[in] g A uniform random number generator object, used as the source of randomness.
86  *
87  * @return A new random number.
88  */
89  template <class URNG>
91  {
92  unsigned int rand_select = _selector(g);
93  return _distributions[rand_select](g);
94  }
95 
96 private:
97  std::vector<DT> _distributions;
98  std::uniform_int_distribution<uint32_t> _selector;
99 };
100 } // namespace random
101 } // namespace utils
102 } // namespace arm_compute
103 #endif /* ARM_COMPUTE_MISC_RANDOM_H */
arm_compute::utils::random::RangedUniformDistribution::RangedUniformDistribution
RangedUniformDistribution(result_type low, result_type high, const std::vector< range_pair > &exclude_ranges)
Constructor.
Definition: Random.h:64
type
decltype(strategy::transforms) typedef type
Definition: gemm_interleaved.hpp:261
arm_compute::utils::random::RangedUniformDistribution::result_type
T result_type
Definition: Random.h:55
arm_compute::utils::random::RangedUniformDistribution::range_pair
std::pair< result_type, result_type > range_pair
Definition: Random.h:56
arm_compute::utils::random::RangedUniformDistribution::DT
typename std::conditional< is_integral, std::uniform_int_distribution< T >, fp_dist >::type DT
Definition: Random.h:54
Error.h
arm_compute::utils::random::RangedUniformDistribution
Uniform distribution within a given number of sub-ranges.
Definition: Random.h:45
ARM_COMPUTE_ERROR_ON
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
arm_compute::utils::random::RangedUniformDistribution::operator()
result_type operator()(URNG &g)
Generate random number.
Definition: Random.h:90
arm_compute::utils::random::RangedUniformDistribution::is_fp_16bit
static constexpr bool is_fp_16bit
Definition: Random.h:48
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::utils::random::RangedUniformDistribution::fp_dist
typename std::conditional< is_fp_16bit, arm_compute::utils::uniform_real_distribution_16bit< T >, std::uniform_real_distribution< T > >::type fp_dist
Definition: Random.h:53
arm_compute::utils::random::RangedUniformDistribution::is_integral
static constexpr bool is_integral
Definition: Random.h:49
arm_compute::quantization::epsilon
constexpr float epsilon
Definition: AsymmHelpers.cpp:41
arm_compute::utils::uniform_real_distribution_16bit
Specialized class to generate random non-zero FP16 values.
Definition: Utils.h:255