blob: de84bb4999a42750da079c57f51d6e77388f5f15 [file] [log] [blame]
surmeh0176660052018-03-29 16:33:54 +01001//
2// Copyright © 2017 Arm Ltd. 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 <boost/test/unit_test.hpp>
8#include <log/log.h>
9
surmeh0176660052018-03-29 16:33:54 +010010#include "../Utils.hpp"
Francis Murtaghd5232962021-02-12 15:01:22 +000011#include <armnn/src/armnn/OptimizedNetworkImpl.hpp>
surmeh0176660052018-03-29 16:33:54 +010012
13#include <fstream>
14#include <iomanip>
Francis Murtaghd5232962021-02-12 15:01:22 +000015#include <memory>
surmeh0176660052018-03-29 16:33:54 +010016#include <armnn/INetwork.hpp>
Francis Murtaghd5232962021-02-12 15:01:22 +000017#include "armnn/NetworkFwd.hpp"
surmeh0176660052018-03-29 16:33:54 +010018
Colm Donelan08d9a1c2020-09-09 17:56:55 +010019#include <Filesystem.hpp>
20
surmeh0176660052018-03-29 16:33:54 +010021
surmeh0176660052018-03-29 16:33:54 +010022using namespace android;
telsoa01ce3e84a2018-08-31 09:31:35 +010023using namespace android::nn;
24using namespace android::hardware;
25using namespace armnn_driver;
surmeh0176660052018-03-29 16:33:54 +010026
Francis Murtaghd5232962021-02-12 15:01:22 +000027namespace armnn
28{
29
30class Graph
31{
32public:
33 Graph(Graph&& graph) = default;
34};
35
36class MockOptimizedNetworkImpl final : public ::armnn::OptimizedNetworkImpl
37{
38public:
39 MockOptimizedNetworkImpl(const std::string& mockSerializedContent, std::unique_ptr<armnn::Graph>)
40 : ::armnn::OptimizedNetworkImpl(nullptr)
41 , m_MockSerializedContent(mockSerializedContent)
42 {}
43 ~MockOptimizedNetworkImpl() {}
44
45 ::armnn::Status PrintGraph() override { return ::armnn::Status::Failure; }
46 ::armnn::Status SerializeToDot(std::ostream& stream) const override
47 {
48 stream << m_MockSerializedContent;
49
50 return stream.good() ? ::armnn::Status::Success : ::armnn::Status::Failure;
51 }
52
53 ::armnn::profiling::ProfilingGuid GetGuid() const final { return ::armnn::profiling::ProfilingGuid(0); }
54
55 void UpdateMockSerializedContent(const std::string& mockSerializedContent)
56 {
57 this->m_MockSerializedContent = mockSerializedContent;
58 }
59
60private:
61 std::string m_MockSerializedContent;
62};
63
64
65} // armnn namespace
66
67BOOST_AUTO_TEST_SUITE(UtilsTests)
68
surmeh0176660052018-03-29 16:33:54 +010069// The following are helpers for writing unit tests for the driver.
70namespace
71{
72
73struct ExportNetworkGraphFixture
74{
75public:
76 // Setup: set the output dump directory and an empty dummy model (as only its memory address is used).
telsoa01ce3e84a2018-08-31 09:31:35 +010077 // Defaulting the output dump directory to "/data" because it should exist and be writable in all deployments.
surmeh0176660052018-03-29 16:33:54 +010078 ExportNetworkGraphFixture()
telsoa01ce3e84a2018-08-31 09:31:35 +010079 : ExportNetworkGraphFixture("/data")
surmeh0176660052018-03-29 16:33:54 +010080 {}
81 ExportNetworkGraphFixture(const std::string& requestInputsAndOutputsDumpDir)
82 : m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir)
surmeh0176660052018-03-29 16:33:54 +010083 , m_FileName()
84 , m_FileStream()
85 {
surmeh0176660052018-03-29 16:33:54 +010086 // Set the name of the output .dot file.
Jim Flynn829ad302019-12-13 14:43:24 +000087 // NOTE: the export now uses a time stamp to name the file so we
88 // can't predict ahead of time what the file name will be.
89 std::string timestamp = "dummy";
Colm Donelan08d9a1c2020-09-09 17:56:55 +010090 m_FileName = m_RequestInputsAndOutputsDumpDir / (timestamp + "_networkgraph.dot");
surmeh0176660052018-03-29 16:33:54 +010091 }
92
93 // Teardown: delete the dump file regardless of the outcome of the tests.
94 ~ExportNetworkGraphFixture()
95 {
96 // Close the file stream.
97 m_FileStream.close();
98
99 // Ignore any error (such as file not found).
surmeh0149b9e102018-05-17 14:11:25 +0100100 (void)remove(m_FileName.c_str());
surmeh0176660052018-03-29 16:33:54 +0100101 }
102
103 bool FileExists()
104 {
105 // Close any file opened in a previous session.
106 if (m_FileStream.is_open())
107 {
108 m_FileStream.close();
109 }
110
Jim Flynn829ad302019-12-13 14:43:24 +0000111 if (m_FileName.empty())
112 {
113 return false;
114 }
115
surmeh0176660052018-03-29 16:33:54 +0100116 // Open the file.
117 m_FileStream.open(m_FileName, std::ifstream::in);
118
119 // Check that the file is open.
120 if (!m_FileStream.is_open())
121 {
122 return false;
123 }
124
125 // Check that the stream is readable.
126 return m_FileStream.good();
127 }
128
129 std::string GetFileContent()
130 {
131 // Check that the stream is readable.
132 if (!m_FileStream.good())
133 {
134 return "";
135 }
136
137 // Get all the contents of the file.
138 return std::string((std::istreambuf_iterator<char>(m_FileStream)),
139 (std::istreambuf_iterator<char>()));
140 }
141
Colm Donelan08d9a1c2020-09-09 17:56:55 +0100142 fs::path m_RequestInputsAndOutputsDumpDir;
143 fs::path m_FileName;
surmeh0176660052018-03-29 16:33:54 +0100144
145private:
surmeh0176660052018-03-29 16:33:54 +0100146 std::ifstream m_FileStream;
147};
148
surmeh0176660052018-03-29 16:33:54 +0100149
surmeh0176660052018-03-29 16:33:54 +0100150
151} // namespace
152
153BOOST_AUTO_TEST_CASE(ExportToEmptyDirectory)
154{
155 // Set the fixture for this test.
156 ExportNetworkGraphFixture fixture("");
157
158 // Set a mock content for the optimized network.
159 std::string mockSerializedContent = "This is a mock serialized content.";
160
161 // Set a mock optimized network.
Francis Murtaghd5232962021-02-12 15:01:22 +0000162 std::unique_ptr<armnn::Graph> graphPtr;
163
164 std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
165 new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
166 ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));
surmeh0176660052018-03-29 16:33:54 +0100167
168 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000169 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
170 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100171
172 // Check that the output file does not exist.
173 BOOST_TEST(!fixture.FileExists());
174}
175
176BOOST_AUTO_TEST_CASE(ExportNetwork)
177{
178 // Set the fixture for this test.
179 ExportNetworkGraphFixture fixture;
180
181 // Set a mock content for the optimized network.
182 std::string mockSerializedContent = "This is a mock serialized content.";
183
184 // Set a mock optimized network.
Francis Murtaghd5232962021-02-12 15:01:22 +0000185 std::unique_ptr<armnn::Graph> graphPtr;
186
187 std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
188 new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
189 ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));
190
surmeh0176660052018-03-29 16:33:54 +0100191
192 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000193 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
194 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100195
196 // Check that the output file exists and that it has the correct name.
197 BOOST_TEST(fixture.FileExists());
198
199 // Check that the content of the output file matches the mock content.
200 BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
201}
202
203BOOST_AUTO_TEST_CASE(ExportNetworkOverwriteFile)
204{
205 // Set the fixture for this test.
206 ExportNetworkGraphFixture fixture;
207
208 // Set a mock content for the optimized network.
209 std::string mockSerializedContent = "This is a mock serialized content.";
210
211 // Set a mock optimized network.
Francis Murtaghd5232962021-02-12 15:01:22 +0000212 std::unique_ptr<armnn::Graph> graphPtr;
213
214 std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
215 new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
216 ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));
surmeh0176660052018-03-29 16:33:54 +0100217
218 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000219 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
220 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100221
222 // Check that the output file exists and that it has the correct name.
223 BOOST_TEST(fixture.FileExists());
224
225 // Check that the content of the output file matches the mock content.
226 BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
227
228 // Update the mock serialized content of the network.
229 mockSerializedContent = "This is ANOTHER mock serialized content!";
Francis Murtaghd5232962021-02-12 15:01:22 +0000230 std::unique_ptr<armnn::Graph> graphPtr2;
231 std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl2(
232 new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr2)));
233 static_cast<armnn::MockOptimizedNetworkImpl*>(mockImpl2.get())->UpdateMockSerializedContent(mockSerializedContent);
234 ::armnn::IOptimizedNetwork mockOptimizedNetwork2(std::move(mockImpl2));
surmeh0176660052018-03-29 16:33:54 +0100235
236 // Export the mock optimized network.
Francis Murtaghd5232962021-02-12 15:01:22 +0000237 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork2,
Jim Flynn829ad302019-12-13 14:43:24 +0000238 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100239
240 // Check that the output file still exists and that it has the correct name.
241 BOOST_TEST(fixture.FileExists());
242
243 // Check that the content of the output file matches the mock content.
244 BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
245}
246
247BOOST_AUTO_TEST_CASE(ExportMultipleNetworks)
248{
249 // Set the fixtures for this test.
250 ExportNetworkGraphFixture fixture1;
251 ExportNetworkGraphFixture fixture2;
252 ExportNetworkGraphFixture fixture3;
253
254 // Set a mock content for the optimized network.
255 std::string mockSerializedContent = "This is a mock serialized content.";
256
257 // Set a mock optimized network.
Francis Murtaghd5232962021-02-12 15:01:22 +0000258 std::unique_ptr<armnn::Graph> graphPtr;
259
260 std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
261 new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
262 ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));
surmeh0176660052018-03-29 16:33:54 +0100263
264 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000265 fixture1.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
266 fixture1.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100267
268 // Check that the output file exists and that it has the correct name.
269 BOOST_TEST(fixture1.FileExists());
270
271 // Check that the content of the output file matches the mock content.
272 BOOST_TEST(fixture1.GetFileContent() == mockSerializedContent);
273
274 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000275 fixture2.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
276 fixture2.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100277
278 // Check that the output file exists and that it has the correct name.
279 BOOST_TEST(fixture2.FileExists());
280
281 // Check that the content of the output file matches the mock content.
282 BOOST_TEST(fixture2.GetFileContent() == mockSerializedContent);
283
284 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000285 fixture3.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
286 fixture3.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100287 // Check that the output file exists and that it has the correct name.
288 BOOST_TEST(fixture3.FileExists());
289
290 // Check that the content of the output file matches the mock content.
291 BOOST_TEST(fixture3.GetFileContent() == mockSerializedContent);
292}
293
294BOOST_AUTO_TEST_SUITE_END()