Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / libs / misc / src / RandomGenerator.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "misc/RandomGenerator.h"
18
19 namespace nnfw
20 {
21 namespace misc
22 {
23
24 template <> uint8_t RandomGenerator::generate<uint8_t>(void)
25 {
26   // The value of type_range is 255.
27   float type_range = static_cast<float>(std::numeric_limits<uint8_t>::max()) -
28                      static_cast<float>(std::numeric_limits<uint8_t>::min());
29   // Most _dist values range from -5.0 to 5.0.
30   float min_range = -5.0f;
31   float max_range = 5.0f;
32   // NOTE shifted_relative_val has Gaussian distribution that origin mean was 0 and standard
33   // deviation was 2. And then its values are distributed and shift to that mean is 127.5 and range
34   // is about [0, 255].
35   float shifted_relative_val = (_dist(_rand) - min_range) * type_range / (max_range - min_range);
36
37   // shifted_relative_val is adjusted to be mapped to end points of the range, if it is out of range
38   // values.
39   if (shifted_relative_val < 0.0f)
40   {
41     return 0;
42   }
43   else if (shifted_relative_val > type_range)
44   {
45     return 255;
46   }
47
48   // Convert shifted_relative_val from float to uint8
49   return static_cast<uint8_t>(shifted_relative_val);
50 }
51
52 template <> bool RandomGenerator::generate<bool>(void)
53 {
54   std::uniform_int_distribution<> dist(0, 1); // [0, 1]
55   return dist(_rand);
56 }
57
58 template <> int32_t RandomGenerator::generate<int32_t>(void)
59 {
60   // Instead of INT_MAX, 99 is chosen because int32_t input does not mean
61   // that the model can have any value in int32_t can hold.
62   // For example, one_hot operation gets indices as int32_t tensor.
63   // However, we usually expect it would hold a value in [0..depth).
64   // In our given model, depth was 10137.
65   const int int32_random_max = 99;
66   std::uniform_int_distribution<> dist(0, int32_random_max);
67   return dist(_rand);
68 }
69
70 template <> int64_t RandomGenerator::generate<int64_t>(void)
71 {
72   // Instead of INT_MAX, 99 is chosen because int64_t input does not mean
73   // that the model can have any value in int64_t can hold.
74   // For example, one_hot operation gets indices as int64_t tensor.
75   // However, we usually expect it would hold a value in [0..depth).
76   // In our given model, depth was 10137.
77   const int64_t int64_random_max = 99;
78   std::uniform_int_distribution<> dist(0, int64_random_max);
79   return dist(_rand);
80 }
81
82 } // namespace misc
83 } // namespace nnfw