blob: aa3c28078841c8f3fc5d7b2de34511e31bf3de39 [file] [log] [blame]
Gian Marcoe75a02b2017-11-08 12:24:09 +00001/*
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/NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel.h"
25
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
29#include "arm_compute/core/Types.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/Window.h"
32
33#include <arm_neon.h>
34#include <cstddef>
35#include <cstdint>
36
37using namespace arm_compute;
38
39namespace arm_compute
40{
41class Coordinates;
42} // namespace arm_compute
43
44NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel()
45 : _input(nullptr), _output(nullptr), _result_offset(0), _result_mult_int(0), _result_shift(0)
46{
47}
48
49void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::configure(const ITensor *input, ITensor *output, int result_offset, int result_mult_int, int result_shift)
50{
51 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32);
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
53
54 _input = input;
55 _output = output;
56 _result_offset = result_offset;
57 _result_mult_int = result_mult_int;
58 _result_shift = result_shift;
59
60 constexpr unsigned int num_elems_processed_per_iteration = 16;
61
62 // Configure kernel window
63 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
64
65 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
66 AccessWindowHorizontal output_result_access(output->info(), 0, num_elems_processed_per_iteration);
67
68 update_window_and_padding(win,
69 input_access,
70 output_result_access);
71
72 output_result_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape()));
73
74 INEKernel::configure(win);
75}
76
77void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run(const Window &window, const ThreadInfo &info)
78{
79 ARM_COMPUTE_UNUSED(info);
80 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
81 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
82
83 const int32x4_t result_offset_s32 = vdupq_n_s32(_result_offset);
84 const int32x4_t result_shift_s32 = vdupq_n_s32(-_result_shift);
85 const int32x4_t zero_s32 = vdupq_n_s32(0);
86
87 Iterator in(_input, window);
88 Iterator out(_output, window);
89
90 execute_window_loop(window, [&](const Coordinates & id)
91 {
92 int32x4x4_t in_s32 =
93 {
94 {
95 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 0),
96 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 4),
97 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 8),
98 vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 12)
99 }
100 };
101
102 // Add the offset terms to GEMM's result
103 in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_s32);
104 in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_s32);
105 in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_s32);
106 in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_s32);
107
108 // Multiply by c_mult_int
109 in_s32.val[0] = vmulq_n_s32(in_s32.val[0], _result_mult_int);
110 in_s32.val[1] = vmulq_n_s32(in_s32.val[1], _result_mult_int);
111 in_s32.val[2] = vmulq_n_s32(in_s32.val[2], _result_mult_int);
112 in_s32.val[3] = vmulq_n_s32(in_s32.val[3], _result_mult_int);
113
114 // Shift final result (negative value shift right)
115 in_s32.val[0] = vshlq_s32(in_s32.val[0], result_shift_s32);
116 in_s32.val[1] = vshlq_s32(in_s32.val[1], result_shift_s32);
117 in_s32.val[2] = vshlq_s32(in_s32.val[2], result_shift_s32);
118 in_s32.val[3] = vshlq_s32(in_s32.val[3], result_shift_s32);
119
120 // Saturate negative values
121 in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32);
122 in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32);
123 in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32);
124 in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32);
125
126 // Convert S32 to S16
127 const int16x8x2_t in_s16 =
128 {
129 {
130 vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])),
131 vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3]))
132 }
133 };
134
135 // Convert S16 to U8
136 const uint8x16_t out_u8 = vcombine_u8(vqmovun_s16(in_s16.val[0]), vqmovun_s16(in_s16.val[1]));
137
138 vst1q_u8(out.ptr(), out_u8);
139 },
140 in, out);
141}