Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <armnn/backends/ICustomAllocator.hpp> |
| 7 | #include <armnn/Descriptors.hpp> |
| 8 | #include <armnn/Exceptions.hpp> |
| 9 | #include <armnn/INetwork.hpp> |
| 10 | #include <armnn/IRuntime.hpp> |
| 11 | #include <armnn/Utils.hpp> |
| 12 | #include <armnn/BackendRegistry.hpp> |
Francis Murtagh | e8d7ccb | 2021-10-14 17:30:24 +0100 | [diff] [blame] | 13 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 14 | #include <cl/ClBackend.hpp> |
Francis Murtagh | b80eaff | 2021-08-16 11:59:53 +0100 | [diff] [blame] | 15 | #if defined(ARMCOMPUTENEON_ENABLED) |
| 16 | #include <neon/NeonBackend.hpp> |
| 17 | #endif |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 18 | #include <doctest/doctest.h> |
Francis Murtagh | e8d7ccb | 2021-10-14 17:30:24 +0100 | [diff] [blame] | 19 | #include <armnn/utility/IgnoreUnused.hpp> |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 20 | // Contains the OpenCl interfaces for mapping memory in the Gpu Page Tables |
| 21 | // Requires the OpenCl backend to be included (GpuAcc) |
| 22 | #include <arm_compute/core/CL/CLKernelLibrary.h> |
| 23 | #include <CL/cl_ext.h> |
| 24 | #include <arm_compute/runtime/CL/CLScheduler.h> |
| 25 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 26 | /** Sample implementation of ICustomAllocator for use with the ClBackend. |
| 27 | * Note: any memory allocated must be host accessible with write access to allow for weights and biases |
| 28 | * to be passed in. Read access is not required.. */ |
| 29 | class SampleClBackendCustomAllocator : public armnn::ICustomAllocator |
| 30 | { |
| 31 | public: |
| 32 | SampleClBackendCustomAllocator() = default; |
| 33 | |
Francis Murtagh | e8d7ccb | 2021-10-14 17:30:24 +0100 | [diff] [blame] | 34 | void* allocate(size_t size, size_t alignment) override |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 35 | { |
| 36 | // If alignment is 0 just use the CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE for alignment |
| 37 | if (alignment == 0) |
| 38 | { |
| 39 | alignment = arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>(); |
| 40 | } |
| 41 | size_t space = size + alignment + alignment; |
| 42 | auto allocatedMemPtr = std::malloc(space * sizeof(size_t)); |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 43 | if (std::align(alignment, size, allocatedMemPtr, space) == nullptr) |
| 44 | { |
| 45 | throw armnn::Exception("SampleClBackendCustomAllocator::Alignment failed"); |
| 46 | } |
| 47 | return allocatedMemPtr; |
| 48 | } |
| 49 | |
| 50 | /** Interface to be implemented by the child class to free the allocated tensor */ |
Francis Murtagh | e8d7ccb | 2021-10-14 17:30:24 +0100 | [diff] [blame] | 51 | void free(void* ptr) override |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 52 | { |
| 53 | std::free(ptr); |
| 54 | } |
| 55 | |
Francis Murtagh | e8d7ccb | 2021-10-14 17:30:24 +0100 | [diff] [blame] | 56 | armnn::MemorySource GetMemorySourceType() override |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 57 | { |
| 58 | return armnn::MemorySource::Malloc; |
| 59 | } |
| 60 | }; |
| 61 | |
Francis Murtagh | 62573b6 | 2021-08-12 11:55:21 +0100 | [diff] [blame] | 62 | armnn::INetworkPtr CreateTestNetwork(armnn::TensorInfo& inputTensorInfo) |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 63 | { |
| 64 | using namespace armnn; |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 65 | |
| 66 | armnn::FullyConnectedDescriptor fullyConnectedDesc; |
| 67 | float weightsData[] = {1.0f}; // Identity |
Cathal Corbett | 5b8093c | 2021-10-22 11:12:07 +0100 | [diff] [blame] | 68 | TensorInfo weightsInfo(TensorShape({1, 1}), DataType::Float32, 0.0f, 0, true); |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 69 | weightsInfo.SetConstant(true); |
| 70 | armnn::ConstTensor weights(weightsInfo, weightsData); |
| 71 | |
Francis Murtagh | bb6c649 | 2022-02-09 15:13:38 +0000 | [diff] [blame] | 72 | armnn::INetworkPtr network = armnn::INetwork::Create(); |
| 73 | armnn::IConnectableLayer* const inputLayer = network->AddInputLayer(0); |
| 74 | armnn::IConnectableLayer* const weightsLayer = network->AddConstantLayer(weights, "Weights"); |
| 75 | armnn::IConnectableLayer* const fullyConnectedLayer = |
| 76 | network->AddFullyConnectedLayer(fullyConnectedDesc, "fully connected"); |
| 77 | armnn::IConnectableLayer* const outputLayer = network->AddOutputLayer(0); |
| 78 | |
| 79 | inputLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(0)); |
| 80 | weightsLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(1)); |
| 81 | fullyConnectedLayer->GetOutputSlot(0).Connect(outputLayer->GetInputSlot(0)); |
| 82 | |
| 83 | weightsLayer->GetOutputSlot(0).SetTensorInfo(weightsInfo); |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 84 | |
Francis Murtagh | 62573b6 | 2021-08-12 11:55:21 +0100 | [diff] [blame] | 85 | //Set the tensors in the network. |
| 86 | |
Francis Murtagh | bb6c649 | 2022-02-09 15:13:38 +0000 | [diff] [blame] | 87 | inputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo); |
Francis Murtagh | 62573b6 | 2021-08-12 11:55:21 +0100 | [diff] [blame] | 88 | |
| 89 | TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32); |
Francis Murtagh | bb6c649 | 2022-02-09 15:13:38 +0000 | [diff] [blame] | 90 | fullyConnectedLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
Francis Murtagh | 62573b6 | 2021-08-12 11:55:21 +0100 | [diff] [blame] | 91 | |
Francis Murtagh | bb6c649 | 2022-02-09 15:13:38 +0000 | [diff] [blame] | 92 | return network; |
Francis Murtagh | 62573b6 | 2021-08-12 11:55:21 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | TEST_SUITE("ClCustomAllocatorTests") |
| 96 | { |
| 97 | |
| 98 | // This is a copy of the SimpleSample app modified to use a custom |
| 99 | // allocator for the clbackend. It creates a FullyConnected network with a single layer |
| 100 | // taking a single number as an input |
| 101 | TEST_CASE("ClCustomAllocatorTest") |
| 102 | { |
| 103 | using namespace armnn; |
| 104 | |
| 105 | float number = 3; |
| 106 | |
| 107 | // Construct ArmNN network |
| 108 | armnn::NetworkId networkIdentifier; |
| 109 | |
| 110 | TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32); |
| 111 | |
| 112 | INetworkPtr myNetwork = CreateTestNetwork(inputTensorInfo); |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 113 | |
| 114 | // Create ArmNN runtime |
| 115 | IRuntime::CreationOptions options; // default options |
| 116 | auto customAllocator = std::make_shared<SampleClBackendCustomAllocator>(); |
| 117 | options.m_CustomAllocatorMap = {{"GpuAcc", std::move(customAllocator)}}; |
| 118 | IRuntimePtr run = IRuntime::Create(options); |
| 119 | |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 120 | // Optimise ArmNN network |
| 121 | OptimizerOptions optOptions; |
| 122 | optOptions.m_ImportEnabled = true; |
Francis Murtagh | 626bd90 | 2022-06-21 13:16:23 +0000 | [diff] [blame] | 123 | optOptions.m_ExportEnabled = true; |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 124 | armnn::IOptimizedNetworkPtr optNet = Optimize(*myNetwork, {"GpuAcc"}, run->GetDeviceSpec(), optOptions); |
| 125 | CHECK(optNet); |
| 126 | |
| 127 | // Load graph into runtime |
| 128 | std::string ignoredErrorMessage; |
| 129 | INetworkProperties networkProperties(false, MemorySource::Malloc, MemorySource::Malloc); |
| 130 | run->LoadNetwork(networkIdentifier, std::move(optNet), ignoredErrorMessage, networkProperties); |
| 131 | |
| 132 | // Creates structures for input & output |
| 133 | unsigned int numElements = inputTensorInfo.GetNumElements(); |
| 134 | size_t totalBytes = numElements * sizeof(float); |
| 135 | |
| 136 | const size_t alignment = |
| 137 | arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>(); |
| 138 | |
| 139 | void* alignedInputPtr = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, alignment); |
| 140 | |
| 141 | // Input with negative values |
| 142 | auto* inputPtr = reinterpret_cast<float*>(alignedInputPtr); |
| 143 | std::fill_n(inputPtr, numElements, number); |
| 144 | |
| 145 | void* alignedOutputPtr = options.m_CustomAllocatorMap["GpuAcc"]->allocate(totalBytes, alignment); |
| 146 | auto* outputPtr = reinterpret_cast<float*>(alignedOutputPtr); |
| 147 | std::fill_n(outputPtr, numElements, -10.0f); |
| 148 | |
Cathal Corbett | 5b8093c | 2021-10-22 11:12:07 +0100 | [diff] [blame] | 149 | armnn::TensorInfo inputTensorInfo2 = run->GetInputTensorInfo(networkIdentifier, 0); |
| 150 | inputTensorInfo2.SetConstant(true); |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 151 | armnn::InputTensors inputTensors |
| 152 | { |
Cathal Corbett | 5b8093c | 2021-10-22 11:12:07 +0100 | [diff] [blame] | 153 | {0, armnn::ConstTensor(inputTensorInfo2, alignedInputPtr)}, |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 154 | }; |
| 155 | armnn::OutputTensors outputTensors |
| 156 | { |
| 157 | {0, armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 0), alignedOutputPtr)} |
| 158 | }; |
| 159 | |
| 160 | // Execute network |
| 161 | run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors); |
| 162 | run->UnloadNetwork(networkIdentifier); |
| 163 | |
| 164 | |
| 165 | // Tell the CLBackend to sync memory so we can read the output. |
| 166 | arm_compute::CLScheduler::get().sync(); |
| 167 | auto* outputResult = reinterpret_cast<float*>(alignedOutputPtr); |
| 168 | |
| 169 | run->UnloadNetwork(networkIdentifier); |
| 170 | CHECK(outputResult[0] == number); |
| 171 | auto& backendRegistry = armnn::BackendRegistryInstance(); |
| 172 | backendRegistry.DeregisterAllocator(ClBackend::GetIdStatic()); |
| 173 | } |
| 174 | |
Francis Murtagh | b80eaff | 2021-08-16 11:59:53 +0100 | [diff] [blame] | 175 | // Only run this test if NEON is enabled |
| 176 | #if defined(ARMCOMPUTENEON_ENABLED) |
| 177 | |
Francis Murtagh | 62573b6 | 2021-08-12 11:55:21 +0100 | [diff] [blame] | 178 | TEST_CASE("ClCustomAllocatorCpuAccNegativeTest") |
| 179 | { |
| 180 | using namespace armnn; |
| 181 | |
| 182 | // Create ArmNN runtime |
| 183 | IRuntime::CreationOptions options; // default options |
| 184 | auto customAllocator = std::make_shared<SampleClBackendCustomAllocator>(); |
| 185 | options.m_CustomAllocatorMap = {{"CpuAcc", std::move(customAllocator)}}; |
| 186 | IRuntimePtr run = IRuntime::Create(options); |
Francis Murtagh | 62573b6 | 2021-08-12 11:55:21 +0100 | [diff] [blame] | 187 | TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32); |
| 188 | INetworkPtr myNetwork = CreateTestNetwork(inputTensorInfo); |
| 189 | |
| 190 | // Optimise ArmNN network |
| 191 | OptimizerOptions optOptions; |
| 192 | optOptions.m_ImportEnabled = true; |
| 193 | IOptimizedNetworkPtr optNet(nullptr, nullptr); |
| 194 | std::vector<std::string> errMessages; |
| 195 | |
Colm Donelan | 380c1a0 | 2021-08-17 00:52:23 +0100 | [diff] [blame] | 196 | CHECK_THROWS_AS_MESSAGE(Optimize(*myNetwork, {"CpuAcc"}, run->GetDeviceSpec(), optOptions, errMessages), |
| 197 | armnn::InvalidArgumentException, |
| 198 | "Expected an exception as GetAvailablePreferredBackends() should be empty in Optimize()."); |
Francis Murtagh | 62573b6 | 2021-08-12 11:55:21 +0100 | [diff] [blame] | 199 | |
| 200 | auto& backendRegistry = armnn::BackendRegistryInstance(); |
Francis Murtagh | b80eaff | 2021-08-16 11:59:53 +0100 | [diff] [blame] | 201 | backendRegistry.DeregisterAllocator(NeonBackend::GetIdStatic()); |
Francis Murtagh | 62573b6 | 2021-08-12 11:55:21 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Francis Murtagh | b80eaff | 2021-08-16 11:59:53 +0100 | [diff] [blame] | 204 | #endif |
Francis Murtagh | 62573b6 | 2021-08-12 11:55:21 +0100 | [diff] [blame] | 205 | |
Colm Donelan | 380c1a0 | 2021-08-17 00:52:23 +0100 | [diff] [blame] | 206 | TEST_CASE("ClCustomAllocatorGpuAccNullptrTest") |
| 207 | { |
| 208 | using namespace armnn; |
| 209 | |
| 210 | // Create ArmNN runtime |
| 211 | IRuntime::CreationOptions options; // default options |
| 212 | auto customAllocator = std::make_shared<SampleClBackendCustomAllocator>(); |
| 213 | options.m_CustomAllocatorMap = {{"GpuAcc", nullptr}}; |
| 214 | |
| 215 | CHECK_THROWS_AS_MESSAGE(IRuntimePtr run = IRuntime::Create(options), |
| 216 | armnn::Exception, |
| 217 | "Expected exception in RuntimeImpl::RuntimeImpl() as allocator was nullptr."); |
| 218 | } |
| 219 | |
Francis Murtagh | bb6c649 | 2022-02-09 15:13:38 +0000 | [diff] [blame] | 220 | } // test suite ClCustomAllocatorTests |