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