blob: fe79df25289ed81e66effb99c67c51a5aa1d2cd2 [file] [log] [blame]
Michalis Spyroub7b31532017-11-23 12:10:21 +00001/*
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/NEGEMMMatrixVectorMultiplyKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/NEON/INEKernel.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
34
35#include <arm_neon.h>
36#include <cstddef>
37#include <cstdint>
38#include <tuple>
39
40using namespace arm_compute;
41
42NEGEMMMatrixVectorMultiplyKernel::NEGEMMMatrixVectorMultiplyKernel()
43 : _input0(nullptr), _input1(nullptr), _output(nullptr)
44{
45}
46
47void NEGEMMMatrixVectorMultiplyKernel::configure(const ITensor *input0, const ITensor *input1, ITensor *output)
48{
49 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input0, 1, DataType::F16, DataType::F32);
50 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input0, input1, output);
51 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input0, input1, output);
52 ARM_COMPUTE_ERROR_ON(input0->info()->dimension(2) != input1->info()->dimension(1));
53
54 _input0 = input0;
55 _input1 = input1;
56 _output = output;
57
58 // Configure kernel window
59 const unsigned int num_elems_read_per_iteration = 4;
60
61 Window win = calculate_max_window(*input0->info(), Steps(num_elems_read_per_iteration));
62
63 AccessWindowHorizontal input0_access(input0->info(), 0, num_elems_read_per_iteration);
64 AccessWindowHorizontal input1_access(input1->info(), 0, num_elems_read_per_iteration);
Michalis Spyrou4db041e2017-11-29 11:56:49 +000065 AccessWindowStatic output_access(output->info(), 0, 0, output->info()->dimension(0), output->info()->dimension(1));
Michalis Spyroub7b31532017-11-23 12:10:21 +000066
67 update_window_and_padding(win, input0_access, input1_access, output_access);
68
69 _output->info()->set_valid_region(ValidRegion(Coordinates(), _output->info()->tensor_shape()));
70
71 INEKernel::configure(win);
72}
73
74void NEGEMMMatrixVectorMultiplyKernel::run(const Window &window, const ThreadInfo &info)
75{
76 ARM_COMPUTE_UNUSED(info);
77 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
78
79 Window window_slice = window.first_slice_window_3D();
80
81 Window window_in(window);
82 Window window_weights(window_slice);
83 Window window_out(window);
84
85 // Setup input0 slice
86 window_in.set(Window::DimX, Window::Dimension(0, _input0->info()->dimension(0), _input0->info()->dimension(0)));
87 window_in.set(Window::DimY, Window::Dimension(0, _input0->info()->dimension(1), 1));
88 window_in.set(Window::DimZ, Window::Dimension(0, _input0->info()->dimension(2), 1));
89
90 // Setup input1 and output slice. Their dimensions are increased in the kernel.
91 window_weights.set(Window::DimX, Window::Dimension(0, 0, 0));
92 window_weights.set(Window::DimY, Window::Dimension(0, 0, 0));
93 window_weights.set(Window::DimZ, Window::Dimension(0, 0, 0));
94
95 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
96 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
97 window_out.set(Window::DimZ, Window::Dimension(0, 0, 0));
98
99 Iterator in(_input0, window_in);
100 Iterator in2(_input1, window_weights);
101 Iterator out(_output, window_out);
102
103 const int input_w = _input0->info()->dimension(0);
104 const int input_h = _input0->info()->dimension(1);
105 const int input_stride_x = _input0->info()->strides_in_bytes().x();
106 const int weights_stride_x = _input1->info()->strides_in_bytes().x();
107 const int weights_stride_y = _input1->info()->strides_in_bytes().y();
108 const int output_stride_x = _output->info()->strides_in_bytes().x();
109
110 execute_window_loop(window_in, [&](const Coordinates & id)
111 {
112 // Get pointers
113 const uint8_t *const input_ptr = in.ptr();
114 const uint8_t *const weights_ptr = in2.ptr() + id.z() * weights_stride_y;
115 auto output_ptr = reinterpret_cast<float *>(out.ptr() + (id.y() + id.z() * input_h) * output_stride_x);
116
117 float32x4_t row_dot = vdupq_n_f32(0.f);
118 for(int i = 0; i < input_w; i += 4)
119 {
120 const auto input = vld1q_f32(reinterpret_cast<const float *>(input_ptr + i * input_stride_x));
121 const auto weights = vld1q_f32(reinterpret_cast<const float *>(weights_ptr + i * weights_stride_x));
122 row_dot = vaddq_f32(row_dot, vmulq_f32(input, weights));
123 }
124
125 auto temp = vadd_f32(vget_high_f32(row_dot), vget_low_f32(row_dot));
126 temp = vpadd_f32(temp, temp);
127
128 *output_ptr = vget_lane_f32(temp, 0);
129 },
130 in, in2, out);
131}