Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 1 | /* |
Jenkins | 6a7771e | 2020-05-28 11:28:36 +0100 | [diff] [blame^] | 2 | * Copyright (c) 2018-2020 ARM Limited. |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [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 | */ |
| 24 | #include "arm_compute/graph/backends/CL/CLDeviceBackend.h" |
| 25 | |
| 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/CL/CLFunctionFactory.h" |
| 33 | #include "arm_compute/graph/backends/CL/CLNodeValidator.h" |
| 34 | #include "arm_compute/graph/backends/CL/CLSubTensorHandle.h" |
| 35 | #include "arm_compute/graph/backends/CL/CLTensorHandle.h" |
| 36 | |
Jenkins | 0e205f7 | 2019-11-28 16:53:35 +0000 | [diff] [blame] | 37 | #include "arm_compute/core/CL/CLCoreRuntimeContext.h" |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 38 | #include "arm_compute/core/TensorInfo.h" |
| 39 | #include "arm_compute/runtime/BlobLifetimeManager.h" |
| 40 | #include "arm_compute/runtime/CL/CLBufferAllocator.h" |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 41 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Jenkins | 0e205f7 | 2019-11-28 16:53:35 +0000 | [diff] [blame] | 42 | #include "arm_compute/runtime/IWeightsManager.h" |
| 43 | #include "arm_compute/runtime/MemoryGroup.h" |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 44 | #include "arm_compute/runtime/MemoryManagerOnDemand.h" |
| 45 | #include "arm_compute/runtime/PoolManager.h" |
| 46 | |
| 47 | #include "support/ToolchainSupport.h" |
| 48 | |
| 49 | namespace arm_compute |
| 50 | { |
| 51 | namespace graph |
| 52 | { |
| 53 | namespace backends |
| 54 | { |
| 55 | namespace |
| 56 | { |
| 57 | bool file_exists(const std::string &filename) |
| 58 | { |
| 59 | std::ifstream file(filename); |
| 60 | return file.good(); |
| 61 | } |
| 62 | } // namespace |
| 63 | |
| 64 | /** Register CL backend */ |
| 65 | static detail::BackendRegistrar<CLDeviceBackend> CLDeviceBackend_registrar(Target::CL); |
| 66 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 67 | CLDeviceBackend::CLDeviceBackend() |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 68 | : _context_count(0), _tuner(), _allocator(nullptr), _tuner_file() |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 69 | { |
| 70 | } |
| 71 | |
| 72 | CLDeviceBackend::~CLDeviceBackend() |
| 73 | { |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 74 | // TODO (geopin01) : Shouldn't call non exception safe stuff here |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 75 | if(_tuner.tune_new_kernels() && !_tuner.lws_table().empty() && !_tuner_file.empty()) |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 76 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 77 | _tuner.save_to_file(_tuner_file); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | void CLDeviceBackend::set_kernel_tuning(bool enable_tuning) |
| 82 | { |
| 83 | _tuner.set_tune_new_kernels(enable_tuning); |
| 84 | } |
| 85 | |
Jenkins | 4ba87db | 2019-05-23 17:11:51 +0100 | [diff] [blame] | 86 | void CLDeviceBackend::set_kernel_tuning_mode(CLTunerMode tuning_mode) |
| 87 | { |
| 88 | _tuner.set_tuner_mode(tuning_mode); |
| 89 | } |
| 90 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 91 | void CLDeviceBackend::initialize_backend() |
| 92 | { |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 93 | // Setup Scheduler |
| 94 | CLScheduler::get().default_init(&_tuner); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 95 | // Create allocator with new context |
Jenkins | 0e205f7 | 2019-11-28 16:53:35 +0000 | [diff] [blame] | 96 | _allocator = support::cpp14::make_unique<CLBufferAllocator>(nullptr /* legacy path for CLCoreRuntimeContext */); |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void CLDeviceBackend::release_backend_context(GraphContext &ctx) |
| 100 | { |
| 101 | ARM_COMPUTE_UNUSED(ctx); |
| 102 | _context_count--; |
| 103 | if(_context_count == 0) // No more context using the backend: free resources |
| 104 | { |
| 105 | _allocator = nullptr; |
| 106 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void CLDeviceBackend::setup_backend_context(GraphContext &ctx) |
| 110 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 111 | // Force backend initialization |
| 112 | _context_count++; |
| 113 | if(_context_count == 1) |
| 114 | { |
| 115 | initialize_backend(); |
| 116 | } |
| 117 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 118 | // Setup tuner |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 119 | _tuner_file = ctx.config().tuner_file; |
| 120 | // Load tuner data if available |
| 121 | if(file_exists(_tuner_file)) |
| 122 | { |
| 123 | _tuner.load_from_file(_tuner_file); |
| 124 | } |
| 125 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 126 | set_kernel_tuning(ctx.config().use_tuner); |
Jenkins | 4ba87db | 2019-05-23 17:11:51 +0100 | [diff] [blame] | 127 | set_kernel_tuning_mode(ctx.config().tuner_mode); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 128 | |
| 129 | // Setup a management backend |
| 130 | if(ctx.memory_management_ctx(Target::CL) == nullptr) |
| 131 | { |
| 132 | MemoryManagerContext mm_ctx; |
| 133 | mm_ctx.target = Target::CL; |
| 134 | mm_ctx.intra_mm = create_memory_manager(MemoryManagerAffinity::Buffer); |
| 135 | mm_ctx.cross_mm = create_memory_manager(MemoryManagerAffinity::Buffer); |
Jenkins | 0e205f7 | 2019-11-28 16:53:35 +0000 | [diff] [blame] | 136 | mm_ctx.cross_group = std::make_shared<MemoryGroup>(mm_ctx.cross_mm); |
Jenkins | b9abeae | 2018-11-22 11:58:08 +0000 | [diff] [blame] | 137 | mm_ctx.allocator = _allocator.get(); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 138 | |
| 139 | ctx.insert_memory_management_ctx(std::move(mm_ctx)); |
| 140 | } |
Jenkins | 0e205f7 | 2019-11-28 16:53:35 +0000 | [diff] [blame] | 141 | |
| 142 | // Create function level weights manager |
| 143 | if(ctx.weights_management_ctx(Target::CL) == nullptr) |
| 144 | { |
| 145 | WeightsManagerContext wm_ctx; |
| 146 | wm_ctx.target = Target::CL; |
| 147 | wm_ctx.wm = create_weights_manager(); |
| 148 | |
| 149 | ctx.insert_weights_management_ctx(std::move(wm_ctx)); |
| 150 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool CLDeviceBackend::is_backend_supported() |
| 154 | { |
| 155 | return arm_compute::opencl_is_available(); |
| 156 | } |
| 157 | |
| 158 | IAllocator *CLDeviceBackend::backend_allocator() |
| 159 | { |
Jenkins | 52ba29e | 2018-08-29 15:32:11 +0000 | [diff] [blame] | 160 | return _allocator.get(); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | std::unique_ptr<ITensorHandle> CLDeviceBackend::create_tensor(const Tensor &tensor) |
| 164 | { |
| 165 | // Get tensor descriptor |
| 166 | const TensorDescriptor &tensor_desc = tensor.desc(); |
| 167 | ARM_COMPUTE_ERROR_ON(tensor_desc.target != Target::CL); |
| 168 | |
| 169 | // Create backend tensor handle |
| 170 | TensorInfo info(tensor_desc.shape, 1, tensor_desc.data_type, tensor_desc.quant_info); |
| 171 | info.set_data_layout(tensor_desc.layout); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 172 | |
Jenkins | 6a7771e | 2020-05-28 11:28:36 +0100 | [diff] [blame^] | 173 | return support::cpp14::make_unique<CLTensorHandle>(info); |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | std::unique_ptr<ITensorHandle> CLDeviceBackend::create_subtensor(ITensorHandle *parent, TensorShape shape, Coordinates coords, bool extend_parent) |
| 177 | { |
| 178 | if(parent == nullptr) |
| 179 | { |
| 180 | return nullptr; |
| 181 | } |
| 182 | |
| 183 | return support::cpp14::make_unique<CLSubTensorHandle>(parent, shape, coords, extend_parent); |
| 184 | } |
| 185 | |
| 186 | std::unique_ptr<arm_compute::IFunction> CLDeviceBackend::configure_node(INode &node, GraphContext &ctx) |
| 187 | { |
| 188 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Configuring CL node with ID : " << node.id() << std::endl); |
| 189 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL); |
| 190 | |
| 191 | // Configure node |
| 192 | return CLFunctionFactory::create(&node, ctx); |
| 193 | } |
| 194 | |
| 195 | arm_compute::Status CLDeviceBackend::validate_node(INode &node) |
| 196 | { |
| 197 | ARM_COMPUTE_LOG_GRAPH_VERBOSE("Validating CL node with ID : " << node.id() << std::endl); |
| 198 | ARM_COMPUTE_ERROR_ON(node.assigned_target() != Target::CL); |
| 199 | |
| 200 | return CLNodeValidator::validate(&node); |
| 201 | } |
| 202 | |
| 203 | std::shared_ptr<arm_compute::IMemoryManager> CLDeviceBackend::create_memory_manager(MemoryManagerAffinity affinity) |
| 204 | { |
| 205 | if(affinity == MemoryManagerAffinity::Offset) |
| 206 | { |
| 207 | ARM_COMPUTE_LOG_GRAPH_WARNING("CL Backend does not support offset affinity memory management!"); |
| 208 | return nullptr; |
| 209 | } |
| 210 | |
| 211 | auto lifetime_mgr = std::make_shared<BlobLifetimeManager>(); |
| 212 | auto pool_mgr = std::make_shared<PoolManager>(); |
| 213 | auto mm = std::make_shared<MemoryManagerOnDemand>(lifetime_mgr, pool_mgr); |
| 214 | |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 215 | return mm; |
| 216 | } |
Jenkins | 0e205f7 | 2019-11-28 16:53:35 +0000 | [diff] [blame] | 217 | |
| 218 | std::shared_ptr<arm_compute::IWeightsManager> CLDeviceBackend::create_weights_manager() |
| 219 | { |
| 220 | auto weights_mgr = std::make_shared<IWeightsManager>(); |
| 221 | return weights_mgr; |
| 222 | } |
Jenkins | b3a371b | 2018-05-23 11:36:53 +0100 | [diff] [blame] | 223 | } // namespace backends |
| 224 | } // namespace graph |
| 225 | } // namespace arm_compute |