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/NEON/NEDeviceBackend.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/NEON/NEFunctionFactory.h"
33 #include "arm_compute/graph/backends/NEON/NENodeValidator.h"
34 #include "arm_compute/graph/backends/NEON/NESubTensorHandle.h"
35 #include "arm_compute/graph/backends/NEON/NETensorHandle.h"
36
37 #include "arm_compute/core/TensorInfo.h"
38 #include "arm_compute/runtime/Allocator.h"
39 #include "arm_compute/runtime/BlobLifetimeManager.h"
40 #include "arm_compute/runtime/IWeightsManager.h"
41 #include "arm_compute/runtime/MemoryGroup.h"
42 #include "arm_compute/runtime/MemoryManagerOnDemand.h"
43 #include "arm_compute/runtime/OffsetLifetimeManager.h"
44 #include "arm_compute/runtime/PoolManager.h"
45 #include "arm_compute/runtime/Scheduler.h"
46
47 #include "support/ToolchainSupport.h"
48
49 namespace arm_compute
50 {
51 namespace graph
52 {
53 namespace backends
54 {
55 /** Register CPU backend */
56 static detail::BackendRegistrar<NEDeviceBackend> NEDeviceBackend_registrar(Target::NEON);
57
NEDeviceBackend()58 NEDeviceBackend::NEDeviceBackend()
59 : _allocator()
60 {
61 }
62
initialize_backend()63 void NEDeviceBackend::initialize_backend()
64 {
65 //Nothing to do
66 }
67
release_backend_context(GraphContext & ctx)68 void NEDeviceBackend::release_backend_context(GraphContext &ctx)
69 {
70 //Nothing to do
71 ARM_COMPUTE_UNUSED(ctx);
72 }
73
setup_backend_context(GraphContext & ctx)74 void NEDeviceBackend::setup_backend_context(GraphContext &ctx)
75 {
76 // Set number of threads
77 if(ctx.config().num_threads >= 0)
78 {
79 Scheduler::get().set_num_threads(ctx.config().num_threads);
80 }
81
82 // Create function level memory manager
83 if(ctx.memory_management_ctx(Target::NEON) == nullptr)
84 {
85 MemoryManagerContext mm_ctx;
86 mm_ctx.target = Target::NEON;
87 mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Offset);
88 mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Offset);
89 mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm);
90 mm_ctx.allocator = &_allocator;
91
92 ctx.insert_memory_management_ctx(std::move(mm_ctx));
93 }
94
95 // Create function level weights manager
96 if(ctx.weights_management_ctx(Target::NEON) == nullptr)
97 {
98 WeightsManagerContext wm_ctx;
99 wm_ctx.target = Target::NEON;
100 wm_ctx.wm = create_weights_manager();
101
102 ctx.insert_weights_management_ctx(std::move(wm_ctx));
103 }
104 }
105
is_backend_supported()106 bool NEDeviceBackend::is_backend_supported()
107 {
108 return true;
109 }
110
backend_allocator()111 IAllocator *NEDeviceBackend::backend_allocator()
112 {
113 return &_allocator;
114 }
115
create_tensor(const Tensor & tensor)116 std::unique_ptr<ITensorHandle> NEDeviceBackend::create_tensor(const Tensor &tensor)
117 {
118 // Get tensor descriptor
119 const TensorDescriptor &tensor_desc = tensor.desc();
120 ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::NEON);
121
122 // Create backend tensor handle
123 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
124 info.set_data_layout(tensor_desc.layout);
125
126 return std::make_unique<NETensorHandle>(info);
127 }
128
create_subtensor(ITensorHandle * parent,TensorShape shape,Coordinates coords,bool extend_parent)129 std::unique_ptr<ITensorHandle> NEDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
130 {
131 if(parent == nullptr)
132 {
133 return nullptr;
134 }
135
136 return std::make_unique<NESubTensorHandle>(parent, shape, coords, extend_parent);
137 }
138
configure_node(INode & node,GraphContext & ctx)139 std::unique_ptr<arm_compute::IFunction> NEDeviceBackend::configure_node(INode &node, GraphContext &ctx)
140 {
141 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CPU node with ID : " << node.id() << std::endl);
142 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
143
144 // Configure node
145 return NEFunctionFactory::create(&node, ctx);
146 }
147
validate_node(INode & node)148 arm_compute::Status NEDeviceBackend::validate_node(INode &node)
149 {
150 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CPU node with ID : " << node.id() << std::endl);
151 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
152
153 return NENodeValidator::validate(&node);
154 }
155
create_memory_manager(MemoryManagerAffinity affinity)156 std::shared_ptr<arm_compute::IMemoryManager> NEDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
157 {
158 std::shared_ptr<ILifetimeManager> lifetime_mgr = nullptr;
159 if(affinity == MemoryManagerAffinity::Buffer)
160 {
161 lifetime_mgr = std::make_shared<BlobLifetimeManager>();
162 }
163 else
164 {
165 lifetime_mgr = std::make_shared<OffsetLifetimeManager>();
166 }
167 auto pool_mgr = std::make_shared<PoolManager>();
168 auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
169
170 return mm;
171 }
172
create_weights_manager()173 std::shared_ptr<arm_compute::IWeightsManager> NEDeviceBackend::create_weights_manager()
174 {
175 auto weights_mgr = std::make_shared<IWeightsManager>();
176 return weights_mgr;
177 }
178
sync()179 void NEDeviceBackend::sync()
180 {
181 // nop
182 }
183 } // namespace backends
184 } // namespace graph
185 } // namespace arm_compute
186