blob: 1f4d9b176e88611d5053b95b271f75e384fb6cf7 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 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/NEGEMMInterleave4x4Kernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/NEON/INEKernel.h"
30#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
33
34#include <arm_neon.h>
35#include <cstddef>
36#include <cstdint>
37#include <tuple>
38
39using namespace arm_compute;
40
41namespace
42{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000043Error validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, DataType::U8, DataType::S8,
46 DataType::QS16, DataType::U16, DataType::S16, DataType::U32, DataType::S32,
47 DataType::F16, DataType::F32);
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
50
51 if(output->total_size() != 0)
52 {
53 TensorShape output_shape = input->tensor_shape();
54 output_shape.set(0, input->dimension(0) * 4);
55 output_shape.set(1, std::ceil(input->dimension(1) / 4.0f));
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
59 }
60
61 return Error{};
62}
63
64std::pair<Error, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
65{
66 unsigned int num_elems_processed_per_iteration_x = (input->element_size() == 1) ? 8 : 4;
67 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
68 bool window_changed = false;
69
70 // Configure kernel window
71 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
72 AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
73 window_changed = window_changed || update_window_and_padding(win, input_access);
74
75 // Configure window in case of configured output
76 if(output->total_size() != 0)
77 {
78 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y, 1, 4.0f, 0.25f);
79 window_changed = window_changed || update_window_and_padding(win, output_access);
80 output_access.set_valid_region(win, input->valid_region());
81 }
82
83 Error err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Error{};
84 return std::make_pair(err, win);
85}
86
Anthony Barbier6ff3b192017-09-04 18:44:23 +010087void gemm_interleave_8bit_elements(const ITensor *input, ITensor *output, const Window &window)
88{
89 const size_t in_stride = input->info()->strides_in_bytes()[1];
90
91 // Set window for output tensor
92 Window win_out(window);
93 win_out.scale(Window::DimY, 0.25f);
94 Iterator in(input, window);
95
96 win_out.set_dimension_step(Window::DimX, 32);
97 Iterator out(output, win_out);
98
99 execute_window_loop(window, [&](const Coordinates &)
100 {
101 const uint8x8x4_t data =
102 {
103 {
104 vld1_u8(in.ptr() + 0 * in_stride),
105 vld1_u8(in.ptr() + 1 * in_stride),
106 vld1_u8(in.ptr() + 2 * in_stride),
107 vld1_u8(in.ptr() + 3 * in_stride),
108 }
109 };
110 vst4_u8(out.ptr(), data);
111 },
112 in, out);
113}
114
115void gemm_interleave_16bit_elements(const ITensor *input, ITensor *output, const Window &window)
116{
117 const size_t in_stride = input->info()->strides_in_bytes()[1];
118
119 // Set window for output tensor
120 Window win_out(window);
121 win_out.scale(Window::DimY, 0.25f);
122 Iterator in(input, window);
123
124 win_out.set_dimension_step(Window::DimX, 16);
125 Iterator out(output, win_out);
126
127 execute_window_loop(window, [&](const Coordinates & id)
128 {
129 const uint16x4x4_t data =
130 {
131 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100132 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 0 * in_stride)),
133 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 1 * in_stride)),
134 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 2 * in_stride)),
135 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 3 * in_stride)),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100136 }
137 };
138 vst4_u16(reinterpret_cast<uint16_t *>(out.ptr()), data);
139 },
140 in, out);
141}
142
143void gemm_interleave_32bit_elements(const ITensor *input, ITensor *output, const Window &window)
144{
145 const size_t in_stride = input->info()->strides_in_bytes()[1];
146
147 // Set window for output tensor
148 Window win_out(window);
149 win_out.scale(Window::DimY, 0.25f);
150 Iterator in(input, window);
151
152 win_out.set_dimension_step(Window::DimX, 16);
153 Iterator out(output, win_out);
154
155 execute_window_loop(window, [&](const Coordinates & id)
156 {
157 const uint32x4x4_t data =
158 {
159 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100160 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 0 * in_stride)),
161 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 1 * in_stride)),
162 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 2 * in_stride)),
163 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 3 * in_stride))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 }
165 };
166 vst4q_u32(reinterpret_cast<uint32_t *>(out.ptr()), data);
167 },
168 in, out);
169}
170} // namespace
171
172NEGEMMInterleave4x4Kernel::NEGEMMInterleave4x4Kernel()
173 : _func(nullptr)
174{
175}
176
177void NEGEMMInterleave4x4Kernel::configure(const ITensor *input, ITensor *output)
178{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000179 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100180
181 TensorShape output_shape = input->info()->tensor_shape();
182 output_shape.set(0, input->info()->dimension(0) * 4);
183 output_shape.set(1, std::ceil(input->info()->dimension(1) / 4.0f));
184
185 // Output auto inizialitation if not yet initialized
186 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
187
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000188 // Perform validate step
189 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190
191 _input = input;
192 _output = output;
193
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 switch(input->info()->element_size())
195 {
196 case 1:
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000197 _func = &gemm_interleave_8bit_elements;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100198 break;
199 case 2:
200 _func = &gemm_interleave_16bit_elements;
201 break;
202 case 4:
203 _func = &gemm_interleave_32bit_elements;
204 break;
205 default:
206 ARM_COMPUTE_ERROR_ON("Element size not supported");
207 break;
208 }
209
210 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000211 auto win_config = validate_and_configure_window(input->info(), output->info());
212 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
213 INEKernel::configure(win_config.second);
214}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100215
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000216Error NEGEMMInterleave4x4Kernel::validate(const ITensorInfo *input, const ITensorInfo *output)
217{
218 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
219 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000221 return Error{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222}
223
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100224void NEGEMMInterleave4x4Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100226 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100227 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
228 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
229 ARM_COMPUTE_ERROR_ON(_func == nullptr);
230 /*
231 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
232 * |a00 a01 a02 a03|
233 * |a10 a11 a12 a13|
234 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
235 * |a30 a31 a32 a33|
236 *
237 * After this operation, the output matrix will have the following shape: [ height * 4, ceil(width / 4.0f) ]
238 */
239 (*_func)(_input, _output, window);
240}