blob: d376d53081897b62ea79b30266f7780599f153ec [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitasc7b183a2020-03-06 18:12:09 +00002 * Copyright (c) 2017-2020 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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
33using namespace arm_compute;
34
35namespace
36{
Giorgio Arena7c23ad02017-11-30 15:08:38 +000037TensorShape get_output_shape(const ITensorInfo *input, bool has_bias)
38{
39 TensorShape output_shape{ input->tensor_shape() };
40
41 output_shape.collapse(3);
42 const size_t tmp_dim = output_shape[0];
43 output_shape.set(0, output_shape[1]);
44 output_shape.set(1, tmp_dim + (has_bias ? 1 : 0));
45
46 return output_shape;
47}
48
49Status validate_arguments(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output)
50{
Anthony Barbiereaefd002018-07-20 17:49:35 +010051 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Georgios Pinitas6e1791b2019-12-02 19:01:25 +000052 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1,
53 DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL,
Georgios Pinitasc7b183a2020-03-06 18:12:09 +000054 DataType::BFLOAT16, DataType::F16, DataType::F32);
Giorgio Arena7c23ad02017-11-30 15:08:38 +000055 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
56
57 if(biases != nullptr)
58 {
Isabella Gottardie6630e42018-01-18 15:50:39 +000059 ARM_COMPUTE_RETURN_ERROR_ON(is_data_type_quantized_asymmetric(input->data_type()));
Giorgio Arena7c23ad02017-11-30 15:08:38 +000060 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, biases);
Giorgio Arena7c23ad02017-11-30 15:08:38 +000061 ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->num_dimensions() != 1));
62 ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->num_dimensions() != 2));
63 ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 4) && (biases->dimension(0) != input->tensor_shape()[3]));
64 ARM_COMPUTE_RETURN_ERROR_ON((input->num_dimensions() == 5) && (biases->dimension(0) != input->tensor_shape()[3] || biases->dimension(1) != input->tensor_shape()[4]));
65 }
66
67 // Checks performed when output is configured
68 if(output->total_size() != 0)
69 {
70 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input, biases != nullptr));
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000072 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Giorgio Arena7c23ad02017-11-30 15:08:38 +000073 }
74
75 return Status{};
76}
77
78std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
79{
80 Window window = calculate_max_window(*input, Steps());
81 window.set(Window::DimX, Window::Dimension(0, input->dimension(0), input->dimension(0)));
82 window.set(Window::DimY, Window::Dimension(0, input->dimension(1), input->dimension(1)));
83 window.set(Window::DimZ, Window::Dimension(0, input->dimension(2), input->dimension(2)));
84
85 // The NEConvolutionLayerWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
86 output->set_valid_region(ValidRegion(Coordinates(), output->tensor_shape()));
87
88 return std::make_pair(Status{}, window);
89}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090} // namespace
91
92NEWeightsReshapeKernel::NEWeightsReshapeKernel()
Michalis Spyroua50e7022019-04-09 14:03:17 +010093 : _input(nullptr), _bias(nullptr), _output(nullptr)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094{
95}
96
97void NEWeightsReshapeKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output)
98{
Giorgio Arena7c23ad02017-11-30 15:08:38 +000099 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +0100101 // Output tensor auto inizialitation if not yet initialized
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000102 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(get_output_shape(input->info(), (bias != nullptr))));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000104 // Perform validation step
105 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(),
106 (bias != nullptr) ? bias->info() : nullptr,
107 output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
109 _input = input;
110 _bias = bias;
111 _output = output;
112
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 // Configure kernel
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000114 auto win_config = validate_and_configure_window(input->info(), output->info());
115 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
116 INEKernel::configure(win_config.second);
117}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000119Status NEWeightsReshapeKernel::validate(const ITensorInfo *input, const ITensorInfo *biases, const ITensorInfo *output)
120{
121 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, biases, output));
122 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123
Giorgio Arena7c23ad02017-11-30 15:08:38 +0000124 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100125}
126
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100127void NEWeightsReshapeKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100129 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
131 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
132
Michalis Spyroua50e7022019-04-09 14:03:17 +0100133 const unsigned int kernel_size_x = _input->info()->dimension(0);
134 const unsigned int kernel_size_y = _input->info()->dimension(1);
135 const unsigned int kernel_depth = _input->info()->dimension(2);
136 const unsigned int input_stride_x = _input->info()->strides_in_bytes().x();
137 const unsigned int input_stride_y = _input->info()->strides_in_bytes().y();
138 const unsigned int input_stride_z = _input->info()->strides_in_bytes().z();
139 const unsigned int output_stride_y = _output->info()->strides_in_bytes().y();
140
141 // Create iterators
142 Iterator in(_input, window);
143 execute_window_loop(window, [&](const Coordinates & id)
144 {
145 // Get column index
146 const int kernel_idx = id[3];
147 const int kernel_idz = id[4];
148
149 // Setup pointers
150 const uint8_t *tmp_input_ptr = in.ptr();
151 uint8_t *tmp_output_ptr = _output->ptr_to_element(Coordinates(kernel_idx, 0, kernel_idz));
152 const uint8_t *curr_input_row_ptr = tmp_input_ptr;
153 const uint8_t *curr_input_depth_ptr = tmp_input_ptr;
154
155 // Linearize volume
156 for(unsigned int d = 0; d < kernel_depth; ++d)
157 {
158 for(unsigned int j = 0; j < kernel_size_y; ++j)
159 {
160 for(unsigned int i = 0; i < kernel_size_x; ++i)
161 {
162 std::memcpy(tmp_output_ptr, tmp_input_ptr, _input->info()->element_size());
163 tmp_input_ptr += input_stride_x;
164 tmp_output_ptr += output_stride_y;
165 }
166 curr_input_row_ptr += input_stride_y;
167 tmp_input_ptr = curr_input_row_ptr;
168 }
169 curr_input_depth_ptr += input_stride_z;
170 curr_input_row_ptr = curr_input_depth_ptr;
171 tmp_input_ptr = curr_input_depth_ptr;
172 }
173
174 // Add bias
175 if(_bias != nullptr)
176 {
177 std::memcpy(tmp_output_ptr, _bias->ptr_to_element(Coordinates(kernel_idx, kernel_idz)), _input->info()->element_size());
178 }
179 },
180 in);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181}