blob: aa73869a0efa3ab59aa0cabd7ce7139eed9146b8 [file] [log] [blame]
Moritz Pflanzerb3d25792017-07-26 11:49:37 +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 "ConvolutionLayer.h"
25
Chunosovd621bca2017-11-03 17:33:15 +070026#include "tests/validation/CPP/Utils.h"
27#include "tests/validation/CPP/UtilsQuantizedAsymm.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010028#include "tests/validation/FixedPoint.h"
29#include "tests/validation/Helpers.h"
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010030
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010031#include "tests/framework/Asserts.h"
32
Chunosovd621bca2017-11-03 17:33:15 +070033#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
34
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010035namespace arm_compute
36{
37namespace test
38{
39namespace validation
40{
41namespace reference
42{
43namespace
44{
45inline bool is_valid_pixel(int i, int min, int max)
46{
47 return (i >= min && i < max);
48}
49
50// 3D convolution for floating point type
51template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type = 0>
Chunosovd621bca2017-11-03 17:33:15 +070052void convolution3d(const SimpleTensor<T> &in, const SimpleTensor<T> &weights, const SimpleTensor<T> &bias, SimpleTensor<T> &out,
53 int i_offset, int w_offset, int b_offset, int o_offset,
54 int xi, int yi, int width_in, int height_in, int depth_in, int width_weights, int height_weights)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010055{
Chunosovd621bca2017-11-03 17:33:15 +070056 const T *in_ptr = in.data() + i_offset;
57 const T *w_ptr = weights.data() + w_offset;
58 const T *b_ptr = bias.data() + b_offset;
59 T *out_ptr = out.data() + o_offset;
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010060
61 const int half_width_weights = width_weights / 2;
62 const int half_height_weights = height_weights / 2;
63
64 // Reset accumulator
65 T acc(0);
66
67 // Compute a 2D convolution for each IFM and accumulate the result
68 for(int ifm = 0; ifm < depth_in; ++ifm)
69 {
70 // Compute the offset for the input slice
71 const int offset_slice_in = xi + yi * width_in + ifm * width_in * height_in;
72
73 // Compute 2D convolution
74 for(int yk = -half_height_weights; yk <= half_height_weights; ++yk)
75 {
76 for(int xk = -half_width_weights; xk <= half_width_weights; ++xk)
77 {
78 // Check if the pixel is out-of-bound
79 if(is_valid_pixel(xi + xk, 0, width_in) && is_valid_pixel(yi + yk, 0, height_in))
80 {
81 const int idx = xk + half_width_weights;
82 const int idy = yk + half_height_weights;
83
Chunosovd621bca2017-11-03 17:33:15 +070084 const T i_value = in_ptr[offset_slice_in + xk + yk * width_in];
85 const T w_value = w_ptr[idx + idy * width_weights + ifm * width_weights * height_weights];
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010086
87 acc += i_value * w_value;
88 }
89 }
90 }
91 }
92
93 // Accumulate the bias and store the result
Chunosovd621bca2017-11-03 17:33:15 +070094 *out_ptr = acc + (*b_ptr);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +010095}
96
97// 3D convolution for fixed point type
98template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
Chunosovd621bca2017-11-03 17:33:15 +070099void convolution3d(const SimpleTensor<T> &in, const SimpleTensor<T> &weights, const SimpleTensor<T> &bias, SimpleTensor<T> &out,
100 int i_offset, int w_offset, int b_offset, int o_offset,
101 int xi, int yi, int width_in, int height_in, int depth_in, int width_weights, int height_weights)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100102{
Chunosovd621bca2017-11-03 17:33:15 +0700103 const T *in_ptr = in.data() + i_offset;
104 const T *w_ptr = weights.data() + w_offset;
105 const T *b_ptr = bias.data() + b_offset;
106 T *out_ptr = out.data() + o_offset;
107 int fixed_point_position = in.fixed_point_position();
108
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100109 const int half_width_weights = width_weights / 2;
110 const int half_height_weights = height_weights / 2;
111
112 using namespace fixed_point_arithmetic;
113 using promoted_type = fixed_point_arithmetic::traits::promote_t<T>;
114
115 // Reset accumulator
116 fixed_point<promoted_type> acc(0, fixed_point_position);
117
118 // Compute a 2D convolution for each IFM and accumulate the result
119 for(int ifm = 0; ifm < depth_in; ++ifm)
120 {
121 // Compute the offset for the input slice
122 const int offset_slice_in = xi + yi * width_in + ifm * width_in * height_in;
123
124 // Compute 2D convolution
125 for(int yk = -half_height_weights; yk <= half_height_weights; ++yk)
126 {
127 for(int xk = -half_width_weights; xk <= half_width_weights; ++xk)
128 {
129 // Check if the pixel is out-of-bound
130 if(is_valid_pixel(xi + xk, 0, width_in) && is_valid_pixel(yi + yk, 0, height_in))
131 {
132 const int idx = xk + half_width_weights;
133 const int idy = yk + half_height_weights;
134
Chunosovd621bca2017-11-03 17:33:15 +0700135 const fixed_point<promoted_type> i_value(in_ptr[offset_slice_in + xk + yk * width_in], fixed_point_position, true);
136 const fixed_point<promoted_type> w_value(w_ptr[idx + idy * width_weights + ifm * width_weights * height_weights], fixed_point_position, true);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100137 const fixed_point<promoted_type> iw = i_value * w_value;
138 acc = iw + acc;
139 }
140 }
141 }
142 }
143
144 // Get the bias
Chunosovd621bca2017-11-03 17:33:15 +0700145 const fixed_point<promoted_type> b(*b_ptr, fixed_point_position, true);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100146
147 // Accumulate the bias and covert back
148 acc = acc + b;
149 fixed_point<T> res(acc);
Chunosovd621bca2017-11-03 17:33:15 +0700150 *out_ptr = res.raw();
151}
152
153// 3D convolution for QASYMM8 type
154template <>
155void convolution3d(const SimpleTensor<uint8_t> &in, const SimpleTensor<uint8_t> &weights, const SimpleTensor<uint8_t> &bias, SimpleTensor<uint8_t> &out,
156 int i_offset, int w_offset, int b_offset, int o_offset,
157 int xi, int yi, int width_in, int height_in, int depth_in, int width_weights, int height_weights)
158{
159 const uint8_t *in_ptr = in.data() + i_offset;
160 const uint8_t *w_ptr = weights.data() + w_offset;
161 const uint8_t *b_ptr = bias.data() + b_offset;
162 uint8_t *out_ptr = out.data() + o_offset;
163
164 const int input_offset = -in.quantization_info().offset;
165 const float input_scale = in.quantization_info().scale;
166 const int weights_offset = -weights.quantization_info().offset;
167 const float weights_scale = weights.quantization_info().scale;
168 const int output_offset = out.quantization_info().offset;
169 const float output_scale = out.quantization_info().scale;
170
171 int output_multiplier = 0;
172 int output_shift = 0;
173 const float multiplier = input_scale * weights_scale / output_scale;
174 arm_compute::quantization::calculate_quantized_multiplier_less_than_one(multiplier, &output_multiplier, &output_shift);
175
176 const int half_width_weights = width_weights / 2;
177 const int half_height_weights = height_weights / 2;
178
179 // Reset accumulator
180 int32_t acc(0);
181
182 // Compute a 2D convolution for each IFM and accumulate the result
183 for(int ifm = 0; ifm < depth_in; ++ifm)
184 {
185 // Compute the offset for the input slice
186 const int offset_slice_in = xi + yi * width_in + ifm * width_in * height_in;
187
188 // Compute 2D convolution
189 for(int yk = -half_height_weights; yk <= half_height_weights; ++yk)
190 {
191 for(int xk = -half_width_weights; xk <= half_width_weights; ++xk)
192 {
193 // Check if the pixel is out-of-bound
194 if(is_valid_pixel(xi + xk, 0, width_in) && is_valid_pixel(yi + yk, 0, height_in))
195 {
196 const int idx = xk + half_width_weights;
197 const int idy = yk + half_height_weights;
198
199 const uint8_t i_value = in_ptr[offset_slice_in + xk + yk * width_in];
200 const uint8_t w_value = w_ptr[idx + idy * width_weights + ifm * width_weights * height_weights];
201
202 acc += (i_value + input_offset) * (w_value + weights_offset);
203 }
204 }
205 }
206 }
207
208 // Accumulate the bias
209 acc += (*b_ptr);
210
211 acc = asymm_rounding_divide_by_pow2(asymm_int_mult(acc, output_multiplier), output_shift);
212 acc += output_offset;
213 acc = std::max<int32_t>(acc, 0);
214 acc = std::min<int32_t>(acc, 255);
215
216 // Store the result
217 *out_ptr = acc;
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100218}
219} // namespace
220
221template <typename T>
222SimpleTensor<T> convolution_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<T> &bias, const TensorShape &output_shape, const PadStrideInfo &info)
223{
224 // Create reference
Chunosovd621bca2017-11-03 17:33:15 +0700225 SimpleTensor<T> dst{ output_shape, src.data_type(), 1, src.fixed_point_position(), src.quantization_info() };
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100226
227 // Compute reference
228 const int width_in = src.shape().x();
229 const int height_in = src.shape().y();
230 const int depth_in = src.shape().z();
231 const int width_out = dst.shape().x();
232 const int height_out = dst.shape().y();
233 const int depth_out = dst.shape().z();
234 const int width_weights = weights.shape().x();
235 const int height_weights = weights.shape().y();
236 const int depth_weights = weights.shape().z();
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100237 const int pad_left = std::min(static_cast<int>(info.pad_left()), width_weights / 2);
238 const int pad_top = std::min(static_cast<int>(info.pad_top()), height_weights / 2);
239 const int pad_right = std::min(static_cast<int>(info.pad_right()), width_weights / 2);
240 const int pad_bottom = std::min(static_cast<int>(info.pad_bottom()), height_weights / 2);
241
242 const int start_xi = width_weights / 2 - pad_left;
243 const int start_yi = height_weights / 2 - pad_top;
244 const int end_xi = width_in + pad_left - width_weights / 2 + pad_right - width_weights / 2;
245 const int end_yi = height_in + pad_top - height_weights / 2 + pad_bottom - height_weights / 2;
246 const int stride_xi = info.stride().first;
247 const int stride_yi = info.stride().second;
248 const int num_batches = src.shape().total_size() / (width_in * height_in * depth_in);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100249
250 for(int r = 0; r < num_batches; ++r)
251 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100252 for(int yi = start_yi; yi < start_yi + end_yi; yi += stride_yi)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100253 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100254 for(int xi = start_xi; xi < start_xi + end_xi; xi += stride_xi)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100255 {
256 for(int ofm = 0; ofm < depth_out; ++ofm)
257 {
258 // Compute input and output offsets
259 const int offset_in = r * width_in * height_in * depth_in;
260 const int xo = (xi - start_xi) / stride_xi;
261 const int yo = (yi - start_yi) / stride_yi;
262 const int offset_out = xo + yo * width_out + ofm * width_out * height_out + r * width_out * height_out * depth_out;
263
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100264 ARM_COMPUTE_ASSERT(xo < width_out);
265 ARM_COMPUTE_ASSERT(yo < height_out);
266
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100267 // Compute 3D convolution
Chunosovd621bca2017-11-03 17:33:15 +0700268 convolution3d(src, weights, bias, dst,
269 offset_in, ofm * width_weights * height_weights * depth_weights, ofm, offset_out,
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100270 xi, yi,
271 width_in, height_in, depth_in,
Chunosovd621bca2017-11-03 17:33:15 +0700272 width_weights, height_weights);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100273 }
274 }
275 }
276 }
277
278 return dst;
279}
280
281template SimpleTensor<float> convolution_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &output_shape,
282 const PadStrideInfo &info);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100283template SimpleTensor<half> convolution_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &output_shape,
284 const PadStrideInfo &info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100285template SimpleTensor<qint8_t> convolution_layer(const SimpleTensor<qint8_t> &src, const SimpleTensor<qint8_t> &weights, const SimpleTensor<qint8_t> &bias, const TensorShape &output_shape,
286 const PadStrideInfo &info);
287template SimpleTensor<qint16_t> convolution_layer(const SimpleTensor<qint16_t> &src, const SimpleTensor<qint16_t> &weights, const SimpleTensor<qint16_t> &bias, const TensorShape &output_shape,
288 const PadStrideInfo &info);
Chunosovd621bca2017-11-03 17:33:15 +0700289template SimpleTensor<uint8_t> convolution_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<uint8_t> &bias, const TensorShape &output_shape,
290 const PadStrideInfo &info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100291} // namespace reference
292} // namespace validation
293} // namespace test
294} // namespace arm_compute