blob: 54836027865aacd21adfde5ab342dfb17728242f [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{
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);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000060 }
61
Georgios Pinitas631c41a2017-12-06 11:53:03 +000062 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000063}
64
Georgios Pinitas631c41a2017-12-06 11:53:03 +000065std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000066{
67 unsigned int num_elems_processed_per_iteration_x = (input->element_size() == 1) ? 8 : 4;
68 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
69 bool window_changed = false;
70
71 // Configure kernel window
72 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
73 AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
74 window_changed = window_changed || update_window_and_padding(win, input_access);
75
76 // Configure window in case of configured output
77 if(output->total_size() != 0)
78 {
79 AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y, 1, 4.0f, 0.25f);
80 window_changed = window_changed || update_window_and_padding(win, output_access);
81 output_access.set_valid_region(win, input->valid_region());
82 }
83
Georgios Pinitas631c41a2017-12-06 11:53:03 +000084 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000085 return std::make_pair(err, win);
86}
87
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088void gemm_interleave_8bit_elements(const ITensor *input, ITensor *output, const Window &window)
89{
90 const size_t in_stride = input->info()->strides_in_bytes()[1];
91
92 // Set window for output tensor
93 Window win_out(window);
94 win_out.scale(Window::DimY, 0.25f);
95 Iterator in(input, window);
96
97 win_out.set_dimension_step(Window::DimX, 32);
98 Iterator out(output, win_out);
99
100 execute_window_loop(window, [&](const Coordinates &)
101 {
102 const uint8x8x4_t data =
103 {
104 {
105 vld1_u8(in.ptr() + 0 * in_stride),
106 vld1_u8(in.ptr() + 1 * in_stride),
107 vld1_u8(in.ptr() + 2 * in_stride),
108 vld1_u8(in.ptr() + 3 * in_stride),
109 }
110 };
111 vst4_u8(out.ptr(), data);
112 },
113 in, out);
114}
115
116void gemm_interleave_16bit_elements(const ITensor *input, ITensor *output, const Window &window)
117{
118 const size_t in_stride = input->info()->strides_in_bytes()[1];
119
120 // Set window for output tensor
121 Window win_out(window);
122 win_out.scale(Window::DimY, 0.25f);
123 Iterator in(input, window);
124
125 win_out.set_dimension_step(Window::DimX, 16);
126 Iterator out(output, win_out);
127
128 execute_window_loop(window, [&](const Coordinates & id)
129 {
130 const uint16x4x4_t data =
131 {
132 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100133 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 0 * in_stride)),
134 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 1 * in_stride)),
135 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 2 * in_stride)),
136 vld1_u16(reinterpret_cast<const uint16_t *>(in.ptr() + 3 * in_stride)),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100137 }
138 };
139 vst4_u16(reinterpret_cast<uint16_t *>(out.ptr()), data);
140 },
141 in, out);
142}
143
144void gemm_interleave_32bit_elements(const ITensor *input, ITensor *output, const Window &window)
145{
146 const size_t in_stride = input->info()->strides_in_bytes()[1];
147
148 // Set window for output tensor
149 Window win_out(window);
150 win_out.scale(Window::DimY, 0.25f);
151 Iterator in(input, window);
152
153 win_out.set_dimension_step(Window::DimX, 16);
154 Iterator out(output, win_out);
155
156 execute_window_loop(window, [&](const Coordinates & id)
157 {
158 const uint32x4x4_t data =
159 {
160 {
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100161 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 0 * in_stride)),
162 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 1 * in_stride)),
163 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 2 * in_stride)),
164 vld1q_u32(reinterpret_cast<const uint32_t *>(in.ptr() + 3 * in_stride))
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100165 }
166 };
167 vst4q_u32(reinterpret_cast<uint32_t *>(out.ptr()), data);
168 },
169 in, out);
170}
171} // namespace
172
173NEGEMMInterleave4x4Kernel::NEGEMMInterleave4x4Kernel()
174 : _func(nullptr)
175{
176}
177
178void NEGEMMInterleave4x4Kernel::configure(const ITensor *input, ITensor *output)
179{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000180 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100181
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100182 // Output auto inizialitation if not yet initialized
Isabella Gottardie6630e42018-01-18 15:50:39 +0000183 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 +0100184
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000185 // Perform validate step
186 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187
188 _input = input;
189 _output = output;
190
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191 switch(input->info()->element_size())
192 {
193 case 1:
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000194 _func = &gemm_interleave_8bit_elements;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100195 break;
196 case 2:
197 _func = &gemm_interleave_16bit_elements;
198 break;
199 case 4:
200 _func = &gemm_interleave_32bit_elements;
201 break;
202 default:
203 ARM_COMPUTE_ERROR_ON("Element size not supported");
204 break;
205 }
206
207 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000208 auto win_config = validate_and_configure_window(input->info(), output->info());
209 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
210 INEKernel::configure(win_config.second);
211}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100212
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000213Status NEGEMMInterleave4x4Kernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000214{
215 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
216 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100217
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000218 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100219}
220
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100221void NEGEMMInterleave4x4Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100222{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100223 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100224 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
225 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
226 ARM_COMPUTE_ERROR_ON(_func == nullptr);
227 /*
228 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
229 * |a00 a01 a02 a03|
230 * |a10 a11 a12 a13|
231 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
232 * |a30 a31 a32 a33|
233 *
234 * After this operation, the output matrix will have the following shape: [ height * 4, ceil(width / 4.0f) ]
235 */
236 (*_func)(_input, _output, window);
237}