blob: d9029478a1d02b73e2f7fee6eed1b41efffecb11 [file] [log] [blame]
Gian Marco05288a22017-11-21 10:57:50 +00001/*
SiCong Libbd8fac2021-02-04 13:12:19 +00002 * Copyright (c) 2017-2021 Arm Limited.
Gian Marco05288a22017-11-21 10:57:50 +00003 *
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 Pinitasf4e84fb2021-07-08 15:36:07 +010026#include "arm_compute/core/CL/CLKernelLibrary.h"
Gian Marco05288a22017-11-21 10:57:50 +000027#include "arm_compute/core/CL/ICLTensor.h"
28#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
Michele Di Giorgiof64d3362020-04-03 12:40:10 +010030#include "arm_compute/core/KernelDescriptors.h"
SiCong Libbd8fac2021-02-04 13:12:19 +000031#include "arm_compute/core/Log.h"
Gian Marco05288a22017-11-21 10:57:50 +000032#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Types.h"
34#include "arm_compute/core/Validate.h"
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000035#include "arm_compute/core/utils/quantization/AsymmHelpers.h"
Gian Marco05288a22017-11-21 10:57:50 +000036#include "arm_compute/runtime/CL/CLScheduler.h"
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010037#include "arm_compute/runtime/IMemoryManager.h"
38#include "src/core/helpers/MemoryHelpers.h"
39
Georgios Pinitas7891a732021-08-20 21:39:25 +010040#include "src/gpu/cl/operators/ClGemmLowpMatrixMultiplyCore.h"
Gian Marco05288a22017-11-21 10:57:50 +000041
giuros011c9efeb2019-01-11 14:04:43 +000042namespace arm_compute
43{
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010044using namespace arm_compute::experimental;
45using OperatorType = opencl::ClGemmLowpMatrixMultiplyCore;
Gian Marco05288a22017-11-21 10:57:50 +000046
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010047struct CLGEMMLowpMatrixMultiplyCore::Impl
Gian Marco19835e52018-01-30 13:35:54 +000048{
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010049 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 Marco19835e52018-01-30 13:35:54 +000057
Gian Marco05288a22017-11-21 10:57:50 +000058CLGEMMLowpMatrixMultiplyCore::CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010059 : _impl(std::make_unique<Impl>())
Gian Marco05288a22017-11-21 10:57:50 +000060{
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010061 _impl->memory_group = MemoryGroup(memory_manager);
Gian Marco05288a22017-11-21 10:57:50 +000062}
63
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010064CLGEMMLowpMatrixMultiplyCore::~CLGEMMLowpMatrixMultiplyCore() = default;
65
Gian Marco Iodice4b908652018-10-18 10:21:02 +010066void CLGEMMLowpMatrixMultiplyCore::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info)
Gian Marco05288a22017-11-21 10:57:50 +000067{
Manuel Bottini2b84be52020-04-08 10:15:51 +010068 configure(CLKernelLibrary::get().get_compile_context(), a, b, c, output, gemm_info);
69}
70
71void CLGEMMLowpMatrixMultiplyCore::configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info)
72{
Georgios Pinitas358ca202017-12-07 16:47:52 +000073 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
Gian Marco05288a22017-11-21 10:57:50 +000074
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010075 _impl->b = b;
76 _impl->op = std::make_unique<OperatorType>();
77 _impl->is_prepared = gemm_info.retain_internal_weights();
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000078
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010079 _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 Marco05288a22017-11-21 10:57:50 +000081
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010082 // Manage/allocate auxilairy tensors
83 if(_impl->is_prepared)
Vidhya Sudhan Loganathan951b8a42019-11-04 14:42:08 +000084 {
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010085 _impl->run_pack.add_const_tensor(ACL_SRC_0, a);
86 _impl->run_pack.add_tensor(ACL_DST, output);
Gian Marco Iodice4b908652018-10-18 10:21:02 +010087 }
88 else
89 {
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010090 _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 Marco05288a22017-11-21 10:57:50 +000092 }
93}
94
Gian Marco Iodice4b908652018-10-18 10:21:02 +010095Status CLGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info)
Georgios Pinitas358ca202017-12-07 16:47:52 +000096{
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +010097 return OperatorType::validate(a, b, c, output, gemm_info);
Georgios Pinitas358ca202017-12-07 16:47:52 +000098}
99
Gian Marco05288a22017-11-21 10:57:50 +0000100void CLGEMMLowpMatrixMultiplyCore::run()
101{
Georgios Pinitas72219332018-06-05 14:56:06 +0100102 prepare();
103
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +0100104 MemoryGroupResourceScope scope_mg(_impl->memory_group);
Gian Marco05288a22017-11-21 10:57:50 +0000105
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +0100106 _impl->op->run(_impl->run_pack);
Georgios Pinitas72219332018-06-05 14:56:06 +0100107}
Chunosov5124be52017-11-22 20:42:13 +0700108
Georgios Pinitas72219332018-06-05 14:56:06 +0100109void CLGEMMLowpMatrixMultiplyCore::prepare()
110{
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +0100111 if(!_impl->is_prepared)
Georgios Pinitas72219332018-06-05 14:56:06 +0100112 {
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +0100113 _impl->op->prepare(_impl->run_pack);
114
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +0100115 // Release temporary tensors that are only used in prepare stage
Georgios Pinitas98055832021-07-27 10:34:59 +0100116 release_temporaries(_impl->aux_mem_req, _impl->workspace_tensors);
Georgios Pinitas72219332018-06-05 14:56:06 +0100117
Georgios Pinitasf4e84fb2021-07-08 15:36:07 +0100118 _impl->is_prepared = true;
Georgios Pinitas72219332018-06-05 14:56:06 +0100119 }
Gian Marco05288a22017-11-21 10:57:50 +0000120}
Sheri Zhang0cdbda52020-02-25 15:57:21 +0000121} // namespace arm_compute