Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 | |
| 25 | #pragma once |
| 26 | |
| 27 | #include "src/core/NEON/kernels/arm_gemm/utils.hpp" |
| 28 | |
| 29 | #ifdef CYCLE_PROFILING |
| 30 | #include "profiler.hpp" |
| 31 | #endif |
| 32 | |
Michele Di Giorgio | 0f033df | 2021-07-16 15:00:08 +0100 | [diff] [blame] | 33 | #include <limits> |
| 34 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 35 | namespace arm_conv { |
| 36 | namespace depthwise { |
| 37 | |
| 38 | template <class Strategy, unsigned OutputRows, unsigned int OutputCols> |
| 39 | class DepthwiseDepthfirstGenericBase : |
| 40 | public DepthwiseCommon<typename Strategy::input_type, |
| 41 | typename Strategy::weight_type, |
| 42 | typename Strategy::return_type> |
| 43 | { |
| 44 | protected: |
| 45 | |
| 46 | using TInput = typename Strategy::input_type; |
| 47 | using TWeight = typename Strategy::weight_type; |
| 48 | using TOutput = typename Strategy::return_type; |
| 49 | using TAccum = typename Strategy::bias_type; |
| 50 | |
| 51 | size_t sizeof_input_ptr_array(void) const |
| 52 | { |
| 53 | return sizeof(TInput *) * this->m_args.kernel_rows * this->m_args.kernel_cols * Strategy::n_output_points; |
| 54 | } |
| 55 | |
| 56 | size_t sizeof_input_buffer(unsigned int n_channels) const |
| 57 | { |
| 58 | const unsigned int vl = arm_gemm::utils::get_vector_length<TInput>(Strategy::vl_type); |
| 59 | const auto rounded_channels = arm_gemm::roundup(n_channels, vl); |
| 60 | return sizeof(TInput) * rounded_channels; |
| 61 | } |
| 62 | |
| 63 | size_t sizeof_output_buffer(unsigned int n_channels) const |
| 64 | { |
| 65 | const unsigned int vl = arm_gemm::utils::get_vector_length<TOutput>(Strategy::vl_type); |
| 66 | const auto rounded_channels = arm_gemm::roundup(n_channels, vl); |
| 67 | return sizeof(TOutput) * rounded_channels; |
| 68 | } |
| 69 | |
| 70 | unsigned int input_rows(void) const |
| 71 | { |
| 72 | return this->m_args.kernel_rows + (OutputRows - 1)*this->m_args.stride_rows; |
| 73 | } |
| 74 | |
| 75 | unsigned int input_cols(void) const |
| 76 | { |
| 77 | return this->m_args.kernel_cols + (OutputCols - 1)*this->m_args.stride_cols; |
| 78 | } |
| 79 | |
| 80 | void execute_tiles( |
| 81 | std::function<void(const TInput *const *, TOutput *const *)> tile_fn, |
| 82 | std::function<void(TInput *, unsigned int)> initialise_input_buffer, |
| 83 | const unsigned int batches, |
| 84 | const unsigned int input_height, |
| 85 | const unsigned int input_width, |
| 86 | const unsigned int input_channels, |
| 87 | const PaddingValues &padding, |
| 88 | const void *const _input, |
| 89 | const size_t ld_input_col, |
| 90 | const size_t ld_input_row, |
| 91 | const size_t ld_input_batch, |
| 92 | const unsigned int output_height, |
| 93 | const unsigned int output_width, |
| 94 | void *const _output, |
| 95 | const size_t ld_output_col, |
| 96 | const size_t ld_output_row, |
| 97 | const size_t ld_output_batch, |
| 98 | void *const _working_space, |
| 99 | const unsigned int thread_id, |
| 100 | const unsigned int n_threads |
| 101 | ) const |
| 102 | { |
| 103 | static_assert(OutputRows * OutputCols <= Strategy::n_output_points, |
| 104 | "Too many output points for kernel."); |
| 105 | |
| 106 | // Determine what portion of the work to do. |
| 107 | const unsigned int n_rows_per_thread = arm_gemm::iceildiv(output_height, n_threads); |
| 108 | const int start_out_height = std::min(thread_id * n_rows_per_thread, output_height); |
| 109 | const int end_out_height = std::min(start_out_height + n_rows_per_thread, output_height); |
| 110 | |
| 111 | // Cast input and output pointers into the right types |
| 112 | const TInput *const inptr = static_cast<const TInput *>(_input); |
| 113 | TOutput *const outptr = static_cast<TOutput *>(_output); |
| 114 | |
| 115 | // Allocate portions of the working space |
| 116 | uint8_t *const working_space = static_cast<uint8_t *>(_working_space) + this->get_working_size(thread_id, input_channels); |
| 117 | const TInput **const inptr_array = reinterpret_cast<const TInput **>(working_space); |
| 118 | TOutput *const output_buffer = reinterpret_cast<TOutput *>(working_space + this->sizeof_input_ptr_array()); |
| 119 | TInput *const input_buffer = reinterpret_cast<TInput *>(working_space + this->sizeof_input_ptr_array() + this->sizeof_output_buffer(input_channels * this->m_args.channel_multiplier)); |
| 120 | |
| 121 | // Create an array for the output pointers |
| 122 | TOutput * _outptr_array[Strategy::n_output_points]; |
| 123 | TOutput **const outptr_array = _outptr_array; |
| 124 | |
| 125 | // Initialise the input buffer |
| 126 | initialise_input_buffer(input_buffer, input_channels); |
| 127 | |
| 128 | // For each output tile, construct the requisite set of pointers and call |
| 129 | // into the kernel. |
| 130 | for (unsigned int batch = 0; batch < batches; batch++) |
| 131 | { |
| 132 | // Get batch pointers |
| 133 | const auto inptr_batch = inptr + batch * ld_input_batch; |
| 134 | const auto outptr_batch = outptr + batch * ld_output_batch; |
| 135 | |
| 136 | for (int start_out_i = start_out_height; |
| 137 | start_out_i < end_out_height; |
| 138 | start_out_i += static_cast<int>(OutputRows)) |
| 139 | { |
| 140 | const int end_out_i = std::min(start_out_i + OutputRows, |
| 141 | output_height); |
| 142 | |
| 143 | for (int start_out_j = 0; |
| 144 | start_out_j < static_cast<int>(output_width); |
| 145 | start_out_j += static_cast<int>(OutputCols)) |
| 146 | { |
| 147 | const int end_out_j = std::min(start_out_j + OutputCols, |
| 148 | output_width); |
| 149 | |
| 150 | // Fill the pointer arrays with pointers to the input/output buffers. |
| 151 | for (auto index = 0u; |
| 152 | index < (Strategy::n_output_points * this->m_args.kernel_rows * this->m_args.kernel_cols); |
| 153 | index++) |
| 154 | { |
| 155 | inptr_array[index] = input_buffer; |
| 156 | } |
| 157 | for (auto index = 0u; index < Strategy::n_output_points; index++) |
| 158 | { |
| 159 | outptr_array[index] = output_buffer; |
| 160 | } |
| 161 | |
| 162 | // Construct the pointer arrays together. Note that the input pointer |
| 163 | // array is striped. Since the array has already been filled with |
| 164 | // pointers to the padding array we merely fill in the valid points |
| 165 | // as we get to them. |
| 166 | unsigned int output_index = 0; |
| 167 | auto outptr_row = outptr_batch + start_out_i * ld_output_row + start_out_j * ld_output_col; |
| 168 | for (auto out_i = start_out_i; out_i < end_out_i; out_i++) |
| 169 | { |
| 170 | auto outptr_col = outptr_row; |
| 171 | |
| 172 | // Compute the padding for this row of tiles. |
| 173 | const int start_in_i = out_i * this->m_args.stride_rows - padding.top; |
| 174 | const int end_in_i = start_in_i + this->m_args.kernel_rows; |
| 175 | const auto pad_top = static_cast<unsigned int>(std::max<int>(0, 0 - start_in_i)); |
| 176 | const auto pad_bottom = static_cast<unsigned int>(std::max<int>(0, end_in_i - input_height)); |
| 177 | const unsigned int valid_rows = this->m_args.kernel_rows - pad_top - pad_bottom; |
| 178 | |
| 179 | for (auto out_j = start_out_j; out_j < end_out_j; out_j++, output_index++) |
| 180 | { |
| 181 | // Compute the output pointer. |
| 182 | outptr_array[output_index] = outptr_col; |
| 183 | outptr_col += ld_output_col; |
| 184 | |
| 185 | // Compute the padding for this tile. |
| 186 | const int start_in_j = out_j * this->m_args.stride_cols - padding.left; |
| 187 | const int end_in_j = start_in_j + this->m_args.kernel_cols; |
| 188 | const auto pad_left = static_cast<unsigned int>(std::max<int>(0, 0 - start_in_j)); |
| 189 | const auto pad_right = static_cast<unsigned int>(std::max<int>(0, end_in_j - input_width)); |
| 190 | const unsigned int valid_cols = this->m_args.kernel_cols - pad_left - pad_right; |
| 191 | |
| 192 | // Hence compute the input pointers. |
| 193 | auto input_index = output_index + Strategy::n_output_points * (pad_top * this->m_args.kernel_cols + pad_left); |
| 194 | auto inptr_row = inptr_batch + (start_in_i + pad_top) * ld_input_row + (start_in_j + pad_left) * ld_input_col; |
| 195 | for (auto in_i = 0u; in_i < valid_rows; in_i++) |
| 196 | { |
| 197 | auto inptr_col = inptr_row; |
| 198 | auto input_index_col = input_index; |
| 199 | |
| 200 | for (auto in_j = 0u; in_j < valid_cols; in_j++) |
| 201 | { |
| 202 | inptr_array[input_index_col] = inptr_col; |
| 203 | inptr_col += ld_input_col; |
| 204 | input_index_col += Strategy::n_output_points; |
| 205 | } |
| 206 | |
| 207 | inptr_row += ld_input_row; |
| 208 | input_index += Strategy::n_output_points * this->m_args.kernel_cols; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | outptr_row += ld_output_row; |
| 213 | } |
| 214 | |
| 215 | tile_fn(inptr_array, outptr_array); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | public: |
| 222 | DepthwiseDepthfirstGenericBase(const DepthwiseArgs &args) : DepthwiseCommon<TInput, TWeight, TOutput>(args) |
| 223 | { |
| 224 | } |
| 225 | |
| 226 | DepthwiseDepthfirstGenericBase(DepthwiseDepthfirstGenericBase &) = delete; |
| 227 | DepthwiseDepthfirstGenericBase &operator=(DepthwiseDepthfirstGenericBase &) = delete; |
| 228 | |
| 229 | size_t get_storage_size(void) const override |
| 230 | { |
| 231 | const unsigned int vl = arm_gemm::utils::get_vector_length<TAccum>(Strategy::vl_type); |
| 232 | const auto rounded_channels = arm_gemm::roundup(this->m_args.input_channels, vl); |
| 233 | return (this->m_args.kernel_rows * this->m_args.kernel_cols) * rounded_channels * sizeof(TWeight); |
| 234 | } |
| 235 | |
| 236 | void pack_parameters(void *_buffer, const void *, const void *_weights, size_t ld_weight_col, size_t ld_weight_row) override |
| 237 | { |
| 238 | // Cast the pointers |
| 239 | TWeight *buffer = static_cast<TWeight *>(_buffer); |
| 240 | const TWeight *const weights = static_cast<const TWeight *>(_weights); |
| 241 | |
| 242 | const unsigned int vl = arm_gemm::utils::get_vector_length<TAccum>(Strategy::vl_type); |
| 243 | ld_weight_col = (ld_weight_col == 0) ? this->m_args.input_channels : ld_weight_col; |
| 244 | ld_weight_row = (ld_weight_row == 0) ? this->m_args.kernel_cols * ld_weight_col : ld_weight_row; |
| 245 | |
| 246 | for (unsigned int n = 0; n < this->m_args.input_channels; n += vl) |
| 247 | { |
| 248 | const unsigned int todo = std::min(vl, this->m_args.input_channels - n); |
| 249 | |
| 250 | // Copy each of the weights in turn |
| 251 | auto weights_row = weights + n; |
| 252 | for (unsigned int i = 0; i < this->m_args.kernel_rows; i++) |
| 253 | { |
| 254 | auto weights_col = weights_row; |
| 255 | |
| 256 | for (unsigned int j = 0; j < this->m_args.kernel_cols; j++) |
| 257 | { |
| 258 | for (unsigned int m = 0; m < todo; m++) |
| 259 | { |
| 260 | buffer[m] = weights_col[m]; |
| 261 | } |
| 262 | buffer += vl; |
| 263 | |
| 264 | weights_col += ld_weight_col; |
| 265 | } |
| 266 | |
| 267 | weights_row += ld_weight_row; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | size_t get_working_size(const unsigned int n_threads, const unsigned int n_channels) const override |
| 273 | { |
| 274 | const unsigned int n_output_channels = n_channels * this->m_args.channel_multiplier; |
| 275 | return n_threads * (sizeof_input_ptr_array() + |
| 276 | sizeof_output_buffer(n_output_channels) + |
| 277 | sizeof_input_buffer(n_channels)); |
| 278 | } |
| 279 | }; |
| 280 | |
| 281 | template <class Strategy, unsigned OutputRows, unsigned int OutputCols> |
| 282 | class DepthwiseDepthfirstGeneric : public DepthwiseDepthfirstGenericBase<Strategy, OutputRows, OutputCols> |
| 283 | { |
| 284 | using Parent = DepthwiseDepthfirstGenericBase<Strategy, OutputRows, OutputCols>; |
| 285 | using TInput = typename Parent::TInput; |
| 286 | using TWeight = typename Parent::TWeight; |
| 287 | using TAccum = typename Parent::TAccum; |
| 288 | using TOutput = typename Parent::TOutput; |
| 289 | |
| 290 | const TAccum *m_bias = nullptr; |
| 291 | |
| 292 | public: |
| 293 | DepthwiseDepthfirstGeneric(const DepthwiseArgs &args) : Parent(args) |
| 294 | { |
| 295 | } |
| 296 | |
| 297 | DepthwiseDepthfirstGeneric(DepthwiseDepthfirstGeneric &) = delete; |
| 298 | DepthwiseDepthfirstGeneric &operator=(DepthwiseDepthfirstGeneric &) = delete; |
| 299 | |
| 300 | void pack_parameters(void *buffer, const void *bias, const void *weights, size_t ld_weight_col, size_t ld_weight_row) override |
| 301 | { |
| 302 | m_bias = static_cast<const TAccum *>(bias); |
| 303 | Parent::pack_parameters(buffer, bias, weights, ld_weight_col, ld_weight_row); |
| 304 | } |
| 305 | |
| 306 | using DepthwiseDepthfirstGenericBase<Strategy, OutputRows, OutputCols>::execute; |
| 307 | void execute( |
| 308 | const unsigned int batches, |
| 309 | const unsigned int input_height, |
| 310 | const unsigned int input_width, |
| 311 | const unsigned int input_channels, |
| 312 | const PaddingValues &padding, |
| 313 | const void *const _input, |
| 314 | const size_t ld_input_col, |
| 315 | const size_t ld_input_row, |
| 316 | const size_t ld_input_batch, |
| 317 | const void *const parameters, |
| 318 | const unsigned int output_height, |
| 319 | const unsigned int output_width, |
| 320 | void *const _output, |
| 321 | const size_t ld_output_col, |
| 322 | const size_t ld_output_row, |
| 323 | const size_t ld_output_batch, |
| 324 | void *const _working_space, |
| 325 | const unsigned int thread_id, |
| 326 | const unsigned int n_threads |
| 327 | ) const override |
| 328 | { |
| 329 | Strategy strat(this->m_args.cpu_info); |
| 330 | #ifdef CYCLE_PROFILING |
| 331 | arm_gemm::profiler prof; |
| 332 | #endif |
| 333 | |
| 334 | // Compute activation values |
| 335 | TAccum activation_min, activation_max; |
| 336 | if (std::numeric_limits<TAccum>::is_integer) |
| 337 | { |
| 338 | activation_min = std::numeric_limits<TAccum>::min(); |
| 339 | activation_max = std::numeric_limits<TAccum>::max(); |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | activation_min = static_cast<TAccum>(-std::numeric_limits<float>::infinity()); |
| 344 | activation_max = static_cast<TAccum>(std::numeric_limits<float>::infinity()); |
| 345 | } |
| 346 | |
| 347 | switch (this->m_args.activation.type) |
| 348 | { |
| 349 | case arm_gemm::Activation::Type::BoundedReLU: |
| 350 | activation_max = static_cast<TAccum>(this->m_args.activation.param1); |
| 351 | // Fall through |
| 352 | case arm_gemm::Activation::Type::ReLU: |
| 353 | activation_min = static_cast<TAccum>(0); |
| 354 | break; |
| 355 | default: |
| 356 | break; |
| 357 | } |
| 358 | |
| 359 | // Create a function to initialise the input buffer |
| 360 | const auto initialise_input_buffer = [] (TInput *const buffer, const unsigned int n) { |
| 361 | std::memset(buffer, 0, n * sizeof(TInput)); |
| 362 | }; |
| 363 | |
| 364 | // Create a function to execute a tile of work |
| 365 | const auto tile_fn = [&] (const TInput *const *const inptrs, TOutput *const * const outptrs) { |
| 366 | #ifdef CYCLE_PROFILING |
| 367 | auto p = prof.ScopedProfiler( |
| 368 | PROFILE_KERNEL, |
| 369 | (unsigned long) (OutputRows * OutputCols * this->m_args.kernel_rows* this->m_args.kernel_cols) |
| 370 | ); |
| 371 | #endif |
| 372 | strat.kernel(inptrs, outptrs, parameters, m_bias, |
| 373 | this->m_args.kernel_rows * this->m_args.kernel_cols, |
| 374 | this->m_args.input_channels, activation_min, activation_max); |
| 375 | }; |
| 376 | |
| 377 | // Call into a parent utility function to do the actual work. |
| 378 | Parent::execute_tiles( |
| 379 | tile_fn, initialise_input_buffer, |
| 380 | batches, input_height, input_width, input_channels, padding, |
| 381 | _input, ld_input_col, ld_input_row, ld_input_batch, |
| 382 | output_height, output_width, |
| 383 | _output, ld_output_col, ld_output_row, ld_output_batch, |
| 384 | _working_space, thread_id, n_threads |
| 385 | ); |
| 386 | } |
| 387 | }; |
| 388 | |
| 389 | } // namespace depthwise |
| 390 | } // namespace arm_conv |