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/CL/functions/CLGEMMLowpMatrixMultiplyCore.h"
25
26 #include "arm_compute/core/CL/CLKernelLibrary.h"
27 #include "arm_compute/core/CL/ICLTensor.h"
28 #include "arm_compute/core/Error.h"
29 #include "arm_compute/core/Helpers.h"
30 #include "arm_compute/core/KernelDescriptors.h"
31 #include "arm_compute/core/Log.h"
32 #include "arm_compute/core/TensorInfo.h"
33 #include "arm_compute/core/Types.h"
34 #include "arm_compute/core/Validate.h"
35 #include "arm_compute/core/utils/quantization/AsymmHelpers.h"
36 #include "arm_compute/runtime/CL/CLScheduler.h"
37 #include "arm_compute/runtime/IMemoryManager.h"
38 #include "src/core/helpers/MemoryHelpers.h"
39
40 #include "src/gpu/cl/operators/ClGemmLowpMatrixMultiplyCore.h"
41
42 namespace arm_compute
43 {
44 using namespace arm_compute::experimental;
45 using OperatorType = opencl::ClGemmLowpMatrixMultiplyCore;
46
47 struct CLGEMMLowpMatrixMultiplyCore::Impl
48 {
49 const ICLTensor *b{ nullptr };
50 std::unique_ptr<OperatorType> op{ nullptr };
51 MemoryGroup memory_group{};
52 ITensorPack run_pack{};
53 MemoryRequirements aux_mem_req{};
54 WorkspaceData<CLTensor> workspace_tensors{};
55 bool is_prepared{ false };
56 };
57
CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)58 CLGEMMLowpMatrixMultiplyCore::CLGEMMLowpMatrixMultiplyCore(std::shared_ptr<IMemoryManager> memory_manager)
59 : _impl(std::make_unique<Impl>())
60 {
61 _impl->memory_group = MemoryGroup(memory_manager);
62 }
63
64 CLGEMMLowpMatrixMultiplyCore::~CLGEMMLowpMatrixMultiplyCore() = default;
65
configure(const ICLTensor * a,const ICLTensor * b,const ICLTensor * c,ICLTensor * output,const GEMMInfo & gemm_info)66 void CLGEMMLowpMatrixMultiplyCore::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info)
67 {
68 configure(CLKernelLibrary::get().get_compile_context(), a, b, c, output, gemm_info);
69 }
70
configure(const CLCompileContext & compile_context,const ICLTensor * a,const ICLTensor * b,const ICLTensor * c,ICLTensor * output,const GEMMInfo & gemm_info)71 void CLGEMMLowpMatrixMultiplyCore::configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info)
72 {
73 ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
74
75 _impl->b = b;
76 _impl->op = std::make_unique<OperatorType>();
77 _impl->is_prepared = gemm_info.retain_internal_weights();
78
79 _impl->op->configure(compile_context, a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), gemm_info);
80 _impl->aux_mem_req = _impl->op->workspace();
81
82 // Manage/allocate auxilairy tensors
83 if(_impl->is_prepared)
84 {
85 _impl->run_pack.add_const_tensor(ACL_SRC_0, a);
86 _impl->run_pack.add_tensor(ACL_DST, output);
87 }
88 else
89 {
90 _impl->run_pack = { { ACL_SRC_0, a }, { ACL_SRC_1, _impl->b }, { ACL_SRC_2, c }, { ACL_DST, output } };
91 _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack, _impl->run_pack);
92 }
93 }
94
validate(const ITensorInfo * a,const ITensorInfo * b,const ITensorInfo * c,const ITensorInfo * output,const GEMMInfo & gemm_info)95 Status CLGEMMLowpMatrixMultiplyCore::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info)
96 {
97 return OperatorType::validate(a, b, c, output, gemm_info);
98 }
99
run()100 void CLGEMMLowpMatrixMultiplyCore::run()
101 {
102 prepare();
103
104 MemoryGroupResourceScope scope_mg(_impl->memory_group);
105
106 _impl->op->run(_impl->run_pack);
107 }
108
prepare()109 void CLGEMMLowpMatrixMultiplyCore::prepare()
110 {
111 if(!_impl->is_prepared)
112 {
113 _impl->op->prepare(_impl->run_pack);
114
115 // Release temporary tensors that are only used in prepare stage
116 release_temporaries(_impl->aux_mem_req, _impl->workspace_tensors);
117
118 _impl->is_prepared = true;
119 }
120 }
121 } // namespace arm_compute
122