blob: 3ff8b7b201dbf5320b451b8a88a64171a76b4606 [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{
135 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::U8, DataType::S8, DataType::U16, DataType::S16, DataType::U32, DataType::S32, DataType::F16, DataType::F32);
136 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QS8, DataType::U8, DataType::S8, DataType::U16, DataType::S16, DataType::U32, DataType::S32, DataType::F16, DataType::F32);
137 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
138 ARM_COMPUTE_ERROR_ON(output->info()->dimension(0) != input->info()->dimension(0) * 4);
139 ARM_COMPUTE_ERROR_ON(output->info()->dimension(1) != std::ceil(input->info()->dimension(1) / 4.0f));
140
141 _input = input;
142 _output = output;
143
144 unsigned int num_elems_processed_per_iteration_x = 4;
145 constexpr unsigned int num_elems_processed_per_iteration_y = 4;
146
147 switch(input->info()->element_size())
148 {
149 case 1:
150 num_elems_processed_per_iteration_x = 8;
151 _func = &gemm_interleave_8bit_elements;
152 break;
153 case 2:
154 _func = &gemm_interleave_16bit_elements;
155 break;
156 case 4:
157 _func = &gemm_interleave_32bit_elements;
158 break;
159 default:
160 ARM_COMPUTE_ERROR_ON("Element size not supported");
161 break;
162 }
163
164 // Configure kernel window
165 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
166
167 AccessWindowRectangle output_access(output->info(), 0, 0, num_elems_processed_per_iteration_x * num_elems_processed_per_iteration_y, 1, 4.0f, 0.25f);
168 AccessWindowRectangle input_access(input->info(), 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
169 update_window_and_padding(win, output_access, input_access);
170
171 output_access.set_valid_region(win, input->info()->valid_region());
172
173 INEKernel::configure(win);
174}
175
176void NEGEMMInterleave4x4Kernel::run(const Window &window)
177{
178 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
179 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
180 ARM_COMPUTE_ERROR_ON(_func == nullptr);
181 /*
182 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
183 * |a00 a01 a02 a03|
184 * |a10 a11 a12 a13|
185 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
186 * |a30 a31 a32 a33|
187 *
188 * After this operation, the output matrix will have the following shape: [ height * 4, ceil(width / 4.0f) ]
189 */
190 (*_func)(_input, _output, window);
191}