Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 1 | // |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 2 | // Copyright © 2019 Arm Ltd and Contributors. All rights reserved. |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 6 | #include <Filesystem.hpp> |
Aron Virginas-Tar | 8bf442e | 2019-11-07 18:41:40 +0000 | [diff] [blame] | 7 | #include <ProfilingService.hpp> |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 8 | #include "ProfilingTestUtils.hpp" |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 9 | #include "PrintPacketHeaderHandler.hpp" |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 10 | #include <Runtime.hpp> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 11 | #include "TestTimelinePacketHandler.hpp" |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 12 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 13 | #include <doctest/doctest.h> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 14 | |
Nikhil Raj | 77fe76b | 2021-06-09 14:55:32 +0100 | [diff] [blame] | 15 | #include <common/include/LabelsAndEventClasses.hpp> |
| 16 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 17 | #include <cstdio> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 18 | #include <sstream> |
| 19 | #include <sys/stat.h> |
| 20 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 21 | using namespace armnn::profiling; |
| 22 | using namespace armnn; |
| 23 | |
| 24 | using namespace std::chrono_literals; |
| 25 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 26 | class FileOnlyHelperService : public ProfilingService |
| 27 | { |
| 28 | public: |
| 29 | // Wait for a notification from the send thread |
| 30 | bool WaitForPacketsSent(uint32_t timeout = 1000) |
| 31 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 32 | return ProfilingService::WaitForPacketSent(m_ProfilingService, timeout); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 33 | } |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 34 | armnn::profiling::ProfilingService m_ProfilingService; |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 37 | TEST_SUITE("FileOnlyProfilingDecoratorTests") |
| 38 | { |
| 39 | TEST_CASE("TestFileOnlyProfiling") |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 40 | { |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 41 | // Get all registered backends |
| 42 | std::vector<BackendId> suitableBackends = GetSuitableBackendRegistered(); |
| 43 | |
| 44 | // Run test for each backend separately |
| 45 | for (auto const& backend : suitableBackends) |
Finn Williams | 0c32ccf | 2020-05-12 13:37:06 +0100 | [diff] [blame] | 46 | { |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 47 | // Enable m_FileOnly but also provide ILocalPacketHandler which should consume the packets. |
| 48 | // This won't dump anything to file. |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 49 | armnn::IRuntime::CreationOptions creationOptions; |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 50 | creationOptions.m_ProfilingOptions.m_EnableProfiling = true; |
| 51 | creationOptions.m_ProfilingOptions.m_FileOnly = true; |
| 52 | creationOptions.m_ProfilingOptions.m_CapturePeriod = 100; |
| 53 | creationOptions.m_ProfilingOptions.m_TimelineEnabled = true; |
| 54 | ILocalPacketHandlerSharedPtr localPacketHandlerPtr = std::make_shared<TestTimelinePacketHandler>(); |
| 55 | creationOptions.m_ProfilingOptions.m_LocalPacketHandlers.push_back(localPacketHandlerPtr); |
| 56 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 57 | armnn::RuntimeImpl runtime(creationOptions); |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 58 | // ensure the GUID generator is reset to zero |
| 59 | GetProfilingService(&runtime).ResetGuidGenerator(); |
| 60 | |
| 61 | // Load a simple network |
| 62 | // build up the structure of the network |
| 63 | INetworkPtr net(INetwork::Create()); |
| 64 | |
| 65 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 66 | |
| 67 | ElementwiseUnaryDescriptor descriptor(UnaryOperation::Rsqrt); |
| 68 | IConnectableLayer* Rsqrt = net->AddElementwiseUnaryLayer(descriptor, "Rsqrt"); |
| 69 | |
| 70 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 71 | |
| 72 | input->GetOutputSlot(0).Connect(Rsqrt->GetInputSlot(0)); |
| 73 | Rsqrt->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 74 | |
| 75 | input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 76 | Rsqrt->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 77 | |
| 78 | std::vector<armnn::BackendId> backendsVec {backend}; |
| 79 | IOptimizedNetworkPtr optNet = Optimize(*net, backendsVec, runtime.GetDeviceSpec()); |
| 80 | |
| 81 | // Load it into the runtime. It should succeed. |
| 82 | armnn::NetworkId netId; |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 83 | CHECK(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success); |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 84 | |
| 85 | // Creates structures for input & output. |
| 86 | std::vector<float> inputData(16); |
| 87 | std::vector<float> outputData(16); |
| 88 | for (unsigned int i = 0; i < 16; ++i) { |
| 89 | inputData[i] = 9.0; |
| 90 | outputData[i] = 3.0; |
| 91 | } |
| 92 | |
| 93 | InputTensors inputTensors |
| 94 | { |
| 95 | {0, ConstTensor(runtime.GetInputTensorInfo(netId, 0), inputData.data())} |
| 96 | }; |
| 97 | OutputTensors outputTensors |
| 98 | { |
| 99 | {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())} |
| 100 | }; |
| 101 | |
| 102 | // Does the inference. |
| 103 | runtime.EnqueueWorkload(netId, inputTensors, outputTensors); |
| 104 | |
| 105 | static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000); |
| 106 | |
| 107 | const TimelineModel &model = |
| 108 | static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->GetTimelineModel(); |
| 109 | |
| 110 | for (auto &error : model.GetErrors()) { |
| 111 | std::cout << error.what() << std::endl; |
| 112 | } |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 113 | CHECK(model.GetErrors().empty()); |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 114 | std::vector<std::string> desc = GetModelDescription(model); |
| 115 | std::vector<std::string> expectedOutput; |
| 116 | expectedOutput.push_back("Entity [0] name = input type = layer"); |
Jim Flynn | f771321 | 2020-07-14 09:50:59 +0100 | [diff] [blame] | 117 | expectedOutput.push_back(" connection [17] from entity [0] to entity [1]"); |
| 118 | expectedOutput.push_back(" child: Entity [26] backendId = " + backend.Get() + " type = workload"); |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 119 | expectedOutput.push_back("Entity [1] name = Rsqrt type = layer"); |
Jim Flynn | f771321 | 2020-07-14 09:50:59 +0100 | [diff] [blame] | 120 | expectedOutput.push_back(" connection [25] from entity [1] to entity [2]"); |
| 121 | expectedOutput.push_back(" child: Entity [18] backendId = " + backend.Get() + " type = workload"); |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 122 | expectedOutput.push_back("Entity [2] name = output type = layer"); |
Jim Flynn | f771321 | 2020-07-14 09:50:59 +0100 | [diff] [blame] | 123 | expectedOutput.push_back(" child: Entity [30] backendId = " + backend.Get() + " type = workload"); |
| 124 | expectedOutput.push_back("Entity [6] processId = [processId] type = network"); |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 125 | expectedOutput.push_back(" child: Entity [0] name = input type = layer"); |
| 126 | expectedOutput.push_back(" child: Entity [1] name = Rsqrt type = layer"); |
| 127 | expectedOutput.push_back(" child: Entity [2] name = output type = layer"); |
Jim Flynn | f771321 | 2020-07-14 09:50:59 +0100 | [diff] [blame] | 128 | expectedOutput.push_back(" execution: Entity [34] type = inference"); |
| 129 | expectedOutput.push_back(" event: [8] class [start_of_life]"); |
| 130 | expectedOutput.push_back("Entity [18] backendId = " + backend.Get() + " type = workload"); |
| 131 | expectedOutput.push_back(" execution: Entity [47] type = workload_execution"); |
| 132 | expectedOutput.push_back("Entity [26] backendId = " + backend.Get() + " type = workload"); |
| 133 | expectedOutput.push_back(" execution: Entity [39] type = workload_execution"); |
| 134 | expectedOutput.push_back("Entity [30] backendId = " + backend.Get() + " type = workload"); |
| 135 | expectedOutput.push_back(" execution: Entity [55] type = workload_execution"); |
| 136 | expectedOutput.push_back("Entity [34] type = inference"); |
| 137 | expectedOutput.push_back(" child: Entity [39] type = workload_execution"); |
| 138 | expectedOutput.push_back(" child: Entity [47] type = workload_execution"); |
| 139 | expectedOutput.push_back(" child: Entity [55] type = workload_execution"); |
| 140 | expectedOutput.push_back(" event: [37] class [start_of_life]"); |
| 141 | expectedOutput.push_back(" event: [63] class [end_of_life]"); |
| 142 | expectedOutput.push_back("Entity [39] type = workload_execution"); |
| 143 | expectedOutput.push_back(" event: [43] class [start_of_life]"); |
| 144 | expectedOutput.push_back(" event: [45] class [end_of_life]"); |
| 145 | expectedOutput.push_back("Entity [47] type = workload_execution"); |
| 146 | expectedOutput.push_back(" event: [51] class [start_of_life]"); |
| 147 | expectedOutput.push_back(" event: [53] class [end_of_life]"); |
| 148 | expectedOutput.push_back("Entity [55] type = workload_execution"); |
| 149 | expectedOutput.push_back(" event: [59] class [start_of_life]"); |
| 150 | expectedOutput.push_back(" event: [61] class [end_of_life]"); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 151 | CHECK(CompareOutput(desc, expectedOutput)); |
Finn Williams | 0c32ccf | 2020-05-12 13:37:06 +0100 | [diff] [blame] | 152 | } |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 155 | TEST_CASE("DumpOutgoingValidFileEndToEnd") |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 156 | { |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 157 | // Get all registered backends |
| 158 | std::vector<BackendId> suitableBackends = GetSuitableBackendRegistered(); |
| 159 | |
| 160 | // Run test for each backend separately |
| 161 | for (auto const& backend : suitableBackends) |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 162 | { |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 163 | // Create a temporary file name. |
| 164 | fs::path tempPath = armnnUtils::Filesystem::NamedTempFile("DumpOutgoingValidFileEndToEnd_CaptureFile.txt"); |
| 165 | // Make sure the file does not exist at this point |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 166 | CHECK(!fs::exists(tempPath)); |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 167 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 168 | armnn::IRuntime::CreationOptions options; |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 169 | options.m_ProfilingOptions.m_EnableProfiling = true; |
| 170 | options.m_ProfilingOptions.m_FileOnly = true; |
| 171 | options.m_ProfilingOptions.m_IncomingCaptureFile = ""; |
| 172 | options.m_ProfilingOptions.m_OutgoingCaptureFile = tempPath.string(); |
| 173 | options.m_ProfilingOptions.m_CapturePeriod = 100; |
| 174 | options.m_ProfilingOptions.m_TimelineEnabled = true; |
| 175 | |
| 176 | ILocalPacketHandlerSharedPtr localPacketHandlerPtr = std::make_shared<TestTimelinePacketHandler>(); |
| 177 | options.m_ProfilingOptions.m_LocalPacketHandlers.push_back(localPacketHandlerPtr); |
| 178 | |
Kevin May | d92a6e4 | 2021-02-04 10:27:41 +0000 | [diff] [blame] | 179 | armnn::RuntimeImpl runtime(options); |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 180 | // ensure the GUID generator is reset to zero |
| 181 | GetProfilingService(&runtime).ResetGuidGenerator(); |
| 182 | |
| 183 | // Load a simple network |
| 184 | // build up the structure of the network |
| 185 | INetworkPtr net(INetwork::Create()); |
| 186 | |
| 187 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 188 | |
| 189 | ElementwiseUnaryDescriptor descriptor(UnaryOperation::Rsqrt); |
| 190 | IConnectableLayer* Rsqrt = net->AddElementwiseUnaryLayer(descriptor, "Rsqrt"); |
| 191 | |
| 192 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 193 | |
| 194 | input->GetOutputSlot(0).Connect(Rsqrt->GetInputSlot(0)); |
| 195 | Rsqrt->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 196 | |
| 197 | input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 198 | Rsqrt->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 199 | |
| 200 | |
| 201 | std::vector<BackendId> backendsVec{backend}; |
| 202 | IOptimizedNetworkPtr optNet = Optimize(*net, backendsVec, runtime.GetDeviceSpec()); |
| 203 | |
| 204 | // Load it into the runtime. It should succeed. |
| 205 | armnn::NetworkId netId; |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 206 | CHECK(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success); |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 207 | |
| 208 | // Creates structures for input & output. |
| 209 | std::vector<float> inputData(16); |
| 210 | std::vector<float> outputData(16); |
| 211 | for (unsigned int i = 0; i < 16; ++i) { |
| 212 | inputData[i] = 9.0; |
| 213 | outputData[i] = 3.0; |
| 214 | } |
| 215 | |
| 216 | InputTensors inputTensors |
| 217 | { |
| 218 | {0, ConstTensor(runtime.GetInputTensorInfo(netId, 0), inputData.data())} |
| 219 | }; |
| 220 | OutputTensors outputTensors |
| 221 | { |
| 222 | {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())} |
| 223 | }; |
| 224 | |
| 225 | // Does the inference. |
| 226 | runtime.EnqueueWorkload(netId, inputTensors, outputTensors); |
| 227 | |
| 228 | static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000); |
| 229 | |
| 230 | // In order to flush the files we need to gracefully close the profiling service. |
| 231 | options.m_ProfilingOptions.m_EnableProfiling = false; |
| 232 | GetProfilingService(&runtime).ResetExternalProfilingOptions(options.m_ProfilingOptions, true); |
| 233 | |
| 234 | // The output file size should be greater than 0. |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 235 | CHECK(fs::file_size(tempPath) > 0); |
Jan Eilers | f78c767 | 2020-07-01 18:09:39 +0100 | [diff] [blame] | 236 | |
| 237 | // NOTE: would be an interesting exercise to take this file and decode it |
| 238 | |
| 239 | // Delete the tmp file. |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 240 | CHECK(fs::remove(tempPath)); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 241 | } |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 244 | } |