Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, 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/NEGEMMMatrixAdditionKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | |
| 32 | #include <arm_neon.h> |
| 33 | |
| 34 | using namespace arm_compute; |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | class Coordinates; |
| 39 | } // namespace arm_compute |
| 40 | |
| 41 | namespace |
| 42 | { |
| 43 | void matrix_addition_f32(const ITensor *input, ITensor *output, const Window &window, float beta) |
| 44 | { |
| 45 | const float32x4_t beta_f32 = vdupq_n_f32(beta); |
| 46 | |
| 47 | Iterator in(input, window); |
| 48 | Iterator out(output, window); |
| 49 | |
| 50 | execute_window_loop(window, [&](const Coordinates & id) |
| 51 | { |
| 52 | const auto in_ptr = reinterpret_cast<const float *>(in.ptr()); |
| 53 | const auto out_ptr = reinterpret_cast<float *>(out.ptr()); |
| 54 | |
Pablo Tello | 221f381 | 2017-06-28 17:27:56 +0100 | [diff] [blame] | 55 | float32x4x4_t alpha_ab = vld4q_f32(out_ptr); |
| 56 | const float32x4x4_t c = vld4q_f32(in_ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | |
| 58 | // Multiply matrix C by its weight and accumulate |
| 59 | alpha_ab.val[0] = vmlaq_f32(alpha_ab.val[0], c.val[0], beta_f32); |
| 60 | alpha_ab.val[1] = vmlaq_f32(alpha_ab.val[1], c.val[1], beta_f32); |
| 61 | alpha_ab.val[2] = vmlaq_f32(alpha_ab.val[2], c.val[2], beta_f32); |
| 62 | alpha_ab.val[3] = vmlaq_f32(alpha_ab.val[3], c.val[3], beta_f32); |
| 63 | |
Pablo Tello | 221f381 | 2017-06-28 17:27:56 +0100 | [diff] [blame] | 64 | vst4q_f32(out_ptr, alpha_ab); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 65 | }, |
| 66 | in, out); |
| 67 | } |
| 68 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 69 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | void matrix_addition_f16(const ITensor *input, ITensor *output, const Window &window, float beta) |
| 71 | { |
| 72 | const float16x8_t beta_f16 = vdupq_n_f16(beta); |
| 73 | |
| 74 | Iterator in(input, window); |
| 75 | Iterator out(output, window); |
| 76 | |
| 77 | execute_window_loop(window, [&](const Coordinates & id) |
| 78 | { |
| 79 | const auto in_ptr = reinterpret_cast<const float16_t *>(in.ptr()); |
| 80 | const auto out_ptr = reinterpret_cast<float16_t *>(out.ptr()); |
| 81 | |
Pablo Tello | 221f381 | 2017-06-28 17:27:56 +0100 | [diff] [blame] | 82 | float16x8x2_t alpha_ab = vld2q_f16(out_ptr); |
| 83 | const float16x8x2_t c = vld2q_f16(in_ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 84 | // Multiply matrix C by its weight and accumulate |
| 85 | alpha_ab.val[0] = vaddq_f16(alpha_ab.val[0], vmulq_f16(c.val[0], beta_f16)); |
| 86 | alpha_ab.val[1] = vaddq_f16(alpha_ab.val[1], vmulq_f16(c.val[1], beta_f16)); |
| 87 | |
Pablo Tello | 221f381 | 2017-06-28 17:27:56 +0100 | [diff] [blame] | 88 | vst2q_f16(out_ptr + 0, alpha_ab); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 89 | }, |
| 90 | in, out); |
| 91 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 92 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | |
| 94 | void matrix_addition_qs8(const ITensor *input, ITensor *output, const Window &window, float beta) |
| 95 | { |
| 96 | const int fixed_point_position = input->info()->fixed_point_position(); |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 97 | const qint8x16_t beta_qs8 = vdupq_n_qs8(sqcvt_qs8_f32(beta, fixed_point_position)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 98 | |
| 99 | Iterator in(input, window); |
| 100 | Iterator out(output, window); |
| 101 | |
| 102 | execute_window_loop(window, [&](const Coordinates & id) |
| 103 | { |
| 104 | const auto in_ptr = reinterpret_cast<const qint8_t *>(in.ptr()); |
| 105 | const auto out_ptr = reinterpret_cast<qint8_t *>(out.ptr()); |
| 106 | |
| 107 | qint8x16_t alpha_ab = vld1q_qs8(out_ptr); |
| 108 | const qint8x16_t c = vld1q_qs8(in_ptr); |
| 109 | |
| 110 | // Multiply matrix C by its weight and accumulate |
| 111 | alpha_ab = vqmlaq_qs8(alpha_ab, c, beta_qs8, fixed_point_position); |
| 112 | |
| 113 | vst1q_qs8(out_ptr, alpha_ab); |
| 114 | }, |
| 115 | in, out); |
| 116 | } |
Gian Marco Iodice | bdb6b0b | 2017-06-30 12:21:00 +0100 | [diff] [blame] | 117 | |
| 118 | void matrix_addition_qs16(const ITensor *input, ITensor *output, const Window &window, float beta) |
| 119 | { |
| 120 | const int fixed_point_position = input->info()->fixed_point_position(); |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 121 | const qint16x8_t beta_qs16 = vdupq_n_qs16(sqcvt_qs16_f32(beta, fixed_point_position)); |
Gian Marco Iodice | bdb6b0b | 2017-06-30 12:21:00 +0100 | [diff] [blame] | 122 | |
| 123 | Iterator in(input, window); |
| 124 | Iterator out(output, window); |
| 125 | |
| 126 | execute_window_loop(window, [&](const Coordinates & id) |
| 127 | { |
| 128 | const auto in_ptr = reinterpret_cast<const qint16_t *>(in.ptr()); |
| 129 | const auto out_ptr = reinterpret_cast<qint16_t *>(out.ptr()); |
| 130 | |
| 131 | qint16x8x2_t alpha_ab = vld2q_s16(out_ptr); |
| 132 | const qint16x8x2_t c = vld2q_s16(in_ptr); |
| 133 | |
| 134 | // Multiply matrix C by its weight and accumulate |
| 135 | alpha_ab.val[0] = vqmlaq_qs16(alpha_ab.val[0], c.val[0], beta_qs16, fixed_point_position); |
| 136 | alpha_ab.val[1] = vqmlaq_qs16(alpha_ab.val[1], c.val[1], beta_qs16, fixed_point_position); |
| 137 | |
| 138 | vst2q_s16(out_ptr, alpha_ab); |
| 139 | }, |
| 140 | in, out); |
| 141 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | } // namespace |
| 143 | |
| 144 | NEGEMMMatrixAdditionKernel::NEGEMMMatrixAdditionKernel() |
| 145 | : INESimpleKernel(), _func(nullptr), _beta(0.0f) |
| 146 | { |
| 147 | } |
| 148 | |
| 149 | void NEGEMMMatrixAdditionKernel::configure(const ITensor *input, ITensor *output, float beta) |
| 150 | { |
Gian Marco Iodice | bdb6b0b | 2017-06-30 12:21:00 +0100 | [diff] [blame] | 151 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
| 152 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 154 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 155 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != output->info()->dimension(0)); |
| 156 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != output->info()->dimension(1)); |
| 157 | |
| 158 | switch(input->info()->data_type()) |
| 159 | { |
| 160 | case DataType::F32: |
| 161 | _func = &matrix_addition_f32; |
| 162 | break; |
| 163 | case DataType::QS8: |
| 164 | _func = &matrix_addition_qs8; |
| 165 | break; |
Gian Marco Iodice | bdb6b0b | 2017-06-30 12:21:00 +0100 | [diff] [blame] | 166 | case DataType::QS16: |
| 167 | _func = &matrix_addition_qs16; |
| 168 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | case DataType::F16: |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 170 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | _func = &matrix_addition_f16; |
| 172 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 173 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | default: |
| 175 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 180 | |
| 181 | INESimpleKernel::configure(input, output, num_elems_processed_per_iteration); |
| 182 | |
| 183 | _beta = beta; |
| 184 | } |
| 185 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 186 | void NEGEMMMatrixAdditionKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 188 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 190 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 191 | |
| 192 | if(_beta != 0.0f) |
| 193 | { |
| 194 | (*_func)(_input, _output, window, _beta); |
| 195 | } |
| 196 | } |