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/runtime/NEON/functions/NEGEMMLowp.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/NEON/kernels/arm64/NEGEMMLowpAArch64V8P4Kernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Types.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 34 | #include "arm_compute/runtime/TensorAllocator.h" |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 35 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 39 | #define NEGEMMLOWP_VALIDATE_DIMENSIONS(a, b, output) \ |
| 40 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN((a), 1, DataType::U8); \ |
| 41 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN((b), 1, DataType::U8); \ |
| 42 | ARM_COMPUTE_ERROR_ON_MSG((a)->info()->dimension(0) != (b)->info()->dimension(1), "The product AB is defined only if the number of columns in A is equal to the number of rows in B"); \ |
| 43 | ARM_COMPUTE_ERROR_ON_MSG((a)->info()->dimension(1) != (output)->info()->dimension(1), "The C matrix must have the same number of rows as the matrix A"); \ |
| 44 | ARM_COMPUTE_ERROR_ON_MSG((b)->info()->dimension(0) != (output)->info()->dimension(0), "The C matrix must have the same number of columns as the matrix C"); |
| 45 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 46 | NEGEMMLowp::NEGEMMLowp(std::shared_ptr<IMemoryManager> memory_manager) |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 47 | : _memory_group(std::move(memory_manager)), _interleave_kernel(), _transpose_kernel(), _mm_kernel(), _mm_optimised_kernel(nullptr), _interleave_blocked(), _interleave_blocked_transposed(), _tmp_a(), |
| 48 | _tmp_b() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | { |
| 50 | } |
| 51 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 52 | void NEGEMMLowp::configure(const ITensor *a, const ITensor *b, ITensor *output) |
| 53 | { |
| 54 | NEGEMMLOWP_VALIDATE_DIMENSIONS(a, b, output); |
| 55 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32); |
| 56 | |
| 57 | const struct CPUInfo ci = NEScheduler::get().cpu_info(); |
| 58 | const int cpu_has_dotprod = static_cast<int>(ci.CPU) & static_cast<int>(CPUTarget::DOT); |
| 59 | if(cpu_has_dotprod != 0) |
| 60 | { |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 61 | #ifdef ARM_COMPUTE_AARCH64_V8_2 |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 62 | // NEGEMMLowpAArch64V8P4Kernel only compiled in AArch64 targets |
| 63 | _mm_optimised_kernel = support::cpp14::make_unique<NEGEMMLowpAArch64V8P4Kernel>(); |
| 64 | TensorShape shape_a_int = a->info()->tensor_shape(); |
| 65 | shape_a_int.set(0, a->info()->dimension(0) * 8.f); |
| 66 | shape_a_int.set(1, std::ceil(a->info()->dimension(1) / 8.f)); |
| 67 | |
| 68 | TensorShape shape_b_int = b->info()->tensor_shape(); |
| 69 | shape_b_int.set(0, b->info()->dimension(0) * 12.f); |
| 70 | shape_b_int.set(1, std::ceil(b->info()->dimension(1) / 12.f)); |
| 71 | |
| 72 | TensorInfo info_a_int(shape_a_int, 1, a->info()->data_type()); |
| 73 | TensorInfo info_b_int(shape_b_int, 1, b->info()->data_type()); |
| 74 | _tmp_a.allocator()->init(info_a_int); |
| 75 | _tmp_b.allocator()->init(info_b_int); |
| 76 | |
| 77 | _memory_group.manage(&_tmp_a); |
| 78 | _memory_group.manage(&_tmp_b); |
| 79 | |
| 80 | _interleave_blocked.configure(a, &_tmp_a, 8, 4, false); |
| 81 | _interleave_blocked_transposed.configure(b, &_tmp_b, 12, 4, true); |
| 82 | _mm_optimised_kernel->configure(&_tmp_a, &_tmp_b, output); |
| 83 | |
| 84 | _tmp_a.allocator()->allocate(); |
| 85 | _tmp_b.allocator()->allocate(); |
Ioan-Cristian Szabo | 33fd07b | 2017-10-26 15:42:24 +0100 | [diff] [blame] | 86 | #endif /* ARM_COMPUTE_AARCH64_V8_2 */ |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | { |
| 90 | ARM_COMPUTE_ERROR("Not implemented"); |
Anthony Barbier | 960ccc4 | 2017-10-19 13:26:42 +0100 | [diff] [blame] | 91 | //FIXME: This is in the process of being updated, for more info please refer to COMPMID-624. |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | void NEGEMMLowp::run() |
| 96 | { |
| 97 | _memory_group.acquire(); |
| 98 | |
| 99 | if(_mm_optimised_kernel != nullptr) |
| 100 | { |
| 101 | NEScheduler::get().schedule(&_interleave_blocked, Window::DimY); |
| 102 | NEScheduler::get().schedule(&_interleave_blocked_transposed, Window::DimY); |
| 103 | NEScheduler::get().schedule(_mm_optimised_kernel.get(), Window::DimY); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | /* Run interleave kernel */ |
| 108 | NEScheduler::get().schedule(&_interleave_kernel, Window::DimY); |
| 109 | /* Run transpose kernel */ |
| 110 | NEScheduler::get().schedule(&_transpose_kernel, Window::DimY); |
| 111 | /* Run matrix multiply kernel */ |
| 112 | NEScheduler::get().schedule(&_mm_kernel, Window::DimY); |
| 113 | } |
| 114 | |
| 115 | _memory_group.release(); |
| 116 | } |
| 117 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 118 | void NEGEMMLowp::configure(const ITensor *a, const ITensor *b, ITensor *output, int32_t a_offset, int32_t b_offset, int32_t output_offset, int32_t output_mult_int, int32_t shift) |
| 119 | { |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 120 | NEGEMMLOWP_VALIDATE_DIMENSIONS(a, b, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(a, b, output); |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 122 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 123 | |
| 124 | /* The interleaved output matrix will have the following shape: [ a_height * 4, ceil(a_width / 4.0f) ] */ |
| 125 | TensorShape shape_tmp_a = a->info()->tensor_shape(); |
| 126 | shape_tmp_a.set(0, a->info()->dimension(0) * 4); |
| 127 | shape_tmp_a.set(1, std::ceil(a->info()->dimension(1) / 4.f)); |
| 128 | |
| 129 | TensorShape shape_tmp_b = b->info()->tensor_shape(); |
| 130 | shape_tmp_b.set(0, b->info()->dimension(1) * 16); |
| 131 | shape_tmp_b.set(1, std::ceil(b->info()->dimension(0) / 16.f)); |
| 132 | |
| 133 | TensorInfo info_a(shape_tmp_a, 1, a->info()->data_type()); |
| 134 | TensorInfo info_b(shape_tmp_b, 1, b->info()->data_type()); |
| 135 | _tmp_a.allocator()->init(info_a); |
| 136 | _tmp_b.allocator()->init(info_b); |
| 137 | |
Georgios Pinitas | 658039b | 2017-09-15 16:30:50 +0100 | [diff] [blame] | 138 | // Manage intermediate buffers |
| 139 | _memory_group.manage(&_tmp_a); |
| 140 | _memory_group.manage(&_tmp_b); |
| 141 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 142 | _interleave_kernel.configure(a, &_tmp_a); |
| 143 | _transpose_kernel.configure(b, &_tmp_b); |
| 144 | _mm_kernel.configure(&_tmp_a, &_tmp_b, output, a_offset, b_offset, output_offset, output_mult_int, shift); |
| 145 | |
| 146 | _tmp_a.allocator()->allocate(); |
| 147 | _tmp_b.allocator()->allocate(); |
| 148 | } |
| 149 | |
Pablo Tello | bf2fb95 | 2017-09-29 16:43:25 +0100 | [diff] [blame] | 150 | #undef NEGEMMLOWP_VALIDATE_DIMENSIONS |