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 | */ |
| 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" |
| 33 | #include "arm_compute/graph2/backends/NEON/NESubTensorHandle.h" |
| 34 | #include "arm_compute/graph2/backends/NEON/NETensorHandle.h" |
| 35 | |
| 36 | #include "arm_compute/core/TensorInfo.h" |
| 37 | #include "arm_compute/runtime/Allocator.h" |
| 38 | #include "arm_compute/runtime/BlobLifetimeManager.h" |
| 39 | #include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 40 | #include "arm_compute/runtime/OffsetLifetimeManager.h" |
| 41 | #include "arm_compute/runtime/PoolManager.h" |
| 42 | |
| 43 | #include "support/ToolchainSupport.h" |
| 44 | |
| 45 | namespace arm_compute |
| 46 | { |
| 47 | namespace graph2 |
| 48 | { |
| 49 | namespace backends |
| 50 | { |
| 51 | /** Register NEON backend */ |
| 52 | static detail::BackendRegistrar<NEDeviceBackend> NEDeviceBackend_registrar(Target::NEON); |
| 53 | |
| 54 | NEDeviceBackend::NEDeviceBackend() |
| 55 | : _allocator() |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | void NEDeviceBackend::initialize_backend() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | void NEDeviceBackend::setup_backend_context(GraphContext &ctx) |
| 64 | { |
| 65 | if(ctx.memory_management_ctx(Target::NEON) == nullptr) |
| 66 | { |
| 67 | MemoryManagerContext mm_ctx; |
| 68 | mm_ctx.target = Target::NEON; |
| 69 | mm_ctx.mm = create_memory_manager(MemoryManagerAffinity::Buffer); |
| 70 | |
| 71 | ctx.insert_memory_management_ctx(std::move(mm_ctx)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | std::unique_ptr<ITensorHandle> NEDeviceBackend::create_tensor(const Tensor &tensor) |
| 76 | { |
| 77 | // Get tensor descriptor |
| 78 | const TensorDescriptor &tensor_desc = tensor.desc(); |
| 79 | ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::NEON); |
| 80 | |
| 81 | // Create backend tensor handle |
| 82 | TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type); |
| 83 | auto backend_tensor_handle = support::cpp14::make_unique<NETensorHandle>(info); |
| 84 | |
| 85 | return std::move(backend_tensor_handle); |
| 86 | } |
| 87 | |
| 88 | std::unique_ptr<ITensorHandle> NEDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords) |
| 89 | { |
| 90 | if(parent == nullptr) |
| 91 | { |
| 92 | return nullptr; |
| 93 | } |
| 94 | |
| 95 | return support::cpp14::make_unique<NESubTensorHandle>(parent, shape, coords); |
| 96 | } |
| 97 | |
| 98 | std::unique_ptr<arm_compute::IFunction> NEDeviceBackend::configure_node(INode &node, GraphContext &ctx) |
| 99 | { |
| 100 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring NEON node with ID : " << node.id() << std::endl); |
| 101 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::NEON); |
| 102 | |
| 103 | // Configure node |
| 104 | return NEFunctionFactory::create(&node, ctx); |
| 105 | } |
| 106 | |
| 107 | arm_compute::Status NEDeviceBackend::validate_node(const INode &node) |
| 108 | { |
| 109 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating NEON node with ID : " << node.id() << std::endl); |
| 110 | ARM_COMPUTE_UNUSED(node); |
| 111 | |
| 112 | return Status{}; |
| 113 | } |
| 114 | |
| 115 | std::shared_ptr<arm_compute::IMemoryManager> NEDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity) |
| 116 | { |
| 117 | std::shared_ptr<ILifetimeManager> lifetime_mgr = nullptr; |
| 118 | if(affinity == MemoryManagerAffinity::Buffer) |
| 119 | { |
| 120 | lifetime_mgr = std::make_shared<BlobLifetimeManager>(); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | lifetime_mgr = std::make_shared<OffsetLifetimeManager>(); |
| 125 | } |
| 126 | auto pool_mgr = std::make_shared<PoolManager>(); |
| 127 | auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr); |
| 128 | |
| 129 | mm->set_allocator(&_allocator); |
| 130 | |
| 131 | return mm; |
| 132 | } |
| 133 | } // namespace backends |
| 134 | } // namespace graph2 |
| 135 | } // namespace arm_compute |