• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
24 #include "arm_compute/graph/backends/CL/CLDeviceBackend.h"
25 
26 #include "arm_compute/graph/Graph.h"
27 #include "arm_compute/graph/GraphContext.h"
28 #include "arm_compute/graph/INode.h"
29 #include "arm_compute/graph/Logger.h"
30 #include "arm_compute/graph/Tensor.h"
31 #include "arm_compute/graph/backends/BackendRegistrar.h"
32 #include "arm_compute/graph/backends/CL/CLFunctionFactory.h"
33 #include "arm_compute/graph/backends/CL/CLNodeValidator.h"
34 #include "arm_compute/graph/backends/CL/CLSubTensorHandle.h"
35 #include "arm_compute/graph/backends/CL/CLTensorHandle.h"
36 
37 #include "arm_compute/core/TensorInfo.h"
38 #include "arm_compute/runtime/BlobLifetimeManager.h"
39 #include "arm_compute/runtime/CL/CLBufferAllocator.h"
40 #include "arm_compute/runtime/CL/CLScheduler.h"
41 #include "arm_compute/runtime/IWeightsManager.h"
42 #include "arm_compute/runtime/MemoryGroup.h"
43 #include "arm_compute/runtime/MemoryManagerOnDemand.h"
44 #include "arm_compute/runtime/PoolManager.h"
45 
46 #include "support/ToolchainSupport.h"
47 
48 namespace arm_compute
49 {
50 namespace graph
51 {
52 namespace backends
53 {
54 namespace
55 {
file_exists(const std::string & filename)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 
CLDeviceBackend()66 CLDeviceBackend::CLDeviceBackend()
67     : _context_count(0), _tuner(), _gemm_heuristics(), _allocator(nullptr), _tuner_file(), _backend_type(CLBackendType::Native)
68 {
69 }
70 
~CLDeviceBackend()71 CLDeviceBackend::~CLDeviceBackend()
72 {
73     _tuner.save_to_file(_tuner_file);
74 }
75 
set_kernel_tuning(bool enable_tuning)76 void CLDeviceBackend::set_kernel_tuning(bool enable_tuning)
77 {
78     _tuner.set_tune_new_kernels(enable_tuning);
79 }
80 
set_kernel_tuning_mode(CLTunerMode tuning_mode)81 void CLDeviceBackend::set_kernel_tuning_mode(CLTunerMode tuning_mode)
82 {
83     _tuner.set_tuner_mode(tuning_mode);
84 }
85 
initialize_backend()86 void CLDeviceBackend::initialize_backend()
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 
release_backend_context(GraphContext & ctx)94 void CLDeviceBackend::release_backend_context(GraphContext &ctx)
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 
setup_backend_context(GraphContext & ctx)104 void CLDeviceBackend::setup_backend_context(GraphContext &ctx)
105 {
106     // Force backend initialization
107     _context_count++;
108     if(_context_count == 1)
109     {
110         _backend_type = ctx.config().backend_type;
111         initialize_backend();
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 
123     set_kernel_tuning(ctx.config().use_tuner);
124     set_kernel_tuning_mode(ctx.config().tuner_mode);
125 
126     // Attempt to load mlgo heuristics
127     ARM_COMPUTE_ERROR_ON(CLScheduler::get().gemm_heuristics() == nullptr);
128     CLScheduler::get().gemm_heuristics()->reload_from_file(ctx.config().mlgo_file);
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;
135         mm_ctx.intra_mm    = create_memory_manager(MemoryManagerAffinity::Buffer);
136         mm_ctx.cross_mm    = create_memory_manager(MemoryManagerAffinity::Buffer);
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 
is_backend_supported()154 bool CLDeviceBackend::is_backend_supported()
155 {
156     return arm_compute::opencl_is_available();
157 }
158 
backend_allocator()159 IAllocator *CLDeviceBackend::backend_allocator()
160 {
161     return _allocator.get();
162 }
163 
create_tensor(const Tensor & tensor)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 
create_subtensor(ITensorHandle * parent,TensorShape shape,Coordinates coords,bool extend_parent)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 
configure_node(INode & node,GraphContext & ctx)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);
190     ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL);
191 
192     // Configure node
193     return CLFunctionFactory::create(&node, ctx);
194 }
195 
validate_node(INode & node)196 arm_compute::Status CLDeviceBackend::validate_node(INode &node)
197 {
198     ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CL node with ID : " << node.id() << std::endl);
199     ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL);
200 
201     return CLNodeValidator::validate(&node);
202 }
203 
create_memory_manager(MemoryManagerAffinity affinity)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 
create_weights_manager()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 
sync()225 void CLDeviceBackend::sync()
226 {
227     CLScheduler::get().sync();
228 }
229 } // namespace backends
230 } // namespace graph
231 } // namespace arm_compute
232