summaryrefslogtreecommitdiff
path: root/runtime/libs/misc/src/RandomGenerator.cpp
blob: e7fbc10caa7bfe0920f462abef1ac021249bd300 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "misc/RandomGenerator.h"

namespace nnfw
{
namespace misc
{

template <> uint8_t RandomGenerator::generate<uint8_t>(void)
{
  // The value of type_range is 255.
  float type_range = static_cast<float>(std::numeric_limits<uint8_t>::max()) -
                     static_cast<float>(std::numeric_limits<uint8_t>::min());
  // Most _dist values range from -5.0 to 5.0.
  float min_range = -5.0f;
  float max_range = 5.0f;
  // NOTE shifted_relative_val has Gaussian distribution that origin mean was 0 and standard
  // deviation was 2. And then its values are distributed and shift to that mean is 127.5 and range
  // is about [0, 255].
  float shifted_relative_val = (_dist(_rand) - min_range) * type_range / (max_range - min_range);

  // shifted_relative_val is adjusted to be mapped to end points of the range, if it is out of range
  // values.
  if (shifted_relative_val < 0.0f)
  {
    return 0;
  }
  else if (shifted_relative_val > type_range)
  {
    return 255;
  }

  // Convert shifted_relative_val from float to uint8
  return static_cast<uint8_t>(shifted_relative_val);
}

template <> bool RandomGenerator::generate<bool>(void)
{
  std::uniform_int_distribution<> dist(0, 1); // [0, 1]
  return dist(_rand);
}

template <> int32_t RandomGenerator::generate<int32_t>(void)
{
  // Instead of INT_MAX, 99 is chosen because int32_t input does not mean
  // that the model can have any value in int32_t can hold.
  // For example, one_hot operation gets indices as int32_t tensor.
  // However, we usually expect it would hold a value in [0..depth).
  // In our given model, depth was 10137.
  const int int32_random_max = 99;
  std::uniform_int_distribution<> dist(0, int32_random_max);
  return dist(_rand);
}

template <> int64_t RandomGenerator::generate<int64_t>(void)
{
  // Instead of INT_MAX, 99 is chosen because int64_t input does not mean
  // that the model can have any value in int64_t can hold.
  // For example, one_hot operation gets indices as int64_t tensor.
  // However, we usually expect it would hold a value in [0..depth).
  // In our given model, depth was 10137.
  const int64_t int64_random_max = 99;
  std::uniform_int_distribution<> dist(0, int64_random_max);
  return dist(_rand);
}

} // namespace misc
} // namespace nnfw