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> |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 7 | #include <LabelsAndEventClasses.hpp> |
Aron Virginas-Tar | 8bf442e | 2019-11-07 18:41:40 +0000 | [diff] [blame] | 8 | #include <ProfilingService.hpp> |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 9 | #include "ProfilingTestUtils.hpp" |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 10 | #include "PrintPacketHeaderHandler.hpp" |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 11 | #include <Runtime.hpp> |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 12 | #include "TestTimelinePacketHandler.hpp" |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 13 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 14 | #include <boost/filesystem.hpp> |
| 15 | #include <boost/numeric/conversion/cast.hpp> |
| 16 | #include <boost/test/unit_test.hpp> |
| 17 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 18 | #include <cstdio> |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 19 | #include <sstream> |
| 20 | #include <sys/stat.h> |
| 21 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 22 | using namespace armnn::profiling; |
| 23 | using namespace armnn; |
| 24 | |
| 25 | using namespace std::chrono_literals; |
| 26 | |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 27 | class FileOnlyHelperService : public ProfilingService |
| 28 | { |
| 29 | public: |
| 30 | // Wait for a notification from the send thread |
| 31 | bool WaitForPacketsSent(uint32_t timeout = 1000) |
| 32 | { |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 33 | return ProfilingService::WaitForPacketSent(m_ProfilingService, timeout); |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 34 | } |
Sadik Armagan | 3184c90 | 2020-03-18 10:57:30 +0000 | [diff] [blame] | 35 | armnn::profiling::ProfilingService m_ProfilingService; |
Finn Williams | 09ad6f9 | 2019-12-19 17:05:18 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(FileOnlyProfilingDecoratorTests) |
| 39 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 40 | std::string UniqueFileName() |
| 41 | { |
| 42 | std::time_t t = std::time(nullptr); |
| 43 | char mbstr[100]; |
| 44 | std::strftime(mbstr, sizeof(mbstr), "%Y_%m_%d_%H_%M_%S_", std::localtime(&t)); |
| 45 | std::stringstream ss; |
| 46 | ss << mbstr; |
| 47 | ss << t; |
| 48 | ss << ".bin"; |
| 49 | return ss.str(); |
| 50 | } |
| 51 | |
| 52 | BOOST_AUTO_TEST_CASE(TestFileOnlyProfiling) |
| 53 | { |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 54 | // This test requires at least one backend registry to be enabled |
| 55 | // which can execute a NormalizationLayer |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 56 | if (!HasSuitableBackendRegistered()) |
Finn Williams | 0c32ccf | 2020-05-12 13:37:06 +0100 | [diff] [blame] | 57 | { |
| 58 | return; |
| 59 | } |
| 60 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 61 | // Create a temporary file name. |
Jim Flynn | c963110 | 2020-06-24 11:11:20 +0100 | [diff] [blame] | 62 | fs::path tempPath = fs::temp_directory_path(); |
| 63 | fs::path tempFile = UniqueFileName(); |
| 64 | tempPath = tempPath / tempFile; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 65 | armnn::Runtime::CreationOptions creationOptions; |
| 66 | creationOptions.m_ProfilingOptions.m_EnableProfiling = true; |
| 67 | creationOptions.m_ProfilingOptions.m_FileOnly = true; |
| 68 | creationOptions.m_ProfilingOptions.m_CapturePeriod = 100; |
| 69 | creationOptions.m_ProfilingOptions.m_TimelineEnabled = true; |
| 70 | ILocalPacketHandlerSharedPtr localPacketHandlerPtr = std::make_shared<TestTimelinePacketHandler>(); |
| 71 | creationOptions.m_ProfilingOptions.m_LocalPacketHandlers.push_back(localPacketHandlerPtr); |
| 72 | |
| 73 | armnn::Runtime runtime(creationOptions); |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 74 | // ensure the GUID generator is reset to zero |
| 75 | GetProfilingService(&runtime).ResetGuidGenerator(); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 76 | |
| 77 | // Load a simple network |
| 78 | // build up the structure of the network |
| 79 | INetworkPtr net(INetwork::Create()); |
| 80 | |
| 81 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 82 | |
Jim Flynn | d5ba9aa | 2020-06-24 10:32:43 +0100 | [diff] [blame] | 83 | ElementwiseUnaryDescriptor descriptor(UnaryOperation::Sqrt); |
| 84 | IConnectableLayer* normalize = net->AddElementwiseUnaryLayer(descriptor, "normalization"); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 85 | |
| 86 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 87 | |
| 88 | input->GetOutputSlot(0).Connect(normalize->GetInputSlot(0)); |
| 89 | normalize->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 90 | |
| 91 | input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 92 | normalize->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 93 | |
| 94 | // optimize the network |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 95 | std::vector<armnn::BackendId> backends = |
| 96 | { armnn::Compute::CpuRef, armnn::Compute::CpuAcc, armnn::Compute::GpuAcc }; |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 97 | IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime.GetDeviceSpec()); |
| 98 | |
| 99 | // Load it into the runtime. It should succeed. |
| 100 | armnn::NetworkId netId; |
| 101 | BOOST_TEST(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success); |
| 102 | |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 103 | // Creates structures for input & output. |
| 104 | std::vector<float> inputData(16); |
| 105 | std::vector<float> outputData(16); |
Jim Flynn | d5ba9aa | 2020-06-24 10:32:43 +0100 | [diff] [blame] | 106 | for (unsigned int i = 0; i < 16; ++i) |
| 107 | { |
| 108 | inputData[i] = 9.0; |
| 109 | outputData[i] = 3.0; |
| 110 | } |
Jim Flynn | 01d0281 | 2020-04-29 21:12:13 +0100 | [diff] [blame] | 111 | |
| 112 | InputTensors inputTensors |
| 113 | { |
| 114 | {0, ConstTensor(runtime.GetInputTensorInfo(netId, 0), inputData.data())} |
| 115 | }; |
| 116 | OutputTensors outputTensors |
| 117 | { |
| 118 | {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())} |
| 119 | }; |
| 120 | |
| 121 | // Does the inference. |
| 122 | runtime.EnqueueWorkload(netId, inputTensors, outputTensors); |
| 123 | |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 124 | static_cast<TestTimelinePacketHandler*>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000); |
Jim Flynn | 6398a98 | 2020-05-27 17:05:21 +0100 | [diff] [blame] | 125 | |
| 126 | const TimelineModel& model = |
| 127 | static_cast<TestTimelinePacketHandler*>(localPacketHandlerPtr.get())->GetTimelineModel(); |
| 128 | |
| 129 | for (auto& error : model.GetErrors()) |
| 130 | { |
| 131 | std::cout << error.what() << std::endl; |
| 132 | } |
| 133 | BOOST_TEST(model.GetErrors().empty()); |
| 134 | std::vector<std::string> desc = GetModelDescription(model); |
| 135 | std::vector<std::string> expectedOutput; |
| 136 | expectedOutput.push_back("Entity [0] name = input type = layer"); |
| 137 | expectedOutput.push_back(" connection [14] from entity [0] to entity [1]"); |
| 138 | expectedOutput.push_back(" child: Entity [23] backendId = CpuRef type = workload"); |
| 139 | expectedOutput.push_back("Entity [1] name = normalization type = layer"); |
| 140 | expectedOutput.push_back(" connection [22] from entity [1] to entity [2]"); |
| 141 | expectedOutput.push_back(" child: Entity [15] backendId = CpuRef type = workload"); |
| 142 | expectedOutput.push_back("Entity [2] name = output type = layer"); |
| 143 | expectedOutput.push_back(" child: Entity [27] backendId = CpuRef type = workload"); |
| 144 | expectedOutput.push_back("Entity [6] type = network"); |
| 145 | expectedOutput.push_back(" child: Entity [0] name = input type = layer"); |
| 146 | expectedOutput.push_back(" child: Entity [1] name = normalization type = layer"); |
| 147 | expectedOutput.push_back(" child: Entity [2] name = output type = layer"); |
| 148 | expectedOutput.push_back(" execution: Entity [31] type = inference"); |
| 149 | expectedOutput.push_back("Entity [15] backendId = CpuRef type = workload"); |
| 150 | expectedOutput.push_back(" execution: Entity [44] type = workload_execution"); |
| 151 | expectedOutput.push_back("Entity [23] backendId = CpuRef type = workload"); |
| 152 | expectedOutput.push_back(" execution: Entity [36] type = workload_execution"); |
| 153 | expectedOutput.push_back("Entity [27] backendId = CpuRef type = workload"); |
| 154 | expectedOutput.push_back(" execution: Entity [52] type = workload_execution"); |
| 155 | expectedOutput.push_back("Entity [31] type = inference"); |
| 156 | expectedOutput.push_back(" child: Entity [36] type = workload_execution"); |
| 157 | expectedOutput.push_back(" child: Entity [44] type = workload_execution"); |
| 158 | expectedOutput.push_back(" child: Entity [52] type = workload_execution"); |
| 159 | expectedOutput.push_back(" event: [34] class [start_of_life]"); |
| 160 | expectedOutput.push_back(" event: [60] class [end_of_life]"); |
| 161 | expectedOutput.push_back("Entity [36] type = workload_execution"); |
| 162 | expectedOutput.push_back(" event: [40] class [start_of_life]"); |
| 163 | expectedOutput.push_back(" event: [42] class [end_of_life]"); |
| 164 | expectedOutput.push_back("Entity [44] type = workload_execution"); |
| 165 | expectedOutput.push_back(" event: [48] class [start_of_life]"); |
| 166 | expectedOutput.push_back(" event: [50] class [end_of_life]"); |
| 167 | expectedOutput.push_back("Entity [52] type = workload_execution"); |
| 168 | expectedOutput.push_back(" event: [56] class [start_of_life]"); |
| 169 | expectedOutput.push_back(" event: [58] class [end_of_life]"); |
| 170 | BOOST_TEST(CompareOutput(desc, expectedOutput)); |
Jim Flynn | 4e755a5 | 2020-03-29 17:48:26 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Jim Flynn | c963110 | 2020-06-24 11:11:20 +0100 | [diff] [blame] | 173 | BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd) |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 174 | { |
Jim Flynn | c963110 | 2020-06-24 11:11:20 +0100 | [diff] [blame] | 175 | // This test requires at least one backend registry to be enabled |
| 176 | // which can execute a NormalizationLayer |
| 177 | if (!HasSuitableBackendRegistered()) |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 178 | { |
Jim Flynn | c963110 | 2020-06-24 11:11:20 +0100 | [diff] [blame] | 179 | return; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Jim Flynn | c963110 | 2020-06-24 11:11:20 +0100 | [diff] [blame] | 182 | // Create a temporary file name. |
| 183 | fs::path tempPath = fs::temp_directory_path(); |
| 184 | fs::path tempFile = UniqueFileName(); |
| 185 | tempPath = tempPath / tempFile; |
| 186 | armnn::Runtime::CreationOptions options; |
| 187 | options.m_ProfilingOptions.m_EnableProfiling = true; |
| 188 | options.m_ProfilingOptions.m_FileOnly = true; |
| 189 | options.m_ProfilingOptions.m_IncomingCaptureFile = ""; |
| 190 | options.m_ProfilingOptions.m_OutgoingCaptureFile = tempPath.string(); |
| 191 | options.m_ProfilingOptions.m_CapturePeriod = 100; |
| 192 | options.m_ProfilingOptions.m_TimelineEnabled = true; |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 193 | |
Jim Flynn | c963110 | 2020-06-24 11:11:20 +0100 | [diff] [blame] | 194 | ILocalPacketHandlerSharedPtr localPacketHandlerPtr = std::make_shared<TestTimelinePacketHandler>(); |
| 195 | options.m_ProfilingOptions.m_LocalPacketHandlers.push_back(localPacketHandlerPtr); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 196 | |
Jim Flynn | c963110 | 2020-06-24 11:11:20 +0100 | [diff] [blame] | 197 | // Make sure the file does not exist at this point |
| 198 | BOOST_CHECK(armnnUtils::Filesystem::GetFileSize(tempPath.string().c_str()) == -1); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 199 | |
Jim Flynn | c963110 | 2020-06-24 11:11:20 +0100 | [diff] [blame] | 200 | armnn::Runtime runtime(options); |
| 201 | // ensure the GUID generator is reset to zero |
| 202 | GetProfilingService(&runtime).ResetGuidGenerator(); |
| 203 | |
| 204 | // Load a simple network |
| 205 | // build up the structure of the network |
| 206 | INetworkPtr net(INetwork::Create()); |
| 207 | |
| 208 | IConnectableLayer* input = net->AddInputLayer(0, "input"); |
| 209 | |
| 210 | ElementwiseUnaryDescriptor descriptor(UnaryOperation::Sqrt); |
| 211 | IConnectableLayer* normalize = net->AddElementwiseUnaryLayer(descriptor, "normalization"); |
| 212 | |
| 213 | IConnectableLayer* output = net->AddOutputLayer(0, "output"); |
| 214 | |
| 215 | input->GetOutputSlot(0).Connect(normalize->GetInputSlot(0)); |
| 216 | normalize->GetOutputSlot(0).Connect(output->GetInputSlot(0)); |
| 217 | |
| 218 | input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 219 | normalize->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32)); |
| 220 | |
| 221 | // optimize the network |
| 222 | std::vector<armnn::BackendId> backends = |
| 223 | { armnn::Compute::CpuRef, armnn::Compute::CpuAcc, armnn::Compute::GpuAcc }; |
| 224 | IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime.GetDeviceSpec()); |
| 225 | |
| 226 | // Load it into the runtime. It should succeed. |
| 227 | armnn::NetworkId netId; |
| 228 | BOOST_TEST(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success); |
| 229 | |
| 230 | // Creates structures for input & output. |
| 231 | std::vector<float> inputData(16); |
| 232 | std::vector<float> outputData(16); |
| 233 | for (unsigned int i = 0; i < 16; ++i) |
| 234 | { |
| 235 | inputData[i] = 9.0; |
| 236 | outputData[i] = 3.0; |
| 237 | } |
| 238 | |
| 239 | InputTensors inputTensors |
| 240 | { |
| 241 | {0, ConstTensor(runtime.GetInputTensorInfo(netId, 0), inputData.data())} |
| 242 | }; |
| 243 | OutputTensors outputTensors |
| 244 | { |
| 245 | {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())} |
| 246 | }; |
| 247 | |
| 248 | // Does the inference. |
| 249 | runtime.EnqueueWorkload(netId, inputTensors, outputTensors); |
| 250 | |
| 251 | static_cast<TestTimelinePacketHandler*>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 252 | |
| 253 | // In order to flush the files we need to gracefully close the profiling service. |
Jim Flynn | c963110 | 2020-06-24 11:11:20 +0100 | [diff] [blame] | 254 | options.m_ProfilingOptions.m_EnableProfiling = false; |
| 255 | GetProfilingService(&runtime).ResetExternalProfilingOptions(options.m_ProfilingOptions, true); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 256 | |
| 257 | // The output file size should be greater than 0. |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 258 | BOOST_CHECK(armnnUtils::Filesystem::GetFileSize(tempPath.string().c_str()) > 0); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 259 | |
Jim Flynn | c963110 | 2020-06-24 11:11:20 +0100 | [diff] [blame] | 260 | // NOTE: would be an interesting exercise to take this file and decode it |
| 261 | |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 262 | // Delete the tmp file. |
Rob Hughes | bdee426 | 2020-01-07 17:05:24 +0000 | [diff] [blame] | 263 | BOOST_CHECK(armnnUtils::Filesystem::Remove(tempPath.string().c_str())); |
Keith Davis | 3201eea | 2019-10-24 17:30:41 +0100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | BOOST_AUTO_TEST_SUITE_END() |