blob: 6519a39b9c311afd52292ed3879b3dd4168595de [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Isabella Gottardie6630e42018-01-18 15:50:39 +00002 * Copyright (c) 2016-2018 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/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"
Isabella Gottardie6630e42018-01-18 15:50:39 +000033#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35#include <arm_neon.h>
36#include <cstddef>
37#include <cstdint>
38#include <tuple>
39
40using namespace arm_compute;
Isabella Gottardie6630e42018-01-18 15:50:39 +000041using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010042
43namespace
44{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000045Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000046{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010047 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S8,
48 DataType::U16, DataType::S16, DataType::U32, DataType::S32,
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000049 DataType::F16, DataType::F32);
50 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000051
52 if(output->total_size() != 0)
53 {
54 TensorShape output_shape = input->tensor_shape();
55 output_shape.set(0, input->dimension(0) * 4);
56 output_shape.set(1, std::ceil(input->dimension(1) / 4.0f));
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000059 }
60
Georgios Pinitas631c41a2017-12-06 11:53:03 +000061 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000062}
63
Georgios Pinitas631c41a2017-12-06 11:53:03 +000064std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000065{
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
Georgios Pinitas631c41a2017-12-06 11:53:03 +000083 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000084 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
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100181 // Output auto inizialitation if not yet initialized
Isabella Gottardie6630e42018-01-18 15:50:39 +0000182 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(compute_interleaved_shape(*input->info())));
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100183
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000184 // Perform validate step
185 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186
187 _input = input;
188 _output = output;
189
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 switch(input->info()->element_size())
191 {
192 case 1:
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000193 _func = &gemm_interleave_8bit_elements;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194 break;
195 case 2:
196 _func = &gemm_interleave_16bit_elements;
197 break;
198 case 4:
199 _func = &gemm_interleave_32bit_elements;
200 break;
201 default:
202 ARM_COMPUTE_ERROR_ON("Element size not supported");
203 break;
204 }
205
206 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000207 auto win_config = validate_and_configure_window(input->info(), output->info());
208 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
209 INEKernel::configure(win_config.second);
210}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000212Status NEGEMMInterleave4x4Kernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000213{
214 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
215 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100216
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000217 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218}
219
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100220void NEGEMMInterleave4x4Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100221{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100222 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
224 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
225 ARM_COMPUTE_ERROR_ON(_func == nullptr);
226 /*
227 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
228 * |a00 a01 a02 a03|
229 * |a10 a11 a12 a13|
230 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
231 * |a30 a31 a32 a33|
232 *
233 * After this operation, the output matrix will have the following shape: [ height * 4, ceil(width / 4.0f) ]
234 */
235 (*_func)(_input, _output, window);
236}