Compute Library
 21.08
CLGEMM.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 
31 #include "arm_compute/core/Types.h"
32 #include "arm_compute/core/Utils.h"
36 
37 namespace arm_compute
38 {
39 using namespace arm_compute::experimental;
41 
42 struct CLGEMM::Impl
43 {
44  const ICLTensor *b{ nullptr };
45  std::unique_ptr<OperatorType> op{ nullptr };
46  MemoryGroup memory_group{};
47  IWeightsManager *weights_manager{ nullptr };
48  ITensorPack run_pack{};
49  ITensorPack prep_pack{};
50  MemoryRequirements aux_mem_req{};
51  WorkspaceData<CLTensor> workspace_tensors{};
52  bool is_prepared{ false };
53 };
54 
55 CLGEMM::CLGEMM(std::shared_ptr<IMemoryManager> memory_manager, IWeightsManager *weights_manager)
56  : _impl(std::make_unique<Impl>())
57 {
58  _impl->memory_group = MemoryGroup(memory_manager);
59  _impl->weights_manager = weights_manager;
60 }
61 
62 CLGEMM::~CLGEMM() = default;
63 
64 void CLGEMM::configure(const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info)
65 {
66  configure(CLKernelLibrary::get().get_compile_context(), a, b, c, output, alpha, beta, gemm_info);
67 }
68 
69 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)
70 {
71  ARM_COMPUTE_ERROR_ON_NULLPTR(a, b, output);
72 
73  _impl->b = b;
74  _impl->op = std::make_unique<OperatorType>();
75  _impl->is_prepared = gemm_info.retain_internal_weights();
76 
77  _impl->op->configure(compile_context, a->info(), b->info(), c != nullptr ? c->info() : nullptr, output->info(), alpha, beta, gemm_info);
78  _impl->aux_mem_req = _impl->op->workspace();
79 
80  // Manage/allocate auxilairy tensors
81  if(_impl->is_prepared)
82  {
83  _impl->run_pack.add_const_tensor(ACL_SRC_0, a);
84  _impl->run_pack.add_tensor(ACL_DST, output);
85  }
86  else
87  {
88  _impl->run_pack = { { ACL_SRC_0, a }, { ACL_SRC_2, c }, { ACL_DST, output } };
89  _impl->prep_pack = { { ACL_SRC_1, _impl->b } };
90 
91  _impl->workspace_tensors = manage_workspace<CLTensor>(_impl->op->workspace(), _impl->memory_group, _impl->run_pack, _impl->prep_pack);
92  }
93 }
94 
95 Status CLGEMM::validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info)
96 {
97  return OperatorType::validate(a, b, c, output, alpha, beta, 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->prep_pack);
114 
115  auto has_reshape = std::find_if(_impl->aux_mem_req.begin(),
116  _impl->aux_mem_req.end(),
117  [](const MemoryInfo & m) -> bool { return m.lifetime == MemoryLifetime::Persistent; });
118 
119  if(has_reshape != std::end(_impl->aux_mem_req))
120  {
121  _impl->b->mark_as_unused();
122  }
123  else
124  {
125  // Pack the B matrix to be used as the underlying GEMM performs no reshapes
126  _impl->run_pack.add_const_tensor(ACL_SRC_1, _impl->b);
127  }
128  _impl->is_prepared = true;
129  }
130 }
131 } // namespace arm_compute
std::vector< WorkspaceDataElement< TensorType > > WorkspaceData
Definition: MemoryHelpers.h:52
void prepare() override
Prepare the function for executing.
Definition: CLGEMM.cpp:109
~CLGEMM()
Default destructor.
void run() override
Run the kernels contained in the function.
Definition: CLGEMM.cpp:100
SimpleTensor< float > b
Definition: DFT.cpp:157
CLGEMM(std::shared_ptr< IMemoryManager > memory_manager=nullptr, IWeightsManager *weights_manager=nullptr)
Default constructor.
Definition: CLGEMM.cpp:55
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
Manages all the OpenCL kernels compilation and caching, provides accessors for the OpenCL Context...
Status class.
Definition: Error.h:52
bool retain_internal_weights() const
Flag which specifies if the weights tensor has to be retained from previous run.
Definition: Types.h:2040
Copyright (c) 2017-2021 Arm Limited.
std::vector< MemoryInfo > MemoryRequirements
Definition: Types.h:113
void configure(const CLCompileContext &compile_context, const ICLTensor *a, const ICLTensor *b, const ICLTensor *c, ICLTensor *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
Initialise the kernel&#39;s inputs and output.
Definition: CLGEMM.cpp:69
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
Weights manager interface to handle weights transformations.
CLCompileContext class.
Memory group resources scope handling class.
Definition: IMemoryGroup.h:82
Interface for OpenCL tensor.
Definition: ICLTensor.h:42
static Status validate(const ITensorInfo *a, const ITensorInfo *b, const ITensorInfo *c, const ITensorInfo *output, float alpha, float beta, const GEMMInfo &gemm_info=GEMMInfo())
Static function to check if given info will lead to a valid configuration of CLGEMM.
Definition: CLGEMM.cpp:95
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
GEMM information class.
Definition: Types.h:1939
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:612
Basic function to execute GEMM on OpenCL.
Definition: ClGemm.h:55