Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +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/NEON/functions/NEConcatenateLayer.h" |
| 25 | |
| 26 | #include "arm_compute/runtime/NEON/functions/NEDepthConcatenateLayer.h" |
| 27 | #include "arm_compute/runtime/NEON/functions/NEWidthConcatenateLayer.h" |
| 28 | |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 30 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 31 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Error.h" |
| 33 | #include "arm_compute/core/ITensor.h" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Types.h" |
| 36 | #include "support/ToolchainSupport.h" |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | NEConcatenateLayer::NEConcatenateLayer() |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 41 | : _concat_function(nullptr), |
| 42 | _hconcat_kernels(), |
| 43 | _num_inputs(0), |
| 44 | _axis(Window::DimX) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 45 | { |
| 46 | } |
| 47 | |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 48 | Status NEConcatenateLayer::validate_h_concatenate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output) |
| 49 | { |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2); |
| 52 | |
| 53 | // Output auto inizialitation if not yet initialized |
| 54 | TensorInfo tmp_output_info = *output->clone(); |
| 55 | TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, Window::DimY); |
| 56 | auto_init_if_empty(tmp_output_info, output_shape, 1, inputs_vector[0]->data_type()); |
| 57 | |
| 58 | unsigned int offset = 0; |
| 59 | for(const auto &input : inputs_vector) |
| 60 | { |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 62 | ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, &tmp_output_info)); |
| 63 | offset += input->dimension(Window::DimY); |
| 64 | } |
| 65 | |
| 66 | return Status{}; |
| 67 | } |
| 68 | |
| 69 | void NEConcatenateLayer::configure_h_concatenate(std::vector<ITensor *> inputs_vector, ITensor *output) |
| 70 | { |
| 71 | _num_inputs = inputs_vector.size(); |
| 72 | |
| 73 | std::vector<ITensorInfo *> inputs_vector_info; |
| 74 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 75 | { |
| 76 | ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i)); |
| 77 | inputs_vector_info.emplace_back(inputs_vector.at(i)->info()); |
| 78 | } |
| 79 | TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, Window::DimY); |
| 80 | |
| 81 | // Output auto inizialitation if not yet initialized |
| 82 | auto_init_if_empty(*output->info(), output_shape, 1, inputs_vector[0]->info()->data_type()); |
| 83 | ARM_COMPUTE_ERROR_THROW_ON(validate_h_concatenate(inputs_vector_info, output->info())); |
| 84 | |
| 85 | unsigned int offset = 0; |
| 86 | |
| 87 | _hconcat_kernels = arm_compute::support::cpp14::make_unique<NEHeightConcatenateLayerKernel[]>(_num_inputs); |
| 88 | |
| 89 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 90 | { |
| 91 | _hconcat_kernels[i].configure(inputs_vector.at(i), offset, output); |
| 92 | offset += inputs_vector.at(i)->info()->dimension(Window::DimY); |
| 93 | } |
| 94 | } |
| 95 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 96 | void NEConcatenateLayer::configure(const std::vector<ITensor *> &inputs_vector, ITensor *output, DataLayoutDimension axis) |
| 97 | { |
| 98 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 99 | _axis = get_data_layout_dimension_index(output->info()->data_layout(), axis); |
| 100 | switch(_axis) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 101 | { |
| 102 | case 0: |
| 103 | { |
| 104 | auto func = support::cpp14::make_unique<NEWidthConcatenateLayer>(); |
| 105 | func->configure(inputs_vector, output); |
| 106 | _concat_function = std::move(func); |
| 107 | break; |
| 108 | } |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 109 | case 1: |
| 110 | { |
| 111 | configure_h_concatenate(inputs_vector, output); |
| 112 | break; |
| 113 | } |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 114 | case 2: |
| 115 | { |
| 116 | auto func = support::cpp14::make_unique<NEDepthConcatenateLayer>(); |
| 117 | func->configure(inputs_vector, output); |
| 118 | _concat_function = std::move(func); |
| 119 | break; |
| 120 | } |
| 121 | default: |
| 122 | ARM_COMPUTE_ERROR("Concatenation is supported across width and depth only!"); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | Status NEConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, DataLayoutDimension axis) |
| 127 | { |
| 128 | ARM_COMPUTE_RETURN_ERROR_ON(output == nullptr); |
| 129 | |
| 130 | switch(get_data_layout_dimension_index(output->data_layout(), axis)) |
| 131 | { |
| 132 | case 0: |
| 133 | ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayer::validate(inputs_vector, output)); |
| 134 | break; |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 135 | case 1: |
| 136 | ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate_h_concatenate(inputs_vector, output)); |
| 137 | break; |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 138 | case 2: |
| 139 | ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayer::validate(inputs_vector, output)); |
| 140 | break; |
| 141 | default: |
| 142 | ARM_COMPUTE_RETURN_ERROR_MSG("Concatenation is supported across width and depth only!"); |
| 143 | } |
| 144 | return Status{}; |
| 145 | } |
| 146 | |
| 147 | void NEConcatenateLayer::run() |
| 148 | { |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 149 | switch(_axis) |
| 150 | { |
| 151 | case 0: |
| 152 | case 2: |
| 153 | { |
| 154 | ARM_COMPUTE_ERROR_ON(_concat_function == nullptr); |
| 155 | _concat_function->run(); |
| 156 | break; |
| 157 | } |
| 158 | case 1: |
| 159 | { |
| 160 | for(unsigned i = 0; i < _num_inputs; ++i) |
| 161 | { |
| 162 | NEScheduler::get().schedule(_hconcat_kernels.get() + i, Window::DimY); |
| 163 | } |
| 164 | break; |
| 165 | } |
| 166 | default: |
| 167 | { |
| 168 | ARM_COMPUTE_ERROR("Axis not supported."); |
| 169 | break; |
| 170 | } |
| 171 | } |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 172 | } |
| 173 | } // namespace arm_compute |