blob: a11c82744aad326cbc3c06d00bcaaed2803de938 [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"
11
12#include <fstream>
13#include <iomanip>
14#include <boost/format.hpp>
15#include <armnn/INetwork.hpp>
16
17BOOST_AUTO_TEST_SUITE(UtilsTests)
18
surmeh0176660052018-03-29 16:33:54 +010019using namespace android;
telsoa01ce3e84a2018-08-31 09:31:35 +010020using namespace android::nn;
21using namespace android::hardware;
22using namespace armnn_driver;
surmeh0176660052018-03-29 16:33:54 +010023
24// The following are helpers for writing unit tests for the driver.
25namespace
26{
27
28struct ExportNetworkGraphFixture
29{
30public:
31 // Setup: set the output dump directory and an empty dummy model (as only its memory address is used).
telsoa01ce3e84a2018-08-31 09:31:35 +010032 // Defaulting the output dump directory to "/data" because it should exist and be writable in all deployments.
surmeh0176660052018-03-29 16:33:54 +010033 ExportNetworkGraphFixture()
telsoa01ce3e84a2018-08-31 09:31:35 +010034 : ExportNetworkGraphFixture("/data")
surmeh0176660052018-03-29 16:33:54 +010035 {}
36 ExportNetworkGraphFixture(const std::string& requestInputsAndOutputsDumpDir)
37 : m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir)
surmeh0176660052018-03-29 16:33:54 +010038 , m_FileName()
39 , m_FileStream()
40 {
surmeh0176660052018-03-29 16:33:54 +010041 // Set the name of the output .dot file.
Jim Flynn829ad302019-12-13 14:43:24 +000042 // NOTE: the export now uses a time stamp to name the file so we
43 // can't predict ahead of time what the file name will be.
44 std::string timestamp = "dummy";
45 m_FileName = boost::str(boost::format("%1%/%2%_networkgraph.dot")
surmeh0176660052018-03-29 16:33:54 +010046 % m_RequestInputsAndOutputsDumpDir
Jim Flynn829ad302019-12-13 14:43:24 +000047 % timestamp);
surmeh0176660052018-03-29 16:33:54 +010048 }
49
50 // Teardown: delete the dump file regardless of the outcome of the tests.
51 ~ExportNetworkGraphFixture()
52 {
53 // Close the file stream.
54 m_FileStream.close();
55
56 // Ignore any error (such as file not found).
surmeh0149b9e102018-05-17 14:11:25 +010057 (void)remove(m_FileName.c_str());
surmeh0176660052018-03-29 16:33:54 +010058 }
59
60 bool FileExists()
61 {
62 // Close any file opened in a previous session.
63 if (m_FileStream.is_open())
64 {
65 m_FileStream.close();
66 }
67
Jim Flynn829ad302019-12-13 14:43:24 +000068 if (m_FileName.empty())
69 {
70 return false;
71 }
72
surmeh0176660052018-03-29 16:33:54 +010073 // Open the file.
74 m_FileStream.open(m_FileName, std::ifstream::in);
75
76 // Check that the file is open.
77 if (!m_FileStream.is_open())
78 {
79 return false;
80 }
81
82 // Check that the stream is readable.
83 return m_FileStream.good();
84 }
85
86 std::string GetFileContent()
87 {
88 // Check that the stream is readable.
89 if (!m_FileStream.good())
90 {
91 return "";
92 }
93
94 // Get all the contents of the file.
95 return std::string((std::istreambuf_iterator<char>(m_FileStream)),
96 (std::istreambuf_iterator<char>()));
97 }
98
99 std::string m_RequestInputsAndOutputsDumpDir;
Jim Flynn829ad302019-12-13 14:43:24 +0000100 std::string m_FileName;
surmeh0176660052018-03-29 16:33:54 +0100101
102private:
surmeh0176660052018-03-29 16:33:54 +0100103 std::ifstream m_FileStream;
104};
105
106class MockOptimizedNetwork final : public armnn::IOptimizedNetwork
107{
108public:
109 MockOptimizedNetwork(const std::string& mockSerializedContent)
110 : m_MockSerializedContent(mockSerializedContent)
111 {}
112 ~MockOptimizedNetwork() {}
113
114 armnn::Status PrintGraph() override { return armnn::Status::Failure; }
115 armnn::Status SerializeToDot(std::ostream& stream) const override
116 {
117 stream << m_MockSerializedContent;
118
119 return stream.good() ? armnn::Status::Success : armnn::Status::Failure;
120 }
121
janeil01f76a8762019-11-06 12:44:26 +0000122 armnn::profiling::ProfilingGuid GetGuid() const final { return armnn::profiling::ProfilingGuid(0); }
123
surmeh0176660052018-03-29 16:33:54 +0100124 void UpdateMockSerializedContent(const std::string& mockSerializedContent)
125 {
126 this->m_MockSerializedContent = mockSerializedContent;
127 }
128
129private:
130 std::string m_MockSerializedContent;
131};
132
133} // namespace
134
135BOOST_AUTO_TEST_CASE(ExportToEmptyDirectory)
136{
137 // Set the fixture for this test.
138 ExportNetworkGraphFixture fixture("");
139
140 // Set a mock content for the optimized network.
141 std::string mockSerializedContent = "This is a mock serialized content.";
142
143 // Set a mock optimized network.
144 MockOptimizedNetwork mockOptimizedNetwork(mockSerializedContent);
145
146 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000147 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
148 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100149
150 // Check that the output file does not exist.
151 BOOST_TEST(!fixture.FileExists());
152}
153
154BOOST_AUTO_TEST_CASE(ExportNetwork)
155{
156 // Set the fixture for this test.
157 ExportNetworkGraphFixture fixture;
158
159 // Set a mock content for the optimized network.
160 std::string mockSerializedContent = "This is a mock serialized content.";
161
162 // Set a mock optimized network.
163 MockOptimizedNetwork mockOptimizedNetwork(mockSerializedContent);
164
165 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000166 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
167 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100168
169 // Check that the output file exists and that it has the correct name.
170 BOOST_TEST(fixture.FileExists());
171
172 // Check that the content of the output file matches the mock content.
173 BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
174}
175
176BOOST_AUTO_TEST_CASE(ExportNetworkOverwriteFile)
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.
185 MockOptimizedNetwork mockOptimizedNetwork(mockSerializedContent);
186
187 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000188 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
189 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100190
191 // Check that the output file exists and that it has the correct name.
192 BOOST_TEST(fixture.FileExists());
193
194 // Check that the content of the output file matches the mock content.
195 BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
196
197 // Update the mock serialized content of the network.
198 mockSerializedContent = "This is ANOTHER mock serialized content!";
199 mockOptimizedNetwork.UpdateMockSerializedContent(mockSerializedContent);
200
201 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000202 fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
203 fixture.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100204
205 // Check that the output file still exists and that it has the correct name.
206 BOOST_TEST(fixture.FileExists());
207
208 // Check that the content of the output file matches the mock content.
209 BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
210}
211
212BOOST_AUTO_TEST_CASE(ExportMultipleNetworks)
213{
214 // Set the fixtures for this test.
215 ExportNetworkGraphFixture fixture1;
216 ExportNetworkGraphFixture fixture2;
217 ExportNetworkGraphFixture fixture3;
218
219 // Set a mock content for the optimized network.
220 std::string mockSerializedContent = "This is a mock serialized content.";
221
222 // Set a mock optimized network.
223 MockOptimizedNetwork mockOptimizedNetwork(mockSerializedContent);
224
225 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000226 fixture1.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
227 fixture1.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100228
229 // Check that the output file exists and that it has the correct name.
230 BOOST_TEST(fixture1.FileExists());
231
232 // Check that the content of the output file matches the mock content.
233 BOOST_TEST(fixture1.GetFileContent() == mockSerializedContent);
234
235 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000236 fixture2.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
237 fixture2.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100238
239 // Check that the output file exists and that it has the correct name.
240 BOOST_TEST(fixture2.FileExists());
241
242 // Check that the content of the output file matches the mock content.
243 BOOST_TEST(fixture2.GetFileContent() == mockSerializedContent);
244
245 // Export the mock optimized network.
Jim Flynn829ad302019-12-13 14:43:24 +0000246 fixture3.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
247 fixture3.m_RequestInputsAndOutputsDumpDir);
surmeh0176660052018-03-29 16:33:54 +0100248 // Check that the output file exists and that it has the correct name.
249 BOOST_TEST(fixture3.FileExists());
250
251 // Check that the content of the output file matches the mock content.
252 BOOST_TEST(fixture3.GetFileContent() == mockSerializedContent);
253}
254
255BOOST_AUTO_TEST_SUITE_END()