blob: 259afd19cd36b0495105861f369d31b0b891c9cc [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 */
75 static void compute_reference_min_max_location(const TensorShape &shape, DataType dt_in, int32_t &min, int32_t &max, Coordinates2DArray &min_loc, Coordinates2DArray &max_loc, uint32_t &min_count,
76 uint32_t &max_count);
Giorgio Arenaf7959862017-06-13 15:19:51 +010077 /** Compute reference mean and standard deviation.
78 *
79 * @param[in] shape Shape of the input tensors.
80 *
81 * @return Computed mean and standard deviation.
82 */
83 static std::pair<float, float> compute_reference_mean_and_standard_deviation(const TensorShape &shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 /** Compute reference integral image.
85 *
86 * @param[in] shape Shape of the input and output tensors.
87 *
88 * @return Computed raw tensor.
89 */
90 static RawTensor compute_reference_integral_image(const TensorShape &shape);
91 /** Compute reference absolute difference.
92 *
93 * @param[in] shape Shape of the input and output tensors.
94 * @param[in] dt_in0 Data type of first input tensor.
95 * @param[in] dt_in1 Data type of second input tensor.
96 * @param[in] dt_out Data type of the output tensor.
97 *
98 * @return Computed raw tensor.
99 */
100 static RawTensor compute_reference_absolute_difference(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out);
101 /** Compute reference accumulate.
102 *
103 * @param[in] shape Shape of the input and output tensors.
104 *
105 * @return Computed raw tensor.
106 */
107 static RawTensor compute_reference_accumulate(const TensorShape &shape);
108 /** Compute reference accumulate.
109 *
110 * @param[in] shape Shape of the input and output tensors.
111 * @param[in] shift A uint32_t value within the range of [0, 15]
112 *
113 * @return Computed raw tensor.
114 */
115 static RawTensor compute_reference_accumulate_squared(const TensorShape &shape, uint32_t shift);
116 /** Compute reference accumulate.
117 *
118 * @param[in] shape Shape of the input and output tensors.
119 * @param[in] alpha A float value within the range of [0, 1]
120 *
121 * @return Computed raw tensor.
122 */
123 static RawTensor compute_reference_accumulate_weighted(const TensorShape &shape, float alpha);
124 /** Compute reference arithmetic addition.
125 *
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100126 * @param[in] shape Shape of the input and output tensors.
127 * @param[in] dt_in0 Data type of first input tensor.
128 * @param[in] dt_in1 Data type of second input tensor.
129 * @param[in] dt_out Data type of the output tensor.
130 * @param[in] convert_policy Overflow policy of the operation.
131 * @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 +0100132 *
133 * @return Computed raw tensor.
134 */
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100135 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 +0100136 /** Compute reference arithmetic subtraction.
137 *
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100138 * @param[in] shape Shape of the input and output tensors.
139 * @param[in] dt_in0 Data type of first input tensor.
140 * @param[in] dt_in1 Data type of second input tensor.
141 * @param[in] dt_out Data type of the output tensor.
142 * @param[in] convert_policy Overflow policy of the operation.
143 * @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 +0100144 *
145 * @return Computed raw tensor.
146 */
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100147 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 +0100148 /** Compute reference bitwise and.
149 *
150 * @param[in] shape Shape of the input and output tensors.
151 *
152 * @return Computed raw tensor.
153 */
154 static RawTensor compute_reference_bitwise_and(const TensorShape &shape);
155 /** Compute reference bitwise or.
156 *
157 * @param[in] shape Shape of the input and output tensors.
158 *
159 * @return Computed raw tensor.
160 */
161 static RawTensor compute_reference_bitwise_or(const TensorShape &shape);
162 /** Compute reference bitwise xor.
163 *
164 * @param[in] shape Shape of the input and output tensors.
165 *
166 * @return Computed raw tensor.
167 */
168 static RawTensor compute_reference_bitwise_xor(const TensorShape &shape);
169 /** Compute reference bitwise not.
170 *
171 * @param[in] shape Shape of the input and output tensors.
172 *
173 * @return Computed raw tensor.
174 */
175 static RawTensor compute_reference_bitwise_not(const TensorShape &shape);
SiCong Libacaf9a2017-06-19 13:41:45 +0100176 /** Compute reference box3x3 filter.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100178 * @param[in] shape Shape of the input and output tensors.
179 * @param[in] border_mode BorderMode used by the input tensor.
180 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 *
182 * @return Computed raw tensor.
183 */
SiCong Libacaf9a2017-06-19 13:41:45 +0100184 static RawTensor compute_reference_box3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100185 /** Compute reference depth convert.
186 *
Georgios Pinitase2229412017-07-12 12:30:40 +0100187 * @param[in] shape Shape of the input and output tensors.
188 * @param[in] dt_in Data type of input tensor.
189 * @param[in] dt_out Data type of the output tensor.
190 * @param[in] policy Overflow policy of the operation.
191 * @param[in] shift Value for down/up conversions. Must be 0 <= shift < 8.
192 * @param[in] fixed_point_position_in (Optional) Fixed point position for the input tensor.
193 * @param[in] fixed_point_position_out (Optional) Fixed point position for the output tensor.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 *
195 * @return Computed raw tensor.
196 */
Georgios Pinitase2229412017-07-12 12:30:40 +0100197 static RawTensor compute_reference_depth_convert(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy,
198 uint32_t shift, uint32_t fixed_point_position_in = 0, uint32_t fixed_point_position_out = 0);
SiCong Li5a536642017-06-19 14:47:05 +0100199 /** Compute reference gaussian3x3 filter.
200 *
201 * @param[in] shape Shape of the input and output tensors.
202 * @param[in] border_mode BorderMode used by the input tensor
203 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT
204 *
205 * @return Computed raw tensor.
206 */
207 static RawTensor compute_reference_gaussian3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
SiCong Li3eb263e2017-06-19 15:31:43 +0100208 /** Compute reference gaussian5x5 filter.
209 *
210 * @param[in] shape Shape of the input and output tensors.
211 * @param[in] border_mode BorderMode used by the input tensor.
212 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT.
213 *
214 * @return Computed raw tensor.
215 */
216 static RawTensor compute_reference_gaussian5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217 /** Compute matrix multiply function.
218 *
219 * @param[in] src_shape1 First input tensor shape
220 * @param[in] src_shape2 Second input tensor shape
221 * @param[in] src_shape3 Third input tensor shape
222 * @param[out] dst_shape Output tensor.
223 * @param[in] alpha Weight of the matrix product
224 * @param[in] beta Weight of the third matrix
225 * @param[in] dt Tensor's data type
226 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
227 *
228 * @return Computed output tensor.
229 */
230 static RawTensor compute_reference_gemm(const TensorShape &src_shape1, const TensorShape &src_shape2, const TensorShape &src_shape3,
231 const TensorShape &dst_shape, float alpha, float beta, DataType dt, int fixed_point_position = 0);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100232 /** Compute reference non linear filter function
233 *
234 * @param[in] shape Shape of the input and output tensors.Data type supported: U8
235 * @param[in] function Non linear function to perform
236 * @param[in] mask_size Mask size. Supported sizes: 3, 5
237 * @param[in] pattern Matrix pattern
238 * @param[in] mask The given mask. Will be used only if pattern is specified to PATTERN_OTHER
239 * @param[in] border_mode Strategy to use for borders.
240 * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT.
241 *
242 * @return Computed raw tensor.
243 */
244 static RawTensor compute_reference_non_linear_filter(const TensorShape &shape, NonLinearFilterFunction function, unsigned int mask_size,
245 MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246 /** Compute reference pixel-wise multiplication
247 *
248 * @param[in] shape Shape of the input and output tensors.
249 * @param[in] dt_in0 Data type of first input tensor.
250 * @param[in] dt_in1 Data type of second input tensor.
251 * @param[in] dt_out Data type of the output tensor.
252 * @param[in] scale Non-negative scale.
253 * @param[in] convert_policy Overflow policy of the operation.
254 * @param[in] rounding_policy Rounding policy of the operation.
255 *
256 * @return Computed raw tensor.
257 */
258 static RawTensor compute_reference_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, ConvertPolicy convert_policy,
259 RoundingPolicy rounding_policy);
260 /** Compute reference pixel-wise multiplication.
261 *
262 * @param[in] shape Shape of the input and output tensors.
263 * @param[in] dt_in0 Data type of first input tensor.
264 * @param[in] dt_in1 Data type of second input tensor.
265 * @param[in] dt_out Data type of the output tensor.
266 * @param[in] scale Scale to apply after multiplication. Must be positive.
267 * @param[in] fixed_point_position Fixed point position that expresses the number of bits for the fractional part of the number.
268 * @param[in] convert_policy Overflow policy of the operation.
269 * @param[in] rounding_policy Rounding policy of the operation.
270 *
271 * @return Computed raw tensor.
272 */
273 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,
274 ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
Isabella Gottardib797fa22017-06-23 15:02:11 +0100275 /** Compute reference Table Lookup.
276 *
277 * @param[in] shape Shape of the input and output tensors.
278 * @param[in] dt_inout Data type of input/output tensor.
279 * @param[in] lut Input lookup table.
280 *
281 * @return Computed raw tensor.
282 */
283 template <typename T>
284 static RawTensor compute_reference_table_lookup(const TensorShape &shape, DataType dt_inout, std::map<T, T> &lut);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100285 /** Compute reference threshold.
286 *
287 * @param[in] shape Shape of the input and output tensors.
288 * @param[in] threshold Threshold. When the threshold type is RANGE, this is used as the lower threshold.
289 * @param[in] false_value value to set when the condition is not respected.
290 * @param[in] true_value value to set when the condition is respected.
291 * @param[in] type Thresholding type. Either RANGE or BINARY.
292 * @param[in] upper Upper threshold. Only used when the thresholding type is RANGE.
293 *
294 * @return Computed raw tensor.
295 */
296 static RawTensor compute_reference_threshold(const TensorShape &shape, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper);
297 /** Compute reference activation layer.
298 *
299 * @param[in] shape Shape of the input and output tensors.
300 * @param[in] dt Data type of the tensors.
301 * @param[in] act_info Activation layer information.
302 * @param[in] fixed_point_position (Optional)Number of bits for the fractional part of fixed point numbers.
303 *
304 * @return Computed raw tensor.
305 */
306 static RawTensor compute_reference_activation_layer(const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position = 0);
307 /** Compute reference batch normalization layer.
308 *
309 * @param[in] shape0 Shape of the input and output tensors.
310 * @param[in] shape1 Shape of the vector tensors.
311 * @param[in] dt Data type of all input and output tensors.
312 * @param[in] epsilon Small value to avoid division with zero.
313 * @param[in] fixed_point_position Fixed point position.
314 *
315 * @return Computed raw tensor.
316 */
317 static RawTensor compute_reference_batch_normalization_layer(const TensorShape &shape0, const TensorShape &shape1, DataType dt, float epsilon, int fixed_point_position = 0);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100318 /** Compute reference convolution layer
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100319 *
320 * @param[in] input_shape Shape for the input tensor
321 * @param[in] weights_shape Shape for the weights tensor
322 * @param[in] bias_shape Shape for the bias tensor
323 * @param[in] output_shape Shape for the output tensor
324 * @param[in] dt Data type to use
325 * @param[in] conv_info Pads and strides information for the convolution layer
326 * @param[in] fixed_point_position Number of bits for the fractional part of the fixed point numbers
327 *
328 * @return Computed raw tensor.
329 */
330 static RawTensor compute_reference_convolution_layer(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, DataType dt,
331 const PadStrideInfo &conv_info, int fixed_point_position);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100332 /** Compute reference depth concatenation layer
333 *
334 * @param[in] shapes Input tensor shapes (All dimensions should match apart from DimZ)
335 * @param[in] dt Data type to use
336 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
337 *
338 * @return Computed raw tensor.
339 */
340 static RawTensor compute_reference_depth_concatenate_layer(const std::vector<TensorShape> &shapes, DataType dt, int fixed_point_position = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100341 /** Compute reference for fully connected layer function
342 *
343 * @param[in] input_shape Shape for the input tensor
344 * @param[in] weights_shape Shape for the weights tensor
345 * @param[in] bias_shape Shape for the bias tensor
346 * @param[in] output_shape Shape for the output tensor
347 * @param[in] dt Data type to use
348 * @param[in] transpose_weights Transpose the weights if true
349 * @param[in] fixed_point_position Number of bits for the fractional part of the fixed point numbers
350 *
351 * @return Computed raw tensor.
352 */
353 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,
354 bool transpose_weights, int fixed_point_position);
355 /** Compute reference normalization layer.
356 *
357 * @param[in] shape Shape of the input and output tensors.
358 * @param[in] dt Data type of input and output tensors.
359 * @param[in] norm_info Normalization Layer information.
360 * @param[in] fixed_point_position (Optional) Fixed point position that expresses the number of bits for the fractional part of the number when the tensor's data type is QS8 or QS16 (default = 0).
361 *
362 * @return Computed raw tensor.
363 */
364 static RawTensor compute_reference_normalization_layer(const TensorShape &shape, DataType dt, NormalizationLayerInfo norm_info, int fixed_point_position = 0);
365 /** Compute reference pooling layer.
366 *
367 * @param[in] shape_in Shape of the input tensor.
368 * @param[in] shape_out Shape of the output tensor.
369 * @param[in] dt Data type of input and output tensors.
370 * @param[in] pool_info Pooling Layer information.
371 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers.
372 *
373 * @return Computed raw tensor.
374 */
375 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 +0100376 /** Compute reference roi pooling layer.
377 *
378 * @param[in] shape Shape of the input tensor.
379 * @param[in] dt Data type of input and output tensors.
380 * @param[in] rois Region of interest vector.
381 * @param[in] pool_info ROI Pooling Layer information.
382 */
383 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 +0100384 /** Compute reference softmax layer.
385 *
386 * @param[in] shape Shape of the input and output tensors.
387 * @param[in] dt Data type of input and output tensors.
388 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
389 *
390 * @return Computed raw tensor.
391 */
392 static RawTensor compute_reference_softmax_layer(const TensorShape &shape, DataType dt, int fixed_point_position = 0);
393 /** Compute reference fixed point operation.
394 *
395 * @param[in] shape Shape of the input and output tensors.
396 * @param[in] dt_in Data type of the input tensor.
397 * @param[in] dt_out Data type of the output tensor.
398 * @param[in] op Fixed point operation to perform.
399 * @param[in] fixed_point_position Number of bits for the fractional part of the fixed point numbers
400 *
401 * @return Computed raw tensor.
402 */
403 static RawTensor compute_reference_fixed_point_operation(const TensorShape &shape, DataType dt_in, DataType dt_out, FixedPointOp op, int fixed_point_position);
404
405protected:
406 Reference() = default;
407 ~Reference() = default;
408};
409} // namespace validation
410} // namespace test
411} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100412#endif /* __ARM_COMPUTE_TEST_REFERENCE_REFERENCE_H__ */