blob: f3216fbaf990a95f1b9f2602a9942f22216f0f0a [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_REFERENCE_REFERENCE_H__
25#define __ARM_COMPUTE_TEST_REFERENCE_REFERENCE_H__
26
27#include "RawTensor.h"
28#include "Types.h"
Giorgio Arena2ca209e2017-06-13 15:49:37 +010029#include "arm_compute/runtime/Array.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030
Isabella Gottardib797fa22017-06-23 15:02:11 +010031#include <map>
Georgios Pinitasd4f8c272017-06-30 16:16:19 +010032#include <vector>
33
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034namespace arm_compute
35{
36namespace test
37{
38namespace validation
39{
40/** Interface for reference implementations. */
41class Reference
42{
43public:
Giorgio Arena50f9fd72017-06-19 17:05:30 +010044 /** Compute reference sobel 3x3.
45 *
46 * @param[in] shape Shape of the input and output tensors.
47 * @param[in] border_mode Border mode to use for input tensor
48 * @param[in] constant_border_value Constant value to use if @p border_mode is constant
49 *
50 * @return Computed raw tensors along x and y axis.
51 */
52 static std::pair<RawTensor, RawTensor> compute_reference_sobel_3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
53 /** Compute reference sobel 5x5.
54 *
55 * @param[in] shape Shape of the input and output tensors.
56 * @param[in] border_mode Border mode to use for input tensor
57 * @param[in] constant_border_value Constant value to use if @p border_mode is constant
58 *
59 * @return Computed raw tensors along x and y axis.
60 */
61 static std::pair<RawTensor, RawTensor> compute_reference_sobel_5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
Giorgio Arena2ca209e2017-06-13 15:49:37 +010062 /** Compute min max location.
63 *
64 * @param[in] shape Shape of the input tensors.
65 * @param[in] dt_in Data type of input tensor.
66 * @param[out] min Minimum value of tensor
67 * @param[out] max Maximum value of tensor
68 * @param[out] min_loc Array with locations of minimum values
69 * @param[out] max_loc Array with locations of maximum values
70 * @param[out] min_count Number of minimum values found
71 * @param[out] max_count Number of maximum values found
72 *
73 * @return Computed minimum, maximum values and their locations.
74 */
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +010075 static void compute_reference_min_max_location(const TensorShape &shape, DataType dt_in, void *min, void *max, IArray<Coordinates2D> &min_loc, IArray<Coordinates2D> &max_loc,
Giorgio Arena935deee2017-06-14 13:40:36 +010076 uint32_t &min_count,
Giorgio Arena2ca209e2017-06-13 15:49:37 +010077 uint32_t &max_count);
Giorgio Arenaf7959862017-06-13 15:19:51 +010078 /** Compute reference mean and standard deviation.
79 *
80 * @param[in] shape Shape of the input tensors.
81 *
82 * @return Computed mean and standard deviation.
83 */
84 static std::pair<float, float> compute_reference_mean_and_standard_deviation(const TensorShape &shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010085 /** Compute reference integral image.
86 *
87 * @param[in] shape Shape of the input and output tensors.
88 *
89 * @return Computed raw tensor.
90 */
91 static RawTensor compute_reference_integral_image(const TensorShape &shape);
92 /** Compute reference absolute difference.
93 *
94 * @param[in] shape Shape of the input and output tensors.
95 * @param[in] dt_in0 Data type of first input tensor.
96 * @param[in] dt_in1 Data type of second input tensor.
97 * @param[in] dt_out Data type of the output tensor.
98 *
99 * @return Computed raw tensor.
100 */
101 static RawTensor compute_reference_absolute_difference(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out);
102 /** Compute reference accumulate.
103 *
104 * @param[in] shape Shape of the input and output tensors.
105 *
106 * @return Computed raw tensor.
107 */
108 static RawTensor compute_reference_accumulate(const TensorShape &shape);
109 /** Compute reference accumulate.
110 *
111 * @param[in] shape Shape of the input and output tensors.
112 * @param[in] shift A uint32_t value within the range of [0, 15]
113 *
114 * @return Computed raw tensor.
115 */
116 static RawTensor compute_reference_accumulate_squared(const TensorShape &shape, uint32_t shift);
117 /** Compute reference accumulate.
118 *
119 * @param[in] shape Shape of the input and output tensors.
120 * @param[in] alpha A float value within the range of [0, 1]
121 *
122 * @return Computed raw tensor.
123 */
124 static RawTensor compute_reference_accumulate_weighted(const TensorShape &shape, float alpha);
125 /** Compute reference arithmetic addition.
126 *
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100127 * @param[in] shape Shape of the input and output tensors.
128 * @param[in] dt_in0 Data type of first input tensor.
129 * @param[in] dt_in1 Data type of second input tensor.
130 * @param[in] dt_out Data type of the output tensor.
131 * @param[in] convert_policy Overflow policy of the operation.
132 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 *
134 * @return Computed raw tensor.
135 */
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100136 static RawTensor compute_reference_arithmetic_addition(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, ConvertPolicy convert_policy, int fixed_point_position = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 /** Compute reference arithmetic subtraction.
138 *
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100139 * @param[in] shape Shape of the input and output tensors.
140 * @param[in] dt_in0 Data type of first input tensor.
141 * @param[in] dt_in1 Data type of second input tensor.
142 * @param[in] dt_out Data type of the output tensor.
143 * @param[in] convert_policy Overflow policy of the operation.
144 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 *
146 * @return Computed raw tensor.
147 */
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100148 static RawTensor compute_reference_arithmetic_subtraction(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, ConvertPolicy convert_policy, int fixed_point_position = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 /** Compute reference bitwise and.
150 *
151 * @param[in] shape Shape of the input and output tensors.
152 *
153 * @return Computed raw tensor.
154 */
155 static RawTensor compute_reference_bitwise_and(const TensorShape &shape);
156 /** Compute reference bitwise or.
157 *
158 * @param[in] shape Shape of the input and output tensors.
159 *
160 * @return Computed raw tensor.
161 */
162 static RawTensor compute_reference_bitwise_or(const TensorShape &shape);
163 /** Compute reference bitwise xor.
164 *
165 * @param[in] shape Shape of the input and output tensors.
166 *
167 * @return Computed raw tensor.
168 */
169 static RawTensor compute_reference_bitwise_xor(const TensorShape &shape);
170 /** Compute reference bitwise not.
171 *
172 * @param[in] shape Shape of the input and output tensors.
173 *
174 * @return Computed raw tensor.
175 */
176 static RawTensor compute_reference_bitwise_not(const TensorShape &shape);
SiCong Libacaf9a2017-06-19 13:41:45 +0100177 /** Compute reference box3x3 filter.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100179 * @param[in] shape Shape of the input and output tensors.
180 * @param[in] border_mode BorderMode used by the input tensor.
181 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 *
183 * @return Computed raw tensor.
184 */
SiCong Libacaf9a2017-06-19 13:41:45 +0100185 static RawTensor compute_reference_box3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186 /** Compute reference depth convert.
187 *
Georgios Pinitase2229412017-07-12 12:30:40 +0100188 * @param[in] shape Shape of the input and output tensors.
189 * @param[in] dt_in Data type of input tensor.
190 * @param[in] dt_out Data type of the output tensor.
191 * @param[in] policy Overflow policy of the operation.
192 * @param[in] shift Value for down/up conversions. Must be 0 <= shift < 8.
193 * @param[in] fixed_point_position_in (Optional) Fixed point position for the input tensor.
194 * @param[in] fixed_point_position_out (Optional) Fixed point position for the output tensor.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 *
196 * @return Computed raw tensor.
197 */
Georgios Pinitase2229412017-07-12 12:30:40 +0100198 static RawTensor compute_reference_depth_convert(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy,
199 uint32_t shift, uint32_t fixed_point_position_in = 0, uint32_t fixed_point_position_out = 0);
SiCong Li5a536642017-06-19 14:47:05 +0100200 /** Compute reference gaussian3x3 filter.
201 *
202 * @param[in] shape Shape of the input and output tensors.
203 * @param[in] border_mode BorderMode used by the input tensor
204 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT
205 *
206 * @return Computed raw tensor.
207 */
208 static RawTensor compute_reference_gaussian3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
SiCong Li3eb263e2017-06-19 15:31:43 +0100209 /** Compute reference gaussian5x5 filter.
210 *
211 * @param[in] shape Shape of the input and output tensors.
212 * @param[in] border_mode BorderMode used by the input tensor.
213 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT.
214 *
215 * @return Computed raw tensor.
216 */
217 static RawTensor compute_reference_gaussian5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218 /** Compute matrix multiply function.
219 *
220 * @param[in] src_shape1 First input tensor shape
221 * @param[in] src_shape2 Second input tensor shape
222 * @param[in] src_shape3 Third input tensor shape
223 * @param[out] dst_shape Output tensor.
224 * @param[in] alpha Weight of the matrix product
225 * @param[in] beta Weight of the third matrix
226 * @param[in] dt Tensor's data type
227 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
228 *
229 * @return Computed output tensor.
230 */
231 static RawTensor compute_reference_gemm(const TensorShape &src_shape1, const TensorShape &src_shape2, const TensorShape &src_shape3,
232 const TensorShape &dst_shape, float alpha, float beta, DataType dt, int fixed_point_position = 0);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100233 /** Compute reference non linear filter function
234 *
235 * @param[in] shape Shape of the input and output tensors.Data type supported: U8
236 * @param[in] function Non linear function to perform
237 * @param[in] mask_size Mask size. Supported sizes: 3, 5
238 * @param[in] pattern Matrix pattern
239 * @param[in] mask The given mask. Will be used only if pattern is specified to PATTERN_OTHER
240 * @param[in] border_mode Strategy to use for borders.
241 * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT.
242 *
243 * @return Computed raw tensor.
244 */
245 static RawTensor compute_reference_non_linear_filter(const TensorShape &shape, NonLinearFilterFunction function, unsigned int mask_size,
246 MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100247 /** Compute reference pixel-wise multiplication
248 *
249 * @param[in] shape Shape of the input and output tensors.
250 * @param[in] dt_in0 Data type of first input tensor.
251 * @param[in] dt_in1 Data type of second input tensor.
252 * @param[in] dt_out Data type of the output tensor.
253 * @param[in] scale Non-negative scale.
254 * @param[in] convert_policy Overflow policy of the operation.
255 * @param[in] rounding_policy Rounding policy of the operation.
256 *
257 * @return Computed raw tensor.
258 */
259 static RawTensor compute_reference_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, ConvertPolicy convert_policy,
260 RoundingPolicy rounding_policy);
261 /** Compute reference pixel-wise multiplication.
262 *
263 * @param[in] shape Shape of the input and output tensors.
264 * @param[in] dt_in0 Data type of first input tensor.
265 * @param[in] dt_in1 Data type of second input tensor.
266 * @param[in] dt_out Data type of the output tensor.
267 * @param[in] scale Scale to apply after multiplication. Must be positive.
268 * @param[in] fixed_point_position Fixed point position that expresses the number of bits for the fractional part of the number.
269 * @param[in] convert_policy Overflow policy of the operation.
270 * @param[in] rounding_policy Rounding policy of the operation.
271 *
272 * @return Computed raw tensor.
273 */
274 static RawTensor compute_reference_fixed_point_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, int fixed_point_position,
275 ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
Isabella Gottardib797fa22017-06-23 15:02:11 +0100276 /** Compute reference Table Lookup.
277 *
278 * @param[in] shape Shape of the input and output tensors.
279 * @param[in] dt_inout Data type of input/output tensor.
280 * @param[in] lut Input lookup table.
281 *
282 * @return Computed raw tensor.
283 */
284 template <typename T>
285 static RawTensor compute_reference_table_lookup(const TensorShape &shape, DataType dt_inout, std::map<T, T> &lut);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100286 /** Compute reference threshold.
287 *
288 * @param[in] shape Shape of the input and output tensors.
289 * @param[in] threshold Threshold. When the threshold type is RANGE, this is used as the lower threshold.
290 * @param[in] false_value value to set when the condition is not respected.
291 * @param[in] true_value value to set when the condition is respected.
292 * @param[in] type Thresholding type. Either RANGE or BINARY.
293 * @param[in] upper Upper threshold. Only used when the thresholding type is RANGE.
294 *
295 * @return Computed raw tensor.
296 */
297 static RawTensor compute_reference_threshold(const TensorShape &shape, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100298 /** Compute reference batch normalization layer.
299 *
300 * @param[in] shape0 Shape of the input and output tensors.
301 * @param[in] shape1 Shape of the vector tensors.
302 * @param[in] dt Data type of all input and output tensors.
303 * @param[in] epsilon Small value to avoid division with zero.
304 * @param[in] fixed_point_position Fixed point position.
305 *
306 * @return Computed raw tensor.
307 */
308 static RawTensor compute_reference_batch_normalization_layer(const TensorShape &shape0, const TensorShape &shape1, DataType dt, float epsilon, int fixed_point_position = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309 /** Compute reference for fully connected layer function
310 *
311 * @param[in] input_shape Shape for the input tensor
312 * @param[in] weights_shape Shape for the weights tensor
313 * @param[in] bias_shape Shape for the bias tensor
314 * @param[in] output_shape Shape for the output tensor
315 * @param[in] dt Data type to use
316 * @param[in] transpose_weights Transpose the weights if true
317 * @param[in] fixed_point_position Number of bits for the fractional part of the fixed point numbers
318 *
319 * @return Computed raw tensor.
320 */
321 static RawTensor compute_reference_fully_connected_layer(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, DataType dt,
322 bool transpose_weights, int fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100323 /** Compute reference pooling layer.
324 *
325 * @param[in] shape_in Shape of the input tensor.
326 * @param[in] shape_out Shape of the output tensor.
327 * @param[in] dt Data type of input and output tensors.
328 * @param[in] pool_info Pooling Layer information.
329 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers.
330 *
331 * @return Computed raw tensor.
332 */
333 static RawTensor compute_reference_pooling_layer(const TensorShape &shape_in, const TensorShape &shape_out, DataType dt, PoolingLayerInfo pool_info, int fixed_point_position = 0);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100334 /** Compute reference roi pooling layer.
335 *
336 * @param[in] shape Shape of the input tensor.
337 * @param[in] dt Data type of input and output tensors.
338 * @param[in] rois Region of interest vector.
339 * @param[in] pool_info ROI Pooling Layer information.
340 */
341 static RawTensor compute_reference_roi_pooling_layer(const TensorShape &shape, DataType dt, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100342 /** Compute reference fixed point operation.
343 *
344 * @param[in] shape Shape of the input and output tensors.
345 * @param[in] dt_in Data type of the input tensor.
346 * @param[in] dt_out Data type of the output tensor.
347 * @param[in] op Fixed point operation to perform.
348 * @param[in] fixed_point_position Number of bits for the fractional part of the fixed point numbers
349 *
350 * @return Computed raw tensor.
351 */
352 static RawTensor compute_reference_fixed_point_operation(const TensorShape &shape, DataType dt_in, DataType dt_out, FixedPointOp op, int fixed_point_position);
353
354protected:
355 Reference() = default;
356 ~Reference() = default;
357};
358} // namespace validation
359} // namespace test
360} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100361#endif /* __ARM_COMPUTE_TEST_REFERENCE_REFERENCE_H__ */