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 | 25747e2 | 2018-08-08 17:12:38 +0100 | [diff] [blame] | 42 | // OpenCL kernel requires input width to be a power of 2. |
| 43 | constexpr unsigned int border_val = 64; |
| 44 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op) |
| 46 | { |
| 47 | ARM_COMPUTE_UNUSED(op); |
| 48 | |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Michalis Spyrou | d1794eb | 2018-06-15 16:15:26 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() != DataLayout::NCHW); |
| 52 | |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 0, "Unsupported reduction axis, Supported axis is 0"); |
| 55 | |
| 56 | if(output->total_size() != 0) |
| 57 | { |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON(output->data_layout() != DataLayout::NCHW); |
| 60 | } |
| 61 | |
| 62 | return Status{}; |
| 63 | } |
| 64 | |
| 65 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, unsigned int axis) |
| 66 | { |
| 67 | // Output tensor auto initialization if not yet initialized |
| 68 | TensorShape output_shape{ input->tensor_shape() }; |
| 69 | output_shape.set(axis, 1); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 70 | auto_init_if_empty(*output, output_shape, 1, input->data_type()); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 71 | |
| 72 | const unsigned int num_elems_processed_per_iteration = 16; |
| 73 | |
| 74 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
Michalis Spyrou | 25747e2 | 2018-08-08 17:12:38 +0100 | [diff] [blame] | 75 | const unsigned int border_width = ((input->dimension(0) % border_val) != 0) ? border_val - input->dimension(0) % border_val : 0; |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 76 | |
| 77 | AccessWindowStatic input_access(input, 0, 0, input->dimension(0) + border_width, 1); |
| 78 | AccessWindowHorizontal output_access(output, 0, 1); |
| 79 | |
| 80 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 81 | output_access.set_valid_region(win, output->valid_region()); |
| 82 | |
| 83 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 84 | |
| 85 | return std::make_tuple(err, win); |
| 86 | } |
| 87 | } // namespace |
| 88 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 89 | CLReductionOperationKernel::CLReductionOperationKernel() |
| 90 | : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE), _border_size() |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | BorderSize CLReductionOperationKernel::border_size() const |
| 95 | { |
| 96 | return _border_size; |
| 97 | } |
| 98 | |
| 99 | void CLReductionOperationKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op) |
| 100 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 101 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 102 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 103 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 104 | |
| 105 | const unsigned int num_elems_processed_per_iteration = 16; |
Michalis Spyrou | 25747e2 | 2018-08-08 17:12:38 +0100 | [diff] [blame] | 106 | const unsigned int width_leftover = input->info()->dimension(0) % border_val; |
| 107 | const unsigned int border_width = (width_leftover != 0) ? border_val - width_leftover : 0; |
Michalis Spyrou | 343722b | 2018-06-05 13:04:40 +0100 | [diff] [blame] | 108 | const unsigned int num_of_threads = ((input->info()->dimension(0) + border_width) / 16); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 109 | |
| 110 | _input = input; |
| 111 | _output = output; |
| 112 | _reduction_axis = axis; |
| 113 | _op = op; |
Michalis Spyrou | 343722b | 2018-06-05 13:04:40 +0100 | [diff] [blame] | 114 | |
| 115 | // Set the number of WG based on the input size. If input width is < 128 |
| 116 | // we can use fewer threads than 8. |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 117 | cl::NDRange lws_hint = cl::NDRange(std::min(8U, num_of_threads)); |
| 118 | _border_size = BorderSize(0, border_width, 0, 0); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 119 | |
| 120 | // Set build options |
| 121 | std::set<std::string> build_opts; |
| 122 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 123 | build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 124 | |
| 125 | switch(op) |
| 126 | { |
| 127 | case ReductionOperation::SUM_SQUARE: |
| 128 | build_opts.emplace(("-DOPERATION=square_sum")); |
| 129 | break; |
| 130 | case ReductionOperation::SUM: |
| 131 | build_opts.emplace(("-DOPERATION=sum")); |
| 132 | break; |
| 133 | default: |
| 134 | ARM_COMPUTE_ERROR("Unsupported reduction operation"); |
| 135 | } |
| 136 | |
| 137 | // Create kernel |
| 138 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reduction_operation", build_opts)); |
| 139 | |
| 140 | // Configure kernel window |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 141 | auto win_config = validate_and_configure_window(_input->info(), _output->info(), axis); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 142 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 143 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 144 | |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 145 | ICLKernel::configure_internal(std::get<1>(win_config), lws_hint); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 146 | } |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 147 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 148 | Status CLReductionOperationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op) |
| 149 | { |
| 150 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op)); |
| 151 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), axis))); |
| 152 | |
| 153 | return Status{}; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void CLReductionOperationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 157 | { |
| 158 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 159 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 160 | |
| 161 | // Set out window |
| 162 | Window out_window(window); |
| 163 | out_window.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 164 | |
| 165 | // Get first input and output slices |
Michalis Spyrou | f6402dd | 2018-01-26 15:06:19 +0000 | [diff] [blame] | 166 | Window in_slice = window.first_slice_window_2D(); |
| 167 | Window out_slice = out_window.first_slice_window_2D(); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 168 | |
| 169 | // Reshape window |
Michalis Spyrou | 25747e2 | 2018-08-08 17:12:38 +0100 | [diff] [blame] | 170 | const unsigned int border_width = ((in_slice.x().end() % border_val) != 0) ? border_val - in_slice.x().end() % border_val : 0; |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 171 | in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), in_slice.x().end() + border_width, in_slice.x().step())); |
| 172 | |
| 173 | // Set local sums buffer |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 174 | unsigned int local_sum_size = lws_hint()[0] * _input->info()->element_size(); |
Michalis Spyrou | f6402dd | 2018-01-26 15:06:19 +0000 | [diff] [blame] | 175 | _kernel.setArg(num_arguments_per_2D_tensor() * 2, local_sum_size, nullptr); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 176 | |
| 177 | do |
| 178 | { |
| 179 | unsigned int idx = 0; |
Michalis Spyrou | f6402dd | 2018-01-26 15:06:19 +0000 | [diff] [blame] | 180 | add_2D_tensor_argument(idx, _input, in_slice); |
| 181 | add_2D_tensor_argument(idx, _output, out_slice); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 182 | enqueue(queue, *this, in_slice, lws_hint()); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 183 | } |
Michalis Spyrou | f6402dd | 2018-01-26 15:06:19 +0000 | [diff] [blame] | 184 | while(window.slide_window_slice_2D(in_slice) && window.slide_window_slice_2D(out_slice)); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 185 | } |