aboutsummaryrefslogtreecommitdiff
path: root/test/UtilsTests.cpp
blob: de84bb4999a42750da079c57f51d6e77388f5f15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "DriverTestHelpers.hpp"
#include <boost/test/unit_test.hpp>
#include <log/log.h>

#include "../Utils.hpp"
#include <armnn/src/armnn/OptimizedNetworkImpl.hpp>

#include <fstream>
#include <iomanip>
#include <memory>
#include <armnn/INetwork.hpp>
#include "armnn/NetworkFwd.hpp"

#include <Filesystem.hpp>


using namespace android;
using namespace android::nn;
using namespace android::hardware;
using namespace armnn_driver;

namespace armnn
{

class Graph
{
public:
    Graph(Graph&& graph) = default;
};

class MockOptimizedNetworkImpl final : public ::armnn::OptimizedNetworkImpl
{
public:
    MockOptimizedNetworkImpl(const std::string& mockSerializedContent, std::unique_ptr<armnn::Graph>)
        : ::armnn::OptimizedNetworkImpl(nullptr)
        , m_MockSerializedContent(mockSerializedContent)
    {}
    ~MockOptimizedNetworkImpl() {}

    ::armnn::Status PrintGraph() override { return ::armnn::Status::Failure; }
    ::armnn::Status SerializeToDot(std::ostream& stream) const override
    {
        stream << m_MockSerializedContent;

        return stream.good() ? ::armnn::Status::Success : ::armnn::Status::Failure;
    }

    ::armnn::profiling::ProfilingGuid GetGuid() const final { return ::armnn::profiling::ProfilingGuid(0); }

    void UpdateMockSerializedContent(const std::string& mockSerializedContent)
    {
        this->m_MockSerializedContent = mockSerializedContent;
    }

private:
    std::string m_MockSerializedContent;
};


} // armnn namespace

BOOST_AUTO_TEST_SUITE(UtilsTests)

// The following are helpers for writing unit tests for the driver.
namespace
{

struct ExportNetworkGraphFixture
{
public:
    // Setup: set the output dump directory and an empty dummy model (as only its memory address is used).
    // Defaulting the output dump directory to "/data" because it should exist and be writable in all deployments.
    ExportNetworkGraphFixture()
        : ExportNetworkGraphFixture("/data")
    {}
    ExportNetworkGraphFixture(const std::string& requestInputsAndOutputsDumpDir)
        : m_RequestInputsAndOutputsDumpDir(requestInputsAndOutputsDumpDir)
        , m_FileName()
        , m_FileStream()
    {
        // Set the name of the output .dot file.
        // NOTE: the export now uses a time stamp to name the file so we
        //       can't predict ahead of time what the file name will be.
        std::string timestamp = "dummy";
        m_FileName = m_RequestInputsAndOutputsDumpDir / (timestamp + "_networkgraph.dot");
    }

    // Teardown: delete the dump file regardless of the outcome of the tests.
    ~ExportNetworkGraphFixture()
    {
        // Close the file stream.
        m_FileStream.close();

        // Ignore any error (such as file not found).
        (void)remove(m_FileName.c_str());
    }

    bool FileExists()
    {
        // Close any file opened in a previous session.
        if (m_FileStream.is_open())
        {
            m_FileStream.close();
        }

        if (m_FileName.empty())
        {
            return false;
        }

        // Open the file.
        m_FileStream.open(m_FileName, std::ifstream::in);

        // Check that the file is open.
        if (!m_FileStream.is_open())
        {
            return false;
        }

        // Check that the stream is readable.
        return m_FileStream.good();
    }

    std::string GetFileContent()
    {
        // Check that the stream is readable.
        if (!m_FileStream.good())
        {
            return "";
        }

        // Get all the contents of the file.
        return std::string((std::istreambuf_iterator<char>(m_FileStream)),
                           (std::istreambuf_iterator<char>()));
    }

    fs::path m_RequestInputsAndOutputsDumpDir;
    fs::path m_FileName;

private:
    std::ifstream m_FileStream;
};



} // namespace

BOOST_AUTO_TEST_CASE(ExportToEmptyDirectory)
{
    // Set the fixture for this test.
    ExportNetworkGraphFixture fixture("");

    // Set a mock content for the optimized network.
    std::string mockSerializedContent = "This is a mock serialized content.";

    // Set a mock optimized network.
    std::unique_ptr<armnn::Graph> graphPtr;

    std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
        new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
    ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));

    // Export the mock optimized network.
    fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
                                              fixture.m_RequestInputsAndOutputsDumpDir);

    // Check that the output file does not exist.
    BOOST_TEST(!fixture.FileExists());
}

