xref: /aosp_15_r20/external/ComputeLibrary/src/runtime/NEON/functions/NEGEMM.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/NEON/functions/NEGEMM.h"
25 
26 #include "arm_compute/core/ITensorPack.h"
27 #include "arm_compute/core/TensorInfo.h"
28 #include "arm_compute/core/Types.h"
29 #include "arm_compute/runtime/MemoryGroup.h"
30 #include "arm_compute/runtime/Tensor.h"
31 #include "src/core/CPP/Validate.h"
32 #include "src/core/helpers/MemoryHelpers.h"
33 #include "src/cpu/operators/CpuGemm.h"
34 
35 using namespace arm_compute::experimental;
36 
37 namespace arm_compute
38 {
39 struct NEGEMM::Impl
40 {
41     MemoryGroup      memory_group{};
42     IWeightsManager *weights_manager{ nullptr };
43 
44     std::unique_ptr<cpu::CpuGemm> op{ nullptr };
45 
46     const ITensor *original_b{ nullptr };
47     bool           is_prepared{ false };
48 
49     ITensorPack                      run_pack{};
50     ITensorPack                      prep_pack{};
51     WorkspaceData<Tensor>            workspace{};
52     experimental::MemoryRequirements aux_mem_req{};
53 };
54 
NEGEMM(std::shared_ptr<IMemoryManager> memory_manager,IWeightsManager * weights_manager)55 NEGEMM::NEGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
56     : _impl(std::make_unique<Impl>())
57 {
58     _impl->memory_group    = MemoryGroup(std::move(memory_manager));
59     _impl->weights_manager = weights_manager;
60 }
61 
62 NEGEMM::~NEGEMM() = default;
63 
configure(const ITensor * a,const ITensor * b,const ITensor * c,ITensor * d,float alpha,float beta,const GEMMInfo & gemm_info)64 void NEGEMM::configure(const ITensor *a, const ITensor *b, const ITensor *c, ITensor *d, float alpha, float beta, const GEMMInfo &gemm_info)
65 {
66     ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, d);
67     ARM_COMPUTE_ERROR_THROW_ON(cpu::CpuGemm::validate(a->info(), b->info(), (c != nullptr) ? c->info() : nullptr, d->info(), alpha, beta, gemm_info));
68 
69     // Check if we need to reshape the matrix B only on the first run
70     _impl->is_prepared = false;
71     _impl->original_b  = b;
72     _impl->op          = std::make_unique<cpu::CpuGemm>();
73 
74     _impl->op->configure(a->info(), b->info(), (c != nullptr) ? c->info() : nullptr, d->info(), alpha, beta, gemm_info);
75 
76     _impl->aux_mem_req = _impl->op->workspace();
77     _impl->run_pack    = { { ACL_SRC_0, a }, { ACL_SRC_1, b }, { ACL_SRC_2, c }, { ACL_DST, d } };
78     _impl->prep_pack   = { { ACL_SRC_1, b }, { ACL_SRC_2, c } };
79     _impl->workspace   = manage_workspace<Tensor>(_impl->aux_mem_req, _impl->memory_group, _impl->run_pack, _impl->prep_pack);
80 }
81 
validate(const ITensorInfo * a,const ITensorInfo * b,const ITensorInfo * c,const ITensorInfo * output,float alpha,float beta,const GEMMInfo & gemm_info)82 Status NEGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
83 {
84     return cpu::CpuGemm::validate(a, b, c, output, alpha, beta, gemm_info);
85 }
86 
has_opt_impl(arm_compute::WeightFormat & expected_weight_format,const ITensorInfo * a,const ITensorInfo * b,const ITensorInfo * c,const ITensorInfo * output,float alpha,float beta,const GEMMInfo & gemm_info)87 Status NEGEMM::has_opt_impl(arm_compute::WeightFormat &expected_weight_format, const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output,
88                             float alpha, float beta, const GEMMInfo &gemm_info)
89 {
90     ARM_COMPUTE_UNUSED(alpha, beta);
91     return cpu::CpuGemm::has_opt_impl(expected_weight_format, a, b, c, output, gemm_info);
92 }
93 
run()94 void NEGEMM::run()
95 {
96     prepare();
97 
98     MemoryGroupResourceScope scope_mg(_impl->memory_group);
99     _impl->op->run(_impl->run_pack);
100 }
101 
prepare()102 void NEGEMM::prepare()
103 {
104     if(!_impl->is_prepared)
105     {
106         _impl->op->prepare(_impl->prep_pack);
107 
108         auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
109                                         _impl->aux_mem_req.end(),
110                                         [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
111 
112         if(has_reshape != std::end(_impl->aux_mem_req))
113         {
114             _impl->original_b->mark_as_unused();
115         }
116         else
117         {
118             _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->original_b);
119         }
120 
121         // Release temporary tensors that are only used in prepare stage
122         release_temporaries<Tensor>(_impl->aux_mem_req, _impl->workspace);
123         _impl->is_prepared = true;
124     }
125 }
126 } // namespace arm_compute
127