Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEWeightsReshapeKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Dimensions.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | |
| 33 | using namespace arm_compute; |
| 34 | |
| 35 | namespace |
| 36 | { |
| 37 | template <typename T> |
| 38 | void weights_reshape(const ITensor *input, const ITensor *bias, ITensor *output, const Window &window) |
| 39 | { |
Gian Marco Iodice | 7b06cde | 2017-06-21 08:54:02 +0100 | [diff] [blame] | 40 | const unsigned int kernel_size_x = input->info()->dimension(0); |
| 41 | const unsigned int kernel_size_y = input->info()->dimension(1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 42 | const unsigned int kernel_depth = input->info()->dimension(2); |
| 43 | const unsigned int input_stride_x = input->info()->strides_in_bytes().x(); |
| 44 | const unsigned int input_stride_y = input->info()->strides_in_bytes().y(); |
| 45 | const unsigned int input_stride_z = input->info()->strides_in_bytes().z(); |
| 46 | const unsigned int output_stride_y = output->info()->strides_in_bytes().y(); |
| 47 | |
| 48 | // Create iterators |
| 49 | Iterator in(input, window); |
| 50 | execute_window_loop(window, [&](const Coordinates & id) |
| 51 | { |
| 52 | // Get column index |
| 53 | const int kernel_idx = id[3]; |
| 54 | const int kernel_idz = id[4]; |
| 55 | |
| 56 | // Setup pointers |
| 57 | const uint8_t *tmp_input_ptr = in.ptr(); |
| 58 | uint8_t *tmp_output_ptr = output->ptr_to_element(Coordinates(kernel_idx, 0, kernel_idz)); |
| 59 | const uint8_t *curr_input_row_ptr = tmp_input_ptr; |
| 60 | const uint8_t *curr_input_depth_ptr = tmp_input_ptr; |
| 61 | |
| 62 | // Linearize volume |
| 63 | for(unsigned int d = 0; d < kernel_depth; ++d) |
| 64 | { |
Gian Marco Iodice | 7b06cde | 2017-06-21 08:54:02 +0100 | [diff] [blame] | 65 | for(unsigned int j = 0; j < kernel_size_y; ++j) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | { |
Gian Marco Iodice | 7b06cde | 2017-06-21 08:54:02 +0100 | [diff] [blame] | 67 | for(unsigned int i = 0; i < kernel_size_x; ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | { |
| 69 | *(reinterpret_cast<T *>(tmp_output_ptr)) = *(reinterpret_cast<const T *>(tmp_input_ptr)); |
| 70 | tmp_input_ptr += input_stride_x; |
| 71 | tmp_output_ptr += output_stride_y; |
| 72 | } |
| 73 | curr_input_row_ptr += input_stride_y; |
| 74 | tmp_input_ptr = curr_input_row_ptr; |
| 75 | } |
| 76 | curr_input_depth_ptr += input_stride_z; |
| 77 | curr_input_row_ptr = curr_input_depth_ptr; |
| 78 | tmp_input_ptr = curr_input_depth_ptr; |
| 79 | } |
| 80 | |
| 81 | // Add bias |
| 82 | if(bias != nullptr) |
| 83 | { |
| 84 | *(reinterpret_cast<T *>(tmp_output_ptr)) = *(reinterpret_cast<const T *>(bias->ptr_to_element(Coordinates(kernel_idx, kernel_idz)))); |
| 85 | } |
| 86 | }, |
| 87 | in); |
| 88 | } |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 89 | |
| 90 | TensorShape get_output_shape(const ITensorInfo *input, bool has_bias) |
| 91 | { |
| 92 | TensorShape output_shape{ input->tensor_shape() }; |
| 93 | |
| 94 | output_shape.collapse(3); |
| 95 | const size_t tmp_dim = output_shape[0]; |
| 96 | output_shape.set(0, output_shape[1]); |
| 97 | output_shape.set(1, tmp_dim + (has_bias ? 1 : 0)); |
| 98 | |
| 99 | return output_shape; |
| 100 | } |
| 101 | |
| 102 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output) |
| 103 | { |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 104 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F16, DataType::F32); |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 105 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 106 | |
| 107 | if(biases != nullptr) |
| 108 | { |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 109 | 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] | 110 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases); |
| 111 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, biases); |
| 112 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->num_dimensions() != 1)); |
| 113 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->num_dimensions() != 2)); |
| 114 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->dimension(0) != input->tensor_shape()[3])); |
| 115 | ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->dimension(0) != input->tensor_shape()[3] || biases->dimension(1) != input->tensor_shape()[4])); |
| 116 | } |
| 117 | |
| 118 | // Checks performed when output is configured |
| 119 | if(output->total_size() != 0) |
| 120 | { |
| 121 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input, biases != nullptr)); |
| 122 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 123 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 124 | } |
| 125 | |
| 126 | return Status{}; |
| 127 | } |
| 128 | |
| 129 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 130 | { |
| 131 | Window window = calculate_max_window(*input, Steps()); |
| 132 | window.set(Window::DimX, Window::Dimension(0, input->dimension(0), input->dimension(0))); |
| 133 | window.set(Window::DimY, Window::Dimension(0, input->dimension(1), input->dimension(1))); |
| 134 | window.set(Window::DimZ, Window::Dimension(0, input->dimension(2), input->dimension(2))); |
| 135 | |
| 136 | // The NEConvolutionLayerWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped |
| 137 | output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape())); |
| 138 | |
| 139 | return std::make_pair(Status{}, window); |
| 140 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | } // namespace |
| 142 | |
| 143 | NEWeightsReshapeKernel::NEWeightsReshapeKernel() |
| 144 | : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | void NEWeightsReshapeKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output) |
| 149 | { |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 150 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | |
Gian Marco Iodice | 5cb4c42 | 2017-06-23 10:38:25 +0100 | [diff] [blame] | 152 | // Output tensor auto inizialitation if not yet initialized |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 153 | 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] | 154 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 155 | // Perform validation step |
| 156 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), |
| 157 | (bias != nullptr) ? bias->info() : nullptr, |
| 158 | output->info())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | |
| 160 | _input = input; |
| 161 | _bias = bias; |
| 162 | _output = output; |
| 163 | |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 164 | switch(_input->info()->element_size()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | { |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 166 | case 4: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 167 | { |
| 168 | _func = &weights_reshape<uint32_t>; |
| 169 | break; |
| 170 | } |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 171 | case 2: |
Pablo Tello | 659abc0 | 2017-06-22 16:00:16 +0100 | [diff] [blame] | 172 | { |
| 173 | _func = &weights_reshape<uint16_t>; |
| 174 | break; |
| 175 | } |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 176 | case 1: |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | { |
| 178 | _func = &weights_reshape<uint8_t>; |
| 179 | break; |
| 180 | } |
| 181 | default: |
| 182 | { |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 183 | ARM_COMPUTE_ERROR_ON("Element size not supported"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Configure kernel |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 189 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
| 190 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 191 | INEKernel::configure(win_config.second); |
| 192 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 193 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 194 | Status NEWeightsReshapeKernel::validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output) |
| 195 | { |
| 196 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, biases, output)); |
| 197 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 198 | |
Giorgio Arena | 7c23ad0 | 2017-11-30 15:08:38 +0000 | [diff] [blame] | 199 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 202 | void NEWeightsReshapeKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 204 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 206 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 207 | |
| 208 | (*_func)(_input, _bias, _output, window); |
| 209 | } |