surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | 93e4898 | 2018-09-05 13:05:09 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 4 | // |
| 5 | |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 6 | #include "DriverTestHelpers.hpp" |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 7 | #include <boost/test/unit_test.hpp> |
| 8 | #include <log/log.h> |
| 9 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 10 | #include "../Utils.hpp" |
| 11 | |
| 12 | #include <fstream> |
| 13 | #include <iomanip> |
| 14 | #include <boost/format.hpp> |
| 15 | #include <armnn/INetwork.hpp> |
| 16 | |
| 17 | BOOST_AUTO_TEST_SUITE(UtilsTests) |
| 18 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 19 | using namespace android; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 20 | using namespace android::nn; |
| 21 | using namespace android::hardware; |
| 22 | using namespace armnn_driver; |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 23 | |
| 24 | // The following are helpers for writing unit tests for the driver. |
| 25 | namespace |
| 26 | { |
| 27 | |
| 28 | struct ExportNetworkGraphFixture |
| 29 | { |
| 30 | public: |
| 31 | // Setup: set the output dump directory and an empty dummy model (as only its memory address is used). |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 32 | // Defaulting the output dump directory to "/data" because it should exist and be writable in all deployments. |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 33 | ExportNetworkGraphFixture() |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 34 | : ExportNetworkGraphFixture("/data") |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 35 | {} |
| 36 | ExportNetworkGraphFixture(const std::string& requestInputsAndOutputsDumpDir) |
| 37 | : m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir) |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 38 | , m_FileName() |
| 39 | , m_FileStream() |
| 40 | { |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 41 | // Set the name of the output .dot file. |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 42 | // 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") |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 46 | % m_RequestInputsAndOutputsDumpDir |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 47 | % timestamp); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 48 | } |
| 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). |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 57 | (void)remove(m_FileName.c_str()); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 58 | } |
| 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 Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 68 | if (m_FileName.empty()) |
| 69 | { |
| 70 | return false; |
| 71 | } |
| 72 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 73 | // 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 Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 100 | std::string m_FileName; |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 101 | |
| 102 | private: |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 103 | std::ifstream m_FileStream; |
| 104 | }; |
| 105 | |
| 106 | class MockOptimizedNetwork final : public armnn::IOptimizedNetwork |
| 107 | { |
| 108 | public: |
| 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 | |
janeil01 | f76a876 | 2019-11-06 12:44:26 +0000 | [diff] [blame] | 122 | armnn::profiling::ProfilingGuid GetGuid() const final { return armnn::profiling::ProfilingGuid(0); } |
| 123 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 124 | void UpdateMockSerializedContent(const std::string& mockSerializedContent) |
| 125 | { |
| 126 | this->m_MockSerializedContent = mockSerializedContent; |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | std::string m_MockSerializedContent; |
| 131 | }; |
| 132 | |
| 133 | } // namespace |
| 134 | |
| 135 | BOOST_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 Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 147 | fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
| 148 | fixture.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 149 | |
| 150 | // Check that the output file does not exist. |
| 151 | BOOST_TEST(!fixture.FileExists()); |
| 152 | } |
| 153 | |
| 154 | BOOST_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 Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 166 | fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
| 167 | fixture.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 168 | |
| 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 | |
| 176 | BOOST_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 Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 188 | fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
| 189 | fixture.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 190 | |
| 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 Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 202 | fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
| 203 | fixture.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 204 | |
| 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 | |
| 212 | BOOST_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 Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 226 | fixture1.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
| 227 | fixture1.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 228 | |
| 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 Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 236 | fixture2.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
| 237 | fixture2.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 238 | |
| 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 Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 246 | fixture3.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
| 247 | fixture3.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 248 | // 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 | |
| 255 | BOOST_AUTO_TEST_SUITE_END() |