Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +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/CLReduceMean.h" |
| 25 | |
Georgios Pinitas | 32bd4dd | 2019-05-16 14:23:00 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLValidate.h" |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLTensor.h" |
| 28 | #include "arm_compute/core/CL/kernels/CLReductionOperationKernel.h" |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Error.h" |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Types.h" |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 32 | |
| 33 | namespace arm_compute |
| 34 | { |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 35 | namespace |
| 36 | { |
| 37 | Status validate_config(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output) |
| 38 | { |
| 39 | ARM_COMPUTE_UNUSED(keep_dims); |
| 40 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 41 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Michalis Spyrou | 0b18d97 | 2020-01-30 18:11:13 +0000 | [diff] [blame] | 42 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32); |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 43 | ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() < 1); |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions()); |
| 45 | |
| 46 | const unsigned int reduction_ops = reduction_axis.num_dimensions(); |
| 47 | const int input_dims = input->num_dimensions(); |
| 48 | Coordinates axis_local = reduction_axis; |
| 49 | |
| 50 | for(unsigned int i = 0; i < axis_local.num_dimensions(); ++i) |
| 51 | { |
| 52 | //axis: The dimensions to reduce. Must be in the range [-rank(input_tensor), rank(input_tensor)). |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] < (-static_cast<int>(input->num_dimensions()))); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] >= static_cast<int>(input->num_dimensions())); |
| 55 | } |
| 56 | |
| 57 | if(output->tensor_shape().total_size() != 0) |
| 58 | { |
| 59 | // Only validate if not using auto_init for the output tensor |
| 60 | TensorShape out_shape = input->tensor_shape(); |
| 61 | // Validate output_shape only if not using auto_init |
| 62 | convert_negative_axis(axis_local, input_dims); |
| 63 | std::sort(axis_local.begin(), axis_local.begin() + reduction_ops); |
| 64 | for(unsigned int i = 0; i < reduction_ops; ++i) |
| 65 | { |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON(axis_local[i] > 3); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(axis_local[i]) > input->num_dimensions() - 1); |
| 68 | if(output->total_size() > 0 && keep_dims) |
| 69 | { |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(axis_local[i]) != 1); |
| 71 | } |
| 72 | if(keep_dims) |
| 73 | { |
| 74 | out_shape.set(axis_local[i], 1); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON(i > static_cast<unsigned int>(axis_local[i])); |
| 79 | const unsigned int remove_index = axis_local[i] - i; |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON(remove_index >= out_shape.num_dimensions()); |
| 81 | out_shape.remove_dimension(remove_index); |
| 82 | } |
| 83 | } |
| 84 | const TensorInfo out_info = input->clone()->set_tensor_shape(out_shape); |
| 85 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &out_info); |
| 86 | } |
| 87 | return Status{}; |
| 88 | } |
| 89 | } |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 90 | CLReduceMean::CLReduceMean(std::shared_ptr<IMemoryManager> memory_manager) |
| 91 | : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _reduction_ops(), _keep_dims() |
| 92 | { |
| 93 | } |
| 94 | void CLReduceMean::configure(ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output) |
| 95 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 96 | configure(CLKernelLibrary::get().get_compile_context(), input, reduction_axis, keep_dims, output); |
| 97 | } |
| 98 | |
| 99 | void CLReduceMean::configure(const CLCompileContext &compile_context, ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output) |
| 100 | { |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 101 | // Perform validate step |
| 102 | ARM_COMPUTE_ERROR_THROW_ON(CLReduceMean::validate(input->info(), reduction_axis, keep_dims, output->info())); |
| 103 | // Output auto inizialitation if not yet initialized |
| 104 | const TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_reduce_mean_shape(input, reduction_axis, keep_dims); |
| 105 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 106 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 107 | _reduction_ops = reduction_axis.num_dimensions(); |
| 108 | _reduction_kernels.resize(_reduction_ops); |
| 109 | _reduced_outs.resize(_reduction_ops - (keep_dims ? 1 : 0)); |
| 110 | _keep_dims = keep_dims; |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 111 | |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 112 | Coordinates axis_local = reduction_axis; |
| 113 | const int input_dims = input->info()->num_dimensions(); |
| 114 | |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 115 | convert_negative_axis(axis_local, input_dims); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 116 | |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 117 | // Perform reduction for every axis |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 118 | for(int i = 0; i < _reduction_ops; ++i) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 119 | { |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 120 | TensorShape out_shape = i == 0 ? input->info()->tensor_shape() : (&_reduced_outs[i - 1])->info()->tensor_shape(); |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 121 | out_shape.set(axis_local[i], 1); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 122 | auto in = (i == 0) ? input : (&_reduced_outs[i - 1]); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 123 | |
| 124 | if(i == _reduction_ops - 1 && keep_dims) |
| 125 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 126 | _reduction_kernels[i].configure(compile_context, in, output, axis_local[i], ReductionOperation::MEAN_SUM); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 127 | } |
| 128 | else |
| 129 | { |
| 130 | _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type(), input->info()->quantization_info())); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 131 | _memory_group.manage(&_reduced_outs[i]); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 132 | _reduction_kernels[i].configure(compile_context, in, &_reduced_outs[i], axis_local[i], ReductionOperation::MEAN_SUM); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | // Allocate intermediate tensors |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 137 | for(int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 138 | { |
| 139 | _reduced_outs[i].allocator()->allocate(); |
| 140 | } |
| 141 | |
| 142 | // Configure reshape layer if we want to drop the dimensions |
| 143 | if(!keep_dims) |
| 144 | { |
| 145 | TensorShape out_shape = input->info()->tensor_shape(); |
Michalis Spyrou | 96f8461 | 2018-10-24 14:01:04 +0100 | [diff] [blame] | 146 | |
| 147 | // We have to sort the reduction axis vectors in order for remove_dimension |
| 148 | // to work properly |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 149 | std::sort(axis_local.begin(), axis_local.begin() + _reduction_ops); |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 150 | for(int i = 0; i < _reduction_ops; ++i) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 151 | { |
Michalis Spyrou | 8d1b718 | 2019-01-02 15:54:03 +0000 | [diff] [blame] | 152 | out_shape.remove_dimension(axis_local[i] - i); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 153 | } |
| 154 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape)); |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame^] | 155 | _reshape.configure(compile_context, &_reduced_outs[_reduction_ops - 1], output); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | Status CLReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output) |
| 160 | { |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 161 | return validate_config(input, reduction_axis, keep_dims, output); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void CLReduceMean::run() |
| 165 | { |
Georgios Pinitas | da953f2 | 2019-04-02 17:27:03 +0100 | [diff] [blame] | 166 | MemoryGroupResourceScope scope_mg(_memory_group); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 167 | |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 168 | for(auto &kernel : _reduction_kernels) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 169 | { |
Pablo Tello | a0a4ba1 | 2019-12-11 13:04:34 +0000 | [diff] [blame] | 170 | kernel.run(); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if(!_keep_dims) |
| 174 | { |
| 175 | _reshape.run(); |
| 176 | } |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 177 | } |
| 178 | } // namespace arm_compute |