blob: e7567aea9572c31b07763bbaac59d6fc06d60ab6 [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
31#include <ostream>
Georgios Pinitasd4f8c272017-06-30 16:16:19 +010032#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033
34namespace arm_compute
35{
36class Tensor;
37
38namespace test
39{
40namespace validation
41{
42/** C++ reference implementation. */
43class ReferenceCPP final : public Reference
44{
45public:
Giorgio Arena50f9fd72017-06-19 17:05:30 +010046 /** Function to compute reference sobel 3x3.
47 *
48 * @param[in] src Input tensor.
49 * @param[in] dst_x Result tensor along x axis
50 * @param[in] dst_y Result tensor along y axis
51 * @param[in] border_mode Border mode to use for input tensor
52 * @param[in] constant_border_value Constant value to use if @p border_mode is constant
53 *
54 */
55 static void sobel_3x3(RawTensor &src, RawTensor &dst_x, RawTensor &dst_y, BorderMode border_mode, uint8_t constant_border_value);
56 /** Function to compute reference sobel 5x5.
57 *
58 * @param[in] src Input tensor.
59 * @param[in] dst_x Result tensor along x axis
60 * @param[in] dst_y Result tensor along y axis
61 * @param[in] border_mode Border mode to use for input tensor
62 * @param[in] constant_border_value Constant value to use if @p border_mode is constant
63 *
64 */
65 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 +010066 /** Function to compute the mean and standard deviation of a tensor.
67 *
68 * @param[in] src Input tensor.
69 * @param[out] mean Mean of the tensor.
70 * @param[out] std_dev Standard deviation of the tensor
71 */
72 static void mean_and_standard_deviation(const RawTensor &src, float &mean, float &std_dev);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010073 /** Function to compute the integral image of a tensor.
74 *
75 * @param[in] src Input tensor.
76 * @param[out] dst Result tensor.
77 */
78 static void integral_image(const RawTensor &src, RawTensor &dst);
79 /** Function to compute the absolute difference between two tensors.
80 *
81 * @param[in] src1 First tensor.
82 * @param[in] src2 Second tensor.
83 * @param[out] dst Result tensor.
84 */
85 static void absolute_difference(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
86 /** Function to accumulate an input tensor into an output tensor.
87 *
88 * @param[in] src Input tensor.
89 * @param[in, out] dst Result tensor.
90 */
91 static void accumulate(const RawTensor &src, RawTensor &dst);
92 /** Function to accumulate a squared value from an input tensor to an output tensor.
93 *
94 * @param[in] src Input tensor.
95 * @param[in, out] dst Result tensor.
96 * @param[in] shift A uint32_t value within the range of [0, 15]
97 */
98 static void accumulate_squared(const RawTensor &src, RawTensor &dst, uint32_t shift);
99 /** Function to accumulate a weighted value from an input tensor to an output tensor.
100 *
101 * @param[in] src Input tensor.
102 * @param[in, out] dst Result tensor.
103 * @param[in] alpha A float value within the range of [0, 1]
104 */
105 static void accumulate_weighted(const RawTensor &src, RawTensor &dst, float alpha);
106 /** Arithmetic addition of @p src1 and @p src2
107 *
108 * @param[in] src1 First tensor.
109 * @param[in] src2 Second tensor.
110 * @param[out] dst Result tensor.
111 * @param[in] convert_policy Overflow policy.
112 */
113 static void arithmetic_addition(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy);
114 /** Arithmetic subtraction of @p src2 from @p src1
115 *
116 * @param[in] src1 First tensor.
117 * @param[in] src2 Second tensor.
118 * @param[out] dst Result tensor.
119 * @param[in] convert_policy Overflow policy.
120 */
121 static void arithmetic_subtraction(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy);
122 /** Function to compute the bitwise and between two tensors.
123 *
124 * @param[in] src1 First tensor.
125 * @param[in] src2 Second tensor.
126 * @param[out] dst Result tensor.
127 */
128 static void bitwise_and(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
129 /** Function to compute the bitwise or between two tensors.
130 *
131 * @param[in] src1 First tensor.
132 * @param[in] src2 Second tensor.
133 * @param[out] dst Result tensor.
134 */
135 static void bitwise_or(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
136 /** Function to compute the bitwise xor between two tensors.
137 *
138 * @param[in] src1 First tensor.
139 * @param[in] src2 Second tensor.
140 * @param[out] dst Result tensor.
141 */
142 static void bitwise_xor(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
143 /** Function to compute the bitwise not of a tensor.
144 *
145 * @param[in] src Input tensor.
146 * @param[out] dst Result tensor.
147 */
148 static void bitwise_not(const RawTensor &src, RawTensor &dst);
SiCong Libacaf9a2017-06-19 13:41:45 +0100149 /** Function to compute box3x3 filtered result tensor.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150 *
SiCong Libacaf9a2017-06-19 13:41:45 +0100151 * @param[in] src Input tensor.
152 * @param[out] dst Result tensor.
153 * @param[in] border_mode Border mode.
154 * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 */
SiCong Libacaf9a2017-06-19 13:41:45 +0100156 static void box3x3(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157 /** Depth conversion from @p src to @p dst
158 *
159 * @param[in] src First tensor.
160 * @param[out] dst Result tensor.
161 * @param[in] policy Overflow policy.
162 * @param[in] shift Value for down/up conversions.
163 */
164 static void depth_convert(const RawTensor &src, RawTensor &dst, ConvertPolicy policy, uint32_t shift);
SiCong Li5a536642017-06-19 14:47:05 +0100165 /** Function to compute gaussian3x3 filtered result tensor.
166 *
167 * @param[in] src Input tensor.
168 * @param[out] dst Result tensor.
169 * @param[in] border_mode Border mode
170 * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT
171 */
172 static void gaussian3x3(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value);
SiCong Li3eb263e2017-06-19 15:31:43 +0100173 /** Function to compute gaussian5x5 filtered result tensor.
174 *
175 * @param[in] src Input tensor.
176 * @param[out] dst Result tensor.
177 * @param[in] border_mode Border mode
178 * @param[in] constant_border_value Constant border value if @p border_mode is BorderMode::CONSTANT
179 */
180 static void gaussian5x5(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181 /** Compute GEMM function.
182 *
183 * @param[in] src1 First input tensor
184 * @param[in] src2 Second input tensor
185 * @param[in] src3 Third input tensor
186 * @param[out] dst Output tensr
187 * @param[in] alpha Weight of the matrix product
188 * @param[in] beta Weight of the third matrix
189 */
190 static void gemm(const RawTensor &src1, const RawTensor &src2, const RawTensor &src3,
191 RawTensor &dst, float alpha, float beta);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100192 /** Compute non linear filter function.
193 *
194 * @param[in] src First input tensor
195 * @param[out] dst Output tensor
196 * @param[in] function Non linear function to perform
197 * @param[in] mask_size Mask size. Supported sizes: 3, 5
198 * @param[in] pattern Matrix pattern
199 * @param[in] mask The given mask.
200 * @param[in] border_mode Strategy to use for borders.
201 * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT.
202 */
203 static void non_linear_filter(const RawTensor &src, RawTensor &dst, NonLinearFilterFunction function, unsigned int mask_size,
204 MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value = 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205 /** Element-wise multiplication of @p src1, @p src2 and @p scale
206 *
207 * @param[in] src1 First tensor.
208 * @param[in] src2 Second tensor.
209 * @param[out] dst Result tensor.
210 * @param[in] scale A non-negative float multiplied to each product.
211 * @param[in] convert_policy Overflow policy.
212 * @param[in] rounding_policy Rounding policy.
213 */
214 static void pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
215 /** Fixed-point Pixel-wise multiplication of @p src1 by @p src2
216 *
217 * @param[in] src1 First tensor.
218 * @param[in] src2 Second tensor.
219 * @param[out] dst Result tensor.
220 * @param[in] scale A non-negative float multiplied to each product.
221 * @param[in] convert_policy Overflow policy.
222 * @param[in] rounding_policy Rounding policy.
223 */
224 static void fixed_point_pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
225 /** Threshold of@p src to @p dst
226 *
227 * @param[in] src First tensor.
228 * @param[out] dst Result tensor.
229 * @param[in] threshold Threshold. When the threhold type is RANGE, this is used as the lower threshold.
230 * @param[in] false_value value to set when the condition is not respected.
231 * @param[in] true_value value to set when the condition is respected.
232 * @param[in] type Thresholding type. Either RANGE or BINARY.
233 * @param[in] upper Upper threshold. Only used when the thresholding type is RANGE.
234 */
235 static void threshold(const RawTensor &src, RawTensor &dst, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper);
236 /** Activation layer of @p src base on information from @p act_info.
237 *
238 * @param[in] input Input tensor.
239 * @param[in] output Second tensor.
240 * @param[out] act_info Activation layer information.
241 */
242 static void activation_layer(const RawTensor &input, RawTensor &output, ActivationLayerInfo act_info);
243 /** Batch Normalization of @p src based on the information from @p norm_info.
244 *
245 * @param[in] src Input tensor.
246 * @param[out] dst Result tensor.
247 * @param[out] mean Mean vector tensor.
248 * @param[out] var Var vector tensor.
249 * @param[out] beta Beta vector tensor.
250 * @param[out] gamma Gamma vector tensor.
251 * @param[in] epsilon Small value to avoid division with zero.
252 * @param[in] fixed_point_position Fixed point position.
253 */
254 static void batch_normalization_layer(const RawTensor &src, RawTensor &dst, const RawTensor &mean, const RawTensor &var, const RawTensor &beta, const RawTensor &gamma, float epsilon,
255 int fixed_point_position = 0);
256 /** Convolution layer function
257 *
258 * @param[in] src Input tensor.
259 * @param[in] weights Weights tensor.
260 * @param[in] bias Bias tensor.
261 * @param[out] dst Result tensor.
262 * @param[in] conv_info Pads and strides information for the convolution layer.
263 */
264 static void convolution_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst, const PadStrideInfo &conv_info);
265 /** Fully connected layer function
266 *
267 * @param[in] src Input tensor
268 * @param[in] weights Weights tensor.
269 * @param[in] bias Bias tensor.
270 * @param[out] dst Result tensor.
271 */
272 static void fully_connected_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst);
273 /** Normalization of @p src based on the information from @p norm_info.
274 *
275 * @param[in] src Input tensor.
276 * @param[out] dst Result tensor.
277 * @param[in] norm_info Normalization Layer information.
278 */
279 static void normalization_layer(const RawTensor &src, RawTensor &dst, NormalizationLayerInfo norm_info);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100280 /** Pooling layer of @p src based on the information from @p pool_info.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100281 *
282 * @param[in] src Input tensor.
283 * @param[out] dst Result tensor.
284 * @param[in] pool_info Pooling Layer information.
285 * @param[in] fixed_point_position Fixed point position. (Optional)
286 */
287 static void pooling_layer(const RawTensor &src, RawTensor &dst, PoolingLayerInfo pool_info, int fixed_point_position = 0);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100288 /** ROI Pooling layer of @p src based on the information from @p pool_info and @p rois.
289 *
290 * @param[in] src Input tensor.
291 * @param[out] dst Result tensor.
292 * @param[in] rois Region of Interest points.
293 * @param[in] pool_info ROI Pooling Layer information.
294 */
295 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 +0100296 /** Softmax Layer of @p src.
297 *
298 * @param[in] src Input tensor.
299 * @param[out] dst Result tensor.
300 */
301 static void softmax_layer(const RawTensor &src, RawTensor &dst);
302 /** Fixed point operations of @p src
303 *
304 * @param[in] src Input tensor.
305 * @param[out] dst Result tensor.
306 * @param[in] op Fixed point operation to perform.
307 */
308 static void fixed_point_operation(const RawTensor &src, RawTensor &dst, FixedPointOp op);
309
310private:
311 ReferenceCPP() = delete;
312 ~ReferenceCPP() = delete;
313};
314} // namespace validation
315} // namespace test
316} // namespace arm_compute
317#endif