blob: 8b4ad0da23cc2b259a8b613a80127c83461e799c [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 */
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{
Georgios Pinitas33843562019-12-10 13:33:18 +000047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Anthony Barbiereaefd002018-07-20 17:49:35 +010048 //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 +010049 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000050 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000051
52 if(output->total_size() != 0)
53 {
54 TensorShape output_shape = input->tensor_shape();
55 output_shape.set(0, input->dimension(0) * 4);
56 output_shape.set(1, std::ceil(input->dimension(1) / 4.0f));
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000059 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(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}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064} // namespace
65
66NEGEMMInterleave4x4Kernel::NEGEMMInterleave4x4Kernel()
67 : _func(nullptr)
68{
69}
70
71void NEGEMMInterleave4x4Kernel::configure(const ITensor *input, ITensor *output)
72{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000073 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +010074
Gian Marco Iodiceec8b45e2017-06-22 13:00:39 +010075 // Output auto inizialitation if not yet initialized
Isabella Gottardie6630e42018-01-18 15:50:39 +000076 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 +010077
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000078 // Perform validate step
79 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010080
81 _input = input;
82 _output = output;
83
Anthony Barbier6ff3b192017-09-04 18:44:23 +010084 switch(input->info()->element_size())
85 {
86 case 1:
Gian Marco Iodicefeaea102020-09-03 13:20:34 +010087 _func = &NEGEMMInterleave4x4Kernel::gemm_interleave4x4<uint8_t>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010088 break;
89 case 2:
Gian Marco Iodicefeaea102020-09-03 13:20:34 +010090 _func = &NEGEMMInterleave4x4Kernel::gemm_interleave4x4<uint16_t>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010091 break;
92 case 4:
Gian Marco Iodicefeaea102020-09-03 13:20:34 +010093 _func = &NEGEMMInterleave4x4Kernel::gemm_interleave4x4<uint32_t>;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010094 break;
95 default:
96 ARM_COMPUTE_ERROR_ON("Element size not supported");
97 break;
98 }
99
Gian Marco Iodicefeaea102020-09-03 13:20:34 +0100100 Window win = calculate_max_window(*input->info(), Steps(1, 4));
101
102 Coordinates coord;
103 coord.set_num_dimensions(output->info()->num_dimensions());
104 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
105
106 INEKernel::configure(win);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000107}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000109Status NEGEMMInterleave4x4Kernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000110{
111 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000113 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114}
115
Gian Marco Iodicefeaea102020-09-03 13:20:34 +0100116template <typename ScalarType>
117void NEGEMMInterleave4x4Kernel::gemm_interleave4x4(const ITensor *input, ITensor *output, const Window &window)
118{
119 const size_t window_start_x = window.x().start();
120 const size_t window_end_x = window.x().end();
121
122 const size_t in_height = input->info()->dimension(1);
123 const size_t in_stride = input->info()->strides_in_bytes()[1];
124
125 const size_t partial_y = in_height % 4;
126
127 // Set window for the input tensor
128 Window win = window;
129 win.set(Window::DimX, Window::Dimension(0, 1, 1));
130
131 // Set window for the output tensor
132 Window win_out(window);
133 win_out.set(Window::DimX, Window::Dimension(0, 1, 1));
134 win_out.scale(Window::DimY, 0.25f);
135
136 Iterator in(input, win);
137 Iterator out(output, win_out);
138
139 execute_window_loop(win, [&](const Coordinates & id)
140 {
141 if(id.y() + 4 <= static_cast<int>(in_height))
142 {
143 for(size_t x = window_start_x; x < window_end_x; ++x)
144 {
145 const ScalarType data[4] =
146 {
147 *(reinterpret_cast<const ScalarType *>(in.ptr() + 0 * in_stride) + x),
148 *(reinterpret_cast<const ScalarType *>(in.ptr() + 1 * in_stride) + x),
149 *(reinterpret_cast<const ScalarType *>(in.ptr() + 2 * in_stride) + x),
150 *(reinterpret_cast<const ScalarType *>(in.ptr() + 3 * in_stride) + x),
151 };
152 std::memcpy(out.ptr() + x * 4 * sizeof(ScalarType), data, 4 * sizeof(ScalarType));
153 }
154 }
155 else
156 {
157 for(size_t x = window_start_x; x < window_end_x; ++x)
158 {
159 ScalarType data[4] = { 0, 0, 0, 0 };
160
161 for(size_t y = 0; y < partial_y; ++y)
162 {
163 data[y] = *(reinterpret_cast<const ScalarType *>(in.ptr() + y * in_stride) + x);
164 }
165
166 std::memcpy(out.ptr() + x * 4 * sizeof(ScalarType), data, 4 * sizeof(ScalarType));
167 }
168 }
169 },
170 in, out);
171}
172
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100173void NEGEMMInterleave4x4Kernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100175 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
177 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
178 ARM_COMPUTE_ERROR_ON(_func == nullptr);
179 /*
180 * This kernel puts the values in a 4x4 block of Matrix A on the same row (Interleaved values)
181 * |a00 a01 a02 a03|
182 * |a10 a11 a12 a13|
183 * |a20 a21 a22 a23| = | a00 a10 a20 a30 || a01 a11 a21 a31 || a02 a12 a22 a32 || a03 a13 a23 a33 |
184 * |a30 a31 a32 a33|
185 *
186 * After this operation, the output matrix will have the following shape: [ height * 4, ceil(width / 4.0f) ]
187 */
Gian Marco Iodicefeaea102020-09-03 13:20:34 +0100188 (this->*_func)(_input, _output, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189}