aboutsummaryrefslogtreecommitdiff
path: root/tests/ExecuteNetwork/ExecuteNetwork.cpp
blob: 9f81eb1168e46c1cf11a600dd0b3ce3b4646d018 (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
//
// Copyright © 2022-2023 Arm Ltd and Contributors. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "ArmNNExecutor.hpp"
#include "ExecuteNetworkProgramOptions.hpp"
#if defined(ARMNN_TFLITE_DELEGATE) || defined(ARMNN_TFLITE_OPAQUE_DELEGATE)
#include "TfliteExecutor.hpp"
#endif
#include "FileComparisonExecutor.hpp"
#include <armnn/Logging.hpp>

std::unique_ptr<IExecutor> BuildExecutor(ProgramOptions& programOptions)
{
    if (programOptions.m_ExNetParams.m_TfLiteExecutor ==
            ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteOpaqueDelegate ||
        programOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate ||
        programOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::TfliteInterpreter)
    {
#if defined(ARMNN_TFLITE_DELEGATE) || defined(ARMNN_TFLITE_OPAQUE_DELEGATE)
        return std::make_unique<TfLiteExecutor>(programOptions.m_ExNetParams, programOptions.m_RuntimeOptions);
#else
        ARMNN_LOG(fatal) << "Not built with Arm NN Tensorflow-Lite delegate support.";
        return nullptr;
#endif
    }
    else
    {
        return std::make_unique<ArmNNExecutor>(programOptions.m_ExNetParams, programOptions.m_RuntimeOptions);
    }
}

// MAIN
int main(int argc, const char* argv[])
{
    // Configures logging for both the ARMNN library and this test program.
#ifdef NDEBUG
    armnn::LogSeverity level = armnn::LogSeverity::Info;
#else
    armnn::LogSeverity level = armnn::LogSeverity::Debug;
#endif
    armnn::ConfigureLogging(true, true, level);

    // Get ExecuteNetwork parameters and runtime options from command line
    // This might throw an InvalidArgumentException if the user provided invalid inputs
    ProgramOptions programOptions;
    try
    {
        programOptions.ParseOptions(argc, argv);
    }
    catch (const std::exception& e)
    {
        ARMNN_LOG(fatal) << e.what();
        return EXIT_FAILURE;
    }

    std::vector<const void*> outputResults;
    std::unique_ptr<IExecutor> executor;
    try
    {
        executor = BuildExecutor(programOptions);
        if ((!executor) || (executor->m_constructionFailed))
        {
            return EXIT_FAILURE;
        }
    }
    catch (const std::exception& e)
    {
        ARMNN_LOG(fatal) << e.what();
        return EXIT_FAILURE;
    }

    executor->PrintNetworkInfo();
    outputResults = executor->Execute();

    if (!programOptions.m_ExNetParams.m_ComparisonComputeDevices.empty() ||
        programOptions.m_ExNetParams.m_CompareWithTflite)
    {
        ExecuteNetworkParams comparisonParams = programOptions.m_ExNetParams;
        comparisonParams.m_ComputeDevices     = programOptions.m_ExNetParams.m_ComparisonComputeDevices;

        if (programOptions.m_ExNetParams.m_CompareWithTflite)
        {
            comparisonParams.m_TfLiteExecutor = ExecuteNetworkParams::TfLiteExecutor::TfliteInterpreter;
        }

        auto comparisonExecutor = BuildExecutor(programOptions);

        if (!comparisonExecutor)
        {
            return EXIT_FAILURE;
        }

        comparisonExecutor->PrintNetworkInfo();
        comparisonExecutor->Execute();

        comparisonExecutor->CompareAndPrintResult(outputResults);
    }

    // If there's a file comparison specified create a FileComparisonExecutor.
    if (!programOptions.m_ExNetParams.m_ComparisonFile.empty())
    {
        FileComparisonExecutor comparisonExecutor(programOptions.m_ExNetParams);
        comparisonExecutor.Execute();
        comparisonExecutor.CompareAndPrintResult(outputResults);
    }
}