blob: cab3c7a58f32d0685b9e97728862643bdba4c0e4 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +00002 * Copyright (c) 2017-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/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"
Gian Marco900b78f2017-11-20 15:01:12 +000032#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
36#include <arm_neon.h>
37#include <cstddef>
38#include <cstdint>
39
40using namespace arm_compute;
41
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000042namespace
43{
44inline Status validate_arguments(const ITensorInfo *accum, const ITensorInfo *biases)
45{
46 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
47 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum);
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(biases, accum);
49 ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1);
50 ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != accum->dimension(0));
51
52 return Status{};
53}
54
55inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *accum, ITensorInfo *biases)
56{
57 constexpr unsigned int num_elems_processed_per_iteration = 16;
58
59 // Configure kernel window
60 Window win = calculate_max_window(*accum, Steps(num_elems_processed_per_iteration));
61
62 bool window_changed = update_window_and_padding(win,
63 AccessWindowHorizontal(accum, 0, num_elems_processed_per_iteration),
64 AccessWindowStatic(biases, 0, 0, ceil_to_multiple(biases->dimension(0), num_elems_processed_per_iteration), biases->tensor_shape().y()));
65
66 AccessWindowHorizontal output_access(accum, 0, num_elems_processed_per_iteration);
67
68 // Set the valid region for the accum tensor
69 Coordinates coord;
70 coord.set_num_dimensions(accum->num_dimensions());
71 output_access.set_valid_region(win, ValidRegion(coord, accum->tensor_shape()));
72
73 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
74 return std::make_pair(err, win);
75}
76} // namespace
77
Anthony Barbier6ff3b192017-09-04 18:44:23 +010078NEGEMMMatrixAccumulateBiasesKernel::NEGEMMMatrixAccumulateBiasesKernel()
79 : _accum(nullptr), _biases(nullptr)
80{
81}
82
83void NEGEMMMatrixAccumulateBiasesKernel::configure(ITensor *accum, const ITensor *biases)
84{
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000085 ARM_COMPUTE_ERROR_ON_NULLPTR(accum, biases);
86
87 // Perform validate step
88 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(accum->info(), biases->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010089
90 _biases = biases;
91 _accum = accum;
92
Anthony Barbier6ff3b192017-09-04 18:44:23 +010093 // Configure kernel window
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000094 auto win_config = validate_and_configure_window(accum->info(), biases->info());
95 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
96 INEKernel::configure(win_config.second);
97}
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +000099Status NEGEMMMatrixAccumulateBiasesKernel::validate(const ITensorInfo *accum, const ITensorInfo *biases)
100{
101 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(accum, biases));
102 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(accum->clone().get(), biases->clone().get()).first);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103
Ioan-Cristian Szabob4e3e1c2017-11-30 17:17:17 +0000104 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100105}
106
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100107void NEGEMMMatrixAccumulateBiasesKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100109 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100110 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
111 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
112
113 Window win_biases;
114 win_biases.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().step()));
115 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
116
117 Iterator in0_out(_accum, window);
118 Iterator in1(_biases, win_biases);
119
120 switch(_accum->info()->data_type())
121 {
122 case DataType::F32:
123 {
124 execute_window_loop(window, [&](const Coordinates & id)
125 {
126 const float32x4x4_t accum = vld4q_f32(reinterpret_cast<const float *>(in0_out.ptr()));
127 const float32x4x4_t biases = vld4q_f32(reinterpret_cast<const float *>(in1.ptr()));
128 const float32x4x4_t res =
129 {
130 {
131 vaddq_f32(accum.val[0], biases.val[0]),
132 vaddq_f32(accum.val[1], biases.val[1]),
133 vaddq_f32(accum.val[2], biases.val[2]),
134 vaddq_f32(accum.val[3], biases.val[3])
135 }
136 };
137
138 vst4q_f32(reinterpret_cast<float *>(in0_out.ptr()), res);
139 },
140 in0_out, in1);
141 break;
142 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000143#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Pablo Tellodcdc85e2017-06-28 10:05:29 +0100144 case DataType::F16:
145 {
146 execute_window_loop(window, [&](const Coordinates & id)
147 {
148 const float16x8x2_t accum = vld2q_f16(reinterpret_cast<const float16_t *>(in0_out.ptr()));
149 const float16x8x2_t biases = vld2q_f16(reinterpret_cast<const float16_t *>(in1.ptr()));
150 const float16x8x2_t res =
151 {
152 {
153 vaddq_f16(accum.val[0], biases.val[0]),
154 vaddq_f16(accum.val[1], biases.val[1])
155 }
156 };
157
158 vst2q_f16(reinterpret_cast<float16_t *>(in0_out.ptr()), res);
159 },
160 in0_out, in1);
161 break;
162 }
Ioan-Cristian Szabo5edbd1c2017-11-13 13:34:08 +0000163#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100164 case DataType::QS8:
165 {
166 execute_window_loop(window, [&](const Coordinates & id)
167 {
168 const qint8x16_t accum = vld1q_qs8(reinterpret_cast<const qint8_t *>(in0_out.ptr()));
169 const qint8x16_t biases = vld1q_qs8(reinterpret_cast<const qint8_t *>(in1.ptr()));
170
171 vst1q_qs8(reinterpret_cast<qint8_t *>(in0_out.ptr()), vqaddq_qs8(accum, biases));
172 },
173 in0_out, in1);
174 break;
175 }
Gian Marco Iodice2bbd9642017-07-04 16:46:32 +0100176 case DataType::QS16:
177 {
178 execute_window_loop(window, [&](const Coordinates & id)
179 {
180 qint16x8x2_t accum = vld2q_s16(reinterpret_cast<const qint16_t *>(in0_out.ptr()));
181 const qint16x8x2_t biases = vld2q_s16(reinterpret_cast<const qint16_t *>(in1.ptr()));
182
183 accum.val[0] = vqaddq_qs16(accum.val[0], biases.val[0]);
184 accum.val[1] = vqaddq_qs16(accum.val[1], biases.val[1]);
185
186 vst2q_s16(reinterpret_cast<qint16_t *>(in0_out.ptr()), accum);
187 },
188 in0_out, in1);
189 break;
190 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191 default:
192 ARM_COMPUTE_ERROR("Data type not supported");
193 break;
194 }
195}