blob: 567fac0f5ed3bb08e953788114124b55874b6763 [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
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010026#include "tests/validation/FixedPoint.h"
27#include "tests/validation/Helpers.h"
Georgios Pinitas5a7e7762017-12-01 16:27:29 +000028#include "tests/validation/reference/Utils.h"
29#include "tests/validation/reference/UtilsQuantizedAsymm.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
Georgios Pinitas540d0082017-11-17 10:55:00 +000051template < typename T, typename TB, typename std::enable_if < is_floating_point<T>::value &&is_floating_point<TB>::value, int >::type = 0 >
52void convolution3d(const SimpleTensor<T> &in, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, SimpleTensor<T> &out,
Chunosovd621bca2017-11-03 17:33:15 +070053 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;
Dmitry Savenkod7295b72017-11-20 22:00:08 +070058 const TB *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
Georgios Pinitas540d0082017-11-17 10:55:00 +000098template < typename T, typename TB, typename std::enable_if < std::is_integral<T>::value &&std::is_integral<TB>::value, int >::type = 0 >
99void convolution3d(const SimpleTensor<T> &in, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, SimpleTensor<T> &out,
Chunosovd621bca2017-11-03 17:33:15 +0700100 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 <>
Georgios Pinitas540d0082017-11-17 10:55:00 +0000155void convolution3d(const SimpleTensor<uint8_t> &in, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &bias, SimpleTensor<uint8_t> &out,
Chunosovd621bca2017-11-03 17:33:15 +0700156 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;
Georgios Pinitas540d0082017-11-17 10:55:00 +0000161 const int32_t *b_ptr = bias.data() + b_offset;
Chunosovd621bca2017-11-03 17:33:15 +0700162 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;
Diego Lopez Recas490b3d82017-12-19 15:42:25 +0000213 acc = utility::clamp<int32_t>(acc, 0, 255);
Chunosovd621bca2017-11-03 17:33:15 +0700214
215 // Store the result
216 *out_ptr = acc;
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100217}
218} // namespace
219
Georgios Pinitas540d0082017-11-17 10:55:00 +0000220template <typename T, typename TB>
221SimpleTensor<T> convolution_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<TB> &bias, const TensorShape &output_shape, const PadStrideInfo &info)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100222{
223 // Create reference
Chunosovd621bca2017-11-03 17:33:15 +0700224 SimpleTensor<T> dst{ output_shape, src.data_type(), 1, src.fixed_point_position(), src.quantization_info() };
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100225
226 // Compute reference
227 const int width_in = src.shape().x();
228 const int height_in = src.shape().y();
229 const int depth_in = src.shape().z();
230 const int width_out = dst.shape().x();
231 const int height_out = dst.shape().y();
232 const int depth_out = dst.shape().z();
233 const int width_weights = weights.shape().x();
234 const int height_weights = weights.shape().y();
235 const int depth_weights = weights.shape().z();
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100236 const int pad_left = std::min(static_cast<int>(info.pad_left()), width_weights / 2);
237 const int pad_top = std::min(static_cast<int>(info.pad_top()), height_weights / 2);
238 const int pad_right = std::min(static_cast<int>(info.pad_right()), width_weights / 2);
239 const int pad_bottom = std::min(static_cast<int>(info.pad_bottom()), height_weights / 2);
240
241 const int start_xi = width_weights / 2 - pad_left;
242 const int start_yi = height_weights / 2 - pad_top;
243 const int end_xi = width_in + pad_left - width_weights / 2 + pad_right - width_weights / 2;
244 const int end_yi = height_in + pad_top - height_weights / 2 + pad_bottom - height_weights / 2;
245 const int stride_xi = info.stride().first;
246 const int stride_yi = info.stride().second;
247 const int num_batches = src.shape().total_size() / (width_in * height_in * depth_in);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100248
249 for(int r = 0; r < num_batches; ++r)
250 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100251 for(int yi = start_yi; yi < start_yi + end_yi; yi += stride_yi)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100252 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100253 for(int xi = start_xi; xi < start_xi + end_xi; xi += stride_xi)
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100254 {
255 for(int ofm = 0; ofm < depth_out; ++ofm)
256 {
257 // Compute input and output offsets
258 const int offset_in = r * width_in * height_in * depth_in;
259 const int xo = (xi - start_xi) / stride_xi;
260 const int yo = (yi - start_yi) / stride_yi;
261 const int offset_out = xo + yo * width_out + ofm * width_out * height_out + r * width_out * height_out * depth_out;
262
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100263 ARM_COMPUTE_ASSERT(xo < width_out);
264 ARM_COMPUTE_ASSERT(yo < height_out);
265
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100266 // Compute 3D convolution
Chunosovd621bca2017-11-03 17:33:15 +0700267 convolution3d(src, weights, bias, dst,
268 offset_in, ofm * width_weights * height_weights * depth_weights, ofm, offset_out,
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100269 xi, yi,
270 width_in, height_in, depth_in,
Chunosovd621bca2017-11-03 17:33:15 +0700271 width_weights, height_weights);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100272 }
273 }
274 }
275 }
276
277 return dst;
278}
279
280template SimpleTensor<float> convolution_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &output_shape,
281 const PadStrideInfo &info);
Georgios Pinitas583137c2017-08-31 18:12:42 +0100282template SimpleTensor<half> convolution_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &output_shape,
283 const PadStrideInfo &info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100284template SimpleTensor<qint8_t> convolution_layer(const SimpleTensor<qint8_t> &src, const SimpleTensor<qint8_t> &weights, const SimpleTensor<qint8_t> &bias, const TensorShape &output_shape,
285 const PadStrideInfo &info);
286template SimpleTensor<qint16_t> convolution_layer(const SimpleTensor<qint16_t> &src, const SimpleTensor<qint16_t> &weights, const SimpleTensor<qint16_t> &bias, const TensorShape &output_shape,
287 const PadStrideInfo &info);
Georgios Pinitas540d0082017-11-17 10:55:00 +0000288template SimpleTensor<uint8_t> convolution_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint8_t> &weights, const SimpleTensor<int32_t> &bias, const TensorShape &output_shape,
Chunosovd621bca2017-11-03 17:33:15 +0700289 const PadStrideInfo &info);
Moritz Pflanzerb3d25792017-07-26 11:49:37 +0100290} // namespace reference
291} // namespace validation
292} // namespace test
293} // namespace arm_compute