Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 ARM Limited. |
| 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 | |
| 26 | #include "arm_compute/core/CL/ICLTensor.h" |
| 27 | #include "arm_compute/core/CL/kernels/CLReductionOperationKernel.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/core/utils/helpers/tensor_transform.h" |
| 30 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 31 | #include "support/ToolchainSupport.h" |
| 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | CLReduceMean::CLReduceMean(std::shared_ptr<IMemoryManager> memory_manager) |
| 36 | : _memory_group(std::move(memory_manager)), _reduction_kernels(), _reduced_outs(), _reshape(), _reduction_ops(), _keep_dims() |
| 37 | { |
| 38 | } |
| 39 | void CLReduceMean::configure(ICLTensor *input, const Coordinates &reduction_axis, bool keep_dims, ICLTensor *output) |
| 40 | { |
| 41 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 42 | |
| 43 | _reduction_ops = reduction_axis.num_dimensions(); |
| 44 | _reduction_kernels = arm_compute::support::cpp14::make_unique<CLReductionOperation[]>(_reduction_ops); |
| 45 | _reduced_outs = arm_compute::support::cpp14::make_unique<CLTensor[]>(_reduction_ops - (keep_dims ? 1 : 0)); |
| 46 | _keep_dims = keep_dims; |
| 47 | |
| 48 | // Perform reduction for every axis |
| 49 | for(unsigned int i = 0; i < _reduction_ops; ++i) |
| 50 | { |
| 51 | TensorShape out_shape = i == 0 ? input->info()->tensor_shape() : (_reduced_outs.get() + i - 1)->info()->tensor_shape(); |
| 52 | out_shape.set(reduction_axis[i], 1); |
| 53 | auto in = (i == 0) ? input : (_reduced_outs.get() + i - 1); |
| 54 | |
| 55 | if(i == _reduction_ops - 1 && keep_dims) |
| 56 | { |
| 57 | _reduction_kernels[i].configure(in, output, reduction_axis[i], ReductionOperation::MEAN_SUM); |
| 58 | } |
| 59 | else |
| 60 | { |
| 61 | _reduced_outs[i].allocator()->init(TensorInfo(out_shape, input->info()->num_channels(), input->info()->data_type(), input->info()->quantization_info())); |
| 62 | _memory_group.manage(_reduced_outs.get() + i); |
| 63 | _reduction_kernels[i].configure(in, _reduced_outs.get() + i, reduction_axis[i], ReductionOperation::MEAN_SUM); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // Allocate intermediate tensors |
| 68 | for(unsigned int i = 0; i < _reduction_ops - (keep_dims ? 1 : 0); ++i) |
| 69 | { |
| 70 | _reduced_outs[i].allocator()->allocate(); |
| 71 | } |
| 72 | |
| 73 | // Configure reshape layer if we want to drop the dimensions |
| 74 | if(!keep_dims) |
| 75 | { |
| 76 | TensorShape out_shape = input->info()->tensor_shape(); |
Michalis Spyrou | 96f8461 | 2018-10-24 14:01:04 +0100 | [diff] [blame] | 77 | |
| 78 | // We have to sort the reduction axis vectors in order for remove_dimension |
| 79 | // to work properly |
| 80 | Coordinates axis_copy = reduction_axis; |
| 81 | std::sort(axis_copy.begin(), axis_copy.begin() + _reduction_ops); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 82 | for(unsigned int i = 0; i < _reduction_ops; ++i) |
| 83 | { |
Michalis Spyrou | 96f8461 | 2018-10-24 14:01:04 +0100 | [diff] [blame] | 84 | out_shape.remove_dimension(axis_copy[i] - i); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 85 | } |
| 86 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(out_shape)); |
| 87 | _reshape.configure(_reduced_outs.get() + _reduction_ops - 1, output); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | Status CLReduceMean::validate(const ITensorInfo *input, const Coordinates &reduction_axis, bool keep_dims, const ITensorInfo *output) |
| 92 | { |
| 93 | ARM_COMPUTE_UNUSED(keep_dims); |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 95 | ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis.num_dimensions() > input->num_dimensions()); |
| 96 | |
| 97 | for(unsigned int i = 0; i < reduction_axis.num_dimensions(); ++i) |
| 98 | { |
| 99 | ARM_COMPUTE_RETURN_ERROR_ON(reduction_axis[i] > 3); |
Michalis Spyrou | 96f8461 | 2018-10-24 14:01:04 +0100 | [diff] [blame] | 100 | ARM_COMPUTE_RETURN_ERROR_ON(static_cast<unsigned int>(reduction_axis[i]) > input->num_dimensions() - 1); |
| 101 | if(output->total_size() > 0 && keep_dims) |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 102 | { |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(reduction_axis[i]) != 1); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 104 | } |
| 105 | |
Michalis Spyrou | e55a013 | 2018-10-26 10:48:56 +0100 | [diff] [blame] | 106 | ARM_COMPUTE_RETURN_ON_ERROR(CLReductionOperation::validate(input, output, reduction_axis[i], ReductionOperation::MEAN_SUM)); |
Michalis Spyrou | 7e9391b | 2018-10-05 14:49:28 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | return Status{}; |
| 110 | } |
| 111 | |
| 112 | void CLReduceMean::run() |
| 113 | { |
| 114 | _memory_group.acquire(); |
| 115 | |
| 116 | for(unsigned int i = 0; i < _reduction_ops; ++i) |
| 117 | { |
| 118 | _reduction_kernels[i].run(); |
| 119 | } |
| 120 | |
| 121 | if(!_keep_dims) |
| 122 | { |
| 123 | _reshape.run(); |
| 124 | } |
| 125 | _memory_group.release(); |
| 126 | } |
| 127 | } // namespace arm_compute |