Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEGEMMMatrixAdditionKernel.h" |
| 25 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame^] | 26 | #include "arm_compute/core/CPP/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | |
| 33 | #include <arm_neon.h> |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | class Coordinates; |
| 40 | } // namespace arm_compute |
| 41 | |
| 42 | namespace |
| 43 | { |
| 44 | void matrix_addition_f32(const ITensor *input, ITensor *output, const Window &window, float beta) |
| 45 | { |
| 46 | const float32x4_t beta_f32 = vdupq_n_f32(beta); |
| 47 | |
| 48 | Iterator in(input, window); |
| 49 | Iterator out(output, window); |
| 50 | |
| 51 | execute_window_loop(window, [&](const Coordinates & id) |
| 52 | { |
| 53 | const auto in_ptr = reinterpret_cast<const float *>(in.ptr()); |
| 54 | const auto out_ptr = reinterpret_cast<float *>(out.ptr()); |
| 55 | |
Pablo Tello | 221f381 | 2017-06-28 17:27:56 +0100 | [diff] [blame] | 56 | float32x4x4_t alpha_ab = vld4q_f32(out_ptr); |
| 57 | const float32x4x4_t c = vld4q_f32(in_ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 58 | |
| 59 | // Multiply matrix C by its weight and accumulate |
| 60 | alpha_ab.val[0] = vmlaq_f32(alpha_ab.val[0], c.val[0], beta_f32); |
| 61 | alpha_ab.val[1] = vmlaq_f32(alpha_ab.val[1], c.val[1], beta_f32); |
| 62 | alpha_ab.val[2] = vmlaq_f32(alpha_ab.val[2], c.val[2], beta_f32); |
| 63 | alpha_ab.val[3] = vmlaq_f32(alpha_ab.val[3], c.val[3], beta_f32); |
| 64 | |
Pablo Tello | 221f381 | 2017-06-28 17:27:56 +0100 | [diff] [blame] | 65 | vst4q_f32(out_ptr, alpha_ab); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 66 | }, |
| 67 | in, out); |
| 68 | } |
| 69 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 70 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | void matrix_addition_f16(const ITensor *input, ITensor *output, const Window &window, float beta) |
| 72 | { |
| 73 | const float16x8_t beta_f16 = vdupq_n_f16(beta); |
| 74 | |
| 75 | Iterator in(input, window); |
| 76 | Iterator out(output, window); |
| 77 | |
| 78 | execute_window_loop(window, [&](const Coordinates & id) |
| 79 | { |
| 80 | const auto in_ptr = reinterpret_cast<const float16_t *>(in.ptr()); |
| 81 | const auto out_ptr = reinterpret_cast<float16_t *>(out.ptr()); |
| 82 | |
Pablo Tello | 221f381 | 2017-06-28 17:27:56 +0100 | [diff] [blame] | 83 | float16x8x2_t alpha_ab = vld2q_f16(out_ptr); |
| 84 | const float16x8x2_t c = vld2q_f16(in_ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 85 | // Multiply matrix C by its weight and accumulate |
| 86 | alpha_ab.val[0] = vaddq_f16(alpha_ab.val[0], vmulq_f16(c.val[0], beta_f16)); |
| 87 | alpha_ab.val[1] = vaddq_f16(alpha_ab.val[1], vmulq_f16(c.val[1], beta_f16)); |
| 88 | |
Pablo Tello | 221f381 | 2017-06-28 17:27:56 +0100 | [diff] [blame] | 89 | vst2q_f16(out_ptr + 0, alpha_ab); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | }, |
| 91 | in, out); |
| 92 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 93 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | } // namespace |
| 96 | |
| 97 | NEGEMMMatrixAdditionKernel::NEGEMMMatrixAdditionKernel() |
| 98 | : INESimpleKernel(), _func(nullptr), _beta(0.0f) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | void NEGEMMMatrixAdditionKernel::configure(const ITensor *input, ITensor *output, float beta) |
| 103 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame^] | 104 | ARM_COMPUTE_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 105 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 106 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 107 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) != output->info()->dimension(0)); |
| 109 | ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) != output->info()->dimension(1)); |
| 110 | |
| 111 | switch(input->info()->data_type()) |
| 112 | { |
| 113 | case DataType::F32: |
| 114 | _func = &matrix_addition_f32; |
| 115 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | case DataType::F16: |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 117 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | _func = &matrix_addition_f16; |
| 119 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 120 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | default: |
| 122 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 127 | |
| 128 | INESimpleKernel::configure(input, output, num_elems_processed_per_iteration); |
| 129 | |
| 130 | _beta = beta; |
| 131 | } |
| 132 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 133 | void NEGEMMMatrixAdditionKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 134 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 135 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 136 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 137 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INESimpleKernel::window(), window); |
| 138 | |
| 139 | if(_beta != 0.0f) |
| 140 | { |
| 141 | (*_func)(_input, _output, window, _beta); |
| 142 | } |
| 143 | } |