Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [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 | #ifndef __ARM_COMPUTE_NEGEMMINTERLEAVEDPREPAREBWRAPPERKERNEL_H__ |
| 25 | #define __ARM_COMPUTE_NEGEMMINTERLEAVEDPREPAREBWRAPPERKERNEL_H__ |
| 26 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/INEKernel.h" |
| 30 | #include "arm_compute/core/NEON/kernels/assembly/Helpers.h" |
| 31 | #include "arm_compute/core/NEON/kernels/assembly/INEGEMMWrapperKernel.h" |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 34 | |
| 35 | namespace arm_compute |
| 36 | { |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 37 | /** Unit of work for @ref NEGEMMInterleavedPrepareBWrapperKernel to process */ |
| 38 | struct PrepareBWorkload |
| 39 | { |
| 40 | /** Constructor |
| 41 | * |
| 42 | * @param[in] offset_b Offset from the start of b's allocation |
| 43 | * @param[in] offset_transformed_b Offset from the start of transformed_b's allocation. |
| 44 | * @param[in] x0 First value to process along the X dimension (N). |
| 45 | * @param[in] xmax Last value to process along the X dimension (N). |
| 46 | * @param[in] k0 First value to process along the K dimension. |
| 47 | * @param[in] kmax Last value to process along the K dimension. |
| 48 | */ |
| 49 | PrepareBWorkload(unsigned int offset_b, unsigned int offset_transformed_b, unsigned int x0, unsigned int xmax, unsigned int k0, unsigned int kmax) |
| 50 | : _offset_b(offset_b), _offset_transformed_b(offset_transformed_b), _x0(x0), _xmax(xmax), _k0(k0), _kmax(kmax) |
| 51 | { |
| 52 | } |
| 53 | unsigned int _offset_b; /**< Offset from the start of b's allocation.*/ |
| 54 | unsigned int _offset_transformed_b; /**< Offset from the start of transformed_b's allocation.*/ |
| 55 | unsigned int _x0; /**< First value to process along the X dimension (N). */ |
| 56 | unsigned int _xmax; /**< Last value to process along the X dimension (N). */ |
| 57 | unsigned int _k0; /**< First value to process along the K dimension. */ |
| 58 | unsigned int _kmax; /**< Last value to process along the K dimension. */ |
| 59 | }; |
| 60 | |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 61 | namespace detail |
| 62 | { |
| 63 | // Call the lambda function for each workload generated by the passed window. |
| 64 | template <typename strategy, bool use_buffer_manager, typename Lambda> |
| 65 | void for_each_element_in_window(const Window &window, const ITensor *b, ITensor *transformed_b, unsigned int N, unsigned int K, Lambda &&lambda) |
| 66 | { |
| 67 | unsigned int wl_index = 0; |
| 68 | unsigned int num_buffers = 0, reshaped_block_size = 0; |
| 69 | |
| 70 | if(use_buffer_manager) |
| 71 | { |
| 72 | num_buffers = transformed_b->info()->tensor_shape()[1]; |
| 73 | reshaped_block_size = transformed_b->info()->strides_in_bytes().y(); |
| 74 | } |
| 75 | |
| 76 | unsigned int offset_transformed_b = transformed_b->info()->offset_first_element_in_bytes(); |
| 77 | execute_window_loop(window, [&](const Coordinates & coordinates) |
| 78 | { |
| 79 | const unsigned int x0 = coordinates.x(); |
| 80 | const unsigned int k0 = coordinates.y(); |
| 81 | const unsigned int multi = coordinates.z(); |
| 82 | |
| 83 | const unsigned int offset_b = b->info()->offset_element_in_bytes(Coordinates(0, 0, multi)); |
| 84 | const unsigned int xmax = std::min(x0 + window.x().step(), N); |
| 85 | const unsigned int kmax = std::min(k0 + window.y().step(), K); |
| 86 | |
| 87 | /* Figure out the size of each block. */ |
| 88 | unsigned int x_size = (xmax - x0); |
| 89 | unsigned int k_size = (kmax - k0); |
| 90 | |
| 91 | /* Round sizes up as needed. */ |
| 92 | x_size = ceil_to_multiple(x_size, strategy::out_width()); |
| 93 | k_size = ceil_to_multiple(k_size, strategy::k_unroll()); |
| 94 | |
| 95 | lambda(PrepareBWorkload(offset_b, offset_transformed_b, x0, xmax, k0, kmax)); |
| 96 | |
| 97 | //Each workload represents one block: |
| 98 | if(use_buffer_manager) |
| 99 | { |
| 100 | // Rotate through the BufferManager's buffers: |
| 101 | wl_index++; |
| 102 | offset_transformed_b = (wl_index % num_buffers) * reshaped_block_size; |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | offset_transformed_b += (x_size * k_size * sizeof(typename strategy::operand_type)); |
| 107 | } |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | // Calculate the size of transformed_b: |
| 112 | template <typename strategy> |
| 113 | unsigned int get_B_pretransposed_array_size(unsigned int N, unsigned int K, const BlockSizes &bs, unsigned int multis) |
| 114 | { |
| 115 | // How many full blocks do N / K contain ? |
| 116 | size_t num_full_k = K / bs.k_block; |
| 117 | size_t num_full_x = N / bs.x_block; |
| 118 | |
| 119 | ARM_COMPUTE_ERROR_ON(bs.x_block % strategy::out_width() != 0); |
| 120 | ARM_COMPUTE_ERROR_ON(bs.k_block % strategy::k_unroll() != 0); |
| 121 | |
| 122 | size_t normal_x_size = bs.x_block; |
| 123 | size_t normal_k_size = bs.k_block; |
| 124 | |
| 125 | // Round up the leftovers to be a multiple of the strategy processing size: |
| 126 | size_t left_over_x_size = ceil_to_multiple(N % bs.x_block, strategy::out_width()); |
| 127 | size_t left_over_k_size = ceil_to_multiple(K % bs.k_block, strategy::k_unroll()); |
| 128 | |
| 129 | // Calculate the total size of the buffer: |
| 130 | size_t total = num_full_k * normal_k_size * (num_full_x * normal_x_size + left_over_x_size); |
| 131 | total += left_over_k_size * (left_over_x_size + num_full_x * normal_x_size); |
| 132 | |
| 133 | total *= multis; |
| 134 | |
| 135 | return total; |
| 136 | } |
| 137 | } // namespace detail |
| 138 | |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 139 | /** Common interface for the templated wrappers around the B reshape NEON assembly implementations */ |
| 140 | class NEGEMMInterleavedPrepareBWrapperKernel : public INEKernel |
| 141 | { |
| 142 | public: |
| 143 | /** Transform the block at the given coordinates |
| 144 | * |
| 145 | * @param[in] wl Workload to process. |
| 146 | * @param[in] info Information about the current thread. |
| 147 | */ |
| 148 | virtual void transform(const PrepareBWorkload &wl, const ThreadInfo &info) = 0; |
| 149 | /** Generate an array of workloads |
| 150 | * |
| 151 | * @param[out] workloads Container to store the generated workloads. |
| 152 | */ |
| 153 | virtual void create_workloads(std::vector<PrepareBWorkload> &workloads) = 0; |
| 154 | /** Return the block_sizes used to resape B |
| 155 | * |
| 156 | * The same block sizes must be used to reshape A and for the matrix multiplication |
| 157 | * |
| 158 | * @return The block sizes used to reshape B. |
| 159 | */ |
| 160 | virtual BlockSizes block_sizes() const = 0; |
| 161 | |
| 162 | // Inherited methods overridden: |
| 163 | const char *name() const override |
| 164 | { |
| 165 | return "NEGEMMInterleavedPrepareBWrapperKernel"; |
| 166 | } |
| 167 | |
| 168 | bool is_parallelisable() const override |
| 169 | { |
| 170 | return false; // Can't run on arbitrary windows but can be parallelised using an array of workloads |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | /** Equivalent to arm_gemm::GemmInterleaved's strategy::transforms::PrepareB() but using Compute Library types. |
| 175 | */ |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 176 | template <typename strategy> |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 177 | class NEGEMMInterleavedPrepareBWrapperKernelTemplate : public NEGEMMInterleavedPrepareBWrapperKernel |
| 178 | { |
| 179 | public: |
| 180 | /** Configure the reshape B routine. |
| 181 | * |
| 182 | * @param[in] b Input matrix B. |
| 183 | * @param[out] transformed_b Reshaped matrix B. |
| 184 | * @param[in] transpose_b Also transpose B ? |
| 185 | * @param[in] ci CPU information |
| 186 | * @param[in] params M, N, K sizes. |
| 187 | */ |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 188 | void configure(const ITensor *b, ITensor *transformed_b, bool transpose_b, const CPUInfo &ci, const INEGEMMWrapperKernel::Params ¶ms) |
| 189 | { |
| 190 | const unsigned int multis = b->info()->tensor_shape().z(); |
| 191 | _Nsize = b->info()->tensor_shape().x(); |
| 192 | _Ksize = b->info()->tensor_shape().y(); |
| 193 | _b = b; |
| 194 | _transformed_b = transformed_b; |
| 195 | _transpose_b = transpose_b; |
| 196 | |
| 197 | _block_sizes = calculate_block_sizes<strategy>(ci, params.M, params.N, params.K); |
| 198 | |
| 199 | auto_init_if_empty(*transformed_b->info(), b->info()->clone()->set_tensor_shape(TensorShape{ detail::get_B_pretransposed_array_size<strategy>(_Nsize, _Ksize, _block_sizes, multis) })); |
| 200 | |
| 201 | Window window; |
| 202 | window.set(Window::DimX, Window::Dimension(0, ceil_to_multiple(_Nsize, _block_sizes.x_block), _block_sizes.x_block)); |
| 203 | window.set(Window::DimY, Window::Dimension(0, ceil_to_multiple(_Ksize, _block_sizes.k_block), _block_sizes.k_block)); |
| 204 | window.set(Window::DimZ, Window::Dimension(0, multis)); |
| 205 | |
| 206 | INEKernel::configure(window); |
| 207 | } |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 208 | |
| 209 | // Inherited methods overridden: |
Georgios Pinitas | 7cd26d4 | 2019-01-09 18:35:17 +0000 | [diff] [blame] | 210 | void transform(const PrepareBWorkload &wl, const ThreadInfo &info) override |
| 211 | { |
| 212 | strategy strat(info.cpu_info); |
| 213 | strat.transforms.PrepareB(reinterpret_cast<typename strategy::operand_type *>(_transformed_b->buffer() + wl._offset_transformed_b), |
| 214 | reinterpret_cast<typename strategy::operand_type *>(_b->buffer() + wl._offset_b), |
| 215 | _b->info()->strides_in_bytes().y() / sizeof(typename strategy::operand_type), |
| 216 | wl._x0, wl._xmax, wl._k0, wl._kmax, _transpose_b); |
| 217 | } |
| 218 | void create_workloads(std::vector<PrepareBWorkload> &workloads) override |
| 219 | { |
| 220 | detail::for_each_element_in_window<strategy, true>(window(), _b, _transformed_b, _Nsize, _Ksize, [&workloads](PrepareBWorkload && wl) |
| 221 | { |
| 222 | workloads.push_back(std::move(wl)); |
| 223 | }); |
| 224 | } |
| 225 | void run(const Window &window, const ThreadInfo &info) override |
| 226 | { |
| 227 | ARM_COMPUTE_ERROR_ON_MISMATCHING_WINDOWS(window, INEKernel::window()); |
| 228 | detail::for_each_element_in_window<strategy, false>(window, _b, _transformed_b, _Nsize, _Ksize, [&](PrepareBWorkload && wl) |
| 229 | { |
| 230 | this->transform(wl, info); |
| 231 | }); |
| 232 | } |
| 233 | BlockSizes block_sizes() const override |
| 234 | { |
| 235 | return _block_sizes; |
| 236 | } |
Anthony Barbier | 3d677cc | 2018-07-23 16:42:59 +0100 | [diff] [blame] | 237 | |
| 238 | private: |
| 239 | const ITensor *_b |
| 240 | { |
| 241 | nullptr |
| 242 | }; |
| 243 | ITensor *_transformed_b{ nullptr }; |
| 244 | unsigned int _Nsize{ 0 }; |
| 245 | unsigned int _Ksize{ 0 }; |
| 246 | bool _transpose_b{ false }; |
| 247 | BlockSizes _block_sizes{}; |
| 248 | }; |
| 249 | |
| 250 | } // namespace arm_compute |
| 251 | #endif /* __ARM_COMPUTE_NEGEMMINTERLEAVEDPREPAREBWRAPPERKERNEL_H__ */ |