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