BOOST_AUTO_TEST_CASE(ExportNetwork)
{
    // Set the fixture for this test.
    ExportNetworkGraphFixture fixture;

    // Set a mock content for the optimized network.
    std::string mockSerializedContent = "This is a mock serialized content.";

    // Set a mock optimized network.
    std::unique_ptr<armnn::Graph> graphPtr;

    std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
        new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
    ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));


    // Export the mock optimized network.
    fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
                                              fixture.m_RequestInputsAndOutputsDumpDir);

    // Check that the output file exists and that it has the correct name.
    BOOST_TEST(fixture.FileExists());

    // Check that the content of the output file matches the mock content.
    BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
}

BOOST_AUTO_TEST_CASE(ExportNetworkOverwriteFile)
{
    // Set the fixture for this test.
    ExportNetworkGraphFixture fixture;

    // Set a mock content for the optimized network.
    std::string mockSerializedContent = "This is a mock serialized content.";

    // Set a mock optimized network.
    std::unique_ptr<armnn::Graph> graphPtr;

    std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
        new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
    ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));

    // Export the mock optimized network.
    fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
                                              fixture.m_RequestInputsAndOutputsDumpDir);

    // Check that the output file exists and that it has the correct name.
    BOOST_TEST(fixture.FileExists());

    // Check that the content of the output file matches the mock content.
    BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);

    // Update the mock serialized content of the network.
    mockSerializedContent = "This is ANOTHER mock serialized content!";
    std::unique_ptr<armnn::Graph> graphPtr2;
    std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl2(
        new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr2)));
    static_cast<armnn::MockOptimizedNetworkImpl*>(mockImpl2.get())->UpdateMockSerializedContent(mockSerializedContent);
    ::armnn::IOptimizedNetwork mockOptimizedNetwork2(std::move(mockImpl2));

    // Export the mock optimized network.
    fixture.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork2,
                                              fixture.m_RequestInputsAndOutputsDumpDir);

    // Check that the output file still exists and that it has the correct name.
    BOOST_TEST(fixture.FileExists());

    // Check that the content of the output file matches the mock content.
    BOOST_TEST(fixture.GetFileContent() == mockSerializedContent);
}

BOOST_AUTO_TEST_CASE(ExportMultipleNetworks)
{
    // Set the fixtures for this test.
    ExportNetworkGraphFixture fixture1;
    ExportNetworkGraphFixture fixture2;
    ExportNetworkGraphFixture fixture3;

    // Set a mock content for the optimized network.
    std::string mockSerializedContent = "This is a mock serialized content.";

    // Set a mock optimized network.
    std::unique_ptr<armnn::Graph> graphPtr;

    std::unique_ptr<::armnn::OptimizedNetworkImpl> mockImpl(
        new armnn::MockOptimizedNetworkImpl(mockSerializedContent, std::move(graphPtr)));
    ::armnn::IOptimizedNetwork mockOptimizedNetwork(std::move(mockImpl));

    // Export the mock optimized network.
    fixture1.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
                                              fixture1.m_RequestInputsAndOutputsDumpDir);

    // Check that the output file exists and that it has the correct name.
    BOOST_TEST(fixture1.FileExists());

    // Check that the content of the output file matches the mock content.
    BOOST_TEST(fixture1.GetFileContent() == mockSerializedContent);

    // Export the mock optimized network.
    fixture2.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
                                              fixture2.m_RequestInputsAndOutputsDumpDir);

    // Check that the output file exists and that it has the correct name.
    BOOST_TEST(fixture2.FileExists());

    // Check that the content of the output file matches the mock content.
    BOOST_TEST(fixture2.GetFileContent() == mockSerializedContent);

    // Export the mock optimized network.
    fixture3.m_FileName = armnn_driver::ExportNetworkGraphToDotFile(mockOptimizedNetwork,
                                              fixture3.m_RequestInputsAndOutputsDumpDir);
    // Check that the output file exists and that it has the correct name.
    BOOST_TEST(fixture3.FileExists());

    // Check that the content of the output file matches the mock content.
    BOOST_TEST(fixture3.GetFileContent() == mockSerializedContent);
}

BOOST_AUTO_TEST_SUITE_END()