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 "common.hpp" |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 29 | #include <cstddef> |
| 30 | #include <tuple> |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 31 | |
| 32 | namespace arm_conv |
| 33 | { |
| 34 | namespace depthwise |
| 35 | { |
| 36 | using arm_gemm::Nothing; |
| 37 | |
| 38 | enum class DepthwiseMethod |
| 39 | { |
| 40 | DEFAULT, |
| 41 | DEPTHFIRST, |
| 42 | PLANAR, |
| 43 | }; |
| 44 | |
| 45 | struct KernelDescription |
| 46 | { |
| 47 | DepthwiseMethod method = DepthwiseMethod::DEFAULT; |
| 48 | std::string name = ""; |
| 49 | bool is_default = false; |
| 50 | uint64_t cycle_estimate = 0; |
| 51 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 52 | KernelDescription(DepthwiseMethod method, std::string name, bool is_default, uint64_t cycle_estimate) |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 53 | : method(method), name(name), is_default(is_default), cycle_estimate(cycle_estimate) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | KernelDescription() noexcept {}; |
| 58 | }; |
| 59 | |
| 60 | class IDepthwiseCommon |
| 61 | { |
| 62 | public: |
| 63 | virtual ~IDepthwiseCommon() = default; |
| 64 | |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 65 | // Get the name of the depthwise implementation |
| 66 | virtual std::string name() const = 0; |
| 67 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 68 | // Determine the amount of storage space required for the rearranged weights |
| 69 | // and bias. |
| 70 | virtual size_t get_storage_size(void) const = 0; |
| 71 | |
| 72 | // Rearrange the weights and biases into a storage buffer. |
| 73 | // Accepts a pointer to a buffer into which to store the packed parameters, a |
| 74 | // pointer the bias vector (which may be nullptr in the case of no bias) and |
| 75 | // a pointer to the array of weights (stored in HWIO order). |
| 76 | virtual void pack_parameters( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 77 | void *buffer, const void *biases, const void *weights, size_t ld_weight_col = 0, size_t ld_weight_row = 0) = 0; |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 78 | |
| 79 | // Determine the amount of working space required |
Michael Tyler | 8deee9b | 2023-06-30 11:26:05 +0100 | [diff] [blame] | 80 | virtual size_t get_working_size(unsigned int n_threads) const = 0; |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 81 | |
| 82 | // Execute the convolution over the specified area of memory. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 83 | virtual void execute(const void *input, // Pointer to input tensor |
| 84 | const void *parameters, // Packed parameters buffer |
| 85 | void *output, |
| 86 | void *working_space, |
| 87 | unsigned int thread_id, |
| 88 | unsigned int n_threads) const = 0; |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 89 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 90 | virtual void execute(const void *input, |
| 91 | size_t ld_input_col, |
| 92 | size_t ld_input_row, |
| 93 | size_t ld_input_batch, |
| 94 | const void *parameters, |
| 95 | void *output, |
| 96 | size_t ld_output_col, |
| 97 | size_t ld_output_row, |
| 98 | size_t ld_output_batch, |
| 99 | void *working_space, |
| 100 | unsigned int thread_id, |
| 101 | unsigned int n_threads) const = 0; |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 102 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 103 | virtual void execute(unsigned int batches, |
| 104 | unsigned int input_height, |
| 105 | unsigned int input_width, |
| 106 | unsigned int channels, |
| 107 | const PaddingValues &, |
| 108 | const void *input, |
| 109 | size_t ld_input_col, |
| 110 | size_t ld_input_row, |
| 111 | size_t ld_input_batch, |
| 112 | const void *parameters, |
| 113 | unsigned int output_height, |
| 114 | unsigned int output_width, |
| 115 | void *output, |
| 116 | size_t ld_output_col, |
| 117 | size_t ld_output_row, |
| 118 | size_t ld_output_batch, |
| 119 | void *working_space, |
| 120 | unsigned int thread_id, |
| 121 | unsigned int n_threads) const = 0; |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 124 | // To handle a dilation factor of D execute the kernel once for each d in |
| 125 | // [0..D). Each `d` corresponds to a portion or "view" of the input and output |
| 126 | // tensors. The output view corresponds to every Dth pixel starting from `d`; |
| 127 | // this function computes how many pixels are covered. The input view consists |
| 128 | // of an amount of before padding, every Dth pixel starting from an offset, and |
| 129 | // some after padding. This function computes the start padding, input offset, |
| 130 | // number of valid input pixels, and the after padding. |
| 131 | // |
| 132 | // Returns |
| 133 | // - Number of valid output pixels corresponding to `d` |
| 134 | // - Number of valid input pixels corresponding to `d` |
| 135 | // - Offset of the first pixel corresponding to `d` |
| 136 | // - Amount of padding in the view for `d` |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 137 | std::tuple<size_t, size_t, size_t, size_t, size_t> get_reduced_view_for_dilation(size_t out_size, |
| 138 | size_t in_size, |
| 139 | size_t d, |
| 140 | size_t dilation_factor, |
| 141 | size_t kernel_size, |
| 142 | size_t stride, |
| 143 | size_t pad_before); |
Pablo Marquez Tello | 4e2bbbb | 2023-01-09 17:21:01 +0000 | [diff] [blame] | 144 | |
Michele Di Giorgio | d02d5ed | 2021-01-22 09:47:04 +0000 | [diff] [blame] | 145 | } // namespace depthwise |
| 146 | } // namespace arm_conv |