arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | #pragma once |
| 6 | |
Matteo Martincigh | e5b8eb9 | 2019-11-28 15:45:42 +0000 | [diff] [blame] | 7 | #include <armnn/backends/IBackendInternal.hpp> |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 8 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 9 | #include <arm_compute/core/Types.h> |
| 10 | #include <arm_compute/runtime/CL/CLBufferAllocator.h> |
| 11 | |
| 12 | #include <aclCommon/BaseMemoryManager.hpp> |
| 13 | #include <arm_compute/runtime/CL/CLMemoryRegion.h> |
| 14 | |
| 15 | #include <arm_compute/core/CL/CLKernelLibrary.h> |
| 16 | #include <CL/cl_ext.h> |
| 17 | |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 18 | // System includes for mapping and unmapping memory |
| 19 | #include <sys/mman.h> |
| 20 | |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 21 | namespace armnn |
| 22 | { |
| 23 | |
Finn Williams | b9af86e | 2021-05-26 18:38:12 +0100 | [diff] [blame] | 24 | // add new capabilities here.. |
| 25 | const BackendCapabilities gpuAccCapabilities("GpuAcc", |
| 26 | { |
| 27 | {"NonConstWeights", false}, |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 28 | {"AsyncExecution", false}, |
Finn Williams | dbf5f31 | 2021-08-26 11:08:01 +0100 | [diff] [blame] | 29 | {"ProtectedContentAllocation", true}, |
| 30 | {"ConstantTensorsAsInputs", false}, |
Sadik Armagan | c840263 | 2021-09-10 11:40:54 +0100 | [diff] [blame] | 31 | {"PreImportIOTensors", false}, |
Finn Williams | b1aad42 | 2021-10-28 19:07:32 +0100 | [diff] [blame] | 32 | {"ExternallyManagedMemory", true}, |
Sadik Armagan | b8a26d8 | 2021-10-04 15:13:11 +0100 | [diff] [blame] | 33 | {"MultiAxisPacking", false}, |
| 34 | {"SingleAxisPacking", true} |
Finn Williams | b9af86e | 2021-05-26 18:38:12 +0100 | [diff] [blame] | 35 | }); |
Sadik Armagan | aede8ca | 2021-03-31 16:12:13 +0100 | [diff] [blame] | 36 | |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 37 | class ClBackend : public IBackendInternal |
| 38 | { |
| 39 | public: |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 40 | ClBackend() : m_CustomAllocator(nullptr) {}; |
| 41 | ClBackend(std::shared_ptr<ICustomAllocator> allocator) |
| 42 | { |
| 43 | std::string err; |
| 44 | UseCustomMemoryAllocator(allocator, err); |
| 45 | } |
David Beck | 9efb57d | 2018-11-05 13:40:33 +0000 | [diff] [blame] | 46 | ~ClBackend() = default; |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 47 | |
David Beck | 3cc9a62 | 2018-10-12 10:38:31 +0100 | [diff] [blame] | 48 | static const BackendId& GetIdStatic(); |
| 49 | const BackendId& GetId() const override { return GetIdStatic(); } |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 50 | |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 51 | IBackendInternal::IMemoryManagerUniquePtr CreateMemoryManager() const override; |
| 52 | |
| 53 | IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory( |
| 54 | const IBackendInternal::IMemoryManagerSharedPtr& memoryManager = nullptr) const override; |
| 55 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 56 | IBackendInternal::IWorkloadFactoryPtr CreateWorkloadFactory( |
| 57 | TensorHandleFactoryRegistry& registry) const override; |
| 58 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 59 | IWorkloadFactoryPtr CreateWorkloadFactory(const IMemoryManagerSharedPtr& memoryManager, |
| 60 | const ModelOptions& modelOptions) const override; |
Sadik Armagan | 04a7297 | 2020-09-14 15:44:18 +0100 | [diff] [blame] | 61 | |
| 62 | IWorkloadFactoryPtr CreateWorkloadFactory(class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry, |
| 63 | const ModelOptions& modelOptions) const override; |
| 64 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 65 | IWorkloadFactoryPtr CreateWorkloadFactory(class TensorHandleFactoryRegistry& tensorHandleFactoryRegistry, |
| 66 | const ModelOptions& modelOptions, |
| 67 | MemorySourceFlags inputFlags, |
| 68 | MemorySourceFlags outputFlags) const override; |
| 69 | |
Jan Eilers | e9f0f0f | 2019-08-16 10:28:37 +0100 | [diff] [blame] | 70 | std::vector<ITensorHandleFactory::FactoryId> GetHandleFactoryPreferences() const override; |
| 71 | |
| 72 | void RegisterTensorHandleFactories(TensorHandleFactoryRegistry& registry) override; |
| 73 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 74 | void RegisterTensorHandleFactories(TensorHandleFactoryRegistry& registry, |
| 75 | MemorySourceFlags inputFlags, |
| 76 | MemorySourceFlags outputFlags) override; |
| 77 | |
David Beck | 1b61be5 | 2018-11-08 09:19:14 +0000 | [diff] [blame] | 78 | IBackendInternal::IBackendContextPtr CreateBackendContext(const IRuntime::CreationOptions&) const override; |
Colm Donelan | 1aff393 | 2020-02-05 17:48:59 +0000 | [diff] [blame] | 79 | IBackendInternal::IBackendProfilingContextPtr CreateBackendProfilingContext( |
| 80 | const IRuntime::CreationOptions&, IBackendProfilingPtr& backendProfiling) override; |
Aron Virginas-Tar | 5605519 | 2018-11-12 18:10:43 +0000 | [diff] [blame] | 81 | |
David Beck | 111b5d9 | 2018-11-12 14:59:37 +0000 | [diff] [blame] | 82 | IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override; |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 83 | IBackendInternal::ILayerSupportSharedPtr GetLayerSupport(const ModelOptions& modelOptions) const override; |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 84 | |
Mike Kelly | 07810fc | 2020-11-12 10:58:48 +0000 | [diff] [blame] | 85 | OptimizationViews OptimizeSubgraphView(const SubgraphView& subgraph, |
| 86 | const ModelOptions& modelOptions) const override; |
Sadik Armagan | 045f6be | 2020-09-10 13:37:32 +0100 | [diff] [blame] | 87 | |
| 88 | IBackendInternal::IBackendSpecificModelContextPtr CreateBackendSpecificModelContext( |
| 89 | const ModelOptions& modelOptions) const override; |
Sadik Armagan | aede8ca | 2021-03-31 16:12:13 +0100 | [diff] [blame] | 90 | |
David Monahan | 6642b8a | 2021-11-04 16:31:46 +0000 | [diff] [blame] | 91 | std::unique_ptr<ICustomAllocator> GetDefaultAllocator() const override; |
| 92 | |
Finn Williams | b9af86e | 2021-05-26 18:38:12 +0100 | [diff] [blame] | 93 | BackendCapabilities GetCapabilities() const override |
| 94 | { |
| 95 | return gpuAccCapabilities; |
| 96 | }; |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 97 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 98 | virtual bool UseCustomMemoryAllocator(std::shared_ptr<ICustomAllocator> allocator, |
| 99 | armnn::Optional<std::string&> errMsg) override |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 100 | { |
| 101 | IgnoreUnused(errMsg); |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 102 | ARMNN_LOG(info) << "Using Custom Allocator for ClBackend"; |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 103 | |
| 104 | // Set flag to signal the backend to use a custom memory allocator |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 105 | m_CustomAllocator = std::make_shared<ClBackendCustomAllocatorWrapper>(std::move(allocator)); |
| 106 | m_UsingCustomAllocator = true; |
| 107 | return m_UsingCustomAllocator; |
Jan Eilers | 15fcc7e | 2021-07-14 13:50:15 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Sadik Armagan | b7851f9 | 2021-10-06 16:37:02 +0100 | [diff] [blame] | 110 | virtual unsigned int GetNumberOfCacheFiles() const override { return 1; } |
| 111 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 112 | // Cl requires a arm_compute::IAllocator we wrap the Arm NN ICustomAllocator to achieve this |
| 113 | class ClBackendCustomAllocatorWrapper : public arm_compute::IAllocator |
| 114 | { |
| 115 | public: |
| 116 | ClBackendCustomAllocatorWrapper(std::shared_ptr<ICustomAllocator> alloc) : m_CustomAllocator(alloc) |
| 117 | {} |
| 118 | // Inherited methods overridden: |
| 119 | void* allocate(size_t size, size_t alignment) override |
| 120 | { |
| 121 | auto alloc = m_CustomAllocator->allocate(size, alignment); |
| 122 | return MapAllocatedMemory(alloc, size, m_CustomAllocator->GetMemorySourceType()); |
| 123 | } |
| 124 | void free(void* ptr) override |
| 125 | { |
| 126 | auto hostMemPtr = m_AllocatedBufferMappings[ptr]; |
| 127 | clReleaseMemObject(static_cast<cl_mem>(ptr)); |
| 128 | m_CustomAllocator->free(hostMemPtr); |
| 129 | } |
| 130 | std::unique_ptr<arm_compute::IMemoryRegion> make_region(size_t size, size_t alignment) override |
| 131 | { |
| 132 | auto hostMemPtr = m_CustomAllocator->allocate(size, alignment); |
| 133 | cl_mem buffer = MapAllocatedMemory(hostMemPtr, size, m_CustomAllocator->GetMemorySourceType()); |
| 134 | |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 135 | return std::make_unique<ClBackendCustomAllocatorMemoryRegion>(cl::Buffer(buffer), |
| 136 | hostMemPtr, |
| 137 | m_CustomAllocator->GetMemorySourceType()); |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 138 | } |
| 139 | private: |
| 140 | cl_mem MapAllocatedMemory(void* memory, size_t size, MemorySource source) |
| 141 | { |
| 142 | // Round the size of the buffer to a multiple of the CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE |
| 143 | auto cachelineAlignment = |
| 144 | arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>(); |
| 145 | auto roundedSize = cachelineAlignment + size - (size % cachelineAlignment); |
| 146 | |
| 147 | if (source == MemorySource::Malloc) |
| 148 | { |
| 149 | const cl_import_properties_arm importProperties[] = |
| 150 | { |
Francis Murtagh | 9db96e0 | 2021-08-13 16:15:09 +0100 | [diff] [blame] | 151 | CL_IMPORT_TYPE_ARM, |
| 152 | CL_IMPORT_TYPE_HOST_ARM, |
| 153 | 0 |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 154 | }; |
| 155 | cl_int error = CL_SUCCESS; |
| 156 | cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().get(), |
| 157 | CL_MEM_READ_WRITE, |
| 158 | importProperties, |
| 159 | memory, |
| 160 | roundedSize, |
| 161 | &error); |
| 162 | if (error == CL_SUCCESS) |
| 163 | { |
| 164 | m_AllocatedBufferMappings.insert(std::make_pair(static_cast<void *>(buffer), memory)); |
| 165 | return buffer; |
| 166 | } |
| 167 | throw armnn::Exception( |
| 168 | "Mapping allocated memory from CustomMemoryAllocator failed, errcode: " + std::to_string(error)); |
| 169 | } |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 170 | else if (source == MemorySource::DmaBuf) |
| 171 | { |
| 172 | const cl_import_properties_arm importProperties[] = |
| 173 | { |
Francis Murtagh | 9db96e0 | 2021-08-13 16:15:09 +0100 | [diff] [blame] | 174 | CL_IMPORT_TYPE_ARM, |
| 175 | CL_IMPORT_TYPE_DMA_BUF_ARM, |
| 176 | CL_IMPORT_DMA_BUF_DATA_CONSISTENCY_WITH_HOST_ARM, |
| 177 | CL_TRUE, |
| 178 | 0 |
| 179 | }; |
| 180 | cl_int error = CL_SUCCESS; |
| 181 | cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().get(), |
| 182 | CL_MEM_READ_WRITE, |
| 183 | importProperties, |
| 184 | memory, |
| 185 | roundedSize, |
| 186 | &error); |
| 187 | if (error == CL_SUCCESS) |
| 188 | { |
| 189 | m_AllocatedBufferMappings.insert(std::make_pair(static_cast<void *>(buffer), memory)); |
| 190 | return buffer; |
| 191 | } |
| 192 | throw armnn::Exception( |
| 193 | "Mapping allocated memory from CustomMemoryAllocator failed, errcode: " |
| 194 | + std::to_string(error)); |
| 195 | } |
| 196 | else if (source == MemorySource::DmaBufProtected) |
| 197 | { |
| 198 | const cl_import_properties_arm importProperties[] = |
| 199 | { |
| 200 | CL_IMPORT_TYPE_ARM, |
| 201 | CL_IMPORT_TYPE_DMA_BUF_ARM, |
| 202 | CL_IMPORT_TYPE_PROTECTED_ARM, |
| 203 | CL_TRUE, |
| 204 | 0 |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 205 | }; |
| 206 | cl_int error = CL_SUCCESS; |
| 207 | cl_mem buffer = clImportMemoryARM(arm_compute::CLKernelLibrary::get().context().get(), |
| 208 | CL_MEM_READ_WRITE, |
| 209 | importProperties, |
| 210 | memory, |
| 211 | roundedSize, |
| 212 | &error); |
| 213 | if (error == CL_SUCCESS) |
| 214 | { |
| 215 | m_AllocatedBufferMappings.insert(std::make_pair(static_cast<void *>(buffer), memory)); |
| 216 | return buffer; |
| 217 | } |
| 218 | throw armnn::Exception( |
| 219 | "Mapping allocated memory from CustomMemoryAllocator failed, errcode: " |
| 220 | + std::to_string(error)); |
| 221 | } |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 222 | throw armnn::Exception( |
| 223 | "Attempting to allocate memory with unsupported MemorySource type in CustomAllocator"); |
| 224 | } |
| 225 | std::shared_ptr<ICustomAllocator> m_CustomAllocator; |
| 226 | std::map<void*, void*> m_AllocatedBufferMappings; |
| 227 | }; |
| 228 | |
| 229 | class ClBackendCustomAllocatorMemoryRegion : public arm_compute::ICLMemoryRegion |
| 230 | { |
| 231 | public: |
| 232 | // We need to have a new version of ICLMemoryRegion which holds a hostMemPtr to allow for cpu copy access |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 233 | ClBackendCustomAllocatorMemoryRegion(const cl::Buffer &buffer, void* hostMemPtr, armnn::MemorySource source) |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 234 | : ICLMemoryRegion(buffer.getInfo<CL_MEM_SIZE>()) |
| 235 | { |
| 236 | _mem = buffer; |
| 237 | m_HostMemPtr = hostMemPtr; |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 238 | m_MemorySource = source; |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Inherited methods overridden : |
| 242 | void* ptr() override |
| 243 | { |
| 244 | return nullptr; |
| 245 | } |
| 246 | |
| 247 | void* map(cl::CommandQueue &q, bool blocking) override |
| 248 | { |
| 249 | armnn::IgnoreUnused(q, blocking); |
| 250 | if (m_HostMemPtr == nullptr) |
| 251 | { |
| 252 | throw armnn::Exception("ClBackend: Attempting to map memory with an invalid host ptr"); |
| 253 | } |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 254 | if (_mapping != nullptr) |
| 255 | { |
| 256 | throw armnn::Exception("ClBackend: Attempting to map memory which has not yet been unmapped"); |
| 257 | } |
| 258 | switch (m_MemorySource) |
| 259 | { |
| 260 | case armnn::MemorySource::Malloc: |
| 261 | _mapping = m_HostMemPtr; |
| 262 | return _mapping; |
| 263 | break; |
| 264 | case armnn::MemorySource::DmaBuf: |
Francis Murtagh | 9db96e0 | 2021-08-13 16:15:09 +0100 | [diff] [blame] | 265 | case armnn::MemorySource::DmaBufProtected: |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 266 | // If the source is a Dmabuf then the memory ptr should be pointing to an integer value for the fd |
| 267 | _mapping = mmap(NULL, _size, PROT_WRITE, MAP_SHARED, *(reinterpret_cast<int*>(m_HostMemPtr)), 0); |
| 268 | return _mapping; |
| 269 | break; |
| 270 | default: |
| 271 | throw armnn::Exception("ClBackend: Attempting to map imported memory without a valid source"); |
| 272 | break; |
| 273 | } |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void unmap(cl::CommandQueue &q) override |
| 277 | { |
| 278 | armnn::IgnoreUnused(q); |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 279 | switch (m_MemorySource) |
| 280 | { |
| 281 | case armnn::MemorySource::Malloc: |
| 282 | _mapping = nullptr; |
| 283 | break; |
| 284 | case armnn::MemorySource::DmaBuf: |
Francis Murtagh | 9db96e0 | 2021-08-13 16:15:09 +0100 | [diff] [blame] | 285 | case armnn::MemorySource::DmaBufProtected: |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 286 | munmap(_mapping, _size); |
| 287 | _mapping = nullptr; |
| 288 | break; |
| 289 | default: |
| 290 | throw armnn::Exception("ClBackend: Attempting to unmap imported memory without a valid source"); |
| 291 | break; |
| 292 | } |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 293 | } |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 294 | private: |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 295 | void* m_HostMemPtr = nullptr; |
David Monahan | e9b1230 | 2021-08-12 10:20:21 +0100 | [diff] [blame] | 296 | armnn::MemorySource m_MemorySource; |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | std::shared_ptr<ClBackendCustomAllocatorWrapper> m_CustomAllocator; |
| 300 | bool m_UsingCustomAllocator = false; |
arovir01 | 4424b0a | 2018-10-04 10:46:04 +0100 | [diff] [blame] | 301 | }; |
| 302 | |
Matteo Martincigh | adddddb | 2019-01-24 14:06:23 +0000 | [diff] [blame] | 303 | } // namespace armnn |