Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-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/NEGEMMMatrixAccumulateBiasesKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CPP/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 32 | #include "arm_compute/core/Types.h" |
Gian Marco | 900b78f | 2017-11-20 15:01:12 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | |
| 37 | #include <arm_neon.h> |
| 38 | #include <cstddef> |
| 39 | #include <cstdint> |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 43 | namespace |
| 44 | { |
| 45 | inline Status validate_arguments(const ITensorInfo *accum, const ITensorInfo *biases) |
| 46 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(accum); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(accum, 1, DataType::F16, DataType::F32); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(biases, accum); |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON(biases->num_dimensions() > 1); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(biases->dimension(0) != accum->dimension(0)); |
| 52 | |
| 53 | return Status{}; |
| 54 | } |
| 55 | |
| 56 | inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *accum, ITensorInfo *biases) |
| 57 | { |
| 58 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 59 | |
| 60 | // Configure kernel window |
| 61 | Window win = calculate_max_window(*accum, Steps(num_elems_processed_per_iteration)); |
| 62 | |
| 63 | bool window_changed = update_window_and_padding(win, |
| 64 | AccessWindowHorizontal(accum, 0, num_elems_processed_per_iteration), |
| 65 | AccessWindowStatic(biases, 0, 0, ceil_to_multiple(biases->dimension(0), num_elems_processed_per_iteration), biases->tensor_shape().y())); |
| 66 | |
| 67 | AccessWindowHorizontal output_access(accum, 0, num_elems_processed_per_iteration); |
| 68 | |
| 69 | // Set the valid region for the accum tensor |
| 70 | Coordinates coord; |
| 71 | coord.set_num_dimensions(accum->num_dimensions()); |
| 72 | output_access.set_valid_region(win, ValidRegion(coord, accum->tensor_shape())); |
| 73 | |
| 74 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 75 | return std::make_pair(err, win); |
| 76 | } |
| 77 | } // namespace |
| 78 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | NEGEMMMatrixAccumulateBiasesKernel::NEGEMMMatrixAccumulateBiasesKernel() |
| 80 | : _accum(nullptr), _biases(nullptr) |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | void NEGEMMMatrixAccumulateBiasesKernel::configure(ITensor *accum, const ITensor *biases) |
| 85 | { |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 86 | ARM_COMPUTE_ERROR_ON_NULLPTR(accum, biases); |
| 87 | |
| 88 | // Perform validate step |
| 89 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(accum->info(), biases->info())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 90 | |
| 91 | _biases = biases; |
| 92 | _accum = accum; |
| 93 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 94 | // Configure kernel window |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 95 | auto win_config = validate_and_configure_window(accum->info(), biases->info()); |
| 96 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 97 | INEKernel::configure(win_config.second); |
| 98 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 99 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 100 | Status NEGEMMMatrixAccumulateBiasesKernel::validate(const ITensorInfo *accum, const ITensorInfo *biases) |
| 101 | { |
| 102 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(accum, biases)); |
| 103 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(accum->clone().get(), biases->clone().get()).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | |
Ioan-Cristian Szabo | b4e3e1c | 2017-11-30 17:17:17 +0000 | [diff] [blame] | 105 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 108 | void NEGEMMMatrixAccumulateBiasesKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 110 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 112 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 113 | |
| 114 | Window win_biases; |
| 115 | win_biases.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().step())); |
| 116 | win_biases.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 117 | |
| 118 | Iterator in0_out(_accum, window); |
| 119 | Iterator in1(_biases, win_biases); |
| 120 | |
| 121 | switch(_accum->info()->data_type()) |
| 122 | { |
| 123 | case DataType::F32: |
| 124 | { |
| 125 | execute_window_loop(window, [&](const Coordinates & id) |
| 126 | { |
| 127 | const float32x4x4_t accum = vld4q_f32(reinterpret_cast<const float *>(in0_out.ptr())); |
| 128 | const float32x4x4_t biases = vld4q_f32(reinterpret_cast<const float *>(in1.ptr())); |
| 129 | const float32x4x4_t res = |
| 130 | { |
| 131 | { |
| 132 | vaddq_f32(accum.val[0], biases.val[0]), |
| 133 | vaddq_f32(accum.val[1], biases.val[1]), |
| 134 | vaddq_f32(accum.val[2], biases.val[2]), |
| 135 | vaddq_f32(accum.val[3], biases.val[3]) |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | vst4q_f32(reinterpret_cast<float *>(in0_out.ptr()), res); |
| 140 | }, |
| 141 | in0_out, in1); |
| 142 | break; |
| 143 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 144 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | dcdc85e | 2017-06-28 10:05:29 +0100 | [diff] [blame] | 145 | case DataType::F16: |
| 146 | { |
| 147 | execute_window_loop(window, [&](const Coordinates & id) |
| 148 | { |
| 149 | const float16x8x2_t accum = vld2q_f16(reinterpret_cast<const float16_t *>(in0_out.ptr())); |
| 150 | const float16x8x2_t biases = vld2q_f16(reinterpret_cast<const float16_t *>(in1.ptr())); |
| 151 | const float16x8x2_t res = |
| 152 | { |
| 153 | { |
| 154 | vaddq_f16(accum.val[0], biases.val[0]), |
| 155 | vaddq_f16(accum.val[1], biases.val[1]) |
| 156 | } |
| 157 | }; |
| 158 | |
| 159 | vst2q_f16(reinterpret_cast<float16_t *>(in0_out.ptr()), res); |
| 160 | }, |
| 161 | in0_out, in1); |
| 162 | break; |
| 163 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 164 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | default: |
| 166 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 167 | break; |
| 168 | } |
| 169 | } |