blob: c09745604fd9071d587d367d900ba0565acf57e0 [file] [log] [blame]
David Svantesson3b162e52023-03-28 14:13:32 +00001/*
2 * Copyright (c) 2023 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
25#include "src/core/NEON/kernels/NEReorderKernel.h"
26#include "src/common/utils/Log.h"
27#include "src/core/NEON/kernels/arm_gemm/transform.hpp"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/Validate.h"
30
31namespace arm_compute
32{
33
34void NEReorderKernel::run(const Window &window, const ThreadInfo &info)
35{
36 ARM_COMPUTE_UNUSED(info);
37 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
38 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
39 switch(_input->info()->data_type())
40 {
41 case DataType::F32:
42 {
43 const int ksize_rows_elements = _xmax * _ksize;
44 const int jump_rows = ksize_rows_elements * window.x().start();
45 const int k_start = window.x().start() * _ksize;
46 const int k_end = std::min(window.x().end() * _ksize, _kmax);
47 const int stride = _kmax;
48 if(k_start < k_end)
49 {
50
51 switch(_output_wf)
52 {
53 case WeightFormat::OHWIo4:
54 {
55 arm_gemm::Transform<4, 1, true, arm_gemm::VLType::None>(reinterpret_cast<float *>(_output->buffer()) + jump_rows, reinterpret_cast<float *>(_input->buffer()), stride, k_start, k_end, 0, _xmax);
56 break;
57 }
58#if defined(ARM_COMPUTE_ENABLE_SVE)
59 case WeightFormat::OHWIo8:
60 {
61 arm_gemm::Transform<1, 1, true, arm_gemm::VLType::SVE>(reinterpret_cast<float *>(_output->buffer()) + jump_rows, reinterpret_cast<float *>(_input->buffer()), stride, k_start, k_end, 0, _xmax);
62 break;
63 }
64#endif /* ARM_COMPUTE_ENABLE_SVE */
65 default:
66 {
67 ARM_COMPUTE_ERROR("Unsupported data type!");
68 break;
69 }
70 }
71 }
72 break;
73 }
74 default:
75 ARM_COMPUTE_ERROR("Unsupported data type!");
76 }
77}
78
79NEReorderKernel::NEReorderKernel()
80 : _input(nullptr), _output(nullptr), _ksize(0), _kmax(0), _xmax(0), _input_wf(WeightFormat::ANY), _output_wf(WeightFormat::ANY)
81{
82}
83
84void NEReorderKernel::configure(const ITensor *input, ITensor *output, arm_compute::WeightFormat input_wf, arm_compute::WeightFormat output_wf)
85{
86 ARM_COMPUTE_LOG_PARAMS(input, output, input_wf, output_wf);
87 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
88 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), input_wf, output_wf));
89
90 // Set variables
91 _input = input;
92 _output = output;
93 _input_wf = input_wf;
94 _output_wf = output_wf;
95
96 // Setting parameters for transform
97 auto dims = input->info()->num_dimensions();
98 switch(dims)
99 {
100 case 2:
101 {
102 _xmax = input->info()->dimension(0); // Number of columns in input matrix
103 _kmax = input->info()->dimension(1); // Number of rows in input matrix
104 break;
105 }
106 case 4:
107 {
108 _xmax = input->info()->dimension(2); // Number of columns in input matrix
109 _kmax = input->info()->dimension(3); // Number of rows in input matrix
110 break;
111 }
112 default:
113 {
114 ARM_COMPUTE_ERROR("Only 2 or 4 dimensions supported.");
115 }
116 }
117
118 // Configure kernel window
119 // Window size is set by rows / _ksize
120 Window win;
121 int window_size = 0;
122 switch(_output_wf)
123 {
124#if defined(ARM_COMPUTE_ENABLE_SVE)
125 case WeightFormat::OHWIo8:
126 {
127 _ksize = 8;
128 window_size = _kmax / _ksize;
129 break;
130 }
131#endif /* ARM_COMPUTE_ENABLE_SVE */
132 case WeightFormat::OHWIo4:
133 {
134 _ksize = 4;
135 window_size = _kmax / _ksize;
136 break;
137 }
138 default:
139 {
140 ARM_COMPUTE_ERROR("Unsupported weight format.");
141 break;
142 }
143 }
144 if(_kmax % _ksize != 0)
145 {
146 window_size += 1;
147 }
148
149 win.set(Window::DimX, Window::Dimension(0, window_size, 1));
150
151 INEKernel::configure(win);
152}
153
154Status NEReorderKernel::validate(const ITensorInfo *input, const ITensorInfo *output, arm_compute::WeightFormat input_wf, arm_compute::WeightFormat output_wf)
155{
156 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
157 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
158 if(output->tensor_shape().total_size() != 0)
159 {
160 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
161 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
162 // Only input WeightFormat OHWI supported
163 ARM_COMPUTE_RETURN_ERROR_ON(input_wf != arm_compute::WeightFormat::OHWI);
164 int input_x_dim;
165 int input_k_dim;
166 int output_x_dim;
167 int output_k_dim;
168 auto dims = output->num_dimensions();
169 switch(dims)
170 {
171 case 2:
172 {
173 input_x_dim = input->dimension(0); // Number of columns in input matrix
174 input_k_dim = input->dimension(1); // Number of rows in input matrix
175 output_x_dim = output->dimension(0); // Number of columns in output matrix
176 output_k_dim = output->dimension(1); // Number of rows in output matrix
177 break;
178 }
179 case 4:
180 {
181 input_x_dim = input->dimension(2); // Number of columns in input matrix
182 input_k_dim = input->dimension(3); // Number of rows in input matrix
183 output_x_dim = output->dimension(2); // Number of columns in output matrix
184 output_k_dim = output->dimension(3); // Number of rows in output matrix
185 break;
186 }
187 default:
188 {
189 ARM_COMPUTE_RETURN_ERROR_MSG("Only 2 or 4 dimensions supported.");
190 }
191 }
192
193 int ksize;
194 switch(output_wf)
195 {
196 case WeightFormat::OHWIo8:
197 {
198 ksize = 8;
199 break;
200 }
201 case WeightFormat::OHWIo4:
202 {
203 ksize = 4;
204 break;
205 }
206 default:
207 {
208 ARM_COMPUTE_RETURN_ERROR_MSG("Unsupported weight format.");
209 break;
210 }
211 }
212
213 // output k_dim needs to be same as input but multiple of ksize
214 int32_t rnd_up_input_kdim = arm_compute::ceil_to_multiple<int32_t, int32_t>(input_k_dim, ksize);
215 ARM_COMPUTE_RETURN_ERROR_ON(rnd_up_input_kdim != output_k_dim);
216 // output x_dim needs to be same as input
217 ARM_COMPUTE_RETURN_ERROR_ON(input_x_dim != output_x_dim);
218
219 }
220 return Status{};
221}
222
223} // namespace arm_compute