blob: ddb84835c3fc30a5f22516b06e0c4a7e0ce4add5 [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{
51// Absolute difference
52void ReferenceCPP::absolute_difference(const RawTensor &src1, const RawTensor &src2, RawTensor &dst)
53{
54 const TensorVariant s1 = TensorFactory::get_tensor(src1);
55 const TensorVariant s2 = TensorFactory::get_tensor(src2);
56 TensorVariant d = TensorFactory::get_tensor(dst);
57 boost::apply_visitor(absolute_difference_visitor(), s1, s2, d);
58}
59// Integral image
60void ReferenceCPP::integral_image(const RawTensor &src, RawTensor &dst)
61{
62 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U32);
63 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
64 Tensor<uint32_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint32_t *>(dst.data()));
65 tensor_operations::integral_image(s, d);
66}
67// Accumulate
68void ReferenceCPP::accumulate(const RawTensor &src, RawTensor &dst)
69{
70 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::S16);
71 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
72 Tensor<int16_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<int16_t *>(dst.data()));
73 tensor_operations::accumulate(s, d);
74}
75
76// Accumulate squared
77void ReferenceCPP::accumulate_squared(const RawTensor &src, RawTensor &dst, uint32_t shift)
78{
79 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::S16);
80 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
81 Tensor<int16_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<int16_t *>(dst.data()));
82 tensor_operations::accumulate_squared(s, d, shift);
83}
84
85// Accumulate weighted
86void ReferenceCPP::accumulate_weighted(const RawTensor &src, RawTensor &dst, float alpha)
87{
88 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
89 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
90 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
91 tensor_operations::accumulate_weighted(s, d, alpha);
92}
93
94// Arithmetic addition
95void ReferenceCPP::arithmetic_addition(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy)
96{
97 const TensorVariant s1 = TensorFactory::get_tensor(src1);
98 const TensorVariant s2 = TensorFactory::get_tensor(src2);
99 TensorVariant d = TensorFactory::get_tensor(dst);
100 boost::apply_visitor(arithmetic_addition_visitor(convert_policy), s1, s2, d);
101}
102
103// Arithmetic subtraction
104void ReferenceCPP::arithmetic_subtraction(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, ConvertPolicy convert_policy)
105{
106 const TensorVariant s1 = TensorFactory::get_tensor(src1);
107 const TensorVariant s2 = TensorFactory::get_tensor(src2);
108 TensorVariant d = TensorFactory::get_tensor(dst);
109 boost::apply_visitor(arithmetic_subtraction_visitor(convert_policy), s1, s2, d);
110}
111
112// Bitwise and
113void ReferenceCPP::bitwise_and(const RawTensor &src1, const RawTensor &src2, RawTensor &dst)
114{
115 ARM_COMPUTE_ERROR_ON(src1.data_type() != DataType::U8 || src2.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
116 const Tensor<uint8_t> s1(src1.shape(), src1.data_type(), src1.fixed_point_position(), reinterpret_cast<const uint8_t *>(src1.data()));
117 const Tensor<uint8_t> s2(src2.shape(), src2.data_type(), src2.fixed_point_position(), reinterpret_cast<const uint8_t *>(src2.data()));
118 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
119 tensor_operations::bitwise_and(s1, s2, d);
120}
121
122// Bitwise or
123void ReferenceCPP::bitwise_or(const RawTensor &src1, const RawTensor &src2, RawTensor &dst)
124{
125 ARM_COMPUTE_ERROR_ON(src1.data_type() != DataType::U8 || src2.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
126 const Tensor<uint8_t> s1(src1.shape(), src1.data_type(), src1.fixed_point_position(), reinterpret_cast<const uint8_t *>(src1.data()));
127 const Tensor<uint8_t> s2(src2.shape(), src2.data_type(), src2.fixed_point_position(), reinterpret_cast<const uint8_t *>(src2.data()));
128 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
129 tensor_operations::bitwise_or(s1, s2, d);
130}
131
132// Bitwise xor
133void ReferenceCPP::bitwise_xor(const RawTensor &src1, const RawTensor &src2, RawTensor &dst)
134{
135 ARM_COMPUTE_ERROR_ON(src1.data_type() != DataType::U8 || src2.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
136 const Tensor<uint8_t> s1(src1.shape(), src1.data_type(), src1.fixed_point_position(), reinterpret_cast<const uint8_t *>(src1.data()));
137 const Tensor<uint8_t> s2(src2.shape(), src2.data_type(), src2.fixed_point_position(), reinterpret_cast<const uint8_t *>(src2.data()));
138 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
139 tensor_operations::bitwise_xor(s1, s2, d);
140}
141
142// Bitwise not
143void ReferenceCPP::bitwise_not(const RawTensor &src, RawTensor &dst)
144{
145 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
146 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
147 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
148 tensor_operations::bitwise_not(s, d);
149}
150
151// 3-by-3 box filter
152void ReferenceCPP::box3x3(const RawTensor &src, RawTensor &dst)
153{
154 ARM_COMPUTE_ERROR_ON(src.data_type() != DataType::U8 || dst.data_type() != DataType::U8);
155 const Tensor<uint8_t> s(src.shape(), src.data_type(), src.fixed_point_position(), reinterpret_cast<const uint8_t *>(src.data()));
156 Tensor<uint8_t> d(dst.shape(), dst.data_type(), dst.fixed_point_position(), reinterpret_cast<uint8_t *>(dst.data()));
157 tensor_operations::box3x3(s, d);
158}
159
160// Depth conversion
161void ReferenceCPP::depth_convert(const RawTensor &src, RawTensor &dst, ConvertPolicy policy, uint32_t shift)
162{
163 const TensorVariant s = TensorFactory::get_tensor(src);
164 TensorVariant d = TensorFactory::get_tensor(dst);
165 boost::apply_visitor(tensor_visitors::depth_convert_visitor(policy, shift), s, d);
166}
167
168// GEMM
169void ReferenceCPP::gemm(const RawTensor &src1, const RawTensor &src2, const RawTensor &src3,
170 RawTensor &dst, float alpha, float beta)
171{
172 const TensorVariant s1 = TensorFactory::get_tensor(src1);
173 const TensorVariant s2 = TensorFactory::get_tensor(src2);
174 const TensorVariant s3 = TensorFactory::get_tensor(src3);
175 TensorVariant d = TensorFactory::get_tensor(dst);
176
177 boost::apply_visitor(tensor_visitors::gemm_visitor(s1, s2, s3, alpha, beta), d);
178}
179
180// Pixel-wise multiplication
181void ReferenceCPP::pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
182{
183 const TensorVariant s1 = TensorFactory::get_tensor(src1);
184 const TensorVariant s2 = TensorFactory::get_tensor(src2);
185 TensorVariant d = TensorFactory::get_tensor(dst);
186 boost::apply_visitor(pixel_wise_multiplication_visitor(scale, convert_policy, rounding_policy), s1, s2, d);
187}
188
189// Fixed-point Pixel-wise multiplication
190void ReferenceCPP::fixed_point_pixel_wise_multiplication(const RawTensor &src1, const RawTensor &src2, RawTensor &dst, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
191{
192 const TensorVariant s1 = TensorFactory::get_tensor(src1);
193 const TensorVariant s2 = TensorFactory::get_tensor(src2);
194 TensorVariant d = TensorFactory::get_tensor(dst);
195 boost::apply_visitor(tensor_visitors::fixed_point_pixel_wise_multiplication_visitor(s1, s2, scale, convert_policy, rounding_policy), d);
196}
197
198// Threshold
199void ReferenceCPP::threshold(const RawTensor &src, RawTensor &dst, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper)
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 threshold_operation(s, d, threshold, false_value, true_value, type, upper);
205}
206
207// Activation layer
208void ReferenceCPP::activation_layer(const RawTensor &input, RawTensor &output, ActivationLayerInfo act_info)
209{
210 const TensorVariant s = TensorFactory::get_tensor(input);
211 TensorVariant d = TensorFactory::get_tensor(output);
212 boost::apply_visitor(tensor_visitors::activation_layer_visitor(s, act_info), d);
213}
214
215// Batch Normalization Layer
216void ReferenceCPP::batch_normalization_layer(const RawTensor &src, RawTensor &dst, const RawTensor &mean, const RawTensor &var, const RawTensor &beta, const RawTensor &gamma, float epsilon,
217 int fixed_point_position)
218{
219 const TensorVariant s = TensorFactory::get_tensor(src);
220 TensorVariant d = TensorFactory::get_tensor(dst);
221 const TensorVariant m = TensorFactory::get_tensor(mean);
222 const TensorVariant v = TensorFactory::get_tensor(var);
223 const TensorVariant b = TensorFactory::get_tensor(beta);
224 const TensorVariant g = TensorFactory::get_tensor(gamma);
225 boost::apply_visitor(tensor_visitors::batch_normalization_layer_visitor(s, m, v, b, g, epsilon, fixed_point_position), d);
226}
227
228// Convolution Layer
229void ReferenceCPP::convolution_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst, const PadStrideInfo &conv_info)
230{
231 const TensorVariant s = TensorFactory::get_tensor(src);
232 const TensorVariant w = TensorFactory::get_tensor(weights);
233 const TensorVariant b = TensorFactory::get_tensor(bias);
234 TensorVariant d = TensorFactory::get_tensor(dst);
235 boost::apply_visitor(tensor_visitors::convolution_layer_visitor(s, w, b, conv_info), d);
236}
237
238// Fully connected layer
239void ReferenceCPP::fully_connected_layer(const RawTensor &src, const RawTensor &weights, const RawTensor &bias, RawTensor &dst)
240{
241 const TensorVariant s = TensorFactory::get_tensor(src);
242 const TensorVariant w = TensorFactory::get_tensor(weights);
243 const TensorVariant b = TensorFactory::get_tensor(bias);
244 TensorVariant d = TensorFactory::get_tensor(dst);
245 boost::apply_visitor(tensor_visitors::fully_connected_layer_visitor(s, w, b), d);
246}
247
248// Normalization Layer
249void ReferenceCPP::normalization_layer(const RawTensor &src, RawTensor &dst, NormalizationLayerInfo norm_info)
250{
251 const TensorVariant s = TensorFactory::get_tensor(src);
252 TensorVariant d = TensorFactory::get_tensor(dst);
253 boost::apply_visitor(tensor_visitors::normalization_layer_visitor(s, norm_info), d);
254}
255
256// Pooling Layer
257void ReferenceCPP::pooling_layer(const RawTensor &src, RawTensor &dst, PoolingLayerInfo pool_info, int fixed_point_position)
258{
259 const TensorVariant s = TensorFactory::get_tensor(src);
260 TensorVariant d = TensorFactory::get_tensor(dst);
261 boost::apply_visitor(tensor_visitors::pooling_layer_visitor(s, pool_info, fixed_point_position), d);
262}
263
264// Softmax Layer
265void ReferenceCPP::softmax_layer(const RawTensor &src, RawTensor &dst)
266{
267 const TensorVariant s = TensorFactory::get_tensor(src);
268 TensorVariant d = TensorFactory::get_tensor(dst);
269 boost::apply_visitor(tensor_visitors::softmax_layer_visitor(s), d);
270}
271
272// Fixed point operation
273void ReferenceCPP::fixed_point_operation(const RawTensor &src, RawTensor &dst, FixedPointOp op)
274{
275 const TensorVariant s = TensorFactory::get_tensor(src);
276 TensorVariant d = TensorFactory::get_tensor(dst);
277 boost::apply_visitor(tensor_visitors::fixed_point_operation_visitor(s, op), d);
278}
279
280} // namespace validation
281} // namespace test
282} // namespace arm_compute