blob: e49fd2911570472ffc21113a30feab15e3134ec8 [file] [log] [blame]
Gian Marco58c57942017-11-28 09:10:03 +00001/*
SiCongLib88272e2021-02-24 15:40:57 +00002 * Copyright (c) 2017-2021 Arm Limited.
Gian Marco58c57942017-11-28 09:10:03 +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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.h"
Gian Marco58c57942017-11-28 09:10:03 +000025
Gian Marco58c57942017-11-28 09:10:03 +000026#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Georgios Pinitas932491f2018-09-21 16:33:15 +010029#include "arm_compute/core/TensorInfo.h"
Gian Marco58c57942017-11-28 09:10:03 +000030#include "arm_compute/core/Types.h"
31#include "arm_compute/core/Utils.h"
32#include "arm_compute/core/Validate.h"
33#include "arm_compute/core/Window.h"
Georgios Pinitas041f36d2018-09-18 18:38:37 +010034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010035#include "src/core/NEON/NEAsymm.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Gian Marco58c57942017-11-28 09:10:03 +000038
39#include <arm_neon.h>
Gian Marco58c57942017-11-28 09:10:03 +000040
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +000041namespace arm_compute
42{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010043namespace cpu
44{
45namespace kernels
46{
Gian Marco58c57942017-11-28 09:10:03 +000047namespace
48{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010049Status validate_arguments(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +000050{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010051 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::S32);
Giorgio Arena1856ff72020-02-07 13:46:45 +000053 ARM_COMPUTE_RETURN_ERROR_ON(min > max);
Gian Marco58c57942017-11-28 09:10:03 +000054
55 // Check biases if exist
56 if(bias != nullptr)
57 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010058 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, bias);
Gian Marco58c57942017-11-28 09:10:03 +000059 ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1);
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010060 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(0) != bias->dimension(0));
Gian Marco58c57942017-11-28 09:10:03 +000061 }
Chunosov5124be52017-11-22 20:42:13 +070062
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010063 if(dst->total_size() != 0)
Chunosov5124be52017-11-22 20:42:13 +070064 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010065 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::QASYMM8);
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, src);
Chunosov5124be52017-11-22 20:42:13 +070067 }
68
Georgios Pinitas631c41a2017-12-06 11:53:03 +000069 return Status{};
Gian Marco58c57942017-11-28 09:10:03 +000070}
Georgios Pinitasf72f9362018-01-12 16:29:45 +000071} // namespace
Gian Marco58c57942017-11-28 09:10:03 +000072
Gian Marco58c57942017-11-28 09:10:03 +000073template <bool is_bounded_relu>
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010074void CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run_internal(const ITensor *src, const ITensor *bias, ITensor *dst, const Window &window)
Gian Marco58c57942017-11-28 09:10:03 +000075{
76 const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
77 const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min));
78 const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max));
79
80 ARM_COMPUTE_UNUSED(min_u8);
81 ARM_COMPUTE_UNUSED(max_u8);
82
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000083 const int window_step_x = 16;
84 const auto window_start_x = static_cast<int>(window.x().start());
85 const auto window_end_x = static_cast<int>(window.x().end());
Gian Marco7f0f7902017-12-07 09:26:56 +000086
Georgios Pinitas041f36d2018-09-18 18:38:37 +010087 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
88 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco7f0f7902017-12-07 09:26:56 +000089
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010090 Iterator in(src, win_collapsed);
91 Iterator out(dst, win_collapsed);
92 if(bias != nullptr)
Gian Marco58c57942017-11-28 09:10:03 +000093 {
94 Window win_biases;
Gian Marco7f0f7902017-12-07 09:26:56 +000095 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
Gian Marco58c57942017-11-28 09:10:03 +000096 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
97
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010098 Iterator bias_i(bias, win_biases);
Michalis Spyroua4f378d2019-04-26 14:54:54 +010099 execute_window_loop(win_collapsed, [&](const Coordinates &)
Gian Marco58c57942017-11-28 09:10:03 +0000100 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000101 // Compute 16 elements per iteration
102 int x = window_start_x;
103 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000104 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000105 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000106 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000107 {
108 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
109 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
110 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
111 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
112 }
113 };
Gian Marco58c57942017-11-28 09:10:03 +0000114
Gian Marco7f0f7902017-12-07 09:26:56 +0000115 const int32x4x4_t bias_s32 =
116 {
117 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100118 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 0),
119 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 4),
120 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 8),
121 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 12)
Gian Marco7f0f7902017-12-07 09:26:56 +0000122 }
123 };
124
125 // Add the bias to GEMM's result
126 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
127 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
128 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
129 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
130
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100131 vst1q_u8(out.ptr() + x, finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_u8, max_u8, is_bounded_relu));
Gian Marco7f0f7902017-12-07 09:26:56 +0000132 }
133
134 // Compute left-over elements
135 for(; x < window_end_x; ++x)
Gian Marco58c57942017-11-28 09:10:03 +0000136 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100137 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x);
Gian Marco7f0f7902017-12-07 09:26:56 +0000138 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
Gian Marco58c57942017-11-28 09:10:03 +0000139
Gian Marco7f0f7902017-12-07 09:26:56 +0000140 // Add bias
141 in_value += bias_value;
Gian Marco7f0f7902017-12-07 09:26:56 +0000142 // Finalize and store the result
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100143 *(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max), is_bounded_relu);
Gian Marco7f0f7902017-12-07 09:26:56 +0000144 }
Gian Marco58c57942017-11-28 09:10:03 +0000145 },
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100146 in, out, bias_i);
Gian Marco58c57942017-11-28 09:10:03 +0000147 }
148 else
149 {
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100150 execute_window_loop(win_collapsed, [&](const Coordinates &)
Gian Marco58c57942017-11-28 09:10:03 +0000151 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000152 // Compute 16 elements per iteration
153 int x = window_start_x;
154 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Gian Marco58c57942017-11-28 09:10:03 +0000155 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000156 int32x4x4_t in_s32 =
Gian Marco58c57942017-11-28 09:10:03 +0000157 {
Gian Marco7f0f7902017-12-07 09:26:56 +0000158 {
159 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
160 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
161 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
162 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
163 }
164 };
Gian Marco58c57942017-11-28 09:10:03 +0000165
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100166 vst1q_u8(out.ptr() + x, finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_u8, max_u8, is_bounded_relu));
Gian Marco7f0f7902017-12-07 09:26:56 +0000167 }
168
169 // Compute left-over elements
170 for(; x < window_end_x; ++x)
171 {
George Wort2d7e6832019-02-22 16:37:41 +0000172 const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
Gian Marco7f0f7902017-12-07 09:26:56 +0000173
174 // Finalize and store the result
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100175 *(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max), is_bounded_relu);
Gian Marco7f0f7902017-12-07 09:26:56 +0000176 }
Gian Marco58c57942017-11-28 09:10:03 +0000177 },
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000178 in, out);
Gian Marco58c57942017-11-28 09:10:03 +0000179 }
180}
181
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100182void CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::configure(ITensorInfo *src, ITensorInfo *bias, ITensorInfo *dst, int result_fixedpoint_multiplier, int result_shift,
183 int result_offset_after_shift, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +0000184{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100185 ARM_COMPUTE_UNUSED(bias);
Gian Marco58c57942017-11-28 09:10:03 +0000186 // Perform validate step
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100187 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
188 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, bias, dst, min, max));
Gian Marco58c57942017-11-28 09:10:03 +0000189
Gian Marco58c57942017-11-28 09:10:03 +0000190 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
191 _result_shift = result_shift;
192 _result_offset_after_shift = result_offset_after_shift;
193 _min = min;
194 _max = max;
195
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100196 // Output auto inizialitation if not yet initialized
197 auto_init_if_empty(*dst, src->clone()->set_data_type(DataType::QASYMM8));
198
Gian Marco58c57942017-11-28 09:10:03 +0000199 // Configure kernel window
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100200 auto win_config = calculate_max_window(*src, Steps());
201 ICpuKernel::configure(win_config);
Gian Marco58c57942017-11-28 09:10:03 +0000202
203 // Check if we need to clamp the result using min and max
Giorgio Arena1856ff72020-02-07 13:46:45 +0000204 const bool is_bounded_relu = !(min <= 0 && max >= 255);
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100205 _func = is_bounded_relu ? &CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run_internal<true> :
206 &CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run_internal<false>;
Gian Marco58c57942017-11-28 09:10:03 +0000207}
208
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100209Status CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, int min, int max)
Gian Marco58c57942017-11-28 09:10:03 +0000210{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100211 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
212 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, bias, dst, min, max));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000213 return Status{};
Gian Marco58c57942017-11-28 09:10:03 +0000214}
215
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100216void CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Gian Marco58c57942017-11-28 09:10:03 +0000217{
218 ARM_COMPUTE_UNUSED(info);
219 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100220 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
221 ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
Gian Marco58c57942017-11-28 09:10:03 +0000222
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100223 auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
224 auto bias = tensors.get_const_tensor(TensorType::ACL_BIAS);
225 auto dst = tensors.get_tensor(TensorType::ACL_DST);
226
227 (this->*_func)(src, bias, dst, window);
Michele Di Giorgiof29d1b72019-10-29 10:58:13 +0000228}
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100229
230const char *CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::name() const
231{
232 return "CpuGemmLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel";
233}
234} // namespace kernels
235} // namespace cpu
236} // namespace arm_compute