Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 1 | /* |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [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/runtime/CL/functions/CLReductionOperation.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/CL/kernels/CLReductionOperationKernel.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/PixelValue.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 33 | #include "arm_compute/runtime/Tensor.h" |
| 34 | #include "support/ToolchainSupport.h" |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 38 | namespace |
| 39 | { |
| 40 | unsigned int calculate_number_of_stages(const ITensorInfo *input) |
| 41 | { |
| 42 | // Calculate number of WGs. 16 elements per thread, 8 threads per WG |
| 43 | const unsigned int num_of_wg = ceil(input->dimension(0) / 128.f); |
| 44 | |
| 45 | // Calculate number of stages. First stage performs op and the rest reduction sum |
| 46 | // depending on the size of the input. Last stage should have only 1 WG. |
| 47 | const unsigned int num_of_stages = num_of_wg / 128 + 2; |
| 48 | |
| 49 | return num_of_stages; |
| 50 | } |
| 51 | } // namespace |
| 52 | |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 53 | CLReductionOperation::CLReductionOperation(std::shared_ptr<IMemoryManager> memory_manager) |
| 54 | : _memory_group(std::move(memory_manager)), _sums_vector(), _reduction_kernels_vector(), _border_handlers_vector(), _num_of_stages() |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 55 | { |
| 56 | } |
| 57 | |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 58 | Status CLReductionOperation::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op) |
| 59 | { |
| 60 | const unsigned int num_of_stages = calculate_number_of_stages(input); |
| 61 | |
| 62 | // Create temporary tensor infos |
| 63 | auto sums_vector = arm_compute::support::cpp14::make_unique<TensorInfo[]>(num_of_stages - 1); |
| 64 | |
| 65 | // Create intermediate tensor info |
| 66 | TensorShape shape{ input->tensor_shape() }; |
| 67 | |
| 68 | for(unsigned int i = 0; i < num_of_stages - 1; i++) |
| 69 | { |
| 70 | shape.set(0, ceil(shape.x() / 128.f)); |
| 71 | sums_vector[i].set_data_type(input->data_type()); |
| 72 | sums_vector[i].set_tensor_shape(shape); |
| 73 | sums_vector[i].set_num_channels(input->num_channels()); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // Validate ReductionOperation only on first kernel |
| 77 | ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(input, sums_vector.get(), axis, op)); |
| 78 | |
| 79 | // Validate ReductionOperation on intermediate stages |
| 80 | for(unsigned int i = 1; i < num_of_stages - 1; ++i) |
| 81 | { |
| 82 | ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(sums_vector.get() + i - 1, sums_vector.get() + i, axis, op)); |
| 83 | } |
| 84 | |
| 85 | // Validate ReductionOperation on the last stage |
| 86 | const unsigned int last_stage = num_of_stages - 1; |
| 87 | ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperationKernel::validate(sums_vector.get() + last_stage - 1, output, axis, op)); |
| 88 | |
| 89 | return Status{}; |
| 90 | } |
| 91 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 92 | void CLReductionOperation::configure(ICLTensor *input, ICLTensor *output, unsigned int axis, ReductionOperation op) |
| 93 | { |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 94 | _num_of_stages = calculate_number_of_stages(input->info()); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 95 | |
Georgios Pinitas | aec513c | 2017-09-15 19:36:30 +0100 | [diff] [blame] | 96 | // Create temporary tensors |
| 97 | _sums_vector = arm_compute::support::cpp14::make_unique<CLTensor[]>(_num_of_stages - 1); |
| 98 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 99 | // Configure reduction operation kernels |
| 100 | _reduction_kernels_vector = arm_compute::support::cpp14::make_unique<CLReductionOperationKernel[]>(_num_of_stages); |
| 101 | _border_handlers_vector = arm_compute::support::cpp14::make_unique<CLFillBorderKernel[]>(_num_of_stages); |
| 102 | |
| 103 | TensorShape shape{ input->info()->tensor_shape() }; |
| 104 | for(unsigned int i = 0; i < _num_of_stages - 1; i++) |
| 105 | { |
| 106 | shape.set(0, ceil(shape.x() / 128.f)); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 107 | _sums_vector[i].allocator()->init(TensorInfo(shape, input->info()->num_channels(), input->info()->data_type())); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Apply ReductionOperation only on first kernel |
Georgios Pinitas | aec513c | 2017-09-15 19:36:30 +0100 | [diff] [blame] | 111 | _memory_group.manage(_sums_vector.get()); |
| 112 | _reduction_kernels_vector[0].configure(input, _sums_vector.get(), axis, op); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 113 | _border_handlers_vector[0].configure(input, _reduction_kernels_vector[0].border_size(), BorderMode::CONSTANT, PixelValue(0)); |
Georgios Pinitas | aec513c | 2017-09-15 19:36:30 +0100 | [diff] [blame] | 114 | |
| 115 | // Apply ReductionOperation on intermediate stages |
| 116 | for(unsigned int i = 1; i < _num_of_stages - 1; ++i) |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 117 | { |
Georgios Pinitas | aec513c | 2017-09-15 19:36:30 +0100 | [diff] [blame] | 118 | _memory_group.manage(_sums_vector.get() + i); |
| 119 | _reduction_kernels_vector[i].configure(_sums_vector.get() + i - 1, _sums_vector.get() + i, axis, ReductionOperation::SUM); |
| 120 | _border_handlers_vector[i].configure(_sums_vector.get() + i - 1, _reduction_kernels_vector[i].border_size(), BorderMode::CONSTANT, PixelValue(0)); |
| 121 | _sums_vector[i - 1].allocator()->allocate(); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 122 | } |
Georgios Pinitas | aec513c | 2017-09-15 19:36:30 +0100 | [diff] [blame] | 123 | |
| 124 | // Apply ReductionOperation on the last stage |
| 125 | const unsigned int last_stage = _num_of_stages - 1; |
| 126 | _reduction_kernels_vector[last_stage].configure(_sums_vector.get() + last_stage - 1, output, axis, ReductionOperation::SUM); |
| 127 | _border_handlers_vector[last_stage].configure(_sums_vector.get() + last_stage - 1, _reduction_kernels_vector[last_stage].border_size(), BorderMode::CONSTANT, PixelValue(0)); |
| 128 | _sums_vector[last_stage - 1].allocator()->allocate(); |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void CLReductionOperation::run() |
| 132 | { |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 133 | _memory_group.acquire(); |
| 134 | |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 135 | for(unsigned int i = 0; i < _num_of_stages; ++i) |
| 136 | { |
| 137 | CLScheduler::get().enqueue(_border_handlers_vector[i], false); |
| 138 | CLScheduler::get().enqueue(_reduction_kernels_vector[i], false); |
| 139 | } |
Georgios Pinitas | 8a94e7c | 2017-09-15 19:06:47 +0100 | [diff] [blame] | 140 | |
| 141 | _memory_group.release(); |
John Richardson | 62385bc | 2018-04-20 13:11:36 +0100 | [diff] [blame] | 142 | } |