blob: 13c2d314e47eb8f9bf74c797d2819578d059acb3 [file] [log] [blame]
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00001/*
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +00002 * Copyright (c) 2021-2023 Arm Limited.
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +00003 *
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
25#pragma once
26
27#include "arm_gemm.hpp"
28#include "arm_gemm_local.hpp"
29#include "depthwise_common.hpp"
Michael Tyler8deee9b2023-06-30 11:26:05 +010030#include "premultiply.hpp"
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000031
32namespace arm_conv
33{
34namespace depthwise
35{
36struct DepthwiseConfig
37{
38 DepthwiseMethod method = DepthwiseMethod::DEFAULT;
39 std::string filter = "";
40
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010041 DepthwiseConfig(DepthwiseMethod method) : method(method){};
42 DepthwiseConfig(){};
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000043};
44
45struct DepthwiseArgs
46{
47 const CPUInfo *cpu_info;
48
49 unsigned int kernel_rows, kernel_cols;
50 unsigned int stride_rows, stride_cols;
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +000051 unsigned int dilation_rows, dilation_cols;
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +000052
53 unsigned int n_batches, input_rows, input_cols, input_channels;
54 unsigned int output_rows, output_cols;
55 unsigned int channel_multiplier;
56
57 PaddingValues padding;
58
59 arm_gemm::Activation activation;
60
61 const DepthwiseConfig *config;
62
ramelg018a164882022-04-07 02:42:52 +010063 bool fast_mode = false;
64
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010065 DepthwiseArgs(const CPUInfo *cpu_info,
66 unsigned int kernel_rows,
67 unsigned int kernel_cols,
68 unsigned int stride_rows,
69 unsigned int stride_cols,
70 unsigned int dilation_rows,
71 unsigned int dilation_cols,
72 unsigned int n_batches,
73 unsigned int input_rows,
74 unsigned int input_cols,
75 unsigned int input_channels,
76 unsigned int output_rows,
77 unsigned int output_cols,
78 unsigned int channel_multiplier,
79 PaddingValues padding,
80 arm_gemm::Activation activation,
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +000081
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082 const DepthwiseConfig *config)
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +000083 : cpu_info(cpu_info),
84 kernel_rows(kernel_rows),
85 kernel_cols(kernel_cols),
86 stride_rows(stride_rows),
87 stride_cols(stride_cols),
88 dilation_rows(dilation_rows),
89 dilation_cols(dilation_cols),
90 n_batches(n_batches),
91 input_rows(input_rows),
92 input_cols(input_cols),
93 input_channels(input_channels),
94 output_rows(output_rows),
95 output_cols(output_cols),
96 channel_multiplier(channel_multiplier),
97 padding(padding),
98 activation(activation),
99 config(config)
100 {
101 }
102
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 DepthwiseArgs(const CPUInfo *cpu_info,
104 unsigned int kernel_rows,
105 unsigned int kernel_cols,
106 unsigned int stride_rows,
107 unsigned int stride_cols,
108 unsigned int n_batches,
109 unsigned int input_rows,
110 unsigned int input_cols,
111 unsigned int input_channels,
112 unsigned int output_rows,
113 unsigned int output_cols,
114 unsigned int channel_multiplier,
115 PaddingValues padding,
116 arm_gemm::Activation activation,
117 const DepthwiseConfig *config)
118 : DepthwiseArgs(cpu_info,
119 kernel_rows,
120 kernel_cols,
121 stride_rows,
122 stride_cols,
123 1,
124 1,
125 n_batches,
126 input_rows,
127 input_cols,
128 input_channels,
129 output_rows,
130 output_cols,
131 channel_multiplier,
132 padding,
133 activation,
134 config)
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000135 {
136 }
137};
138
Michael Tyler8deee9b2023-06-30 11:26:05 +0100139template <typename TInput>
140struct Tile
141{
142 TInput *array;
143
144 unsigned int tile_rows = 0;
145 unsigned int tile_cols = 0;
146 unsigned int tile_channels = 0;
147
148 Tile(TInput *array, unsigned int tile_rows, unsigned int tile_cols, unsigned int tile_channels)
149 : array(array), tile_rows(tile_rows), tile_cols(tile_cols), tile_channels(tile_channels)
150 {
151 }
152
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100153 Tile() : Tile(nullptr, 0, 0, 0)
Michael Tyler8deee9b2023-06-30 11:26:05 +0100154 {
155 }
156
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100157 void load_from(const TInput *input,
158 const unsigned int ld_row,
159 const unsigned int ld_col,
160 const unsigned int n_rows,
161 const unsigned int n_cols,
162 const int input_i,
163 const int input_j,
164 const unsigned int channel_multiplier) const
Michael Tyler8deee9b2023-06-30 11:26:05 +0100165 {
166 const auto pad_top = input_i < 0 ? -input_i : 0;
167 const auto pad_left = input_j < 0 ? -input_j : 0;
168
169 const auto padded_rows = std::min(n_rows - input_i, tile_rows) - pad_top;
170 const auto padded_cols = std::min(n_cols - input_j, tile_cols) - pad_left;
171
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 if (padded_rows < tile_rows || padded_cols < tile_cols)
Michael Tyler8deee9b2023-06-30 11:26:05 +0100173 {
174 memset(array, 0, tile_rows * tile_cols * tile_channels * sizeof(TInput));
175 }
176
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100177 do_premultiply<TInput>((TInput *)input + std::max(input_i, 0) * ld_row + std::max(input_j, 0) * ld_col, ld_row,
178 ld_col, array + pad_top * tile_cols * tile_channels + pad_left * tile_channels,
179 tile_cols * tile_channels, tile_channels, padded_rows, padded_cols,
180 tile_channels / channel_multiplier, channel_multiplier);
Michael Tyler8deee9b2023-06-30 11:26:05 +0100181 }
182};
183
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000184template <typename TInput, typename TWeight, typename TOutput>
185class DepthwiseCommon : public IDepthwiseCommon
186{
Michael Tyler8deee9b2023-06-30 11:26:05 +0100187protected:
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000188 const DepthwiseArgs m_args; // Copy of arguments
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000189 std::string m_name{};
ramelg018a164882022-04-07 02:42:52 +0100190
Michael Tyler8deee9b2023-06-30 11:26:05 +0100191public:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100192 DepthwiseCommon(const DepthwiseArgs &args) : m_args(args){};
193 DepthwiseCommon(DepthwiseCommon &) = delete;
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000194 DepthwiseCommon &operator=(DepthwiseCommon &) = delete;
195
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000196 std::string name() const override
197 {
198 return m_name;
199 }
200
201 void set_name(std::string name)
202 {
203 // Only allow the name to be set once
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100204 if (m_name.empty())
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000205 {
206 m_name = name;
207 }
208 }
209
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100210 void execute(const void *const input,
211 const void *const parameters,
212 void *const output,
213 void *const working_space,
214 const unsigned int thread_id,
215 const unsigned int n_threads) const override final
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000216 {
217 const size_t ld_input_col = m_args.input_channels;
218 const size_t ld_input_row = ld_input_col * m_args.input_cols;
219 const size_t ld_input_batch = ld_input_row * m_args.input_rows;
220 const size_t ld_output_col = m_args.input_channels * m_args.channel_multiplier;
221 const size_t ld_output_row = ld_output_col * m_args.output_cols;
222 const size_t ld_output_batch = ld_output_row * m_args.output_rows;
223
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100224 execute(input, ld_input_col, ld_input_row, ld_input_batch, parameters, output, ld_output_col, ld_output_row,
225 ld_output_batch, working_space, thread_id, n_threads);
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000226 }
227
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100228 void execute(const void *const input,
229 size_t ld_input_col,
230 size_t ld_input_row,
231 size_t ld_input_batch,
232 const void *const parameters,
233 void *const output,
234 size_t ld_output_col,
235 size_t ld_output_row,
236 size_t ld_output_batch,
237 void *const working_space,
238 const unsigned int thread_id,
239 const unsigned int n_threads) const override final
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000240 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100241 execute(m_args.n_batches, m_args.input_rows, m_args.input_cols, m_args.input_channels, m_args.padding, input,
242 ld_input_col, ld_input_row, ld_input_batch, parameters, m_args.output_rows, m_args.output_cols, output,
243 ld_output_col, ld_output_row, ld_output_batch, working_space, thread_id, n_threads);
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000244 }
245
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100246 void execute(unsigned int batches,
247 unsigned int input_height,
248 unsigned int input_width,
249 unsigned int channels,
250 const PaddingValues &padding,
251 const void *input,
252 size_t ld_input_col,
253 size_t ld_input_row,
254 size_t ld_input_batch,
255 const void *parameters,
256 unsigned int output_height,
257 unsigned int output_width,
258 void *output,
259 size_t ld_output_col,
260 size_t ld_output_row,
261 size_t ld_output_batch,
262 void *working_space,
263 unsigned int thread_id,
264 unsigned int n_threads) const override final
ramelg018a164882022-04-07 02:42:52 +0100265 {
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000266 // Construct a new set of arguments to reflect that we might have been
267 // passed different input/output tensors. Dilation is handled at this
268 // level; so we set the dilation in the arguments to zero.
269 DepthwiseArgs args(this->m_args);
Michael Tyler8deee9b2023-06-30 11:26:05 +0100270 args.n_batches = batches;
271 args.input_rows = input_height;
272 args.input_cols = input_width;
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000273 args.input_channels = channels;
Michael Tyler8deee9b2023-06-30 11:26:05 +0100274 args.output_rows = output_height;
275 args.output_cols = output_width;
276 args.padding = padding;
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000277 args.dilation_rows = args.dilation_cols = 1;
278
Michael Tyler8deee9b2023-06-30 11:26:05 +0100279 auto ld_input_col_d = ld_input_col * m_args.dilation_cols;
280 auto ld_input_row_d = ld_input_row * m_args.dilation_rows;
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000281 auto ld_output_col_d = ld_output_col * m_args.dilation_cols;
282 auto ld_output_row_d = ld_output_row * m_args.dilation_rows;
283
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100284 for (size_t drow = 0; drow < m_args.dilation_rows; drow++)
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000285 {
286 size_t start_i;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100287 std::tie(args.output_rows, args.input_rows, start_i, args.padding.top, args.padding.bottom) =
288 get_reduced_view_for_dilation(output_height, input_height, drow, m_args.dilation_rows,
289 m_args.kernel_rows, m_args.stride_rows, padding.top);
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000290
Michael Tyler8deee9b2023-06-30 11:26:05 +0100291 auto input_row = static_cast<const TInput *>(input) + start_i * ld_input_row;
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000292 auto output_row = static_cast<TOutput *>(output) + drow * ld_output_row;
293
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100294 if (args.output_rows)
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000295 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100296 for (size_t dcol = 0; dcol < m_args.dilation_cols; dcol++)
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000297 {
298 size_t start_j;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100299 std::tie(args.output_cols, args.input_cols, start_j, args.padding.left, args.padding.right) =
300 get_reduced_view_for_dilation(output_width, input_width, dcol, m_args.dilation_cols,
301 m_args.kernel_cols, m_args.stride_cols, padding.left);
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000302
Michael Tyler8deee9b2023-06-30 11:26:05 +0100303 const TInput *input_col = input_row + start_j * ld_input_col;
304 TOutput *output_col = output_row + dcol * ld_output_col;
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000305
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100306 if (args.output_cols)
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000307 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100308 this->execute_internal(args, input_col, ld_input_col_d, ld_input_row_d, ld_input_batch,
309 parameters, output_col, ld_output_col_d, ld_output_row_d,
310 ld_output_batch, working_space, thread_id, n_threads);
Pablo Marquez Tello4e2bbbb2023-01-09 17:21:01 +0000311 }
312 }
313 }
314 }
ramelg018a164882022-04-07 02:42:52 +0100315 }
316
Michael Tyler8deee9b2023-06-30 11:26:05 +0100317protected:
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100318 virtual void execute_internal(const DepthwiseArgs &instance_args,
319 const void *input,
320 size_t ld_input_col,
321 size_t ld_input_row,
322 size_t ld_input_batch,
323 const void *parameters,
324 void *output,
325 size_t ld_output_col,
326 size_t ld_output_row,
327 size_t ld_output_batch,
328 void *working_space,
329 unsigned int thread_id,
330 unsigned int n_threads) const = 0;
Michael Tyler8deee9b2023-06-30 11:26:05 +0100331
332 virtual bool uses_premultiply() const
333 {
334 return true;
335 }
Michele Di Giorgiod02d5ed2021-01-22 09:47:04 +0000336};
337
338template <typename TInput, typename TWeight = TInput, typename TOutput = TInput>
339using UniqueDepthwiseCommon = std::unique_ptr<DepthwiseCommon<TInput, TWeight, TOutput>>;
340
341template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
342KernelDescription get_depthwise_method(const DepthwiseArgs &, const OutputStage & = {});
343
344template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
345UniqueDepthwiseCommon<TInput, TWeight, TOutput> depthwise(const DepthwiseArgs &, const OutputStage & = {});
346
347template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing>
348std::vector<KernelDescription> get_compatible_kernels(const DepthwiseArgs &, const OutputStage & = {});
349
350} // namespace depthwise
351} // namespace arm_conv