Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2023 Arm Limited. |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 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 | |
| 25 | #pragma once |
| 26 | |
| 27 | #include "arm_gemm.hpp" |
| 28 | #include "arm_gemm_local.hpp" |
| 29 | #include "depthwise_common.hpp" |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 30 | #include "premultiply.hpp" |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 31 | |
| 32 | namespace arm_conv |
| 33 | { |
| 34 | namespace depthwise |
| 35 | { |
| 36 | struct DepthwiseConfig |
| 37 | { |
| 38 | DepthwiseMethod method = DepthwiseMethod::DEFAULT; |
| 39 | std::string filter = ""; |
| 40 | |
| 41 | DepthwiseConfig(DepthwiseMethod method) |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 42 | : method(method) {}; |
| 43 | DepthwiseConfig() {}; |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct DepthwiseArgs |
| 47 | { |
| 48 | const CPUInfo *cpu_info; |
| 49 | |
| 50 | unsigned int kernel_rows, kernel_cols; |
| 51 | unsigned int stride_rows, stride_cols; |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 52 | unsigned int dilation_rows, dilation_cols; |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 53 | |
| 54 | unsigned int n_batches, input_rows, input_cols, input_channels; |
| 55 | unsigned int output_rows, output_cols; |
| 56 | unsigned int channel_multiplier; |
| 57 | |
| 58 | PaddingValues padding; |
| 59 | |
| 60 | arm_gemm::Activation activation; |
| 61 | |
| 62 | const DepthwiseConfig *config; |
| 63 | |
ramelg01 | 8a16488 | 2022-04-07 02:42:52 +0100 | [diff] [blame] | 64 | bool fast_mode = false; |
| 65 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 66 | DepthwiseArgs( |
| 67 | const CPUInfo *cpu_info, |
| 68 | unsigned int kernel_rows, unsigned int kernel_cols, |
| 69 | unsigned int stride_rows, unsigned int stride_cols, |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 70 | unsigned int dilation_rows, unsigned int dilation_cols, |
| 71 | unsigned int n_batches, unsigned int input_rows, unsigned int input_cols, |
| 72 | unsigned int input_channels, |
| 73 | unsigned int output_rows, unsigned int output_cols, |
| 74 | unsigned int channel_multiplier, |
| 75 | PaddingValues padding, arm_gemm::Activation activation, |
| 76 | |
| 77 | const DepthwiseConfig *config) |
| 78 | : cpu_info(cpu_info), |
| 79 | kernel_rows(kernel_rows), |
| 80 | kernel_cols(kernel_cols), |
| 81 | stride_rows(stride_rows), |
| 82 | stride_cols(stride_cols), |
| 83 | dilation_rows(dilation_rows), |
| 84 | dilation_cols(dilation_cols), |
| 85 | n_batches(n_batches), |
| 86 | input_rows(input_rows), |
| 87 | input_cols(input_cols), |
| 88 | input_channels(input_channels), |
| 89 | output_rows(output_rows), |
| 90 | output_cols(output_cols), |
| 91 | channel_multiplier(channel_multiplier), |
| 92 | padding(padding), |
| 93 | activation(activation), |
| 94 | config(config) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | DepthwiseArgs( |
| 99 | const CPUInfo *cpu_info, |
| 100 | unsigned int kernel_rows, unsigned int kernel_cols, |
| 101 | unsigned int stride_rows, unsigned int stride_cols, |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 102 | unsigned int n_batches, unsigned int input_rows, unsigned int input_cols, |
| 103 | unsigned int input_channels, |
| 104 | unsigned int output_rows, unsigned int output_cols, |
| 105 | unsigned int channel_multiplier, |
| 106 | PaddingValues padding, arm_gemm::Activation activation, |
| 107 | const DepthwiseConfig *config) |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 108 | : DepthwiseArgs(cpu_info, kernel_rows, kernel_cols, stride_rows, |
| 109 | stride_cols, 1, 1, n_batches, input_rows, input_cols, |
| 110 | input_channels, output_rows, output_cols, |
| 111 | channel_multiplier, padding, activation, config) |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 112 | { |
| 113 | } |
| 114 | }; |
| 115 | |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 116 | template <typename TInput> |
| 117 | struct Tile |
| 118 | { |
| 119 | TInput *array; |
| 120 | |
| 121 | unsigned int tile_rows = 0; |
| 122 | unsigned int tile_cols = 0; |
| 123 | unsigned int tile_channels = 0; |
| 124 | |
| 125 | Tile(TInput *array, unsigned int tile_rows, unsigned int tile_cols, unsigned int tile_channels) |
| 126 | : array(array), tile_rows(tile_rows), tile_cols(tile_cols), tile_channels(tile_channels) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | Tile() |
| 131 | : Tile(nullptr, 0, 0, 0) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | void load_from( |
| 136 | const TInput *input, |
| 137 | const unsigned int ld_row, const unsigned int ld_col, |
| 138 | const unsigned int n_rows, const unsigned int n_cols, |
| 139 | const int input_i, const int input_j, |
| 140 | const unsigned int channel_multiplier) const |
| 141 | { |
| 142 | const auto pad_top = input_i < 0 ? -input_i : 0; |
| 143 | const auto pad_left = input_j < 0 ? -input_j : 0; |
| 144 | |
| 145 | const auto padded_rows = std::min(n_rows - input_i, tile_rows) - pad_top; |
| 146 | const auto padded_cols = std::min(n_cols - input_j, tile_cols) - pad_left; |
| 147 | |
| 148 | if(padded_rows < tile_rows || padded_cols < tile_cols) |
| 149 | { |
| 150 | memset(array, 0, tile_rows * tile_cols * tile_channels * sizeof(TInput)); |
| 151 | } |
| 152 | |
| 153 | do_premultiply<TInput>( |
| 154 | (TInput *)input + std::max(input_i, 0) * ld_row + std::max(input_j, 0) * ld_col, |
| 155 | ld_row, ld_col, |
| 156 | array + pad_top * tile_cols * tile_channels + pad_left * tile_channels, |
| 157 | tile_cols * tile_channels, tile_channels, |
| 158 | padded_rows, padded_cols, tile_channels / channel_multiplier, |
| 159 | channel_multiplier); |
| 160 | } |
| 161 | }; |
| 162 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 163 | template <typename TInput, typename TWeight, typename TOutput> |
| 164 | class DepthwiseCommon : public IDepthwiseCommon |
| 165 | { |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 166 | protected: |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 167 | const DepthwiseArgs m_args; // Copy of arguments |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 168 | std::string m_name{}; |
ramelg01 | 8a16488 | 2022-04-07 02:42:52 +0100 | [diff] [blame] | 169 | |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 170 | public: |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 171 | DepthwiseCommon(const DepthwiseArgs &args) |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 172 | : m_args(args) {}; |
| 173 | DepthwiseCommon(DepthwiseCommon &) = delete; |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 174 | DepthwiseCommon &operator=(DepthwiseCommon &) = delete; |
| 175 | |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 176 | std::string name() const override |
| 177 | { |
| 178 | return m_name; |
| 179 | } |
| 180 | |
| 181 | void set_name(std::string name) |
| 182 | { |
| 183 | // Only allow the name to be set once |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 184 | if(m_name.empty()) |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 185 | { |
| 186 | m_name = name; |
| 187 | } |
| 188 | } |
| 189 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 190 | void execute( |
| 191 | const void *const input, |
| 192 | const void *const parameters, |
| 193 | void *const output, |
| 194 | void *const working_space, |
| 195 | const unsigned int thread_id, |
ramelg01 | 8a16488 | 2022-04-07 02:42:52 +0100 | [diff] [blame] | 196 | const unsigned int n_threads) const override final |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 197 | { |
| 198 | const size_t ld_input_col = m_args.input_channels; |
| 199 | const size_t ld_input_row = ld_input_col * m_args.input_cols; |
| 200 | const size_t ld_input_batch = ld_input_row * m_args.input_rows; |
| 201 | const size_t ld_output_col = m_args.input_channels * m_args.channel_multiplier; |
| 202 | const size_t ld_output_row = ld_output_col * m_args.output_cols; |
| 203 | const size_t ld_output_batch = ld_output_row * m_args.output_rows; |
| 204 | |
| 205 | execute( |
| 206 | input, ld_input_col, ld_input_row, ld_input_batch, |
| 207 | parameters, output, ld_output_col, ld_output_row, ld_output_batch, |
| 208 | working_space, thread_id, n_threads); |
| 209 | } |
| 210 | |
| 211 | void execute( |
| 212 | const void *const input, |
| 213 | size_t ld_input_col, |
| 214 | size_t ld_input_row, |
| 215 | size_t ld_input_batch, |
| 216 | const void *const parameters, |
| 217 | void *const output, |
| 218 | size_t ld_output_col, |
| 219 | size_t ld_output_row, |
| 220 | size_t ld_output_batch, |
| 221 | void *const working_space, |
| 222 | const unsigned int thread_id, |
ramelg01 | 8a16488 | 2022-04-07 02:42:52 +0100 | [diff] [blame] | 223 | const unsigned int n_threads) const override final |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 224 | { |
| 225 | execute( |
| 226 | m_args.n_batches, m_args.input_rows, m_args.input_cols, |
| 227 | m_args.input_channels, m_args.padding, |
| 228 | input, ld_input_col, ld_input_row, ld_input_batch, |
| 229 | parameters, |
| 230 | m_args.output_rows, m_args.output_cols, |
| 231 | output, ld_output_col, ld_output_row, ld_output_batch, |
| 232 | working_space, thread_id, n_threads); |
| 233 | } |
| 234 | |
ramelg01 | 8a16488 | 2022-04-07 02:42:52 +0100 | [diff] [blame] | 235 | void execute( |
| 236 | unsigned int batches, |
| 237 | unsigned int input_height, |
| 238 | unsigned int input_width, |
| 239 | unsigned int channels, |
| 240 | const PaddingValues &padding, |
| 241 | const void *input, |
| 242 | size_t ld_input_col, |
| 243 | size_t ld_input_row, |
| 244 | size_t ld_input_batch, |
| 245 | const void *parameters, |
| 246 | unsigned int output_height, |
| 247 | unsigned int output_width, |
| 248 | void *output, |
| 249 | size_t ld_output_col, |
| 250 | size_t ld_output_row, |
| 251 | size_t ld_output_batch, |
| 252 | void *working_space, |
| 253 | unsigned int thread_id, |
| 254 | unsigned int n_threads) const override final |
| 255 | { |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 256 | // Construct a new set of arguments to reflect that we might have been |
| 257 | // passed different input/output tensors. Dilation is handled at this |
| 258 | // level; so we set the dilation in the arguments to zero. |
| 259 | DepthwiseArgs args(this->m_args); |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 260 | args.n_batches = batches; |
| 261 | args.input_rows = input_height; |
| 262 | args.input_cols = input_width; |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 263 | args.input_channels = channels; |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 264 | args.output_rows = output_height; |
| 265 | args.output_cols = output_width; |
| 266 | args.padding = padding; |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 267 | args.dilation_rows = args.dilation_cols = 1; |
| 268 | |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 269 | auto ld_input_col_d = ld_input_col * m_args.dilation_cols; |
| 270 | auto ld_input_row_d = ld_input_row * m_args.dilation_rows; |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 271 | auto ld_output_col_d = ld_output_col * m_args.dilation_cols; |
| 272 | auto ld_output_row_d = ld_output_row * m_args.dilation_rows; |
| 273 | |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 274 | for(size_t drow = 0; drow < m_args.dilation_rows; drow++) |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 275 | { |
| 276 | size_t start_i; |
| 277 | std::tie(args.output_rows, args.input_rows, start_i, |
| 278 | args.padding.top, args.padding.bottom) = |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 279 | get_reduced_view_for_dilation( |
| 280 | output_height, input_height, drow, m_args.dilation_rows, |
| 281 | m_args.kernel_rows, m_args.stride_rows, padding.top); |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 282 | |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 283 | auto input_row = static_cast<const TInput *>(input) + start_i * ld_input_row; |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 284 | auto output_row = static_cast<TOutput *>(output) + drow * ld_output_row; |
| 285 | |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 286 | if(args.output_rows) |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 287 | { |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 288 | for(size_t dcol = 0; dcol < m_args.dilation_cols; dcol++) |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 289 | { |
| 290 | size_t start_j; |
| 291 | std::tie(args.output_cols, args.input_cols, start_j, |
| 292 | args.padding.left, args.padding.right) = |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 293 | get_reduced_view_for_dilation( |
| 294 | output_width, input_width, dcol, m_args.dilation_cols, |
| 295 | m_args.kernel_cols, m_args.stride_cols, padding.left); |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 296 | |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 297 | const TInput *input_col = input_row + start_j * ld_input_col; |
| 298 | TOutput *output_col = output_row + dcol * ld_output_col; |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 299 | |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 300 | if(args.output_cols) |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 301 | { |
| 302 | this->execute_internal( |
| 303 | args, input_col, ld_input_col_d, ld_input_row_d, ld_input_batch, parameters, |
| 304 | output_col, ld_output_col_d, ld_output_row_d, ld_output_batch, |
| 305 | working_space, thread_id, n_threads); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
ramelg01 | 8a16488 | 2022-04-07 02:42:52 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 312 | protected: |
ramelg01 | 8a16488 | 2022-04-07 02:42:52 +0100 | [diff] [blame] | 313 | virtual void execute_internal( |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 314 | const DepthwiseArgs &instance_args, |
| 315 | const void *input, |
| 316 | size_t ld_input_col, |
| 317 | size_t ld_input_row, |
| 318 | size_t ld_input_batch, |
| 319 | const void *parameters, |
| 320 | void *output, |
| 321 | size_t ld_output_col, |
| 322 | size_t ld_output_row, |
| 323 | size_t ld_output_batch, |
| 324 | void *working_space, |
| 325 | unsigned int thread_id, |
| 326 | unsigned int n_threads) const = 0; |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 327 | |
| 328 | virtual bool uses_premultiply() const |
| 329 | { |
| 330 | return true; |
| 331 | } |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 332 | }; |
| 333 | |
| 334 | template <typename TInput, typename TWeight = TInput, typename TOutput = TInput> |
| 335 | using UniqueDepthwiseCommon = std::unique_ptr<DepthwiseCommon<TInput, TWeight, TOutput>>; |
| 336 | |
| 337 | template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing> |
| 338 | KernelDescription get_depthwise_method(const DepthwiseArgs &, const OutputStage & = {}); |
| 339 | |
| 340 | template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing> |
| 341 | UniqueDepthwiseCommon<TInput, TWeight, TOutput> depthwise(const DepthwiseArgs &, const OutputStage & = {}); |
| 342 | |
| 343 | template <typename TInput, typename TWeight = TInput, typename TOutput = TInput, class OutputStage = Nothing> |
| 344 | std::vector<KernelDescription> get_compatible_kernels(const DepthwiseArgs &, const OutputStage & = {}); |
| 345 | |
| 346 | } // namespace depthwise |
| 347 | } // namespace arm_conv |