blob: 68d7b501cff5103486f07a56b8a8e6fa0ebb7056 [file] [log] [blame]
surmeh0176660052018-03-29 16:33:54 +01001//
Mike Kellye2d611e2021-10-14 12:35:58 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
surmeh0176660052018-03-29 16:33:54 +01004//
5
surmeh0149b9e102018-05-17 14:11:25 +01006#include "DriverTestHelpers.hpp"
surmeh0176660052018-03-29 16:33:54 +01007#include <log/log.h>
8
Francis Murtaghd5232962021-02-12 15:01:22 +00009#include <armnn/src/armnn/OptimizedNetworkImpl.hpp>
surmeh0176660052018-03-29 16:33:54 +010010
11#include <fstream>
Francis Murtaghd5232962021-02-12 15:01:22 +000012#include <memory>
surmeh0176660052018-03-29 16:33:54 +010013#include <armnn/INetwork.hpp>
14
Rob Hughes083be702021-07-19 15:29:47 +010015#include <armnnUtils/Filesystem.hpp>
Colm Donelan08d9a1c2020-09-09 17:56:55 +010016
surmeh0176660052018-03-29 16:33:54 +010017using namespace android;
telsoa01ce3e84a2018-08-31 09:31:35 +010018using namespace android::nn;
19using namespace android::hardware;
20using namespace armnn_driver;
surmeh0176660052018-03-29 16:33:54 +010021
Francis Murtaghd5232962021-02-12 15:01:22 +000022namespace armnn
23{
24
25class Graph
26{
27public:
28 Graph(Graph&& graph) = default;
29};
30
31class MockOptimizedNetworkImpl final : public ::armnn::OptimizedNetworkImpl
32{
33public:
34 MockOptimizedNetworkImpl(const std::string& mockSerializedContent, std::unique_ptr<armnn::Graph>)
35 : ::armnn::OptimizedNetworkImpl(nullptr)
36 , m_MockSerializedContent(mockSerializedContent)
37 {}
38 ~MockOptimizedNetworkImpl() {}
39
40 ::armnn::Status PrintGraph() override { return ::armnn::Status::Failure; }
41 ::armnn::Status SerializeToDot(std::ostream& stream) const override
42 {
43 stream << m_MockSerializedContent;
44
45 return stream.good() ? ::armnn::Status::Success : ::armnn::Status::Failure;
46 }
47
Cathal Corbettf45e3ab2022-02-28 10:06:26 +000048 ::arm::pipe::ProfilingGuid GetGuid() const final { return ::arm::pipe::ProfilingGuid(0); }
Francis Murtaghd5232962021-02-12 15:01:22 +000049
50 void UpdateMockSerializedContent(const std::string& mockSerializedContent)
51 {
52 this->m_MockSerializedContent = mockSerializedContent;
53 }
54
55private:
56 std::string m_MockSerializedContent;
57};
58
59
60} // armnn namespace
61
Francis Murtaghd5232962021-02-12 15:01:22 +000062
surmeh0176660052018-03-29 16:33:54 +010063// The following are helpers for writing unit tests for the driver.
64namespace
65{
66
67struct ExportNetworkGraphFixture
68{
69public:
70 // Setup: set the output dump directory and an empty dummy model (as only its memory address is used).
telsoa01ce3e84a2018-08-31 09:31:35 +010071 // Defaulting the output dump directory to "/data" because it should exist and be writable in all deployments.
surmeh0176660052018-03-29 16:33:54 +010072 ExportNetworkGraphFixture()
telsoa01ce3e84a2018-08-31 09:31:35 +010073 : ExportNetworkGraphFixture("/data")
surmeh0176660052018-03-29 16:33:54 +010074 {}
Sadik Armagan9150bff2021-05-26 15:40:53 +010075
surmeh0176660052018-03-29 16:33:54 +010076 ExportNetworkGraphFixture(const std::string& requestInputsAndOutputsDumpDir)
Sadik Armagan9150bff2021-05-26 15:40:53 +010077 : m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir), m_FileName(), m_FileStream()
surmeh0176660052018-03-29 16:33:54 +010078 {
surmeh0176660052018-03-29 16:33:54 +010079 // Set the name of the output .dot file.
Jim Flynn829ad302019-12-13 14:43:24 +000080 // NOTE: the export now uses a time stamp to name the file so we
81 // can't predict ahead of time what the file name will be.
82 std::string timestamp = "dummy";
Colm Donelan08d9a1c2020-09-09 17:56:55 +010083 m_FileName = m_RequestInputsAndOutputsDumpDir / (timestamp + "_networkgraph.dot");
surmeh0176660052018-03-29 16:33:54 +010084 }
85
86 // Teardown: delete the dump file regardless of the outcome of the tests.
87 ~ExportNetworkGraphFixture()
88 {
89 // Close the file stream.
90 m_FileStream.close();
91
92 // Ignore any error (such as file not found).
Sadik Armagan9150bff2021-05-26 15:40:53 +010093 (void) remove(m_FileName.c_str());
surmeh0176660052018-03-29 16:33:54 +010094 }
95
96 bool FileExists()
97 {
98 // Close any file opened in a previous session.
99 if (m_FileStream.is_open())
100 {
101 m_FileStream.close();
102 }
103
Jim Flynn829ad302019-12-13 14:43:24 +0000104 if (m_FileName.empty())
105 {
106 return false;
107 }
108
surmeh0176660052018-03-29 16:33:54 +0100109 // Open the file.
110 m_FileStream.open(m_FileName, std::ifstream::in);
111
112 // Check that the file is open.
113 if (!m_FileStream.is_open())
114 {
115 return false;
116 }
117
118 // Check that the stream is readable.
119 return m_FileStream.good();
120 }
121
122 std::string GetFileContent()
123 {
124 // Check that the stream is readable.
125 if (!m_FileStream.good())
126 {
127 return "";
128 }
129
130 // Get all the contents of the file.
131 return std::string((std::istreambuf_iterator<char>(m_FileStream)),
132 (std::istreambuf_iterator<char>()));
133 }
134
Colm Donelan08d9a1c2020-09-09 17:56:55 +0100135 fs::path m_RequestInputsAndOutputsDumpDir;
136 fs::path m_FileName;
surmeh0176660052018-03-29 16:33:54 +0100137
138private:
surmeh0176660052018-03-29 16:33:54 +0100139 std::ifstream m_FileStream;
140};
141
surmeh0176660052018-03-29 16:33:54 +0100142
surmeh0176660052018-03-29 16:33:54 +0100143} // namespace
144
Mike Kellye2d611e2021-10-14 12:35:58 +0100145DOCTEST_TEST_SUITE("UtilsTests")
Sadik Armagan9150bff2021-05-26 15:40:53 +0100146{
147
Mike Kellye2d611e2021-10-14 12:35:58 +0100148DOCTEST_TEST_CASE("ExportToEmptyDirectory")
surmeh0176660052018-03-29 16:33:54 +0100149{
150 // Set the fixture for this test.
151 ExportNetworkGraphFixture fixture("");
152
153 // Set a mock content for the optimized network.
154 std::string mockSerializedContent = "This is a mock serialized content.";
155
156 // Set a mock optimized network.
Francis Murtaghd5232962021-02-12 15:01:22 +0000157 std::unique_ptr<armnn::Graph> graphPtr;
158
159 std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
160 new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
161 ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));
surmeh0176660052018-03-29 16:33:54 +0100162
163 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000164 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
Sadik Armagan9150bff2021-05-26 15:40:53 +0100165 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100166
167 // Check that the output file does not exist.
Mike Kellye2d611e2021-10-14 12:35:58 +0100168 DOCTEST_CHECK(!fixture.FileExists());
surmeh0176660052018-03-29 16:33:54 +0100169}
170
Mike Kellye2d611e2021-10-14 12:35:58 +0100171DOCTEST_TEST_CASE("ExportNetwork")
surmeh0176660052018-03-29 16:33:54 +0100172{
173 // Set the fixture for this test.
174 ExportNetworkGraphFixture fixture;
175
176 // Set a mock content for the optimized network.
177 std::string mockSerializedContent = "This is a mock serialized content.";
178
179 // Set a mock optimized network.
Francis Murtaghd5232962021-02-12 15:01:22 +0000180 std::unique_ptr<armnn::Graph> graphPtr;
181
182 std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
183 new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
184 ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));
185
surmeh0176660052018-03-29 16:33:54 +0100186
187 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000188 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
Sadik Armagan9150bff2021-05-26 15:40:53 +0100189 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100190
191 // Check that the output file exists and that it has the correct name.
Mike Kellye2d611e2021-10-14 12:35:58 +0100192 DOCTEST_CHECK(fixture.FileExists());
surmeh0176660052018-03-29 16:33:54 +0100193
194 // Check that the content of the output file matches the mock content.
Mike Kellye2d611e2021-10-14 12:35:58 +0100195 DOCTEST_CHECK(fixture.GetFileContent() == mockSerializedContent);
surmeh0176660052018-03-29 16:33:54 +0100196}
197
Mike Kellye2d611e2021-10-14 12:35:58 +0100198DOCTEST_TEST_CASE("ExportNetworkOverwriteFile")
surmeh0176660052018-03-29 16:33:54 +0100199{
200 // Set the fixture for this test.
201 ExportNetworkGraphFixture fixture;
202
203 // Set a mock content for the optimized network.
204 std::string mockSerializedContent = "This is a mock serialized content.";
205
206 // Set a mock optimized network.
Francis Murtaghd5232962021-02-12 15:01:22 +0000207 std::unique_ptr<armnn::Graph> graphPtr;
208
209 std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
210 new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
211 ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));
surmeh0176660052018-03-29 16:33:54 +0100212
213 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000214 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
Sadik Armagan9150bff2021-05-26 15:40:53 +0100215 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100216
217 // Check that the output file exists and that it has the correct name.
Mike Kellye2d611e2021-10-14 12:35:58 +0100218 DOCTEST_CHECK(fixture.FileExists());
surmeh0176660052018-03-29 16:33:54 +0100219
220 // Check that the content of the output file matches the mock content.
Mike Kellye2d611e2021-10-14 12:35:58 +0100221 DOCTEST_CHECK(fixture.GetFileContent() == mockSerializedContent);
surmeh0176660052018-03-29 16:33:54 +0100222
223 // Update the mock serialized content of the network.
224 mockSerializedContent = "This is ANOTHER mock serialized content!";
Francis Murtaghd5232962021-02-12 15:01:22 +0000225 std::unique_ptr<armnn::Graph> graphPtr2;
226 std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl2(
227 new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr2)));
228 static_cast<armnn::MockOptimizedNetworkImpl*>(mockImpl2.get())->UpdateMockSerializedContent(mockSerializedContent);
229 ::armnn::IOptimizedNetwork mockOptimizedNetwork2(std::move(mockImpl2));
surmeh0176660052018-03-29 16:33:54 +0100230
231 // Export the mock optimized network.
Francis Murtaghd5232962021-02-12 15:01:22 +0000232 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork2,
Sadik Armagan9150bff2021-05-26 15:40:53 +0100233 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100234
235 // Check that the output file still exists and that it has the correct name.
Mike Kellye2d611e2021-10-14 12:35:58 +0100236 DOCTEST_CHECK(fixture.FileExists());
surmeh0176660052018-03-29 16:33:54 +0100237
238 // Check that the content of the output file matches the mock content.
Mike Kellye2d611e2021-10-14 12:35:58 +0100239 DOCTEST_CHECK(fixture.GetFileContent() == mockSerializedContent);
surmeh0176660052018-03-29 16:33:54 +0100240}
241
Mike Kellye2d611e2021-10-14 12:35:58 +0100242DOCTEST_TEST_CASE("ExportMultipleNetworks")
surmeh0176660052018-03-29 16:33:54 +0100243{
244 // Set the fixtures for this test.
245 ExportNetworkGraphFixture fixture1;
246 ExportNetworkGraphFixture fixture2;
247 ExportNetworkGraphFixture fixture3;
248
249 // Set a mock content for the optimized network.
250 std::string mockSerializedContent = "This is a mock serialized content.";
251
252 // Set a mock optimized network.
Francis Murtaghd5232962021-02-12 15:01:22 +0000253 std::unique_ptr<armnn::Graph> graphPtr;
254
255 std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
256 new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
257 ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));
surmeh0176660052018-03-29 16:33:54 +0100258
259 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000260 fixture1.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
Sadik Armagan9150bff2021-05-26 15:40:53 +0100261 fixture1.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100262
263 // Check that the output file exists and that it has the correct name.
Mike Kellye2d611e2021-10-14 12:35:58 +0100264 DOCTEST_CHECK(fixture1.FileExists());
surmeh0176660052018-03-29 16:33:54 +0100265
266 // Check that the content of the output file matches the mock content.
Mike Kellye2d611e2021-10-14 12:35:58 +0100267 DOCTEST_CHECK(fixture1.GetFileContent() == mockSerializedContent);
surmeh0176660052018-03-29 16:33:54 +0100268
269 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000270 fixture2.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
Sadik Armagan9150bff2021-05-26 15:40:53 +0100271 fixture2.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100272
273 // Check that the output file exists and that it has the correct name.
Mike Kellye2d611e2021-10-14 12:35:58 +0100274 DOCTEST_CHECK(fixture2.FileExists());
surmeh0176660052018-03-29 16:33:54 +0100275
276 // Check that the content of the output file matches the mock content.
Mike Kellye2d611e2021-10-14 12:35:58 +0100277 DOCTEST_CHECK(fixture2.GetFileContent() == mockSerializedContent);
surmeh0176660052018-03-29 16:33:54 +0100278
279 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000280 fixture3.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
Sadik Armagan9150bff2021-05-26 15:40:53 +0100281 fixture3.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100282 // Check that the output file exists and that it has the correct name.
Mike Kellye2d611e2021-10-14 12:35:58 +0100283 DOCTEST_CHECK(fixture3.FileExists());
surmeh0176660052018-03-29 16:33:54 +0100284
285 // Check that the content of the output file matches the mock content.
Mike Kellye2d611e2021-10-14 12:35:58 +0100286 DOCTEST_CHECK(fixture3.GetFileContent() == mockSerializedContent);
surmeh0176660052018-03-29 16:33:54 +0100287}
288
Sadik Armagan9150bff2021-05-26 15:40:53 +0100289}