Compute Library
 22.05
CLGEMMLowpMatrixMultiplyCore.cpp
Go to the documentation of this file.
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  */
25 
28 #include "arm_compute/core/Error.h"
31 #include "arm_compute/core/Log.h"
33 #include "arm_compute/core/Types.h"
39 
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 
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 
65 
67 {
68  configure(CLKernelLibrary::get().get_compile_context(), a, b, c, output, gemm_info);
69 }
70 
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 
96 {
97  return OperatorType::validate(a, b, c, output, gemm_info);
98 }
99 
101 {
102  prepare();
103 
104  MemoryGroupResourceScope scope_mg(_impl->memory_group);
105 
106  _impl->op->run(_impl->run_pack);
107 }
108 
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
void prepare() override
Prepare the function for executing.
SimpleTensor< float > b
Definition: DFT.cpp:157
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
void configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, const GEMMInfo &gemm_info=GEMMInfo())
Initialise the kernel&#39;s inputs, output.
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
Status class.
Definition: Error.h:52
void run() override
Run the kernels contained in the function.
bool retain_internal_weights() const
Flag which specifies if the weights tensor has to be retained from previous run.
Definition: Types.h:2193
Copyright (c) 2017-2022 Arm Limited.
std::vector< MemoryInfo > MemoryRequirements
Definition: Types.h:134
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, const GEMMInfo &gemm_info=GEMMInfo())
Static function to check if given info will lead to a valid configuration of CLGEMMLowpMatrixMultiply...
~CLGEMMLowpMatrixMultiplyCore()
Default destructor.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void release_temporaries(const experimental::MemoryRequirements &mem_reqs, WorkspaceData< TensorType > &workspace)
Utility function to release tensors with lifetime marked as Prepare.
CLCompileContext class.
CLGEMMLowpMatrixMultiplyCore(std::shared_ptr< IMemoryManager > memory_manager=nullptr)
Constructor.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
opencl::ClGemm OperatorType
Definition: CLGEMM.cpp:40
GEMM information class.
Definition: Types.h:2090
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
Static function to check if given info will lead to a valid configuration.
Definition: ClGemm.cpp:511