blob: 656cd2ee26e548aab199f657f68f51652ff732fd [file] [log] [blame]
Kaizen8938bd32017-09-28 14:38: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 "ConvolutionLayer.h"
25
26#include "tests/validation/FixedPoint.h"
27#include "tests/validation/Helpers.h"
28
29namespace arm_compute
30{
31namespace test
32{
33namespace validation
34{
35namespace reference
36{
37namespace
38{
39inline bool is_valid_pixel(int i, int min, int max)
40{
41 return (i >= min && i < max);
42}
43
44// 3D convolution for floating point type
45template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type = 0>
46void convolution3d(const T *in, const T *weights, const T *bias, T *out, int xi, int yi, int width_in, int height_in, int depth_in, int width_weights, int height_weights, int fixed_point_position)
47{
48 ARM_COMPUTE_UNUSED(fixed_point_position);
49
50 const int half_width_weights = width_weights / 2;
51 const int half_height_weights = height_weights / 2;
52
53 // Reset accumulator
54 T acc(0);
55
56 // Compute a 2D convolution for each IFM and accumulate the result
57 for(int ifm = 0; ifm < depth_in; ++ifm)
58 {
59 // Compute the offset for the input slice
60 const int offset_slice_in = xi + yi * width_in + ifm * width_in * height_in;
61
62 // Compute 2D convolution
63 for(int yk = -half_height_weights; yk <= half_height_weights; ++yk)
64 {
65 for(int xk = -half_width_weights; xk <= half_width_weights; ++xk)
66 {
67 // Check if the pixel is out-of-bound
68 if(is_valid_pixel(xi + xk, 0, width_in) && is_valid_pixel(yi + yk, 0, height_in))
69 {
70 const int idx = xk + half_width_weights;
71 const int idy = yk + half_height_weights;
72
73 const T i_value = in[offset_slice_in + xk + yk * width_in];
74 const T w_value = weights[idx + idy * width_weights + ifm * width_weights * height_weights];
75
76 acc += i_value * w_value;
77 }
78 }
79 }
80 }
81
82 // Accumulate the bias and store the result
83 *out = acc + (*bias);
84}
85
86// 3D convolution for fixed point type
87template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
88void convolution3d(const T *in, const T *weights, const T *bias, T *out, int xi, int yi, int width_in, int height_in, int depth_in, int width_weights, int height_weights,
89 int fixed_point_position)
90{
91 const int half_width_weights = width_weights / 2;
92 const int half_height_weights = height_weights / 2;
93
94 using namespace fixed_point_arithmetic;
95 using promoted_type = fixed_point_arithmetic::traits::promote_t<T>;
96
97 // Reset accumulator
98 fixed_point<promoted_type> acc(0, fixed_point_position);
99
100 // Compute a 2D convolution for each IFM and accumulate the result
101 for(int ifm = 0; ifm < depth_in; ++ifm)
102 {
103 // Compute the offset for the input slice
104 const int offset_slice_in = xi + yi * width_in + ifm * width_in * height_in;
105
106 // Compute 2D convolution
107 for(int yk = -half_height_weights; yk <= half_height_weights; ++yk)
108 {
109 for(int xk = -half_width_weights; xk <= half_width_weights; ++xk)
110 {
111 // Check if the pixel is out-of-bound
112 if(is_valid_pixel(xi + xk, 0, width_in) && is_valid_pixel(yi + yk, 0, height_in))
113 {
114 const int idx = xk + half_width_weights;
115 const int idy = yk + half_height_weights;
116
117 const fixed_point<promoted_type> i_value(in[offset_slice_in + xk + yk * width_in], fixed_point_position, true);
118 const fixed_point<promoted_type> w_value(weights[idx + idy * width_weights + ifm * width_weights * height_weights], fixed_point_position, true);
119 const fixed_point<promoted_type> iw = i_value * w_value;
120 acc = iw + acc;
121 }
122 }
123 }
124 }
125
126 // Get the bias
127 const fixed_point<promoted_type> b(*bias, fixed_point_position, true);
128
129 // Accumulate the bias and covert back
130 acc = acc + b;
131 fixed_point<T> res(acc);
132 *out = res.raw();
133}
134} // namespace
135
136template <typename T>
137SimpleTensor<T> convolution_layer(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<T> &bias, const TensorShape &output_shape, const PadStrideInfo &info)
138{
139 // Create reference
140 SimpleTensor<T> dst{ output_shape, src.data_type(), 1, src.fixed_point_position() };
141
142 // Compute reference
143 const int width_in = src.shape().x();
144 const int height_in = src.shape().y();
145 const int depth_in = src.shape().z();
146 const int width_out = dst.shape().x();
147 const int height_out = dst.shape().y();
148 const int depth_out = dst.shape().z();
149 const int width_weights = weights.shape().x();
150 const int height_weights = weights.shape().y();
151 const int depth_weights = weights.shape().z();
152 const int pad_xi = std::min(static_cast<int>(info.pad().first), width_weights / 2);
153 const int pad_yi = std::min(static_cast<int>(info.pad().second), height_weights / 2);
154 const int start_xi = width_weights / 2 - pad_xi;
155 const int start_yi = height_weights / 2 - pad_yi;
156 const int end_xi = width_in - start_xi;
157 const int end_yi = height_in - start_yi;
158 const int stride_xi = info.stride().first;
159 const int stride_yi = info.stride().second;
160 const int num_batches = src.shape().total_size() / (width_in * height_in * depth_in);
161
162 for(int r = 0; r < num_batches; ++r)
163 {
164 for(int yi = start_yi; yi < end_yi; yi += stride_yi)
165 {
166 for(int xi = start_xi; xi < end_xi; xi += stride_xi)
167 {
168 for(int ofm = 0; ofm < depth_out; ++ofm)
169 {
170 // Compute input and output offsets
171 const int offset_in = r * width_in * height_in * depth_in;
172 const int xo = (xi - start_xi) / stride_xi;
173 const int yo = (yi - start_yi) / stride_yi;
174 const int offset_out = xo + yo * width_out + ofm * width_out * height_out + r * width_out * height_out * depth_out;
175
176 // Compute 3D convolution
177 convolution3d(src.data() + offset_in,
178 weights.data() + ofm * width_weights * height_weights * depth_weights,
179 bias.data() + ofm,
180 dst.data() + offset_out,
181 xi, yi,
182 width_in, height_in, depth_in,
183 width_weights, height_weights,
184 src.fixed_point_position());
185 }
186 }
187 }
188 }
189
190 return dst;
191}
192
193template SimpleTensor<float> convolution_layer(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &bias, const TensorShape &output_shape,
194 const PadStrideInfo &info);
195template SimpleTensor<half> convolution_layer(const SimpleTensor<half> &src, const SimpleTensor<half> &weights, const SimpleTensor<half> &bias, const TensorShape &output_shape,
196 const PadStrideInfo &info);
197template SimpleTensor<qint8_t> convolution_layer(const SimpleTensor<qint8_t> &src, const SimpleTensor<qint8_t> &weights, const SimpleTensor<qint8_t> &bias, const TensorShape &output_shape,
198 const PadStrideInfo &info);
199template SimpleTensor<qint16_t> convolution_layer(const SimpleTensor<qint16_t> &src, const SimpleTensor<qint16_t> &weights, const SimpleTensor<qint16_t> &bias, const TensorShape &output_shape,
200 const PadStrideInfo &info);
201} // namespace reference
202} // namespace validation
203} // namespace test
204} // namespace arm_compute