blob: fb16c8dcc1632936d18581aec3905ed7d01e403b [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/NEDirectConvolutionLayerBiasAccumulateKernel.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
41namespace
42{
43// Internal load
44inline float32x4_t internal_vld1q(const float *in)
45{
46 return vld1q_f32(in);
47}
48inline qint8x16_t internal_vld1q(const qint8_t *in)
49{
50 return vld1q_qs8(in);
51}
52inline qint16x8_t internal_vld1q(const qint16_t *in)
53{
54 return vld1q_qs16(in);
55}
56
57// Internal store
58inline void internal_vst1q(float *p, const float32x4_t &v)
59{
60 vst1q_f32(p, v);
61}
62inline void internal_vst1q(qint8_t *p, const qint8x16_t &v)
63{
64 vst1q_qs8(p, v);
65}
66inline void internal_vst1q(qint8_t *p, const qint16x8_t &v)
67{
68 vst1_qs8(p, vqmovn_s16(v));
69}
70inline void internal_vst1q(qint16_t *p, const qint16x8_t &v)
71{
72 vst1q_qs16(p, v);
73}
74
75// Internal vdup
76inline float32x4_t internal_vdupq_n(float v)
77{
78 return vdupq_n_f32(v);
79}
80inline qint8x16_t internal_vdupq_n(qint8_t v)
81{
82 return vdupq_n_qs8(v);
83}
84inline qint16x8_t internal_vdupq_n(qint16_t v)
85{
86 return vdupq_n_qs16(v);
87}
88
89// Internal vadd
90inline float32x4_t internal_vqaddq(const float32x4_t &x, const float32x4_t &y)
91{
92 return vaddq_f32(x, y);
93}
94inline qint8x16_t internal_vqaddq(const qint8x16_t &x, const qint8x16_t &y)
95{
96 return vqaddq_qs8(x, y);
97}
98inline qint16x8_t internal_vqaddq(const qint16x8_t &x, const qint16x8_t &y)
99{
100 return vqaddq_qs16(x, y);
101}
102
Pablo Tello0d176142017-07-06 16:43:14 +0100103#ifdef ARM_COMPUTE_ENABLE_FP16
104inline float16x8_t internal_vld1q(const float16_t *in)
105{
106 return vld1q_f16(in);
107}
108inline void internal_vst1q(float16_t *p, const float16x8_t &v)
109{
110 vst1q_f16(p, v);
111}
112inline float16x8_t internal_vdupq_n(float16_t v)
113{
114 return vdupq_n_f16(v);
115}
116inline float16x8_t internal_vqaddq(const float16x8_t &x, const float16x8_t &y)
117{
118 return vaddq_f16(x, y);
119}
120#endif /* ARM_COMPUTE_ENABLE_FP16 */
121
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122template <typename T1, typename T2, bool in_place>
123void accumulate_bias(ITensor *input, const ITensor *bias, const Window window, ITensor *output)
124{
125 Iterator in(input, window);
126
127 if(in_place) // In place accumulate
128 {
129 execute_window_loop(window, [&](const Coordinates & id)
130 {
131 // Get bias and pointer to input
132 const auto in_ptr = reinterpret_cast<T1 *>(in.ptr());
133 const auto vb = internal_vdupq_n(static_cast<T1>(*reinterpret_cast<const T2 *>(bias->ptr_to_element(Coordinates(id.z())))));
134
135 // Accumulate bias
136 internal_vst1q(in_ptr, internal_vqaddq(internal_vld1q(in_ptr), vb));
137 },
138 in);
139 }
140 else // Out of place accumulate
141 {
142 Iterator out(output, window);
143 execute_window_loop(window, [&](const Coordinates & id)
144 {
145 // Get bias and pointer to input
146 const auto in_ptr = reinterpret_cast<const T1 *>(in.ptr());
147 const auto out_ptr = reinterpret_cast<T2 *>(out.ptr());
148 const auto vb = internal_vdupq_n(static_cast<T1>(*reinterpret_cast<const T2 *>(bias->ptr_to_element(Coordinates(id.z())))));
149
150 // Accumulate bias
151 internal_vst1q(out_ptr, internal_vqaddq(internal_vld1q(in_ptr), vb));
152 },
153 in, out);
154 }
155}
156} // namespace
157
158NEDirectConvolutionLayerBiasAccumulateKernel::NEDirectConvolutionLayerBiasAccumulateKernel()
159 : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr)
160{
161}
162
163void NEDirectConvolutionLayerBiasAccumulateKernel::configure(ITensor *input, const ITensor *bias, ITensor *output)
164{
Pablo Tello0d176142017-07-06 16:43:14 +0100165 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
166 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167 ARM_COMPUTE_ERROR_ON(input->info()->fixed_point_position() != bias->info()->fixed_point_position());
168 if(output != nullptr)
169 {
170 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QS8, DataType::QS16, DataType::F32);
171 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(bias, output);
172 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(bias, output);
173 }
174 ARM_COMPUTE_ERROR_ON(bias->info()->num_dimensions() > 1);
175
176 _func = nullptr;
177 _bias = bias;
178 _input = input;
179 _output = output;
180
181 const unsigned int num_elems_processed_per_iteration = 16 / element_size_from_data_type(input->info()->data_type());
182
183 // Configure kernel window
184 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
185 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
186 AccessWindowStatic bias_access(bias->info(), 0, 0, bias->info()->dimension(0), bias->info()->dimension(1));
187 if(output != nullptr)
188 {
189 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
190 update_window_and_padding(win, input_access, output_access, bias_access);
191 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
192 }
193 else
194 {
195 update_window_and_padding(win, input_access, bias_access);
196 input_access.set_valid_region(win, ValidRegion(Coordinates(), input->info()->tensor_shape()));
197 }
198 INEKernel::configure(win);
199
200 // Set appropriate function
201 if(input->info()->data_type() == DataType::F32)
202 {
203 _func = (output == nullptr) ? &accumulate_bias<float, float, true> : &accumulate_bias<float, float, false>;
204 }
Pablo Tello0d176142017-07-06 16:43:14 +0100205#ifdef ARM_COMPUTE_ENABLE_FP16
206 else if(input->info()->data_type() == DataType::F16)
207 {
208 _func = (output == nullptr) ? &accumulate_bias<float16_t, float16_t, true> : &accumulate_bias<float16_t, float16_t, false>;
209 }
210#endif /* ARM_COMPUTE_ENABLE_FP16 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100211 else if(input->info()->data_type() == DataType::QS8)
212 {
213 _func = (output == nullptr) ? &accumulate_bias<qint8_t, qint8_t, true> : &accumulate_bias<qint8_t, qint8_t, false>;
214 }
215 else if(input->info()->data_type() == DataType::QS16 && bias->info()->data_type() == DataType::QS8)
216 {
217 _func = (output == nullptr) ? &accumulate_bias<qint16_t, qint8_t, true> : &accumulate_bias<qint16_t, qint8_t, false>;
218 }
219 else
220 {
221 ARM_COMPUTE_ERROR("Unsupported combination of types among the inputs.");
222 }
223}
224
225void NEDirectConvolutionLayerBiasAccumulateKernel::run(const Window &window)
226{
227 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
228 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
229 ARM_COMPUTE_ERROR_ON(_func == nullptr);
230
231 (*_func)(_input, _bias, window, _output);
232}