Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
| 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 Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 24 | #include "arm_compute/graph/backends/NEON/NEDeviceBackend.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 25 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 26 | #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 Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 36 | |
| 37 | #include "arm_compute/core/TensorInfo.h" |
| 38 | #include "arm_compute/runtime/Allocator.h" |
| 39 | #include "arm_compute/runtime/BlobLifetimeManager.h" |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 40 | #include "arm_compute/runtime/MemoryGroup.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 41 | #include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 42 | #include "arm_compute/runtime/OffsetLifetimeManager.h" |
| 43 | #include "arm_compute/runtime/PoolManager.h" |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 44 | #include "arm_compute/runtime/Scheduler.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 45 | |
| 46 | #include "support/ToolchainSupport.h" |
| 47 | |
| 48 | namespace arm_compute |
| 49 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 50 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 51 | { |
| 52 | namespace backends |
| 53 | { |
| 54 | /** Register NEON backend */ |
| 55 | static detail::BackendRegistrar<NEDeviceBackend> NEDeviceBackend_registrar(Target::NEON); |
| 56 | |
| 57 | NEDeviceBackend::NEDeviceBackend() |
| 58 | : _allocator() |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | void NEDeviceBackend::initialize_backend() |
| 63 | { |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 64 | //Nothing to do |
| 65 | } |
| 66 | |
| 67 | void NEDeviceBackend::release_backend_context(GraphContext &ctx) |
| 68 | { |
| 69 | //Nothing to do |
| 70 | ARM_COMPUTE_UNUSED(ctx); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void NEDeviceBackend::setup_backend_context(GraphContext &ctx) |
| 74 | { |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 75 | // Set number of threads |
Anthony Barbier | a1be5ba | 2018-04-10 16:53:02 +0100 | [diff] [blame] | 76 | if(ctx.config().num_threads >= 0) |
| 77 | { |
| 78 | Scheduler::get().set_num_threads(ctx.config().num_threads); |
| 79 | } |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 80 | |
| 81 | // Create function level memory manager |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 82 | if(ctx.memory_management_ctx(Target::NEON) == nullptr) |
| 83 | { |
| 84 | MemoryManagerContext mm_ctx; |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 85 | mm_ctx.target = Target::NEON; |
| 86 | mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Offset); |
| 87 | mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Offset); |
| 88 | mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 89 | |
| 90 | ctx.insert_memory_management_ctx(std::move(mm_ctx)); |
| 91 | } |
| 92 | } |
| 93 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 94 | bool NEDeviceBackend::is_backend_supported() |
| 95 | { |
| 96 | return true; |
| 97 | } |
| 98 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 99 | IAllocator *NEDeviceBackend::backend_allocator() |
| 100 | { |
| 101 | return &_allocator; |
| 102 | } |
| 103 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 104 | std::unique_ptr<ITensorHandle> NEDeviceBackend::create_tensor(const Tensor &tensor) |
| 105 | { |
| 106 | // Get tensor descriptor |
| 107 | const TensorDescriptor &tensor_desc = tensor.desc(); |
| 108 | ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::NEON); |
| 109 | |
| 110 | // Create backend tensor handle |
Giorgio Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 111 | TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info); |
Georgios Pinitas | cac13b1 | 2018-04-27 19:07:19 +0100 | [diff] [blame] | 112 | info.set_data_layout(tensor_desc.layout); |
| 113 | auto backend_tensor_handle = support::cpp14::make_unique<NETensorHandle>(info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 114 | |
| 115 | return std::move(backend_tensor_handle); |
| 116 | } |
| 117 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 118 | std::unique_ptr<ITensorHandle> NEDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 119 | { |
| 120 | if(parent == nullptr) |
| 121 | { |
| 122 | return nullptr; |
| 123 | } |
| 124 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 125 | return support::cpp14::make_unique<NESubTensorHandle>(parent, shape, coords, extend_parent); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | std::unique_ptr<arm_compute::IFunction> NEDeviceBackend::configure_node(INode &node, GraphContext &ctx) |
| 129 | { |
| 130 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring NEON node with ID : " << node.id() << std::endl); |
| 131 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON); |
| 132 | |
| 133 | // Configure node |
| 134 | return NEFunctionFactory::create(&node, ctx); |
| 135 | } |
| 136 | |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 137 | arm_compute::Status NEDeviceBackend::validate_node(INode &node) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 138 | { |
| 139 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating NEON node with ID : " << node.id() << std::endl); |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 140 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 141 | |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 142 | return NENodeValidator::validate(&node); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | std::shared_ptr<arm_compute::IMemoryManager> NEDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity) |
| 146 | { |
| 147 | std::shared_ptr<ILifetimeManager> lifetime_mgr = nullptr; |
| 148 | if(affinity == MemoryManagerAffinity::Buffer) |
| 149 | { |
| 150 | lifetime_mgr = std::make_shared<BlobLifetimeManager>(); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | lifetime_mgr = std::make_shared<OffsetLifetimeManager>(); |
| 155 | } |
| 156 | auto pool_mgr = std::make_shared<PoolManager>(); |
| 157 | auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr); |
| 158 | |
| 159 | mm->set_allocator(&_allocator); |
| 160 | |
| 161 | return mm; |
| 162 | } |
| 163 | } // namespace backends |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 164 | } // namespace graph |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 165 | } // namespace arm_compute |