Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 | #ifndef SRC_CORE_NEON_KERNELS_CONV3D_QUANTIZED_H |
| 25 | #define SRC_CORE_NEON_KERNELS_CONV3D_QUANTIZED_H |
| 26 | |
| 27 | #include "arm_compute/core/Types.h" |
| 28 | #include "arm_compute/core/utils/misc/Traits.h" |
| 29 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
| 30 | #include "arm_compute/runtime/FunctionDescriptors.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | |
| 32 | #include "src/core/helpers/WindowHelpers.h" |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 33 | #include "src/core/NEON/NEAsymm.h" |
| 34 | #include "src/core/NEON/wrapper/wrapper.h" |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | namespace cpu |
| 39 | { |
| 40 | template <typename T> |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 41 | void directconv3d_quantized_neon_ndhwc(const ITensor *src0, |
| 42 | const ITensor *src1, |
| 43 | const ITensor *src2, |
| 44 | ITensor *dst, |
| 45 | const Conv3dInfo &conv_info, |
| 46 | const Window &window) |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 47 | { |
| 48 | const ITensor *src = src0; |
| 49 | const ITensor *weights = src1; |
| 50 | const ITensor *biases = src2; |
| 51 | |
| 52 | using vtype = wrapper::traits::neon_bitvector<T, wrapper::traits::BitWidth::W128>; |
| 53 | using vector_type = typename vtype::type; |
| 54 | using tag_type = typename vtype::tag_type; |
| 55 | constexpr int num_elems_read_per_iteration = 16 / sizeof(T); |
| 56 | using q16_t = typename wrapper::traits::promote_t<T>; |
| 57 | using q32_t = typename wrapper::traits::promote_t<q16_t>; |
| 58 | using q32x4_t = typename wrapper::traits::neon_vector<q32_t, 4>::type; |
| 59 | |
| 60 | const int32_t input_offset = -src->info()->quantization_info().uniform().offset; |
| 61 | const float input_scale = src->info()->quantization_info().uniform().scale; |
| 62 | const int32_t weights_offset = -weights->info()->quantization_info().uniform().offset; |
| 63 | const float weights_scale = weights->info()->quantization_info().uniform().scale; |
| 64 | const int32_t output_offset = dst->info()->quantization_info().uniform().offset; |
| 65 | const float output_scale = dst->info()->quantization_info().uniform().scale; |
| 66 | |
| 67 | int32_t output_multiplier = 0; |
| 68 | int32_t output_shift = 0; |
| 69 | const float multiplier = input_scale * weights_scale / output_scale; |
| 70 | arm_compute::quantization::calculate_quantized_multiplier(multiplier, &output_multiplier, &output_shift); |
| 71 | |
| 72 | // Scalar quantities (N D H W Cin) |
| 73 | const int element_size = src->info()->element_size(); |
| 74 | const int input_stride_w = src->info()->strides_in_bytes().y() / element_size; |
| 75 | const int input_stride_h = src->info()->strides_in_bytes().z() / element_size; |
| 76 | const int input_stride_d = src->info()->strides_in_bytes()[3] / element_size; |
| 77 | const int input_stride_n = src->info()->strides_in_bytes()[4] / element_size; |
| 78 | const int input_dim_w = src->info()->dimension(1); |
| 79 | const int input_dim_h = src->info()->dimension(2); |
| 80 | const int input_dim_d = src->info()->dimension(3); |
| 81 | |
| 82 | // Kernel info (D H W Cin Cout) |
| 83 | const unsigned int kernel_stride_w = weights->info()->strides_in_bytes()[2] / element_size; |
| 84 | const unsigned int kernel_stride_h = weights->info()->strides_in_bytes()[3] / element_size; |
| 85 | const unsigned int kernel_stride_d = weights->info()->strides_in_bytes()[4] / element_size; |
| 86 | const int kernel_dim_w = weights->info()->dimension(2); |
| 87 | const int kernel_dim_h = weights->info()->dimension(3); |
| 88 | const int kernel_dim_d = weights->info()->dimension(4); |
| 89 | |
| 90 | // Convolution padding and stride |
| 91 | const int conv_pad_top = conv_info.padding.top; |
| 92 | const int conv_pad_left = conv_info.padding.left; |
| 93 | const int conv_pad_front = conv_info.padding.front; |
| 94 | const int conv_stride_w = conv_info.stride.width; |
| 95 | const int conv_stride_h = conv_info.stride.height; |
| 96 | const int conv_stride_d = conv_info.stride.depth; |
| 97 | |
| 98 | // Setup input window for the output iterator |
| 99 | Window window_out = window; |
| 100 | window_out.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 101 | |
| 102 | // Setup input window for the weights iterator |
| 103 | Window window_w = calculate_max_window(*weights->info(), Steps()); |
| 104 | window_w.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 105 | window_w.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
| 106 | window_w.set(Window::DimW, Window::Dimension(0, 1, 1)); |
| 107 | window_w.set(4, Window::Dimension(0, 1, 1)); |
| 108 | |
| 109 | Iterator out(dst, window_out); |
| 110 | Iterator wei(weights, window_w); |
| 111 | |
| 112 | const int32_t *biases_ptr = nullptr; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 113 | if (biases != nullptr) |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 114 | { |
| 115 | biases_ptr = reinterpret_cast<int32_t *>(biases->buffer() + biases->info()->offset_first_element_in_bytes()); |
| 116 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 117 | execute_window_loop( |
| 118 | window_out, |
| 119 | [&](const Coordinates &id) |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 120 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 121 | // We are computing the theoretical input starting points |
| 122 | const int in_w_start_t = static_cast<int>(id.y()) * conv_stride_w - conv_pad_left; |
| 123 | const int in_h_start_t = static_cast<int>(id.z()) * conv_stride_h - conv_pad_top; |
| 124 | const int in_d_start_t = static_cast<int>(id[3]) * conv_stride_d - conv_pad_front; |
| 125 | const int in_w_end_t = in_w_start_t + kernel_dim_w; |
| 126 | const int in_h_end_t = in_h_start_t + kernel_dim_h; |
| 127 | const int in_d_end_t = in_d_start_t + kernel_dim_d; |
| 128 | |
| 129 | // We are computing the valid initial and ending input points by checking the borders |
| 130 | const int in_w_start = std::max(in_w_start_t, 0); |
| 131 | const int in_h_start = std::max(in_h_start_t, 0); |
| 132 | const int in_d_start = std::max(in_d_start_t, 0); |
| 133 | const int in_w_end = std::min(in_w_end_t, input_dim_w); |
| 134 | const int in_h_end = std::min(in_h_end_t, input_dim_h); |
| 135 | const int in_d_end = std::min(in_d_end_t, input_dim_d); |
| 136 | |
| 137 | // We use the input points to select the valid weight points to use |
| 138 | const int wei_w_start = in_w_start - in_w_start_t; |
| 139 | const int wei_h_start = in_h_start - in_h_start_t; |
| 140 | const int wei_d_start = in_d_start - in_d_start_t; |
| 141 | const int wei_w_end = kernel_dim_w - (in_w_end_t - in_w_end); |
| 142 | const int wei_h_end = kernel_dim_h - (in_h_end_t - in_h_end); |
| 143 | const int wei_d_end = kernel_dim_d - (in_d_end_t - in_d_end); |
| 144 | |
| 145 | const int index_c_out_end = weights->info()->dimension(0); |
| 146 | const int index_c_in_end = weights->info()->dimension(1); |
| 147 | const T *const in_ptr_start = |
| 148 | reinterpret_cast<const T *>(src->buffer() + src->info()->offset_first_element_in_bytes()) + |
| 149 | id[4] * input_stride_n; |
| 150 | |
| 151 | execute_window_loop( |
| 152 | window_w, |
| 153 | [&](const Coordinates &id_w) |
| 154 | { |
| 155 | /* |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 156 | * This is the loop in the weights, and it goes along OFM (output feature map) |
| 157 | */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 158 | const auto weights_ptr_start = reinterpret_cast<const T *>(wei.ptr()); |
| 159 | int32_t acc = static_cast<int32_t>(0); |
| 160 | T *out_ptr = reinterpret_cast<T *>(out.ptr()); |
| 161 | for (int index_wei_d = wei_d_start, index_in_d = in_d_start; index_wei_d < wei_d_end; |
| 162 | ++index_wei_d, ++index_in_d) |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 163 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 164 | const auto in_ptr_d = in_ptr_start + index_in_d * input_stride_d; |
| 165 | const auto weights_ptr_d = weights_ptr_start + index_wei_d * kernel_stride_d; |
| 166 | for (int index_wei_h = wei_h_start, index_in_h = in_h_start; index_wei_h < wei_h_end; |
| 167 | ++index_wei_h, ++index_in_h) |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 168 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 169 | const T *const in_ptr_row = in_ptr_d + index_in_h * input_stride_h; |
| 170 | const T *const weights_ptr_row = weights_ptr_d + index_wei_h * kernel_stride_h; |
| 171 | for (int index_wei_w = wei_w_start, index_in_w = in_w_start; index_wei_w < wei_w_end; |
| 172 | ++index_wei_w, ++index_in_w) |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 173 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 174 | const T *in_ptr_mover = in_ptr_row + index_in_w * input_stride_w; |
| 175 | const T *weights_ptr_mover = weights_ptr_row + index_wei_w * kernel_stride_w; |
| 176 | int index_c_in = 0; |
| 177 | vector_type w_vec = wrapper::vdup_n(static_cast<T>(0), tag_type()); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 178 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 179 | q32x4_t acc_q32_0 = wrapper::vdup_n(static_cast<q32_t>(0), tag_type()); |
| 180 | q32x4_t acc_q32_1 = wrapper::vdup_n(static_cast<q32_t>(0), tag_type()); |
| 181 | q32x4_t acc_q32_2 = wrapper::vdup_n(static_cast<q32_t>(0), tag_type()); |
| 182 | q32x4_t acc_q32_3 = wrapper::vdup_n(static_cast<q32_t>(0), tag_type()); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 183 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 184 | for (; index_c_in <= index_c_in_end - num_elems_read_per_iteration; |
| 185 | index_c_in += num_elems_read_per_iteration, |
| 186 | in_ptr_mover += num_elems_read_per_iteration) |
| 187 | { |
| 188 | const auto src_vec = wrapper::vloadq(in_ptr_mover); |
| 189 | //Load Cin weights |
| 190 | for (int k = 0; k < num_elems_read_per_iteration; |
| 191 | ++k, weights_ptr_mover += index_c_out_end) |
| 192 | { |
| 193 | w_vec = wrapper::vsetlane(*weights_ptr_mover, w_vec, k); |
| 194 | } |
| 195 | q32x4_t src_q32_0 = wrapper::vdup_n(static_cast<q32_t>(input_offset), tag_type()); |
| 196 | q32x4_t src_q32_1 = wrapper::vdup_n(static_cast<q32_t>(input_offset), tag_type()); |
| 197 | q32x4_t src_q32_2 = wrapper::vdup_n(static_cast<q32_t>(input_offset), tag_type()); |
| 198 | q32x4_t src_q32_3 = wrapper::vdup_n(static_cast<q32_t>(input_offset), tag_type()); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 199 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 200 | q32x4_t wei_q32_0 = wrapper::vdup_n(static_cast<q32_t>(weights_offset), tag_type()); |
| 201 | q32x4_t wei_q32_1 = wrapper::vdup_n(static_cast<q32_t>(weights_offset), tag_type()); |
| 202 | q32x4_t wei_q32_2 = wrapper::vdup_n(static_cast<q32_t>(weights_offset), tag_type()); |
| 203 | q32x4_t wei_q32_3 = wrapper::vdup_n(static_cast<q32_t>(weights_offset), tag_type()); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 204 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 205 | const auto src_q16_0 = wrapper::vmovl(wrapper::vgetlow(src_vec)); |
| 206 | const auto src_q16_1 = wrapper::vmovl(wrapper::vgethigh(src_vec)); |
| 207 | const auto wei_q16_0 = wrapper::vmovl(wrapper::vgetlow(w_vec)); |
| 208 | const auto wei_q16_1 = wrapper::vmovl(wrapper::vgethigh(w_vec)); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 209 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 210 | src_q32_0 = wrapper::vadd(src_q32_0, wrapper::vmovl(wrapper::vgetlow(src_q16_0))); |
| 211 | src_q32_1 = wrapper::vadd(src_q32_1, wrapper::vmovl(wrapper::vgethigh(src_q16_0))); |
| 212 | src_q32_2 = wrapper::vadd(src_q32_2, wrapper::vmovl(wrapper::vgetlow(src_q16_1))); |
| 213 | src_q32_3 = wrapper::vadd(src_q32_3, wrapper::vmovl(wrapper::vgethigh(src_q16_1))); |
| 214 | |
| 215 | wei_q32_0 = wrapper::vadd(wei_q32_0, wrapper::vmovl(wrapper::vgetlow(wei_q16_0))); |
| 216 | wei_q32_1 = wrapper::vadd(wei_q32_1, wrapper::vmovl(wrapper::vgethigh(wei_q16_0))); |
| 217 | wei_q32_2 = wrapper::vadd(wei_q32_2, wrapper::vmovl(wrapper::vgetlow(wei_q16_1))); |
| 218 | wei_q32_3 = wrapper::vadd(wei_q32_3, wrapper::vmovl(wrapper::vgethigh(wei_q16_1))); |
| 219 | |
| 220 | acc_q32_0 = wrapper::vmla(acc_q32_0, wei_q32_0, src_q32_0); |
| 221 | acc_q32_1 = wrapper::vmla(acc_q32_1, wei_q32_1, src_q32_1); |
| 222 | acc_q32_2 = wrapper::vmla(acc_q32_2, wei_q32_2, src_q32_2); |
| 223 | acc_q32_3 = wrapper::vmla(acc_q32_3, wei_q32_3, src_q32_3); |
| 224 | } |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 225 | #if defined(__aarch64__) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 226 | acc += wrapper::vaddv(acc_q32_0); |
| 227 | acc += wrapper::vaddv(acc_q32_1); |
| 228 | acc += wrapper::vaddv(acc_q32_2); |
| 229 | acc += wrapper::vaddv(acc_q32_3); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 230 | #else // __aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 231 | auto temp = wrapper::vpadd(wrapper::vgethigh(acc_q32_0), wrapper::vgetlow(acc_q32_0)); |
| 232 | temp = wrapper::vpadd(temp, temp); |
| 233 | acc += wrapper::vgetlane(temp, 0); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 234 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 235 | temp = wrapper::vpadd(wrapper::vgethigh(acc_q32_1), wrapper::vgetlow(acc_q32_1)); |
| 236 | temp = wrapper::vpadd(temp, temp); |
| 237 | acc += wrapper::vgetlane(temp, 0); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 238 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 239 | temp = wrapper::vpadd(wrapper::vgethigh(acc_q32_2), wrapper::vgetlow(acc_q32_2)); |
| 240 | temp = wrapper::vpadd(temp, temp); |
| 241 | acc += wrapper::vgetlane(temp, 0); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 242 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 243 | temp = wrapper::vpadd(wrapper::vgethigh(acc_q32_3), wrapper::vgetlow(acc_q32_3)); |
| 244 | temp = wrapper::vpadd(temp, temp); |
| 245 | acc += wrapper::vgetlane(temp, 0); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 246 | |
| 247 | #endif // __aarch64__ |
| 248 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 249 | for (; index_c_in < index_c_in_end; |
| 250 | ++index_c_in, ++in_ptr_mover, weights_ptr_mover += index_c_out_end) |
| 251 | { |
| 252 | const auto src_val = *(in_ptr_mover) + input_offset; |
| 253 | const auto w_val = *(weights_ptr_mover) + weights_offset; |
| 254 | acc += src_val * w_val; |
| 255 | } |
| 256 | } |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 259 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 260 | if (biases) |
| 261 | { |
| 262 | acc += *reinterpret_cast<const int32_t *>(biases_ptr + id_w[0]); |
| 263 | } |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 264 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 265 | T out_val = |
| 266 | finalize_quantization(acc, output_multiplier, output_shift, output_offset, T(0), T(0), false); |
| 267 | *(reinterpret_cast<T *>(out_ptr + id_w[0])) = out_val; |
| 268 | }, |
| 269 | wei); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 270 | }, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 271 | out); |
Freddie Liardet | f727ef4 | 2021-10-18 13:28:57 +0100 | [diff] [blame] | 272 | } |
| 273 | } // namespace cpu |
| 274 | } // namespace arm_compute |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 275 | #endif // SRC_CORE_NEON_KERNELS_CONV3D_QUANTIZED_H |