blob: a4fc494f1650597a1bb2cd93d09403d10313dc1a [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/NEGEMMMatrixAccumulateBiasesKernel.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/NEFixedPoint.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
39using namespace arm_compute;
40
41NEGEMMMatrixAccumulateBiasesKernel::NEGEMMMatrixAccumulateBiasesKernel()
42 : _accum(nullptr), _biases(nullptr)
43{
44}
45
46void NEGEMMMatrixAccumulateBiasesKernel::configure(ITensor *accum, const ITensor *biases)
47{
Pablo Tellodcdc85e2017-06-28 10:05:29 +010048 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum);
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +010050 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(biases, accum);
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() != 1);
52
53 _biases = biases;
54 _accum = accum;
55
56 constexpr unsigned int num_elems_processed_per_iteration = 16;
57
58 // Configure kernel window
59 Window win = calculate_max_window(*accum->info(), Steps(num_elems_processed_per_iteration));
60
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061 update_window_and_padding(win,
62 AccessWindowHorizontal(accum->info(), 0, num_elems_processed_per_iteration),
Moritz Pflanzerd6253602017-08-07 16:49:49 +010063 AccessWindowStatic(biases->info(), 0, 0, win.x().end(), biases->info()->tensor_shape().y()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064
65 AccessWindowHorizontal output_access(accum->info(), 0, num_elems_processed_per_iteration);
66
67 // Set the valid region for the accum tensor
68 Coordinates coord;
69 coord.set_num_dimensions(accum->info()->num_dimensions());
70 output_access.set_valid_region(win, ValidRegion(coord, accum->info()->tensor_shape()));
71
72 INEKernel::configure(win);
73}
74
75void NEGEMMMatrixAccumulateBiasesKernel::run(const Window &window)
76{
77 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
78 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
79
80 Window win_biases;
81 win_biases.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().step()));
82 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
83
84 Iterator in0_out(_accum, window);
85 Iterator in1(_biases, win_biases);
86
87 switch(_accum->info()->data_type())
88 {
89 case DataType::F32:
90 {
91 execute_window_loop(window, [&](const Coordinates & id)
92 {
93 const float32x4x4_t accum = vld4q_f32(reinterpret_cast<const float *>(in0_out.ptr()));
94 const float32x4x4_t biases = vld4q_f32(reinterpret_cast<const float *>(in1.ptr()));
95 const float32x4x4_t res =
96 {
97 {
98 vaddq_f32(accum.val[0], biases.val[0]),
99 vaddq_f32(accum.val[1], biases.val[1]),
100 vaddq_f32(accum.val[2], biases.val[2]),
101 vaddq_f32(accum.val[3], biases.val[3])
102 }
103 };
104
105 vst4q_f32(reinterpret_cast<float *>(in0_out.ptr()), res);
106 },
107 in0_out, in1);
108 break;
109 }
Pablo Tellodcdc85e2017-06-28 10:05:29 +0100110#ifdef ARM_COMPUTE_ENABLE_FP16
111 case DataType::F16:
112 {
113 execute_window_loop(window, [&](const Coordinates & id)
114 {
115 const float16x8x2_t accum = vld2q_f16(reinterpret_cast<const float16_t *>(in0_out.ptr()));
116 const float16x8x2_t biases = vld2q_f16(reinterpret_cast<const float16_t *>(in1.ptr()));
117 const float16x8x2_t res =
118 {
119 {
120 vaddq_f16(accum.val[0], biases.val[0]),
121 vaddq_f16(accum.val[1], biases.val[1])
122 }
123 };
124
125 vst2q_f16(reinterpret_cast<float16_t *>(in0_out.ptr()), res);
126 },
127 in0_out, in1);
128 break;
129 }
130#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 case DataType::QS8:
132 {
133 execute_window_loop(window, [&](const Coordinates & id)
134 {
135 const qint8x16_t accum = vld1q_qs8(reinterpret_cast<const qint8_t *>(in0_out.ptr()));
136 const qint8x16_t biases = vld1q_qs8(reinterpret_cast<const qint8_t *>(in1.ptr()));
137
138 vst1q_qs8(reinterpret_cast<qint8_t *>(in0_out.ptr()), vqaddq_qs8(accum, biases));
139 },
140 in0_out, in1);
141 break;
142 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100143 case DataType::QS16:
144 {
145 execute_window_loop(window, [&](const Coordinates & id)
146 {
147 qint16x8x2_t accum = vld2q_s16(reinterpret_cast<const qint16_t *>(in0_out.ptr()));
148 const qint16x8x2_t biases = vld2q_s16(reinterpret_cast<const qint16_t *>(in1.ptr()));
149
150 accum.val[0] = vqaddq_qs16(accum.val[0], biases.val[0]);
151 accum.val[1] = vqaddq_qs16(accum.val[1], biases.val[1]);
152
153 vst2q_s16(reinterpret_cast<qint16_t *>(in0_out.ptr()), accum);
154 },
155 in0_out, in1);
156 break;
157 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158 default:
159 ARM_COMPUTE_ERROR("Data type not supported");
160 break;
161 }
162}