blob: 5827c0db9bf848978e8130429ee94af8d378ca16 [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
Jim Flynn4e755a52020-03-29 17:48:26 +01006#include <Filesystem.hpp>
Jim Flynn6398a982020-05-27 17:05:21 +01007#include <LabelsAndEventClasses.hpp>
Aron Virginas-Tar8bf442e2019-11-07 18:41:40 +00008#include <ProfilingService.hpp>
Jim Flynn6398a982020-05-27 17:05:21 +01009#include "ProfilingTestUtils.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
Keith Davis3201eea2019-10-24 17:30:41 +010016#include <cstdio>
Keith Davis3201eea2019-10-24 17:30:41 +010017#include <sstream>
18#include <sys/stat.h>
19
Keith Davis3201eea2019-10-24 17:30:41 +010020using namespace armnn::profiling;
21using namespace armnn;
22
23using namespace std::chrono_literals;
24
Finn Williams09ad6f92019-12-19 17:05:18 +000025class FileOnlyHelperService : public ProfilingService
26{
27 public:
28 // Wait for a notification from the send thread
29 bool WaitForPacketsSent(uint32_t timeout = 1000)
30 {
Sadik Armagan3184c902020-03-18 10:57:30 +000031 return ProfilingService::WaitForPacketSent(m_ProfilingService, timeout);
Finn Williams09ad6f92019-12-19 17:05:18 +000032 }
Sadik Armagan3184c902020-03-18 10:57:30 +000033 armnn::profiling::ProfilingService m_ProfilingService;
Finn Williams09ad6f92019-12-19 17:05:18 +000034};
35
Sadik Armagan1625efc2021-06-10 18:24:34 +010036TEST_SUITE("FileOnlyProfilingDecoratorTests")
37{
38TEST_CASE("TestFileOnlyProfiling")
Jim Flynn4e755a52020-03-29 17:48:26 +010039{
Jan Eilersf78c7672020-07-01 18:09:39 +010040 // Get all registered backends
41 std::vector<BackendId> suitableBackends = GetSuitableBackendRegistered();
42
43 // Run test for each backend separately
44 for (auto const& backend : suitableBackends)
Finn Williams0c32ccf2020-05-12 13:37:06 +010045 {
Jan Eilersf78c7672020-07-01 18:09:39 +010046 // Enable m_FileOnly but also provide ILocalPacketHandler which should consume the packets.
47 // This won't dump anything to file.
Kevin Mayd92a6e42021-02-04 10:27:41 +000048 armnn::IRuntime::CreationOptions creationOptions;
Jan Eilersf78c7672020-07-01 18:09:39 +010049 creationOptions.m_ProfilingOptions.m_EnableProfiling = true;
50 creationOptions.m_ProfilingOptions.m_FileOnly = true;
51 creationOptions.m_ProfilingOptions.m_CapturePeriod = 100;
52 creationOptions.m_ProfilingOptions.m_TimelineEnabled = true;
53 ILocalPacketHandlerSharedPtr localPacketHandlerPtr = std::make_shared<TestTimelinePacketHandler>();
54 creationOptions.m_ProfilingOptions.m_LocalPacketHandlers.push_back(localPacketHandlerPtr);
55
Kevin Mayd92a6e42021-02-04 10:27:41 +000056 armnn::RuntimeImpl runtime(creationOptions);
Jan Eilersf78c7672020-07-01 18:09:39 +010057 // ensure the GUID generator is reset to zero
58 GetProfilingService(&runtime).ResetGuidGenerator();
59
60 // Load a simple network
61 // build up the structure of the network
62 INetworkPtr net(INetwork::Create());
63
64 IConnectableLayer* input = net->AddInputLayer(0, "input");
65
66 ElementwiseUnaryDescriptor descriptor(UnaryOperation::Rsqrt);
67 IConnectableLayer* Rsqrt = net->AddElementwiseUnaryLayer(descriptor, "Rsqrt");
68
69 IConnectableLayer* output = net->AddOutputLayer(0, "output");
70
71 input->GetOutputSlot(0).Connect(Rsqrt->GetInputSlot(0));
72 Rsqrt->GetOutputSlot(0).Connect(output->GetInputSlot(0));
73
74 input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
75 Rsqrt->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
76
77 std::vector<armnn::BackendId> backendsVec {backend};
78 IOptimizedNetworkPtr optNet = Optimize(*net, backendsVec, runtime.GetDeviceSpec());
79
80 // Load it into the runtime. It should succeed.
81 armnn::NetworkId netId;
Sadik Armagan1625efc2021-06-10 18:24:34 +010082 CHECK(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success);
Jan Eilersf78c7672020-07-01 18:09:39 +010083
84 // Creates structures for input & output.
85 std::vector<float> inputData(16);
86 std::vector<float> outputData(16);
87 for (unsigned int i = 0; i < 16; ++i) {
88 inputData[i] = 9.0;
89 outputData[i] = 3.0;
90 }
91
92 InputTensors inputTensors
93 {
94 {0, ConstTensor(runtime.GetInputTensorInfo(netId, 0), inputData.data())}
95 };
96 OutputTensors outputTensors
97 {
98 {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())}
99 };
100
101 // Does the inference.
102 runtime.EnqueueWorkload(netId, inputTensors, outputTensors);
103
104 static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000);
105
106 const TimelineModel &model =
107 static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->GetTimelineModel();
108
109 for (auto &error : model.GetErrors()) {
110 std::cout << error.what() << std::endl;
111 }
Sadik Armagan1625efc2021-06-10 18:24:34 +0100112 CHECK(model.GetErrors().empty());
Jan Eilersf78c7672020-07-01 18:09:39 +0100113 std::vector<std::string> desc = GetModelDescription(model);
114 std::vector<std::string> expectedOutput;
115 expectedOutput.push_back("Entity [0] name = input type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100116 expectedOutput.push_back(" connection [17] from entity [0] to entity [1]");
117 expectedOutput.push_back(" child: Entity [26] backendId = " + backend.Get() + " type = workload");
Jan Eilersf78c7672020-07-01 18:09:39 +0100118 expectedOutput.push_back("Entity [1] name = Rsqrt type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100119 expectedOutput.push_back(" connection [25] from entity [1] to entity [2]");
120 expectedOutput.push_back(" child: Entity [18] backendId = " + backend.Get() + " type = workload");
Jan Eilersf78c7672020-07-01 18:09:39 +0100121 expectedOutput.push_back("Entity [2] name = output type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100122 expectedOutput.push_back(" child: Entity [30] backendId = " + backend.Get() + " type = workload");
123 expectedOutput.push_back("Entity [6] processId = [processId] type = network");
Jan Eilersf78c7672020-07-01 18:09:39 +0100124 expectedOutput.push_back(" child: Entity [0] name = input type = layer");
125 expectedOutput.push_back(" child: Entity [1] name = Rsqrt type = layer");
126 expectedOutput.push_back(" child: Entity [2] name = output type = layer");
Jim Flynnf7713212020-07-14 09:50:59 +0100127 expectedOutput.push_back(" execution: Entity [34] type = inference");
128 expectedOutput.push_back(" event: [8] class [start_of_life]");
129 expectedOutput.push_back("Entity [18] backendId = " + backend.Get() + " type = workload");
130 expectedOutput.push_back(" execution: Entity [47] type = workload_execution");
131 expectedOutput.push_back("Entity [26] backendId = " + backend.Get() + " type = workload");
132 expectedOutput.push_back(" execution: Entity [39] type = workload_execution");
133 expectedOutput.push_back("Entity [30] backendId = " + backend.Get() + " type = workload");
134 expectedOutput.push_back(" execution: Entity [55] type = workload_execution");
135 expectedOutput.push_back("Entity [34] type = inference");
136 expectedOutput.push_back(" child: Entity [39] type = workload_execution");
137 expectedOutput.push_back(" child: Entity [47] type = workload_execution");
138 expectedOutput.push_back(" child: Entity [55] type = workload_execution");
139 expectedOutput.push_back(" event: [37] class [start_of_life]");
140 expectedOutput.push_back(" event: [63] class [end_of_life]");
141 expectedOutput.push_back("Entity [39] type = workload_execution");
142 expectedOutput.push_back(" event: [43] class [start_of_life]");
143 expectedOutput.push_back(" event: [45] class [end_of_life]");
144 expectedOutput.push_back("Entity [47] type = workload_execution");
145 expectedOutput.push_back(" event: [51] class [start_of_life]");
146 expectedOutput.push_back(" event: [53] class [end_of_life]");
147 expectedOutput.push_back("Entity [55] type = workload_execution");
148 expectedOutput.push_back(" event: [59] class [start_of_life]");
149 expectedOutput.push_back(" event: [61] class [end_of_life]");
Sadik Armagan1625efc2021-06-10 18:24:34 +0100150 CHECK(CompareOutput(desc, expectedOutput));
Finn Williams0c32ccf2020-05-12 13:37:06 +0100151 }
Jim Flynn4e755a52020-03-29 17:48:26 +0100152}
153
Sadik Armagan1625efc2021-06-10 18:24:34 +0100154TEST_CASE("DumpOutgoingValidFileEndToEnd")
Keith Davis3201eea2019-10-24 17:30:41 +0100155{
Jan Eilersf78c7672020-07-01 18:09:39 +0100156 // Get all registered backends
157 std::vector<BackendId> suitableBackends = GetSuitableBackendRegistered();
158
159 // Run test for each backend separately
160 for (auto const& backend : suitableBackends)
Keith Davis3201eea2019-10-24 17:30:41 +0100161 {
Jan Eilersf78c7672020-07-01 18:09:39 +0100162 // Create a temporary file name.
163 fs::path tempPath = armnnUtils::Filesystem::NamedTempFile("DumpOutgoingValidFileEndToEnd_CaptureFile.txt");
164 // Make sure the file does not exist at this point
Sadik Armagan1625efc2021-06-10 18:24:34 +0100165 CHECK(!fs::exists(tempPath));
Jan Eilersf78c7672020-07-01 18:09:39 +0100166
Kevin Mayd92a6e42021-02-04 10:27:41 +0000167 armnn::IRuntime::CreationOptions options;
Jan Eilersf78c7672020-07-01 18:09:39 +0100168 options.m_ProfilingOptions.m_EnableProfiling = true;
169 options.m_ProfilingOptions.m_FileOnly = true;
170 options.m_ProfilingOptions.m_IncomingCaptureFile = "";
171 options.m_ProfilingOptions.m_OutgoingCaptureFile = tempPath.string();
172 options.m_ProfilingOptions.m_CapturePeriod = 100;
173 options.m_ProfilingOptions.m_TimelineEnabled = true;
174
175 ILocalPacketHandlerSharedPtr localPacketHandlerPtr = std::make_shared<TestTimelinePacketHandler>();
176 options.m_ProfilingOptions.m_LocalPacketHandlers.push_back(localPacketHandlerPtr);
177
Kevin Mayd92a6e42021-02-04 10:27:41 +0000178 armnn::RuntimeImpl runtime(options);
Jan Eilersf78c7672020-07-01 18:09:39 +0100179 // ensure the GUID generator is reset to zero
180 GetProfilingService(&runtime).ResetGuidGenerator();
181
182 // Load a simple network
183 // build up the structure of the network
184 INetworkPtr net(INetwork::Create());
185
186 IConnectableLayer* input = net->AddInputLayer(0, "input");
187
188 ElementwiseUnaryDescriptor descriptor(UnaryOperation::Rsqrt);
189 IConnectableLayer* Rsqrt = net->AddElementwiseUnaryLayer(descriptor, "Rsqrt");
190
191 IConnectableLayer* output = net->AddOutputLayer(0, "output");
192
193 input->GetOutputSlot(0).Connect(Rsqrt->GetInputSlot(0));
194 Rsqrt->GetOutputSlot(0).Connect(output->GetInputSlot(0));
195
196 input->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
197 Rsqrt->GetOutputSlot(0).SetTensorInfo(TensorInfo({ 1, 1, 4, 4 }, DataType::Float32));
198
199
200 std::vector<BackendId> backendsVec{backend};
201 IOptimizedNetworkPtr optNet = Optimize(*net, backendsVec, runtime.GetDeviceSpec());
202
203 // Load it into the runtime. It should succeed.
204 armnn::NetworkId netId;
Sadik Armagan1625efc2021-06-10 18:24:34 +0100205 CHECK(runtime.LoadNetwork(netId, std::move(optNet)) == Status::Success);
Jan Eilersf78c7672020-07-01 18:09:39 +0100206
207 // Creates structures for input & output.
208 std::vector<float> inputData(16);
209 std::vector<float> outputData(16);
210 for (unsigned int i = 0; i < 16; ++i) {
211 inputData[i] = 9.0;
212 outputData[i] = 3.0;
213 }
214
215 InputTensors inputTensors
216 {
217 {0, ConstTensor(runtime.GetInputTensorInfo(netId, 0), inputData.data())}
218 };
219 OutputTensors outputTensors
220 {
221 {0, Tensor(runtime.GetOutputTensorInfo(netId, 0), outputData.data())}
222 };
223
224 // Does the inference.
225 runtime.EnqueueWorkload(netId, inputTensors, outputTensors);
226
227 static_cast<TestTimelinePacketHandler *>(localPacketHandlerPtr.get())->WaitOnInferenceCompletion(3000);
228
229 // In order to flush the files we need to gracefully close the profiling service.
230 options.m_ProfilingOptions.m_EnableProfiling = false;
231 GetProfilingService(&runtime).ResetExternalProfilingOptions(options.m_ProfilingOptions, true);
232
233 // The output file size should be greater than 0.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100234 CHECK(fs::file_size(tempPath) > 0);
Jan Eilersf78c7672020-07-01 18:09:39 +0100235
236 // NOTE: would be an interesting exercise to take this file and decode it
237
238 // Delete the tmp file.
Sadik Armagan1625efc2021-06-10 18:24:34 +0100239 CHECK(fs::remove(tempPath));
Keith Davis3201eea2019-10-24 17:30:41 +0100240 }
Keith Davis3201eea2019-10-24 17:30:41 +0100241}
242
Sadik Armagan1625efc2021-06-10 18:24:34 +0100243}