blob: 5c5ee8a0e493e37443dd2c44b2d9e43cfa859e45 [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 Flynn4c9ed1d2022-01-23 23:57:20 +00009#include "ProfilingOptionsConverter.hpp"
Jim Flynn4e755a52020-03-29 17:48:26 +010010#include "PrintPacketHeaderHandler.hpp"
Jim Flynn6398a982020-05-27 17:05:21 +010011#include <Runtime.hpp>
Jim Flynn4e755a52020-03-29 17:48:26 +010012#include "TestTimelinePacketHandler.hpp"
Keith Davis3201eea2019-10-24 17:30:41 +010013
Sadik Armagan1625efc2021-06-10 18:24:34 +010014#include <doctest/doctest.h>
Keith Davis3201eea2019-10-24 17:30:41 +010015
Nikhil Raj77fe76b2021-06-09 14:55:32 +010016#include <common/include/LabelsAndEventClasses.hpp>
17
Keith Davis3201eea2019-10-24 17:30:41 +010018#include <cstdio>
Keith Davis3201eea2019-10-24 17:30:41 +010019#include <sstream>
20#include <sys/stat.h>
21
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000022using namespace arm::pipe;
Keith Davis3201eea2019-10-24 17:30:41 +010023using namespace armnn;
24
25using namespace std::chrono_literals;
26
Finn Williams09ad6f92019-12-19 17:05:18 +000027class FileOnlyHelperService : public ProfilingService
28{
29 public:
30 // Wait for a notification from the send thread
31 bool WaitForPacketsSent(uint32_t timeout = 1000)
32 {
Sadik Armagan3184c902020-03-18 10:57:30 +000033 return ProfilingService::WaitForPacketSent(m_ProfilingService, timeout);
Finn Williams09ad6f92019-12-19 17:05:18 +000034 }
Cathal Corbett5aa9fd72022-02-25 15:33:28 +000035 ProfilingService m_ProfilingService;
Finn Williams09ad6f92019-12-19 17:05:18 +000036};
37
Sadik Armagan1625efc2021-06-10 18:24:34 +010038TEST_SUITE("FileOnlyProfilingDecoratorTests")
39{
40TEST_CASE("TestFileOnlyProfiling")
Jim Flynn4e755a52020-03-29 17:48:26 +010041{
Jan Eilersf78c7672020-07-01 18:09:39 +010042 // Get all registered backends
43 std::vector<BackendId> suitableBackends = GetSuitableBackendRegistered();
44
45 // Run test for each backend separately
46 for (auto const& backend : suitableBackends)
Finn Williams0c32ccf2020-05-12 13:37:06 +010047 {
Jan Eilersf78c7672020-07-01 18:09:39 +010048 // Enable m_FileOnly but also provide ILocalPacketHandler which should consume the packets.
49 // This won't dump anything to file.
Kevin Mayd92a6e42021-02-04 10:27:41 +000050 armnn::IRuntime::CreationOptions creationOptions;
Jan Eilersf78c7672020-07-01 18:09:39 +010051 creationOptions.m_ProfilingOptions.m_EnableProfiling = true;
52 creationOptions.m_ProfilingOptions.m_FileOnly = true;
53 creationOptions.m_ProfilingOptions.m_CapturePeriod = 100;
54 creationOptions.m_ProfilingOptions.m_TimelineEnabled = true;
55 ILocalPacketHandlerSharedPtr localPacketHandlerPtr = std::make_shared<TestTimelinePacketHandler>();
56 creationOptions.m_ProfilingOptions.m_LocalPacketHandlers.push_back(localPacketHandlerPtr);
57
Kevin Mayd92a6e42021-02-04 10:27:41 +000058 armnn::RuntimeImpl runtime(creationOptions);
Jan Eilersf78c7672020-07-01 18:09:39 +010059 // ensure the GUID generator is reset to zero
60 GetProfilingService(&runtime).ResetGuidGenerator();
61
62 // Load a simple network
63 // build up the structure of the network
64 INetworkPtr net(INetwork::Create());
65
66 IConnectableLayer* input = net->AddInputLayer(0, "input");
67
68 ElementwiseUnaryDescriptor descriptor(UnaryOperation::Rsqrt);
69 IConnectableLayer* Rsqrt = net->AddElementwiseUnaryLayer(descriptor, "Rsqrt");
70
71 IConnectableLayer* output = net->AddOutputLayer(0, "output");
72
73 input->GetOutputSlot(0).Connect(Rsqrt->GetInputSlot(0));
74 Rsqrt->GetOutputSlot(0).Connect(output->GetInputSlot(0));
75
76 input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
77 Rsqrt->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
78
79 std::vector<armnn::BackendId> backendsVec {backend};
80 IOptimizedNetworkPtr optNet = Optimize(*net, backendsVec, runtime.GetDeviceSpec());
81
82 // Load it into the runtime. It should succeed.
83 armnn::NetworkId netId;
Sadik Armagan1625efc2021-06-10 18:24:34 +010084 CHECK(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success);
Jan Eilersf78c7672020-07-01 18:09:39 +010085
86 // Creates structures for input & output.
87 std::vector<float> inputData(16);
88 std::vector<float> outputData(16);
89 for (unsigned int i = 0; i < 16; ++i) {
90 inputData[i] = 9.0;
91 outputData[i] = 3.0;
92 }
93
Cathal Corbett5b8093c2021-10-22 11:12:07 +010094 TensorInfo inputTensorInfo = runtime.GetInputTensorInfo(netId, 0);
95 inputTensorInfo.SetConstant(true);
Jan Eilersf78c7672020-07-01 18:09:39 +010096 InputTensors inputTensors
97 {
Cathal Corbett5b8093c2021-10-22 11:12:07 +010098 {0, ConstTensor(inputTensorInfo, inputData.data())}
Jan Eilersf78c7672020-07-01 18:09:39 +010099 };
100 OutputTensors outputTensors
101 {
102 {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())}
103 };
104
105 // Does the inference.
106 runtime.EnqueueWorkload(netId, inputTensors, outputTensors);
107
108 static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000);
109
110 const TimelineModel &model =
111 static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->GetTimelineModel();
112
113 for (auto &error : model.GetErrors()) {
114 std::cout << error.what() << std::endl;
115 }
Sadik Armagan1625efc2021-06-10 18:24:34 +0100116 CHECK(model.GetErrors().empty());
Jan Eilersf78c7672020-07-01 18:09:39 +0100117 std::vector<std::string> desc = GetModelDescription(model);
118 std::vector<std::string> expectedOutput;
119 expectedOutput.push_back("Entity [0] name = input type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100120 expectedOutput.push_back(" connection [17] from entity [0] to entity [1]");
121 expectedOutput.push_back(" child: Entity [26] backendId = " + backend.Get() + " type = workload");
Jan Eilersf78c7672020-07-01 18:09:39 +0100122 expectedOutput.push_back("Entity [1] name = Rsqrt type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100123 expectedOutput.push_back(" connection [25] from entity [1] to entity [2]");
124 expectedOutput.push_back(" child: Entity [18] backendId = " + backend.Get() + " type = workload");
Jan Eilersf78c7672020-07-01 18:09:39 +0100125 expectedOutput.push_back("Entity [2] name = output type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100126 expectedOutput.push_back(" child: Entity [30] backendId = " + backend.Get() + " type = workload");
127 expectedOutput.push_back("Entity [6] processId = [processId] type = network");
Jan Eilersf78c7672020-07-01 18:09:39 +0100128 expectedOutput.push_back(" child: Entity [0] name = input type = layer");
129 expectedOutput.push_back(" child: Entity [1] name = Rsqrt type = layer");
130 expectedOutput.push_back(" child: Entity [2] name = output type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100131 expectedOutput.push_back(" execution: Entity [34] type = inference");
132 expectedOutput.push_back(" event: [8] class [start_of_life]");
133 expectedOutput.push_back("Entity [18] backendId = " + backend.Get() + " type = workload");
134 expectedOutput.push_back(" execution: Entity [47] type = workload_execution");
135 expectedOutput.push_back("Entity [26] backendId = " + backend.Get() + " type = workload");
136 expectedOutput.push_back(" execution: Entity [39] type = workload_execution");
137 expectedOutput.push_back("Entity [30] backendId = " + backend.Get() + " type = workload");
138 expectedOutput.push_back(" execution: Entity [55] type = workload_execution");
139 expectedOutput.push_back("Entity [34] type = inference");
140 expectedOutput.push_back(" child: Entity [39] type = workload_execution");
141 expectedOutput.push_back(" child: Entity [47] type = workload_execution");
142 expectedOutput.push_back(" child: Entity [55] type = workload_execution");
143 expectedOutput.push_back(" event: [37] class [start_of_life]");
144 expectedOutput.push_back(" event: [63] class [end_of_life]");
145 expectedOutput.push_back("Entity [39] type = workload_execution");
146 expectedOutput.push_back(" event: [43] class [start_of_life]");
147 expectedOutput.push_back(" event: [45] class [end_of_life]");
148 expectedOutput.push_back("Entity [47] type = workload_execution");
149 expectedOutput.push_back(" event: [51] class [start_of_life]");
150 expectedOutput.push_back(" event: [53] class [end_of_life]");
151 expectedOutput.push_back("Entity [55] type = workload_execution");
152 expectedOutput.push_back(" event: [59] class [start_of_life]");
153 expectedOutput.push_back(" event: [61] class [end_of_life]");
Sadik Armagan1625efc2021-06-10 18:24:34 +0100154 CHECK(CompareOutput(desc, expectedOutput));
Finn Williams0c32ccf2020-05-12 13:37:06 +0100155 }
Jim Flynn4e755a52020-03-29 17:48:26 +0100156}
157
Sadik Armagan1625efc2021-06-10 18:24:34 +0100158TEST_CASE("DumpOutgoingValidFileEndToEnd")
Keith Davis3201eea2019-10-24 17:30:41 +0100159{
Jan Eilersf78c7672020-07-01 18:09:39 +0100160 // Get all registered backends
161 std::vector<BackendId> suitableBackends = GetSuitableBackendRegistered();
162
163 // Run test for each backend separately
164 for (auto const& backend : suitableBackends)
Keith Davis3201eea2019-10-24 17:30:41 +0100165 {
Jan Eilersf78c7672020-07-01 18:09:39 +0100166 // Create a temporary file name.
167 fs::path tempPath = armnnUtils::Filesystem::NamedTempFile("DumpOutgoingValidFileEndToEnd_CaptureFile.txt");
168 // Make sure the file does not exist at this point
Sadik Armagan1625efc2021-06-10 18:24:34 +0100169 CHECK(!fs::exists(tempPath));
Jan Eilersf78c7672020-07-01 18:09:39 +0100170
Kevin Mayd92a6e42021-02-04 10:27:41 +0000171 armnn::IRuntime::CreationOptions options;
Jan Eilersf78c7672020-07-01 18:09:39 +0100172 options.m_ProfilingOptions.m_EnableProfiling = true;
173 options.m_ProfilingOptions.m_FileOnly = true;
174 options.m_ProfilingOptions.m_IncomingCaptureFile = "";
175 options.m_ProfilingOptions.m_OutgoingCaptureFile = tempPath.string();
176 options.m_ProfilingOptions.m_CapturePeriod = 100;
177 options.m_ProfilingOptions.m_TimelineEnabled = true;
178
179 ILocalPacketHandlerSharedPtr localPacketHandlerPtr = std::make_shared<TestTimelinePacketHandler>();
180 options.m_ProfilingOptions.m_LocalPacketHandlers.push_back(localPacketHandlerPtr);
181
Kevin Mayd92a6e42021-02-04 10:27:41 +0000182 armnn::RuntimeImpl runtime(options);
Jan Eilersf78c7672020-07-01 18:09:39 +0100183 // ensure the GUID generator is reset to zero
184 GetProfilingService(&runtime).ResetGuidGenerator();
185
186 // Load a simple network
187 // build up the structure of the network
188 INetworkPtr net(INetwork::Create());
189
190 IConnectableLayer* input = net->AddInputLayer(0, "input");
191
192 ElementwiseUnaryDescriptor descriptor(UnaryOperation::Rsqrt);
193 IConnectableLayer* Rsqrt = net->AddElementwiseUnaryLayer(descriptor, "Rsqrt");
194
195 IConnectableLayer* output = net->AddOutputLayer(0, "output");
196
197 input->GetOutputSlot(0).Connect(Rsqrt->GetInputSlot(0));
198 Rsqrt->GetOutputSlot(0).Connect(output->GetInputSlot(0));
199
200 input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
201 Rsqrt->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
202
203
204 std::vector<BackendId> backendsVec{backend};
205 IOptimizedNetworkPtr optNet = Optimize(*net, backendsVec, runtime.GetDeviceSpec());
206
207 // Load it into the runtime. It should succeed.
208 armnn::NetworkId netId;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100209 CHECK(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success);
Jan Eilersf78c7672020-07-01 18:09:39 +0100210
211 // Creates structures for input & output.
212 std::vector<float> inputData(16);
213 std::vector<float> outputData(16);
214 for (unsigned int i = 0; i < 16; ++i) {
215 inputData[i] = 9.0;
216 outputData[i] = 3.0;
217 }
218
Cathal Corbett5b8093c2021-10-22 11:12:07 +0100219 TensorInfo inputTensorInfo = runtime.GetInputTensorInfo(netId, 0);
220 inputTensorInfo.SetConstant(true);
Jan Eilersf78c7672020-07-01 18:09:39 +0100221 InputTensors inputTensors
222 {
Cathal Corbett5b8093c2021-10-22 11:12:07 +0100223 {0, ConstTensor(inputTensorInfo, inputData.data())}
Jan Eilersf78c7672020-07-01 18:09:39 +0100224 };
225 OutputTensors outputTensors
226 {
227 {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())}
228 };
229
230 // Does the inference.
231 runtime.EnqueueWorkload(netId, inputTensors, outputTensors);
232
233 static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000);
234
235 // In order to flush the files we need to gracefully close the profiling service.
236 options.m_ProfilingOptions.m_EnableProfiling = false;
Jim Flynn4c9ed1d2022-01-23 23:57:20 +0000237 GetProfilingService(&runtime).ResetExternalProfilingOptions(
238 ConvertExternalProfilingOptions(options.m_ProfilingOptions), true);
Jan Eilersf78c7672020-07-01 18:09:39 +0100239
240 // The output file size should be greater than 0.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100241 CHECK(fs::file_size(tempPath) > 0);
Jan Eilersf78c7672020-07-01 18:09:39 +0100242
243 // NOTE: would be an interesting exercise to take this file and decode it
244
245 // Delete the tmp file.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100246 CHECK(fs::remove(tempPath));
Keith Davis3201eea2019-10-24 17:30:41 +0100247 }
Keith Davis3201eea2019-10-24 17:30:41 +0100248}
249
Sadik Armagan1625efc2021-06-10 18:24:34 +0100250}