blob: 318b6a06f851849b802c43302896107d56fce490 [file] [log] [blame]
Georgios Pinitas448a81f2019-11-21 14:10:25 +00001/*
SiCongLib88272e2021-02-24 15:40:57 +00002 * Copyright (c) 2019-2021 Arm Limited.
Georgios Pinitas448a81f2019-11-21 14:10:25 +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 */
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010024#include "src/core/cpu/kernels/CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel.h"
Georgios Pinitas448a81f2019-11-21 14:10:25 +000025
Georgios Pinitas448a81f2019-11-21 14:10:25 +000026#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Georgios Pinitas448a81f2019-11-21 14:10:25 +000029#include "arm_compute/core/TensorInfo.h"
30#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"
34#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"
Georgios Pinitas448a81f2019-11-21 14:10:25 +000038
39#include <arm_neon.h>
Georgios Pinitas448a81f2019-11-21 14:10:25 +000040
41namespace arm_compute
42{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010043namespace cpu
44{
45namespace kernels
46{
Georgios Pinitas448a81f2019-11-21 14:10:25 +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)
Georgios Pinitas448a81f2019-11-21 14:10:25 +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);
Georgios Pinitas448a81f2019-11-21 14:10:25 +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);
Georgios Pinitas448a81f2019-11-21 14:10:25 +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));
Georgios Pinitas448a81f2019-11-21 14:10:25 +000061 }
62
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010063 if(dst->total_size() != 0)
Georgios Pinitas448a81f2019-11-21 14:10:25 +000064 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010065 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::QASYMM8_SIGNED);
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(dst, src);
Georgios Pinitas448a81f2019-11-21 14:10:25 +000067 }
68
69 return Status{};
70}
Georgios Pinitas448a81f2019-11-21 14:10:25 +000071} // namespace
72
73template <bool is_bounded_relu>
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010074void CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run_internal(const ITensor *src, const ITensor *bias, ITensor *dst, const Window &window)
Georgios Pinitas448a81f2019-11-21 14:10:25 +000075{
76 const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift);
77 const int8x16_t min_s8 = vdupq_n_s8(static_cast<int8_t>(_min));
78 const int8x16_t max_s8 = vdupq_n_s8(static_cast<int8_t>(_max));
79
80 ARM_COMPUTE_UNUSED(min_s8, max_s8);
81
82 const int window_step_x = 16;
83 const auto window_start_x = static_cast<int>(window.x().start());
84 const auto window_end_x = static_cast<int>(window.x().end());
85
86 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
87 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
88
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010089 Iterator in(src, win_collapsed);
90 Iterator out(dst, win_collapsed);
91 if(bias != nullptr)
Georgios Pinitas448a81f2019-11-21 14:10:25 +000092 {
93 Window win_biases;
94 win_biases.set(Window::DimX, Window::Dimension(0, 1, 1));
95 win_biases.set(Window::DimY, Window::Dimension(0, 1, 1));
96
Manuel Bottiniae58bdf2021-06-17 17:18:45 +010097 Iterator bias_i(bias, win_biases);
Georgios Pinitas448a81f2019-11-21 14:10:25 +000098 execute_window_loop(win_collapsed, [&](const Coordinates &)
99 {
100 // Compute 16 elements per iteration
101 int x = window_start_x;
102 for(; x <= (window_end_x - window_step_x); x += window_step_x)
103 {
104 int32x4x4_t in_s32 =
105 {
106 {
107 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
108 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
109 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
110 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
111 }
112 };
113
114 const int32x4x4_t bias_s32 =
115 {
116 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100117 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 0),
118 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 4),
119 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 8),
120 vld1q_s32(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x + 12)
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000121 }
122 };
123
124 // Add the bias to GEMM's result
125 in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]);
126 in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]);
127 in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]);
128 in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]);
129
130 vst1q_s8(reinterpret_cast<int8_t *>(out.ptr() + x),
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100131 finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_s8, max_s8, is_bounded_relu));
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000132 }
133
134 // Compute left-over elements
135 for(; x < window_end_x; ++x)
136 {
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100137 const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias_i.ptr()) + x);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000138 int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
139
140 // Add bias
141 in_value += bias_value;
142 // Finalize and store the result
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100143 *reinterpret_cast<int8_t *>(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift,
144 static_cast<int8_t>(_min), static_cast<int8_t>(_max), is_bounded_relu);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000145 }
146 },
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100147 in, out, bias_i);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000148 }
149 else
150 {
151 execute_window_loop(win_collapsed, [&](const Coordinates &)
152 {
153 // Compute 16 elements per iteration
154 int x = window_start_x;
155 for(; x <= (window_end_x - window_step_x); x += window_step_x)
156 {
157 int32x4x4_t in_s32 =
158 {
159 {
160 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0),
161 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4),
162 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8),
163 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12)
164 }
165 };
166
167 vst1q_s8(reinterpret_cast<int8_t *>(out.ptr() + x),
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100168 finalize_quantization(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_s8, max_s8, is_bounded_relu));
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000169 }
170
171 // Compute left-over elements
172 for(; x < window_end_x; ++x)
173 {
174 const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x);
175
176 // Finalize and store the result
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100177 *reinterpret_cast<int8_t *>(out.ptr() + x) = finalize_quantization(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift,
178 static_cast<int8_t>(_min), static_cast<int8_t>(_max), is_bounded_relu);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000179 }
180 },
181 in, out);
182 }
183}
184
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100185void CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::configure(ITensorInfo *src, ITensorInfo *bias, ITensorInfo *dst, int result_fixedpoint_multiplier, int result_shift,
186 int result_offset_after_shift, int min, int max)
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000187{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100188 ARM_COMPUTE_UNUSED(bias);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000189 // Perform validate step
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100190 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
191 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, bias, dst, min, max));
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000192
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000193 _result_fixedpoint_multiplier = result_fixedpoint_multiplier;
194 _result_shift = result_shift;
195 _result_offset_after_shift = result_offset_after_shift;
196 _min = min;
197 _max = max;
198
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100199 // Output auto initialization if not yet initialized
200 auto_init_if_empty(*dst, src->clone()->set_data_type(DataType::QASYMM8_SIGNED));
201
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000202 // Configure kernel window
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100203 Window win_config = calculate_max_window(*src, Steps());
204 ICpuKernel::configure(win_config);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000205
206 // Check if we need to clamp the result using min and max
Giorgio Arena1856ff72020-02-07 13:46:45 +0000207 const bool is_bounded_relu = !(min <= -128 && max >= 127);
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100208 _func = is_bounded_relu ? &CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run_internal<true> :
209 &CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run_internal<false>;
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000210}
211
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100212Status CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::validate(const ITensorInfo *src, const ITensorInfo *bias, const ITensorInfo *dst, int min, int max)
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000213{
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100214 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
215 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, bias, dst, min, max));
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000216 return Status{};
217}
218
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100219void CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000220{
221 ARM_COMPUTE_UNUSED(info);
222 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100223 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
224 ARM_COMPUTE_ERROR_ON_MSG(tensors.empty(), "No inputs provided");
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000225
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100226 auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
227 auto bias = tensors.get_const_tensor(TensorType::ACL_BIAS);
228 auto dst = tensors.get_tensor(TensorType::ACL_DST);
229
230 (this->*_func)(src, bias, dst, window);
Georgios Pinitas448a81f2019-11-21 14:10:25 +0000231}
Manuel Bottiniae58bdf2021-06-17 17:18:45 +0100232
233const char *CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel::name() const
234{
235 return "CpuGemmLowpQuantizeDownInt32ToInt8ScaleByFixedPointKernel";
236}
237} // namespace kernels
238} // namespace cpu
Michalis Spyrou70d43a32020-06-22 17:05:43 +0100239} // namespace arm_compute