David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <arm_compute/runtime/CL/functions/CLActivationLayer.h> |
| 7 | |
| 8 | #include <cl/ClImportTensorHandle.hpp> |
| 9 | #include <cl/ClImportTensorHandleFactory.hpp> |
| 10 | #include <cl/test/ClContextControlFixture.hpp> |
| 11 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 12 | #include <doctest/doctest.h> |
| 13 | |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 14 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 15 | #include <armnn/IRuntime.hpp> |
| 16 | #include <armnn/INetwork.hpp> |
| 17 | |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 18 | using namespace armnn; |
| 19 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 20 | TEST_SUITE("ClImportTensorHandleTests") |
| 21 | { |
| 22 | TEST_CASE_FIXTURE(ClContextControlFixture, "ClMallocImport") |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 23 | { |
| 24 | ClImportTensorHandleFactory handleFactory(static_cast<MemorySourceFlags>(MemorySource::Malloc), |
| 25 | static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 26 | |
| 27 | TensorInfo info({ 1, 24, 16, 3 }, DataType::Float32); |
| 28 | unsigned int numElements = info.GetNumElements(); |
| 29 | |
| 30 | // create TensorHandle for memory import |
| 31 | auto handle = handleFactory.CreateTensorHandle(info); |
| 32 | |
| 33 | // Get CLtensor |
| 34 | arm_compute::CLTensor& tensor = PolymorphicDowncast<ClImportTensorHandle*>(handle.get())->GetTensor(); |
| 35 | |
| 36 | // Create and configure activation function |
| 37 | const arm_compute::ActivationLayerInfo act_info(arm_compute::ActivationLayerInfo::ActivationFunction::RELU); |
| 38 | arm_compute::CLActivationLayer act_func; |
| 39 | act_func.configure(&tensor, nullptr, act_info); |
| 40 | |
| 41 | // Allocate user memory |
| 42 | const size_t totalBytes = tensor.info()->total_size(); |
| 43 | const size_t alignment = |
| 44 | arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>(); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 45 | size_t space = totalBytes + alignment + alignment; |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 46 | auto testData = std::make_unique<uint8_t[]>(space); |
| 47 | void* alignedPtr = testData.get(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 48 | CHECK(std::align(alignment, totalBytes, alignedPtr, space)); |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 49 | |
| 50 | // Import memory |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 51 | CHECK(handle->Import(alignedPtr, armnn::MemorySource::Malloc)); |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 52 | |
| 53 | // Input with negative values |
| 54 | auto* typedPtr = reinterpret_cast<float*>(alignedPtr); |
| 55 | std::fill_n(typedPtr, numElements, -5.0f); |
| 56 | |
| 57 | // Execute function and sync |
| 58 | act_func.run(); |
| 59 | arm_compute::CLScheduler::get().sync(); |
| 60 | |
| 61 | // Validate result by checking that the output has no negative values |
| 62 | for(unsigned int i = 0; i < numElements; ++i) |
| 63 | { |
Jan Eilers | c1c872f | 2021-07-22 13:17:04 +0100 | [diff] [blame] | 64 | CHECK(typedPtr[i] == 0); |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 68 | TEST_CASE_FIXTURE(ClContextControlFixture, "ClIncorrectMemorySourceImport") |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 69 | { |
| 70 | ClImportTensorHandleFactory handleFactory(static_cast<MemorySourceFlags>(MemorySource::Malloc), |
| 71 | static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 72 | |
| 73 | TensorInfo info({ 1, 24, 16, 3 }, DataType::Float32); |
| 74 | |
| 75 | // create TensorHandle for memory import |
| 76 | auto handle = handleFactory.CreateTensorHandle(info); |
| 77 | |
| 78 | // Get CLtensor |
| 79 | arm_compute::CLTensor& tensor = PolymorphicDowncast<ClImportTensorHandle*>(handle.get())->GetTensor(); |
| 80 | |
| 81 | // Allocate user memory |
| 82 | const size_t totalBytes = tensor.info()->total_size(); |
| 83 | const size_t alignment = |
| 84 | arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>(); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 85 | size_t space = totalBytes + alignment + alignment; |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 86 | auto testData = std::make_unique<uint8_t[]>(space); |
| 87 | void* alignedPtr = testData.get(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 88 | CHECK(std::align(alignment, totalBytes, alignedPtr, space)); |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 89 | |
| 90 | // Import memory |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 91 | CHECK_THROWS_AS(handle->Import(alignedPtr, armnn::MemorySource::Undefined), MemoryImportException); |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 94 | TEST_CASE_FIXTURE(ClContextControlFixture, "ClInvalidMemorySourceImport") |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 95 | { |
| 96 | MemorySource invalidMemSource = static_cast<MemorySource>(256); |
| 97 | ClImportTensorHandleFactory handleFactory(static_cast<MemorySourceFlags>(invalidMemSource), |
| 98 | static_cast<MemorySourceFlags>(invalidMemSource)); |
| 99 | |
| 100 | TensorInfo info({ 1, 2, 2, 1 }, DataType::Float32); |
| 101 | |
| 102 | // create TensorHandle for memory import |
| 103 | auto handle = handleFactory.CreateTensorHandle(info); |
| 104 | |
| 105 | // Allocate user memory |
| 106 | std::vector<float> inputData |
| 107 | { |
| 108 | 1.0f, 2.0f, 3.0f, 4.0f |
| 109 | }; |
| 110 | |
| 111 | // Import non-support memory |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 112 | CHECK_THROWS_AS(handle->Import(inputData.data(), invalidMemSource), MemoryImportException); |
David Monahan | e4a41dc | 2021-04-14 16:55:36 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 115 | TEST_CASE_FIXTURE(ClContextControlFixture, "ClImportEndToEnd") |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 116 | { |
| 117 | // Create runtime in which test will run |
| 118 | IRuntime::CreationOptions options; |
| 119 | IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
| 120 | |
| 121 | // build up the structure of the network |
| 122 | INetworkPtr net(INetwork::Create()); |
| 123 | |
| 124 | IConnectableLayer* input = net->AddInputLayer(0, "Input"); |
| 125 | |
| 126 | ActivationDescriptor descriptor; |
| 127 | descriptor.m_Function = ActivationFunction::ReLu; |
| 128 | IConnectableLayer* activation = net->AddActivationLayer(descriptor, "Activation"); |
| 129 | |
| 130 | IConnectableLayer* output = net->AddOutputLayer(0, "Output"); |
| 131 | |
| 132 | input->GetOutputSlot(0).Connect(activation->GetInputSlot(0)); |
| 133 | activation->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 134 | |
| 135 | TensorInfo tensorInfo = TensorInfo({ 1, 24, 16, 3 }, DataType::Float32); |
| 136 | unsigned int numElements = tensorInfo.GetNumElements(); |
| 137 | size_t totalBytes = numElements * sizeof(float); |
| 138 | |
| 139 | input->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 140 | activation->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 141 | |
| 142 | // Optimize the network |
| 143 | OptimizerOptions optOptions; |
| 144 | optOptions.m_ImportEnabled = true; |
| 145 | std::vector<armnn::BackendId> backends = {armnn::Compute::GpuAcc}; |
| 146 | IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec(), optOptions); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 147 | CHECK(optNet); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 148 | |
| 149 | // Loads it into the runtime. |
| 150 | NetworkId netId; |
| 151 | std::string ignoredErrorMessage; |
| 152 | // Enable Importing |
| 153 | INetworkProperties networkProperties(false, MemorySource::Malloc, MemorySource::Malloc); |
| 154 | runtime->LoadNetwork(netId, std::move(optNet), ignoredErrorMessage, networkProperties); |
| 155 | |
| 156 | // Creates structures for input & output |
| 157 | const size_t alignment = |
| 158 | arm_compute::CLKernelLibrary::get().get_device().getInfo<CL_DEVICE_GLOBAL_MEM_CACHELINE_SIZE>(); |
| 159 | size_t space = totalBytes + alignment + alignment; |
| 160 | auto inputData = std::make_unique<uint8_t[]>(space); |
| 161 | void* alignedInputPtr = inputData.get(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 162 | CHECK(std::align(alignment, totalBytes, alignedInputPtr, space)); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 163 | |
| 164 | // Input with negative values |
| 165 | auto* intputPtr = reinterpret_cast<float*>(alignedInputPtr); |
| 166 | std::fill_n(intputPtr, numElements, -5.0f); |
| 167 | |
| 168 | auto outputData = std::make_unique<uint8_t[]>(space); |
| 169 | void* alignedOutputPtr = outputData.get(); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 170 | CHECK(std::align(alignment, totalBytes, alignedOutputPtr, space)); |
Narumol Prangnawarat | 878e0f9 | 2021-05-11 19:51:14 +0100 | [diff] [blame] | 171 | auto* outputPtr = reinterpret_cast<float*>(alignedOutputPtr); |
| 172 | std::fill_n(outputPtr, numElements, -10.0f); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 173 | |
| 174 | InputTensors inputTensors |
| 175 | { |
| 176 | {0,armnn::ConstTensor(runtime->GetInputTensorInfo(netId, 0), alignedInputPtr)}, |
| 177 | }; |
| 178 | OutputTensors outputTensors |
| 179 | { |
| 180 | {0,armnn::Tensor(runtime->GetOutputTensorInfo(netId, 0), alignedOutputPtr)} |
| 181 | }; |
| 182 | |
| 183 | runtime->GetProfiler(netId)->EnableProfiling(true); |
| 184 | |
| 185 | // Do the inference |
| 186 | runtime->EnqueueWorkload(netId, inputTensors, outputTensors); |
| 187 | |
| 188 | // Retrieve the Profiler.Print() output to get the workload execution |
| 189 | ProfilerManager& profilerManager = armnn::ProfilerManager::GetInstance(); |
| 190 | std::stringstream ss; |
| 191 | profilerManager.GetProfiler()->Print(ss);; |
| 192 | std::string dump = ss.str(); |
| 193 | |
| 194 | // Contains ActivationWorkload |
| 195 | std::size_t found = dump.find("ActivationWorkload"); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 196 | CHECK(found != std::string::npos); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 197 | |
| 198 | // Contains SyncMemGeneric |
| 199 | found = dump.find("SyncMemGeneric"); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 200 | CHECK(found != std::string::npos); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 201 | |
| 202 | // Does not contain CopyMemGeneric |
| 203 | found = dump.find("CopyMemGeneric"); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 204 | CHECK(found == std::string::npos); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 205 | |
Narumol Prangnawarat | 878e0f9 | 2021-05-11 19:51:14 +0100 | [diff] [blame] | 206 | runtime->UnloadNetwork(netId); |
| 207 | |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 208 | // Check output is as expected |
| 209 | // Validate result by checking that the output has no negative values |
| 210 | auto* outputResult = reinterpret_cast<float*>(alignedOutputPtr); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 211 | CHECK(outputResult); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 212 | for(unsigned int i = 0; i < numElements; ++i) |
| 213 | { |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 214 | CHECK(outputResult[i] >= 0); |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 215 | } |
Narumol Prangnawarat | e5f0b24 | 2021-05-07 17:52:36 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 218 | } |