Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEWeightsReshapeKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Helpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 28 | #include "src/core/helpers/AutoConfiguration.h" |
| 29 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 31 | namespace arm_compute |
| 32 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | namespace |
| 34 | { |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 35 | TensorShape get_output_shape(const ITensorInfo *input, bool has_bias) |
| 36 | { |
| 37 | TensorShape output_shape{ input->tensor_shape() }; |
| 38 | |
| 39 | output_shape.collapse(3); |
| 40 | const size_t tmp_dim = output_shape[0]; |
| 41 | output_shape.set(0, output_shape[1]); |
| 42 | output_shape.set(1, tmp_dim + (has_bias ? 1 : 0)); |
| 43 | |
| 44 | return output_shape; |
| 45 | } |
| 46 | |
| 47 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output) |
| 48 | { |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 50 | //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use Neon FP16 instructions. |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 52 | |
| 53 | if(biases != nullptr) |
| 54 | { |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(input->data_type())); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->num_dimensions() != 1)); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->num_dimensions() != 2)); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->dimension(0) != input->tensor_shape()[3])); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->dimension(0) != input->tensor_shape()[3] || biases->dimension(1) != input->tensor_shape()[4])); |
| 61 | } |
| 62 | |
| 63 | // Checks performed when output is configured |
| 64 | if(output->total_size() != 0) |
| 65 | { |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input, biases != nullptr)); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 68 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | return Status{}; |
| 72 | } |
| 73 | |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 74 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input) |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 75 | { |
| 76 | Window window = calculate_max_window(*input, Steps()); |
| 77 | window.set(Window::DimX, Window::Dimension(0, input->dimension(0), input->dimension(0))); |
| 78 | window.set(Window::DimY, Window::Dimension(0, input->dimension(1), input->dimension(1))); |
| 79 | window.set(Window::DimZ, Window::Dimension(0, input->dimension(2), input->dimension(2))); |
| 80 | |
| 81 | // The NEConvolutionLayerWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 82 | |
| 83 | return std::make_pair(Status{}, window); |
| 84 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | } // namespace |
| 86 | |
| 87 | NEWeightsReshapeKernel::NEWeightsReshapeKernel() |
Michalis Spyrou | a50e702 | 2019-04-09 14:03:17 +0100 | [diff] [blame] | 88 | : _input(nullptr), _bias(nullptr), _output(nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | { |
| 90 | } |
| 91 | |
| 92 | void NEWeightsReshapeKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output) |
| 93 | { |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 94 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 96 | // Output tensor auto inizialitation if not yet initialized |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 97 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(get_output_shape(input->info(), (bias != nullptr)))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 99 | // Perform validation step |
| 100 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), |
| 101 | (bias != nullptr) ? bias->info() : nullptr, |
| 102 | output->info())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | |
| 104 | _input = input; |
| 105 | _bias = bias; |
| 106 | _output = output; |
| 107 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | // Configure kernel |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 109 | auto win_config = validate_and_configure_window(input->info()); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 110 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 111 | INEKernel::configure(win_config.second); |
| 112 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 114 | Status NEWeightsReshapeKernel::validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output) |
| 115 | { |
| 116 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, biases, output)); |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 117 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get()).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 119 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 120 | } |
| 121 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 122 | void NEWeightsReshapeKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 124 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 126 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 127 | |
Michalis Spyrou | a50e702 | 2019-04-09 14:03:17 +0100 | [diff] [blame] | 128 | const unsigned int kernel_size_x = _input->info()->dimension(0); |
| 129 | const unsigned int kernel_size_y = _input->info()->dimension(1); |
| 130 | const unsigned int kernel_depth = _input->info()->dimension(2); |
| 131 | const unsigned int input_stride_x = _input->info()->strides_in_bytes().x(); |
| 132 | const unsigned int input_stride_y = _input->info()->strides_in_bytes().y(); |
| 133 | const unsigned int input_stride_z = _input->info()->strides_in_bytes().z(); |
| 134 | const unsigned int output_stride_y = _output->info()->strides_in_bytes().y(); |
| 135 | |
| 136 | // Create iterators |
| 137 | Iterator in(_input, window); |
| 138 | execute_window_loop(window, [&](const Coordinates & id) |
| 139 | { |
| 140 | // Get column index |
| 141 | const int kernel_idx = id[3]; |
| 142 | const int kernel_idz = id[4]; |
| 143 | |
| 144 | // Setup pointers |
| 145 | const uint8_t *tmp_input_ptr = in.ptr(); |
| 146 | uint8_t *tmp_output_ptr = _output->ptr_to_element(Coordinates(kernel_idx, 0, kernel_idz)); |
| 147 | const uint8_t *curr_input_row_ptr = tmp_input_ptr; |
| 148 | const uint8_t *curr_input_depth_ptr = tmp_input_ptr; |
| 149 | |
| 150 | // Linearize volume |
| 151 | for(unsigned int d = 0; d < kernel_depth; ++d) |
| 152 | { |
| 153 | for(unsigned int j = 0; j < kernel_size_y; ++j) |
| 154 | { |
| 155 | for(unsigned int i = 0; i < kernel_size_x; ++i) |
| 156 | { |
| 157 | std::memcpy(tmp_output_ptr, tmp_input_ptr, _input->info()->element_size()); |
| 158 | tmp_input_ptr += input_stride_x; |
| 159 | tmp_output_ptr += output_stride_y; |
| 160 | } |
| 161 | curr_input_row_ptr += input_stride_y; |
| 162 | tmp_input_ptr = curr_input_row_ptr; |
| 163 | } |
| 164 | curr_input_depth_ptr += input_stride_z; |
| 165 | curr_input_row_ptr = curr_input_depth_ptr; |
| 166 | tmp_input_ptr = curr_input_depth_ptr; |
| 167 | } |
| 168 | |
| 169 | // Add bias |
| 170 | if(_bias != nullptr) |
| 171 | { |
| 172 | std::memcpy(tmp_output_ptr, _bias->ptr_to_element(Coordinates(kernel_idx, kernel_idz)), _input->info()->element_size()); |
| 173 | } |
| 174 | }, |
| 175 | in); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | } |
Michele Di Giorgio | f9b595a | 2020-07-03 13:34:52 +0100 | [diff] [blame] | 177 | } // namespace arm_compute |