blob: f5bea3e1631f9260bf0d54e6aeb2173f61530629 [file] [log] [blame]
David Svantesson3b162e52023-03-28 14:13:32 +00001/*
Renato Arantes0eb9cfb2023-11-23 11:12:51 +00002 * Copyright (c) 2023-2024 Arm Limited.
David Svantesson3b162e52023-03-28 14:13:32 +00003 *
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 */
David Svantessoncd8b40d2023-05-02 13:05:36 +000024#if defined(__aarch64__)
David Svantesson3b162e52023-03-28 14:13:32 +000025
26#include "src/core/NEON/kernels/NEReorderKernel.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010027
David Svantesson3b162e52023-03-28 14:13:32 +000028#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/Validate.h"
30
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031#include "src/common/utils/Log.h"
32#include "src/core/NEON/kernels/arm_gemm/transform.hpp"
33
David Svantesson3b162e52023-03-28 14:13:32 +000034namespace arm_compute
35{
36
37void NEReorderKernel::run(const Window &window, const ThreadInfo &info)
38{
39 ARM_COMPUTE_UNUSED(info);
40 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
41 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010042 switch (_input->info()->data_type())
David Svantesson3b162e52023-03-28 14:13:32 +000043 {
44 case DataType::F32:
45 {
46 const int ksize_rows_elements = _xmax * _ksize;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 const int jump_rows = ksize_rows_elements * window.x().start();
48 const int k_start = window.x().start() * _ksize;
49 const int k_end = std::min(window.x().end() * _ksize, _kmax);
50 const int stride = _kmax;
51 if (k_start < k_end)
David Svantesson3b162e52023-03-28 14:13:32 +000052 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010053 switch (_output_wf)
David Svantesson3b162e52023-03-28 14:13:32 +000054 {
55 case WeightFormat::OHWIo4:
56 {
Renato Arantes0eb9cfb2023-11-23 11:12:51 +000057 switch (_output->info()->data_type())
58 {
59 case DataType::F32:
60 arm_gemm::Transform<4, 1, true, arm_gemm::VLType::None>(
61 reinterpret_cast<float *>(_output->buffer()) + jump_rows,
62 reinterpret_cast<float *>(_input->buffer()), stride, k_start, k_end, 0, _xmax);
63 break;
64 case DataType::BFLOAT16:
65 arm_gemm::Transform<4, 4, true, arm_gemm::VLType::None>(
66 reinterpret_cast<bfloat16 *>(_output->buffer()) + jump_rows,
67 reinterpret_cast<float *>(_input->buffer()), stride, k_start, k_end, 0, _xmax);
68 break;
69 default:
70 ARM_COMPUTE_ERROR("Unsupported data type!");
71 }
David Svantesson3b162e52023-03-28 14:13:32 +000072 break;
73 }
74#if defined(ARM_COMPUTE_ENABLE_SVE)
75 case WeightFormat::OHWIo8:
76 {
Renato Arantes0eb9cfb2023-11-23 11:12:51 +000077 switch (_output->info()->data_type())
78 {
79 case DataType::F32:
80 arm_gemm::Transform<1, 1, true, arm_gemm::VLType::SVE>(
81 reinterpret_cast<float *>(_output->buffer()) + jump_rows,
82 reinterpret_cast<float *>(_input->buffer()), stride, k_start, k_end, 0, _xmax);
83 break;
84 case DataType::BFLOAT16:
85 arm_gemm::Transform<2, 4, true, arm_gemm::VLType::SVE>(
86 reinterpret_cast<bfloat16 *>(_output->buffer()) + jump_rows,
87 reinterpret_cast<float *>(_input->buffer()), stride, k_start, k_end, 0, _xmax);
88 break;
89 default:
90 ARM_COMPUTE_ERROR("Unsupported data type!");
91 }
David Svantesson3b162e52023-03-28 14:13:32 +000092 break;
93 }
94#endif /* ARM_COMPUTE_ENABLE_SVE */
95 default:
96 {
97 ARM_COMPUTE_ERROR("Unsupported data type!");
98 break;
99 }
100 }
101 }
102 break;
103 }
104 default:
105 ARM_COMPUTE_ERROR("Unsupported data type!");
106 }
107}
108
109NEReorderKernel::NEReorderKernel()
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 : _input(nullptr),
111 _output(nullptr),
112 _ksize(0),
113 _kmax(0),
114 _xmax(0),
115 _input_wf(WeightFormat::ANY),
116 _output_wf(WeightFormat::ANY)
David Svantesson3b162e52023-03-28 14:13:32 +0000117{
118}
119
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100120void NEReorderKernel::configure(const ITensor *input,
121 ITensor *output,
122 arm_compute::WeightFormat input_wf,
123 arm_compute::WeightFormat output_wf)
David Svantesson3b162e52023-03-28 14:13:32 +0000124{
125 ARM_COMPUTE_LOG_PARAMS(input, output, input_wf, output_wf);
126 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
127 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), input_wf, output_wf));
128
129 // Set variables
130 _input = input;
131 _output = output;
132 _input_wf = input_wf;
133 _output_wf = output_wf;
134
135 // Setting parameters for transform
136 auto dims = input->info()->num_dimensions();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137 switch (dims)
David Svantesson3b162e52023-03-28 14:13:32 +0000138 {
139 case 2:
140 {
141 _xmax = input->info()->dimension(0); // Number of columns in input matrix
142 _kmax = input->info()->dimension(1); // Number of rows in input matrix
143 break;
144 }
145 case 4:
146 {
147 _xmax = input->info()->dimension(2); // Number of columns in input matrix
148 _kmax = input->info()->dimension(3); // Number of rows in input matrix
149 break;
150 }
151 default:
152 {
153 ARM_COMPUTE_ERROR("Only 2 or 4 dimensions supported.");
154 }
155 }
156
157 // Configure kernel window
158 // Window size is set by rows / _ksize
159 Window win;
160 int window_size = 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100161 switch (_output_wf)
David Svantesson3b162e52023-03-28 14:13:32 +0000162 {
163#if defined(ARM_COMPUTE_ENABLE_SVE)
164 case WeightFormat::OHWIo8:
165 {
166 _ksize = 8;
167 window_size = _kmax / _ksize;
168 break;
169 }
170#endif /* ARM_COMPUTE_ENABLE_SVE */
171 case WeightFormat::OHWIo4:
172 {
173 _ksize = 4;
174 window_size = _kmax / _ksize;
175 break;
176 }
177 default:
178 {
179 ARM_COMPUTE_ERROR("Unsupported weight format.");
180 break;
181 }
182 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100183 if (_kmax % _ksize != 0)
David Svantesson3b162e52023-03-28 14:13:32 +0000184 {
185 window_size += 1;
186 }
187
188 win.set(Window::DimX, Window::Dimension(0, window_size, 1));
189
190 INEKernel::configure(win);
191}
192
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100193Status NEReorderKernel::validate(const ITensorInfo *input,
194 const ITensorInfo *output,
195 arm_compute::WeightFormat input_wf,
196 arm_compute::WeightFormat output_wf)
David Svantesson3b162e52023-03-28 14:13:32 +0000197{
198 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
199 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100200 if (output->tensor_shape().total_size() != 0)
David Svantesson3b162e52023-03-28 14:13:32 +0000201 {
Renato Arantes0eb9cfb2023-11-23 11:12:51 +0000202 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() != DataType::F32);
203 ARM_COMPUTE_RETURN_ERROR_ON(output->data_type() != DataType::F32 && output->data_type() != DataType::BFLOAT16);
David Svantesson3b162e52023-03-28 14:13:32 +0000204 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
205 // Only input WeightFormat OHWI supported
206 ARM_COMPUTE_RETURN_ERROR_ON(input_wf != arm_compute::WeightFormat::OHWI);
207 int input_x_dim;
208 int input_k_dim;
209 int output_x_dim;
210 int output_k_dim;
211 auto dims = output->num_dimensions();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100212 switch (dims)
David Svantesson3b162e52023-03-28 14:13:32 +0000213 {
214 case 2:
215 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100216 input_x_dim = input->dimension(0); // Number of columns in input matrix
217 input_k_dim = input->dimension(1); // Number of rows in input matrix
David Svantesson3b162e52023-03-28 14:13:32 +0000218 output_x_dim = output->dimension(0); // Number of columns in output matrix
219 output_k_dim = output->dimension(1); // Number of rows in output matrix
220 break;
221 }
222 case 4:
223 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100224 input_x_dim = input->dimension(2); // Number of columns in input matrix
225 input_k_dim = input->dimension(3); // Number of rows in input matrix
David Svantesson3b162e52023-03-28 14:13:32 +0000226 output_x_dim = output->dimension(2); // Number of columns in output matrix
227 output_k_dim = output->dimension(3); // Number of rows in output matrix
228 break;
229 }
230 default:
231 {
232 ARM_COMPUTE_RETURN_ERROR_MSG("Only 2 or 4 dimensions supported.");
233 }
234 }
235
236 int ksize;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100237 switch (output_wf)
David Svantesson3b162e52023-03-28 14:13:32 +0000238 {
David Svantessondfd56a62023-10-10 11:52:51 +0000239#if defined(ARM_COMPUTE_ENABLE_SVE)
David Svantesson3b162e52023-03-28 14:13:32 +0000240 case WeightFormat::OHWIo8:
241 {
242 ksize = 8;
243 break;
244 }
David Svantessondfd56a62023-10-10 11:52:51 +0000245#endif /* ARM_COMPUTE_ENABLE_SVE */
David Svantesson3b162e52023-03-28 14:13:32 +0000246 case WeightFormat::OHWIo4:
247 {
248 ksize = 4;
249 break;
250 }
251 default:
252 {
253 ARM_COMPUTE_RETURN_ERROR_MSG("Unsupported weight format.");
254 break;
255 }
256 }
257
258 // output k_dim needs to be same as input but multiple of ksize
259 int32_t rnd_up_input_kdim = arm_compute::ceil_to_multiple<int32_t, int32_t>(input_k_dim, ksize);
260 ARM_COMPUTE_RETURN_ERROR_ON(rnd_up_input_kdim != output_k_dim);
261 // output x_dim needs to be same as input
262 ARM_COMPUTE_RETURN_ERROR_ON(input_x_dim != output_x_dim);
David Svantesson3b162e52023-03-28 14:13:32 +0000263 }
264 return Status{};
265}
266
267} // namespace arm_compute
David Svantessoncd8b40d2023-05-02 13:05:36 +0000268
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100269#endif // defined(__aarch64__)