blob: dd5c44801e7d197e2c0d34b8b57eef5444ca4d63 [file] [log] [blame]
Michalis Spyrou7362f0d2017-10-18 17:58:22 +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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "arm_compute/core/NEON/kernels/NEDepthwiseConvolutionLayer3x3Kernel.h"
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010025#include "arm_compute/core/NEON/kernels/convolution/NEDirectConvolutionDetail.h"
26
27#include "arm_compute/core/AccessWindowStatic.h"
28#include "arm_compute/core/AccessWindowTranspose.h"
29#include "arm_compute/core/Coordinates.h"
30#include "arm_compute/core/Error.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/ITensor.h"
33#include "arm_compute/core/NEON/INEKernel.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/TensorShape.h"
36#include "arm_compute/core/Types.h"
37#include "arm_compute/core/Validate.h"
38#include "arm_compute/core/Window.h"
39
40using namespace arm_compute;
41using namespace arm_compute::detail;
42
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000043NEDepthwiseConvolutionLayer3x3Kernel::NEDepthwiseConvolutionLayer3x3Kernel()
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010044 : _border_size(0), _input(), _output(), _weights(), _conv_info()
45{
46}
47
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000048BorderSize NEDepthwiseConvolutionLayer3x3Kernel::border_size() const
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010049{
50 return _border_size;
51}
52
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000053void NEDepthwiseConvolutionLayer3x3Kernel::configure(const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010054{
55 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
56 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output, weights);
57 ARM_COMPUTE_ERROR_ON(weights->info()->dimension(0) != 3 || weights->info()->dimension(1) != 3);
58
59 std::pair<unsigned int, unsigned int> expected_output = scaled_dimensions(input->info()->tensor_shape().x(), input->info()->tensor_shape().y(),
60 weights->info()->tensor_shape().x(), weights->info()->tensor_shape().y(),
61 conv_info);
62
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010063 ARM_COMPUTE_ERROR_ON(expected_output.first != output->info()->tensor_shape().x());
64 ARM_COMPUTE_ERROR_ON(expected_output.second != output->info()->tensor_shape().y());
65
66 _input = input;
67 _output = output;
68 _weights = weights;
69 _conv_info = conv_info;
70 const unsigned int conv_stride_x = conv_info.stride().first;
Anthony Barbier15686212017-12-12 17:17:50 +000071 const unsigned int conv_stride_y = conv_info.stride().second;
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010072 const unsigned int conv_pad_x = conv_info.pad().first;
73 const unsigned int conv_pad_y = conv_info.pad().second;
74
75 ARM_COMPUTE_ERROR_ON(conv_stride_x < 1 || conv_stride_x > 3);
76
77 const unsigned int num_elems_written_per_iteration = 16 >> conv_stride_x;
78 _border_size = BorderSize(conv_pad_y, conv_pad_x);
79
80 // Configure kernel window
81 Window win = calculate_max_window(*output->info(), Steps(num_elems_written_per_iteration));
82
Anthony Barbier15686212017-12-12 17:17:50 +000083 const unsigned int num_x_steps = (expected_output.first + num_elems_written_per_iteration - 1) / num_elems_written_per_iteration;
84 const int input_num_elems_processed = get_input_num_elems_processed(num_elems_written_per_iteration, conv_stride_x);
85
86 AccessWindowStatic input_access(input->info(), -conv_pad_x, -conv_pad_y, (num_x_steps - 1) * input_num_elems_processed + 12, conv_stride_y * (expected_output.second - 1) + 2);
87 AccessWindowStatic weights_access(weights->info(), 0, 0, weights->info()->dimension(0), weights->info()->dimension(1));
88 AccessWindowStatic output_access(output->info(), 0, 0, num_x_steps * num_elems_written_per_iteration, expected_output.second);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +010089
90 update_window_and_padding(win, input_access, weights_access, output_access);
91 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
92
93 INEKernel::configure(win);
94}
95
96template <unsigned int stridex>
97class convolver_3x3
98{
99public:
100 static void convolve(const Window &window, unsigned int num_elems_written_per_iteration,
101 const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info)
102 {
103 const int input_stride_x = input->info()->strides_in_bytes().x();
104 const int input_stride_y = input->info()->strides_in_bytes().y();
105 const int output_stride_y = output->info()->strides_in_bytes().y();
106 const int kernel_stride_y = weights->info()->strides_in_bytes().y();
107 const int kernel_stride_z = weights->info()->strides_in_bytes().z();
108 const int output_w = output->info()->dimension(0);
109 const int output_h = output->info()->dimension(1);
110 const int delta_input = get_input_num_elems_processed<stridex>(num_elems_written_per_iteration);
111 const unsigned int conv_stride_y = std::get<1>(conv_info.stride());
112 const unsigned int conv_pad_x = std::get<0>(conv_info.pad());
113 const unsigned int conv_pad_y = std::get<1>(conv_info.pad());
114
115 // setup output window for the iterator
116 Window window_out = window;
117 window_out.set(Window::DimX, Window::Dimension(0, output->info()->dimension(Window::DimX), output->info()->dimension(Window::DimX)));
118 window_out.set(Window::DimY, Window::Dimension(0, output->info()->dimension(Window::DimY), output->info()->dimension(Window::DimY)));
119
120 // setup input window for the iterator
121 Window window_in = window;
122 // we just want execute_window_loop to iterate over the dimensions > 2, so we set the first 2 dimensions to 0
123 window_in.set(Window::DimX, Window::Dimension(0, 0, 0));
124 window_in.set(Window::DimY, Window::Dimension(0, 0, 0));
125
126 Window window_k = calculate_max_window(*weights->info(), Steps(1u));
127
128 Iterator in(input, window_in);
129 Iterator out(output, window_out);
130 Iterator w(weights, window_k);
131
132 const uint8_t *weights_ptr = w.ptr();
133
134 execute_window_loop(window_out, [&](const Coordinates & id)
135 {
136 const uint8_t *input_ptr = in.ptr() - conv_pad_x * input_stride_x - conv_pad_y * input_stride_y;
137 int ih = 0;
138 int oh = 0;
139
Anthony Barbier15686212017-12-12 17:17:50 +0000140 const uint8_t *ptr_weights_base = weights_ptr + id.z() * kernel_stride_z;
141 const auto ptr_weights_r0 = reinterpret_cast<const float *>(ptr_weights_base);
142 const auto ptr_weights_r1 = reinterpret_cast<const float *>(ptr_weights_base + kernel_stride_y);
143 const auto ptr_weights_r2 = reinterpret_cast<const float *>(ptr_weights_base + kernel_stride_y * 2);
144 const float32x4x3_t vw_r0 = load_matrix_row(ptr_weights_r0);
145 const float32x4x3_t vw_r1 = load_matrix_row(ptr_weights_r1);
146 const float32x4x3_t vw_r2 = load_matrix_row(ptr_weights_r2);
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100147
148 for(ih = 0, oh = 0; oh < output_h; ++oh, ih += conv_stride_y)
149 {
150 auto in_top = reinterpret_cast<const float *>(input_ptr + (ih + 0) * input_stride_y);
151 auto in_mid = reinterpret_cast<const float *>(input_ptr + (ih + 1) * input_stride_y);
152 auto in_low = reinterpret_cast<const float *>(input_ptr + (ih + 2) * input_stride_y);
153 auto p_out = reinterpret_cast<float *>(out.ptr() + oh * output_stride_y);
154
155 for(int ow = 0; ow < output_w; ow += num_elems_written_per_iteration,
156 in_top += delta_input, in_mid += delta_input, in_low += delta_input, p_out += num_elems_written_per_iteration)
157 {
158 auto vres = convolve_3x3<stridex>(in_top, in_mid, in_low, vw_r0, vw_r1, vw_r2, 0);
159 store_results<stridex>(p_out, vres);
160 }
161 }
162 },
163 in, out);
164 }
165};
166
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000167void NEDepthwiseConvolutionLayer3x3Kernel::run(const Window &window, const ThreadInfo &info)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100168{
169 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
170 ARM_COMPUTE_UNUSED(info);
171
172 const unsigned int conv_stride_x = _conv_info.stride().first;
173 const unsigned int num_elems_written_per_iteration = 16 >> conv_stride_x;
174
175 switch(conv_stride_x)
176 {
177 case 1:
178 convolver_3x3<1>::convolve(window, num_elems_written_per_iteration, _input, _weights, _output, _conv_info);
179 break;
180 case 2:
181 convolver_3x3<2>::convolve(window, num_elems_written_per_iteration, _input, _weights, _output, _conv_info);
182 break;
183 case 3:
184 convolver_3x3<3>::convolve(window, num_elems_written_per_iteration, _input, _weights, _output, _conv_info);
185 break;
186 default:
187 ARM_COMPUTE_ERROR("Not implemented");
188 }
189}