surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. 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 <log/log.h> |
| 8 | |
Francis Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 9 | #include <armnn/src/armnn/OptimizedNetworkImpl.hpp> |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 10 | |
| 11 | #include <fstream> |
Francis Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 12 | #include <memory> |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 13 | #include <armnn/INetwork.hpp> |
| 14 | |
Rob Hughes | 083be70 | 2021-07-19 15:29:47 +0100 | [diff] [blame] | 15 | #include <armnnUtils/Filesystem.hpp> |
Colm Donelan | 08d9a1c | 2020-09-09 17:56:55 +0100 | [diff] [blame] | 16 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 17 | using namespace android; |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 18 | using namespace android::nn; |
| 19 | using namespace android::hardware; |
| 20 | using namespace armnn_driver; |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 21 | |
Francis Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 22 | namespace armnn |
| 23 | { |
| 24 | |
| 25 | class Graph |
| 26 | { |
| 27 | public: |
| 28 | Graph(Graph&& graph) = default; |
| 29 | }; |
| 30 | |
| 31 | class MockOptimizedNetworkImpl final : public ::armnn::OptimizedNetworkImpl |
| 32 | { |
| 33 | public: |
| 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 Corbett | f45e3ab | 2022-02-28 10:06:26 +0000 | [diff] [blame] | 48 | ::arm::pipe::ProfilingGuid GetGuid() const final { return ::arm::pipe::ProfilingGuid(0); } |
Francis Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 49 | |
| 50 | void UpdateMockSerializedContent(const std::string& mockSerializedContent) |
| 51 | { |
| 52 | this->m_MockSerializedContent = mockSerializedContent; |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | std::string m_MockSerializedContent; |
| 57 | }; |
| 58 | |
| 59 | |
| 60 | } // armnn namespace |
| 61 | |
Francis Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 62 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 63 | // The following are helpers for writing unit tests for the driver. |
| 64 | namespace |
| 65 | { |
| 66 | |
| 67 | struct ExportNetworkGraphFixture |
| 68 | { |
| 69 | public: |
| 70 | // 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] | 71 | // 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] | 72 | ExportNetworkGraphFixture() |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 73 | : ExportNetworkGraphFixture("/data") |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 74 | {} |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 75 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 76 | ExportNetworkGraphFixture(const std::string& requestInputsAndOutputsDumpDir) |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 77 | : m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir), m_FileName(), m_FileStream() |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 78 | { |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 79 | // Set the name of the output .dot file. |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 80 | // 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 Donelan | 08d9a1c | 2020-09-09 17:56:55 +0100 | [diff] [blame] | 83 | m_FileName = m_RequestInputsAndOutputsDumpDir / (timestamp + "_networkgraph.dot"); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 84 | } |
| 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 Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 93 | (void) remove(m_FileName.c_str()); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 94 | } |
| 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 Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 104 | if (m_FileName.empty()) |
| 105 | { |
| 106 | return false; |
| 107 | } |
| 108 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 109 | // 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 Donelan | 08d9a1c | 2020-09-09 17:56:55 +0100 | [diff] [blame] | 135 | fs::path m_RequestInputsAndOutputsDumpDir; |
| 136 | fs::path m_FileName; |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 137 | |
| 138 | private: |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 139 | std::ifstream m_FileStream; |
| 140 | }; |
| 141 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 142 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 143 | } // namespace |
| 144 | |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 145 | DOCTEST_TEST_SUITE("UtilsTests") |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 146 | { |
| 147 | |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 148 | DOCTEST_TEST_CASE("ExportToEmptyDirectory") |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 149 | { |
| 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 Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 157 | 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)); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 162 | |
| 163 | // Export the mock optimized network. |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 164 | fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 165 | fixture.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 166 | |
| 167 | // Check that the output file does not exist. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 168 | DOCTEST_CHECK(!fixture.FileExists()); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 171 | DOCTEST_TEST_CASE("ExportNetwork") |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 172 | { |
| 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 Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 180 | 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 | |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 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, |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 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. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 192 | DOCTEST_CHECK(fixture.FileExists()); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 193 | |
| 194 | // Check that the content of the output file matches the mock content. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 195 | DOCTEST_CHECK(fixture.GetFileContent() == mockSerializedContent); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 198 | DOCTEST_TEST_CASE("ExportNetworkOverwriteFile") |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 199 | { |
| 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 Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 207 | 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)); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 212 | |
| 213 | // Export the mock optimized network. |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 214 | fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 215 | fixture.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 216 | |
| 217 | // Check that the output file exists and that it has the correct name. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 218 | DOCTEST_CHECK(fixture.FileExists()); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 219 | |
| 220 | // Check that the content of the output file matches the mock content. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 221 | DOCTEST_CHECK(fixture.GetFileContent() == mockSerializedContent); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 222 | |
| 223 | // Update the mock serialized content of the network. |
| 224 | mockSerializedContent = "This is ANOTHER mock serialized content!"; |
Francis Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 225 | 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)); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 230 | |
| 231 | // Export the mock optimized network. |
Francis Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 232 | fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork2, |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 233 | fixture.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 234 | |
| 235 | // Check that the output file still exists and that it has the correct name. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 236 | DOCTEST_CHECK(fixture.FileExists()); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 237 | |
| 238 | // Check that the content of the output file matches the mock content. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 239 | DOCTEST_CHECK(fixture.GetFileContent() == mockSerializedContent); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 240 | } |
| 241 | |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 242 | DOCTEST_TEST_CASE("ExportMultipleNetworks") |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 243 | { |
| 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 Murtagh | d523296 | 2021-02-12 15:01:22 +0000 | [diff] [blame] | 253 | 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)); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 258 | |
| 259 | // Export the mock optimized network. |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 260 | fixture1.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 261 | fixture1.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 262 | |
| 263 | // Check that the output file exists and that it has the correct name. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 264 | DOCTEST_CHECK(fixture1.FileExists()); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 265 | |
| 266 | // Check that the content of the output file matches the mock content. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 267 | DOCTEST_CHECK(fixture1.GetFileContent() == mockSerializedContent); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 268 | |
| 269 | // Export the mock optimized network. |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 270 | fixture2.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 271 | fixture2.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 272 | |
| 273 | // Check that the output file exists and that it has the correct name. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 274 | DOCTEST_CHECK(fixture2.FileExists()); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 275 | |
| 276 | // Check that the content of the output file matches the mock content. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 277 | DOCTEST_CHECK(fixture2.GetFileContent() == mockSerializedContent); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 278 | |
| 279 | // Export the mock optimized network. |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 280 | fixture3.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork, |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 281 | fixture3.m_RequestInputsAndOutputsDumpDir); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 282 | // Check that the output file exists and that it has the correct name. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 283 | DOCTEST_CHECK(fixture3.FileExists()); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 284 | |
| 285 | // Check that the content of the output file matches the mock content. |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 286 | DOCTEST_CHECK(fixture3.GetFileContent() == mockSerializedContent); |
surmeh01 | 7666005 | 2018-03-29 16:33:54 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Sadik Armagan | 9150bff | 2021-05-26 15:40:53 +0100 | [diff] [blame] | 289 | } |