blob: d3c77a224363893bfc90ee88d189686f581c1c4b [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
27#include "Reference.h"
28
29#include "RawTensor.h"
30
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 Arenaf7959862017-06-13 15:19:51 +010067 /** Function to compute the mean and standard deviation of a tensor.
68 *
69 * @param[in] src Input tensor.
70 * @param[out] mean Mean of the tensor.
71 * @param[out] std_dev Standard deviation of the tensor
72 */
73 static void mean_and_standard_deviation(const RawTensor &src, float &mean, float &std_dev);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 /** Function to compute the integral image of a tensor.
75 *
76 * @param[in] src Input tensor.
77 * @param[out] dst Result tensor.
78 */
79 static void integral_image(const RawTensor &src, RawTensor &dst);
80 /** Function to compute the absolute difference between two tensors.
81 *
82 * @param[in] src1 First tensor.
83 * @param[in] src2 Second tensor.
84 * @param[out] dst Result tensor.
85 */
86 static void absolute_difference(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
87 /** Function to accumulate an input tensor into an output tensor.
88 *
89 * @param[in] src Input tensor.
90 * @param[in, out] dst Result tensor.
91 */
92 static void accumulate(const RawTensor &src, RawTensor &dst);
93 /** Function to accumulate a squared value from an input tensor to an output tensor.
94 *
95 * @param[in] src Input tensor.
96 * @param[in, out] dst Result tensor.
97 * @param[in] shift A uint32_t value within the range of [0, 15]
98 */
99 static void accumulate_squared(const RawTensor &src, RawTensor &dst, uint32_t shift);
100 /** Function to accumulate a weighted value from an input tensor to an output tensor.
101 *
102 * @param[in] src Input tensor.
103 * @param[in, out] dst Result tensor.
104 * @param[in] alpha A float value within the range of [0, 1]
105 */
106 static void accumulate_weighted(const RawTensor &src, RawTensor &dst, float alpha);
107 /** Arithmetic addition of @p src1 and @p src2
108 *
109 * @param[in] src1 First tensor.
110 * @param[in] src2 Second tensor.
111 * @param[out] dst Result tensor.
112 * @param[in] convert_policy Overflow policy.
113 */
114 static void arithmetic_addition(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy);
115 /** Arithmetic subtraction of @p src2 from @p src1
116 *
117 * @param[in] src1 First tensor.
118 * @param[in] src2 Second tensor.
119 * @param[out] dst Result tensor.
120 * @param[in] convert_policy Overflow policy.
121 */
122 static void arithmetic_subtraction(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy);
123 /** Function to compute the bitwise and between two tensors.
124 *
125 * @param[in] src1 First tensor.
126 * @param[in] src2 Second tensor.
127 * @param[out] dst Result tensor.
128 */
129 static void bitwise_and(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
130 /** Function to compute the bitwise or between two tensors.
131 *
132 * @param[in] src1 First tensor.
133 * @param[in] src2 Second tensor.
134 * @param[out] dst Result tensor.
135 */
136 static void bitwise_or(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
137 /** Function to compute the bitwise xor between two tensors.
138 *
139 * @param[in] src1 First tensor.
140 * @param[in] src2 Second tensor.
141 * @param[out] dst Result tensor.
142 */
143 static void bitwise_xor(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
144 /** Function to compute the bitwise not of a tensor.
145 *
146 * @param[in] src Input tensor.
147 * @param[out] dst Result tensor.
148 */
149 static void bitwise_not(const RawTensor &src, RawTensor &dst);
SiCong Libacaf9a2017-06-19 13:41:45 +0100150 /** Function to compute box3x3 filtered result tensor.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100152 * @param[in] src Input tensor.
153 * @param[out] dst Result tensor.
154 * @param[in] border_mode Border mode.
155 * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 */
SiCong Libacaf9a2017-06-19 13:41:45 +0100157 static void box3x3(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 /** Depth conversion from @p src to @p dst
159 *
160 * @param[in] src First tensor.
161 * @param[out] dst Result tensor.
162 * @param[in] policy Overflow policy.
163 * @param[in] shift Value for down/up conversions.
164 */
165 static void depth_convert(const RawTensor &src, RawTensor &dst, ConvertPolicy policy, uint32_t shift);
SiCong Li5a536642017-06-19 14:47:05 +0100166 /** Function to compute gaussian3x3 filtered result tensor.
167 *
168 * @param[in] src Input tensor.
169 * @param[out] dst Result tensor.
170 * @param[in] border_mode Border mode
171 * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT
172 */
173 static void gaussian3x3(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value);
SiCong Li3eb263e2017-06-19 15:31:43 +0100174 /** Function to compute gaussian5x5 filtered result tensor.
175 *
176 * @param[in] src Input tensor.
177 * @param[out] dst Result tensor.
178 * @param[in] border_mode Border mode
179 * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT
180 */
181 static void gaussian5x5(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182 /** Compute GEMM function.
183 *
184 * @param[in] src1 First input tensor
185 * @param[in] src2 Second input tensor
186 * @param[in] src3 Third input tensor
187 * @param[out] dst Output tensr
188 * @param[in] alpha Weight of the matrix product
189 * @param[in] beta Weight of the third matrix
190 */
191 static void gemm(const RawTensor &src1, const RawTensor &src2, const RawTensor &src3,
192 RawTensor &dst, float alpha, float beta);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100193 /** Compute non linear filter function.
194 *
195 * @param[in] src First input tensor
196 * @param[out] dst Output tensor
197 * @param[in] function Non linear function to perform
198 * @param[in] mask_size Mask size. Supported sizes: 3, 5
199 * @param[in] pattern Matrix pattern
200 * @param[in] mask The given mask.
201 * @param[in] border_mode Strategy to use for borders.
202 * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT.
203 */
204 static void non_linear_filter(const RawTensor &src, RawTensor &dst, NonLinearFilterFunction function, unsigned int mask_size,
205 MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100206 /** Element-wise multiplication of @p src1, @p src2 and @p scale
207 *
208 * @param[in] src1 First tensor.
209 * @param[in] src2 Second tensor.
210 * @param[out] dst Result tensor.
211 * @param[in] scale A non-negative float multiplied to each product.
212 * @param[in] convert_policy Overflow policy.
213 * @param[in] rounding_policy Rounding policy.
214 */
215 static void pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
216 /** Fixed-point Pixel-wise multiplication of @p src1 by @p src2
217 *
218 * @param[in] src1 First tensor.
219 * @param[in] src2 Second tensor.
220 * @param[out] dst Result tensor.
221 * @param[in] scale A non-negative float multiplied to each product.
222 * @param[in] convert_policy Overflow policy.
223 * @param[in] rounding_policy Rounding policy.
224 */
225 static void fixed_point_pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
226 /** Threshold of@p src to @p dst
227 *
228 * @param[in] src First tensor.
229 * @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);
237 /** Activation layer of @p src base on information from @p act_info.
238 *
239 * @param[in] input Input tensor.
240 * @param[in] output Second tensor.
241 * @param[out] act_info Activation layer information.
242 */
243 static void activation_layer(const RawTensor &input, RawTensor &output, ActivationLayerInfo act_info);
244 /** Batch Normalization of @p src based on the information from @p norm_info.
245 *
246 * @param[in] src Input tensor.
247 * @param[out] dst Result tensor.
248 * @param[out] mean Mean vector tensor.
249 * @param[out] var Var vector tensor.
250 * @param[out] beta Beta vector tensor.
251 * @param[out] gamma Gamma vector tensor.
252 * @param[in] epsilon Small value to avoid division with zero.
253 * @param[in] fixed_point_position Fixed point position.
254 */
255 static void batch_normalization_layer(const RawTensor &src, RawTensor &dst, const RawTensor &mean, const RawTensor &var, const RawTensor &beta, const RawTensor &gamma, float epsilon,
256 int fixed_point_position = 0);
257 /** Convolution layer function
258 *
259 * @param[in] src Input tensor.
260 * @param[in] weights Weights tensor.
261 * @param[in] bias Bias tensor.
262 * @param[out] dst Result tensor.
263 * @param[in] conv_info Pads and strides information for the convolution layer.
264 */
265 static void convolution_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst, const PadStrideInfo &conv_info);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100266 /** Depth concatenate layer from @p srcs to @p dst
267 *
268 * @param[in] srcs Input tensors.
269 * @param[out] dst Result tensor.
270 */
271 static void depth_concatenate_layer(const std::vector<std::unique_ptr<RawTensor>> &srcs, RawTensor &dst);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100272 /** Fully connected layer function
273 *
274 * @param[in] src Input tensor
275 * @param[in] weights Weights tensor.
276 * @param[in] bias Bias tensor.
277 * @param[out] dst Result tensor.
278 */
279 static void fully_connected_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst);
280 /** Normalization of @p src based on the information from @p norm_info.
281 *
282 * @param[in] src Input tensor.
283 * @param[out] dst Result tensor.
284 * @param[in] norm_info Normalization Layer information.
285 */
286 static void normalization_layer(const RawTensor &src, RawTensor &dst, NormalizationLayerInfo norm_info);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100287 /** Pooling layer of @p src based on the information from @p pool_info.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100288 *
289 * @param[in] src Input tensor.
290 * @param[out] dst Result tensor.
291 * @param[in] pool_info Pooling Layer information.
292 * @param[in] fixed_point_position Fixed point position. (Optional)
293 */
294 static void pooling_layer(const RawTensor &src, RawTensor &dst, PoolingLayerInfo pool_info, int fixed_point_position = 0);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100295 /** ROI Pooling layer of @p src based on the information from @p pool_info and @p rois.
296 *
297 * @param[in] src Input tensor.
298 * @param[out] dst Result tensor.
299 * @param[in] rois Region of Interest points.
300 * @param[in] pool_info ROI Pooling Layer information.
301 */
302 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 +0100303 /** Softmax Layer of @p src.
304 *
305 * @param[in] src Input tensor.
306 * @param[out] dst Result tensor.
307 */
308 static void softmax_layer(const RawTensor &src, RawTensor &dst);
309 /** Fixed point operations of @p src
310 *
311 * @param[in] src Input tensor.
312 * @param[out] dst Result tensor.
313 * @param[in] op Fixed point operation to perform.
314 */
315 static void fixed_point_operation(const RawTensor &src, RawTensor &dst, FixedPointOp op);
316
317private:
318 ReferenceCPP() = delete;
319 ~ReferenceCPP() = delete;
320};
321} // namespace validation
322} // namespace test
323} // namespace arm_compute
Anthony Barbierac69aa12017-07-03 17:39:37 +0100324#endif /* __ARM_COMPUTE_TEST_REFERENCE_REFERENCE_CPP_H__ */