Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 1 | /* |
SiCong Li | bbd8fac | 2021-02-04 13:12:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [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/runtime/CL/functions/CLGEMMLowpMatrixMultiplyCore.h" |
| 25 | |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLTensor.h" |
| 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
Michele Di Giorgio | f64d336 | 2020-04-03 12:40:10 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/KernelDescriptors.h" |
SiCong Li | bbd8fac | 2021-02-04 13:12:19 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/Log.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/core/Validate.h" |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 35 | #include "arm_compute/core/utils/quantization/AsymmHelpers.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 36 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 37 | #include "arm_compute/runtime/IMemoryManager.h" |
| 38 | #include "src/core/helpers/MemoryHelpers.h" |
| 39 | |
| 40 | #include "src/runtime/gpu/cl/operators/ClGemmLowpMatrixMultiplyCore.h" |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 41 | |
giuros01 | 1c9efeb | 2019-01-11 14:04:43 +0000 | [diff] [blame] | 42 | namespace arm_compute |
| 43 | { |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 44 | using namespace arm_compute::experimental; |
| 45 | using OperatorType = opencl::ClGemmLowpMatrixMultiplyCore; |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 46 | |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 47 | struct CLGEMMLowpMatrixMultiplyCore::Impl |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 48 | { |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 49 | const ICLTensor *b{ nullptr }; |
| 50 | std::unique_ptr<OperatorType> op{ nullptr }; |
| 51 | MemoryGroup memory_group{}; |
| 52 | ITensorPack run_pack{}; |
| 53 | MemoryRequirements aux_mem_req{}; |
| 54 | WorkspaceData<CLTensor> workspace_tensors{}; |
| 55 | bool is_prepared{ false }; |
| 56 | }; |
Gian Marco | 19835e5 | 2018-01-30 13:35:54 +0000 | [diff] [blame] | 57 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 58 | CLGEMMLowpMatrixMultiplyCore::CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager) |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 59 | : _impl(std::make_unique<Impl>()) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 60 | { |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 61 | _impl->memory_group = MemoryGroup(memory_manager); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 64 | CLGEMMLowpMatrixMultiplyCore::~CLGEMMLowpMatrixMultiplyCore() = default; |
| 65 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 66 | void CLGEMMLowpMatrixMultiplyCore::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info) |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 67 | { |
Manuel Bottini | 2b84be5 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 68 | configure(CLKernelLibrary::get().get_compile_context(), a, b, c, output, gemm_info); |
| 69 | } |
| 70 | |
| 71 | void CLGEMMLowpMatrixMultiplyCore::configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info) |
| 72 | { |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 74 | |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 75 | _impl->b = b; |
| 76 | _impl->op = std::make_unique<OperatorType>(); |
| 77 | _impl->is_prepared = gemm_info.retain_internal_weights(); |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 78 | |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 79 | _impl->op->configure(compile_context, a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), gemm_info); |
| 80 | _impl->aux_mem_req = _impl->op->workspace(); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 81 | |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 82 | // Manage/allocate auxilairy tensors |
| 83 | if(_impl->is_prepared) |
Vidhya Sudhan Loganathan | 951b8a4 | 2019-11-04 14:42:08 +0000 | [diff] [blame] | 84 | { |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 85 | _impl->run_pack.add_const_tensor(ACL_SRC_0, a); |
| 86 | _impl->run_pack.add_tensor(ACL_DST, output); |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | { |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 90 | _impl->run_pack = { { ACL_SRC_0, a }, { ACL_SRC_1, _impl->b }, { ACL_SRC_2, c }, { ACL_DST, output } }; |
| 91 | _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack, _impl->run_pack); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 95 | Status CLGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info) |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 96 | { |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 97 | return OperatorType::validate(a, b, c, output, gemm_info); |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 100 | void CLGEMMLowpMatrixMultiplyCore::run() |
| 101 | { |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 102 | prepare(); |
| 103 | |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 104 | MemoryGroupResourceScope scope_mg(_impl->memory_group); |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 105 | |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 106 | _impl->op->run(_impl->run_pack); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 107 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 108 | |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 109 | void CLGEMMLowpMatrixMultiplyCore::prepare() |
| 110 | { |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 111 | if(!_impl->is_prepared) |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 112 | { |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 113 | _impl->op->prepare(_impl->run_pack); |
| 114 | |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 115 | // Release temporary tensors that are only used in prepare stage |
Georgios Pinitas | 9805583 | 2021-07-27 10:34:59 +0100 | [diff] [blame] | 116 | release_temporaries(_impl->aux_mem_req, _impl->workspace_tensors); |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 117 | |
Georgios Pinitas | f4e84fb | 2021-07-08 15:36:07 +0100 | [diff] [blame] | 118 | _impl->is_prepared = true; |
Georgios Pinitas | 7221933 | 2018-06-05 14:56:06 +0100 | [diff] [blame] | 119 | } |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 120 | } |
Sheri Zhang | 0cdbda5 | 2020-02-25 15:57:21 +0000 | [diff] [blame] | 121 | } // namespace arm_compute |