blob: 6a038f8f44beb0e33b5d6b03cb00222495b4fd96 [file] [log] [blame]
Sang-Hoon Park0d008f72020-03-13 14:56:05 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2020 Arm Limited.
Sang-Hoon Park0d008f72020-03-13 14:56:05 +00003 *
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/NEQLSTMLayerNormalizationKernel.h"
25
26#include "arm_compute/core/CPP/Validate.h"
27#include "arm_compute/core/Helpers.h"
Sang-Hoon Park0d008f72020-03-13 14:56:05 +000028#include "arm_compute/core/TensorInfo.h"
29#include "arm_compute/core/Utils.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/Window.h"
32#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010033#include "src/core/NEON/NEFixedPoint.h"
34#include "src/core/NEON/NEMath.h"
35#include "src/core/NEON/NESymm.h"
36
37#include "src/core/NEON/kernels/detail/NEActivationFunctionDetail.h"
Sang-Hoon Park0d008f72020-03-13 14:56:05 +000038
39#include <map>
40
41namespace arm_compute
42{
43namespace
44{
45inline std::pair<int64_t, int64_t> compute_mean_variance(int64_t sum, int64_t sum_sq, uint32_t num_input)
46{
47 const auto temp = static_cast<int64_t>(0x100000) / num_input;
48 const auto mean = sum * 1024 / static_cast<int64_t>(num_input);
49 const int64_t variance = ((sum_sq * temp) - (mean * mean)) / 0x100000;
50
51 return std::make_pair(mean, variance);
52}
53
54inline int64x2x2_t mul_add(const int32x4_t &a, const int32x4_t &b, const int32x4_t &bias)
55{
56 using namespace wrapper;
57 const int64x2_t a_low = vmovl(vgetlow(a));
58 const int64x2_t a_high = vmovl(vgethigh(a));
59 const int64x2_t b_low = vmovl(vgetlow(b));
60 const int64x2_t b_high = vmovl(vgethigh(b));
61
62 const int64_t a_0 = vgetlane(a_low, 0);
63 const int64_t a_1 = vgetlane(a_low, 1);
64 const int64_t a_2 = vgetlane(a_high, 0);
65 const int64_t a_3 = vgetlane(a_high, 1);
66
67 const int64_t b_0 = vgetlane(b_low, 0);
68 const int64_t b_1 = vgetlane(b_low, 1);
69 const int64_t b_2 = vgetlane(b_high, 0);
70 const int64_t b_3 = vgetlane(b_high, 1);
71
72 int64x2x2_t result;
73 const int64x2_t result_0{ a_0 * b_0, a_1 * b_1 };
74 const int64x2_t result_1{ a_2 * b_2, a_3 * b_3 };
75 result.val[0] = vadd(vmovl(vgetlow(bias)), result_0);
76 result.val[1] = vadd(vmovl(vgethigh(bias)), result_1);
77
78 return result;
79}
80} // namespace
81
82void NEQLSTMLayerNormalizationKernel::configure(const ITensor *input, ITensor *output, const ITensor *weight, const ITensor *bias)
83{
Sang-Hoon Park9230e272020-04-18 00:46:34 +010084 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weight, bias, output);
85 ARM_COMPUTE_ERROR_ON(input == output);
86 ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), output->info(), weight->info(), bias->info()));
Sang-Hoon Park0d008f72020-03-13 14:56:05 +000087
88 static const std::map<DataType, ComputeFuncType> fn_map =
89 {
90 { DataType::QSYMM16, std::mem_fn(&NEQLSTMLayerNormalizationKernel::compute_qsymm16) },
91 };
92
93 _input = input;
94 _output = output;
95 _weight = weight;
96 _bias = bias;
97 _fn = fn_map.at(_input->info()->data_type());
98
99 auto_init_if_empty(*_output->info(), *_input->info());
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100100 _output->info()->set_quantization_info(compute_output_qinfo());
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000101
102 const UniformQuantizationInfo wq_info = _weight->info()->quantization_info().uniform();
103 const Status s = quantization::calculate_quantized_multiplier(wq_info.scale, &_output_multiplier, &_output_shift);
104 _output_shift *= -1;
105
106 if(!bool(s))
107 {
108 _output_multiplier = 0;
109 _output_shift = 0;
110 }
111
112 Window win = configure_window(output);
113 INEKernel::configure(win);
114}
115
116Window NEQLSTMLayerNormalizationKernel::configure_window(ITensor *target)
117{
118 Window window = calculate_max_window(*target->info(), Steps());
119 Coordinates coord;
120 coord.set_num_dimensions(target->info()->num_dimensions());
121 target->info()->set_valid_region(ValidRegion(coord, target->info()->tensor_shape()));
122
123 _window_start_x = static_cast<int32_t>(window.x().start());
124 _window_end_x = static_cast<int32_t>(window.x().end());
125 _window_step_x = static_cast<int32_t>(vector_size_byte) / _output->info()->element_size();
126
127 // input and output windows will iterator over y-axis, while execute_window will handler x-axis.
128 _inout_window = window;
129 _inout_window.set(Window::DimX, Window::Dimension(0, 1, 1));
130
131 // weight and bias cannot iterator along y-axis since they are 1D.
132 _weight_window = _inout_window;
133 _weight_window.set(Window::DimY, Window::Dimension(0, 1, 1));
134
135 return window;
136}
137
138Status NEQLSTMLayerNormalizationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *weight, const ITensorInfo *bias)
139{
140 ARM_COMPUTE_UNUSED(output, bias, weight, input);
141
142 ARM_COMPUTE_ERROR_ON_NULLPTR(input, weight, bias, output);
143
144 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QSYMM16);
145 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(weight, 1, DataType::QSYMM16);
146 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32);
147
148 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > max_input_dimension);
149 ARM_COMPUTE_RETURN_ERROR_ON(weight->num_dimensions() > max_weight_dimension);
150 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > max_bias_dimension);
151
152 ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape().x() != weight->tensor_shape().x());
153 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(weight, bias);
154
155 if(output->total_size() != 0)
156 {
157 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
158 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
159 }
160
161 return Status{};
162}
163
164void NEQLSTMLayerNormalizationKernel::run(const Window &window, const ThreadInfo &info)
165{
166 ARM_COMPUTE_UNUSED(window, info);
167 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
168 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
169 ARM_COMPUTE_ERROR_ON_MSG(!_fn, "internal function is not defined for computation");
170
171 _fn(*this);
172}
173
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100174inline QuantizationInfo NEQLSTMLayerNormalizationKernel::compute_output_qinfo()
175{
Sheri Zhang3a353982020-04-21 13:10:24 +0100176 return QuantizationInfo(1.f / 4096);
Sang-Hoon Park9230e272020-04-18 00:46:34 +0100177}
178
Sang-Hoon Park0d008f72020-03-13 14:56:05 +0000179inline std::pair<int64_t, int64_t> NEQLSTMLayerNormalizationKernel::sum_qsymm16(const int16_t *input_ptr)
180{
181 ARM_COMPUTE_ERROR_ON(!input_ptr);
182
183 using AccType = int64_t;
184 using InputDataType = int16_t;
185
186 AccType sum{ 0 };
187 AccType sum_sq{ 0 };
188
189 int32_t x = _window_start_x;
190 for(; x <= _window_end_x && _window_step_x <= (_window_end_x - x); x += _window_step_x)
191 {
192 using namespace wrapper;
193 const int16x8_t val = vloadq(input_ptr + x);
194 const int32x4_t val_low = vmovl(vgetlow(val));
195 const int32x4_t val_high = vmovl(vgethigh(val));
196
197#if defined(__aarch64__)
198 sum += static_cast<AccType>(vaddv(val_low));
199 sum += static_cast<AccType>(vaddv(val_high));
200
201 sum_sq += static_cast<AccType>(vaddv(vmul(val_low, val_low)));
202 sum_sq += static_cast<AccType>(vaddv(vmul(val_high, val_high)));
203#else // __aarch64__
204 // only AArch64 supports vaddv
205 const int64x2_t pair_sum_low = vpaddl(val_low);
206 const int64x2_t pair_sum_high = vpaddl(val_high);
207 const int64x2_t pair_sum = vadd(pair_sum_low, pair_sum_high);
208 sum += vgetlane(pair_sum, 0) + vgetlane(pair_sum, 1);
209
210 const int32x4_t square_low = vmul(val_low, val_low);
211 const int32x4_t square_high = vmul(val_high, val_high);
212 const int64x2_t pair_sum_sq_low = vpaddl(square_low);
213 const int64x2_t pair_sum_sq_high = vpaddl(square_high);
214 const int64x2_t pair_sum_sq = vadd(pair_sum_sq_low, pair_sum_sq_high);
215 sum_sq += vgetlane(pair_sum_sq, 0) + vgetlane(pair_sum_sq, 1);
216#endif // __aarch64__
217 }
218
219 for(; x < _window_end_x; ++x)
220 {
221 const InputDataType val = input_ptr[x];
222 sum += static_cast<AccType>(val);
223 sum_sq += static_cast<AccType>(val * val);
224 }
225
226 return std::make_pair(sum, sum_sq);
227}
228
229inline void NEQLSTMLayerNormalizationKernel::normalize_qasymm16(const int16_t *input_ptr,
230 int16_t *output_ptr,
231 const int16_t *weight_ptr,
232 const int32_t *bias_ptr,
233 int32_t mean, int32_t inv_std_mul, int32_t inv_std_shift)
234{
235 using OutputDataType = int16_t;
236
237 using namespace wrapper;
238 const int32x4_t mean_vec = vdup_n(mean, wrapper::traits::vector_128_tag{});
239
240 int32_t x = _window_start_x;
241 for(; x <= _window_end_x && _window_step_x <= (_window_end_x - x); x += _window_step_x)
242 {
243 const int16x8_t val = vloadq(input_ptr + x);
244 int32x4x2_t shifted;
245 shifted.val[0] = vsub(vshlq_n_s32(vmovl(vgetlow(val)), 10), mean_vec);
246 shifted.val[1] = vsub(vshlq_n_s32(vmovl(vgethigh(val)), 10), mean_vec);
247
248 int32x4x2_t rescaled = multiply_by_quantized_multiplier_2row(shifted, inv_std_mul, inv_std_shift);
249
250 const int16x8_t weight_val = vloadq(weight_ptr + x);
251 const int32x4_t weight_low = vmovl(vgetlow(weight_val));
252 const int32x4_t weight_high = vmovl(vgethigh(weight_val));
253
254 const int32x4_t bias_low = vloadq(bias_ptr + x);
255 const int32x4_t bias_high = vloadq(bias_ptr + 4 + x);
256
257 int64x2x2_t result_0 = mul_add(rescaled.val[0], weight_low, bias_low);
258 int64x2x2_t result_1 = mul_add(rescaled.val[1], weight_high, bias_high);
259
260 int32x4x2_t combined;
261 combined.val[0] = vcombine(vmovn(vrshrq_n_s64(result_0.val[0], 10)), vmovn(vrshrq_n_s64(result_0.val[1], 10)));
262 combined.val[1] = vcombine(vmovn(vrshrq_n_s64(result_1.val[0], 10)), vmovn(vrshrq_n_s64(result_1.val[1], 10)));
263
264 int32x4x2_t out_val = multiply_by_quantized_multiplier_2row(combined, _output_multiplier, _output_shift + 12);
265
266 vstore(output_ptr + x, vqmovn(out_val.val[0]));
267 vstore(output_ptr + x + 4, vqmovn(out_val.val[1]));
268 }
269
270 for(; x < _window_end_x; ++x)
271 {
272 const auto val = static_cast<int32_t>(input_ptr[x]);
273 const int32_t shifted = (val << 10) - mean;
274 const int32_t rescaled = quantization::multiply_by_quantized_multiplier(shifted, inv_std_mul, inv_std_shift);
275 const int64_t weighted = rescaled * weight_ptr[x] + bias_ptr[x];
276 const auto reverse_shifted = static_cast<int32_t>((weighted + 512) >> 10);
277 int32_t out_val = quantization::multiply_by_quantized_multiplier(reverse_shifted, _output_multiplier, _output_shift + 12);
278 out_val = utility::clamp<decltype(out_val), OutputDataType>(out_val, std::numeric_limits<OutputDataType>::min());
279 output_ptr[x] = static_cast<OutputDataType>(out_val);
280 }
281}
282
283void NEQLSTMLayerNormalizationKernel::compute_qsymm16()
284{
285 using InputDataType = int16_t;
286 using OutputDataType = int16_t;
287 using BiasDataType = int32_t;
288 using AccType = int64_t;
289
290 Iterator input_iterator{ _input, _inout_window };
291 Iterator output_iterator{ _output, _inout_window };
292 Iterator weight_iterator{ _weight, _weight_window };
293 Iterator bias_iterator{ _bias, _weight_window };
294
295 const auto weight_ptr = reinterpret_cast<const InputDataType *>(weight_iterator.ptr());
296 const auto bias_ptr = reinterpret_cast<const BiasDataType *>(bias_iterator.ptr());
297
298 const uint32_t column_size = _input->info()->tensor_shape()[0];
299
300 execute_window_loop(_inout_window, [ &, this](const Coordinates &)
301 {
302 const auto in_ptr = reinterpret_cast<const InputDataType *>(input_iterator.ptr());
303 auto out_ptr = reinterpret_cast<OutputDataType *>(output_iterator.ptr());
304
305 AccType sum{ 0 };
306 AccType sum_sq{ 0 };
307 std::tie(sum, sum_sq) = sum_qsymm16(in_ptr);
308
309 AccType mean{ 0 };
310 AccType variance{ 0 };
311 std::tie(mean, variance) = compute_mean_variance(sum, sum_sq, column_size);
312
313 int32_t stddev_invsqrt_mul{};
314 int32_t stddev_invsqrt_shift{};
315 quantization::get_invsqrt_quantized_multiplier_exp(static_cast<int32_t>(variance), -1, stddev_invsqrt_mul, stddev_invsqrt_shift);
316
317 normalize_qasymm16(in_ptr, out_ptr, weight_ptr, bias_ptr, mean, stddev_invsqrt_mul, stddev_invsqrt_shift);
318 },
319 input_iterator, output_iterator);
320}
321} // namespace arm_compute