blob: ce424bed03a758a860b55b430744b2eafd1d8bb6 [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_CPP_H__
25#define __ARM_COMPUTE_TEST_REFERENCE_REFERENCE_CPP_H__
26
Isabella Gottardib797fa22017-06-23 15:02:11 +010027#include "RawTensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "Reference.h"
29
Isabella Gottardib797fa22017-06-23 15:02:11 +010030#include <map>
Georgios Pinitasac4e8732017-07-05 17:02:25 +010031#include <memory>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include <ostream>
Georgios Pinitasd4f8c272017-06-30 16:16:19 +010033#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35namespace arm_compute
36{
37class Tensor;
38
39namespace test
40{
41namespace validation
42{
43/** C++ reference implementation. */
44class ReferenceCPP final : public Reference
45{
46public:
Giorgio Arena50f9fd72017-06-19 17:05:30 +010047 /** Function to compute reference sobel 3x3.
48 *
49 * @param[in] src Input tensor.
50 * @param[in] dst_x Result tensor along x axis
51 * @param[in] dst_y Result tensor along y axis
52 * @param[in] border_mode Border mode to use for input tensor
53 * @param[in] constant_border_value Constant value to use if @p border_mode is constant
54 *
55 */
56 static void sobel_3x3(RawTensor &src, RawTensor &dst_x, RawTensor &dst_y, BorderMode border_mode, uint8_t constant_border_value);
57 /** Function to compute reference sobel 5x5.
58 *
59 * @param[in] src Input tensor.
60 * @param[in] dst_x Result tensor along x axis
61 * @param[in] dst_y Result tensor along y axis
62 * @param[in] border_mode Border mode to use for input tensor
63 * @param[in] constant_border_value Constant value to use if @p border_mode is constant
64 *
65 */
66 static void sobel_5x5(RawTensor &src, RawTensor &dst_x, RawTensor &dst_y, BorderMode border_mode, uint8_t constant_border_value);
Giorgio Arenafc2817d2017-06-27 17:26:37 +010067 /** Function to compute reference Harris corners.
68 *
69 * @param[in] src Input tensor
70 * @param[in] Gx Tensor used to compute Sobel along the x axis
71 * @param[in] Gy Tensor used to compute Sobel along the y axis
72 * @param[in] candidates Tensor used to store candidate corners
73 * @param[in] non_maxima Tensor used to store non_maxima suppressed candidate corners
74 * @param[in] threshold Minimum threshold with which to eliminate Harris Corner scores (computed using the normalized Sobel kernel).
75 * @param[in] min_dist Radial Euclidean distance for the euclidean distance stage
76 * @param[in] sensitivity Sensitivity threshold k from the Harris-Stephens equation
77 * @param[in] gradient_size The gradient window size to use on the input. The implementation supports 3, 5, and 7
78 * @param[in] block_size The block window size used to compute the Harris Corner score. The implementation supports 3, 5, and 7.
79 * @param[out] corners Array of keypoints to store the results.
80 * @param[in] border_mode Border mode to use
81 * @param[in] constant_border_value Constant value to use for borders if border_mode is set to CONSTANT.
82 *
83 */
84 static void harris_corners(RawTensor &src, RawTensor &Gx, RawTensor &Gy, const RawTensor &candidates, const RawTensor &non_maxima, float threshold, float min_dist, float sensitivity,
85 int32_t gradient_size, int32_t block_size, KeyPointArray &corners, BorderMode border_mode, uint8_t constant_border_value);
Giorgio Arena2ca209e2017-06-13 15:49:37 +010086 /** Function to compute the min max values and their location in a tensor.
87 *
88 * @param[in] src Input tensor.
89 * @param[out] min Minimum value of the tensor.
90 * @param[out] max Maximum value of the tensor
91 * @param[out] min_loc Array with locations of minimum values
92 * @param[out] max_loc Array with locations of maximum values
93 * @param[out] min_count Number of minimum values found
94 * @param[out] max_count Number of maximum values found
95 */
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +010096 static void min_max_location(const RawTensor &src, void *min, void *max, IArray<Coordinates2D> &min_loc, IArray<Coordinates2D> &max_loc, uint32_t &min_count, uint32_t &max_count);
Giorgio Arenaf7959862017-06-13 15:19:51 +010097 /** Function to compute the mean and standard deviation of a tensor.
98 *
99 * @param[in] src Input tensor.
100 * @param[out] mean Mean of the tensor.
101 * @param[out] std_dev Standard deviation of the tensor
102 */
103 static void mean_and_standard_deviation(const RawTensor &src, float &mean, float &std_dev);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 /** Function to compute the integral image of a tensor.
105 *
106 * @param[in] src Input tensor.
107 * @param[out] dst Result tensor.
108 */
109 static void integral_image(const RawTensor &src, RawTensor &dst);
110 /** Function to compute the absolute difference between two tensors.
111 *
112 * @param[in] src1 First tensor.
113 * @param[in] src2 Second tensor.
114 * @param[out] dst Result tensor.
115 */
116 static void absolute_difference(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
117 /** Function to accumulate an input tensor into an output tensor.
118 *
119 * @param[in] src Input tensor.
120 * @param[in, out] dst Result tensor.
121 */
122 static void accumulate(const RawTensor &src, RawTensor &dst);
123 /** Function to accumulate a squared value from an input tensor to an output tensor.
124 *
125 * @param[in] src Input tensor.
126 * @param[in, out] dst Result tensor.
127 * @param[in] shift A uint32_t value within the range of [0, 15]
128 */
129 static void accumulate_squared(const RawTensor &src, RawTensor &dst, uint32_t shift);
130 /** Function to accumulate a weighted value from an input tensor to an output tensor.
131 *
132 * @param[in] src Input tensor.
133 * @param[in, out] dst Result tensor.
134 * @param[in] alpha A float value within the range of [0, 1]
135 */
136 static void accumulate_weighted(const RawTensor &src, RawTensor &dst, float alpha);
137 /** Arithmetic addition of @p src1 and @p src2
138 *
139 * @param[in] src1 First tensor.
140 * @param[in] src2 Second tensor.
141 * @param[out] dst Result tensor.
142 * @param[in] convert_policy Overflow policy.
143 */
144 static void arithmetic_addition(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy);
145 /** Arithmetic subtraction of @p src2 from @p src1
146 *
147 * @param[in] src1 First tensor.
148 * @param[in] src2 Second tensor.
149 * @param[out] dst Result tensor.
150 * @param[in] convert_policy Overflow policy.
151 */
152 static void arithmetic_subtraction(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy);
SiCong Libacaf9a2017-06-19 13:41:45 +0100153 /** Function to compute box3x3 filtered result tensor.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100154 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100155 * @param[in] src Input tensor.
156 * @param[out] dst Result tensor.
157 * @param[in] border_mode Border mode.
158 * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 */
SiCong Libacaf9a2017-06-19 13:41:45 +0100160 static void box3x3(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161 /** Depth conversion from @p src to @p dst
162 *
163 * @param[in] src First tensor.
164 * @param[out] dst Result tensor.
165 * @param[in] policy Overflow policy.
166 * @param[in] shift Value for down/up conversions.
167 */
168 static void depth_convert(const RawTensor &src, RawTensor &dst, ConvertPolicy policy, uint32_t shift);
SiCong Li5a536642017-06-19 14:47:05 +0100169 /** Function to compute gaussian3x3 filtered result tensor.
170 *
171 * @param[in] src Input tensor.
172 * @param[out] dst Result tensor.
173 * @param[in] border_mode Border mode
174 * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT
175 */
176 static void gaussian3x3(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value);
SiCong Li3eb263e2017-06-19 15:31:43 +0100177 /** Function to compute gaussian5x5 filtered result tensor.
178 *
179 * @param[in] src Input tensor.
180 * @param[out] dst Result tensor.
181 * @param[in] border_mode Border mode
182 * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT
183 */
184 static void gaussian5x5(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100185 /** Compute non linear filter function.
186 *
187 * @param[in] src First input tensor
188 * @param[out] dst Output tensor
189 * @param[in] function Non linear function to perform
190 * @param[in] mask_size Mask size. Supported sizes: 3, 5
191 * @param[in] pattern Matrix pattern
192 * @param[in] mask The given mask.
193 * @param[in] border_mode Strategy to use for borders.
194 * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT.
195 */
196 static void non_linear_filter(const RawTensor &src, RawTensor &dst, NonLinearFilterFunction function, unsigned int mask_size,
197 MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 /** Element-wise multiplication of @p src1, @p src2 and @p scale
199 *
200 * @param[in] src1 First tensor.
201 * @param[in] src2 Second tensor.
202 * @param[out] dst Result tensor.
203 * @param[in] scale A non-negative float multiplied to each product.
204 * @param[in] convert_policy Overflow policy.
205 * @param[in] rounding_policy Rounding policy.
206 */
207 static void pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
208 /** Fixed-point Pixel-wise multiplication of @p src1 by @p src2
209 *
210 * @param[in] src1 First tensor.
211 * @param[in] src2 Second tensor.
212 * @param[out] dst Result tensor.
213 * @param[in] scale A non-negative float multiplied to each product.
214 * @param[in] convert_policy Overflow policy.
215 * @param[in] rounding_policy Rounding policy.
216 */
217 static void fixed_point_pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
Isabella Gottardib797fa22017-06-23 15:02:11 +0100218 /** Table Lookup f@p src to @p dst
219 *
220 * @param[in] src Input tensor.
221 * @param[out] dst Result tensor.
222 * @param[in] lut Input lookup table.
223 */
224 template <typename T>
225 static void table_lookup(const RawTensor &src, RawTensor &dst, std::map<T, T> &lut);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100226 /** Threshold of@p src to @p dst
227 *
Isabella Gottardib797fa22017-06-23 15:02:11 +0100228 * @param[in] src Input tensor.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100229 * @param[out] dst Result tensor.
230 * @param[in] threshold Threshold. When the threhold type is RANGE, this is used as the lower threshold.
231 * @param[in] false_value value to set when the condition is not respected.
232 * @param[in] true_value value to set when the condition is respected.
233 * @param[in] type Thresholding type. Either RANGE or BINARY.
234 * @param[in] upper Upper threshold. Only used when the thresholding type is RANGE.
235 */
236 static void threshold(const RawTensor &src, RawTensor &dst, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper);
Isabella Gottardi62031532017-07-04 11:21:28 +0100237 /** Warp perspective of@p src to @p dst
238 *
239 * @param[in] src First tensor.
240 * @param[out] dst Result tensor.
241 * @param[out] valid_mask Valid mask tensor.
242 * @param[in] matrix The perspective matrix. Must be 3x3 of type float.
243 * @param[in] policy The interpolation type.
244 * @param[in] border_mode Strategy to use for borders.
245 * @param[in] constant_border_value Constant value to use for borders if border_mode is set to CONSTANT.
246 */
247 static void warp_perspective(const RawTensor &src, RawTensor &dst, RawTensor &valid_mask, const float *matrix, InterpolationPolicy policy, BorderMode border_mode, uint8_t constant_border_value);
248
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100249 /** Batch Normalization of @p src based on the information from @p norm_info.
250 *
251 * @param[in] src Input tensor.
252 * @param[out] dst Result tensor.
253 * @param[out] mean Mean vector tensor.
254 * @param[out] var Var vector tensor.
255 * @param[out] beta Beta vector tensor.
256 * @param[out] gamma Gamma vector tensor.
257 * @param[in] epsilon Small value to avoid division with zero.
258 * @param[in] fixed_point_position Fixed point position.
259 */
260 static void batch_normalization_layer(const RawTensor &src, RawTensor &dst, const RawTensor &mean, const RawTensor &var, const RawTensor &beta, const RawTensor &gamma, float epsilon,
261 int fixed_point_position = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100262 /** Fully connected layer function
263 *
264 * @param[in] src Input tensor
265 * @param[in] weights Weights tensor.
266 * @param[in] bias Bias tensor.
267 * @param[out] dst Result tensor.
268 */
269 static void fully_connected_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100270 /** Pooling layer of @p src based on the information from @p pool_info.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100271 *
Michalis Spyroubbd9fb92017-06-22 12:57:51 +0100272 * @param[in] src Input tensor.
273 * @param[out] dst Result tensor.
274 * @param[in] pool_info Pooling Layer information.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100275 */
Michalis Spyroubbd9fb92017-06-22 12:57:51 +0100276 static void pooling_layer(const RawTensor &src, RawTensor &dst, PoolingLayerInfo pool_info);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100277 /** ROI Pooling layer of @p src based on the information from @p pool_info and @p rois.
278 *
279 * @param[in] src Input tensor.
280 * @param[out] dst Result tensor.
281 * @param[in] rois Region of Interest points.
282 * @param[in] pool_info ROI Pooling Layer information.
283 */
284 static void roi_pooling_layer(const RawTensor &src, RawTensor &dst, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100285 /** Fixed point operations of @p src
286 *
287 * @param[in] src Input tensor.
288 * @param[out] dst Result tensor.
289 * @param[in] op Fixed point operation to perform.
290 */
291 static void fixed_point_operation(const RawTensor &src, RawTensor &dst, FixedPointOp op);
292
293private:
294 ReferenceCPP() = delete;
295 ~ReferenceCPP() = delete;
296};
297} // namespace validation
298} // namespace test
299} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100300#endif /* __ARM_COMPUTE_TEST_REFERENCE_REFERENCE_CPP_H__ */