blob: d52e88c37a97556d87a7ab20e94948806e4607cd [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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/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{
37template <typename T>
38void weights_reshape(const ITensor *input, const ITensor *bias, ITensor *output, const Window &window)
39{
Gian Marco Iodice7b06cde2017-06-21 08:54:02 +010040 const unsigned int kernel_size_x = input->info()->dimension(0);
41 const unsigned int kernel_size_y = input->info()->dimension(1);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042 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 Iodice7b06cde2017-06-21 08:54:02 +010065 for(unsigned int j = 0; j < kernel_size_y; ++j)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066 {
Gian Marco Iodice7b06cde2017-06-21 08:54:02 +010067 for(unsigned int i = 0; i < kernel_size_x; ++i)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010068 {
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}
89} // namespace
90
91NEWeightsReshapeKernel::NEWeightsReshapeKernel()
92 : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr)
93{
94}
95
96void NEWeightsReshapeKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output)
97{
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +010098 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100101 const int fixed_point_position = input->info()->fixed_point_position();
102 const DataType dt = input->info()->data_type();
103 const TensorShape &input_shape = input->info()->tensor_shape();
104 TensorShape output_shape{ input_shape };
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105 output_shape.collapse(3);
Gian Marco Iodice13edbff2017-06-26 17:20:16 +0100106
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100107 const size_t tmp_dim = output_shape[0];
108 output_shape.set(0, output_shape[1]);
109 output_shape.set(1, tmp_dim + (bias != nullptr ? 1 : 0));
110
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +0100111 // Output tensor auto inizialitation if not yet initialized
112 auto_init_if_empty(*output->info(), output_shape, 1, dt, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113
114 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
116 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
117
118 if(bias != nullptr)
119 {
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100120 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias);
Gian Marco Iodice5cb4c422017-06-23 10:38:25 +0100121 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, bias);
122 ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 4) && (bias->info()->num_dimensions() != 1));
123 ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 5) && (bias->info()->num_dimensions() != 2));
124 ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 4) && (bias->info()->dimension(0) != input->info()->tensor_shape()[3]));
125 ARM_COMPUTE_ERROR_ON((input->info()->num_dimensions() == 5) && (bias->info()->dimension(0) != input->info()->tensor_shape()[3] || bias->info()->dimension(1) != input->info()->tensor_shape()[4]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 }
127
128 _input = input;
129 _bias = bias;
130 _output = output;
131
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100132 switch(_input->info()->element_size())
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100133 {
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100134 case 4:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 {
136 _func = &weights_reshape<uint32_t>;
137 break;
138 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100139 case 2:
Pablo Tello659abc02017-06-22 16:00:16 +0100140 {
141 _func = &weights_reshape<uint16_t>;
142 break;
143 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100144 case 1:
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145 {
146 _func = &weights_reshape<uint8_t>;
147 break;
148 }
149 default:
150 {
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100151 ARM_COMPUTE_ERROR_ON("Element size not supported");
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 break;
153 }
154 }
155
156 // Configure kernel
157 Window window = calculate_max_window(*input->info(), Steps());
158 window.set(Window::DimX, Window::Dimension(0, _input->info()->dimension(0), _input->info()->dimension(0)));
159 window.set(Window::DimY, Window::Dimension(0, _input->info()->dimension(1), _input->info()->dimension(1)));
160 window.set(Window::DimZ, Window::Dimension(0, _input->info()->dimension(2), _input->info()->dimension(2)));
161
162 // The NEConvolutionLayerWeightsReshapeKernel doesn't need padding so update_window_and_padding() can be skipped
163 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
164
165 INEKernel::configure(window);
166}
167
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100168void NEWeightsReshapeKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100170 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
172 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
173
174 (*_func)(_input, _bias, _output, window);
175}