blob: c0c5865e568aca3bd5cec176ec0f13f191ac5efc [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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_TEST_VALIDATION_HELPERS_H__
25#define __ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__
26
27#include "Types.h"
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010028#include "ValidationUserConfiguration.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029
30#include <type_traits>
31#include <utility>
32
33namespace arm_compute
34{
35namespace test
36{
37namespace validation
38{
39/** Helper function to get the testing range for each activation layer.
40 *
41 * @param[in] activation Activation function to test.
42 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1.
43 *
44 * @return A pair containing the lower upper testing bounds for a given function.
45 */
46template <typename T>
47std::pair<T, T> get_activation_layer_test_bounds(ActivationLayerInfo::ActivationFunction activation, int fixed_point_position = 1)
48{
49 bool is_float = std::is_floating_point<T>::value;
50 std::pair<T, T> bounds;
51
52 // Set initial values
53 if(is_float)
54 {
55 bounds = std::make_pair(-255.f, 255.f);
56 }
57 else
58 {
59 bounds = std::make_pair(std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max());
60 }
61
62 // Reduce testing ranges
63 switch(activation)
64 {
65 case ActivationLayerInfo::ActivationFunction::LOGISTIC:
66 case ActivationLayerInfo::ActivationFunction::SOFT_RELU:
67 // Reduce range as exponent overflows
68 if(is_float)
69 {
70 bounds.first = -40.f;
71 bounds.second = 40.f;
72 }
73 else
74 {
75 bounds.first = -(1 << (fixed_point_position));
76 bounds.second = 1 << (fixed_point_position);
77 }
78 break;
79 case ActivationLayerInfo::ActivationFunction::TANH:
80 // Reduce range as exponent overflows
81 if(!is_float)
82 {
83 bounds.first = -(1 << (fixed_point_position));
84 bounds.second = 1 << (fixed_point_position);
85 }
86 break;
87 case ActivationLayerInfo::ActivationFunction::SQRT:
88 // Reduce range as sqrt should take a non-negative number
89 bounds.first = (is_float) ? 0 : 1 << (fixed_point_position);
90 break;
91 default:
92 break;
93 }
94 return bounds;
95}
96
97/** Helper function to get the testing range for batch normalization layer.
98 *
99 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part. Defaults to 1.
100 *
101 * @return A pair containing the lower upper testing bounds.
102 */
103template <typename T>
104std::pair<T, T> get_batchnormalization_layer_test_bounds(int fixed_point_position = 1)
105{
106 bool is_float = std::is_floating_point<T>::value;
107 std::pair<T, T> bounds;
108
109 // Set initial values
110 if(is_float)
111 {
112 bounds = std::make_pair(-1.f, 1.f);
113 }
114 else
115 {
116 bounds = std::make_pair(1, 1 << (fixed_point_position));
117 }
118
119 return bounds;
120}
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100121
122/** Fill mask with the corresponding given pattern.
123 *
124 * @param[in,out] mask Mask to be filled according to pattern
125 * @param[in] cols Columns (width) of mask
126 * @param[in] rows Rows (height) of mask
127 * @param[in] pattern Pattern to fill the mask according to
128 */
129inline void fill_mask_from_pattern(uint8_t *mask, int cols, int rows, MatrixPattern pattern)
130{
131 unsigned int v = 0;
132 std::mt19937 gen(user_config.seed.get());
133 std::bernoulli_distribution dist(0.5);
134
135 for(int r = 0; r < rows; ++r)
136 {
137 for(int c = 0; c < cols; ++c, ++v)
138 {
139 uint8_t val = 0;
140
141 switch(pattern)
142 {
143 case MatrixPattern::BOX:
144 val = 255;
145 break;
146 case MatrixPattern::CROSS:
147 val = ((r == (rows / 2)) || (c == (cols / 2))) ? 255 : 0;
148 break;
149 case MatrixPattern::DISK:
150 val = (((r - rows / 2.0f + 0.5f) * (r - rows / 2.0f + 0.5f)) / ((rows / 2.0f) * (rows / 2.0f)) + ((c - cols / 2.0f + 0.5f) * (c - cols / 2.0f + 0.5f)) / ((cols / 2.0f) *
151 (cols / 2.0f))) <= 1.0f ? 255 : 0;
152 break;
153 case MatrixPattern::OTHER:
154 val = (dist(gen) ? 0 : 255);
155 break;
156 default:
157 return;
158 }
159
160 mask[v] = val;
161 }
162 }
163
164 if(pattern == MatrixPattern::OTHER)
165 {
166 std::uniform_int_distribution<uint8_t> distribution_u8(0, ((cols * rows) - 1));
167 mask[distribution_u8(gen)] = 255;
168 }
169}
170
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171} // namespace validation
172} // namespace test
173} // namespace arm_compute
174#endif //__ARM_COMPUTE_TEST_VALIDATION_HELPERS_H__