Compute Library
 21.05
CLDeviceBackend.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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 
36 
46 
48 
49 namespace arm_compute
50 {
51 namespace graph
52 {
53 namespace backends
54 {
55 namespace
56 {
57 bool file_exists(const std::string &filename)
58 {
59  std::ifstream file(filename);
60  return file.good();
61 }
62 } // namespace
63 
64 /** Register CL backend */
65 static detail::BackendRegistrar<CLDeviceBackend> CLDeviceBackend_registrar(Target::CL);
66 
68  : _context_count(0), _tuner(), _gemm_heuristics(), _allocator(nullptr), _tuner_file(), _backend_type(CLBackendType::Native)
69 {
70 }
71 
73 {
74  _tuner.save_to_file(_tuner_file);
75 }
76 
77 void CLDeviceBackend::set_kernel_tuning(bool enable_tuning)
78 {
79  _tuner.set_tune_new_kernels(enable_tuning);
80 }
81 
83 {
84  _tuner.set_tuner_mode(tuning_mode);
85 }
86 
88 {
89  // Setup Scheduler
90  CLScheduler::get().default_init(&_tuner, &_gemm_heuristics, _backend_type);
91  // Create allocator with new context
92  _allocator = std::make_unique<CLBufferAllocator>(nullptr /* legacy path for CLCoreRuntimeContext */);
93 }
94 
96 {
97  ARM_COMPUTE_UNUSED(ctx);
98  _context_count--;
99  if(_context_count == 0) // No more context using the backend: free resources
100  {
101  _allocator = nullptr;
102  }
103 }
104 
106 {
107  // Force backend initialization
108  _context_count++;
109  if(_context_count == 1)
110  {
111  _backend_type = ctx.config().backend_type;
113  }
114 
115  // Setup tuner
116  _tuner_file = ctx.config().tuner_file;
117 
118  // Load tuner data if available
119  if(file_exists(_tuner_file))
120  {
121  _tuner.load_from_file(_tuner_file);
122  }
123 
126 
127  // Attempt to load mlgo heuristics
128  ARM_COMPUTE_ERROR_ON(CLScheduler::get().gemm_heuristics() == nullptr);
130 
131  // Setup a management backend
132  if(ctx.memory_management_ctx(Target::CL) == nullptr)
133  {
134  MemoryManagerContext mm_ctx;
135  mm_ctx.target = Target::CL;
138  mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm);
139  mm_ctx.allocator = _allocator.get();
140 
141  ctx.insert_memory_management_ctx(std::move(mm_ctx));
142  }
143 
144  // Create function level weights manager
145  if(ctx.weights_management_ctx(Target::CL) == nullptr)
146  {
147  WeightsManagerContext wm_ctx;
148  wm_ctx.target = Target::CL;
149  wm_ctx.wm = create_weights_manager();
150 
151  ctx.insert_weights_management_ctx(std::move(wm_ctx));
152  }
153 }
154 
156 {
158 }
159 
161 {
162  return _allocator.get();
163 }
164 
165 std::unique_ptr<ITensorHandle> CLDeviceBackend::create_tensor(const Tensor &tensor)
166 {
167  // Get tensor descriptor
168  const TensorDescriptor &tensor_desc = tensor.desc();
169  ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::CL);
170 
171  // Create backend tensor handle
172  TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
173  info.set_data_layout(tensor_desc.layout);
174 
175  return std::make_unique<CLTensorHandle>(info);
176 }
177 
178 std::unique_ptr<ITensorHandle> CLDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
179 {
180  if(parent == nullptr)
181  {
182  return nullptr;
183  }
184 
185  return std::make_unique<CLSubTensorHandle>(parent, shape, coords, extend_parent);
186 }
187 
188 std::unique_ptr<arm_compute::IFunction> CLDeviceBackend::configure_node(INode &node, GraphContext &ctx)
189 {
190  ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CL node with ID : " << node.id() << std::endl);
192 
193  // Configure node
194  return CLFunctionFactory::create(&node, ctx);
195 }
196 
198 {
199  ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CL node with ID : " << node.id() << std::endl);
201 
202  return CLNodeValidator::validate(&node);
203 }
204 
205 std::shared_ptr<arm_compute::IMemoryManager> CLDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
206 {
207  if(affinity == MemoryManagerAffinity::Offset)
208  {
209  ARM_COMPUTE_LOG_GRAPH_WARNING("CL Backend does not support offset affinity memory management!");
210  return nullptr;
211  }
212 
213  auto lifetime_mgr = std::make_shared<BlobLifetimeManager>();
214  auto pool_mgr = std::make_shared<PoolManager>();
215  auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
216 
217  return mm;
218 }
219 
220 std::shared_ptr<arm_compute::IWeightsManager> CLDeviceBackend::create_weights_manager()
221 {
222  auto weights_mgr = std::make_shared<IWeightsManager>();
223  return weights_mgr;
224 }
225 } // namespace backends
226 } // namespace graph
227 } // namespace arm_compute
void setup_backend_context(GraphContext &ctx) override
Setups the given graph context.
void set_tuner_mode(CLTunerMode mode)
Set OpenCL tuner mode.
Definition: CLTuner.cpp:61
Shape of a tensor.
Definition: TensorShape.h:39
CLTunerMode tuner_mode
Tuner mode to be used by the CL tuner.
Definition: Types.h:91
void set_kernel_tuning(bool enable_tuning)
Switchs on or off the kernel tuning.
std::unique_ptr< ITensorHandle > create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent) override
Create a backend Sub-Tensor.
static CLScheduler & get()
Access the scheduler singleton.
void set_kernel_tuning_mode(CLTunerMode tuning_mode)
Set kernel tuning mode.
Target assigned_target() const
Returns assigned target for this node.
Definition: INode.cpp:198
bool insert_weights_management_ctx(WeightsManagerContext &&weights_ctx)
Inserts a weights manager context.
std::shared_ptr< arm_compute::IMemoryGroup > cross_group
Cross-function memory group.
Definition: GraphContext.h:45
static std::unique_ptr< arm_compute::IFunction > create(INode *node, GraphContext &ctx)
Create a backend execution function depending on the node type.
void load_from_file(const std::string &filename)
Load the tuning parameters table from file.
Definition: CLTuner.cpp:280
void default_init(ICLTuner *cl_tuner=nullptr, CLGEMMHeuristicsHandle *gemm_h=nullptr, CLBackendType cl_backend_type=CLBackendType::Native)
Initialises the context and command queue used by the scheduler to default values and sets a default ...
bool insert_memory_management_ctx(MemoryManagerContext &&memory_ctx)
Inserts a memory manager context.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Status class.
Definition: Error.h:52
WeightsManagerContext * weights_management_ctx(Target target)
Gets a weights manager context for a given target.
void initialize_backend() override
Initializes the backend.
std::unique_ptr< arm_compute::IFunction > configure_node(INode &node, GraphContext &ctx) override
Configure a backend Node.
CLGEMMHeuristicsHandle * gemm_heuristics() const
Accessor for the associated CLGEMMHeuristicsHandle.
Definition: CLScheduler.cpp:50
Copyright (c) 2017-2021 Arm Limited.
std::string mlgo_file
Filename to load MLGO heuristics from.
Definition: Types.h:94
std::string tuner_file
File to load/store tuning values from.
Definition: Types.h:93
bool is_backend_supported() override
Checks if an instantiated backend is actually supported.
TensorDescriptor & desc()
TensorInfo metadata accessor.
Definition: Tensor.cpp:40
std::shared_ptr< arm_compute::IWeightsManager > create_weights_manager() override
Create a backend weights manager.
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
QuantizationInfo quant_info
Quantization info.
Node interface.
Definition: INode.h:45
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
bool save_to_file(const std::string &filename) const
Save the content of the tuning parameters table to file.
Definition: CLTuner.cpp:332
bool reload_from_file(const std::string &filename)
(Re)Load the heuristics from reading a dotmlgo file
Coordinates of an item.
Definition: Coordinates.h:37
void set_tune_new_kernels(bool tune_new_kernels)
Setter for tune_new_kernels option.
Definition: CLTuner.cpp:52
void release_backend_context(GraphContext &ctx) override
Release the backend specific resources associated to a given graph context.
NodeID id() const
Returns node's ID.
Definition: INode.cpp:102
Status validate_node(INode &node) override
Validate a node.
std::shared_ptr< arm_compute::IMemoryManager > create_memory_manager(MemoryManagerAffinity affinity) override
Create a backend memory manager given its affinity.
Tensor handle interface object.
Definition: ITensorHandle.h:38
std::shared_ptr< arm_compute::IMemoryManager > intra_mm
Intra-function memory manager.
Definition: GraphContext.h:43
static Status validate(INode *node)
Validate a node.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
#define ARM_COMPUTE_LOG_GRAPH_WARNING(x)
Definition: Logger.h:58
CLBackendType
List the possible OpenCL backends.
Definition: CLTypes.h:63
const GraphConfig & config() const
Graph configuration accessor.
std::shared_ptr< arm_compute::IMemoryManager > cross_mm
Cross-function memory manager.
Definition: GraphContext.h:44
CLTunerMode
< OpenCL tuner modes
Definition: CLTunerTypes.h:35
bool use_tuner
Use a tuner in tunable backends.
Definition: Types.h:88
Contains structs required for weights management.
Definition: GraphContext.h:50
#define ARM_COMPUTE_LOG_GRAPH_VERBOSE(x)
Definition: Logger.h:50
IAllocator * backend_allocator() override
Gets a backend memory allocator.
Contains structs required for memory management.
Definition: GraphContext.h:40
Allocator interface.
Definition: IAllocator.h:35
Store the tensor's metadata.
Definition: TensorInfo.h:43
std::unique_ptr< ITensorHandle > create_tensor(const Tensor &tensor) override
Create a backend Tensor.
IAllocator * allocator
Backend allocator to use.
Definition: GraphContext.h:46
MemoryManagerContext * memory_management_ctx(Target target)
Gets a memory manager context for a given target.
OpenCL capable target device.
CLBackendType backend_type
CL backend type to use.
Definition: Types.h:95
MemoryManagerAffinity
Backend Memory Manager affinity.
Definition: Types.h:200
std::shared_ptr< arm_compute::IWeightsManager > wm
Weights manager.
Definition: GraphContext.h:53
Tensor object.
Definition: Tensor.h:41
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:154