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