Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | f6402dd | 2018-01-26 15:06:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +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 | #include "arm_compute/core/CL/kernels/CLReductionOperationKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/CL/CLHelpers.h" |
| 28 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 29 | #include "arm_compute/core/CL/ICLTensor.h" |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | #include "support/ToolchainSupport.h" |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 40 | namespace |
| 41 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 42 | // OpenCL kernel requires input width to be a power of 2 for x-axis. |
Michalis Spyrou | 25747e2 | 2018-08-08 17:12:38 +0100 | [diff] [blame] | 43 | constexpr unsigned int border_val = 64; |
| 44 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, unsigned int width) |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 46 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(op == ReductionOperation::SUM_SQUARE && input->data_type() == DataType::QASYMM8, "Not supported reduction operation for QASYMM8"); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis"); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(op == ReductionOperation::MEAN_SUM && axis == 0 && width == 0 && input->data_type() != DataType::QASYMM8); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 53 | |
| 54 | if(output->total_size() != 0) |
| 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | return Status{}; |
| 61 | } |
| 62 | |
| 63 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, unsigned int axis) |
| 64 | { |
| 65 | // Output tensor auto initialization if not yet initialized |
| 66 | TensorShape output_shape{ input->tensor_shape() }; |
| 67 | output_shape.set(axis, 1); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 68 | auto_init_if_empty(*output, output_shape, 1, input->data_type()); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 69 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 70 | const unsigned int num_elems_processed_per_iteration = (is_data_type_quantized(input->data_type()) && (axis == 0)) ? 1 : 16; |
| 71 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 72 | bool window_changed = false; |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 73 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 74 | switch(axis) |
| 75 | { |
| 76 | case 0: |
| 77 | { |
| 78 | if(is_data_type_quantized(input->data_type())) |
| 79 | { |
| 80 | AccessWindowHorizontal input_access(input, 0, input->dimension(0)); |
| 81 | AccessWindowHorizontal output_access(output, 0, 1); |
| 82 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 83 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | const unsigned int border_width = ((input->dimension(0) % border_val) != 0) ? border_val - input->dimension(0) % border_val : 0; |
| 88 | AccessWindowStatic input_access(input, 0, 0, input->dimension(0) + border_width, 1); |
| 89 | AccessWindowHorizontal output_access(output, 0, 1); |
| 90 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 91 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 92 | } |
| 93 | } |
| 94 | break; |
| 95 | case 1: |
| 96 | case 2: |
| 97 | case 3: |
| 98 | { |
| 99 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 100 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 101 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 102 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 103 | } |
| 104 | break; |
| 105 | default: |
| 106 | ARM_COMPUTE_ERROR("Not supported"); |
| 107 | } |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 108 | |
| 109 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 110 | |
| 111 | return std::make_tuple(err, win); |
| 112 | } |
| 113 | } // namespace |
| 114 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 115 | CLReductionOperationKernel::CLReductionOperationKernel() |
| 116 | : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE), _border_size() |
| 117 | { |
| 118 | } |
| 119 | |
| 120 | BorderSize CLReductionOperationKernel::border_size() const |
| 121 | { |
| 122 | return _border_size; |
| 123 | } |
| 124 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 125 | void CLReductionOperationKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op, unsigned int width) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 126 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 127 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 128 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 129 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op, width)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 130 | |
| 131 | _input = input; |
| 132 | _output = output; |
| 133 | _reduction_axis = axis; |
| 134 | _op = op; |
Michalis Spyrou | 343722b | 2018-06-05 13:04:40 +0100 | [diff] [blame] | 135 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 136 | // Set build options |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 137 | CLBuildOptions build_opts; |
| 138 | std::string data_type_promoted = get_cl_type_from_data_type(input->info()->data_type()); |
| 139 | if(is_data_type_quantized(input->info()->data_type()) && axis != 0) |
| 140 | { |
| 141 | data_type_promoted = "uint"; |
| 142 | } |
| 143 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 144 | build_opts.add_option("-DDATA_TYPE_PROMOTED=" + data_type_promoted); |
Michalis Spyrou | 8aaf93e | 2018-10-11 17:33:32 +0100 | [diff] [blame] | 145 | build_opts.add_option_if(op == ReductionOperation::SUM_SQUARE, "-DSUM_SQUARE="); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 146 | build_opts.add_option_if(op == ReductionOperation::MEAN_SUM, "-DMEAN"); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 147 | |
| 148 | switch(op) |
| 149 | { |
| 150 | case ReductionOperation::SUM_SQUARE: |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 151 | build_opts.add_option(("-DOPERATION=square_sum")); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 152 | break; |
| 153 | case ReductionOperation::SUM: |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 154 | case ReductionOperation::MEAN_SUM: |
| 155 | build_opts.add_option(("-DOPERATION=sum")); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 156 | break; |
| 157 | default: |
| 158 | ARM_COMPUTE_ERROR("Unsupported reduction operation"); |
| 159 | } |
| 160 | |
| 161 | // Create kernel |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 162 | cl::NDRange lws_hint = CLKernelLibrary::get().default_ndrange(); |
| 163 | std::string kernel_axis_name; |
| 164 | switch(axis) |
| 165 | { |
| 166 | case 0: |
| 167 | { |
| 168 | if(!is_data_type_quantized(input->info()->data_type())) |
| 169 | { |
| 170 | build_opts.add_option_if(op == ReductionOperation::MEAN_SUM, "-DWIDTH=" + support::cpp11::to_string(width)); |
| 171 | const unsigned int width_leftover = input->info()->dimension(0) % border_val; |
| 172 | const unsigned int border_width = (width_leftover != 0) ? border_val - width_leftover : 0; |
| 173 | const unsigned int num_of_threads = ((input->info()->dimension(0) + border_width) / 16); |
| 174 | kernel_axis_name = "x"; |
| 175 | |
| 176 | // Set the number of WG based on the input size. If input width is < 128 |
| 177 | // we can use fewer threads than 8. |
| 178 | lws_hint = cl::NDRange(std::min(8U, num_of_threads)); |
| 179 | _border_size = BorderSize(0, border_width, 0, 0); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(input->info()->dimension(0))); |
| 184 | kernel_axis_name = "quantized_x"; |
| 185 | } |
| 186 | } |
| 187 | break; |
| 188 | case 1: |
| 189 | build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(input->info()->dimension(1))); |
| 190 | kernel_axis_name = "y"; |
| 191 | break; |
| 192 | case 2: |
| 193 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 194 | kernel_axis_name = "z"; |
| 195 | break; |
| 196 | case 3: |
| 197 | build_opts.add_option("-DDEPTH=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 198 | build_opts.add_option("-DBATCH=" + support::cpp11::to_string(input->info()->dimension(3))); |
| 199 | kernel_axis_name = "w"; |
| 200 | break; |
| 201 | default: |
| 202 | ARM_COMPUTE_ERROR("Not supported"); |
| 203 | } |
| 204 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reduction_operation_" + kernel_axis_name, build_opts.options())); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 205 | |
| 206 | // Configure kernel window |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 207 | auto win_config = validate_and_configure_window(_input->info(), _output->info(), axis); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 208 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 209 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 210 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 211 | ICLKernel::configure_internal(std::get<1>(win_config), lws_hint); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 212 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 213 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 214 | Status CLReductionOperationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op, unsigned int width) |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 215 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 216 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op, width)); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 217 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), axis))); |
| 218 | |
| 219 | return Status{}; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void CLReductionOperationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 223 | { |
| 224 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 225 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 226 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 227 | switch(_reduction_axis) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 228 | { |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 229 | case 0: |
| 230 | { |
| 231 | // We use parallel reduction only in non quantized types |
| 232 | if(!is_data_type_quantized(_input->info()->data_type())) |
| 233 | { |
| 234 | // Set out window |
| 235 | Window out_window(window); |
| 236 | out_window.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 237 | |
| 238 | // Get first input and output slices |
| 239 | Window in_slice = window.first_slice_window_2D(); |
| 240 | Window out_slice = out_window.first_slice_window_2D(); |
| 241 | |
| 242 | // Reshape window |
| 243 | const unsigned int border_width = ((in_slice.x().end() % border_val) != 0) ? border_val - in_slice.x().end() % border_val : 0; |
| 244 | in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), in_slice.x().end() + border_width, in_slice.x().step())); |
| 245 | |
| 246 | // Set local sums buffer |
| 247 | unsigned int local_sum_size = lws_hint()[0] * _input->info()->element_size(); |
| 248 | _kernel.setArg(num_arguments_per_2D_tensor() * 2, local_sum_size, nullptr); |
| 249 | |
| 250 | do |
| 251 | { |
| 252 | unsigned int idx = 0; |
| 253 | add_2D_tensor_argument(idx, _input, in_slice); |
| 254 | add_2D_tensor_argument(idx, _output, out_slice); |
| 255 | enqueue(queue, *this, in_slice, lws_hint()); |
| 256 | } |
| 257 | while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice)); |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | // Get first input and output slices |
| 262 | Window window_in{ window }; |
| 263 | window_in.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _input->info()->dimension(0))); |
| 264 | |
| 265 | Window in_slice = window.first_slice_window_1D(); |
| 266 | Window out_slice = window.first_slice_window_1D(); |
| 267 | |
| 268 | do |
| 269 | { |
| 270 | unsigned int idx = 0; |
| 271 | add_1D_tensor_argument(idx, _input, in_slice); |
| 272 | add_1D_tensor_argument(idx, _output, out_slice); |
| 273 | enqueue(queue, *this, in_slice); |
| 274 | } |
| 275 | while(window_in.slide_window_slice_1D(in_slice) && window.slide_window_slice_1D(out_slice)); |
| 276 | } |
| 277 | } |
| 278 | break; |
| 279 | case 1: |
| 280 | { |
| 281 | // Get first input and output slices |
| 282 | Window window_in{ window }; |
| 283 | window_in.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1))); |
| 284 | Window in_slice = window_in.first_slice_window_2D(); |
| 285 | Window out_slice = window.first_slice_window_2D(); |
| 286 | |
| 287 | do |
| 288 | { |
| 289 | unsigned int idx = 0; |
| 290 | add_2D_tensor_argument(idx, _input, in_slice); |
| 291 | add_2D_tensor_argument(idx, _output, out_slice); |
| 292 | enqueue(queue, *this, in_slice); |
| 293 | } |
| 294 | while(window_in.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice)); |
| 295 | } |
| 296 | break; |
| 297 | case 2: |
| 298 | { |
| 299 | // Get first input and output slices |
| 300 | Window window_in{ window }; |
| 301 | window_in.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2))); |
| 302 | Window in_slice = window_in.first_slice_window_3D(); |
| 303 | Window out_slice = window.first_slice_window_3D(); |
| 304 | |
| 305 | do |
| 306 | { |
| 307 | unsigned int idx = 0; |
| 308 | add_3D_tensor_argument(idx, _input, in_slice); |
| 309 | add_3D_tensor_argument(idx, _output, out_slice); |
| 310 | enqueue(queue, *this, in_slice); |
| 311 | } |
| 312 | while(window_in.slide_window_slice_3D(in_slice) && window.slide_window_slice_3D(out_slice)); |
| 313 | } |
| 314 | break; |
| 315 | case 3: |
| 316 | { |
| 317 | // Get first input and output slices |
| 318 | Window window_in{ window }; |
| 319 | window_in.set(3, Window::Dimension(0, 1, 1)); |
| 320 | Window in_slice = window_in.first_slice_window_4D(); |
| 321 | Window out_slice = window.first_slice_window_4D(); |
| 322 | |
| 323 | do |
| 324 | { |
| 325 | unsigned int idx = 0; |
| 326 | add_4D_tensor_argument(idx, _input, in_slice); |
| 327 | add_4D_tensor_argument(idx, _output, out_slice); |
| 328 | enqueue(queue, *this, in_slice); |
| 329 | } |
| 330 | while(window_in.slide_window_slice_4D(in_slice) && window.slide_window_slice_4D(out_slice)); |
| 331 | } |
| 332 | break; |
| 333 | default: |
| 334 | ARM_COMPUTE_ERROR("Not supported"); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 335 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 336 | } |