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" |
| 30 | #include "arm_compute/core/FixedPoint.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | |
| 37 | #include "support/ToolchainSupport.h" |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
| 41 | CLReductionOperationKernel::CLReductionOperationKernel() |
| 42 | : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE), _border_size() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | BorderSize CLReductionOperationKernel::border_size() const |
| 47 | { |
| 48 | return _border_size; |
| 49 | } |
| 50 | |
| 51 | void CLReductionOperationKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op) |
| 52 | { |
| 53 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 54 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 55 | |
| 56 | // Output tensor auto initialization if not yet initialized |
| 57 | TensorShape output_shape{ input->info()->tensor_shape() }; |
| 58 | output_shape.set(axis, 1); |
| 59 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position()); |
| 60 | |
| 61 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 62 | ARM_COMPUTE_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); |
| 63 | ARM_COMPUTE_ERROR_ON_MSG(axis > 0, "Unsupported reduction axis, Supported axis is 0"); |
| 64 | |
| 65 | const unsigned int num_elems_processed_per_iteration = 16; |
| 66 | const unsigned int border_width = ((input->info()->dimension(0) % 128) != 0) ? 128 - input->info()->dimension(0) % 128 : 0; |
| 67 | |
| 68 | _input = input; |
| 69 | _output = output; |
| 70 | _reduction_axis = axis; |
| 71 | _op = op; |
| 72 | _lws_hint = cl::NDRange(8); |
| 73 | _border_size = BorderSize(0, border_width, 0, 0); |
| 74 | |
| 75 | // Set build options |
| 76 | std::set<std::string> build_opts; |
| 77 | build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()))); |
| 78 | build_opts.emplace(("-DVEC_SIZE=" + support::cpp11::to_string(num_elems_processed_per_iteration))); |
| 79 | if(is_data_type_fixed_point(input->info()->data_type())) |
| 80 | { |
| 81 | build_opts.emplace("-DFIXED_POINT_POSITION=" + support::cpp11::to_string(input->info()->fixed_point_position())); |
| 82 | } |
| 83 | |
| 84 | switch(op) |
| 85 | { |
| 86 | case ReductionOperation::SUM_SQUARE: |
| 87 | build_opts.emplace(("-DOPERATION=square_sum")); |
| 88 | break; |
| 89 | case ReductionOperation::SUM: |
| 90 | build_opts.emplace(("-DOPERATION=sum")); |
| 91 | break; |
| 92 | default: |
| 93 | ARM_COMPUTE_ERROR("Unsupported reduction operation"); |
| 94 | } |
| 95 | |
| 96 | // Create kernel |
| 97 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel("reduction_operation", build_opts)); |
| 98 | |
| 99 | // Configure kernel window |
| 100 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
| 101 | |
| 102 | AccessWindowStatic input_access(input->info(), 0, 0, input->info()->dimension(0) + border_width, 1); |
| 103 | AccessWindowHorizontal output_access(output->info(), 0, 1); |
| 104 | |
| 105 | update_window_and_padding(win, input_access, output_access); |
| 106 | output_access.set_valid_region(win, output->info()->valid_region()); |
| 107 | |
| 108 | ICLKernel::configure(win); |
| 109 | } |
| 110 | |
| 111 | void CLReductionOperationKernel::run(const Window &window, cl::CommandQueue &queue) |
| 112 | { |
| 113 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 114 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 115 | |
| 116 | // Set out window |
| 117 | Window out_window(window); |
| 118 | out_window.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 119 | |
| 120 | // Get first input and output slices |
Michalis Spyrou | f6402dd | 2018-01-26 15:06:19 +0000 | [diff] [blame] | 121 | Window in_slice = window.first_slice_window_2D(); |
| 122 | Window out_slice = out_window.first_slice_window_2D(); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 123 | |
| 124 | // Reshape window |
| 125 | const unsigned int border_width = ((in_slice.x().end() % 128) != 0) ? 128 - in_slice.x().end() % 128 : 0; |
| 126 | in_slice.set(Window::DimX, Window::Dimension(in_slice.x().start(), in_slice.x().end() + border_width, in_slice.x().step())); |
| 127 | |
| 128 | // Set local sums buffer |
Michalis Spyrou | f8349fe | 2017-09-14 13:50:31 +0100 | [diff] [blame] | 129 | unsigned int local_sum_size = _lws_hint[0] * _input->info()->element_size(); |
Michalis Spyrou | f6402dd | 2018-01-26 15:06:19 +0000 | [diff] [blame] | 130 | _kernel.setArg(num_arguments_per_2D_tensor() * 2, local_sum_size, nullptr); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 131 | |
| 132 | do |
| 133 | { |
| 134 | unsigned int idx = 0; |
Michalis Spyrou | f6402dd | 2018-01-26 15:06:19 +0000 | [diff] [blame] | 135 | add_2D_tensor_argument(idx, _input, in_slice); |
| 136 | add_2D_tensor_argument(idx, _output, out_slice); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 137 | enqueue(queue, *this, in_slice, _lws_hint); |
| 138 | } |
Michalis Spyrou | f6402dd | 2018-01-26 15:06:19 +0000 | [diff] [blame] | 139 | 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] | 140 | } |