blob: 47ef2c239443c0e60e228f8ff828735f9cd0c052 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2018-2021 Arm Limited.
Georgios Pinitasd8734b52017-12-22 15:27:52 +00003 *
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 */
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010024#include "arm_compute/graph/backends/NEON/NEDeviceBackend.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000025
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010026#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"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000036
37#include "arm_compute/core/TensorInfo.h"
38#include "arm_compute/runtime/Allocator.h"
39#include "arm_compute/runtime/BlobLifetimeManager.h"
Michalis Spyrou1a569a32019-09-10 17:20:34 +010040#include "arm_compute/runtime/IWeightsManager.h"
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010041#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000042#include "arm_compute/runtime/MemoryManagerOnDemand.h"
43#include "arm_compute/runtime/OffsetLifetimeManager.h"
44#include "arm_compute/runtime/PoolManager.h"
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000045#include "arm_compute/runtime/Scheduler.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000046
47#include "support/ToolchainSupport.h"
48
49namespace arm_compute
50{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010051namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000052{
53namespace backends
54{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000055/** Register CPU backend */
Georgios Pinitasd8734b52017-12-22 15:27:52 +000056static detail::BackendRegistrar<NEDeviceBackend> NEDeviceBackend_registrar(Target::NEON);
57
58NEDeviceBackend::NEDeviceBackend()
59 : _allocator()
60{
61}
62
63void NEDeviceBackend::initialize_backend()
64{
Anthony Barbierb6eb3532018-08-08 13:20:04 +010065 //Nothing to do
66}
67
68void NEDeviceBackend::release_backend_context(GraphContext &ctx)
69{
70 //Nothing to do
71 ARM_COMPUTE_UNUSED(ctx);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000072}
73
74void NEDeviceBackend::setup_backend_context(GraphContext &ctx)
75{
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000076 // Set number of threads
Anthony Barbiera1be5ba2018-04-10 16:53:02 +010077 if(ctx.config().num_threads >= 0)
78 {
79 Scheduler::get().set_num_threads(ctx.config().num_threads);
80 }
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000081
82 // Create function level memory manager
Georgios Pinitasd8734b52017-12-22 15:27:52 +000083 if(ctx.memory_management_ctx(Target::NEON) == nullptr)
84 {
85 MemoryManagerContext mm_ctx;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010086 mm_ctx.target = Target::NEON;
Georgios Pinitas734151d2019-01-21 18:38:21 +000087 mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Offset);
88 mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Offset);
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010089 mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm);
Georgios Pinitas9da19e92018-10-11 15:33:11 +010090 mm_ctx.allocator = &_allocator;
Georgios Pinitasd8734b52017-12-22 15:27:52 +000091
92 ctx.insert_memory_management_ctx(std::move(mm_ctx));
93 }
Michalis Spyrou1a569a32019-09-10 17:20:34 +010094
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 }
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000104}
105
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100106bool NEDeviceBackend::is_backend_supported()
107{
108 return true;
109}
110
Georgios Pinitas3d1489d2018-05-03 20:47:16 +0100111IAllocator *NEDeviceBackend::backend_allocator()
112{
113 return &_allocator;
114}
115
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000116std::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
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100123 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100124 info.set_data_layout(tensor_desc.layout);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000125
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000126 return std::make_unique<NETensorHandle>(info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000127}
128
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000129std::unique_ptr<ITensorHandle> NEDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000130{
131 if(parent == nullptr)
132 {
133 return nullptr;
134 }
135
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000136 return std::make_unique<NESubTensorHandle>(parent, shape, coords, extend_parent);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000137}
138
139std::unique_ptr<arm_compute::IFunction> NEDeviceBackend::configure_node(INode &node, GraphContext &ctx)
140{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000141 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CPU node with ID : " << node.id() << std::endl);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000142 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
143
144 // Configure node
145 return NEFunctionFactory::create(&node, ctx);
146}
147
Georgios Pinitas28705162018-03-21 20:10:53 +0000148arm_compute::Status NEDeviceBackend::validate_node(INode &node)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000149{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000150 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CPU node with ID : " << node.id() << std::endl);
Georgios Pinitas28705162018-03-21 20:10:53 +0000151 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000152
Georgios Pinitas28705162018-03-21 20:10:53 +0000153 return NENodeValidator::validate(&node);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000154}
155
156std::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
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000170 return mm;
171}
Michalis Spyrou1a569a32019-09-10 17:20:34 +0100172
173std::shared_ptr<arm_compute::IWeightsManager> NEDeviceBackend::create_weights_manager()
174{
175 auto weights_mgr = std::make_shared<IWeightsManager>();
176 return weights_mgr;
177}
Giorgio Arena9c67d382021-08-20 15:24:03 +0100178
179void NEDeviceBackend::sync()
180{
181 // nop
182}
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000183} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100184} // namespace graph
Anthony Barbierb6eb3532018-08-08 13:20:04 +0100185} // namespace arm_compute