2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "misc/RandomGenerator.h"
24 template <> uint8_t RandomGenerator::generate<uint8_t>(void)
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
35 float shifted_relative_val = (_dist(_rand) - min_range) * type_range / (max_range - min_range);
37 // shifted_relative_val is adjusted to be mapped to end points of the range, if it is out of range
39 if (shifted_relative_val < 0.0f)
43 else if (shifted_relative_val > type_range)
48 // Convert shifted_relative_val from float to uint8
49 return static_cast<uint8_t>(shifted_relative_val);
52 template <> bool RandomGenerator::generate<bool>(void)
54 std::uniform_int_distribution<> dist(0, 1); // [0, 1]
58 template <> int32_t RandomGenerator::generate<int32_t>(void)
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);
70 template <> int64_t RandomGenerator::generate<int64_t>(void)
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);