blob: a96de66ecdc87aa84db0e571b613530ccd72b51b [file] [log] [blame]
Keith Davis3201eea2019-10-24 17:30:41 +01001//
Jim Flynn6398a982020-05-27 17:05:21 +01002// Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
Keith Davis3201eea2019-10-24 17:30:41 +01003// SPDX-License-Identifier: MIT
4//
5
Rob Hughes9542f902021-07-14 09:48:54 +01006#include <armnnUtils/Filesystem.hpp>
Aron Virginas-Tar8bf442e2019-11-07 18:41:40 +00007#include <ProfilingService.hpp>
Jim Flynn6398a982020-05-27 17:05:21 +01008#include "ProfilingTestUtils.hpp"
Jim Flynn4e755a52020-03-29 17:48:26 +01009#include "PrintPacketHeaderHandler.hpp"
Jim Flynn6398a982020-05-27 17:05:21 +010010#include <Runtime.hpp>
Jim Flynn4e755a52020-03-29 17:48:26 +010011#include "TestTimelinePacketHandler.hpp"
Keith Davis3201eea2019-10-24 17:30:41 +010012
Sadik Armagan1625efc2021-06-10 18:24:34 +010013#include <doctest/doctest.h>
Keith Davis3201eea2019-10-24 17:30:41 +010014
Nikhil Raj77fe76b2021-06-09 14:55:32 +010015#include <common/include/LabelsAndEventClasses.hpp>
16
Keith Davis3201eea2019-10-24 17:30:41 +010017#include <cstdio>
Keith Davis3201eea2019-10-24 17:30:41 +010018#include <sstream>
19#include <sys/stat.h>
20
Keith Davis3201eea2019-10-24 17:30:41 +010021using namespace armnn::profiling;
22using namespace armnn;
23
24using namespace std::chrono_literals;
25
Finn Williams09ad6f92019-12-19 17:05:18 +000026class FileOnlyHelperService : public ProfilingService
27{
28 public:
29 // Wait for a notification from the send thread
30 bool WaitForPacketsSent(uint32_t timeout = 1000)
31 {
Sadik Armagan3184c902020-03-18 10:57:30 +000032 return ProfilingService::WaitForPacketSent(m_ProfilingService, timeout);
Finn Williams09ad6f92019-12-19 17:05:18 +000033 }
Sadik Armagan3184c902020-03-18 10:57:30 +000034 armnn::profiling::ProfilingService m_ProfilingService;
Finn Williams09ad6f92019-12-19 17:05:18 +000035};
36
Sadik Armagan1625efc2021-06-10 18:24:34 +010037TEST_SUITE("FileOnlyProfilingDecoratorTests")
38{
39TEST_CASE("TestFileOnlyProfiling")
Jim Flynn4e755a52020-03-29 17:48:26 +010040{
Jan Eilersf78c7672020-07-01 18:09:39 +010041 // 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 Williams0c32ccf2020-05-12 13:37:06 +010046 {
Jan Eilersf78c7672020-07-01 18:09:39 +010047 // Enable m_FileOnly but also provide ILocalPacketHandler which should consume the packets.
48 // This won't dump anything to file.
Kevin Mayd92a6e42021-02-04 10:27:41 +000049 armnn::IRuntime::CreationOptions creationOptions;
Jan Eilersf78c7672020-07-01 18:09:39 +010050 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 Mayd92a6e42021-02-04 10:27:41 +000057 armnn::RuntimeImpl runtime(creationOptions);
Jan Eilersf78c7672020-07-01 18:09:39 +010058 // 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 Armagan1625efc2021-06-10 18:24:34 +010083 CHECK(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success);
Jan Eilersf78c7672020-07-01 18:09:39 +010084
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
Cathal Corbett5b8093c2021-10-22 11:12:07 +010093 TensorInfo inputTensorInfo = runtime.GetInputTensorInfo(netId, 0);
94 inputTensorInfo.SetConstant(true);
Jan Eilersf78c7672020-07-01 18:09:39 +010095 InputTensors inputTensors
96 {
Cathal Corbett5b8093c2021-10-22 11:12:07 +010097 {0, ConstTensor(inputTensorInfo, inputData.data())}
Jan Eilersf78c7672020-07-01 18:09:39 +010098 };
99 OutputTensors outputTensors
100 {
101 {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())}
102 };
103
104 // Does the inference.
105 runtime.EnqueueWorkload(netId, inputTensors, outputTensors);
106
107 static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000);
108
109 const TimelineModel &model =
110 static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->GetTimelineModel();
111
112 for (auto &error : model.GetErrors()) {
113 std::cout << error.what() << std::endl;
114 }
Sadik Armagan1625efc2021-06-10 18:24:34 +0100115 CHECK(model.GetErrors().empty());
Jan Eilersf78c7672020-07-01 18:09:39 +0100116 std::vector<std::string> desc = GetModelDescription(model);
117 std::vector<std::string> expectedOutput;
118 expectedOutput.push_back("Entity [0] name = input type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100119 expectedOutput.push_back(" connection [17] from entity [0] to entity [1]");
120 expectedOutput.push_back(" child: Entity [26] backendId = " + backend.Get() + " type = workload");
Jan Eilersf78c7672020-07-01 18:09:39 +0100121 expectedOutput.push_back("Entity [1] name = Rsqrt type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100122 expectedOutput.push_back(" connection [25] from entity [1] to entity [2]");
123 expectedOutput.push_back(" child: Entity [18] backendId = " + backend.Get() + " type = workload");
Jan Eilersf78c7672020-07-01 18:09:39 +0100124 expectedOutput.push_back("Entity [2] name = output type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100125 expectedOutput.push_back(" child: Entity [30] backendId = " + backend.Get() + " type = workload");
126 expectedOutput.push_back("Entity [6] processId = [processId] type = network");
Jan Eilersf78c7672020-07-01 18:09:39 +0100127 expectedOutput.push_back(" child: Entity [0] name = input type = layer");
128 expectedOutput.push_back(" child: Entity [1] name = Rsqrt type = layer");
129 expectedOutput.push_back(" child: Entity [2] name = output type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100130 expectedOutput.push_back(" execution: Entity [34] type = inference");
131 expectedOutput.push_back(" event: [8] class [start_of_life]");
132 expectedOutput.push_back("Entity [18] backendId = " + backend.Get() + " type = workload");
133 expectedOutput.push_back(" execution: Entity [47] type = workload_execution");
134 expectedOutput.push_back("Entity [26] backendId = " + backend.Get() + " type = workload");
135 expectedOutput.push_back(" execution: Entity [39] type = workload_execution");
136 expectedOutput.push_back("Entity [30] backendId = " + backend.Get() + " type = workload");
137 expectedOutput.push_back(" execution: Entity [55] type = workload_execution");
138 expectedOutput.push_back("Entity [34] type = inference");
139 expectedOutput.push_back(" child: Entity [39] type = workload_execution");
140 expectedOutput.push_back(" child: Entity [47] type = workload_execution");
141 expectedOutput.push_back(" child: Entity [55] type = workload_execution");
142 expectedOutput.push_back(" event: [37] class [start_of_life]");
143 expectedOutput.push_back(" event: [63] class [end_of_life]");
144 expectedOutput.push_back("Entity [39] type = workload_execution");
145 expectedOutput.push_back(" event: [43] class [start_of_life]");
146 expectedOutput.push_back(" event: [45] class [end_of_life]");
147 expectedOutput.push_back("Entity [47] type = workload_execution");
148 expectedOutput.push_back(" event: [51] class [start_of_life]");
149 expectedOutput.push_back(" event: [53] class [end_of_life]");
150 expectedOutput.push_back("Entity [55] type = workload_execution");
151 expectedOutput.push_back(" event: [59] class [start_of_life]");
152 expectedOutput.push_back(" event: [61] class [end_of_life]");
Sadik Armagan1625efc2021-06-10 18:24:34 +0100153 CHECK(CompareOutput(desc, expectedOutput));
Finn Williams0c32ccf2020-05-12 13:37:06 +0100154 }
Jim Flynn4e755a52020-03-29 17:48:26 +0100155}
156
Sadik Armagan1625efc2021-06-10 18:24:34 +0100157TEST_CASE("DumpOutgoingValidFileEndToEnd")
Keith Davis3201eea2019-10-24 17:30:41 +0100158{
Jan Eilersf78c7672020-07-01 18:09:39 +0100159 // Get all registered backends
160 std::vector<BackendId> suitableBackends = GetSuitableBackendRegistered();
161
162 // Run test for each backend separately
163 for (auto const& backend : suitableBackends)
Keith Davis3201eea2019-10-24 17:30:41 +0100164 {
Jan Eilersf78c7672020-07-01 18:09:39 +0100165 // Create a temporary file name.
166 fs::path tempPath = armnnUtils::Filesystem::NamedTempFile("DumpOutgoingValidFileEndToEnd_CaptureFile.txt");
167 // Make sure the file does not exist at this point
Sadik Armagan1625efc2021-06-10 18:24:34 +0100168 CHECK(!fs::exists(tempPath));
Jan Eilersf78c7672020-07-01 18:09:39 +0100169
Kevin Mayd92a6e42021-02-04 10:27:41 +0000170 armnn::IRuntime::CreationOptions options;
Jan Eilersf78c7672020-07-01 18:09:39 +0100171 options.m_ProfilingOptions.m_EnableProfiling = true;
172 options.m_ProfilingOptions.m_FileOnly = true;
173 options.m_ProfilingOptions.m_IncomingCaptureFile = "";
174 options.m_ProfilingOptions.m_OutgoingCaptureFile = tempPath.string();
175 options.m_ProfilingOptions.m_CapturePeriod = 100;
176 options.m_ProfilingOptions.m_TimelineEnabled = true;
177
178 ILocalPacketHandlerSharedPtr localPacketHandlerPtr = std::make_shared<TestTimelinePacketHandler>();
179 options.m_ProfilingOptions.m_LocalPacketHandlers.push_back(localPacketHandlerPtr);
180
Kevin Mayd92a6e42021-02-04 10:27:41 +0000181 armnn::RuntimeImpl runtime(options);
Jan Eilersf78c7672020-07-01 18:09:39 +0100182 // ensure the GUID generator is reset to zero
183 GetProfilingService(&runtime).ResetGuidGenerator();
184
185 // Load a simple network
186 // build up the structure of the network
187 INetworkPtr net(INetwork::Create());
188
189 IConnectableLayer* input = net->AddInputLayer(0, "input");
190
191 ElementwiseUnaryDescriptor descriptor(UnaryOperation::Rsqrt);
192 IConnectableLayer* Rsqrt = net->AddElementwiseUnaryLayer(descriptor, "Rsqrt");
193
194 IConnectableLayer* output = net->AddOutputLayer(0, "output");
195
196 input->GetOutputSlot(0).Connect(Rsqrt->GetInputSlot(0));
197 Rsqrt->GetOutputSlot(0).Connect(output->GetInputSlot(0));
198
199 input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
200 Rsqrt->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
201
202
203 std::vector<BackendId> backendsVec{backend};
204 IOptimizedNetworkPtr optNet = Optimize(*net, backendsVec, runtime.GetDeviceSpec());
205
206 // Load it into the runtime. It should succeed.
207 armnn::NetworkId netId;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100208 CHECK(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success);
Jan Eilersf78c7672020-07-01 18:09:39 +0100209
210 // Creates structures for input & output.
211 std::vector<float> inputData(16);
212 std::vector<float> outputData(16);
213 for (unsigned int i = 0; i < 16; ++i) {
214 inputData[i] = 9.0;
215 outputData[i] = 3.0;
216 }
217
Cathal Corbett5b8093c2021-10-22 11:12:07 +0100218 TensorInfo inputTensorInfo = runtime.GetInputTensorInfo(netId, 0);
219 inputTensorInfo.SetConstant(true);
Jan Eilersf78c7672020-07-01 18:09:39 +0100220 InputTensors inputTensors
221 {
Cathal Corbett5b8093c2021-10-22 11:12:07 +0100222 {0, ConstTensor(inputTensorInfo, inputData.data())}
Jan Eilersf78c7672020-07-01 18:09:39 +0100223 };
224 OutputTensors outputTensors
225 {
226 {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())}
227 };
228
229 // Does the inference.
230 runtime.EnqueueWorkload(netId, inputTensors, outputTensors);
231
232 static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000);
233
234 // In order to flush the files we need to gracefully close the profiling service.
235 options.m_ProfilingOptions.m_EnableProfiling = false;
236 GetProfilingService(&runtime).ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
237
238 // The output file size should be greater than 0.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100239 CHECK(fs::file_size(tempPath) > 0);
Jan Eilersf78c7672020-07-01 18:09:39 +0100240
241 // NOTE: would be an interesting exercise to take this file and decode it
242
243 // Delete the tmp file.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100244 CHECK(fs::remove(tempPath));
Keith Davis3201eea2019-10-24 17:30:41 +0100245 }
Keith Davis3201eea2019-10-24 17:30:41 +0100246}
247
Sadik Armagan1625efc2021-06-10 18:24:34 +0100248}