Isabella Gottardi | ee7c15d | 2018-12-17 16:15:34 +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/core/NEON/kernels/NEStackLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/IAccessWindow.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 36 | |
| 37 | #include "support/ToolchainSupport.h" |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | using namespace arm_compute::misc::shape_calculator; |
| 41 | |
| 42 | namespace |
| 43 | { |
| 44 | Status validate_arguments(const ITensorInfo *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, const ITensorInfo *output) |
| 45 | { |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S8, |
| 48 | DataType::U16, DataType::S16, DataType::U32, DataType::S32, |
| 49 | DataType::F16, DataType::F32); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON(idx_input >= num_tensors); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(axis > input->num_dimensions()); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 53 | |
| 54 | if(output->total_size() != 0) |
| 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), compute_stack_shape(*input, axis, num_tensors)); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Isabella Gottardi | ee7c15d | 2018-12-17 16:15:34 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | return Status{}; |
| 62 | } |
| 63 | |
| 64 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int axis, unsigned int num_tensors, ITensorInfo *output) |
| 65 | { |
| 66 | // Output auto inizialitation if not yet initialized |
| 67 | auto_init_if_empty(*output, input->clone()->set_tensor_shape(compute_stack_shape(*input, axis, num_tensors))); |
| 68 | |
| 69 | // Configure kernel window |
| 70 | Window win = calculate_max_window(*input); |
| 71 | |
| 72 | return std::make_pair(Status{}, win); |
| 73 | } |
| 74 | |
| 75 | inline Coordinates shift_from_axis_and_replace_coordinate(const Coordinates &id, unsigned int axis, unsigned int idx_input) |
| 76 | { |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 77 | constexpr int max_out_coord = 5; // Input shape is max a 4D shape, output is max 5D |
| 78 | Coordinates id_out = id; |
Isabella Gottardi | ee7c15d | 2018-12-17 16:15:34 +0000 | [diff] [blame] | 79 | for(unsigned int i = max_out_coord - 1; i > axis; --i) |
| 80 | { |
| 81 | id_out.set(i, id[i - 1]); |
| 82 | } |
| 83 | id_out.set(axis, idx_input); |
| 84 | return id_out; |
| 85 | } |
| 86 | } // namespace |
| 87 | |
| 88 | NEStackLayerKernel::NEStackLayerKernel() |
| 89 | : _input(nullptr), _output(nullptr), _axis(), _idx_input(), _func(nullptr) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | void NEStackLayerKernel::configure(const ITensor *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, ITensor *output) |
| 94 | { |
| 95 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 96 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), axis, idx_input, num_tensors, output->info())); |
| 97 | |
| 98 | _input = input; |
| 99 | _output = output; |
| 100 | _axis = axis; |
| 101 | _idx_input = idx_input; |
| 102 | |
| 103 | switch(input->info()->element_size()) |
| 104 | { |
| 105 | case 1: |
| 106 | _func = &NEStackLayerKernel::run_stack<uint8_t>; |
| 107 | break; |
| 108 | case 2: |
| 109 | _func = &NEStackLayerKernel::run_stack<uint16_t>; |
| 110 | break; |
| 111 | case 4: |
| 112 | _func = &NEStackLayerKernel::run_stack<uint32_t>; |
| 113 | break; |
| 114 | default: |
| 115 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | // Configure kernel window |
| 120 | auto win_config = validate_and_configure_window(input->info(), axis, num_tensors, output->info()); |
| 121 | |
| 122 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 123 | INEKernel::configure(win_config.second); |
| 124 | } |
| 125 | |
| 126 | Status NEStackLayerKernel::validate(const ITensorInfo *input, unsigned int axis, unsigned int idx_input, unsigned int num_tensors, const ITensorInfo *output) |
| 127 | { |
| 128 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, axis, idx_input, num_tensors, output)); |
| 129 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), axis, num_tensors, output->clone().get()).first); |
| 130 | return Status{}; |
| 131 | } |
| 132 | |
| 133 | void NEStackLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 134 | { |
| 135 | ARM_COMPUTE_UNUSED(info); |
| 136 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 137 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 138 | |
| 139 | if(_func != nullptr) |
| 140 | { |
| 141 | (this->*_func)(window); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | template <typename T> |
| 146 | void NEStackLayerKernel::run_stack(const Window &window) |
| 147 | { |
| 148 | Window window_out; |
| 149 | window_out.use_tensor_dimensions(_output->info()->tensor_shape()); |
| 150 | |
| 151 | Iterator input(_input, window); |
| 152 | Iterator output(_output, window_out); |
| 153 | |
| 154 | const int stride_x = _output->info()->strides_in_bytes()[0]; |
| 155 | const int stride_y = _output->info()->num_dimensions() >= 1 ? _output->info()->strides_in_bytes()[1] : 0; |
| 156 | const int stride_z = _output->info()->num_dimensions() >= 2 ? _output->info()->strides_in_bytes()[2] : 0; |
| 157 | const int stride_w = _output->info()->num_dimensions() >= 3 ? _output->info()->strides_in_bytes()[3] : 0; |
| 158 | const int stride_k = _output->info()->num_dimensions() >= 4 ? _output->info()->strides_in_bytes()[4] : 0; |
| 159 | |
| 160 | execute_window_loop(window, [&](const Coordinates & id) |
| 161 | { |
| 162 | Coordinates id_out = shift_from_axis_and_replace_coordinate(id, _axis, _idx_input); |
| 163 | const int idx = id_out[0] * stride_x + id_out[1] * stride_y + id_out[2] * stride_z + id_out[3] * stride_w + id_out[4] * stride_k; |
| 164 | *(reinterpret_cast<T *>(output.ptr() + idx)) = *(reinterpret_cast<const T *>(input.ptr())); |
| 165 | }, |
| 166 | input); |
| 167 | } |