Michele Di Giorgio | d556d7b | 2020-10-27 10:56:31 +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 "pool_common.hpp" |
| 28 | #include "utils.hpp" |
| 29 | |
| 30 | namespace arm_conv { |
| 31 | namespace pooling { |
| 32 | |
| 33 | template <class strategy> |
| 34 | class PoolingDepthfirstGeneric : public PoolingCommon<typename strategy::operand_type, typename strategy::return_type> |
| 35 | { |
| 36 | using TInput = typename strategy::operand_type; |
| 37 | using TOutput = typename strategy::return_type; |
| 38 | |
| 39 | const PoolingArgs m_args; // Copy of arguments |
| 40 | |
| 41 | unsigned int input_rows(void) const |
| 42 | { |
| 43 | return m_args.pool_window.rows; |
| 44 | } |
| 45 | |
| 46 | unsigned int input_cols(void) const |
| 47 | { |
| 48 | return m_args.pool_window.cols; |
| 49 | } |
| 50 | |
| 51 | public: |
| 52 | PoolingDepthfirstGeneric(const PoolingArgs &args) : m_args(args) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | PoolingDepthfirstGeneric(PoolingDepthfirstGeneric &) = delete; |
| 57 | PoolingDepthfirstGeneric &operator=(PoolingDepthfirstGeneric &) = delete; |
| 58 | |
| 59 | size_t sizeof_input_pointer_array(void) const |
| 60 | { |
| 61 | return sizeof(TInput *) * input_rows() * input_cols(); |
| 62 | } |
| 63 | |
| 64 | size_t get_working_size(unsigned int num_threads) const override |
| 65 | { |
| 66 | return num_threads * sizeof_input_pointer_array(); |
| 67 | } |
| 68 | |
| 69 | void execute( |
| 70 | const void *const input, |
| 71 | void *const output, |
| 72 | void *const working_space, |
| 73 | unsigned int thread_id, |
| 74 | unsigned int num_threads |
| 75 | ) const override |
| 76 | { |
| 77 | const size_t ld_input_col = m_args.n_channels; |
| 78 | const size_t ld_input_row = ld_input_col * m_args.input_cols; |
| 79 | const size_t ld_input_batch = ld_input_row * m_args.input_rows; |
| 80 | const size_t ld_output_col = ld_input_col; |
| 81 | const size_t ld_output_row = ld_output_col * m_args.output_cols; |
| 82 | const size_t ld_output_batch = ld_output_row * m_args.output_rows; |
| 83 | |
| 84 | execute( |
| 85 | input, ld_input_col, ld_input_row, ld_input_batch, |
| 86 | output, ld_output_col, ld_output_row, ld_output_batch, |
| 87 | working_space, |
| 88 | thread_id, num_threads |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | void execute( |
| 93 | const void *const input, |
| 94 | size_t ld_input_col, |
| 95 | size_t ld_input_row, |
| 96 | size_t ld_input_batch, |
| 97 | void *const output, |
| 98 | size_t ld_output_col, |
| 99 | size_t ld_output_row, |
| 100 | size_t ld_output_batch, |
| 101 | void *const working_space, |
| 102 | unsigned int thread_id, |
| 103 | unsigned int num_threads |
| 104 | ) const override |
| 105 | { |
| 106 | execute( |
| 107 | m_args.n_batches, m_args.input_rows, m_args.input_cols, |
| 108 | m_args.n_channels, |
| 109 | input, ld_input_col, ld_input_row, ld_input_batch, |
| 110 | m_args.padding, |
| 111 | m_args.output_rows, m_args.output_cols, |
| 112 | output, ld_output_col, ld_output_row, ld_output_batch, |
| 113 | working_space, |
| 114 | thread_id, num_threads |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | void execute( |
| 119 | unsigned int batches, |
| 120 | unsigned int height, |
| 121 | unsigned int width, |
| 122 | unsigned int channels, |
| 123 | const void *const _input, |
| 124 | size_t ld_input_col, |
| 125 | size_t ld_input_row, |
| 126 | size_t ld_input_batch, |
| 127 | const PaddingValues &padding, |
| 128 | unsigned int output_height, |
| 129 | unsigned int output_width, |
| 130 | void *const _output, |
| 131 | size_t ld_output_col, |
| 132 | size_t ld_output_row, |
| 133 | size_t ld_output_batch, |
| 134 | void *const _working_space, |
| 135 | unsigned int thread_id, |
| 136 | unsigned int num_threads |
| 137 | ) const override |
| 138 | { |
| 139 | strategy strat(m_args.cpu_info); |
| 140 | #ifdef CYCLE_PROFILING |
| 141 | arm_gemm::profiler prof; |
| 142 | #endif // CYCLE_PROFILING |
| 143 | |
| 144 | const unsigned int roundup_output_rows = roundup(output_height, num_threads); |
| 145 | const unsigned int rows_per_thread = roundup_output_rows / num_threads; |
| 146 | int start_out_height = static_cast<int>(thread_id * rows_per_thread); |
| 147 | int end_out_height = std::min<int>(output_height, static_cast<int>((thread_id + 1) * rows_per_thread)); |
| 148 | |
| 149 | unsigned int start_channel = 0; |
| 150 | unsigned int end_channel = channels; |
| 151 | if(output_height == 1) |
| 152 | { |
| 153 | const unsigned int channels_per_thread = roundup(channels, num_threads) / num_threads; |
| 154 | start_channel = thread_id * channels_per_thread; |
| 155 | end_channel = std::min(start_channel + channels_per_thread, channels); |
| 156 | |
| 157 | // Reset start and end rows |
| 158 | start_out_height = 0; |
| 159 | end_out_height = output_height; |
| 160 | } |
| 161 | |
Michele Di Giorgio | ef28340 | 2021-01-27 16:22:05 +0000 | [diff] [blame] | 162 | if(start_channel >= end_channel) |
| 163 | { |
| 164 | // Early exit in case of multiple threads parallelising on channels |
| 165 | return; |
| 166 | } |
| 167 | |
Michele Di Giorgio | d556d7b | 2020-10-27 10:56:31 +0000 | [diff] [blame] | 168 | // Cast input and output pointers into the right types |
| 169 | const TInput *const inptr = static_cast<const TInput *>(_input) + start_channel; |
| 170 | TOutput *const outptr = static_cast<TOutput *>(_output) + start_channel; |
| 171 | |
| 172 | // Grab the input pointer array |
| 173 | uint8_t *const working_space = static_cast<uint8_t *>(_working_space); |
| 174 | const TInput **const inptr_array = reinterpret_cast<const TInput **>(working_space + thread_id * sizeof_input_pointer_array()); |
| 175 | |
| 176 | // For each output tile, construct the requisite set of pointers and call |
| 177 | // into the kernel. |
| 178 | for (unsigned int batch = 0; batch < batches; batch++) |
| 179 | { |
| 180 | // Get batch pointers |
| 181 | const auto inptr_batch = inptr + batch * ld_input_batch; |
| 182 | auto outptr_row = outptr + batch * ld_output_batch + start_out_height * ld_output_row; |
| 183 | |
| 184 | for (int out_i = start_out_height; out_i < end_out_height; out_i++) |
| 185 | { |
| 186 | const int start_in_i = out_i * m_args.pool_stride.rows - padding.top; |
| 187 | const int end_in_i = start_in_i + m_args.pool_window.rows; |
| 188 | |
| 189 | // Compute top/bottom padding |
| 190 | const auto pad_top = static_cast<unsigned int>(std::max(0 - start_in_i, 0)); |
| 191 | const auto pad_bottom = static_cast<unsigned int>(std::max<int>(end_in_i - height, 0)); |
| 192 | const auto valid_rows = input_rows() - pad_top - pad_bottom; |
| 193 | |
Michele Di Giorgio | bae2237 | 2021-02-12 17:34:17 +0000 | [diff] [blame] | 194 | // Compute the number of pooling window rows which are contained in |
| 195 | // either the valid region of the input tensor, or the padding. |
| 196 | const auto padded_bottom = std::min<unsigned int>( |
| 197 | start_in_i + m_args.pool_window.rows, height + padding.bottom |
| 198 | ); |
| 199 | const auto n_total_rows = padded_bottom - start_in_i; |
| 200 | |
Michele Di Giorgio | d556d7b | 2020-10-27 10:56:31 +0000 | [diff] [blame] | 201 | auto outptr_col = outptr_row; |
| 202 | auto inptr_row = inptr_batch + (start_in_i + pad_top) * ld_input_row; |
| 203 | |
| 204 | for (int out_j = 0, start_in_j = -padding.left; |
| 205 | out_j < static_cast<int>(output_width); |
| 206 | out_j++, start_in_j += m_args.pool_stride.cols) |
| 207 | { |
| 208 | const int end_in_j = start_in_j + m_args.pool_window.cols; |
| 209 | |
| 210 | // Compute left/right padding |
| 211 | const auto pad_left = static_cast<unsigned int>(std::max(0 - start_in_j, 0)); |
| 212 | const auto pad_right = static_cast<unsigned int>(std::max<int>(0, end_in_j - width)); |
| 213 | const auto valid_cols = input_cols() - pad_left - pad_right; |
| 214 | |
Michele Di Giorgio | bae2237 | 2021-02-12 17:34:17 +0000 | [diff] [blame] | 215 | // Compute the number of pooling window columns which are contained |
| 216 | // in either the valid region of the input tensor, or the padding. |
| 217 | const auto padded_right = std::min<unsigned int>( |
| 218 | start_in_j + m_args.pool_window.cols, width + padding.right |
| 219 | ); |
| 220 | const auto n_total_cols = padded_right - start_in_j; |
| 221 | |
Michele Di Giorgio | d556d7b | 2020-10-27 10:56:31 +0000 | [diff] [blame] | 222 | // Construct the input pointer array - fill in all valid points |
| 223 | // contiguously. |
| 224 | const TInput **ptrs = inptr_array; |
| 225 | const TInput *rowptr = inptr_row + (start_in_j + pad_left) * ld_input_col; |
| 226 | for (auto i = 0u; i < valid_rows; i++) |
| 227 | { |
| 228 | const TInput *colptr = rowptr; |
| 229 | for (auto j = 0u; j < valid_cols; j++) |
| 230 | { |
| 231 | *(ptrs++) = colptr; |
| 232 | colptr += ld_input_col; |
| 233 | } |
| 234 | rowptr += ld_input_row; |
| 235 | } |
| 236 | |
| 237 | // Compute the number of valid cells |
| 238 | const auto valid_cells = valid_rows * valid_cols; |
Michele Di Giorgio | bae2237 | 2021-02-12 17:34:17 +0000 | [diff] [blame] | 239 | const auto cells_in_range = n_total_rows * n_total_cols; |
| 240 | const auto window_cells = m_args.exclude_padding ? valid_cells : cells_in_range; |
Michele Di Giorgio | d556d7b | 2020-10-27 10:56:31 +0000 | [diff] [blame] | 241 | |
| 242 | // Get the output pointer for this call |
| 243 | TOutput *outptr = outptr_col; |
| 244 | outptr_col += ld_output_col; |
| 245 | |
| 246 | #ifdef CYCLE_PROFILING |
| 247 | // TODO Work number |
| 248 | auto p = prof.ScopedProfiler(PROFILE_KERNEL, (unsigned long)(strategy::out_rows() * strategy::out_cols() * strategy::pool_rows() * strategy::pool_cols())); |
| 249 | #endif // CYCLE_PROFILING |
| 250 | strat.kernel(window_cells, valid_cells, end_channel - start_channel, inptr_array, outptr); |
| 251 | } |
| 252 | |
| 253 | outptr_row += ld_output_row; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | } // namespace pooling |
| 260 | } // namespace arm_conv |