blob: 912319654092eff6a3a880568fb65d2339e96732 [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"
40#include "arm_compute/runtime/MemoryManagerOnDemand.h"
41#include "arm_compute/runtime/OffsetLifetimeManager.h"
42#include "arm_compute/runtime/PoolManager.h"
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000043#include "arm_compute/runtime/Scheduler.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000044
45#include "support/ToolchainSupport.h"
46
47namespace arm_compute
48{
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010049namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +000050{
51namespace backends
52{
53/** Register NEON backend */
54static detail::BackendRegistrar<NEDeviceBackend> NEDeviceBackend_registrar(Target::NEON);
55
56NEDeviceBackend::NEDeviceBackend()
57 : _allocator()
58{
59}
60
61void NEDeviceBackend::initialize_backend()
62{
63}
64
65void NEDeviceBackend::setup_backend_context(GraphContext &ctx)
66{
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000067 // Set number of threads
Anthony Barbiera1be5ba2018-04-10 16:53:02 +010068 if(ctx.config().num_threads >= 0)
69 {
70 Scheduler::get().set_num_threads(ctx.config().num_threads);
71 }
Georgios Pinitas9a8c6722018-03-21 17:52:35 +000072
73 // Create function level memory manager
Georgios Pinitasd8734b52017-12-22 15:27:52 +000074 if(ctx.memory_management_ctx(Target::NEON) == nullptr)
75 {
76 MemoryManagerContext mm_ctx;
77 mm_ctx.target = Target::NEON;
78 mm_ctx.mm = create_memory_manager(MemoryManagerAffinity::Buffer);
79
80 ctx.insert_memory_management_ctx(std::move(mm_ctx));
81 }
82}
83
Georgios Pinitasd9eb2752018-04-03 13:44:29 +010084bool NEDeviceBackend::is_backend_supported()
85{
86 return true;
87}
88
Georgios Pinitasd8734b52017-12-22 15:27:52 +000089std::unique_ptr<ITensorHandle> NEDeviceBackend::create_tensor(const Tensor &tensor)
90{
91 // Get tensor descriptor
92 const TensorDescriptor &tensor_desc = tensor.desc();
93 ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::NEON);
94
95 // Create backend tensor handle
Giorgio Arenabb54e4e2018-04-05 17:20:34 +010096 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info);
Georgios Pinitasd8734b52017-12-22 15:27:52 +000097 auto backend_tensor_handle = support::cpp14::make_unique<NETensorHandle>(info);
98
99 return std::move(backend_tensor_handle);
100}
101
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000102std::unique_ptr<ITensorHandle> NEDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000103{
104 if(parent == nullptr)
105 {
106 return nullptr;
107 }
108
Georgios Pinitasee33ea52018-03-08 16:01:29 +0000109 return support::cpp14::make_unique<NESubTensorHandle>(parent, shape, coords, extend_parent);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000110}
111
112std::unique_ptr<arm_compute::IFunction> NEDeviceBackend::configure_node(INode &node, GraphContext &ctx)
113{
114 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring NEON node with ID : " << node.id() << std::endl);
115 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
116
117 // Configure node
118 return NEFunctionFactory::create(&node, ctx);
119}
120
Georgios Pinitas28705162018-03-21 20:10:53 +0000121arm_compute::Status NEDeviceBackend::validate_node(INode &node)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000122{
123 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating NEON node with ID : " << node.id() << std::endl);
Georgios Pinitas28705162018-03-21 20:10:53 +0000124 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000125
Georgios Pinitas28705162018-03-21 20:10:53 +0000126 return NENodeValidator::validate(&node);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000127}
128
129std::shared_ptr<arm_compute::IMemoryManager> NEDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
130{
131 std::shared_ptr<ILifetimeManager> lifetime_mgr = nullptr;
132 if(affinity == MemoryManagerAffinity::Buffer)
133 {
134 lifetime_mgr = std::make_shared<BlobLifetimeManager>();
135 }
136 else
137 {
138 lifetime_mgr = std::make_shared<OffsetLifetimeManager>();
139 }
140 auto pool_mgr = std::make_shared<PoolManager>();
141 auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
142
143 mm->set_allocator(&_allocator);
144
145 return mm;
146}
147} // namespace backends
Georgios Pinitasd9eb2752018-04-03 13:44:29 +0100148} // namespace graph
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000149} // namespace arm_compute