aboutsummaryrefslogtreecommitdiff
path: root/src/armnnQuantizer/CommandLineProcessor.cpp
blob: 74ad5e7dbc030aad39da7fd6c05b2b8cdff58561 (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
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//

#include "CommandLineProcessor.hpp"
#include <Filesystem.hpp>

#include <cxxopts/cxxopts.hpp>

namespace armnnQuantizer
{

bool ValidateOutputDirectory(std::string& dir)
{
    if (dir.empty())
    {
        std::cerr << "No output directory specified" << std::endl;
        return false;
    }

    if (dir[dir.length() - 1] != '/')
    {
        dir += "/";
    }

    if (!fs::exists(dir))
    {
        std::cerr << "Output directory [" << dir << "] does not exist" << std::endl;
        return false;
    }

    if (!fs::is_directory(dir))
    {
        std::cerr << "Given output directory [" << dir << "] is not a directory" << std::endl;
        return false;
    }

    return true;
}

bool ValidateProvidedFile(const std::string& inputFileName)
{
    if (!fs::exists(inputFileName))
    {
        std::cerr << "Provided file [" << inputFileName << "] does not exist" << std::endl;
        return false;
    }

    if (fs::is_directory(inputFileName))
    {
        std::cerr << "Given file [" << inputFileName << "] is a directory" << std::endl;
        return false;
    }

    return true;
}

bool ValidateQuantizationScheme(const std::string& scheme)
{
    if (scheme.empty())
    {
        std::cerr << "No Quantization Scheme specified" << std::endl;
        return false;
    }

    std::vector<std::string> supportedSchemes =
    {
        "QAsymmS8",
        "QAsymmU8",
        "QSymm16"
    };

    auto iterator = std::find(supportedSchemes.begin(), supportedSchemes.end(), scheme);
    if (iterator == supportedSchemes.end())
    {
        std::cerr << "Quantization Scheme [" << scheme << "] is not supported" << std::endl;
        return false;
    }

    return true;
}

bool CommandLineProcessor::ProcessCommandLine(int argc, char* argv[])
{
    try
    {
        cxxopts::Options options("ArmnnQuantizer","Convert a Fp32 ArmNN model to a quantized ArmNN model.");

        options.add_options()
            ("h,help", "Display help messages")
            ("f,infile",
                "Input file containing float 32 ArmNN Input Graph",
                cxxopts::value<std::string>(m_InputFileName))
            ("s,scheme",
                "Quantization scheme,"
                " \"QAsymmU8\" or \"QAsymmS8\" or \"QSymm16\","
                " default value QAsymmU8",
                cxxopts::value<std::string>(m_QuantizationScheme)->default_value("QAsymmU8"))
            ("c,csvfile",
                "CSV file containing paths for RAW input tensors",
                cxxopts::value<std::string>(m_CsvFileName)->default_value(""))
            ("p,preserve-data-type",
                "Preserve the input and output data types",
                cxxopts::value<bool>(m_PreserveDataType)->default_value("false"))
            ("d,outdir",
                "Directory that output file will be written to",
                cxxopts::value<std::string>(m_OutputDirectory))
            ("o,outfile",
                "ArmNN output file name",
                cxxopts::value<std::string>(m_OutputFileName));

        auto result = options.parse(argc, argv);

        if (result.count("help") > 0 || argc <= 1)
        {
            std::cout << options.help() << std::endl;
            return false;
        }

        // Check for mandatory single options.
        std::string mandatorySingleParameters[] = { "infile", "outdir", "outfile" };
        for (auto param : mandatorySingleParameters)
        {
            if (result.count(param) != 1)
            {
                std::cerr << "Parameter \'--" << param << "\' is required but missing." << std::endl;
                return false;
            }
        }
    }
    catch (const cxxopts::OptionException& e)
    {
        std::cerr << e.what() << std::endl << std::endl;
        return false;
    }
    catch (const std::exception& e)
    {
        std::cerr << "Fatal internal error: [" << e.what() << "]" << std::endl;
        return false;
    }

    if (!armnnQuantizer::ValidateProvidedFile(m_InputFileName))
    {
        return false;
    }

    if (!ValidateQuantizationScheme(m_QuantizationScheme))
    {
        return false;
    }

    if (m_CsvFileName != "")
    {
        if (!armnnQuantizer::ValidateProvidedFile(m_CsvFileName))
        {
            return false;
        }
        else
        {
            fs::path csvFilePath(m_CsvFileName);
            m_CsvFileDirectory = csvFilePath.parent_path().c_str();
        }

        // If CSV file is defined, create a QuantizationDataSet for specified CSV file.
        m_QuantizationDataSet = QuantizationDataSet(m_CsvFileName);
    }

    if (!armnnQuantizer::ValidateOutputDirectory(m_OutputDirectory))
    {
        return false;
    }

    std::string output(m_OutputDirectory);
    output.append(m_OutputFileName);

    if (fs::exists(output))
    {
        std::cerr << "Output file [" << output << "] already exists" << std::endl;
        return false;
    }

    return true;
}

} // namespace armnnQuantizer