blob: 871a1d6e2790fff70ff68488e8bed65b81a511cc [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gunes Bayir4bfc70e2021-12-10 16:17:56 +00002 * Copyright (c) 2017-2022 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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
Georgios Pinitas856f66e2021-04-22 21:13:21 +010034#include "src/core/helpers/MemoryHelpers.h"
Georgios Pinitas7891a732021-08-20 21:39:25 +010035#include "src/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{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044 const ICLTensor *b{nullptr};
45 std::unique_ptr<OperatorType> op{nullptr};
Georgios Pinitas856f66e2021-04-22 21:13:21 +010046 MemoryGroup memory_group{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010047 IWeightsManager *weights_manager{nullptr};
Georgios Pinitas856f66e2021-04-22 21:13:21 +010048 ITensorPack run_pack{};
49 ITensorPack prep_pack{};
50 MemoryRequirements aux_mem_req{};
51 WorkspaceData<CLTensor> workspace_tensors{};
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 bool is_prepared{false};
Georgios Pinitas856f66e2021-04-22 21:13:21 +010053};
SiCong Libd8b1e22021-02-04 13:07:09 +000054
Michalis Spyroub27e13a2019-09-27 11:04:27 +010055CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
Georgios Pinitas856f66e2021-04-22 21:13:21 +010056 : _impl(std::make_unique<Impl>())
Anthony Barbier6ff3b192017-09-04 18:44:23 +010057{
Georgios Pinitas856f66e2021-04-22 21:13:21 +010058 _impl->memory_group = MemoryGroup(memory_manager);
59 _impl->weights_manager = weights_manager;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010060}
61
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010062CLGEMM::~CLGEMM() = default;
63
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010064void CLGEMM::configure(const ICLTensor *a,
65 const ICLTensor *b,
66 const ICLTensor *c,
67 ICLTensor *output,
68 float alpha,
69 float beta,
70 const GEMMInfo &gemm_info)
Gian Marco Iodice926afe12019-03-19 11:44:13 +000071{
Manuel Bottini2b84be52020-04-08 10:15:51 +010072 configure(CLKernelLibrary::get().get_compile_context(), a, b, c, output, alpha, beta, gemm_info);
73}
74
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075void CLGEMM::configure(const CLCompileContext &compile_context,
76 const ICLTensor *a,
77 const ICLTensor *b,
78 const ICLTensor *c,
79 ICLTensor *output,
80 float alpha,
81 float beta,
82 const GEMMInfo &gemm_info)
Manuel Bottini2b84be52020-04-08 10:15:51 +010083{
Gian Marco Iodice926afe12019-03-19 11:44:13 +000084 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
85
Georgios Pinitas5a643322021-06-04 16:48:30 +010086 _impl->b = b;
Georgios Pinitas5a643322021-06-04 16:48:30 +010087 _impl->op = std::make_unique<OperatorType>();
88 _impl->is_prepared = gemm_info.retain_internal_weights();
Gian Marco Iodice926afe12019-03-19 11:44:13 +000089
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090 _impl->op->configure(compile_context, a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(),
91 alpha, beta, gemm_info);
Georgios Pinitas856f66e2021-04-22 21:13:21 +010092 _impl->aux_mem_req = _impl->op->workspace();
Gian Marco Iodice926afe12019-03-19 11:44:13 +000093
Georgios Pinitas856f66e2021-04-22 21:13:21 +010094 // Manage/allocate auxilairy tensors
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095 if (_impl->is_prepared)
Georgios Pinitas5a643322021-06-04 16:48:30 +010096 {
Michele Di Giorgiod7316eb2021-06-16 11:14:41 +010097 _impl->run_pack.add_const_tensor(ACL_SRC_0, a);
98 _impl->run_pack.add_tensor(ACL_DST, output);
Georgios Pinitas5a643322021-06-04 16:48:30 +010099 }
100 else
101 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100102 _impl->run_pack = {{ACL_SRC_0, a}, {ACL_SRC_2, c}, {ACL_DST, output}};
103 _impl->prep_pack = {{ACL_SRC_1, _impl->b}};
Georgios Pinitas5a643322021-06-04 16:48:30 +0100104
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100105 _impl->workspace_tensors =
106 manage_workspace<CLTensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack, _impl->prep_pack);
Georgios Pinitas5a643322021-06-04 16:48:30 +0100107 }
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000108}
109
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110Status CLGEMM::validate(const ITensorInfo *a,
111 const ITensorInfo *b,
112 const ITensorInfo *c,
113 const ITensorInfo *output,
114 float alpha,
115 float beta,
116 const GEMMInfo &gemm_info)
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000117{
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100118 return OperatorType::validate(a, b, c, output, alpha, beta, gemm_info);
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000119}
120
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121void CLGEMM::run()
122{
Georgios Pinitase0437672018-05-02 14:07:55 +0100123 prepare();
Georgios Pinitas8a94e7c2017-09-15 19:06:47 +0100124
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100125 MemoryGroupResourceScope scope_mg(_impl->memory_group);
Gian Marco Iodice926afe12019-03-19 11:44:13 +0000126
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100127 _impl->op->run(_impl->run_pack);
Georgios Pinitase0437672018-05-02 14:07:55 +0100128}
Georgios Pinitas82b51482018-04-24 15:14:12 +0100129
Georgios Pinitase0437672018-05-02 14:07:55 +0100130void CLGEMM::prepare()
131{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100132 if (!_impl->is_prepared)
Georgios Pinitase0437672018-05-02 14:07:55 +0100133 {
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100134 _impl->op->prepare(_impl->prep_pack);
135
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100136 auto has_reshape =
137 std::find_if(_impl->aux_mem_req.begin(), _impl->aux_mem_req.end(),
138 [](const MemoryInfo &m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100139
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 if (has_reshape != std::end(_impl->aux_mem_req))
Georgios Pinitase0437672018-05-02 14:07:55 +0100141 {
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100142 _impl->b->mark_as_unused();
Georgios Pinitase0437672018-05-02 14:07:55 +0100143 }
Georgios Pinitas856f66e2021-04-22 21:13:21 +0100144 else
145 {
146 // Pack the B matrix to be used as the underlying GEMM performs no reshapes
147 _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->b);
148 }
Georgios Pinitas5a643322021-06-04 16:48:30 +0100149 _impl->is_prepared = true;
Georgios Pinitase0437672018-05-02 14:07:55 +0100150 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151}
giuros011c9efeb2019-01-11 14:04:43 +0000152} // namespace arm_compute