blob: a3ae3b6d0284aab83dd5f05050d45e3b1969fa4d [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"
29
Isabella Gottardib797fa22017-06-23 15:02:11 +010030#include <map>
Georgios Pinitasd4f8c272017-06-30 16:16:19 +010031#include <vector>
32
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033namespace arm_compute
34{
35namespace test
36{
37namespace validation
38{
39/** Interface for reference implementations. */
40class Reference
41{
42public:
Giorgio Arena50f9fd72017-06-19 17:05:30 +010043 /** Compute reference sobel 3x3.
44 *
45 * @param[in] shape Shape of the input and output tensors.
46 * @param[in] border_mode Border mode to use for input tensor
47 * @param[in] constant_border_value Constant value to use if @p border_mode is constant
48 *
49 * @return Computed raw tensors along x and y axis.
50 */
51 static std::pair<RawTensor, RawTensor> compute_reference_sobel_3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
52 /** Compute reference sobel 5x5.
53 *
54 * @param[in] shape Shape of the input and output tensors.
55 * @param[in] border_mode Border mode to use for input tensor
56 * @param[in] constant_border_value Constant value to use if @p border_mode is constant
57 *
58 * @return Computed raw tensors along x and y axis.
59 */
60 static std::pair<RawTensor, RawTensor> compute_reference_sobel_5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
Giorgio Arenaf7959862017-06-13 15:19:51 +010061 /** Compute reference mean and standard deviation.
62 *
63 * @param[in] shape Shape of the input tensors.
64 *
65 * @return Computed mean and standard deviation.
66 */
67 static std::pair<float, float> compute_reference_mean_and_standard_deviation(const TensorShape &shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 /** Compute reference integral image.
69 *
70 * @param[in] shape Shape of the input and output tensors.
71 *
72 * @return Computed raw tensor.
73 */
74 static RawTensor compute_reference_integral_image(const TensorShape &shape);
75 /** Compute reference absolute difference.
76 *
77 * @param[in] shape Shape of the input and output tensors.
78 * @param[in] dt_in0 Data type of first input tensor.
79 * @param[in] dt_in1 Data type of second input tensor.
80 * @param[in] dt_out Data type of the output tensor.
81 *
82 * @return Computed raw tensor.
83 */
84 static RawTensor compute_reference_absolute_difference(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out);
85 /** Compute reference accumulate.
86 *
87 * @param[in] shape Shape of the input and output tensors.
88 *
89 * @return Computed raw tensor.
90 */
91 static RawTensor compute_reference_accumulate(const TensorShape &shape);
92 /** Compute reference accumulate.
93 *
94 * @param[in] shape Shape of the input and output tensors.
95 * @param[in] shift A uint32_t value within the range of [0, 15]
96 *
97 * @return Computed raw tensor.
98 */
99 static RawTensor compute_reference_accumulate_squared(const TensorShape &shape, uint32_t shift);
100 /** Compute reference accumulate.
101 *
102 * @param[in] shape Shape of the input and output tensors.
103 * @param[in] alpha A float value within the range of [0, 1]
104 *
105 * @return Computed raw tensor.
106 */
107 static RawTensor compute_reference_accumulate_weighted(const TensorShape &shape, float alpha);
108 /** Compute reference arithmetic addition.
109 *
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100110 * @param[in] shape Shape of the input and output tensors.
111 * @param[in] dt_in0 Data type of first input tensor.
112 * @param[in] dt_in1 Data type of second input tensor.
113 * @param[in] dt_out Data type of the output tensor.
114 * @param[in] convert_policy Overflow policy of the operation.
115 * @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 +0100116 *
117 * @return Computed raw tensor.
118 */
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100119 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 +0100120 /** Compute reference arithmetic subtraction.
121 *
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100122 * @param[in] shape Shape of the input and output tensors.
123 * @param[in] dt_in0 Data type of first input tensor.
124 * @param[in] dt_in1 Data type of second input tensor.
125 * @param[in] dt_out Data type of the output tensor.
126 * @param[in] convert_policy Overflow policy of the operation.
127 * @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 +0100128 *
129 * @return Computed raw tensor.
130 */
Michele Di Giorgio81f0d152017-07-11 15:00:52 +0100131 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 +0100132 /** Compute reference bitwise and.
133 *
134 * @param[in] shape Shape of the input and output tensors.
135 *
136 * @return Computed raw tensor.
137 */
138 static RawTensor compute_reference_bitwise_and(const TensorShape &shape);
139 /** Compute reference bitwise or.
140 *
141 * @param[in] shape Shape of the input and output tensors.
142 *
143 * @return Computed raw tensor.
144 */
145 static RawTensor compute_reference_bitwise_or(const TensorShape &shape);
146 /** Compute reference bitwise xor.
147 *
148 * @param[in] shape Shape of the input and output tensors.
149 *
150 * @return Computed raw tensor.
151 */
152 static RawTensor compute_reference_bitwise_xor(const TensorShape &shape);
153 /** Compute reference bitwise not.
154 *
155 * @param[in] shape Shape of the input and output tensors.
156 *
157 * @return Computed raw tensor.
158 */
159 static RawTensor compute_reference_bitwise_not(const TensorShape &shape);
SiCong Libacaf9a2017-06-19 13:41:45 +0100160 /** Compute reference box3x3 filter.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100162 * @param[in] shape Shape of the input and output tensors.
163 * @param[in] border_mode BorderMode used by the input tensor.
164 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165 *
166 * @return Computed raw tensor.
167 */
SiCong Libacaf9a2017-06-19 13:41:45 +0100168 static RawTensor compute_reference_box3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169 /** Compute reference depth convert.
170 *
Georgios Pinitase2229412017-07-12 12:30:40 +0100171 * @param[in] shape Shape of the input and output tensors.
172 * @param[in] dt_in Data type of input tensor.
173 * @param[in] dt_out Data type of the output tensor.
174 * @param[in] policy Overflow policy of the operation.
175 * @param[in] shift Value for down/up conversions. Must be 0 <= shift < 8.
176 * @param[in] fixed_point_position_in (Optional) Fixed point position for the input tensor.
177 * @param[in] fixed_point_position_out (Optional) Fixed point position for the output tensor.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 *
179 * @return Computed raw tensor.
180 */
Georgios Pinitase2229412017-07-12 12:30:40 +0100181 static RawTensor compute_reference_depth_convert(const TensorShape &shape, DataType dt_in, DataType dt_out, ConvertPolicy policy,
182 uint32_t shift, uint32_t fixed_point_position_in = 0, uint32_t fixed_point_position_out = 0);
SiCong Li5a536642017-06-19 14:47:05 +0100183 /** Compute reference gaussian3x3 filter.
184 *
185 * @param[in] shape Shape of the input and output tensors.
186 * @param[in] border_mode BorderMode used by the input tensor
187 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT
188 *
189 * @return Computed raw tensor.
190 */
191 static RawTensor compute_reference_gaussian3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
SiCong Li3eb263e2017-06-19 15:31:43 +0100192 /** Compute reference gaussian5x5 filter.
193 *
194 * @param[in] shape Shape of the input and output tensors.
195 * @param[in] border_mode BorderMode used by the input tensor.
196 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT.
197 *
198 * @return Computed raw tensor.
199 */
200 static RawTensor compute_reference_gaussian5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100201 /** Compute matrix multiply function.
202 *
203 * @param[in] src_shape1 First input tensor shape
204 * @param[in] src_shape2 Second input tensor shape
205 * @param[in] src_shape3 Third input tensor shape
206 * @param[out] dst_shape Output tensor.
207 * @param[in] alpha Weight of the matrix product
208 * @param[in] beta Weight of the third matrix
209 * @param[in] dt Tensor's data type
210 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
211 *
212 * @return Computed output tensor.
213 */
214 static RawTensor compute_reference_gemm(const TensorShape &src_shape1, const TensorShape &src_shape2, const TensorShape &src_shape3,
215 const TensorShape &dst_shape, float alpha, float beta, DataType dt, int fixed_point_position = 0);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100216 /** Compute reference non linear filter function
217 *
218 * @param[in] shape Shape of the input and output tensors.Data type supported: U8
219 * @param[in] function Non linear function to perform
220 * @param[in] mask_size Mask size. Supported sizes: 3, 5
221 * @param[in] pattern Matrix pattern
222 * @param[in] mask The given mask. Will be used only if pattern is specified to PATTERN_OTHER
223 * @param[in] border_mode Strategy to use for borders.
224 * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT.
225 *
226 * @return Computed raw tensor.
227 */
228 static RawTensor compute_reference_non_linear_filter(const TensorShape &shape, NonLinearFilterFunction function, unsigned int mask_size,
229 MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230 /** Compute reference pixel-wise multiplication
231 *
232 * @param[in] shape Shape of the input and output tensors.
233 * @param[in] dt_in0 Data type of first input tensor.
234 * @param[in] dt_in1 Data type of second input tensor.
235 * @param[in] dt_out Data type of the output tensor.
236 * @param[in] scale Non-negative scale.
237 * @param[in] convert_policy Overflow policy of the operation.
238 * @param[in] rounding_policy Rounding policy of the operation.
239 *
240 * @return Computed raw tensor.
241 */
242 static RawTensor compute_reference_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, ConvertPolicy convert_policy,
243 RoundingPolicy rounding_policy);
244 /** Compute reference pixel-wise multiplication.
245 *
246 * @param[in] shape Shape of the input and output tensors.
247 * @param[in] dt_in0 Data type of first input tensor.
248 * @param[in] dt_in1 Data type of second input tensor.
249 * @param[in] dt_out Data type of the output tensor.
250 * @param[in] scale Scale to apply after multiplication. Must be positive.
251 * @param[in] fixed_point_position Fixed point position that expresses the number of bits for the fractional part of the number.
252 * @param[in] convert_policy Overflow policy of the operation.
253 * @param[in] rounding_policy Rounding policy of the operation.
254 *
255 * @return Computed raw tensor.
256 */
257 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,
258 ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
Isabella Gottardib797fa22017-06-23 15:02:11 +0100259 /** Compute reference Table Lookup.
260 *
261 * @param[in] shape Shape of the input and output tensors.
262 * @param[in] dt_inout Data type of input/output tensor.
263 * @param[in] lut Input lookup table.
264 *
265 * @return Computed raw tensor.
266 */
267 template <typename T>
268 static RawTensor compute_reference_table_lookup(const TensorShape &shape, DataType dt_inout, std::map<T, T> &lut);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100269 /** Compute reference threshold.
270 *
271 * @param[in] shape Shape of the input and output tensors.
272 * @param[in] threshold Threshold. When the threshold type is RANGE, this is used as the lower threshold.
273 * @param[in] false_value value to set when the condition is not respected.
274 * @param[in] true_value value to set when the condition is respected.
275 * @param[in] type Thresholding type. Either RANGE or BINARY.
276 * @param[in] upper Upper threshold. Only used when the thresholding type is RANGE.
277 *
278 * @return Computed raw tensor.
279 */
280 static RawTensor compute_reference_threshold(const TensorShape &shape, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper);
281 /** Compute reference activation layer.
282 *
283 * @param[in] shape Shape of the input and output tensors.
284 * @param[in] dt Data type of the tensors.
285 * @param[in] act_info Activation layer information.
286 * @param[in] fixed_point_position (Optional)Number of bits for the fractional part of fixed point numbers.
287 *
288 * @return Computed raw tensor.
289 */
290 static RawTensor compute_reference_activation_layer(const TensorShape &shape, DataType dt, ActivationLayerInfo act_info, int fixed_point_position = 0);
291 /** Compute reference batch normalization layer.
292 *
293 * @param[in] shape0 Shape of the input and output tensors.
294 * @param[in] shape1 Shape of the vector tensors.
295 * @param[in] dt Data type of all input and output tensors.
296 * @param[in] epsilon Small value to avoid division with zero.
297 * @param[in] fixed_point_position Fixed point position.
298 *
299 * @return Computed raw tensor.
300 */
301 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 +0100302 /** Compute reference convolution layer
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100303 *
304 * @param[in] input_shape Shape for the input tensor
305 * @param[in] weights_shape Shape for the weights tensor
306 * @param[in] bias_shape Shape for the bias tensor
307 * @param[in] output_shape Shape for the output tensor
308 * @param[in] dt Data type to use
309 * @param[in] conv_info Pads and strides information for the convolution layer
310 * @param[in] fixed_point_position Number of bits for the fractional part of the fixed point numbers
311 *
312 * @return Computed raw tensor.
313 */
314 static RawTensor compute_reference_convolution_layer(const TensorShape &input_shape, const TensorShape &weights_shape, const TensorShape &bias_shape, const TensorShape &output_shape, DataType dt,
315 const PadStrideInfo &conv_info, int fixed_point_position);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100316 /** Compute reference depth concatenation layer
317 *
318 * @param[in] shapes Input tensor shapes (All dimensions should match apart from DimZ)
319 * @param[in] dt Data type to use
320 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
321 *
322 * @return Computed raw tensor.
323 */
324 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 +0100325 /** Compute reference for fully connected layer function
326 *
327 * @param[in] input_shape Shape for the input tensor
328 * @param[in] weights_shape Shape for the weights tensor
329 * @param[in] bias_shape Shape for the bias tensor
330 * @param[in] output_shape Shape for the output tensor
331 * @param[in] dt Data type to use
332 * @param[in] transpose_weights Transpose the weights if true
333 * @param[in] fixed_point_position Number of bits for the fractional part of the fixed point numbers
334 *
335 * @return Computed raw tensor.
336 */
337 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,
338 bool transpose_weights, int fixed_point_position);
339 /** Compute reference normalization layer.
340 *
341 * @param[in] shape Shape of the input and output tensors.
342 * @param[in] dt Data type of input and output tensors.
343 * @param[in] norm_info Normalization Layer information.
344 * @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).
345 *
346 * @return Computed raw tensor.
347 */
348 static RawTensor compute_reference_normalization_layer(const TensorShape &shape, DataType dt, NormalizationLayerInfo norm_info, int fixed_point_position = 0);
349 /** Compute reference pooling layer.
350 *
351 * @param[in] shape_in Shape of the input tensor.
352 * @param[in] shape_out Shape of the output tensor.
353 * @param[in] dt Data type of input and output tensors.
354 * @param[in] pool_info Pooling Layer information.
355 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers.
356 *
357 * @return Computed raw tensor.
358 */
359 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 +0100360 /** Compute reference roi pooling layer.
361 *
362 * @param[in] shape Shape of the input tensor.
363 * @param[in] dt Data type of input and output tensors.
364 * @param[in] rois Region of interest vector.
365 * @param[in] pool_info ROI Pooling Layer information.
366 */
367 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 +0100368 /** Compute reference softmax layer.
369 *
370 * @param[in] shape Shape of the input and output tensors.
371 * @param[in] dt Data type of input and output tensors.
372 * @param[in] fixed_point_position (Optional) Number of bits for the fractional part of the fixed point numbers
373 *
374 * @return Computed raw tensor.
375 */
376 static RawTensor compute_reference_softmax_layer(const TensorShape &shape, DataType dt, int fixed_point_position = 0);
377 /** Compute reference fixed point operation.
378 *
379 * @param[in] shape Shape of the input and output tensors.
380 * @param[in] dt_in Data type of the input tensor.
381 * @param[in] dt_out Data type of the output tensor.
382 * @param[in] op Fixed point operation to perform.
383 * @param[in] fixed_point_position Number of bits for the fractional part of the fixed point numbers
384 *
385 * @return Computed raw tensor.
386 */
387 static RawTensor compute_reference_fixed_point_operation(const TensorShape &shape, DataType dt_in, DataType dt_out, FixedPointOp op, int fixed_point_position);
388
389protected:
390 Reference() = default;
391 ~Reference() = default;
392};
393} // namespace validation
394} // namespace test
395} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100396#endif /* __ARM_COMPUTE_TEST_REFERENCE_REFERENCE_H__ */