Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | #include "arm_compute/core/NEON/kernels/NEGEMMInterleaveBlockedKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/NEON/INEKernel.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
| 33 | |
| 34 | #include <arm_neon.h> |
| 35 | #include <cstddef> |
| 36 | #include <cstdint> |
| 37 | #include <tuple> |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
| 41 | namespace |
| 42 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 43 | TensorShape get_output_shape(const ITensorInfo *input, unsigned int block_height) |
| 44 | { |
| 45 | TensorShape output_shape = input->tensor_shape(); |
| 46 | const float interleave_by_f32 = block_height; |
| 47 | output_shape.set(0, input->dimension(0) * interleave_by_f32); |
| 48 | output_shape.set(1, std::ceil(static_cast<float>(input->dimension(1)) / interleave_by_f32)); |
| 49 | return output_shape; |
| 50 | } |
| 51 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 52 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int block_width, unsigned int block_height) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 53 | { |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(block_height < 1, "Block height must be greater than 0"); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(block_width < 1, "Block window must be greater than 0"); |
| 58 | |
| 59 | if(output->total_size() != 0) |
| 60 | { |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input, block_height)); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 64 | } |
| 65 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 66 | return Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 69 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, unsigned int block_width, unsigned int block_height) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 70 | { |
| 71 | const unsigned int num_elems_processed_per_iteration_x = block_width; |
| 72 | const unsigned int num_elems_processed_per_iteration_y = block_height; |
| 73 | bool window_changed = false; |
| 74 | |
| 75 | // Configure kernel window |
| 76 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 77 | const float scaley_factor = 1.f / block_height; |
| 78 | |
| 79 | AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 80 | window_changed = window_changed || update_window_and_padding(win, input_access); |
| 81 | |
| 82 | // Configure window in case of configured output |
| 83 | if(output->total_size() != 0) |
| 84 | { |
| 85 | AccessWindowRectangle output_access(output, |
| 86 | 0, 0, |
| 87 | num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y, |
| 88 | 1, num_elems_processed_per_iteration_y, scaley_factor); |
| 89 | window_changed = window_changed || update_window_and_padding(win, output_access); |
| 90 | output_access.set_valid_region(win, input->valid_region()); |
| 91 | } |
| 92 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 93 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 94 | return std::make_pair(err, win); |
| 95 | } |
| 96 | |
Pablo Tello | eae4ce0 | 2017-10-13 15:24:49 +0100 | [diff] [blame] | 97 | inline void gemm_interleave_blocked_transposed_8bit(const ITensor *input, ITensor *output, const Window &window, unsigned int block_width, unsigned int block_height) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 98 | { |
Pablo Tello | eae4ce0 | 2017-10-13 15:24:49 +0100 | [diff] [blame] | 99 | const size_t in_stride = input->info()->strides_in_bytes()[1]; |
| 100 | |
| 101 | const unsigned int in_height = input->info()->dimension(1); |
| 102 | const unsigned int in_width = input->info()->dimension(0); |
| 103 | |
| 104 | const float scale_y_factor = 1.f / float(block_height); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 105 | |
| 106 | // Set window for output tensor |
| 107 | Window win_out(window); |
| 108 | win_out.scale(Window::DimY, scale_y_factor); |
| 109 | Iterator in(input, window); |
| 110 | |
| 111 | win_out.set_dimension_step(Window::DimX, block_width * block_height); |
| 112 | Iterator out(output, win_out); |
Pablo Tello | eae4ce0 | 2017-10-13 15:24:49 +0100 | [diff] [blame] | 113 | |
| 114 | execute_window_loop(window, [&](const Coordinates &) |
| 115 | { |
| 116 | std::fill_n(out.ptr(), block_width * block_height, 0); |
| 117 | }, |
| 118 | out); |
| 119 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 120 | execute_window_loop(window, [&](const Coordinates & id) |
| 121 | { |
Pablo Tello | eae4ce0 | 2017-10-13 15:24:49 +0100 | [diff] [blame] | 122 | for(unsigned int z = id.y(); (z < in_width) && z < (id.y() + block_height); ++z) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 123 | { |
Pablo Tello | eae4ce0 | 2017-10-13 15:24:49 +0100 | [diff] [blame] | 124 | int j = (z - id.y()) * block_width; |
| 125 | for(unsigned int b = id.x(); (b < in_height) && (b < (id.x() + block_width)); ++b) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 126 | { |
Pablo Tello | eae4ce0 | 2017-10-13 15:24:49 +0100 | [diff] [blame] | 127 | *(out.ptr() + j++) = *(input->buffer() + b * in_stride + z); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | }, |
| 131 | in, out); |
| 132 | } |
| 133 | |
Pablo Tello | eae4ce0 | 2017-10-13 15:24:49 +0100 | [diff] [blame] | 134 | inline void gemm_interleave_blocked_8bit(const ITensor *input, ITensor *output, const Window &window, unsigned int block_width, unsigned int block_height) |
| 135 | { |
| 136 | const size_t in_stride = input->info()->strides_in_bytes()[1]; |
| 137 | |
| 138 | const unsigned int in_height = input->info()->dimension(1); |
| 139 | const unsigned int in_width = input->info()->dimension(0); |
| 140 | |
| 141 | const float scale_y_factor = 1.f / float(block_height); |
| 142 | |
| 143 | // Set window for output tensor |
| 144 | Window win_out(window); |
| 145 | win_out.scale(Window::DimY, scale_y_factor); |
| 146 | Iterator in(input, window); |
| 147 | |
| 148 | win_out.set_dimension_step(Window::DimX, block_width * block_height); |
| 149 | Iterator out(output, win_out); |
| 150 | |
| 151 | execute_window_loop(window, [&](const Coordinates &) |
| 152 | { |
| 153 | std::fill_n(out.ptr(), block_width * block_height, 0); |
| 154 | }, |
| 155 | out); |
| 156 | |
| 157 | execute_window_loop(window, [&](const Coordinates & id) |
| 158 | { |
| 159 | for(unsigned int z = id.y(); (z < in_height) && z < (id.y() + block_height); ++z) |
| 160 | { |
| 161 | int j = (z - id.y()) * block_width; |
| 162 | for(unsigned int b = id.x(); (b < in_width) && (b < (id.x() + block_width)); ++b) |
| 163 | { |
| 164 | *(out.ptr() + j++) = *(input->buffer() + z * in_stride + b); |
| 165 | } |
| 166 | } |
| 167 | }, |
| 168 | in, out); |
| 169 | } |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 170 | } // namespace |
| 171 | |
| 172 | NEGEMMInterleaveBlockedKernel::NEGEMMInterleaveBlockedKernel() |
| 173 | : _block_height(0), _block_width(0), _transpose(false) |
| 174 | { |
| 175 | } |
| 176 | |
| 177 | void NEGEMMInterleaveBlockedKernel::configure(const ITensor *input, ITensor *output, unsigned int block_height, unsigned int block_width, bool transpose) |
| 178 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 179 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 180 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 181 | // Output auto inizialitation if not yet initialized |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 182 | auto_init_if_empty(*output->info(), get_output_shape(input->info(), block_height), 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 183 | |
| 184 | // Perform validation step |
| 185 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), block_width, block_height)); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 186 | |
| 187 | _input = input; |
| 188 | _output = output; |
| 189 | _block_height = block_height; |
| 190 | _block_width = block_width; |
| 191 | _transpose = transpose; |
| 192 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 193 | // Configure kernel window |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 194 | auto win_config = validate_and_configure_window(input->info(), output->info(), block_width, block_height); |
| 195 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 196 | INEKernel::configure(win_config.second); |
| 197 | } |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 198 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 199 | Status NEGEMMInterleaveBlockedKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int block_height, unsigned int block_width, bool transpose) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 200 | { |
| 201 | ARM_COMPUTE_UNUSED(transpose); |
| 202 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, block_width, block_height)); |
| 203 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), block_width, block_height).first); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 204 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 205 | return Status{}; |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void NEGEMMInterleaveBlockedKernel::run(const Window &window, const ThreadInfo &info) |
| 209 | { |
| 210 | ARM_COMPUTE_UNUSED(info); |
| 211 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 212 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Pablo Tello | eae4ce0 | 2017-10-13 15:24:49 +0100 | [diff] [blame] | 213 | if(_transpose) |
| 214 | { |
| 215 | gemm_interleave_blocked_transposed_8bit(_input, _output, window, _block_width, _block_height); |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | gemm_interleave_blocked_8bit(_input, _output, window, _block_width, _block_height); |
| 220 | } |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 221 | } |