blob: c929983162c71776fbb92e5f3474d68f10020e19 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Isabella Gottardi0a1090a2019-02-14 18:07:36 +00002 * Copyright (c) 2016-2019 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{
Anthony Barbiereaefd002018-07-20 17:49:35 +010047 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S8,
49 DataType::U16, DataType::S16, DataType::U32, DataType::S32,
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000050 DataType::F16, DataType::F32);
51 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000052
53 if(output->total_size() != 0)
54 {
55 TensorShape output_shape = input->tensor_shape();
56 output_shape.set(0, input->dimension(0) * 4);
57 output_shape.set(1, std::ceil(input->dimension(1) / 4.0f));
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000060 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000061 }
62
Georgios Pinitas631c41a2017-12-06 11:53:03 +000063 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000064}
65
Georgios Pinitas631c41a2017-12-06 11:53:03 +000066std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000067{
68 unsigned int num_elems_processed_per_iteration_x = (input->element_size() == 1) ? 8 : 4;
69 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
70 bool window_changed = false;
71
72 // Configure kernel window
73 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
74 AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
75 window_changed = window_changed || update_window_and_padding(win, input_access);
76
77 // Configure window in case of configured output
78 if(output->total_size() != 0)
79 {
80 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y, 1, 4.0f, 0.25f);
81 window_changed = window_changed || update_window_and_padding(win, output_access);
82 output_access.set_valid_region(win, input->valid_region());
83 }
84
Georgios Pinitas631c41a2017-12-06 11:53:03 +000085 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000086 return std::make_pair(err, win);
87}
88
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089void gemm_interleave_8bit_elements(const ITensor *input, ITensor *output, const Window &window)
90{
91 const size_t in_stride = input->info()->strides_in_bytes()[1];
92
93 // Set window for output tensor
94 Window win_out(window);
95 win_out.scale(Window::DimY, 0.25f);
96 Iterator in(input, window);
97
98 win_out.set_dimension_step(Window::DimX, 32);
99 Iterator out(output, win_out);
100
101 execute_window_loop(window, [&](const Coordinates &)
102 {
103 const uint8x8x4_t data =
104 {
105 {
106 vld1_u8(in.ptr() + 0 * in_stride),
107 vld1_u8(in.ptr() + 1 * in_stride),
108 vld1_u8(in.ptr() + 2 * in_stride),
109 vld1_u8(in.ptr() + 3 * in_stride),
110 }
111 };
112 vst4_u8(out.ptr(), data);
113 },
114 in, out);
115}
116
117void gemm_interleave_16bit_elements(const ITensor *input, ITensor *output, const Window &window)
118{
119 const size_t in_stride = input->info()->strides_in_bytes()[1];
120
121 // Set window for output tensor
122 Window win_out(window);
123 win_out.scale(Window::DimY, 0.25f);
124 Iterator in(input, window);
125
126 win_out.set_dimension_step(Window::DimX, 16);
127 Iterator out(output, win_out);
128
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100129 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130 {
131 const uint16x4x4_t data =
132 {
133 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100134 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 0 * in_stride)),
135 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 1 * in_stride)),
136 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 2 * in_stride)),
137 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 3 * in_stride)),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100138 }
139 };
140 vst4_u16(reinterpret_cast<uint16_t *>(out.ptr()), data);
141 },
142 in, out);
143}
144
145void gemm_interleave_32bit_elements(const ITensor *input, ITensor *output, const Window &window)
146{
147 const size_t in_stride = input->info()->strides_in_bytes()[1];
148
149 // Set window for output tensor
150 Window win_out(window);
151 win_out.scale(Window::DimY, 0.25f);
152 Iterator in(input, window);
153
154 win_out.set_dimension_step(Window::DimX, 16);
155 Iterator out(output, win_out);
156
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100157 execute_window_loop(window, [&](const Coordinates &)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 {
159 const uint32x4x4_t data =
160 {
161 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100162 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 0 * in_stride)),
163 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 1 * in_stride)),
164 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 2 * in_stride)),
165 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 3 * in_stride))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166 }
167 };
168 vst4q_u32(reinterpret_cast<uint32_t *>(out.ptr()), data);
169 },
170 in, out);
171}
172} // namespace
173
174NEGEMMInterleave4x4Kernel::NEGEMMInterleave4x4Kernel()
175 : _func(nullptr)
176{
177}
178
179void NEGEMMInterleave4x4Kernel::configure(const ITensor *input, ITensor *output)
180{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000181 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100182
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100183 // Output auto inizialitation if not yet initialized
Isabella Gottardie6630e42018-01-18 15:50:39 +0000184 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 +0100185
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000186 // Perform validate step
187 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188
189 _input = input;
190 _output = output;
191
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192 switch(input->info()->element_size())
193 {
194 case 1:
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000195 _func = &gemm_interleave_8bit_elements;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 break;
197 case 2:
198 _func = &gemm_interleave_16bit_elements;
199 break;
200 case 4:
201 _func = &gemm_interleave_32bit_elements;
202 break;
203 default:
204 ARM_COMPUTE_ERROR_ON("Element size not supported");
205 break;
206 }
207
208 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000209 auto win_config = validate_and_configure_window(input->info(), output->info());
210 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
211 INEKernel::configure(win_config.second);
212}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100213
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000214Status NEGEMMInterleave4x4Kernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000215{
216 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
217 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100218
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000219 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220}
221
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100222void NEGEMMInterleave4x4Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100223{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100224 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100225 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
226 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
227 ARM_COMPUTE_ERROR_ON(_func == nullptr);
228 /*
229 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
230 * |a00 a01 a02 a03|
231 * |a10 a11 a12 a13|
232 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
233 * |a30 a31 a32 a33|
234 *
235 * After this operation, the output matrix will have the following shape: [ height * 4, ceil(width / 4.0f) ]
236 */
237 (*_func)(_input, _output, window);
238}