blob: 5d178ea85be6319f3cca423d9446b8ae07ef7003 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco Iodicefeaea102020-09-03 13:20:34 +01002 * Copyright (c) 2016-2020 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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEGEMMInterleave4x4Kernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/Window.h"
Isabella Gottardie6630e42018-01-18 15:50:39 +000032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010033#include "src/core/NEON/INEKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036
37#include <arm_neon.h>
38#include <cstddef>
39#include <cstdint>
40#include <tuple>
41
42using namespace arm_compute;
Isabella Gottardie6630e42018-01-18 15:50:39 +000043using namespace arm_compute::misc::shape_calculator;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010044
45namespace
46{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000047Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000048{
Georgios Pinitas33843562019-12-10 13:33:18 +000049 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Anthony Barbiereaefd002018-07-20 17:49:35 +010050 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010051 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000052 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000053
54 if(output->total_size() != 0)
55 {
56 TensorShape output_shape = input->tensor_shape();
57 output_shape.set(0, input->dimension(0) * 4);
58 output_shape.set(1, std::ceil(input->dimension(1) / 4.0f));
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
60 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000061 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000062 }
63
Georgios Pinitas631c41a2017-12-06 11:53:03 +000064 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000065}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010066} // namespace
67
68NEGEMMInterleave4x4Kernel::NEGEMMInterleave4x4Kernel()
69 : _func(nullptr)
70{
71}
72
73void NEGEMMInterleave4x4Kernel::configure(const ITensor *input, ITensor *output)
74{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000075 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +010076
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +010077 // Output auto inizialitation if not yet initialized
Isabella Gottardie6630e42018-01-18 15:50:39 +000078 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 +010079
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000080 // Perform validate step
81 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010082
83 _input = input;
84 _output = output;
85
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086 switch(input->info()->element_size())
87 {
88 case 1:
Gian Marco Iodicefeaea102020-09-03 13:20:34 +010089 _func = &NEGEMMInterleave4x4Kernel::gemm_interleave4x4<uint8_t>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090 break;
91 case 2:
Gian Marco Iodicefeaea102020-09-03 13:20:34 +010092 _func = &NEGEMMInterleave4x4Kernel::gemm_interleave4x4<uint16_t>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 break;
94 case 4:
Gian Marco Iodicefeaea102020-09-03 13:20:34 +010095 _func = &NEGEMMInterleave4x4Kernel::gemm_interleave4x4<uint32_t>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096 break;
97 default:
98 ARM_COMPUTE_ERROR_ON("Element size not supported");
99 break;
100 }
101
Gian Marco Iodicefeaea102020-09-03 13:20:34 +0100102 Window win = calculate_max_window(*input->info(), Steps(1, 4));
103
104 Coordinates coord;
105 coord.set_num_dimensions(output->info()->num_dimensions());
106 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
107
108 INEKernel::configure(win);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000109}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000111Status NEGEMMInterleave4x4Kernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000112{
113 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000115 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100116}
117
Gian Marco Iodicefeaea102020-09-03 13:20:34 +0100118template <typename ScalarType>
119void NEGEMMInterleave4x4Kernel::gemm_interleave4x4(const ITensor *input, ITensor *output, const Window &window)
120{
121 const size_t window_start_x = window.x().start();
122 const size_t window_end_x = window.x().end();
123
124 const size_t in_height = input->info()->dimension(1);
125 const size_t in_stride = input->info()->strides_in_bytes()[1];
126
127 const size_t partial_y = in_height % 4;
128
129 // Set window for the input tensor
130 Window win = window;
131 win.set(Window::DimX, Window::Dimension(0, 1, 1));
132
133 // Set window for the output tensor
134 Window win_out(window);
135 win_out.set(Window::DimX, Window::Dimension(0, 1, 1));
136 win_out.scale(Window::DimY, 0.25f);
137
138 Iterator in(input, win);
139 Iterator out(output, win_out);
140
141 execute_window_loop(win, [&](const Coordinates & id)
142 {
143 if(id.y() + 4 <= static_cast<int>(in_height))
144 {
145 for(size_t x = window_start_x; x < window_end_x; ++x)
146 {
147 const ScalarType data[4] =
148 {
149 *(reinterpret_cast<const ScalarType *>(in.ptr() + 0 * in_stride) + x),
150 *(reinterpret_cast<const ScalarType *>(in.ptr() + 1 * in_stride) + x),
151 *(reinterpret_cast<const ScalarType *>(in.ptr() + 2 * in_stride) + x),
152 *(reinterpret_cast<const ScalarType *>(in.ptr() + 3 * in_stride) + x),
153 };
154 std::memcpy(out.ptr() + x * 4 * sizeof(ScalarType), data, 4 * sizeof(ScalarType));
155 }
156 }
157 else
158 {
159 for(size_t x = window_start_x; x < window_end_x; ++x)
160 {
161 ScalarType data[4] = { 0, 0, 0, 0 };
162
163 for(size_t y = 0; y < partial_y; ++y)
164 {
165 data[y] = *(reinterpret_cast<const ScalarType *>(in.ptr() + y * in_stride) + x);
166 }
167
168 std::memcpy(out.ptr() + x * 4 * sizeof(ScalarType), data, 4 * sizeof(ScalarType));
169 }
170 }
171 },
172 in, out);
173}
174
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100175void NEGEMMInterleave4x4Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100177 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100178 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 */
Gian Marco Iodicefeaea102020-09-03 13:20:34 +0100190 (this->*_func)(_input, _output, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191}