blob: 5569abf41bd0c7fbfc7b04a6c557703477b815c5 [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 */
24#include "arm_compute/graph2/backends/NEON/NEDeviceBackend.h"
25
26#include "arm_compute/graph2/Graph.h"
27#include "arm_compute/graph2/GraphContext.h"
28#include "arm_compute/graph2/INode.h"
29#include "arm_compute/graph2/Logger.h"
30#include "arm_compute/graph2/Tensor.h"
31#include "arm_compute/graph2/backends/BackendRegistrar.h"
32#include "arm_compute/graph2/backends/NEON/NEFunctionFactory.h"
Georgios Pinitas28705162018-03-21 20:10:53 +000033#include "arm_compute/graph2/backends/NEON/NENodeValidator.h"
Georgios Pinitasd8734b52017-12-22 15:27:52 +000034#include "arm_compute/graph2/backends/NEON/NESubTensorHandle.h"
35#include "arm_compute/graph2/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/MemoryManagerOnDemand.h"
41#include "arm_compute/runtime/OffsetLifetimeManager.h"
42#include "arm_compute/runtime/PoolManager.h"
43
44#include "support/ToolchainSupport.h"
45
46namespace arm_compute
47{
48namespace graph2
49{
50namespace backends
51{
52/** Register NEON backend */
53static detail::BackendRegistrar<NEDeviceBackend> NEDeviceBackend_registrar(Target::NEON);
54
55NEDeviceBackend::NEDeviceBackend()
56 : _allocator()
57{
58}
59
60void NEDeviceBackend::initialize_backend()
61{
62}
63
64void NEDeviceBackend::setup_backend_context(GraphContext &ctx)
65{
66 if(ctx.memory_management_ctx(Target::NEON) == nullptr)
67 {
68 MemoryManagerContext mm_ctx;
69 mm_ctx.target = Target::NEON;
70 mm_ctx.mm = create_memory_manager(MemoryManagerAffinity::Buffer);
71
72 ctx.insert_memory_management_ctx(std::move(mm_ctx));
73 }
74}
75
76std::unique_ptr<ITensorHandle> NEDeviceBackend::create_tensor(const Tensor &tensor)
77{
78 // Get tensor descriptor
79 const TensorDescriptor &tensor_desc = tensor.desc();
80 ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::NEON);
81
82 // Create backend tensor handle
83 TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type);
84 auto backend_tensor_handle = support::cpp14::make_unique<NETensorHandle>(info);
85
86 return std::move(backend_tensor_handle);
87}
88
89std::unique_ptr<ITensorHandle> NEDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords)
90{
91 if(parent == nullptr)
92 {
93 return nullptr;
94 }
95
96 return support::cpp14::make_unique<NESubTensorHandle>(parent, shape, coords);
97}
98
99std::unique_ptr<arm_compute::IFunction> NEDeviceBackend::configure_node(INode &node, GraphContext &ctx)
100{
101 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring NEON node with ID : " << node.id() << std::endl);
102 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
103
104 // Configure node
105 return NEFunctionFactory::create(&node, ctx);
106}
107
Georgios Pinitas28705162018-03-21 20:10:53 +0000108arm_compute::Status NEDeviceBackend::validate_node(INode &node)
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000109{
110 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating NEON node with ID : " << node.id() << std::endl);
Georgios Pinitas28705162018-03-21 20:10:53 +0000111 ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000112
Georgios Pinitas28705162018-03-21 20:10:53 +0000113 return NENodeValidator::validate(&node);
Georgios Pinitasd8734b52017-12-22 15:27:52 +0000114}
115
116std::shared_ptr<arm_compute::IMemoryManager> NEDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity)
117{
118 std::shared_ptr<ILifetimeManager> lifetime_mgr = nullptr;
119 if(affinity == MemoryManagerAffinity::Buffer)
120 {
121 lifetime_mgr = std::make_shared<BlobLifetimeManager>();
122 }
123 else
124 {
125 lifetime_mgr = std::make_shared<OffsetLifetimeManager>();
126 }
127 auto pool_mgr = std::make_shared<PoolManager>();
128 auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr);
129
130 mm->set_allocator(&_allocator);
131
132 return mm;
133}
134} // namespace backends
135} // namespace graph2
136} // namespace arm_compute