Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 1 | /* |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/kernels/CpuGemmLowpMatrixReductionKernel.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 25 | |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/ITensor.h" |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/KernelDescriptors.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/TensorInfo.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 29 | #include "src/core/NEON/wrapper/wrapper.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/helpers/AutoConfiguration.h" |
| 31 | #include "src/core/helpers/WindowHelpers.h" |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 32 | |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 33 | namespace arm_compute |
| 34 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 35 | namespace cpu |
| 36 | { |
| 37 | namespace kernels |
| 38 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 39 | namespace |
| 40 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 41 | Status validate_arguments_matrix_a_reduction(const ITensorInfo *src, const ITensorInfo *dst, const GEMMLowpReductionKernelInfo &info) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 42 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 43 | ARM_COMPUTE_UNUSED(info); |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 45 | ARM_COMPUTE_ERROR_ON_MSG(info.is_reshaped == true, "Not supported"); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8, DataType::QSYMM8_PER_CHANNEL); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 47 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 48 | if(dst->total_size() > 0) |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 49 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::S32); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->dimension(0) != src->dimension(1), "Output vector must have length equal to the number of rows of the input matrix"); |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 52 | } |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 53 | return Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 54 | } |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 55 | Status validate_arguments_matrix_b_reduction(const ITensorInfo *src, const ITensorInfo *dst, const GEMMLowpReductionKernelInfo &info) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 56 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_UNUSED(info); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 59 | ARM_COMPUTE_ERROR_ON_MSG(info.is_reshaped == true, "Not supported"); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8, DataType::QSYMM8_PER_CHANNEL); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 61 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 62 | if(dst->total_size() > 0) |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 63 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::S32); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(dst->dimension(0) != src->dimension(0), "Output vector must have length equal to the number of columns of the input matrix"); |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 66 | } |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 67 | return Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 68 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 69 | } // namespace |
| 70 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 71 | void CpuGemmLowpMatrixAReductionKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const GEMMLowpReductionKernelInfo &info) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 72 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 73 | // Perform validate step |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 74 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 75 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_a_reduction(src, dst, info)); |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 76 | _k = info.k; |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 77 | _scalar = info.scalar; |
| 78 | _mul_by_scalar = info.mul_by_scalar; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 79 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 80 | switch(src->data_type()) |
| 81 | { |
| 82 | case DataType::QASYMM8: |
| 83 | _func = &CpuGemmLowpMatrixAReductionKernel::run_internal<uint8_t>; |
| 84 | break; |
| 85 | case DataType::QASYMM8_SIGNED: |
| 86 | case DataType::QSYMM8: |
| 87 | case DataType::QSYMM8_PER_CHANNEL: |
| 88 | _func = &CpuGemmLowpMatrixAReductionKernel::run_internal<int8_t>; |
| 89 | break; |
| 90 | default: |
| 91 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 92 | } |
| 93 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 94 | // Output auto initialization if not yet initialized |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 95 | auto_init_if_empty(*dst, TensorShape(src->dimension(1)), 1, DataType::S32); |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 96 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 97 | Window win = calculate_max_window(*dst, Steps(1)); |
| 98 | ICpuKernel::configure(win); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 99 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 100 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 101 | Status CpuGemmLowpMatrixAReductionKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const GEMMLowpReductionKernelInfo &info) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 102 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 103 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_a_reduction(src, dst, info)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 104 | return Status{}; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 107 | template <typename T> |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 108 | void CpuGemmLowpMatrixAReductionKernel::run_internal(const ITensor *src, ITensor *dst, const arm_compute::Window &window) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 109 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 110 | // Intermediate and final accumulator types |
| 111 | using TIAcc = wrapper::traits::promote_t<T>; |
| 112 | using TAcc = wrapper::traits::promote_t<TIAcc>; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 113 | |
| 114 | Window collapsed_window = window.collapse_if_possible(IKernel::window(), Window::DimY); |
| 115 | |
| 116 | Window win_input(collapsed_window); |
| 117 | win_input.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 118 | win_input.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 119 | win_input.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 120 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 121 | Iterator in(src, win_input); |
| 122 | Iterator out(dst, collapsed_window); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 123 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 124 | execute_window_loop(collapsed_window, [&](const Coordinates & id) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 125 | { |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 126 | auto vsum_row = wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}); |
| 127 | TAcc sum_row = 0; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 128 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 129 | const T *matrix_a = reinterpret_cast<const T *>((in.ptr() + id.x() * src->info()->strides_in_bytes()[1] + id.y() * src->info()->strides_in_bytes()[2])); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 130 | |
| 131 | #if __arm__ |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 132 | asm volatile("PLD [%0, #128*4]" ::"r"(matrix_a)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 133 | #endif /* __arm__ */ |
| 134 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 135 | int i = 0; |
| 136 | // This for loop performs 16 accumulations |
| 137 | for(; i <= (_k - 16); i += 16) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 138 | { |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 139 | const auto a0_d8 = wrapper::vloadq(matrix_a + i); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 140 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 141 | // Partial accumulations in U16 |
| 142 | const auto tmp_sum0 = wrapper::vaddl(wrapper::vgetlow(a0_d8), wrapper::vgethigh(a0_d8)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 143 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 144 | // Accumulate to U32 |
| 145 | vsum_row = wrapper::vadd(vsum_row, wrapper::vpaddl(tmp_sum0)); |
| 146 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 147 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 148 | // This for loop performs the leftover accumulations |
| 149 | for(; i < _k; ++i) |
| 150 | { |
| 151 | sum_row += static_cast<TAcc>(matrix_a[i]); |
| 152 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 153 | |
| 154 | #if defined(__aarch64__) |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 155 | // Reduction operation available on 64 bit architectures only |
| 156 | sum_row += wrapper::vaddv(vsum_row); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 157 | #else // __aarch64__ |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 158 | auto tmp = wrapper::vpadd(wrapper::vgethigh(vsum_row), wrapper::vgetlow(vsum_row)); |
| 159 | tmp = wrapper::vpadd(tmp, tmp); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 160 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 161 | sum_row += wrapper::vgetlane(tmp, 0); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 162 | #endif // __aarch64__ |
| 163 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 164 | // Multiply by scalar if necessary |
| 165 | if(_mul_by_scalar) |
| 166 | { |
| 167 | sum_row *= _scalar; |
| 168 | } |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 169 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 170 | *(reinterpret_cast<int *>(out.ptr())) = static_cast<int32_t>(sum_row); |
| 171 | }, |
| 172 | in, out); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 175 | void CpuGemmLowpMatrixAReductionKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 176 | { |
| 177 | ARM_COMPUTE_UNUSED(info); |
| 178 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 179 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 180 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 181 | auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 182 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 183 | |
| 184 | (this->*_func)(src, dst, window); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 187 | const char *CpuGemmLowpMatrixAReductionKernel::name() const |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 188 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 189 | return "CpuGemmLowpMatrixAReductionKernel"; |
| 190 | } |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 191 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 192 | void CpuGemmLowpMatrixBReductionKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const GEMMLowpReductionKernelInfo &info) |
| 193 | { |
| 194 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 195 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_b_reduction(src, dst, info)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 196 | |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 197 | _k = info.k; |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 198 | _scalar = info.scalar; |
| 199 | _mul_by_scalar = info.mul_by_scalar; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 200 | |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 201 | // Configure kernel window |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 202 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 203 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 204 | switch(src->data_type()) |
| 205 | { |
| 206 | case DataType::QASYMM8: |
| 207 | _func = &CpuGemmLowpMatrixBReductionKernel::run_internal<uint8_t>; |
| 208 | break; |
| 209 | case DataType::QASYMM8_SIGNED: |
| 210 | case DataType::QSYMM8: |
| 211 | case DataType::QSYMM8_PER_CHANNEL: |
| 212 | _func = &CpuGemmLowpMatrixBReductionKernel::run_internal<int8_t>; |
| 213 | break; |
| 214 | default: |
| 215 | ARM_COMPUTE_ERROR("Unsupported data type"); |
| 216 | } |
| 217 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 218 | // Output auto initialization if not yet initialized |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 219 | auto_init_if_empty(*dst, TensorShape(src->dimension(0)), 1, DataType::S32); |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 220 | |
| 221 | // Configure kernel window |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 222 | Window win = calculate_max_window_horizontal(*dst, Steps(num_elems_processed_per_iteration)); |
| 223 | ICpuKernel::configure(win); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 224 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 225 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 226 | Status CpuGemmLowpMatrixBReductionKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const GEMMLowpReductionKernelInfo &info) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 227 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 228 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_b_reduction(src, dst, info)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 229 | return Status{}; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 232 | template <typename T> |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 233 | void CpuGemmLowpMatrixBReductionKernel::run_internal(const ITensor *src, ITensor *dst, const Window &window, const ThreadInfo &info) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 234 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 235 | // Intermediate and final accumulator types |
| 236 | using TIAcc = wrapper::traits::promote_t<T>; |
| 237 | using TAcc = wrapper::traits::promote_t<TIAcc>; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 238 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 239 | Window collapsed_window = window.collapse_if_possible(IKernel::window(), Window::DimY); |
| 240 | const auto vec_scalar = wrapper::vdup_n(static_cast<TAcc>(_scalar), wrapper::traits::vector_128_tag{}); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 241 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 242 | const auto width_matrix_b = static_cast<int>(src->info()->dimension(0)); |
| 243 | const auto in_b_stride = static_cast<int>(src->info()->strides_in_bytes()[1]); |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 244 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 245 | // The implementation computes 16 elements per iteration |
| 246 | const int window_start_x = 16 * info.thread_id; |
| 247 | const int window_step_x = 16 * info.num_threads; |
| 248 | // Make sure (window_end_x - window_start_x) is a multiple of window_step_x |
| 249 | const int window_end_x = ceil_to_multiple(width_matrix_b - window_start_x, window_step_x) + window_start_x; |
| 250 | |
| 251 | Window win_out(collapsed_window); |
| 252 | win_out.set(Window::DimX, Window::Dimension(window_start_x, window_end_x, window_step_x)); |
| 253 | |
| 254 | Window win_in(win_out); |
| 255 | win_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 256 | win_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 257 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 258 | Iterator inb(src, win_in); |
| 259 | Iterator out(dst, win_out); |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 260 | |
| 261 | execute_window_loop(win_out, [&](const Coordinates & id) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 262 | { |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 263 | if(id.x() > width_matrix_b) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 264 | { |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 265 | return; |
| 266 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 267 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 268 | // Note: Since the input is unsigned char, we can safely use unsigned int for the accumulation |
| 269 | typename wrapper::traits::neon_bitvector<TAcc, wrapper::traits::BitWidth::W128>::type sum_col[4] = |
| 270 | { |
| 271 | wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}), |
| 272 | wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}), |
| 273 | wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}), |
| 274 | wrapper::vdup_n(static_cast<TAcc>(0), wrapper::traits::vector_128_tag{}) |
| 275 | }; |
| 276 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 277 | const auto *matrix_b = reinterpret_cast<const T *>(inb.ptr() + id.y() * src->info()->strides_in_bytes()[2]); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 278 | |
| 279 | #if __arm__ |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 280 | asm volatile("PLD [%0, #128*4]" ::"r"(matrix_b)); |
| 281 | asm volatile("PLD [%0, #128*4]" ::"r"(matrix_b + in_b_stride)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 282 | #endif /* __arm__ */ |
| 283 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 284 | int i = 0; |
| 285 | // This for loop performs 4 accumulations |
| 286 | for(; i <= (_k - 4); i += 4) |
| 287 | { |
| 288 | const auto b0_u8 = wrapper::vloadq(matrix_b + 0 * in_b_stride); |
| 289 | const auto b1_u8 = wrapper::vloadq(matrix_b + 1 * in_b_stride); |
| 290 | const auto b2_u8 = wrapper::vloadq(matrix_b + 2 * in_b_stride); |
| 291 | const auto b3_u8 = wrapper::vloadq(matrix_b + 3 * in_b_stride); |
| 292 | |
| 293 | #if __arm__ |
| 294 | asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 1 * in_b_stride)); |
| 295 | asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 2 * in_b_stride)); |
| 296 | asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 3 * in_b_stride)); |
| 297 | asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 4 * in_b_stride)); |
| 298 | #endif /* __arm__ */ |
| 299 | |
| 300 | // Partial accumulation in 16bit |
| 301 | typename wrapper::traits::neon_bitvector<TIAcc, wrapper::traits::BitWidth::W128>::type tmp_sum[2] = |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 302 | { |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 303 | wrapper::vdup_n(static_cast<TIAcc>(0), wrapper::traits::vector_128_tag{}), |
| 304 | wrapper::vdup_n(static_cast<TIAcc>(0), wrapper::traits::vector_128_tag{}) |
| 305 | }; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 306 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 307 | tmp_sum[0] = wrapper::vaddw(tmp_sum[0], wrapper::vgetlow(b1_u8)); |
| 308 | tmp_sum[0] = wrapper::vaddw(tmp_sum[0], wrapper::vgetlow(b0_u8)); |
| 309 | tmp_sum[0] = wrapper::vaddw(tmp_sum[0], wrapper::vgetlow(b2_u8)); |
| 310 | tmp_sum[0] = wrapper::vaddw(tmp_sum[0], wrapper::vgetlow(b3_u8)); |
| 311 | tmp_sum[1] = wrapper::vaddw(tmp_sum[1], wrapper::vgethigh(b0_u8)); |
| 312 | tmp_sum[1] = wrapper::vaddw(tmp_sum[1], wrapper::vgethigh(b1_u8)); |
| 313 | tmp_sum[1] = wrapper::vaddw(tmp_sum[1], wrapper::vgethigh(b2_u8)); |
| 314 | tmp_sum[1] = wrapper::vaddw(tmp_sum[1], wrapper::vgethigh(b3_u8)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 315 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 316 | // Accumulate to 32bit |
| 317 | sum_col[0] = wrapper::vaddw(sum_col[0], wrapper::vgetlow(tmp_sum[0])); |
| 318 | sum_col[1] = wrapper::vaddw(sum_col[1], wrapper::vgethigh(tmp_sum[0])); |
| 319 | sum_col[2] = wrapper::vaddw(sum_col[2], wrapper::vgetlow(tmp_sum[1])); |
| 320 | sum_col[3] = wrapper::vaddw(sum_col[3], wrapper::vgethigh(tmp_sum[1])); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 321 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 322 | matrix_b += 4 * in_b_stride; |
| 323 | } |
| 324 | |
| 325 | // This for loop perfoms the leftover accumulations |
| 326 | for(; i < _k; ++i) |
| 327 | { |
| 328 | const auto b0_b8 = wrapper::vloadq(matrix_b + 0 * in_b_stride); |
| 329 | |
| 330 | // Convert S8 to S16 |
| 331 | const typename wrapper::traits::neon_bitvector<TIAcc, wrapper::traits::BitWidth::W128>::type b0_b16[2] |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 332 | { |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 333 | wrapper::vmovl(wrapper::vgetlow(b0_b8)), |
| 334 | wrapper::vmovl(wrapper::vgethigh(b0_b8)) |
| 335 | }; |
Michele Di Giorgio | a602f03 | 2020-03-12 19:34:33 +0000 | [diff] [blame] | 336 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 337 | // Accumulate to 32bit |
| 338 | sum_col[0] = wrapper::vaddw(sum_col[0], wrapper::vgetlow(b0_b16[0])); |
| 339 | sum_col[1] = wrapper::vaddw(sum_col[1], wrapper::vgethigh(b0_b16[0])); |
| 340 | sum_col[2] = wrapper::vaddw(sum_col[2], wrapper::vgetlow(b0_b16[1])); |
| 341 | sum_col[3] = wrapper::vaddw(sum_col[3], wrapper::vgethigh(b0_b16[1])); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 342 | |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 343 | matrix_b += in_b_stride; |
| 344 | } |
| 345 | |
| 346 | // Multiply by scalar if necessary |
| 347 | if(_mul_by_scalar) |
| 348 | { |
| 349 | sum_col[0] = wrapper::vmul(sum_col[0], vec_scalar); |
| 350 | sum_col[1] = wrapper::vmul(sum_col[1], vec_scalar); |
| 351 | sum_col[2] = wrapper::vmul(sum_col[2], vec_scalar); |
| 352 | sum_col[3] = wrapper::vmul(sum_col[3], vec_scalar); |
| 353 | } |
| 354 | |
| 355 | auto vector_sum_col = reinterpret_cast<int32_t *>(out.ptr()); |
| 356 | if(id.x() + 16 < width_matrix_b) |
| 357 | { |
Michele Di Giorgio | 13ec5f0 | 2020-01-02 12:11:13 +0000 | [diff] [blame] | 358 | wrapper::vstore(vector_sum_col + 0, wrapper::vreinterpret(sum_col[0])); |
| 359 | wrapper::vstore(vector_sum_col + 4, wrapper::vreinterpret(sum_col[1])); |
| 360 | wrapper::vstore(vector_sum_col + 8, wrapper::vreinterpret(sum_col[2])); |
| 361 | wrapper::vstore(vector_sum_col + 12, wrapper::vreinterpret(sum_col[3])); |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 362 | } |
| 363 | else |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 364 | { |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 365 | auto left_over = width_matrix_b - id.x(); |
| 366 | for(auto k = 0; k < 4 && left_over; ++k) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 367 | { |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 368 | for(auto j = 0; j < 4 && left_over; ++j, --left_over) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 369 | { |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 370 | *(vector_sum_col + k * 4 + j) = sum_col[k][j]; |
| 371 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 372 | } |
morgolock | c229e8c | 2020-09-25 12:03:21 +0100 | [diff] [blame] | 373 | } |
| 374 | }, |
| 375 | inb, out); |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 376 | } |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 377 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 378 | void CpuGemmLowpMatrixBReductionKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 379 | { |
| 380 | ARM_COMPUTE_UNUSED(info); |
| 381 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 382 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 383 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 384 | auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 385 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 386 | |
| 387 | (this->*_func)(src, dst, window, info); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 388 | } |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 389 | |
| 390 | const char *CpuGemmLowpMatrixBReductionKernel::name() const |
| 391 | { |
| 392 | return "CpuGemmLowpMatrixBReductionKernel"; |
| 393 | } |
| 394 | } // namespace kernels |
| 395 | } // namespace cpu |
| 396 | } // namespace arm_compute |