blob: c76c39aa4b8b16c4bdb1bd35330d53a585719621 [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{
43void gemm_interleave_8bit_elements(const ITensor *input, ITensor *output, const Window &window)
44{
45 const size_t in_stride = input->info()->strides_in_bytes()[1];
46
47 // Set window for output tensor
48 Window win_out(window);
49 win_out.scale(Window::DimY, 0.25f);
50 Iterator in(input, window);
51
52 win_out.set_dimension_step(Window::DimX, 32);
53 Iterator out(output, win_out);
54
55 execute_window_loop(window, [&](const Coordinates &)
56 {
57 const uint8x8x4_t data =
58 {
59 {
60 vld1_u8(in.ptr() + 0 * in_stride),
61 vld1_u8(in.ptr() + 1 * in_stride),
62 vld1_u8(in.ptr() + 2 * in_stride),
63 vld1_u8(in.ptr() + 3 * in_stride),
64 }
65 };
66 vst4_u8(out.ptr(), data);
67 },
68 in, out);
69}
70
71void gemm_interleave_16bit_elements(const ITensor *input, ITensor *output, const Window &window)
72{
73 const size_t in_stride = input->info()->strides_in_bytes()[1];
74
75 // Set window for output tensor
76 Window win_out(window);
77 win_out.scale(Window::DimY, 0.25f);
78 Iterator in(input, window);
79
80 win_out.set_dimension_step(Window::DimX, 16);
81 Iterator out(output, win_out);
82
83 execute_window_loop(window, [&](const Coordinates & id)
84 {
85 const uint16x4x4_t data =
86 {
87 {
88 vld1_u16(reinterpret_cast<uint16_t *>(in.ptr() + 0 * in_stride)),
89 vld1_u16(reinterpret_cast<uint16_t *>(in.ptr() + 1 * in_stride)),
90 vld1_u16(reinterpret_cast<uint16_t *>(in.ptr() + 2 * in_stride)),
91 vld1_u16(reinterpret_cast<uint16_t *>(in.ptr() + 3 * in_stride)),
92 }
93 };
94 vst4_u16(reinterpret_cast<uint16_t *>(out.ptr()), data);
95 },
96 in, out);
97}
98
99void gemm_interleave_32bit_elements(const ITensor *input, ITensor *output, const Window &window)
100{
101 const size_t in_stride = input->info()->strides_in_bytes()[1];
102
103 // Set window for output tensor
104 Window win_out(window);
105 win_out.scale(Window::DimY, 0.25f);
106 Iterator in(input, window);
107
108 win_out.set_dimension_step(Window::DimX, 16);
109 Iterator out(output, win_out);
110
111 execute_window_loop(window, [&](const Coordinates & id)
112 {
113 const uint32x4x4_t data =
114 {
115 {
116 vld1q_u32(reinterpret_cast<uint32_t *>(in.ptr() + 0 * in_stride)),
117 vld1q_u32(reinterpret_cast<uint32_t *>(in.ptr() + 1 * in_stride)),
118 vld1q_u32(reinterpret_cast<uint32_t *>(in.ptr() + 2 * in_stride)),
119 vld1q_u32(reinterpret_cast<uint32_t *>(in.ptr() + 3 * in_stride))
120 }
121 };
122 vst4q_u32(reinterpret_cast<uint32_t *>(out.ptr()), data);
123 },
124 in, out);
125}
126} // namespace
127
128NEGEMMInterleave4x4Kernel::NEGEMMInterleave4x4Kernel()
129 : _func(nullptr)
130{
131}
132
133void NEGEMMInterleave4x4Kernel::configure(const ITensor *input, ITensor *output)
134{
Gian Marco Iodicebdb6b0b2017-06-30 12:21:00 +0100135 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::U8, DataType::S8, DataType::U16, DataType::S16, DataType::U32, DataType::S32, DataType::F16,
136 DataType::F32);
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100137 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
138
139 TensorShape output_shape = input->info()->tensor_shape();
140 output_shape.set(0, input->info()->dimension(0) * 4);
141 output_shape.set(1, std::ceil(input->info()->dimension(1) / 4.0f));
142
143 // Output auto inizialitation if not yet initialized
144 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
145
146 ARM_COMPUTE_ERROR_ON_MISMATCHING_DIMENSIONS(output->info()->tensor_shape(), output_shape);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +0100148 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149
150 _input = input;
151 _output = output;
152
153 unsigned int num_elems_processed_per_iteration_x = 4;
154 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
155
156 switch(input->info()->element_size())
157 {
158 case 1:
159 num_elems_processed_per_iteration_x = 8;
160 _func = &gemm_interleave_8bit_elements;
161 break;
162 case 2:
163 _func = &gemm_interleave_16bit_elements;
164 break;
165 case 4:
166 _func = &gemm_interleave_32bit_elements;
167 break;
168 default:
169 ARM_COMPUTE_ERROR_ON("Element size not supported");
170 break;
171 }
172
173 // Configure kernel window
174 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
175
176 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y, 1, 4.0f, 0.25f);
177 AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
178 update_window_and_padding(win, output_access, input_access);
179
180 output_access.set_valid_region(win, input->info()->valid_region());
181
182 INEKernel::configure(win);
183}
184
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100185void NEGEMMInterleave4x4Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100186{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100187 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100188 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
189 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
190 ARM_COMPUTE_ERROR_ON(_func == nullptr);
191 /*
192 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
193 * |a00 a01 a02 a03|
194 * |a10 a11 a12 a13|
195 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
196 * |a30 a31 a32 a33|
197 *
198 * After this operation, the output matrix will have the following shape: [ height * 4, ceil(width / 4.0f) ]
199 */
200 (*_func)(_input, _output, window);
201}