Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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/NEGEMMMatrixAccumulateBiasesKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 31 | #include "arm_compute/core/Types.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | #include <arm_neon.h> |
| 36 | #include <cstddef> |
| 37 | #include <cstdint> |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
| 41 | NEGEMMMatrixAccumulateBiasesKernel::NEGEMMMatrixAccumulateBiasesKernel() |
| 42 | : _accum(nullptr), _biases(nullptr) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void NEGEMMMatrixAccumulateBiasesKernel::configure(ITensor *accum, const ITensor *biases) |
| 47 | { |
Pablo Tello | dcdc85e | 2017-06-28 10:05:29 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum); |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(biases, accum); |
Moritz Pflanzer | 484e7b3 | 2017-08-09 11:43:18 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_ERROR_ON(biases->info()->num_dimensions() > 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 52 | |
| 53 | _biases = biases; |
| 54 | _accum = accum; |
| 55 | |
| 56 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 57 | |
| 58 | // Configure kernel window |
| 59 | Window win = calculate_max_window(*accum->info(), Steps(num_elems_processed_per_iteration)); |
| 60 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | update_window_and_padding(win, |
| 62 | AccessWindowHorizontal(accum->info(), 0, num_elems_processed_per_iteration), |
Moritz Pflanzer | d625360 | 2017-08-07 16:49:49 +0100 | [diff] [blame] | 63 | AccessWindowStatic(biases->info(), 0, 0, win.x().end(), biases->info()->tensor_shape().y())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 64 | |
| 65 | AccessWindowHorizontal output_access(accum->info(), 0, num_elems_processed_per_iteration); |
| 66 | |
| 67 | // Set the valid region for the accum tensor |
| 68 | Coordinates coord; |
| 69 | coord.set_num_dimensions(accum->info()->num_dimensions()); |
| 70 | output_access.set_valid_region(win, ValidRegion(coord, accum->info()->tensor_shape())); |
| 71 | |
| 72 | INEKernel::configure(win); |
| 73 | } |
| 74 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 75 | void NEGEMMMatrixAccumulateBiasesKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 77 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 79 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 80 | |
| 81 | Window win_biases; |
| 82 | win_biases.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().step())); |
| 83 | win_biases.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 84 | |
| 85 | Iterator in0_out(_accum, window); |
| 86 | Iterator in1(_biases, win_biases); |
| 87 | |
| 88 | switch(_accum->info()->data_type()) |
| 89 | { |
| 90 | case DataType::F32: |
| 91 | { |
| 92 | execute_window_loop(window, [&](const Coordinates & id) |
| 93 | { |
| 94 | const float32x4x4_t accum = vld4q_f32(reinterpret_cast<const float *>(in0_out.ptr())); |
| 95 | const float32x4x4_t biases = vld4q_f32(reinterpret_cast<const float *>(in1.ptr())); |
| 96 | const float32x4x4_t res = |
| 97 | { |
| 98 | { |
| 99 | vaddq_f32(accum.val[0], biases.val[0]), |
| 100 | vaddq_f32(accum.val[1], biases.val[1]), |
| 101 | vaddq_f32(accum.val[2], biases.val[2]), |
| 102 | vaddq_f32(accum.val[3], biases.val[3]) |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | vst4q_f32(reinterpret_cast<float *>(in0_out.ptr()), res); |
| 107 | }, |
| 108 | in0_out, in1); |
| 109 | break; |
| 110 | } |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 111 | #ifdef ARM_COMPUTE_AARCH64_V8_2 |
Pablo Tello | dcdc85e | 2017-06-28 10:05:29 +0100 | [diff] [blame] | 112 | case DataType::F16: |
| 113 | { |
| 114 | execute_window_loop(window, [&](const Coordinates & id) |
| 115 | { |
| 116 | const float16x8x2_t accum = vld2q_f16(reinterpret_cast<const float16_t *>(in0_out.ptr())); |
| 117 | const float16x8x2_t biases = vld2q_f16(reinterpret_cast<const float16_t *>(in1.ptr())); |
| 118 | const float16x8x2_t res = |
| 119 | { |
| 120 | { |
| 121 | vaddq_f16(accum.val[0], biases.val[0]), |
| 122 | vaddq_f16(accum.val[1], biases.val[1]) |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | vst2q_f16(reinterpret_cast<float16_t *>(in0_out.ptr()), res); |
| 127 | }, |
| 128 | in0_out, in1); |
| 129 | break; |
| 130 | } |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 131 | #endif /* ARM_COMPUTE_AARCH64_V8_2 */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 132 | case DataType::QS8: |
| 133 | { |
| 134 | execute_window_loop(window, [&](const Coordinates & id) |
| 135 | { |
| 136 | const qint8x16_t accum = vld1q_qs8(reinterpret_cast<const qint8_t *>(in0_out.ptr())); |
| 137 | const qint8x16_t biases = vld1q_qs8(reinterpret_cast<const qint8_t *>(in1.ptr())); |
| 138 | |
| 139 | vst1q_qs8(reinterpret_cast<qint8_t *>(in0_out.ptr()), vqaddq_qs8(accum, biases)); |
| 140 | }, |
| 141 | in0_out, in1); |
| 142 | break; |
| 143 | } |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 144 | case DataType::QS16: |
| 145 | { |
| 146 | execute_window_loop(window, [&](const Coordinates & id) |
| 147 | { |
| 148 | qint16x8x2_t accum = vld2q_s16(reinterpret_cast<const qint16_t *>(in0_out.ptr())); |
| 149 | const qint16x8x2_t biases = vld2q_s16(reinterpret_cast<const qint16_t *>(in1.ptr())); |
| 150 | |
| 151 | accum.val[0] = vqaddq_qs16(accum.val[0], biases.val[0]); |
| 152 | accum.val[1] = vqaddq_qs16(accum.val[1], biases.val[1]); |
| 153 | |
| 154 | vst2q_s16(reinterpret_cast<qint16_t *>(in0_out.ptr()), accum); |
| 155 | }, |
| 156 | in0_out, in1); |
| 157 | break; |
| 158 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | default: |
| 160 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 161 | break; |
| 162 | } |
| 163 | } |