blob: 1bc785a0a75829d7398b9b7fa50d6ca69d6ddabe [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
SiCong Libd8b1e22021-02-04 13:07:09 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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/CLGEMM.h"
25
Georgios Pinitas856f66e2021-04-22 21:13:21 +010026#include "arm_compute/core/CL/CLHelpers.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010027#include "arm_compute/core/CL/CLKernelLibrary.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Helpers.h"
Gian Marco Iodice7026b302019-06-26 17:18:11 +010029#include "arm_compute/core/KernelDescriptors.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010030#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
Gian Marco Iodicebb36a8e2018-04-19 12:05:08 +010032#include "arm_compute/core/Utils.h"
Georgios Pinitas856f66e2021-04-22 21:13:21 +010033#include "arm_compute/runtime/CL/functions/CLGEMM.h"
34#include "src/core/helpers/MemoryHelpers.h"
35#include "src/runtime/gpu/cl/operators/ClGemm.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036
giuros011c9efeb2019-01-11 14:04:43 +000037namespace arm_compute
38{
Georgios Pinitas856f66e2021-04-22 21:13:21 +010039using namespace arm_compute::experimental;
40using OperatorType = opencl::ClGemm;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010041
Georgios Pinitas856f66e2021-04-22 21:13:21 +010042struct CLGEMM::Impl
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010043{
Georgios Pinitas856f66e2021-04-22 21:13:21 +010044 const ICLTensor *a{ nullptr };
45 const ICLTensor *b{ nullptr };
46 const ICLTensor *c{ nullptr };
47 ICLTensor *dst{ nullptr };
48 std::unique_ptr<OperatorType> op{ nullptr };
49 MemoryGroup memory_group{};
50 IWeightsManager *weights_manager{ nullptr };
51 CLTensor weights_transformed{};
52 ITensorPack run_pack{};
53 ITensorPack prep_pack{};
54 MemoryRequirements aux_mem_req{};
55 WorkspaceData<CLTensor> workspace_tensors{};
56 bool _is_prepared{ false };
57};
SiCong Libd8b1e22021-02-04 13:07:09 +000058
Michalis Spyroub27e13a2019-09-27 11:04:27 +010059CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Georgios Pinitas856f66e2021-04-22 21:13:21 +010060 : _impl(std::make_unique<Impl>())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010061{
Georgios Pinitas856f66e2021-04-22 21:13:21 +010062 _impl->memory_group = MemoryGroup(memory_manager);
63 _impl->weights_manager = weights_manager;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010064}
65
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010066CLGEMM::~CLGEMM() = default;
67
Gian Marco Iodice926afe12019-03-19 11:44:13 +000068void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
69{
Manuel Bottini2b84be52020-04-08 10:15:51 +010070 configure(CLKernelLibrary::get().get_compile_context(), a, b, c, output, alpha, beta, gemm_info);
71}
72
73void CLGEMM::configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
74{
Gian Marco Iodice926afe12019-03-19 11:44:13 +000075 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
76
Georgios Pinitas856f66e2021-04-22 21:13:21 +010077 _impl->a = a;
78 _impl->b = b;
79 _impl->c = c;
80 _impl->dst = output;
81 _impl->op = std::make_unique<OperatorType>();
Gian Marco Iodice926afe12019-03-19 11:44:13 +000082
Georgios Pinitas856f66e2021-04-22 21:13:21 +010083 _impl->op->configure(compile_context, a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info);
84 _impl->aux_mem_req = _impl->op->workspace();
Gian Marco Iodice926afe12019-03-19 11:44:13 +000085
Georgios Pinitas856f66e2021-04-22 21:13:21 +010086 // Manage/allocate auxilairy tensors
87 _impl->run_pack = { { ACL_SRC_0, _impl->a }, { ACL_SRC_2, _impl->c }, { ACL_DST, _impl->dst } };
88 _impl->prep_pack = { { ACL_SRC_1, _impl->b } };
89 _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack, _impl->prep_pack);
Gian Marco Iodice926afe12019-03-19 11:44:13 +000090}
91
92Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
93{
Georgios Pinitas856f66e2021-04-22 21:13:21 +010094 return OperatorType::validate(a, b, c, output, alpha, beta, gemm_info);
Gian Marco Iodice926afe12019-03-19 11:44:13 +000095}
96
Anthony Barbier6ff3b192017-09-04 18:44:23 +010097void CLGEMM::run()
98{
Georgios Pinitase0437672018-05-02 14:07:55 +010099 prepare();
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100100
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100101 MemoryGroupResourceScope scope_mg(_impl->memory_group);
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000102
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100103 ARM_COMPUTE_ERROR_ON_NULLPTR(_impl->a, _impl->b, _impl->dst);
104 _impl->op->run(_impl->run_pack);
Georgios Pinitase0437672018-05-02 14:07:55 +0100105}
Georgios Pinitas82b51482018-04-24 15:14:12 +0100106
Georgios Pinitase0437672018-05-02 14:07:55 +0100107void CLGEMM::prepare()
108{
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100109 if(!_impl->_is_prepared)
Georgios Pinitase0437672018-05-02 14:07:55 +0100110 {
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100111 _impl->op->prepare(_impl->prep_pack);
112
113 auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
114 _impl->aux_mem_req.end(),
115 [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
116
117 if(has_reshape != std::end(_impl->aux_mem_req))
Georgios Pinitase0437672018-05-02 14:07:55 +0100118 {
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100119 _impl->b->mark_as_unused();
Georgios Pinitase0437672018-05-02 14:07:55 +0100120 }
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100121 else
122 {
123 // Pack the B matrix to be used as the underlying GEMM performs no reshapes
124 _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->b);
125 }
126 _impl->_is_prepared = true;
Georgios Pinitase0437672018-05-02 14:07:55 +0100127 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100128}
giuros011c9efeb2019-01-11 14:04:43 +0000129} // namespace arm_compute