Georgios Pinitas | 6d9d6f4 | 2018-12-24 16:10:47 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2019 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/NEON/functions/NESplit.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 32 | |
| 33 | #include "support/ToolchainSupport.h" |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | NESplit::NESplit() |
| 38 | : _outputs_vector(), _slice_functions(), _num_outputs(0) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void NESplit::configure(const ITensor *input, const std::vector<ITensor *> &outputs, unsigned int axis) |
| 43 | { |
| 44 | // Create Slice functions |
| 45 | _num_outputs = outputs.size(); |
| 46 | _slice_functions = arm_compute::support::cpp14::make_unique<NESlice[]>(_num_outputs); |
| 47 | |
| 48 | // Get output shape |
| 49 | const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_split_shape(input->info(), axis, _num_outputs); |
| 50 | |
| 51 | // Extract output tensor info |
| 52 | std::vector<ITensorInfo *> outputs_info; |
| 53 | for(auto &output : outputs) |
| 54 | { |
| 55 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
| 56 | outputs_info.emplace_back(output->info()); |
| 57 | } |
| 58 | |
| 59 | // Validate |
| 60 | ARM_COMPUTE_ERROR_THROW_ON(NESplit::validate(input->info(), outputs_info, axis)); |
| 61 | |
| 62 | const size_t axis_split_step = output_shape[axis]; |
| 63 | unsigned int axis_offset = 0; |
| 64 | |
| 65 | // Start/End coordinates |
| 66 | Coordinates start_coords; |
| 67 | Coordinates end_coords; |
| 68 | for(unsigned int d = 0; d < output_shape.num_dimensions(); ++d) |
| 69 | { |
| 70 | end_coords.set(d, -1); |
| 71 | } |
| 72 | |
| 73 | for(unsigned int i = 0; i < _num_outputs; i++) |
| 74 | { |
| 75 | // Update coordinate on axis |
| 76 | start_coords.set(axis, axis_offset); |
| 77 | end_coords.set(axis, axis_offset + axis_split_step); |
| 78 | |
| 79 | // Configure slice function |
| 80 | _slice_functions[i].configure(input, outputs[i], start_coords, end_coords); |
| 81 | |
| 82 | // Set valid region from shape |
| 83 | outputs[i]->info()->set_valid_region(ValidRegion(Coordinates(), output_shape)); |
| 84 | |
| 85 | // Update axis offset |
| 86 | axis_offset += axis_split_step; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | Status NESplit::validate(const ITensorInfo *input, const std::vector<ITensorInfo *> &outputs, unsigned int axis) |
| 91 | { |
| 92 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
| 93 | ARM_COMPUTE_RETURN_ERROR_ON(axis >= input->num_dimensions()); |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON(outputs.size() < 2); |
| 95 | |
| 96 | // Get output shape |
| 97 | const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_split_shape(input, axis, outputs.size()); |
| 98 | ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() == 0); |
| 99 | |
| 100 | const size_t axis_split_step = output_shape[axis]; |
| 101 | unsigned int axis_offset = 0; |
| 102 | |
| 103 | // Start/End coordinates |
| 104 | Coordinates start_coords; |
| 105 | Coordinates end_coords; |
| 106 | for(unsigned int d = 0; d < output_shape.num_dimensions(); ++d) |
| 107 | { |
| 108 | end_coords.set(d, -1); |
| 109 | } |
| 110 | |
| 111 | // Validate output tensors |
| 112 | for(const auto &output : outputs) |
| 113 | { |
| 114 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 115 | |
| 116 | // Output auto inizialitation if not yet initialized |
| 117 | TensorInfo tmp_output_info = *output->clone(); |
| 118 | auto_init_if_empty(tmp_output_info, input->clone()->set_is_resizable(true).set_tensor_shape(output_shape)); |
| 119 | |
| 120 | // Update coordinate on axis |
| 121 | start_coords.set(axis, axis_offset); |
| 122 | end_coords.set(axis, axis_offset + axis_split_step); |
| 123 | |
| 124 | ARM_COMPUTE_RETURN_ON_ERROR(NESlice::validate(input, output, start_coords, end_coords)); |
| 125 | axis_offset += axis_split_step; |
| 126 | } |
| 127 | |
| 128 | return Status{}; |
| 129 | } |
| 130 | |
| 131 | void NESplit::run() |
| 132 | { |
| 133 | for(unsigned i = 0; i < _num_outputs; ++i) |
| 134 | { |
| 135 | _slice_functions[i].run(); |
| 136 | } |
| 137 | } |
| 138 | } // namespace arm_compute |