blob: 02962e0492a66717f16fc02f77622f1bc8799204 [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
63 ARM_COMPUTE_UNUSED(expected_output);
64 ARM_COMPUTE_ERROR_ON(expected_output.first != output->info()->tensor_shape().x());
65 ARM_COMPUTE_ERROR_ON(expected_output.second != output->info()->tensor_shape().y());
66
67 _input = input;
68 _output = output;
69 _weights = weights;
70 _conv_info = conv_info;
71 const unsigned int conv_stride_x = conv_info.stride().first;
72 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
83 AccessWindowStatic input_access(input->info(), -conv_pad_x, -conv_pad_y, input->info()->dimension(0) + _border_size.right, input->info()->dimension(1) + _border_size.bottom);
84 AccessWindowStatic weights_access(weights->info(), 0, 0, weights->info()->dimension(0), weights->info()->dimension(1));
85 AccessWindowHorizontal output_access(output->info(), 0, num_elems_written_per_iteration);
86
87 update_window_and_padding(win, input_access, weights_access, output_access);
88 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
89
90 INEKernel::configure(win);
91}
92
93template <unsigned int stridex>
94class convolver_3x3
95{
96public:
97 static void convolve(const Window &window, unsigned int num_elems_written_per_iteration,
98 const ITensor *input, const ITensor *weights, ITensor *output, const PadStrideInfo &conv_info)
99 {
100 const int input_stride_x = input->info()->strides_in_bytes().x();
101 const int input_stride_y = input->info()->strides_in_bytes().y();
102 const int output_stride_y = output->info()->strides_in_bytes().y();
103 const int kernel_stride_y = weights->info()->strides_in_bytes().y();
104 const int kernel_stride_z = weights->info()->strides_in_bytes().z();
105 const int output_w = output->info()->dimension(0);
106 const int output_h = output->info()->dimension(1);
107 const int delta_input = get_input_num_elems_processed<stridex>(num_elems_written_per_iteration);
108 const unsigned int conv_stride_y = std::get<1>(conv_info.stride());
109 const unsigned int conv_pad_x = std::get<0>(conv_info.pad());
110 const unsigned int conv_pad_y = std::get<1>(conv_info.pad());
111
112 // setup output window for the iterator
113 Window window_out = window;
114 window_out.set(Window::DimX, Window::Dimension(0, output->info()->dimension(Window::DimX), output->info()->dimension(Window::DimX)));
115 window_out.set(Window::DimY, Window::Dimension(0, output->info()->dimension(Window::DimY), output->info()->dimension(Window::DimY)));
116
117 // setup input window for the iterator
118 Window window_in = window;
119 // we just want execute_window_loop to iterate over the dimensions > 2, so we set the first 2 dimensions to 0
120 window_in.set(Window::DimX, Window::Dimension(0, 0, 0));
121 window_in.set(Window::DimY, Window::Dimension(0, 0, 0));
122
123 Window window_k = calculate_max_window(*weights->info(), Steps(1u));
124
125 Iterator in(input, window_in);
126 Iterator out(output, window_out);
127 Iterator w(weights, window_k);
128
129 const uint8_t *weights_ptr = w.ptr();
130
131 execute_window_loop(window_out, [&](const Coordinates & id)
132 {
133 const uint8_t *input_ptr = in.ptr() - conv_pad_x * input_stride_x - conv_pad_y * input_stride_y;
134 int ih = 0;
135 int oh = 0;
136
137 const uint8_t *ptr_weights_base = weights_ptr + id.z() * kernel_stride_z;
138 const auto ptr_weights_r0 = reinterpret_cast<const float *>(ptr_weights_base);
139 const auto ptr_weights_r1 = reinterpret_cast<const float *>(ptr_weights_base + kernel_stride_y);
140 const auto ptr_weights_r2 = reinterpret_cast<const float *>(ptr_weights_base + kernel_stride_y * 2);
141 const auto vw_r0 = load_matrix_row(ptr_weights_r0);
142 const auto vw_r1 = load_matrix_row(ptr_weights_r1);
143 const auto vw_r2 = load_matrix_row(ptr_weights_r2);
144
145 for(ih = 0, oh = 0; oh < output_h; ++oh, ih += conv_stride_y)
146 {
147 auto in_top = reinterpret_cast<const float *>(input_ptr + (ih + 0) * input_stride_y);
148 auto in_mid = reinterpret_cast<const float *>(input_ptr + (ih + 1) * input_stride_y);
149 auto in_low = reinterpret_cast<const float *>(input_ptr + (ih + 2) * input_stride_y);
150 auto p_out = reinterpret_cast<float *>(out.ptr() + oh * output_stride_y);
151
152 for(int ow = 0; ow < output_w; ow += num_elems_written_per_iteration,
153 in_top += delta_input, in_mid += delta_input, in_low += delta_input, p_out += num_elems_written_per_iteration)
154 {
155 auto vres = convolve_3x3<stridex>(in_top, in_mid, in_low, vw_r0, vw_r1, vw_r2, 0);
156 store_results<stridex>(p_out, vres);
157 }
158 }
159 },
160 in, out);
161 }
162};
163
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000164void NEDepthwiseConvolutionLayer3x3Kernel::run(const Window &window, const ThreadInfo &info)
Michalis Spyrou7362f0d2017-10-18 17:58:22 +0100165{
166 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
167 ARM_COMPUTE_UNUSED(info);
168
169 const unsigned int conv_stride_x = _conv_info.stride().first;
170 const unsigned int num_elems_written_per_iteration = 16 >> conv_stride_x;
171
172 switch(conv_stride_x)
173 {
174 case 1:
175 convolver_3x3<1>::convolve(window, num_elems_written_per_iteration, _input, _weights, _output, _conv_info);
176 break;
177 case 2:
178 convolver_3x3<2>::convolve(window, num_elems_written_per_iteration, _input, _weights, _output, _conv_info);
179 break;
180 case 3:
181 convolver_3x3<3>::convolve(window, num_elems_written_per_iteration, _input, _weights, _output, _conv_info);
182 break;
183 default:
184 ARM_COMPUTE_ERROR("Not implemented");
185 }
186}