1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2018-2021 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "arm_compute/graph/backends/NEON/NEDeviceBackend.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "arm_compute/graph/Graph.h"
27*c217d954SCole Faust #include "arm_compute/graph/GraphContext.h"
28*c217d954SCole Faust #include "arm_compute/graph/INode.h"
29*c217d954SCole Faust #include "arm_compute/graph/Logger.h"
30*c217d954SCole Faust #include "arm_compute/graph/Tensor.h"
31*c217d954SCole Faust #include "arm_compute/graph/backends/BackendRegistrar.h"
32*c217d954SCole Faust #include "arm_compute/graph/backends/NEON/NEFunctionFactory.h"
33*c217d954SCole Faust #include "arm_compute/graph/backends/NEON/NENodeValidator.h"
34*c217d954SCole Faust #include "arm_compute/graph/backends/NEON/NESubTensorHandle.h"
35*c217d954SCole Faust #include "arm_compute/graph/backends/NEON/NETensorHandle.h"
36*c217d954SCole Faust
37*c217d954SCole Faust #include "arm_compute/core/TensorInfo.h"
38*c217d954SCole Faust #include "arm_compute/runtime/Allocator.h"
39*c217d954SCole Faust #include "arm_compute/runtime/BlobLifetimeManager.h"
40*c217d954SCole Faust #include "arm_compute/runtime/IWeightsManager.h"
41*c217d954SCole Faust #include "arm_compute/runtime/MemoryGroup.h"
42*c217d954SCole Faust #include "arm_compute/runtime/MemoryManagerOnDemand.h"
43*c217d954SCole Faust #include "arm_compute/runtime/OffsetLifetimeManager.h"
44*c217d954SCole Faust #include "arm_compute/runtime/PoolManager.h"
45*c217d954SCole Faust #include "arm_compute/runtime/Scheduler.h"
46*c217d954SCole Faust
47*c217d954SCole Faust #include "support/ToolchainSupport.h"
48*c217d954SCole Faust
49*c217d954SCole Faust namespace arm_compute
50*c217d954SCole Faust {
51*c217d954SCole Faust namespace graph
52*c217d954SCole Faust {
53*c217d954SCole Faust namespace backends
54*c217d954SCole Faust {
55*c217d954SCole Faust /** Register CPU backend */
56*c217d954SCole Faust static detail::BackendRegistrar<NEDeviceBackend> NEDeviceBackend_registrar(Target::NEON);
57*c217d954SCole Faust
NEDeviceBackend()58*c217d954SCole Faust NEDeviceBackend::NEDeviceBackend()
59*c217d954SCole Faust : _allocator()
60*c217d954SCole Faust {
61*c217d954SCole Faust }
62*c217d954SCole Faust
initialize_backend()63*c217d954SCole Faust void NEDeviceBackend::initialize_backend()
64*c217d954SCole Faust {
65*c217d954SCole Faust //Nothing to do
66*c217d954SCole Faust }
67*c217d954SCole Faust
release_backend_context(GraphContext & ctx)68*c217d954SCole Faust void NEDeviceBackend::release_backend_context(GraphContext &ctx)
69*c217d954SCole Faust {
70*c217d954SCole Faust //Nothing to do
71*c217d954SCole Faust ARM_COMPUTE_UNUSED(ctx);
72*c217d954SCole Faust }
73*c217d954SCole Faust
setup_backend_context(GraphContext & ctx)74*c217d954SCole Faust void NEDeviceBackend::setup_backend_context(GraphContext &ctx)
75*c217d954SCole Faust {
76*c217d954SCole Faust // Set number of threads
77*c217d954SCole Faust if(ctx.config().num_threads >= 0)
78*c217d954SCole Faust {
79*c217d954SCole Faust Scheduler::get().set_num_threads(ctx.config().num_threads);
80*c217d954SCole Faust }
81*c217d954SCole Faust
82*c217d954SCole Faust // Create function level memory manager
83*c217d954SCole Faust if(ctx.memory_management_ctx(Target::NEON) == nullptr)
84*c217d954SCole Faust {
85*c217d954SCole Faust MemoryManagerContext mm_ctx;
86*c217d954SCole Faust mm_ctx.target = Target::NEON;
87*c217d954SCole Faust mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Offset);
88*c217d954SCole Faust mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Offset);
89*c217d954SCole Faust mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm);
90*c217d954SCole Faust mm_ctx.allocator = &_allocator;
91*c217d954SCole Faust
92*c217d954SCole Faust ctx.insert_memory_management_ctx(std::move(mm_ctx));
93*c217d954SCole Faust }
94*c217d954SCole Faust
95*c217d954SCole Faust // Create function level weights manager
96*c217d954SCole Faust if(ctx.weights_management_ctx(Target::NEON) == nullptr)
97*c217d954SCole Faust {
98*c217d954SCole Faust WeightsManagerContext wm_ctx;
99*c217d954SCole Faust wm_ctx.target = Target::NEON;
100*c217d954SCole Faust wm_ctx.wm = create_weights_manager();
101*c217d954SCole Faust
102*c217d954SCole Faust ctx.insert_weights_management_ctx(std::move(wm_ctx));
103*c217d954SCole Faust }
104*c217d954SCole Faust }
105*c217d954SCole Faust
is_backend_supported()106*c217d954SCole Faust bool NEDeviceBackend::is_backend_supported()
107*c217d954SCole Faust {
108*c217d954SCole Faust return true;
109*c217d954SCole Faust }
110*c217d954SCole Faust
backend_allocator()111*c217d954SCole Faust IAllocator *NEDeviceBackend::backend_allocator()
112*c217d954SCole Faust {
113*c217d954SCole Faust return &_allocator;
114*c217d954SCole Faust }
115*c217d954SCole Faust
create_tensor(const Tensor & tensor)116*c217d954SCole Faust std::unique_ptr<ITensorHandle> NEDeviceBackend::create_tensor(const Tensor &tensor)
117*c217d954SCole Faust {
118*c217d954SCole Faust // Get tensor descriptor
119*c217d954SCole Faust const TensorDescriptor &tensor_desc = tensor.desc();
120*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::NEON);
121*c217d954SCole Faust
122*c217d954SCole Faust // Create backend tensor handle
123*c217d954SCole Faust TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
124*c217d954SCole Faust info.set_data_layout(tensor_desc.layout);
125*c217d954SCole Faust
126*c217d954SCole Faust return std::make_unique<NETensorHandle>(info);
127*c217d954SCole Faust }
128*c217d954SCole Faust
create_subtensor(ITensorHandle * parent,TensorShape shape,Coordinates coords,bool extend_parent)129*c217d954SCole Faust std::unique_ptr<ITensorHandle> NEDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
130*c217d954SCole Faust {
131*c217d954SCole Faust if(parent == nullptr)
132*c217d954SCole Faust {
133*c217d954SCole Faust return nullptr;
134*c217d954SCole Faust }
135*c217d954SCole Faust
136*c217d954SCole Faust return std::make_unique<NESubTensorHandle>(parent, shape, coords, extend_parent);
137*c217d954SCole Faust }
138*c217d954SCole Faust
configure_node(INode & node,GraphContext & ctx)139*c217d954SCole Faust std::unique_ptr<arm_compute::IFunction> NEDeviceBackend::configure_node(INode &node, GraphContext &ctx)
140*c217d954SCole Faust {
141*c217d954SCole Faust ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CPU node with ID : " << node.id() << std::endl);
142*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
143*c217d954SCole Faust
144*c217d954SCole Faust // Configure node
145*c217d954SCole Faust return NEFunctionFactory::create(&node, ctx);
146*c217d954SCole Faust }
147*c217d954SCole Faust
validate_node(INode & node)148*c217d954SCole Faust arm_compute::Status NEDeviceBackend::validate_node(INode &node)
149*c217d954SCole Faust {
150*c217d954SCole Faust ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CPU node with ID : " << node.id() << std::endl);
151*c217d954SCole Faust ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
152*c217d954SCole Faust
153*c217d954SCole Faust return NENodeValidator::validate(&node);
154*c217d954SCole Faust }
155*c217d954SCole Faust
create_memory_manager(MemoryManagerAffinity affinity)156*c217d954SCole Faust std::shared_ptr<arm_compute::IMemoryManager> NEDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
157*c217d954SCole Faust {
158*c217d954SCole Faust std::shared_ptr<ILifetimeManager> lifetime_mgr = nullptr;
159*c217d954SCole Faust if(affinity == MemoryManagerAffinity::Buffer)
160*c217d954SCole Faust {
161*c217d954SCole Faust lifetime_mgr = std::make_shared<BlobLifetimeManager>();
162*c217d954SCole Faust }
163*c217d954SCole Faust else
164*c217d954SCole Faust {
165*c217d954SCole Faust lifetime_mgr = std::make_shared<OffsetLifetimeManager>();
166*c217d954SCole Faust }
167*c217d954SCole Faust auto pool_mgr = std::make_shared<PoolManager>();
168*c217d954SCole Faust auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
169*c217d954SCole Faust
170*c217d954SCole Faust return mm;
171*c217d954SCole Faust }
172*c217d954SCole Faust
create_weights_manager()173*c217d954SCole Faust std::shared_ptr<arm_compute::IWeightsManager> NEDeviceBackend::create_weights_manager()
174*c217d954SCole Faust {
175*c217d954SCole Faust auto weights_mgr = std::make_shared<IWeightsManager>();
176*c217d954SCole Faust return weights_mgr;
177*c217d954SCole Faust }
178*c217d954SCole Faust
sync()179*c217d954SCole Faust void NEDeviceBackend::sync()
180*c217d954SCole Faust {
181*c217d954SCole Faust // nop
182*c217d954SCole Faust }
183*c217d954SCole Faust } // namespace backends
184*c217d954SCole Faust } // namespace graph
185*c217d954SCole Faust } // namespace arm_compute
186