blob: 2e14e7a8c04d807412ff6bf17ec5707ba0ea8395 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +01002 * 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/NEGEMMTranspose1xWKernel.h"
25
26#include "arm_compute/core/AccessWindowTranspose.h"
27#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/NEON/INEKernel.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/TensorShape.h"
34#include "arm_compute/core/Types.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
37
38#include <arm_neon.h>
39#include <cstddef>
40#include <cstring>
41
42using namespace arm_compute;
43
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000044namespace
45{
46TensorShape get_output_shape(const ITensorInfo *input)
47{
48 TensorShape output_shape{ input->tensor_shape() };
49 const size_t transpose_w = 16 / input->element_size();
50 output_shape.set(0, input->dimension(1) * transpose_w);
51 output_shape.set(1, static_cast<size_t>(std::ceil((input->dimension(0) / static_cast<float>(transpose_w)))));
52 return output_shape;
53}
54
Georgios Pinitas631c41a2017-12-06 11:53:03 +000055Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000056{
Anthony Barbiereaefd002018-07-20 17:49:35 +010057 //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 +010058 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::U8, DataType::S8,
59 DataType::U16, DataType::S16, DataType::U32, DataType::S32,
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000060 DataType::F16, DataType::F32);
61 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000062
63 if(output->total_size() != 0)
64 {
65 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input));
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000067 }
68
Georgios Pinitas631c41a2017-12-06 11:53:03 +000069 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000070}
71
Georgios Pinitas631c41a2017-12-06 11:53:03 +000072std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000073{
74 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
75 const int scale_x = num_elems_processed_per_iteration;
76 bool window_changed = false;
77
78 // Configure kernel window
79 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
80
81 ARM_COMPUTE_ERROR_ON_MSG((win.x().end() / scale_x) == 0, "Transposed shape would be 0 in the second dimension");
82
83 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
84 window_changed = window_changed || update_window_and_padding(win, input_access);
85
86 // Configure window in case of configured output
87 if(output->total_size() != 0)
88 {
89 AccessWindowTranspose output_access(output, 0, 0, num_elems_processed_per_iteration, 1, scale_x, 1.f / scale_x);
90 window_changed = window_changed || update_window_and_padding(win, output_access);
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000091 output_access.set_valid_region(win, ValidRegion(Coordinates(), input->tensor_shape()));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000092 }
93
Georgios Pinitas631c41a2017-12-06 11:53:03 +000094 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000095 return std::make_pair(err, win);
96}
97} // namespace
98
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099void NEGEMMTranspose1xWKernel::configure(const ITensor *input, ITensor *output)
100{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000101 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100102
103 // Output tensor auto inizialitation if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100104 auto_init_if_empty(*output->info(), get_output_shape(input->info()), 1, input->info()->data_type());
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000106 // Perform validate step
107 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108
109 _input = input;
110 _output = output;
111
112 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000113 auto win_config = validate_and_configure_window(input->info(), output->info());
114 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
115 INEKernel::configure(win_config.second);
116}
Moritz Pflanzer0745a982017-07-05 16:34:28 +0100117
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000118Status NEGEMMTranspose1xWKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000119{
120 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
121 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Moritz Pflanzer0745a982017-07-05 16:34:28 +0100122
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000123 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124}
125
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100126void NEGEMMTranspose1xWKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100128 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
130 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window);
131
132 /*
133 * Following an example of how the transposition1xW works when the input data type is F32
134 *
135 * |a00 a01 a02 a03|
136 * |a10 a11 a12 a13|
137 * |a20 a21 a22 a23| = | a00 a01 a02 a03 || a10 a11 a12 a13 || a20 a21 a22 a23 || a30 a31 a32 a33 |
138 * |a30 a31 a32 a33|
139 *
140 * The output matrix will have the following shape: [ height * W, ceil(width / W) ], where W = (16 / element size of the tensor)
141 */
142
143 // Set window for output tensor. Set to 0 the X and Y dimensions in order to allow multi-threading implementation and future batched matrix multiplications
144 Window win_out(window);
145 win_out.set(Window::DimX, Window::Dimension(0, 0, 0));
146 win_out.set(Window::DimY, Window::Dimension(0, 0, 0));
147
148 Iterator in(_input, window);
149 Iterator out(_output, win_out);
150
151 switch(_input->info()->element_size())
152 {
153 case 1:
154 {
155 const size_t out_stride = _output->info()->strides_in_bytes()[1];
156 execute_window_loop(window, [&](const Coordinates & id)
157 {
158 // Output address = base addr + (y * 16) + (x / 16 ) * stride
159 const uint8_t *in_ptr = in.ptr();
160 uint8_t *const out_ptr = out.ptr() + (id.y() << 4) + (id.x() >> 4) * out_stride;
161 vst1q_u8(out_ptr, vld1q_u8(in_ptr));
162 },
163 in, out);
164 break;
165 }
166 case 2:
167 {
168 const size_t out_stride = _output->info()->strides_in_bytes()[1] / sizeof(int16_t);
169 execute_window_loop(window, [&](const Coordinates & id)
170 {
171 // Output address = base addr + (y * 8) + (x / 8 ) * stride
172 const auto in_ptr = reinterpret_cast<const uint16_t *>(in.ptr());
173 const auto out_ptr = reinterpret_cast<uint16_t *>(out.ptr()) + (id.y() << 3) + (id.x() >> 3) * out_stride;
174 vst1q_u16(out_ptr, vld1q_u16(in_ptr));
175 },
176 in, out);
177 break;
178 }
179 case 4:
180 {
181 const size_t out_stride = _output->info()->strides_in_bytes()[1] / sizeof(float);
182 execute_window_loop(window, [&](const Coordinates & id)
183 {
184 // Output address = base addr + (y * 4) + (x / 4 ) * stride
185 const auto in_ptr = reinterpret_cast<const uint32_t *>(in.ptr());
186 const auto out_ptr = reinterpret_cast<uint32_t *>(out.ptr()) + (id.y() << 2) + (id.x() >> 2) * out_stride;
187 vst1q_u32(out_ptr, vld1q_u32(in_ptr));
188 },
189 in, out);
190 break;
191 }
192 default:
193 {
194 ARM_COMPUTE_ERROR("Element size not supported");
195 break;
196 }
197 }
198}