xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/CL/functions/CLGEMM.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2022 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/CL/functions/CLGEMM.h"
25 
26 #include "arm_compute/core/CL/CLHelpers.h"
27 #include "arm_compute/core/CL/CLKernelLibrary.h"
28 #include "arm_compute/core/Helpers.h"
29 #include "arm_compute/core/KernelDescriptors.h"
30 #include "arm_compute/core/TensorInfo.h"
31 #include "arm_compute/core/Types.h"
32 #include "arm_compute/core/Utils.h"
33 #include "src/core/helpers/MemoryHelpers.h"
34 #include "src/gpu/cl/operators/ClGemm.h"
35 
36 namespace arm_compute
37 {
38 using namespace arm_compute::experimental;
39 using OperatorType = opencl::ClGemm;
40 
41 struct CLGEMM::Impl
42 {
43     const ICLTensor              *b{ nullptr };
44     std::unique_ptr<OperatorType> op{ nullptr };
45     MemoryGroup                   memory_group{};
46     IWeightsManager              *weights_manager{ nullptr };
47     ITensorPack                   run_pack{};
48     ITensorPack                   prep_pack{};
49     MemoryRequirements            aux_mem_req{};
50     WorkspaceData<CLTensor>       workspace_tensors{};
51     bool                          is_prepared{ false };
52 };
53 
CLGEMM(std::shared_ptr<IMemoryManager> memory_manager,IWeightsManager * weights_manager)54 CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
55     : _impl(std::make_unique<Impl>())
56 {
57     _impl->memory_group    = MemoryGroup(memory_manager);
58     _impl->weights_manager = weights_manager;
59 }
60 
61 CLGEMM::~CLGEMM() = default;
62 
configure(const ICLTensor * a,const ICLTensor * b,const ICLTensor * c,ICLTensor * output,float alpha,float beta,const GEMMInfo & gemm_info)63 void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
64 {
65     configure(CLKernelLibrary::get().get_compile_context(), a, b, c, output, alpha, beta, gemm_info);
66 }
67 
configure(const CLCompileContext & compile_context,const ICLTensor * a,const ICLTensor * b,const ICLTensor * c,ICLTensor * output,float alpha,float beta,const GEMMInfo & gemm_info)68 void 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)
69 {
70     ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
71 
72     _impl->b           = b;
73     _impl->op          = std::make_unique<OperatorType>();
74     _impl->is_prepared = gemm_info.retain_internal_weights();
75 
76     _impl->op->configure(compile_context, a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info);
77     _impl->aux_mem_req = _impl->op->workspace();
78 
79     // Manage/allocate auxilairy tensors
80     if(_impl->is_prepared)
81     {
82         _impl->run_pack.add_const_tensor(ACL_SRC_0, a);
83         _impl->run_pack.add_tensor(ACL_DST, output);
84     }
85     else
86     {
87         _impl->run_pack  = { { ACL_SRC_0, a }, { ACL_SRC_2, c }, { ACL_DST, output } };
88         _impl->prep_pack = { { ACL_SRC_1, _impl->b } };
89 
90         _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack, _impl->prep_pack);
91     }
92 }
93 
validate(const ITensorInfo * a,const ITensorInfo * b,const ITensorInfo * c,const ITensorInfo * output,float alpha,float beta,const GEMMInfo & gemm_info)94 Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
95 {
96     return OperatorType::validate(a, b, c, output, alpha, beta, gemm_info);
97 }
98 
run()99 void CLGEMM::run()
100 {
101     prepare();
102 
103     MemoryGroupResourceScope scope_mg(_impl->memory_group);
104 
105     _impl->op->run(_impl->run_pack);
106 }
107 
prepare()108 void CLGEMM::prepare()
109 {
110     if(!_impl->is_prepared)
111     {
112         _impl->op->prepare(_impl->prep_pack);
113 
114         auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
115                                         _impl->aux_mem_req.end(),
116                                         [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
117 
118         if(has_reshape != std::end(_impl->aux_mem_req))
119         {
120             _impl->b->mark_as_unused();
121         }
122         else
123         {
124             // Pack the B matrix to be used as the underlying GEMM performs no reshapes
125             _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->b);
126         }
127         _impl->is_prepared = true;
128     }
129 }
130 } // namespace arm_compute
131