blob: 06606c70752d6208b2478def26cc78eb72e34bfc [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#include "ReferenceCPP.h"
25
26#include "TensorFactory.h"
27#include "TensorOperations.h"
28#include "TensorVisitors.h"
29#include "TypePrinter.h"
30
31#include "arm_compute/core/Coordinates.h"
32#include "arm_compute/core/Error.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/TensorShape.h"
35#include "arm_compute/runtime/Tensor.h"
36
37#include "boost_wrapper.h"
38
39#include <functional>
40#include <numeric>
41#include <vector>
42
43using namespace arm_compute::test::validation::tensor_visitors;
44
45namespace arm_compute
46{
47namespace test
48{
49namespace validation
50{
Giorgio Arena50f9fd72017-06-19 17:05:30 +010051// Sobel 3x3
52void ReferenceCPP::sobel_3x3(RawTensor &src, RawTensor &dst_x, RawTensor &dst_y, BorderMode border_mode, uint8_t constant_border_value)
53{
54 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst_x.data_type() != DataType::S16 || dst_y.data_type() != DataType::S16);
55 Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
56 Tensor<int16_t> dx(dst_x.shape(), dst_x.data_type(), dst_x.fixed_point_position(), reinterpret_cast<int16_t *>(dst_x.data()));
57 Tensor<int16_t> dy(dst_y.shape(), dst_y.data_type(), dst_y.fixed_point_position(), reinterpret_cast<int16_t *>(dst_y.data()));
58 tensor_operations::sobel_3x3(s, dx, dy, border_mode, constant_border_value);
59}
60
61// Sobel 5x5
62void ReferenceCPP::sobel_5x5(RawTensor &src, RawTensor &dst_x, RawTensor &dst_y, BorderMode border_mode, uint8_t constant_border_value)
63{
64 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst_x.data_type() != DataType::S16 || dst_y.data_type() != DataType::S16);
65 Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
66 Tensor<int16_t> dx(dst_x.shape(), dst_x.data_type(), dst_x.fixed_point_position(), reinterpret_cast<int16_t *>(dst_x.data()));
67 Tensor<int16_t> dy(dst_y.shape(), dst_y.data_type(), dst_y.fixed_point_position(), reinterpret_cast<int16_t *>(dst_y.data()));
68 tensor_operations::sobel_5x5(s, dx, dy, border_mode, constant_border_value);
69}
70
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071// Absolute difference
72void ReferenceCPP::absolute_difference(const RawTensor &src1, const RawTensor &src2, RawTensor &dst)
73{
74 const TensorVariant s1 = TensorFactory::get_tensor(src1);
75 const TensorVariant s2 = TensorFactory::get_tensor(src2);
76 TensorVariant d = TensorFactory::get_tensor(dst);
77 boost::apply_visitor(absolute_difference_visitor(), s1, s2, d);
78}
Giorgio Arenaf7959862017-06-13 15:19:51 +010079
80// Mean and standard deviation
81void ReferenceCPP::mean_and_standard_deviation(const RawTensor &src, float &mean, float &std_dev)
82{
83 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8);
84 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
85 tensor_operations::mean_and_standard_deviation(s, mean, std_dev);
86}
87
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088// Integral image
89void ReferenceCPP::integral_image(const RawTensor &src, RawTensor &dst)
90{
91 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U32);
92 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
93 Tensor<uint32_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint32_t *>(dst.data()));
94 tensor_operations::integral_image(s, d);
95}
Giorgio Arenaf7959862017-06-13 15:19:51 +010096
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097// Accumulate
98void ReferenceCPP::accumulate(const RawTensor &src, RawTensor &dst)
99{
100 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::S16);
101 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
102 Tensor<int16_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<int16_t *>(dst.data()));
103 tensor_operations::accumulate(s, d);
104}
105
106// Accumulate squared
107void ReferenceCPP::accumulate_squared(const RawTensor &src, RawTensor &dst, uint32_t shift)
108{
109 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::S16);
110 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
111 Tensor<int16_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<int16_t *>(dst.data()));
112 tensor_operations::accumulate_squared(s, d, shift);
113}
114
115// Accumulate weighted
116void ReferenceCPP::accumulate_weighted(const RawTensor &src, RawTensor &dst, float alpha)
117{
118 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
119 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
120 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
121 tensor_operations::accumulate_weighted(s, d, alpha);
122}
123
124// Arithmetic addition
125void ReferenceCPP::arithmetic_addition(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy)
126{
127 const TensorVariant s1 = TensorFactory::get_tensor(src1);
128 const TensorVariant s2 = TensorFactory::get_tensor(src2);
129 TensorVariant d = TensorFactory::get_tensor(dst);
130 boost::apply_visitor(arithmetic_addition_visitor(convert_policy), s1, s2, d);
131}
132
133// Arithmetic subtraction
134void ReferenceCPP::arithmetic_subtraction(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy)
135{
136 const TensorVariant s1 = TensorFactory::get_tensor(src1);
137 const TensorVariant s2 = TensorFactory::get_tensor(src2);
138 TensorVariant d = TensorFactory::get_tensor(dst);
139 boost::apply_visitor(arithmetic_subtraction_visitor(convert_policy), s1, s2, d);
140}
141
142// Bitwise and
143void ReferenceCPP::bitwise_and(const RawTensor &src1, const RawTensor &src2, RawTensor &dst)
144{
145 ARM_COMPUTE_ERROR_ON(src1.data_type() != DataType::U8 || src2.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
146 const Tensor<uint8_t> s1(src1.shape(), src1.data_type(), src1.fixed_point_position(), reinterpret_cast<const uint8_t *>(src1.data()));
147 const Tensor<uint8_t> s2(src2.shape(), src2.data_type(), src2.fixed_point_position(), reinterpret_cast<const uint8_t *>(src2.data()));
148 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
149 tensor_operations::bitwise_and(s1, s2, d);
150}
151
152// Bitwise or
153void ReferenceCPP::bitwise_or(const RawTensor &src1, const RawTensor &src2, RawTensor &dst)
154{
155 ARM_COMPUTE_ERROR_ON(src1.data_type() != DataType::U8 || src2.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
156 const Tensor<uint8_t> s1(src1.shape(), src1.data_type(), src1.fixed_point_position(), reinterpret_cast<const uint8_t *>(src1.data()));
157 const Tensor<uint8_t> s2(src2.shape(), src2.data_type(), src2.fixed_point_position(), reinterpret_cast<const uint8_t *>(src2.data()));
158 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
159 tensor_operations::bitwise_or(s1, s2, d);
160}
161
162// Bitwise xor
163void ReferenceCPP::bitwise_xor(const RawTensor &src1, const RawTensor &src2, RawTensor &dst)
164{
165 ARM_COMPUTE_ERROR_ON(src1.data_type() != DataType::U8 || src2.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
166 const Tensor<uint8_t> s1(src1.shape(), src1.data_type(), src1.fixed_point_position(), reinterpret_cast<const uint8_t *>(src1.data()));
167 const Tensor<uint8_t> s2(src2.shape(), src2.data_type(), src2.fixed_point_position(), reinterpret_cast<const uint8_t *>(src2.data()));
168 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
169 tensor_operations::bitwise_xor(s1, s2, d);
170}
171
172// Bitwise not
173void ReferenceCPP::bitwise_not(const RawTensor &src, RawTensor &dst)
174{
175 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
176 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
177 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
178 tensor_operations::bitwise_not(s, d);
179}
180
SiCong Libacaf9a2017-06-19 13:41:45 +0100181// Box3x3 filter
182void ReferenceCPP::box3x3(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100183{
184 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
185 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
186 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
SiCong Libacaf9a2017-06-19 13:41:45 +0100187 tensor_operations::box3x3(s, d, border_mode, constant_border_value);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188}
189
190// Depth conversion
191void ReferenceCPP::depth_convert(const RawTensor &src, RawTensor &dst, ConvertPolicy policy, uint32_t shift)
192{
193 const TensorVariant s = TensorFactory::get_tensor(src);
194 TensorVariant d = TensorFactory::get_tensor(dst);
195 boost::apply_visitor(tensor_visitors::depth_convert_visitor(policy, shift), s, d);
196}
197
SiCong Li5a536642017-06-19 14:47:05 +0100198// Gaussian3x3 filter
199void ReferenceCPP::gaussian3x3(const RawTensor &src, RawTensor &dst, BorderMode border_mode, uint8_t constant_border_value)
200{
201 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
202 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
203 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
204 tensor_operations::gaussian3x3(s, d, border_mode, constant_border_value);
205}
206
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100207// GEMM
208void ReferenceCPP::gemm(const RawTensor &src1, const RawTensor &src2, const RawTensor &src3,
209 RawTensor &dst, float alpha, float beta)
210{
211 const TensorVariant s1 = TensorFactory::get_tensor(src1);
212 const TensorVariant s2 = TensorFactory::get_tensor(src2);
213 const TensorVariant s3 = TensorFactory::get_tensor(src3);
214 TensorVariant d = TensorFactory::get_tensor(dst);
215
216 boost::apply_visitor(tensor_visitors::gemm_visitor(s1, s2, s3, alpha, beta), d);
217}
218
219// Pixel-wise multiplication
220void ReferenceCPP::pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
221{
222 const TensorVariant s1 = TensorFactory::get_tensor(src1);
223 const TensorVariant s2 = TensorFactory::get_tensor(src2);
224 TensorVariant d = TensorFactory::get_tensor(dst);
225 boost::apply_visitor(pixel_wise_multiplication_visitor(scale, convert_policy, rounding_policy), s1, s2, d);
226}
227
228// Fixed-point Pixel-wise multiplication
229void ReferenceCPP::fixed_point_pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
230{
231 const TensorVariant s1 = TensorFactory::get_tensor(src1);
232 const TensorVariant s2 = TensorFactory::get_tensor(src2);
233 TensorVariant d = TensorFactory::get_tensor(dst);
234 boost::apply_visitor(tensor_visitors::fixed_point_pixel_wise_multiplication_visitor(s1, s2, scale, convert_policy, rounding_policy), d);
235}
236
237// Threshold
238void ReferenceCPP::threshold(const RawTensor &src, RawTensor &dst, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper)
239{
240 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
241 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
242 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
243 threshold_operation(s, d, threshold, false_value, true_value, type, upper);
244}
245
246// Activation layer
247void ReferenceCPP::activation_layer(const RawTensor &input, RawTensor &output, ActivationLayerInfo act_info)
248{
249 const TensorVariant s = TensorFactory::get_tensor(input);
250 TensorVariant d = TensorFactory::get_tensor(output);
251 boost::apply_visitor(tensor_visitors::activation_layer_visitor(s, act_info), d);
252}
253
254// Batch Normalization Layer
255void ReferenceCPP::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)
257{
258 const TensorVariant s = TensorFactory::get_tensor(src);
259 TensorVariant d = TensorFactory::get_tensor(dst);
260 const TensorVariant m = TensorFactory::get_tensor(mean);
261 const TensorVariant v = TensorFactory::get_tensor(var);
262 const TensorVariant b = TensorFactory::get_tensor(beta);
263 const TensorVariant g = TensorFactory::get_tensor(gamma);
264 boost::apply_visitor(tensor_visitors::batch_normalization_layer_visitor(s, m, v, b, g, epsilon, fixed_point_position), d);
265}
266
267// Convolution Layer
268void ReferenceCPP::convolution_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst, const PadStrideInfo &conv_info)
269{
270 const TensorVariant s = TensorFactory::get_tensor(src);
271 const TensorVariant w = TensorFactory::get_tensor(weights);
272 const TensorVariant b = TensorFactory::get_tensor(bias);
273 TensorVariant d = TensorFactory::get_tensor(dst);
274 boost::apply_visitor(tensor_visitors::convolution_layer_visitor(s, w, b, conv_info), d);
275}
276
277// Fully connected layer
278void ReferenceCPP::fully_connected_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst)
279{
280 const TensorVariant s = TensorFactory::get_tensor(src);
281 const TensorVariant w = TensorFactory::get_tensor(weights);
282 const TensorVariant b = TensorFactory::get_tensor(bias);
283 TensorVariant d = TensorFactory::get_tensor(dst);
284 boost::apply_visitor(tensor_visitors::fully_connected_layer_visitor(s, w, b), d);
285}
286
287// Normalization Layer
288void ReferenceCPP::normalization_layer(const RawTensor &src, RawTensor &dst, NormalizationLayerInfo norm_info)
289{
290 const TensorVariant s = TensorFactory::get_tensor(src);
291 TensorVariant d = TensorFactory::get_tensor(dst);
292 boost::apply_visitor(tensor_visitors::normalization_layer_visitor(s, norm_info), d);
293}
294
295// Pooling Layer
296void ReferenceCPP::pooling_layer(const RawTensor &src, RawTensor &dst, PoolingLayerInfo pool_info, int fixed_point_position)
297{
298 const TensorVariant s = TensorFactory::get_tensor(src);
299 TensorVariant d = TensorFactory::get_tensor(dst);
300 boost::apply_visitor(tensor_visitors::pooling_layer_visitor(s, pool_info, fixed_point_position), d);
301}
302
303// Softmax Layer
304void ReferenceCPP::softmax_layer(const RawTensor &src, RawTensor &dst)
305{
306 const TensorVariant s = TensorFactory::get_tensor(src);
307 TensorVariant d = TensorFactory::get_tensor(dst);
308 boost::apply_visitor(tensor_visitors::softmax_layer_visitor(s), d);
309}
310
311// Fixed point operation
312void ReferenceCPP::fixed_point_operation(const RawTensor &src, RawTensor &dst, FixedPointOp op)
313{
314 const TensorVariant s = TensorFactory::get_tensor(src);
315 TensorVariant d = TensorFactory::get_tensor(dst);
316 boost::apply_visitor(tensor_visitors::fixed_point_operation_visitor(s, op), d);
317}
318
319} // namespace validation
320} // namespace test
321} // namespace arm_compute