blob: be5a73389671dffd54c986a924d946aa08aa43bd [file] [log] [blame]
Anthony Barbierdbdab852017-06-23 15:42:00 +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:
45 /** Function to compute the integral image of a tensor.
46 *
47 * @param[in] src Input tensor.
48 * @param[out] dst Result tensor.
49 */
50 static void integral_image(const RawTensor &src, RawTensor &dst);
51 /** Function to compute the absolute difference between two tensors.
52 *
53 * @param[in] src1 First tensor.
54 * @param[in] src2 Second tensor.
55 * @param[out] dst Result tensor.
56 */
57 static void absolute_difference(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
58 /** Function to accumulate an input tensor into an output tensor.
59 *
60 * @param[in] src Input tensor.
61 * @param[in, out] dst Result tensor.
62 */
63 static void accumulate(const RawTensor &src, RawTensor &dst);
64 /** Function to accumulate a squared value from an input tensor to an output tensor.
65 *
66 * @param[in] src Input tensor.
67 * @param[in, out] dst Result tensor.
68 * @param[in] shift A uint32_t value within the range of [0, 15]
69 */
70 static void accumulate_squared(const RawTensor &src, RawTensor &dst, uint32_t shift);
71 /** Function to accumulate a weighted value from an input tensor to an output tensor.
72 *
73 * @param[in] src Input tensor.
74 * @param[in, out] dst Result tensor.
75 * @param[in] alpha A float value within the range of [0, 1]
76 */
77 static void accumulate_weighted(const RawTensor &src, RawTensor &dst, float alpha);
78 /** Arithmetic addition of @p src1 and @p src2
79 *
80 * @param[in] src1 First tensor.
81 * @param[in] src2 Second tensor.
82 * @param[out] dst Result tensor.
83 * @param[in] convert_policy Overflow policy.
84 */
85 static void arithmetic_addition(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy);
86 /** Arithmetic subtraction of @p src2 from @p src1
87 *
88 * @param[in] src1 First tensor.
89 * @param[in] src2 Second tensor.
90 * @param[out] dst Result tensor.
91 * @param[in] convert_policy Overflow policy.
92 */
93 static void arithmetic_subtraction(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy);
94 /** Function to compute the bitwise and between two tensors.
95 *
96 * @param[in] src1 First tensor.
97 * @param[in] src2 Second tensor.
98 * @param[out] dst Result tensor.
99 */
100 static void bitwise_and(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
101 /** Function to compute the bitwise or between two tensors.
102 *
103 * @param[in] src1 First tensor.
104 * @param[in] src2 Second tensor.
105 * @param[out] dst Result tensor.
106 */
107 static void bitwise_or(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
108 /** Function to compute the bitwise xor between two tensors.
109 *
110 * @param[in] src1 First tensor.
111 * @param[in] src2 Second tensor.
112 * @param[out] dst Result tensor.
113 */
114 static void bitwise_xor(const RawTensor &src1, const RawTensor &src2, RawTensor &dst);
115 /** Function to compute the bitwise not of a tensor.
116 *
117 * @param[in] src Input tensor.
118 * @param[out] dst Result tensor.
119 */
120 static void bitwise_not(const RawTensor &src, RawTensor &dst);
121 /** Function to compute 3-by-3 box filtered result tensor.
122 *
123 * @param[in] src Input tensor.
124 * @param[out] dst Result tensor.
125 */
126 static void box3x3(const RawTensor &src, RawTensor &dst);
127 /** Depth conversion from @p src to @p dst
128 *
129 * @param[in] src First tensor.
130 * @param[out] dst Result tensor.
131 * @param[in] policy Overflow policy.
132 * @param[in] shift Value for down/up conversions.
133 */
134 static void depth_convert(const RawTensor &src, RawTensor &dst, ConvertPolicy policy, uint32_t shift);
135 /** Compute GEMM function.
136 *
137 * @param[in] src1 First input tensor
138 * @param[in] src2 Second input tensor
139 * @param[in] src3 Third input tensor
140 * @param[out] dst Output tensr
141 * @param[in] alpha Weight of the matrix product
142 * @param[in] beta Weight of the third matrix
143 */
144 static void gemm(const RawTensor &src1, const RawTensor &src2, const RawTensor &src3,
145 RawTensor &dst, float alpha, float beta);
146 /** Element-wise multiplication of @p src1, @p src2 and @p scale
147 *
148 * @param[in] src1 First tensor.
149 * @param[in] src2 Second tensor.
150 * @param[out] dst Result tensor.
151 * @param[in] scale A non-negative float multiplied to each product.
152 * @param[in] convert_policy Overflow policy.
153 * @param[in] rounding_policy Rounding policy.
154 */
155 static void pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
156 /** Fixed-point Pixel-wise multiplication of @p src1 by @p src2
157 *
158 * @param[in] src1 First tensor.
159 * @param[in] src2 Second tensor.
160 * @param[out] dst Result tensor.
161 * @param[in] scale A non-negative float multiplied to each product.
162 * @param[in] convert_policy Overflow policy.
163 * @param[in] rounding_policy Rounding policy.
164 */
165 static void fixed_point_pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy);
166 /** Threshold of@p src to @p dst
167 *
168 * @param[in] src First tensor.
169 * @param[out] dst Result tensor.
170 * @param[in] threshold Threshold. When the threhold type is RANGE, this is used as the lower threshold.
171 * @param[in] false_value value to set when the condition is not respected.
172 * @param[in] true_value value to set when the condition is respected.
173 * @param[in] type Thresholding type. Either RANGE or BINARY.
174 * @param[in] upper Upper threshold. Only used when the thresholding type is RANGE.
175 */
176 static void threshold(const RawTensor &src, RawTensor &dst, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper);
177 /** Activation layer of @p src base on information from @p act_info.
178 *
179 * @param[in] input Input tensor.
180 * @param[in] output Second tensor.
181 * @param[out] act_info Activation layer information.
182 */
183 static void activation_layer(const RawTensor &input, RawTensor &output, ActivationLayerInfo act_info);
184 /** Batch Normalization of @p src based on the information from @p norm_info.
185 *
186 * @param[in] src Input tensor.
187 * @param[out] dst Result tensor.
188 * @param[out] mean Mean vector tensor.
189 * @param[out] var Var vector tensor.
190 * @param[out] beta Beta vector tensor.
191 * @param[out] gamma Gamma vector tensor.
192 * @param[in] epsilon Small value to avoid division with zero.
193 * @param[in] fixed_point_position Fixed point position.
194 */
195 static void batch_normalization_layer(const RawTensor &src, RawTensor &dst, const RawTensor &mean, const RawTensor &var, const RawTensor &beta, const RawTensor &gamma, float epsilon,
196 int fixed_point_position = 0);
197 /** Convolution layer function
198 *
199 * @param[in] src Input tensor.
200 * @param[in] weights Weights tensor.
201 * @param[in] bias Bias tensor.
202 * @param[out] dst Result tensor.
203 * @param[in] conv_info Pads and strides information for the convolution layer.
204 */
205 static void convolution_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst, const PadStrideInfo &conv_info);
206 /** Fully connected layer function
207 *
208 * @param[in] src Input tensor
209 * @param[in] weights Weights tensor.
210 * @param[in] bias Bias tensor.
211 * @param[out] dst Result tensor.
212 */
213 static void fully_connected_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst);
214 /** Normalization of @p src based on the information from @p norm_info.
215 *
216 * @param[in] src Input tensor.
217 * @param[out] dst Result tensor.
218 * @param[in] norm_info Normalization Layer information.
219 */
220 static void normalization_layer(const RawTensor &src, RawTensor &dst, NormalizationLayerInfo norm_info);
221 /** Pooling layer of @p src based on the information from @p norm_info.
222 *
223 * @param[in] src Input tensor.
224 * @param[out] dst Result tensor.
225 * @param[in] pool_info Pooling Layer information.
226 * @param[in] fixed_point_position Fixed point position. (Optional)
227 */
228 static void pooling_layer(const RawTensor &src, RawTensor &dst, PoolingLayerInfo pool_info, int fixed_point_position = 0);
229 /** Softmax Layer of @p src.
230 *
231 * @param[in] src Input tensor.
232 * @param[out] dst Result tensor.
233 */
234 static void softmax_layer(const RawTensor &src, RawTensor &dst);
235 /** Fixed point operations of @p src
236 *
237 * @param[in] src Input tensor.
238 * @param[out] dst Result tensor.
239 * @param[in] op Fixed point operation to perform.
240 */
241 static void fixed_point_operation(const RawTensor &src, RawTensor &dst, FixedPointOp op);
242
243private:
244 ReferenceCPP() = delete;
245 ~ReferenceCPP() = delete;
246};
247} // namespace validation
248} // namespace test
249} // namespace arm_compute
250#endif