blob: 7c2db402609b45c8f15674b5b7c6c44927d51c14 [file] [log] [blame]
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001/*
2 * Copyright (c) 2018 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 */
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"
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010040#include "arm_compute/runtime/MemoryGroup.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000041#include "arm_compute/runtime/MemoryManagerOnDemand.h"
42#include "arm_compute/runtime/OffsetLifetimeManager.h"
43#include "arm_compute/runtime/PoolManager.h"
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000044#include "arm_compute/runtime/Scheduler.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000045
46#include "support/ToolchainSupport.h"
47
48namespace arm_compute
49{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010050namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000051{
52namespace backends
53{
54/** Register NEON backend */
55static detail::BackendRegistrar<NEDeviceBackend> NEDeviceBackend_registrar(Target::NEON);
56
57NEDeviceBackend::NEDeviceBackend()
58 : _allocator()
59{
60}
61
62void NEDeviceBackend::initialize_backend()
63{
64}
65
66void NEDeviceBackend::setup_backend_context(GraphContext &ctx)
67{
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000068 // Set number of threads
Anthony Barbiera1be5ba2018-04-10 16:53:02 +010069 if(ctx.config().num_threads >= 0)
70 {
71 Scheduler::get().set_num_threads(ctx.config().num_threads);
72 }
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000073
74 // Create function level memory manager
Georgios Pinitasd8734b52017-12-22 15:27:52 +000075 if(ctx.memory_management_ctx(Target::NEON) == nullptr)
76 {
77 MemoryManagerContext mm_ctx;
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010078 mm_ctx.target = Target::NEON;
79 mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Offset);
80 mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Offset);
81 mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000082
83 ctx.insert_memory_management_ctx(std::move(mm_ctx));
84 }
85}
86
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010087bool NEDeviceBackend::is_backend_supported()
88{
89 return true;
90}
91
Georgios Pinitas3d1489d2018-05-03 20:47:16 +010092IAllocator *NEDeviceBackend::backend_allocator()
93{
94 return &_allocator;
95}
96
Georgios Pinitasd8734b52017-12-22 15:27:52 +000097std::unique_ptr<ITensorHandle> NEDeviceBackend::create_tensor(const Tensor &tensor)
98{
99 // Get tensor descriptor
100 const TensorDescriptor &tensor_desc = tensor.desc();
101 ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::NEON);
102
103 // Create backend tensor handle
Giorgio Arenabb54e4e2018-04-05 17:20:34 +0100104 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
Georgios Pinitascac13b12018-04-27 19:07:19 +0100105 info.set_data_layout(tensor_desc.layout);
106 auto backend_tensor_handle = support::cpp14::make_unique<NETensorHandle>(info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000107
108 return std::move(backend_tensor_handle);
109}
110
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000111std::unique_ptr<ITensorHandle> NEDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000112{
113 if(parent == nullptr)
114 {
115 return nullptr;
116 }
117
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000118 return support::cpp14::make_unique<NESubTensorHandle>(parent, shape, coords, extend_parent);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000119}
120
121std::unique_ptr<arm_compute::IFunction> NEDeviceBackend::configure_node(INode &node, GraphContext &ctx)
122{
123 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring NEON node with ID : " << node.id() << std::endl);
124 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
125
126 // Configure node
127 return NEFunctionFactory::create(&node, ctx);
128}
129
Georgios Pinitas28705162018-03-21 20:10:53 +0000130arm_compute::Status NEDeviceBackend::validate_node(INode &node)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000131{
132 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating NEON node with ID : " << node.id() << std::endl);
Georgios Pinitas28705162018-03-21 20:10:53 +0000133 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000134
Georgios Pinitas28705162018-03-21 20:10:53 +0000135 return NENodeValidator::validate(&node);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000136}
137
138std::shared_ptr<arm_compute::IMemoryManager> NEDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
139{
140 std::shared_ptr<ILifetimeManager> lifetime_mgr = nullptr;
141 if(affinity == MemoryManagerAffinity::Buffer)
142 {
143 lifetime_mgr = std::make_shared<BlobLifetimeManager>();
144 }
145 else
146 {
147 lifetime_mgr = std::make_shared<OffsetLifetimeManager>();
148 }
149 auto pool_mgr = std::make_shared<PoolManager>();
150 auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
151
152 mm->set_allocator(&_allocator);
153
154 return mm;
155}
156} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100157} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000158} // namespace arm_compute