blob: 895799c6ca60d2b1fcb7cc5445fb97935ae5489a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 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/NELocallyConnectedMatrixMultiplyKernel.h"
25
26#include "arm_compute/core/AccessWindowTranspose.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/IAccessWindow.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/NEON/NEFixedPoint.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Utils.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 <cstdint>
41#include <tuple>
42
43using namespace arm_compute;
44
45namespace arm_compute
46{
47class Coordinates;
48} // namespace arm_compute
49
50namespace
51{
52void vector_matrix_multiply_f32(const ITensor *input0, const ITensor *input1, ITensor *output, const Window &window)
53{
54 const auto width_matrix_b = static_cast<int>(output->info()->dimension(0));
55 const auto in_b_stride = static_cast<int>(input1->info()->strides_in_bytes()[1] / data_size_from_type(input1->info()->data_type()));
56 const auto num_elems_vec_a = static_cast<int>(input0->info()->dimension(0));
57
58 // The implementation computes 16 elements per iteration
59 const int window_start_x = 16 * window.thread_id();
60 const int window_step_x = 16 * window.num_threads();
61 // Make sure (window_end_x - window_start_x) is a multiple of window_step_x
62 const int window_end_x = ceil_to_multiple(width_matrix_b - window_start_x, window_step_x) + window_start_x;
63
64 Window win_out(window);
65 win_out.set(Window::DimX, Window::Dimension(window_start_x, window_end_x, window_step_x));
66
67 Window win_a(window);
68 win_a.set(Window::DimX, Window::Dimension(0, 1, 1));
69
70 Iterator ina(input0, win_a);
71 Iterator out(output, win_out);
72
73 execute_window_loop(win_out, [&](const Coordinates & id)
74 {
75 if(id.x() > width_matrix_b)
76 {
77 return;
78 }
79
80 float32x4_t acc0 = vdupq_n_f32(0.f);
81 float32x4_t acc1 = vdupq_n_f32(0.f);
82 float32x4_t acc2 = vdupq_n_f32(0.f);
83 float32x4_t acc3 = vdupq_n_f32(0.f);
84
85 auto vec_a = reinterpret_cast<const float *>(ina.ptr());
86 auto matrix_b = reinterpret_cast<const float *>(input1->ptr_to_element(Coordinates(id[0], 0, id[1])));
87
88#if __arm__
89 asm volatile("PLD [%0, #128*4]" ::"r"(reinterpret_cast<const uint8_t *>(vec_a)));
90 asm volatile("PLD [%0, #128*4]" ::"r"(reinterpret_cast<const uint8_t *>(matrix_b)));
91 asm volatile("PLD [%0, #128*4]" ::"r"(reinterpret_cast<const uint8_t *>(matrix_b + in_b_stride)));
Anthony Barbierac69aa12017-07-03 17:39:37 +010092#endif /* __arm__ */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093
94 const float *vec_a_end_addr = vec_a + num_elems_vec_a;
95
96 for(; vec_a <= (vec_a_end_addr - 4);)
97 {
98 float32x2_t a0l = vld1_f32(vec_a);
99
100 float32x4_t b00 = vld1q_f32(matrix_b + 0 + 0 * in_b_stride);
101 float32x4_t b01 = vld1q_f32(matrix_b + 4 + 0 * in_b_stride);
102 float32x4_t b02 = vld1q_f32(matrix_b + 8 + 0 * in_b_stride);
103 float32x4_t b03 = vld1q_f32(matrix_b + 12 + 0 * in_b_stride);
104
105 float32x4_t b10 = vld1q_f32(matrix_b + 0 + 1 * in_b_stride);
106 float32x4_t b11 = vld1q_f32(matrix_b + 4 + 1 * in_b_stride);
107 float32x4_t b12 = vld1q_f32(matrix_b + 8 + 1 * in_b_stride);
108 float32x4_t b13 = vld1q_f32(matrix_b + 12 + 1 * in_b_stride);
109
110#if __arm__
111 asm volatile("PLD [%0, #128*4]" ::"r"(reinterpret_cast<const uint8_t *>(vec_a)));
112 asm volatile("PLD [%0, #128*1]" ::"r"(reinterpret_cast<const uint8_t *>(matrix_b + 1 * in_b_stride)));
113 asm volatile("PLD [%0, #128*1]" ::"r"(reinterpret_cast<const uint8_t *>(matrix_b + 2 * in_b_stride)));
114 asm volatile("PLD [%0, #128*1]" ::"r"(reinterpret_cast<const uint8_t *>(matrix_b + 3 * in_b_stride)));
115 asm volatile("PLD [%0, #128*1]" ::"r"(reinterpret_cast<const uint8_t *>(matrix_b + 4 * in_b_stride)));
Anthony Barbierac69aa12017-07-03 17:39:37 +0100116#endif /* __arm __ */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117
118 acc0 = vmlaq_lane_f32(acc0, b00, a0l, 0);
119 acc1 = vmlaq_lane_f32(acc1, b01, a0l, 0);
120 acc2 = vmlaq_lane_f32(acc2, b02, a0l, 0);
121 acc3 = vmlaq_lane_f32(acc3, b03, a0l, 0);
122
123 acc0 = vmlaq_lane_f32(acc0, b10, a0l, 1);
124 acc1 = vmlaq_lane_f32(acc1, b11, a0l, 1);
125 acc2 = vmlaq_lane_f32(acc2, b12, a0l, 1);
126 acc3 = vmlaq_lane_f32(acc3, b13, a0l, 1);
127
128 vec_a += 2;
129 matrix_b += 2 * in_b_stride;
130
131 a0l = vld1_f32(vec_a);
132
133 b00 = vld1q_f32(matrix_b + 0 + 0 * in_b_stride);
134 b01 = vld1q_f32(matrix_b + 4 + 0 * in_b_stride);
135 b02 = vld1q_f32(matrix_b + 8 + 0 * in_b_stride);
136 b03 = vld1q_f32(matrix_b + 12 + 0 * in_b_stride);
137
138 b10 = vld1q_f32(matrix_b + 0 + 1 * in_b_stride);
139 b11 = vld1q_f32(matrix_b + 4 + 1 * in_b_stride);
140 b12 = vld1q_f32(matrix_b + 8 + 1 * in_b_stride);
141 b13 = vld1q_f32(matrix_b + 12 + 1 * in_b_stride);
142
143 acc0 = vmlaq_lane_f32(acc0, b00, a0l, 0);
144 acc1 = vmlaq_lane_f32(acc1, b01, a0l, 0);
145 acc2 = vmlaq_lane_f32(acc2, b02, a0l, 0);
146 acc3 = vmlaq_lane_f32(acc3, b03, a0l, 0);
147
148 acc0 = vmlaq_lane_f32(acc0, b10, a0l, 1);
149 acc1 = vmlaq_lane_f32(acc1, b11, a0l, 1);
150 acc2 = vmlaq_lane_f32(acc2, b12, a0l, 1);
151 acc3 = vmlaq_lane_f32(acc3, b13, a0l, 1);
152
153 vec_a += 2;
154 matrix_b += 2 * in_b_stride;
155 }
156
157 for(; vec_a < vec_a_end_addr;)
158 {
159 const float a0 = *vec_a;
160
161 const float32x4_t b00 = vld1q_f32(matrix_b + 0 + 0 * in_b_stride);
162 const float32x4_t b01 = vld1q_f32(matrix_b + 4 + 0 * in_b_stride);
163 const float32x4_t b02 = vld1q_f32(matrix_b + 8 + 0 * in_b_stride);
164 const float32x4_t b03 = vld1q_f32(matrix_b + 12 + 0 * in_b_stride);
165
166 acc0 = vmlaq_n_f32(acc0, b00, a0);
167 acc1 = vmlaq_n_f32(acc1, b01, a0);
168 acc2 = vmlaq_n_f32(acc2, b02, a0);
169 acc3 = vmlaq_n_f32(acc3, b03, a0);
170
171 vec_a += 1;
172 matrix_b += in_b_stride;
173 }
174
175 const auto vec_out = reinterpret_cast<float *>(out.ptr());
176
177 vst1q_f32(vec_out + 0, acc0);
178 vst1q_f32(vec_out + 4, acc1);
179 vst1q_f32(vec_out + 8, acc2);
180 vst1q_f32(vec_out + 12, acc3);
181 },
182 ina, out);
183}
184} // namespace
185
186NELocallyConnectedMatrixMultiplyKernel::NELocallyConnectedMatrixMultiplyKernel()
187 : _input0(nullptr), _input1(nullptr), _output(nullptr)
188{
189}
190
191void NELocallyConnectedMatrixMultiplyKernel::configure(const ITensor *input0, const ITensor *input1, ITensor *output)
192{
193 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::F32);
194 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::F32);
195 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32);
196 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32);
197 ARM_COMPUTE_ERROR_ON(input0->info()->dimension(0) != input1->info()->dimension(1));
198
199 _input0 = input0;
200 _input1 = input1;
201 _output = output;
202
203 unsigned int num_elems_processed_per_iteration_x = 16;
204
205 // Configure kernel window
206 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration_x));
207
208 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration_x);
209
210 update_window_and_padding(win,
211 AccessWindowHorizontal(input0->info(), 0, num_elems_processed_per_iteration_x),
212 AccessWindowHorizontal(input1->info(), 0, num_elems_processed_per_iteration_x),
213 output_access);
214
215 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape()));
216
217 INEKernel::configure(win);
218}
219
220void NELocallyConnectedMatrixMultiplyKernel::run(const Window &window)
221{
222 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
223 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
224
225 vector_matrix_multiply_f32(_input0, _input1, _output, window);
226}