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