Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 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" |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 40 | #include "arm_compute/runtime/IWeightsManager.h" |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 41 | #include "arm_compute/runtime/MemoryGroup.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 42 | #include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 43 | #include "arm_compute/runtime/OffsetLifetimeManager.h" |
| 44 | #include "arm_compute/runtime/PoolManager.h" |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 45 | #include "arm_compute/runtime/Scheduler.h" |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 46 | |
| 47 | #include "support/ToolchainSupport.h" |
| 48 | |
| 49 | namespace arm_compute |
| 50 | { |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 51 | namespace graph |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 52 | { |
| 53 | namespace backends |
| 54 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 55 | /** Register CPU backend */ |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 56 | static detail::BackendRegistrar<NEDeviceBackend> NEDeviceBackend_registrar(Target::NEON); |
| 57 | |
| 58 | NEDeviceBackend::NEDeviceBackend() |
| 59 | : _allocator() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | void NEDeviceBackend::initialize_backend() |
| 64 | { |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 65 | //Nothing to do |
| 66 | } |
| 67 | |
| 68 | void NEDeviceBackend::release_backend_context(GraphContext &ctx) |
| 69 | { |
| 70 | //Nothing to do |
| 71 | ARM_COMPUTE_UNUSED(ctx); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void NEDeviceBackend::setup_backend_context(GraphContext &ctx) |
| 75 | { |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 76 | // Set number of threads |
Anthony Barbier | a1be5ba | 2018-04-10 16:53:02 +0100 | [diff] [blame] | 77 | if(ctx.config().num_threads >= 0) |
| 78 | { |
| 79 | Scheduler::get().set_num_threads(ctx.config().num_threads); |
| 80 | } |
Georgios Pinitas | 9a8c672 | 2018-03-21 17:52:35 +0000 | [diff] [blame] | 81 | |
| 82 | // Create function level memory manager |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 83 | if(ctx.memory_management_ctx(Target::NEON) == nullptr) |
| 84 | { |
| 85 | MemoryManagerContext mm_ctx; |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 86 | mm_ctx.target = Target::NEON; |
Georgios Pinitas | 734151d | 2019-01-21 18:38:21 +0000 | [diff] [blame] | 87 | mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Offset); |
| 88 | mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Offset); |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 89 | mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm); |
Georgios Pinitas | 9da19e9 | 2018-10-11 15:33:11 +0100 | [diff] [blame] | 90 | mm_ctx.allocator = &_allocator; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 91 | |
| 92 | ctx.insert_memory_management_ctx(std::move(mm_ctx)); |
| 93 | } |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 94 | |
| 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 Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 106 | bool NEDeviceBackend::is_backend_supported() |
| 107 | { |
| 108 | return true; |
| 109 | } |
| 110 | |
Georgios Pinitas | 3d1489d | 2018-05-03 20:47:16 +0100 | [diff] [blame] | 111 | IAllocator *NEDeviceBackend::backend_allocator() |
| 112 | { |
| 113 | return &_allocator; |
| 114 | } |
| 115 | |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 116 | std::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 Arena | bb54e4e | 2018-04-05 17:20:34 +0100 | [diff] [blame] | 123 | 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] | 124 | info.set_data_layout(tensor_desc.layout); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 125 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 126 | return std::make_unique<NETensorHandle>(info); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Georgios Pinitas | ee33ea5 | 2018-03-08 16:01:29 +0000 | [diff] [blame] | 129 | 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] | 130 | { |
| 131 | if(parent == nullptr) |
| 132 | { |
| 133 | return nullptr; |
| 134 | } |
| 135 | |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame] | 136 | return std::make_unique<NESubTensorHandle>(parent, shape, coords, extend_parent); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | std::unique_ptr<arm_compute::IFunction> NEDeviceBackend::configure_node(INode &node, GraphContext &ctx) |
| 140 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 141 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CPU node with ID : " << node.id() << std::endl); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 142 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON); |
| 143 | |
| 144 | // Configure node |
| 145 | return NEFunctionFactory::create(&node, ctx); |
| 146 | } |
| 147 | |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 148 | arm_compute::Status NEDeviceBackend::validate_node(INode &node) |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 149 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 150 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CPU node with ID : " << node.id() << std::endl); |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 151 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 152 | |
Georgios Pinitas | 2870516 | 2018-03-21 20:10:53 +0000 | [diff] [blame] | 153 | return NENodeValidator::validate(&node); |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | std::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 Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 170 | return mm; |
| 171 | } |
Michalis Spyrou | 1a569a3 | 2019-09-10 17:20:34 +0100 | [diff] [blame] | 172 | |
| 173 | std::shared_ptr<arm_compute::IWeightsManager> NEDeviceBackend::create_weights_manager() |
| 174 | { |
| 175 | auto weights_mgr = std::make_shared<IWeightsManager>(); |
| 176 | return weights_mgr; |
| 177 | } |
Giorgio Arena | 9c67d38 | 2021-08-20 15:24:03 +0100 | [diff] [blame] | 178 | |
| 179 | void NEDeviceBackend::sync() |
| 180 | { |
| 181 | // nop |
| 182 | } |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 183 | } // namespace backends |
Georgios Pinitas | d9eb275 | 2018-04-03 13:44:29 +0100 | [diff] [blame] | 184 | } // namespace graph |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 185 | } // namespace arm_compute |