blob: f337db77dead3969c64ec8418e79c3f1decdf03e [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)
38 , m_Model({})
39 , m_FileName()
40 , m_FileStream()
41 {
42 // Get the memory address of the model and convert it to a hex string (of at least a '0' character).
43 size_t modelAddress = uintptr_t(&m_Model);
44 std::stringstream ss;
45 ss << std::uppercase << std::hex << std::setfill('0') << std::setw(1) << modelAddress;
46 std::string modelAddressHexString = ss.str();
47
48 // Set the name of the output .dot file.
49 m_FileName = boost::str(boost::format("%1%/networkgraph_%2%.dot")
50 % m_RequestInputsAndOutputsDumpDir
51 % modelAddressHexString);
52 }
53
54 // Teardown: delete the dump file regardless of the outcome of the tests.
55 ~ExportNetworkGraphFixture()
56 {
57 // Close the file stream.
58 m_FileStream.close();
59
60 // Ignore any error (such as file not found).
surmeh0149b9e102018-05-17 14:11:25 +010061 (void)remove(m_FileName.c_str());
surmeh0176660052018-03-29 16:33:54 +010062 }
63
64 bool FileExists()
65 {
66 // Close any file opened in a previous session.
67 if (m_FileStream.is_open())
68 {
69 m_FileStream.close();
70 }
71
72 // Open the file.
73 m_FileStream.open(m_FileName, std::ifstream::in);
74
75 // Check that the file is open.
76 if (!m_FileStream.is_open())
77 {
78 return false;
79 }
80
81 // Check that the stream is readable.
82 return m_FileStream.good();
83 }
84
85 std::string GetFileContent()
86 {
87 // Check that the stream is readable.
88 if (!m_FileStream.good())
89 {
90 return "";
91 }
92
93 // Get all the contents of the file.
94 return std::string((std::istreambuf_iterator<char>(m_FileStream)),
95 (std::istreambuf_iterator<char>()));
96 }
97
98 std::string m_RequestInputsAndOutputsDumpDir;
Matteo Martincigh8b287c22018-09-07 09:25:10 +010099 V1_0::Model m_Model;
surmeh0176660052018-03-29 16:33:54 +0100100
101private:
102 std::string m_FileName;
103 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.
147 armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
148 fixture.m_RequestInputsAndOutputsDumpDir,
149 fixture.m_Model);
150
151 // Check that the output file does not exist.
152 BOOST_TEST(!fixture.FileExists());
153}
154
155BOOST_AUTO_TEST_CASE(ExportNetwork)
156{
157 // Set the fixture for this test.
158 ExportNetworkGraphFixture fixture;
159
160 // Set a mock content for the optimized network.
161 std::string mockSerializedContent = "This is a mock serialized content.";
162
163 // Set a mock optimized network.
164 MockOptimizedNetwork mockOptimizedNetwork(mockSerializedContent);
165
166 // Export the mock optimized network.
167 armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
168 fixture.m_RequestInputsAndOutputsDumpDir,
169 fixture.m_Model);
170
171 // Check that the output file exists and that it has the correct name.
172 BOOST_TEST(fixture.FileExists());
173
174 // Check that the content of the output file matches the mock content.
175 BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
176}
177
178BOOST_AUTO_TEST_CASE(ExportNetworkOverwriteFile)
179{
180 // Set the fixture for this test.
181 ExportNetworkGraphFixture fixture;
182
183 // Set a mock content for the optimized network.
184 std::string mockSerializedContent = "This is a mock serialized content.";
185
186 // Set a mock optimized network.
187 MockOptimizedNetwork mockOptimizedNetwork(mockSerializedContent);
188
189 // Export the mock optimized network.
190 armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
191 fixture.m_RequestInputsAndOutputsDumpDir,
192 fixture.m_Model);
193
194 // Check that the output file exists and that it has the correct name.
195 BOOST_TEST(fixture.FileExists());
196
197 // Check that the content of the output file matches the mock content.
198 BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
199
200 // Update the mock serialized content of the network.
201 mockSerializedContent = "This is ANOTHER mock serialized content!";
202 mockOptimizedNetwork.UpdateMockSerializedContent(mockSerializedContent);
203
204 // Export the mock optimized network.
205 armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
206 fixture.m_RequestInputsAndOutputsDumpDir,
207 fixture.m_Model);
208
209 // Check that the output file still exists and that it has the correct name.
210 BOOST_TEST(fixture.FileExists());
211
212 // Check that the content of the output file matches the mock content.
213 BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
214}
215
216BOOST_AUTO_TEST_CASE(ExportMultipleNetworks)
217{
218 // Set the fixtures for this test.
219 ExportNetworkGraphFixture fixture1;
220 ExportNetworkGraphFixture fixture2;
221 ExportNetworkGraphFixture fixture3;
222
223 // Set a mock content for the optimized network.
224 std::string mockSerializedContent = "This is a mock serialized content.";
225
226 // Set a mock optimized network.
227 MockOptimizedNetwork mockOptimizedNetwork(mockSerializedContent);
228
229 // Export the mock optimized network.
230 armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
231 fixture1.m_RequestInputsAndOutputsDumpDir,
232 fixture1.m_Model);
233
234 // Check that the output file exists and that it has the correct name.
235 BOOST_TEST(fixture1.FileExists());
236
237 // Check that the content of the output file matches the mock content.
238 BOOST_TEST(fixture1.GetFileContent() == mockSerializedContent);
239
240 // Export the mock optimized network.
241 armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
242 fixture2.m_RequestInputsAndOutputsDumpDir,
243 fixture2.m_Model);
244
245 // Check that the output file exists and that it has the correct name.
246 BOOST_TEST(fixture2.FileExists());
247
248 // Check that the content of the output file matches the mock content.
249 BOOST_TEST(fixture2.GetFileContent() == mockSerializedContent);
250
251 // Export the mock optimized network.
252 armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
253 fixture3.m_RequestInputsAndOutputsDumpDir,
254 fixture3.m_Model);
255 // Check that the output file exists and that it has the correct name.
256 BOOST_TEST(fixture3.FileExists());
257
258 // Check that the content of the output file matches the mock content.
259 BOOST_TEST(fixture3.GetFileContent() == mockSerializedContent);
260}
261
262BOOST_AUTO_TEST_SUITE_END()