• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_TEST_UTIL_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_TEST_UTIL_H_
17 
18 #include <algorithm>
19 #include <functional>
20 #include <iterator>
21 #include <limits>
22 #include <random>
23 #include <vector>
24 
25 #include "tensorflow/lite/kernels/internal/types.h"
26 
27 namespace tflite {
28 
29 // Computes output and padding dimensions.
30 bool ComputeConvSizes(const RuntimeShape& input_shape, int output_depth,
31                       int filter_width, int filter_height, int stride,
32                       int dilation_width_factor, int dilation_height_factor,
33                       PaddingType padding_type, RuntimeShape* output_shape,
34                       int* pad_width, int* pad_height);
35 
36 // Returns a mt19937 random engine.
37 std::mt19937& RandomEngine();
38 
39 // Returns a random integer uniformly distributed between |min| and |max|.
40 int UniformRandomInt(int min, int max);
41 
42 // Returns a random float uniformly distributed between |min| and |max|.
43 float UniformRandomFloat(float min, float max);
44 
45 // Returns a random element in |v|.
46 template <typename T>
RandomElement(const std::vector<T> & v)47 const T& RandomElement(const std::vector<T>& v) {
48   return v[UniformRandomInt(0, v.size() - 1)];
49 }
50 
51 // Returns a random exponentially distributed integer.
52 int ExponentialRandomPositiveInt(float percentile, int percentile_val,
53                                  int max_val);
54 
55 // Returns a random exponentially distributed float.
56 float ExponentialRandomPositiveFloat(float percentile, float percentile_val,
57                                      float max_val);
58 
59 // Fills a vector with random floats between |min| and |max|.
60 void FillRandom(std::vector<float>* vec, float min, float max);
61 
62 template <typename T>
FillRandom(typename std::vector<T>::iterator begin_it,typename std::vector<T>::iterator end_it,T min,T max)63 void FillRandom(typename std::vector<T>::iterator begin_it,
64                 typename std::vector<T>::iterator end_it, T min, T max) {
65   // Workaround for compilers that don't support (u)int8_t uniform_distribution.
66   typedef typename std::conditional<sizeof(T) >= sizeof(int16_t), T,
67                                     std::int16_t>::type rand_type;
68   std::uniform_int_distribution<rand_type> dist(min, max);
69   // TODO(b/154540105): use std::ref to avoid copying the random engine.
70   auto gen = std::bind(dist, RandomEngine());
71   std::generate(begin_it, end_it, [&gen] { return static_cast<T>(gen()); });
72 }
73 
74 // Fills a vector with random numbers between |min| and |max|.
75 template <typename T>
FillRandom(std::vector<T> * vec,T min,T max)76 void FillRandom(std::vector<T>* vec, T min, T max) {
77   return FillRandom(std::begin(*vec), std::end(*vec), min, max);
78 }
79 
80 // Fills a vector with random numbers.
81 template <typename T>
FillRandom(std::vector<T> * vec)82 void FillRandom(std::vector<T>* vec) {
83   FillRandom(vec, std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
84 }
85 
86 // Fill with a "skyscraper" pattern, in which there is a central section (across
87 // the depth) with higher values than the surround.
88 template <typename T>
FillRandomSkyscraper(std::vector<T> * vec,int depth,double middle_proportion,uint8 middle_min,uint8 sides_max)89 void FillRandomSkyscraper(std::vector<T>* vec, int depth,
90                           double middle_proportion, uint8 middle_min,
91                           uint8 sides_max) {
92   for (auto base_it = std::begin(*vec); base_it != std::end(*vec);
93        base_it += depth) {
94     auto left_it = base_it + std::ceil(0.5 * depth * (1.0 - middle_proportion));
95     auto right_it =
96         base_it + std::ceil(0.5 * depth * (1.0 + middle_proportion));
97     FillRandom(base_it, left_it, std::numeric_limits<T>::min(), sides_max);
98     FillRandom(left_it, right_it, middle_min, std::numeric_limits<T>::max());
99     FillRandom(right_it, base_it + depth, std::numeric_limits<T>::min(),
100                sides_max);
101   }
102 }
103 
104 }  // namespace tflite
105 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_TEST_UTIL_H_
106